Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ext/TensorOperationscuTENSORExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using cuTENSOR: handle, stream
using cuTENSOR: cutensorWorksizePreference_t, cutensorAlgo_t, cutensorOperationDescriptor_t,
cutensorOperator_t, cutensorJitMode_t, cutensorPlanPreference_t,
cutensorComputeDescriptorEnum
using cuTENSOR: WORKSPACE_DEFAULT, ALGO_DEFAULT, JIT_MODE_NONE
using cuTENSOR: WORKSPACE_DEFAULT, WORKSPACE_MAX, WORKSPACE_MIN, ALGO_DEFAULT, JIT_MODE_NONE
using cuTENSOR: cutensorCreatePlanPreference, cutensorPlan, CuTensorPlan,
CuTensorDescriptor, ModeType

Expand Down Expand Up @@ -204,10 +204,23 @@ function TO.tensorcontract!(
B, Binds, opB,
β,
C, Cinds, OP_IDENTITY,
OP_IDENTITY
OP_IDENTITY;
workspace = select_cutensor_workspace(backend)
)
end

function select_cutensor_workspace(backend::cuTENSORBackend)
workspace = backend.workspace
if workspace === :min
return WORKSPACE_MIN
elseif workspace === :max
return WORKSPACE_MAX
else
# Symbol validated at `backend` construction, only remaining option is `:default`.
return WORKSPACE_DEFAULT
end
end

function TO.tensortrace!(
C::CuStridedView,
A::CuStridedView, p::Index2Tuple, q::Index2Tuple, conjA::Bool,
Expand Down
21 changes: 17 additions & 4 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,20 @@ struct TBLISBackend <: AbstractBackend end
# CuTENSOR backend
#-----------------
"""
cuTENSORBackend()
Backend for tensor operations that is based on the NVIDIA cuTENSOR library.
"""
struct cuTENSORBackend <: AbstractBackend end
cuTENSORBackend(; workspace = :default)
Backend for tensor operations that is based on the NVIDIA cuTENSOR library. `workspace` is
cuTENSOR's workspace preference for contractions: `:min`, `:default` or `:max`.
"""
@kwdef struct cuTENSORBackend <: AbstractBackend
workspace::Symbol = :default

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we validate at construction time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

function cuTENSORBackend(workspace::Symbol)
if workspace ∉ (:min, :max, :default)
throw(
ArgumentError(
"unknown cuTENSOR workspace option $(repr(workspace)); options are `:min`, `:max` or `:default`"
)
)
end
return new(workspace)
end
end
18 changes: 18 additions & 0 deletions test/cutensor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ if cuTENSOR.functional()
@tensor F2[a, b, c, d, e, f] := CuArray(D)[a, b, c] * conj(CuArray(E)[d, e, f])
@test collect(F2) ≈ F1
end

@testset "tensorcontract workspace = $workspace" for workspace in (:min, :default, :max)
backend = cuTENSORBackend(; workspace)
@test backend.workspace === workspace
A = randn(Float32, (3, 20, 5, 3, 4))
B = randn(Float32, (5, 6, 20, 3))
@tensor C1[a, g, e, d, f] := A[a, b, c, d, e] * B[c, f, b, g]
@tensor backend = backend allocator = CUDAAllocator() begin
C2[a, g, e, d, f] := A[a, b, c, d, e] * B[c, f, b, g]
end
@test C2 isa CuArray
@test collect(C2) ≈ C1
end

@testset "cuTENSORBackend unknown workspace" begin
@test_throws ArgumentError cuTENSORBackend(; workspace = :maximum)
@test_throws ArgumentError cuTENSORBackend(:maximum)
end
end

@testset "more complicated expressions" verbose = true begin
Expand Down
Loading