cuTENSOR

cuTENSOR.jl offers NVIDIA's tensor primitives at three levels:

The operations take plain arrays and their modes, a Char or integer that labels each dimension, as in Einstein notation. Each array comes with a scaling factor and a unary operator from cuTENSOR's cutensorOperator_t (OP_IDENTITY, OP_CONJ, ...), and the result is written in place:

using CUDA, cuTENSORusing cuTENSOR: contract!, OP_IDENTITYA = CUDA.rand(Float32, 4, 5)B = CUDA.rand(Float32, 5, 6)C = CUDA.zeros(Float32, 4, 6)# C[i,k] = sum over j of A[i,j] * B[j,k]contract!(1, A, ['i', 'j'], OP_IDENTITY, B, ['j', 'k'], OP_IDENTITY,          0, C, ['i', 'k'], OP_IDENTITY, OP_IDENTITY)

Every operation is planned before it is executed. To execute one several times, make a CuTensorPlan with the corresponding plan_* function and pass it to the operation, either as the plan keyword argument or as its first argument. Keyword arguments select the algorithm, the workspace size, the precision of the computation (cutensorComputeDescriptorEnum) and whether to compile a kernel for the operation; the enum values they take, like ALGO_DEFAULT and WORKSPACE_MAX, are defined without NVIDIA's CUTENSOR_ prefix.

The names of the operations are not exported: call them qualified, or import them. cuTENSOR.permute! in particular is a different function than Base.permute!.

Public

cuTENSOR.CUTENSORError — Type
CUTENSORError(code::cutensorStatus_t)

The exception thrown when a cuTENSOR call fails. code is the status that cuTENSOR returned.

source
cuTENSOR.CuTensor — Type
CuTensor(data::CuArray{T,N}, inds::Vector) -> CuTensor{T,N}

A dense tensor: the array data with a mode label for each of its dimensions, given as Chars or integers in inds. Tensors with matching labels can be added and subtracted (+, -, axpy!, axpby!), and multiplied (*, mul!), which contracts the modes they share. data must be aligned to 128 bytes; otherwise it is copied, with a warning. The fields data and inds are part of the API.

source
cuTENSOR.CuTensorBS — Type
CuTensorBS(nonzero_data::Vector{<:CuArray{T}}, blocks_per_mode, block_extents,
           nonzero_block_coords, inds) -> CuTensorBS{T,N}

A block-sparse tensor, for cuTENSOR's block-sparse contractions. Each dimension i is split into blocks_per_mode[i] blocks, of the sizes in the tuple block_extents[i]. Only the blocks listed in nonzero_block_coords, as N-tuples of 1-based block indices, are stored, each as a dense array in nonzero_data. inds labels the dimensions, as for CuTensor. Block-sparse tensors can be multiplied with mul! or contract!.

The block-sparse API is experimental: its constructor and behaviour may still change.

source
cuTENSOR.CuTensorDescriptor — Type
CuTensorDescriptor(A::DenseCuArray; size=size(A), strides=strides(A), eltype=eltype(A))
CuTensorDescriptor(size::Vector{Int64}, strides::Vector{Int64}, eltype::DataType,
                   alignment::UInt32=0x80)

A cuTENSOR tensor descriptor: the size, strides and element type of an array, and the alignment in bytes of its data. The operations and plan_* functions describe their arguments with CuTensorDescriptor(A), so other array types can be supported by adding a method to it. It converts to a cutensorTensorDescriptor_t, for use with the C API, and is destroyed when it is garbage collected. Its fields are not part of the API.

source
cuTENSOR.CuTensorPlan — Type
CuTensorPlan(desc::cutensorOperationDescriptor_t, pref::cutensorPlanPreference_t;
             workspacePref=WORKSPACE_DEFAULT)

A cuTENSOR plan for one operation, together with the workspace it needs. The plan_* functions (plan_contraction, plan_permutation, ...) return one, which the operations (contract!, cuTENSOR.permute!, ...) execute without planning again. Executions of a plan share its workspace, so don't execute one plan concurrently on several streams, and use it in the CUDA context it was created in.

The constructor makes a plan for an operation descriptor and plan preference created with the C API (cutensorCreateContraction, cutensorCreatePlanPreference, ...), which remain owned by the caller. A plan converts to a cutensorPlan_t, and is freed when it is garbage collected, or earlier with CUDA.unsafe_free!(plan). Its fields are not part of the API.

source
cuTENSOR.cutensorComputeDescriptorEnum — Type
cutensorComputeDescriptorEnum

