-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add AMDGPU support * remove patch noises * use gpuarrays ext * rework gemm interface * update test for switching gpu backend --------- Co-authored-by: Radu Diaconu <[email protected]>
- Loading branch information
1 parent
006a5be
commit e7f47be
Showing
11 changed files
with
267 additions
and
422 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
56 changes: 56 additions & 0 deletions
56
ext/NeuralAttentionlibAMDGPUExt/NeuralAttentionlibAMDGPUExt.jl
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,56 @@ | ||
module NeuralAttentionlibAMDGPUExt | ||
|
||
using NeuralAttentionlib | ||
using NeuralAttentionlib.Adapt | ||
using NeuralAttentionlib: TypedPtr, AbstractArrayMask, Indexer, GetIndexer | ||
using AMDGPU | ||
|
||
import LinearAlgebra | ||
import LinearAlgebra.BLAS | ||
using LinearAlgebra.BLAS: get_num_threads, set_num_threads | ||
|
||
const NAlib = NeuralAttentionlib | ||
|
||
import AMDGPU.rocBLAS | ||
for (fname, elty) in | ||
((:rocblas_dgemm_strided_batched, :Float64), | ||
(:rocblas_sgemm_strided_batched, :Float32), | ||
(:rocblas_hgemm_strided_batched, :Float16), | ||
(:rocblas_zgemm_strided_batched, :ComplexF64), | ||
(:rocblas_cgemm_strided_batched, :ComplexF32)) | ||
@eval begin | ||
@inline function NeuralAttentionlib.unsafe_gemm_strided_batched!( | ||
transA::Char, transB::Char, | ||
m::Integer, n::Integer, k::Integer, | ||
alpha::($elty), tptrA::TypedPtr{ROCArray, $elty}, lda::Integer, strideA::Integer, | ||
tptrB::TypedPtr{ROCArray, $elty}, ldb::Integer, strideB::Integer, beta::($elty), | ||
tptrC::TypedPtr{ROCArray, $elty}, ldc::Integer, strideC::Integer, batchCount::Integer) | ||
|
||
ptrA = tptrA.ptr | ||
ptrB = tptrB.ptr | ||
ptrC = tptrC.ptr | ||
rocBLAS.$fname(rocBLAS.handle(), | ||
transA, transB, m, n, k, | ||
alpha, ptrA, lda, strideA, | ||
ptrB, ldb, strideB, beta, | ||
ptrC, ldc, strideC, batchCount) | ||
return nothing | ||
end | ||
end | ||
end | ||
|
||
NeuralAttentionlib.ptrtypetag(::AMDGPU.ROCArrayBackend) = ROCArray | ||
NeuralAttentionlib.check_strided_gemm_type(A::ROCArray{Float16}) = true | ||
|
||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::AbstractArrayMask) = | ||
Indexer{typeof(m)}(map(Base.Fix1(Adapt.adapt, to), GetIndexer(m).__fields)) | ||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::NAlib.FlipMask) = Indexer{typeof(m)}((mask = adapt(to, m.mask),)) | ||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::NAlib.CombinedMask) = | ||
Indexer{typeof(m)}((f = adapt(to, m.f), masks = map(Base.Fix1(adapt, to), m.masks))) | ||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::NAlib.BatchedMask) = | ||
Indexer{typeof(m)}((mask = adapt(to, m.mask), batch_dim = static(m.batch_dim))) | ||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::NAlib.RepeatMask) = Indexer{typeof(m)}((mask = adapt(to, m.mask), num = m.num)) | ||
Adapt.adapt(to::AMDGPU.Runtime.Adaptor, m::NAlib.BiSequenceMask) = | ||
Indexer{typeof(m)}((q_mask = adapt(to, m.q_mask), k_mask = adapt(to, m.k_mask))) | ||
|
||
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
2 changes: 1 addition & 1 deletion
2
...es/NeuralAttentionlibFiniteDifferences.jl → ...NeuralAttentionlibFiniteDifferencesExt.jl
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
22 changes: 22 additions & 0 deletions
22
ext/NeuralAttentionlibGPUArraysExt/NeuralAttentionlibGPUArraysExt.jl
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,22 @@ | ||
module NeuralAttentionlibGPUArraysExt | ||
|
||
using NeuralAttentionlib | ||
using NeuralAttentionlib: CollapsedDimsArray | ||
using GPUArrays | ||
using GPUArrays.GPUArraysCore | ||
|
||
GPUArraysCore.backend(::Type{<:CollapsedDimsArray{E, A}}) where {E, A} = GPUArraysCore.backend(A) | ||
|
||
NeuralAttentionlib.ptrtypetag(arr::AnyGPUArray) = NeuralAttentionlib.ptrtypetag(GPUArraysCore.backend(arr)) | ||
|
||
function NeuralAttentionlib.batched_transpose_f!(f, B::AnyGPUArray{T, 3}, A::AnyGPUArray{T, 3}) where T | ||
axes(B,1) == axes(A,2) && axes(B,2) == axes(A,1) && axes(A,3) == axes(B,3) || throw(DimensionMismatch(string(f))) | ||
GPUArrays.gpu_call(B, A) do ctx, B, A | ||
idx = GPUArrays.@cartesianidx A | ||
@inbounds B[idx[2], idx[1], idx[3]] = f(A[idx[1], idx[2], idx[3]]) | ||
return | ||
end | ||
return B | ||
end | ||
|
||
end |
Oops, something went wrong.
e7f47be
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.
@JuliaRegistrator register()
e7f47be
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.
Registration pull request created: JuliaRegistries/General/105935
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: