-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implementation for batch-wise matrix multiplication #100
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a8251b
implementation for batch-wise matrix multiplication
Roger-luo 6237de3
port batched transpose type, remove transpose keyword
chengchingwen 2bd6d7e
Add adjoint for complex number
chengchingwen d101848
gradient for adjoint bmm
chengchingwen 5042189
transpose/adjoint 2 time return the same
chengchingwen bbdc647
fix bmm gradient
chengchingwen 4ca7695
remove unnecessary grad func
chengchingwen f6d752f
merge and update
chengchingwen 83e359c
organize file
chengchingwen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using LinearAlgebra | ||
import Base: - | ||
|
||
""" | ||
BatchedTranspose{T, N, S} <: AbstractBatchedMatrix{T, N} | ||
Batched transpose. Transpose a batch of matrix. | ||
""" | ||
struct BatchedTranspose{T, S} <: AbstractArray{T, 3} | ||
parent::S | ||
BatchedTranspose{T, S}(X::S) where {T, S} = new{T, S}(X) | ||
end | ||
|
||
""" | ||
batched_transpose(A) | ||
Lazy batched transpose. | ||
""" | ||
batched_transpose(A::AbstractArray{T}) where T = BatchedTranspose(A) | ||
batched_transpose(A::BatchedTranspose) = A.parent | ||
|
||
""" | ||
BatchedAdjoint{T, N, S} <: AbstractBatchedMatrix{T, N} | ||
Batched ajoint. Transpose a batch of matrix. | ||
""" | ||
struct BatchedAdjoint{T, S} <: AbstractArray{T, 3} | ||
parent::S | ||
BatchedAdjoint{T, S}(X::S) where {T, S} = new{T, S}(X) | ||
end | ||
|
||
""" | ||
batched_adjoint(A) | ||
Lazy batched adjoint. | ||
""" | ||
batched_adjoint(A::AbstractArray{T, 3}) where T = BatchedAdjoint(A) | ||
batched_adjoint(A::BatchedAdjoint) = A.parent | ||
|
||
BatchedAdjoint(A) = BatchedAdjoint{Base.promote_op(adjoint,eltype(A)),typeof(A)}(A) | ||
BatchedTranspose(A) = BatchedTranspose{Base.promote_op(transpose,eltype(A)),typeof(A)}(A) | ||
|
||
|
||
const BatchedAdjOrTrans{T, S} = Union{BatchedTranspose{T, S}, BatchedAdjoint{T, S}} | ||
|
||
LinearAlgebra.wrapperop(A::BatchedAdjoint) = batched_adjoint | ||
LinearAlgebra.wrapperop(B::BatchedTranspose) = batched_transpose | ||
|
||
# AbstractArray Interface | ||
Base.length(A::BatchedAdjOrTrans) = length(A.parent) | ||
Base.size(m::BatchedAdjOrTrans) = (size(m.parent, 2), size(m.parent, 1), size(m.parent, 3)) | ||
Base.axes(m::BatchedAdjOrTrans) = (axes(m.parent, 2), axes(m.parent, 1), axes(m.parent, 3)) | ||
|
||
Base.IndexStyle(::Type{<:BatchedAdjOrTrans}) = IndexCartesian() | ||
Base.@propagate_inbounds Base.getindex(m::BatchedTranspose, i::Int, j::Int, k::Int) = getindex(m.parent, j, i, k) | ||
Base.@propagate_inbounds Base.getindex(m::BatchedAdjoint, i::Int, j::Int, k::Int) = adjoint(getindex(m.parent, j, i, k)) | ||
Base.@propagate_inbounds Base.setindex!(m::BatchedAdjOrTrans, v, i::Int, j::Int, k::Int) = setindex!(m.parent, v, j, i, k) | ||
|
||
Base.similar(A::BatchedAdjOrTrans, T::Type, dims::Dims) = similar(A.parent, T, dims) | ||
Base.similar(A::BatchedAdjOrTrans, dims::Dims) = similar(A.parent, dims) | ||
Base.similar(A::BatchedAdjOrTrans, T::Type) = similar(A.parent, T, size(A)) | ||
Base.similar(A::BatchedAdjOrTrans) = similar(A.parent, size(A)) | ||
|
||
Base.parent(A::BatchedAdjOrTrans) = A.parent | ||
|
||
(-)(A::BatchedAdjoint) = BatchedAdjoint( -A.parent) | ||
(-)(A::BatchedTranspose) = BatchedTranspose(-A.parent) | ||
|
||
Base.copy(A::BatchedTranspose) = BatchedTranspose(copy(A.parent)) | ||
Base.copy(A::BatchedAdjoint) = BatchedAdjoint(copy(A.parent)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# batch-wise matrix multiplication | ||
# wrapper for batched_gemm! | ||
export batched_mul, batched_transpose, batched_adjoint | ||
|
||
|
||
include("./batchedadjtrans.jl") | ||
|
||
function batched_mul(A::AbstractArray{T, 3}, B::AbstractArray{T, 3}) where T | ||
size(A, 3) == size(B, 3) || throw(DimensionMismatch("batch size mismatch")) | ||
batched_mul!(similar(A, (size(A, 1), size(B, 2), size(A, 3))), A, B) | ||
end | ||
|
||
""" | ||
batched_mul!(C, A, B) -> C | ||
batched `mul!`. | ||
""" | ||
function batched_mul! end | ||
|
||
_unbatch(A) = A | ||
_unbatch(A::BatchedAdjOrTrans) = A.parent | ||
|
||
# bmm | ||
const _BATCHED_MATRIX_LIST = [ | ||
(:(AbstractArray{T, 3}), 'N'), | ||
(:(BatchedTranspose{T, <:AbstractArray{T, 3}}), 'T'), | ||
(:(BatchedAdjoint{T, <:AbstractArray{T, 3}}), 'C') | ||
] | ||
|
||
for (TA, transA) in _BATCHED_MATRIX_LIST, (TB, transB) in _BATCHED_MATRIX_LIST | ||
@eval begin | ||
function batched_mul!(C::AbstractArray{T, 3}, A::$TA, B::$TB) where T | ||
batched_gemm!($transA, $transB, one(T), _unbatch(A), _unbatch(B), zero(T), C) | ||
C | ||
end | ||
|
||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
function bmm_test(a,b; transA = false, transB = false) | ||
bs = size(a,3) | ||
transA && (a = permutedims(a, [2,1,3])) | ||
transB && (b = permutedims(b, [2,1,3])) | ||
c = [] | ||
for i = 1:bs | ||
push!(c, a[:,:,i]*b[:,:,i]) | ||
end | ||
|
||
cat(c...; dims = 3) | ||
end | ||
|
||
function bmm_adjtest(a,b; adjA = false, adjB = false) | ||
bs = size(a,3) | ||
c = [] | ||
for i = 1:bs | ||
ai = adjA ? adjoint(a[:,:,i]) : a[:,:,i] | ||
bi = adjB ? adjoint(b[:,:,i]) : b[:,:,i] | ||
push!(c, ai*bi) | ||
end | ||
|
||
cat(c...; dims = 3) | ||
end | ||
|
||
@testset "Batched Matrix Multiplication" begin | ||
A = randn(7,5,3) | ||
B = randn(5,7,3) | ||
C = randn(7,6,3) | ||
|
||
@test batched_mul(A, B) == bmm_test(A, B) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add some tests for |
||
@test batched_mul(batched_transpose(A), batched_transpose(B)) == bmm_test(A, B; transA = true, transB = true) | ||
@test batched_mul(batched_transpose(A), C) == bmm_test(A, C; transA = true) | ||
@test batched_mul(A, batched_transpose(A)) == bmm_test(A, A; transB = true) | ||
|
||
|
||
cA = randn(Complex{Float64}, 7,5,3) | ||
cB = randn(Complex{Float64}, 5,7,3) | ||
cC = randn(Complex{Float64}, 7,6,3) | ||
|
||
@test batched_mul(cA, cB) == bmm_adjtest(cA, cB) | ||
@test batched_mul(batched_adjoint(cA), batched_adjoint(cB)) == bmm_adjtest(cA, cB; adjA = true, adjB = true) | ||
@test batched_mul(batched_adjoint(cA), cC) == bmm_adjtest(cA, cC; adjA = true) | ||
@test batched_mul(cA, batched_adjoint(cA)) == bmm_adjtest(cA, cA; adjB = true) | ||
|
||
@test batched_transpose(batched_transpose(A)) == A | ||
@test batched_adjoint(batched_adjoint(cA)) == cA | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a docstring here?