The precisions that cuTENSOR operations can compute in, for their compute_type keyword argument: COMPUTE_DESC_16F, COMPUTE_DESC_16BF, COMPUTE_DESC_TF32, COMPUTE_DESC_3XTF32, COMPUTE_DESC_32F, COMPUTE_DESC_64F, and the emulated COMPUTE_DESC_9X16BF, COMPUTE_DESC_8XINT8 and COMPUTE_DESC_4X16F. Passing a type instead selects COMPUTE_DESC_16F for Float16, COMPUTE_DESC_32F for Float32 and ComplexF32, and COMPUTE_DESC_64F for Float64 and ComplexF64. Which precisions an operation supports depends on its element types, the GPU and the cuTENSOR version; see NVIDIA's documentation.

source
cuTENSOR.contract! — Method
contract!(alpha, A, Ainds, opA, B, Binds, opB, beta, C, Cinds, opC, opOut;
          jit=JIT_MODE_NONE, workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
          compute_type=nothing, plan=nothing) -> C
contract!(plan::CuTensorPlan, alpha, A, B, beta, C) -> C

Contract two tensors. Computes C = alpha * opA(A) * opB(B) + beta * C, summing over the modes that A and B share but C does not, and returns C.

The modes Ainds, Binds and Cinds label the dimensions of each array, one Char or integer per dimension; dimensions with the same label are matched. The arrays are dense CuArrays. opA and opB are unary operators applied to the elements (OP_IDENTITY, OP_CONJ, OP_SQRT, OP_RELU or OP_RCP). opC must be OP_IDENTITY, and opOut must be a unary operator but is not used.

The keyword arguments, shared by the other operations, are:

  • jit: JIT_MODE_DEFAULT to compile a kernel for this operation, which can be faster on recent GPUs (see write_cache!), or JIT_MODE_NONE. Only contract! and the plan_* functions take it;
  • workspace: how much workspace memory cuTENSOR may use (WORKSPACE_MIN, WORKSPACE_DEFAULT or WORKSPACE_MAX);
  • algo: the algorithm (ALGO_DEFAULT, ALGO_DEFAULT_PATIENT, ALGO_GETT, ALGO_TGETT or ALGO_TTGT);
  • compute_type: the precision to compute in, as a type (Float16, Float32 or Float64) or a cutensorComputeDescriptorEnum value such as COMPUTE_DESC_TF32. By default it follows from the element types;
  • plan: a CuTensorPlan from plan_contraction to use instead of making a new one. Without it, a plan is made and freed for every call.

The second form executes a plan made by plan_contraction on arrays with the sizes, strides and element types it was planned for, so that repeated contractions don't pay for planning. It does not free the plan.

contract! also contracts block-sparse tensors (CuTensorBS), with an extra reproducible::Bool keyword argument; this is experimental.

Wraps cutensorCreateContraction and cutensorContract.

source
cuTENSOR.elementwise_binary! — Method
elementwise_binary!(alpha, A, Ainds, opA, gamma, C, Cinds, opC, D, Dinds, opAC;
                    workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
                    compute_type=nothing, plan=nothing) -> D
elementwise_binary!(plan::CuTensorPlan, alpha, A, gamma, C, D) -> D

Combine two tensors elementwise. Computes D = opAC(alpha * opA(A), gamma * opC(C)), where A is permuted to match C's modes, and returns D. opA and opC are unary operators, opAC is a binary one (OP_ADD, OP_MUL, OP_MAX, OP_MIN). D must have the same size, strides and element type as C, and may be C itself; it takes C's modes, so Dinds is not used. See contract! for the modes, keyword arguments and the second form, which takes a plan from plan_elementwise_binary.

Wraps cutensorCreateElementwiseBinary and cutensorElementwiseBinaryExecute.

source
cuTENSOR.elementwise_trinary! — Method
elementwise_trinary!(alpha, A, Ainds, opA, beta, B, Binds, opB, gamma, C, Cinds, opC,
                     D, Dinds, opAB, opABC;
                     workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
                     compute_type=nothing, plan=nothing) -> D
elementwise_trinary!(plan::CuTensorPlan, alpha, A, beta, B, gamma, C, D) -> D

Combine three tensors elementwise. Computes D = opABC(opAB(alpha * opA(A), beta * opB(B)), gamma * opC(C)), where A and B are permuted to match C's modes, and returns D. opA, opB and opC are unary operators, opAB and opABC binary ones (OP_ADD, OP_MUL, OP_MAX, OP_MIN). D must have the same size, strides and element type as C, and may be C itself; it takes C's modes, so Dinds is not used. See contract! for the modes, keyword arguments and the second form, which takes a plan from plan_elementwise_trinary.

