|
| 1 | +module TensorOperationsAMDGPUExt |
| 2 | + |
| 3 | +using AMDGPU |
| 4 | +using TensorOperations |
| 5 | +using TensorOperations: TensorOperations as TO |
| 6 | + |
| 7 | +#------------------------------------------------------------------------------------------- |
| 8 | +# Allocator |
| 9 | +#------------------------------------------------------------------------------------------- |
| 10 | + |
| 11 | +TO.tensoradd_type(TC, A::AnyRocArray, pA::Index2Tuple, conjA::Bool) = |
| 12 | + ROCArray{TC, TO.numind(pA)} |
| 13 | + |
| 14 | +function TO.tensoralloc_add( |
| 15 | + TC, A::AbstractArray, pA::Index2Tuple, conjA::Bool, |
| 16 | + istemp::Val, allocator::TO.AMDAllocator |
| 17 | + ) |
| 18 | + ttype = ROCArray{TC, TO.numind(pA)} |
| 19 | + structure = TO.tensoradd_structure(A, pA, conjA) |
| 20 | + return TO.tensoralloc(ttype, structure, istemp, allocator)::ttype |
| 21 | +end |
| 22 | + |
| 23 | +function TO.tensoralloc_contract( |
| 24 | + TC, |
| 25 | + A::AbstractArray, pA::Index2Tuple, conjA::Bool, |
| 26 | + B::AbstractArray, pB::Index2Tuple, conjB::Bool, |
| 27 | + pAB::Index2Tuple, |
| 28 | + istemp::Val, allocator::TO.AMDAllocator |
| 29 | + ) |
| 30 | + ttype = ROCArray{TC, TO.numind(pAB)} |
| 31 | + structure = TO.tensorcontract_structure(A, pA, conjA, B, pB, conjB, pAB) |
| 32 | + return TO.tensoralloc(ttype, structure, istemp, allocator)::ttype |
| 33 | +end |
| 34 | + |
| 35 | +# NOTE: the general implementation in the `DefaultAllocator` case works just fine, without |
| 36 | +# selecting an explicit memory model |
| 37 | +function TO.tensoralloc( |
| 38 | + ::Type{<:ROCArray{T, N}}, structure, |
| 39 | + ::Val{istemp}, allocator::TO.AMDAllocator |
| 40 | + ) where {T, N} |
| 41 | + return ROCArray{T, N}(undef, structure) |
| 42 | +end |
| 43 | + |
| 44 | +function TO.tensorfree!(C::ROCArray, ::TO.AMDAllocator) |
| 45 | + AMDGPU.unsafe_free!(C) |
| 46 | + return nothing |
| 47 | +end |
| 48 | + |
| 49 | +end |
0 commit comments