Skip to content

Commit d8409ee

Browse files
authored
Add workspace field to cuTENSOR backend. (#318)
1 parent 3f4b9e7 commit d8409ee

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎ext/TensorOperationscuTENSORExt.jl‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ using cuTENSOR: handle, stream
1212
using cuTENSOR: cutensorWorksizePreference_t, cutensorAlgo_t, cutensorOperationDescriptor_t,
1313
cutensorOperator_t, cutensorJitMode_t, cutensorPlanPreference_t,
1414
cutensorComputeDescriptorEnum
15-
using cuTENSOR: WORKSPACE_DEFAULT, ALGO_DEFAULT, JIT_MODE_NONE
15+
using cuTENSOR: WORKSPACE_DEFAULT, WORKSPACE_MAX, WORKSPACE_MIN, ALGO_DEFAULT, JIT_MODE_NONE
1616
using cuTENSOR: cutensorCreatePlanPreference, cutensorPlan, CuTensorPlan,
1717
CuTensorDescriptor, ModeType
1818

@@ -204,10 +204,23 @@ function TO.tensorcontract!(
204204
B, Binds, opB,
205205
β,
206206
C, Cinds, OP_IDENTITY,
207-
OP_IDENTITY
207+
OP_IDENTITY;
208+
workspace = select_cutensor_workspace(backend)
208209
)
209210
end
210211

212+
function select_cutensor_workspace(backend::cuTENSORBackend)
213+
workspace = backend.workspace
214+
if workspace === :min
215+
return WORKSPACE_MIN
216+
elseif workspace === :max
217+
return WORKSPACE_MAX
218+
else
219+
# Symbol validated at `backend` construction, only remaining option is `:default`.
220+
return WORKSPACE_DEFAULT
221+
end
222+
end
223+
211224
function TO.tensortrace!(
212225
C::CuStridedView,
213226
A::CuStridedView, p::Index2Tuple, q::Index2Tuple, conjA::Bool,

‎src/backends.jl‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,20 @@ struct TBLISBackend <: AbstractBackend end
9696
# CuTENSOR backend
9797
#-----------------
9898
"""
99-
cuTENSORBackend()
100-
Backend for tensor operations that is based on the NVIDIA cuTENSOR library.
101-
"""
102-
struct cuTENSORBackend <: AbstractBackend end
99+
cuTENSORBackend(; workspace = :default)
100+
Backend for tensor operations that is based on the NVIDIA cuTENSOR library. `workspace` is
101+
cuTENSOR's workspace preference for contractions: `:min`, `:default` or `:max`.
102+
"""
103+
@kwdef struct cuTENSORBackend <: AbstractBackend
104+
workspace::Symbol = :default
105+
function cuTENSORBackend(workspace::Symbol)
106+
if workspace ∉ (:min, :max, :default)
107+
throw(
108+
ArgumentError(
109+
"unknown cuTENSOR workspace option $(repr(workspace)); options are `:min`, `:max` or `:default`"
110+
)
111+
)
112+
end
113+
return new(workspace)
114+
end
115+
end

‎test/cutensor.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ if cuTENSOR.functional()
7777
@tensor F2[a, b, c, d, e, f] := CuArray(D)[a, b, c] * conj(CuArray(E)[d, e, f])
7878
@test collect(F2) ≈ F1
7979
end
80+
81+
@testset "tensorcontract workspace = $workspace" for workspace in (:min, :default, :max)
82+
backend = cuTENSORBackend(; workspace)
83+
@test backend.workspace === workspace
84+
A = randn(Float32, (3, 20, 5, 3, 4))
85+
B = randn(Float32, (5, 6, 20, 3))
86+
@tensor C1[a, g, e, d, f] := A[a, b, c, d, e] * B[c, f, b, g]
87+
@tensor backend = backend allocator = CUDAAllocator() begin
88+
C2[a, g, e, d, f] := A[a, b, c, d, e] * B[c, f, b, g]
89+
end
90+
@test C2 isa CuArray
91+
@test collect(C2) ≈ C1
92+
end
93+
94+
@testset "cuTENSORBackend unknown workspace" begin
95+
@test_throws ArgumentError cuTENSORBackend(; workspace = :maximum)
96+
@test_throws ArgumentError cuTENSORBackend(:maximum)
97+
end
8098
end
8199

82100
@testset "more complicated expressions" verbose = true begin

0 commit comments

Comments
 (0)