Wraps cutensorCreateElementwiseTrinary and cutensorElementwiseTrinaryExecute.

source
cuTENSOR.enable_logging — Function
cuTENSOR.enable_logging(enable::Bool=true)

Forward log messages from cuTENSOR to Julia's logging system. API and kernel traces are reported at Debug level, performance hints at Info level, and problems at Error level. Starting Julia with JULIA_DEBUG=cuTENSOR enables this automatically, and also shows the Debug-level messages.

source
cuTENSOR.handle — Method
cuTENSOR.handle()

The cuTENSOR handle of the current task, for its active context, for calling the C API of cuTENSOR (cuTENSOR.cutensor*) directly. It converts to a cutensorHandle_t. The handle is borrowed: don't destroy it or change its settings. Its fields are not part of the API.

source
cuTENSOR.permute! — Method
cuTENSOR.permute!(alpha, A, Ainds, opA, B, Binds;
                  workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
                  compute_type=nothing, plan=nothing) -> B
cuTENSOR.permute!(plan::CuTensorPlan, alpha, A, B) -> B

Permute the dimensions of a tensor. Computes B = alpha * opA(A), with A's dimensions reordered from the modes Ainds to the modes Binds, and returns B. opA is a unary operator. See contract! for the modes, keyword arguments and the second form, which takes a plan from plan_permutation.

This function is not Base.permute!, which permutes a vector; call it qualified.

Wraps cutensorCreatePermutation and cutensorPermute.

source
cuTENSOR.plan_contraction — Method
plan_contraction(A, Ainds, opA, B, Binds, opB, C, Cinds, opC, opOut;
                 jit=JIT_MODE_NONE, workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
                 compute_type=nothing) -> CuTensorPlan

Plan the contraction of contract! for arrays with the sizes, strides, element types and modes of A, B and C, to execute it several times. Wraps cutensorCreateContraction and cutensorCreatePlan.

source
cuTENSOR.plan_permutation — Method
plan_permutation(A, Ainds, opA, B, Binds;
                 jit=JIT_MODE_NONE, workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
                 compute_type=nothing) -> CuTensorPlan

Plan the operation of permute! for arrays with the sizes, strides, element types and modes of A and B, to execute it several times. Wraps cutensorCreatePermutation and cutensorCreatePlan.

source
cuTENSOR.plan_reduction — Method
plan_reduction(A, Ainds, opA, C, Cinds, opC, opReduce;
               jit=JIT_MODE_NONE, workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
               compute_type=nothing) -> CuTensorPlan

Plan the reduction of reduce! for arrays with the sizes, strides, element types and modes of A and C, to execute it several times. Wraps cutensorCreateReduction and cutensorCreatePlan.

source
cuTENSOR.read_cache! — Method
cuTENSOR.read_cache!(filename::String)

Load kernels that write_cache! saved to filename into the kernel cache of the current task's cuTENSOR handle, so that plans made with jit=JIT_MODE_DEFAULT don't have to compile them again. Wraps cutensorReadKernelCacheFromFile.

source
cuTENSOR.reduce! — Method
reduce!(alpha, A, Ainds, opA, beta, C, Cinds, opC, opReduce;
        workspace=WORKSPACE_DEFAULT, algo=ALGO_DEFAULT,
        compute_type=nothing, plan=nothing) -> C
reduce!(plan::CuTensorPlan, alpha, A, beta, C) -> C

Reduce a tensor over some of its dimensions. Computes C = alpha * opReduce(opA(A)) + beta * opC(C), where the binary operator opReduce (OP_ADD, OP_MUL, OP_MAX, OP_MIN) combines the elements along the modes of A that are not in Cinds, and returns C. opA and opC are unary operators. See contract! for the modes, keyword arguments and the second form, which takes a plan from plan_reduction.

Wraps cutensorCreateReduction and cutensorReduce.

source
cuTENSOR.version — Method
cuTENSOR.version() -> VersionNumber

The version of the cuTENSOR library in use. This is the library's own version, not that of the CUDA toolkit.

source
cuTENSOR.write_cache! — Method
cuTENSOR.write_cache!(filename::String)

Save the kernels that cuTENSOR compiled for plans made with jit=JIT_MODE_DEFAULT, from the kernel cache of the current task's cuTENSOR handle, to filename, for read_cache! to load in a later session. Throws a CUTENSORError when the GPU does not support compiling kernels. Wraps cutensorWriteKernelCacheToFile.

source

Private