-
Notifications
You must be signed in to change notification settings - Fork 69
Add GPU support for Strided backends #264
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bd7fb5
simplify strided implementations
lkdvos 1fd99ac
add JLArrays extension
lkdvos 3a6bc39
add CUDA extension
lkdvos 06e6294
add testing infrastructure
lkdvos 2624038
Update Project.toml
kshyatt d5b08a0
incremental
kshyatt f66bdee
Update test file
kshyatt 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 hidden or 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 hidden or 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,57 @@ | ||
| module TensorOperationsCUDACoreExt | ||
|
|
||
| using CUDACore | ||
| using TensorOperations | ||
| using TensorOperations: TensorOperations as TO | ||
|
|
||
| #------------------------------------------------------------------------------------------- | ||
| # Allocator | ||
| #------------------------------------------------------------------------------------------- | ||
|
|
||
| TO.tensoradd_type(TC, A::CuArray, pA::Index2Tuple, conjA::Bool) = | ||
| CuArray{TC, TO.numind(pA)} | ||
|
|
||
| function TO.CUDAAllocator() | ||
| Mout = CUDACore.UnifiedMemory | ||
| Min = CUDACore.default_memory | ||
| Mtemp = CUDACore.default_memory | ||
| return TO.CUDAAllocator{Mout, Min, Mtemp}() | ||
| end | ||
|
|
||
| function TO.tensoralloc_add( | ||
| TC, A::AbstractArray, pA::Index2Tuple, conjA::Bool, | ||
| istemp::Val, allocator::TO.CUDAAllocator | ||
| ) | ||
| ttype = CuArray{TC, TO.numind(pA)} | ||
| structure = TO.tensoradd_structure(A, pA, conjA) | ||
| return TO.tensoralloc(ttype, structure, istemp, allocator)::ttype | ||
| end | ||
|
|
||
| function TO.tensoralloc_contract( | ||
| TC, | ||
| A::AbstractArray, pA::Index2Tuple, conjA::Bool, | ||
| B::AbstractArray, pB::Index2Tuple, conjB::Bool, | ||
| pAB::Index2Tuple, | ||
| istemp::Val, allocator::TO.CUDAAllocator | ||
| ) | ||
| ttype = CuArray{TC, TO.numind(pAB)} | ||
| structure = TO.tensorcontract_structure(A, pA, conjA, B, pB, conjB, pAB) | ||
| return TO.tensoralloc(ttype, structure, istemp, allocator)::ttype | ||
| end | ||
|
|
||
| # NOTE: the general implementation in the `DefaultAllocator` case works just fine, without | ||
| # selecting an explicit memory model | ||
| function TO.tensoralloc( | ||
| ::Type{CuArray{T, N}}, structure, | ||
| ::Val{istemp}, allocator::TO.CUDAAllocator{Mout, Min, Mtemp} | ||
| ) where {T, N, istemp, Mout, Min, Mtemp} | ||
| M = istemp ? Mtemp : Mout | ||
| return CuArray{T, N, M}(undef, structure) | ||
| end | ||
|
|
||
| function TO.tensorfree!(C::CuArray, ::TO.CUDAAllocator) | ||
| CUDACore.unsafe_free!(C) | ||
| return nothing | ||
| end | ||
|
|
||
| end |
This file contains hidden or 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,9 @@ | ||
| module TensorOperationsJLArraysExt | ||
|
|
||
| using JLArrays | ||
| using TensorOperations | ||
|
|
||
| TensorOperations.tensoradd_type(TC, A::JLArray, pA::Index2Tuple, conjA::Bool) = | ||
| JLArray{TC, sum(length.(pA))} | ||
|
|
||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,124 @@ | ||
| using TensorOperations | ||
| using TensorOperations: StridedBLAS, StridedNative, linearize, numout | ||
| using Test | ||
| using Adapt | ||
| using TupleTools | ||
| using JLArrays | ||
| using VectorInterface | ||
| using CUDACore | ||
|
|
||
| test_result(a::AbstractArray, b::AbstractArray; kwargs...) = | ||
| isapprox(collect(a), collect(b); kwargs...) | ||
|
|
||
| function compare(f, AT::Type, xs...; kwargs...) | ||
| cpu_in = map(deepcopy, xs) # copy on CPU | ||
| gpu_in = map(adapt(AT), xs) # adapt on GPU | ||
|
|
||
| cpu_out = f(cpu_in...) | ||
| gpu_out = f(gpu_in...) | ||
|
|
||
| return test_result(cpu_out, gpu_out; kwargs...) | ||
| end | ||
|
|
||
| # types to test for | ||
| ATs = [] | ||
| !is_buildkite && push!(ATs, JLArray) | ||
| CUDACore.functional() && push!(ATs, CuArray) | ||
|
|
||
| backends = [StridedBLAS(), StridedNative()] | ||
|
|
||
| @testset "tensoradd! ($AT)" for AT in ATs | ||
| sz = (3, 5, 4, 6) | ||
| p = (3, 1, 4, 2) | ||
| for backend in backends, T in (Float32, ComplexF32) | ||
| A = randn(T, sz) | ||
| C = randn(T, TupleTools.getindices(sz, p)) | ||
|
|
||
| @test compare(AT, C, A) do c, a | ||
| tensoradd!(c, a, (p, ()), false, One(), Zero(), backend) | ||
| end | ||
|
|
||
| α = rand(T) | ||
| @test compare(AT, C, A) do c, a | ||
| tensoradd!(c, a, (p, ()), false, α, Zero(), backend) | ||
| end | ||
|
|
||
| β = rand(T) | ||
| @test compare(AT, C, A) do c, a | ||
| tensoradd!(c, a, (p, ()), false, α, β, backend) | ||
| end | ||
|
|
||
| T <: Real || @test compare(AT, C, A) do c, a | ||
| tensoradd!(c, a, (p, ()), true, α, β, backend) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "tensortrace! ($AT)" for AT in ATs | ||
| sz = (2, 4, 3, 2) | ||
| p = (2, 3) | ||
| q = ((1,), (4,)) | ||
|
|
||
| for backend in backends, T in (Float32, ComplexF32) | ||
| A = randn(T, sz) | ||
| C = randn(T, TupleTools.getindices(sz, p)) | ||
|
|
||
| @test compare(AT, C, A) do c, a | ||
| tensortrace!(c, a, (p, ()), q, false, One(), Zero(), backend) | ||
| end | ||
|
|
||
| α = rand(T) | ||
| @test compare(AT, C, A) do c, a | ||
| tensortrace!(c, a, (p, ()), q, false, α, Zero(), backend) | ||
| end | ||
|
|
||
| β = rand(T) | ||
| @test compare(AT, C, A) do c, a | ||
| tensortrace!(c, a, (p, ()), q, false, α, β, backend) | ||
| end | ||
|
|
||
| T <: Real || @test compare(AT, C, A) do c, a | ||
| tensortrace!(c, a, (p, ()), q, true, α, β, backend) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| @testset "tensorcontract! ($AT)" for AT in ATs | ||
| sz = (2, 4, 3, 4, 2, 5) | ||
| pA = ((4, 1), (2, 3)) | ||
| pB = ((3, 1), (2,)) | ||
| pAB = ((1, 2, 3), ()) | ||
|
|
||
| for backend in backends, T in (Float32, ComplexF32) | ||
| A = randn(T, (2, 4, 3, 2)) | ||
| B = randn(T, (3, 3, 4)) | ||
| C = randn(T, (2, 2, 3)) | ||
|
|
||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, false, b, pB, false, pAB, One(), Zero(), backend) | ||
| end | ||
|
|
||
| α = rand(T) | ||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, false, b, pB, false, pAB, α, Zero(), backend) | ||
| end | ||
|
|
||
| β = rand(T) | ||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, false, b, pB, false, pAB, α, β, backend) | ||
| end | ||
|
|
||
| if !(T <: Real) | ||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, true, b, pB, false, pAB, α, β, backend) | ||
| end | ||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, false, b, pB, true, pAB, α, β, backend) | ||
| end | ||
| @test compare(AT, C, A, B) do c, a, b | ||
| tensorcontract!(c, a, pA, true, b, pB, true, pAB, α, β, backend) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| end |
This file contains hidden or 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
Oops, something went wrong.
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.
Is it not better to have the
GPUArraystests before the cuTENSOR tests? Although it probably doesn't matter as you explicitly specify the backend in those tests, right?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.
Yes, indeed!