-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
0 deletions.
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
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,50 @@ | ||
module NNlibMetalExt | ||
|
||
using Metal, NNlib | ||
using NNlib: AbstractRNG # === Random.AbstractRNG | ||
|
||
# Random | ||
NNlib._rng_from_array(::MtlArray) = Metal.MPS.default_rng() | ||
|
||
NNlib._rng_compat_array(rng::Metal.MPS.RNG, A::MtlArray) = nothing | ||
NNlib._rng_compat_array(rng::AbstractRNG, A::MtlArray) = throw(ArgumentError( | ||
"cannot use rng::$(typeof(rng)) with array::MtlArray, only Metal's own RNG type works")) | ||
|
||
# Batched matrix multiplication | ||
function NNlib._batched_gemm!(::Type{<:MtlArray}, transA::Char, transB::Char, α::Number, A, B, β::Number, C) | ||
eltype(C) <: Complex && @warn "don't trust this on complex arrays!" transA transB | ||
Metal.MPS.matmul!(C, A, B, α, β, transA != 'N', transB != 'N') # transA, transB, α, A, B, β, C) | ||
end | ||
|
||
#= | ||
help?> Metal.MPS.matmul! | ||
matMulMPS(a::MtlMatrix, b::MtlMatrix, c::MtlMatrix, alpha=1, beta=1, | ||
transpose_left=false, transpose_right=false) | ||
A MPSMatrixMultiplication kernel thay computes: c = alpha * op(a) * beta * op(b) + beta * C | ||
This function should not typically be used. Rather, use the normal LinearAlgebra interface with | ||
any MtlArray and it should be accelerated using Metal Performance Shaders. | ||
=# | ||
|
||
using NNlib: BatchedAdjoint, BatchedTranspose, BatchedAdjOrTrans | ||
using Adapt | ||
using Adapt: WrappedArray | ||
|
||
const MetalBatchedAdjoint{T} = BatchedAdjoint{T, <: MtlArray{T}} | ||
const MetalBatchedTranspose{T} = BatchedTranspose{T, <: MtlArray{T}} | ||
const MetalBatchedAdjOrTrans{T} = Union{MetalBatchedAdjoint{T}, MetalBatchedTranspose{T}} | ||
const WrappedMetalBatchedAdjOrTrans{T, N} = WrappedArray{T, N, MetalBatchedAdjOrTrans{T}, MetalBatchedAdjOrTrans{T}} | ||
|
||
Base.print_array(io::IO, b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}) = Base.print_array(io, adapt(Array, b)) | ||
Base._show_nonempty(io::IO, b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}, prefix::String) = Base._show_nonempty(io, adapt(Array, b), prefix) | ||
Base.show_vector(io::IO, b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}, opn, cls) = Base.show_vector(io, adapt(Array, b), opn, cls) | ||
|
||
Base.convert(::Type{T}, b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}) where {T<:Array} = Base.convert(T, adapt(Array, b)) | ||
Base.Array{T, N}(b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}) where {T, N} = Array{T, N}(adapt(Array, b)) | ||
Base.collect(b::Union{MetalBatchedAdjOrTrans, WrappedMetalBatchedAdjOrTrans}) = collect(adapt(Array, b)) | ||
|
||
|
||
end # module NNlibMetalExt |
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,82 @@ | ||
@testset "batched_mul" begin | ||
using NNlib: batched_mul, batched_mul!, batched_vec, | ||
batched_adjoint, batched_transpose | ||
|
||
A = randn(Float32, 3,3,2); | ||
B = randn(Float32, 3,3,2); | ||
|
||
C = batched_mul(A, B) | ||
@test MtlArray(C) ≈ batched_mul(MtlArray(A), MtlArray(B)) | ||
|
||
Ct = batched_mul(batched_transpose(A), B) | ||
@test MtlArray(Ct) ≈ batched_mul(batched_transpose(MtlArray(A)), MtlArray(B)) | ||
|
||
Ca = batched_mul(A, batched_adjoint(B)) | ||
@test MtlArray(Ca) ≈ batched_mul(MtlArray(A), batched_adjoint(MtlArray(B))) | ||
|
||
# 5-arg batched_mul! | ||
C .= pi | ||
batched_mul!(C, A, B, 2f0, 3f0) | ||
gpuCpi = MtlArray(similar(C)) .= pi | ||
@test MtlArray(C) ≈ batched_mul!(gpuCpi, MtlArray(A), MtlArray(B), 2f0, 3f0) | ||
|
||
# PermutedDimsArray | ||
@test MtlArray(Ct) ≈ batched_mul(PermutedDimsArray(MtlArray(A), (2,1,3)), MtlArray(B)) | ||
|
||
D = permutedims(B, (1,3,2)) | ||
Cp = batched_mul(batched_adjoint(A), B) | ||
@test_broken MtlArray(Cp) ≈ batched_mul(batched_adjoint(MtlArray(A)), PermutedDimsArray(MtlArray(D), (1,3,2))) | ||
|
||
# Methods which reshape | ||
M = randn(Float32, 3,3) | ||
|
||
Cm = batched_mul(A, M) | ||
@test MtlArray(Cm) ≈ batched_mul(MtlArray(A), MtlArray(M)) | ||
|
||
Cv = batched_vec(permutedims(A,(3,1,2)), M) | ||
@test_broken MtlArray(Cv) ≈ batched_vec(PermutedDimsArray(MtlArray(A),(3,1,2)), MtlArray(M)) | ||
end | ||
|
||
function print_array_strs(x) | ||
str = sprint((io, x)->show(io, MIME"text/plain"(), x), x) | ||
return @view split(str, '\n')[2:end] | ||
end | ||
|
||
@testset "BatchedAdjOrTrans" begin | ||
x = rand(Float32, 3, 4, 2) | ||
y = MtlArray(x) | ||
|
||
bax = batched_adjoint(x) | ||
btx = batched_transpose(x) | ||
bay = batched_adjoint(y) | ||
bty = batched_transpose(y) | ||
|
||
@test sprint(show, bax) == sprint(show, bay) | ||
@test sprint(show, btx) == sprint(show, bty) | ||
|
||
@test print_array_strs(bax) == print_array_strs(bay) | ||
@test print_array_strs(btx) == print_array_strs(bty) | ||
|
||
@test Array(bax) == Array(bay) | ||
@test collect(bax) == collect(bay) | ||
@test Array(btx) == Array(bty) | ||
@test collect(btx) == collect(bty) | ||
|
||
for shape in (:, (12, 2)) | ||
rbax = reshape(bax, shape) | ||
rbtx = reshape(btx, shape) | ||
rbay = reshape(bay, shape) | ||
rbty = reshape(bty, shape) | ||
|
||
@test sprint(show, rbax) == sprint(show, rbay) | ||
@test sprint(show, rbtx) == sprint(show, rbty) | ||
|
||
@test print_array_strs(rbax) == print_array_strs(rbay) | ||
@test print_array_strs(rbtx) == print_array_strs(rbty) | ||
|
||
@test Array(rbax) == Array(rbay) | ||
@test collect(rbax) == collect(rbay) | ||
@test Array(rbtx) == Array(rbty) | ||
@test collect(rbtx) == collect(rbty) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
Metal.allowscalar(false) | ||
|
||
@testset "Batched multiplication" begin | ||
include("batched_mul.jl") | ||
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