|
| 1 | +module TensorKitAMDGPUExt |
| 2 | + |
| 3 | +using AMDGPU, AMDGPU.rocBLAS, LinearAlgebra |
| 4 | +using AMDGPU: @allowscalar |
| 5 | +import AMDGPU: rand as rocrand, rand! as rocrand!, randn as rocrandn, randn! as rocrandn! |
| 6 | + |
| 7 | +using TensorKit |
| 8 | +import TensorKit.VectorInterface: scalartype as vi_scalartype |
| 9 | +using TensorKit.Factorizations |
| 10 | +using TensorKit.Strided |
| 11 | +using TensorKit.Factorizations: AbstractAlgorithm |
| 12 | +using TensorKit: SectorDict, tensormaptype, scalar, similarstoragetype, AdjointTensorMap |
| 13 | + |
| 14 | +using TensorKit.MatrixAlgebraKit |
| 15 | + |
| 16 | +using Random |
| 17 | + |
| 18 | +include("roctensormap.jl") |
| 19 | + |
| 20 | +const ROCDiagonalTensorMap{T, S} = DiagonalTensorMap{T, S, ROCVector{T, AMDGPU.Mem.HIPBuffer}} |
| 21 | + |
| 22 | +""" |
| 23 | + ROCDiagonalTensorMap{T}(undef, domain::S) where {T,S<:IndexSpace} |
| 24 | + # expert mode: select storage type `A` |
| 25 | + DiagonalTensorMap{T,S,A}(undef, domain::S) where {T,S<:IndexSpace,A<:DenseVector{T}} |
| 26 | +
|
| 27 | +Construct a `DiagonalTensorMap` with uninitialized data. |
| 28 | +""" |
| 29 | +function ROCDiagonalTensorMap{T}(::UndefInitializer, V::TensorMapSpace) where {T} |
| 30 | + (numin(V) == numout(V) == 1 && domain(V) == codomain(V)) || |
| 31 | + throw(ArgumentError("DiagonalTensorMap requires a space with equal domain and codomain and 2 indices")) |
| 32 | + return ROCDiagonalTensorMap{T}(undef, domain(V)) |
| 33 | +end |
| 34 | +function ROCDiagonalTensorMap{T}(::UndefInitializer, V::ProductSpace) where {T} |
| 35 | + length(V) == 1 || |
| 36 | + throw(ArgumentError("DiagonalTensorMap requires `numin(d) == numout(d) == 1`")) |
| 37 | + return ROCDiagonalTensorMap{T}(undef, only(V)) |
| 38 | +end |
| 39 | +function ROCDiagonalTensorMap{T}(::UndefInitializer, V::S) where {T, S <: IndexSpace} |
| 40 | + return ROCDiagonalTensorMap{T, S}(undef, V) |
| 41 | +end |
| 42 | +ROCDiagonalTensorMap(::UndefInitializer, V::IndexSpace) = ROCDiagonalTensorMap{Float64}(undef, V) |
| 43 | + |
| 44 | +function ROCDiagonalTensorMap(data::ROCVector{T}, V::S) where {T, S} |
| 45 | + return ROCDiagonalTensorMap{T, S}(data, V) |
| 46 | +end |
| 47 | + |
| 48 | +function ROCDiagonalTensorMap(data::Vector{T}, V::S) where {T, S} |
| 49 | + return ROCDiagonalTensorMap{T, S}(ROCVector{T}(data), V) |
| 50 | +end |
| 51 | + |
| 52 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(svd_full!), t::ROCDiagonalTensorMap, alg::DiagonalAlgorithm) |
| 53 | + V_cod = fuse(codomain(t)) |
| 54 | + V_dom = fuse(domain(t)) |
| 55 | + U = similar(t, codomain(t) ← V_cod) |
| 56 | + S = ROCDiagonalTensorMap{real(scalartype(t))}(undef, V_cod ← V_dom) |
| 57 | + Vᴴ = similar(t, V_dom ← domain(t)) |
| 58 | + return U, S, Vᴴ |
| 59 | +end |
| 60 | + |
| 61 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(svd_vals!), t::ROCTensorMap, alg::AbstractAlgorithm) |
| 62 | + V_cod = infimum(fuse(codomain(t)), fuse(domain(t))) |
| 63 | + return ROCDiagonalTensorMap{real(scalartype(t))}(undef, V_cod) |
| 64 | +end |
| 65 | + |
| 66 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(svd_compact!), t::ROCTensorMap, ::AbstractAlgorithm) |
| 67 | + V_cod = V_dom = infimum(fuse(codomain(t)), fuse(domain(t))) |
| 68 | + U = similar(t, codomain(t) ← V_cod) |
| 69 | + S = ROCDiagonalTensorMap{real(scalartype(t))}(undef, V_cod) |
| 70 | + Vᴴ = similar(t, V_dom ← domain(t)) |
| 71 | + return U, S, Vᴴ |
| 72 | +end |
| 73 | + |
| 74 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(eigh_full!), t::ROCTensorMap, ::AbstractAlgorithm) |
| 75 | + V_D = fuse(domain(t)) |
| 76 | + T = real(scalartype(t)) |
| 77 | + D = ROCDiagonalTensorMap{T}(undef, V_D) |
| 78 | + V = similar(t, codomain(t) ← V_D) |
| 79 | + return D, V |
| 80 | +end |
| 81 | + |
| 82 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(eig_full!), t::ROCTensorMap, ::AbstractAlgorithm) |
| 83 | + V_D = fuse(domain(t)) |
| 84 | + Tc = complex(scalartype(t)) |
| 85 | + D = ROCDiagonalTensorMap{Tc}(undef, V_D) |
| 86 | + V = similar(t, Tc, codomain(t) ← V_D) |
| 87 | + return D, V |
| 88 | +end |
| 89 | + |
| 90 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(eigh_vals!), t::ROCTensorMap, alg::AbstractAlgorithm) |
| 91 | + V_D = fuse(domain(t)) |
| 92 | + T = real(scalartype(t)) |
| 93 | + return D = ROCDiagonalTensorMap{Tc}(undef, V_D) |
| 94 | +end |
| 95 | + |
| 96 | +function TensorKit.Factorizations.MAK.initialize_output(::typeof(eig_vals!), t::ROCTensorMap, alg::AbstractAlgorithm) |
| 97 | + V_D = fuse(domain(t)) |
| 98 | + Tc = complex(scalartype(t)) |
| 99 | + return D = ROCDiagonalTensorMap{Tc}(undef, V_D) |
| 100 | +end |
| 101 | + |
| 102 | + |
| 103 | +# TODO |
| 104 | +# add VectorInterface extensions for proper AMDGPU promotion |
| 105 | +function TensorKit.VectorInterface.promote_add(TA::Type{<:AMDGPU.StridedROCMatrix{Tx}}, TB::Type{<:AMDGPU.StridedROCMatrix{Ty}}, α::Tα = TensorKit.VectorInterface.One(), β::Tβ = TensorKit.VectorInterface.One()) where {Tx, Ty, Tα, Tβ} |
| 106 | + return Base.promote_op(add, Tx, Ty, Tα, Tβ) |
| 107 | +end |
| 108 | + |
| 109 | +end |
0 commit comments