|
| 1 | +const _ROCMatOrDict{I,T} = Union{ROCMatrix{T},SectorDict{I,ROCMatrix{T}}} |
| 2 | +const ROCTensorMap{T,S,N₁,N₂,I,A<:_ROCMatOrDict{I,T}} = TensorMap{T,S,N₁,N₂,A} |
| 3 | +const ROCTensor{T, S, N, I, A <: _ROCMatOrDict{I, T}} = ROCTensorMap{T, S, N, 0, I, A} |
| 4 | + |
| 5 | +function ROCTensorMap{T}(::UndefInitializer, V::TensorMapSpace{S, N₁, N₂}) where {T, S, N₁, N₂} |
| 6 | + A = ROCMatrix{T, AMDGPU.default_memory} |
| 7 | + TT = tensormaptype{S, N₁, N₂, A} |
| 8 | + return TT(undef, codomain(V), domain(V)) |
| 9 | +end |
| 10 | + |
| 11 | +function ROCTensorMap{T}(::UndefInitializer, codomain::TensorSpace{S}, |
| 12 | + domain::TensorSpace{S}) where {T,S} |
| 13 | + return ROCTensorMap{T}(undef, codomain ← domain) |
| 14 | +end |
| 15 | +function ROCTensor{T}(::UndefInitializer, V::TensorSpace{S}) where {T,S} |
| 16 | + return ROCTensorMap{T}(undef, V ← one(V)) |
| 17 | +end |
| 18 | + |
| 19 | +for (fname, felt) in ((:zeros, :zero), (:ones, :one)) |
| 20 | + @eval begin |
| 21 | + function AMDGPU.$fname(codomain::TensorSpace{S}, |
| 22 | + domain::TensorSpace{S}=one(codomain)) where {S<:IndexSpace} |
| 23 | + return AMDGPU.$fname(codomain ← domain) |
| 24 | + end |
| 25 | + function AMDGPU.$fname(::Type{T}, codomain::TensorSpace{S}, |
| 26 | + domain::TensorSpace{S}=one(codomain)) where {T,S<:IndexSpace} |
| 27 | + return AMDGPU.$fname(T, codomain ← domain) |
| 28 | + end |
| 29 | + AMDGPU.$fname(V::TensorMapSpace) = AMDGPU.$fname(Float64, V) |
| 30 | + function AMDGPU.$fname(::Type{T}, V::TensorMapSpace) where {T} |
| 31 | + t = ROCTensorMap{T}(undef, V) |
| 32 | + fill!(t, $felt(T)) |
| 33 | + return t |
| 34 | + end |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +for randfun in (:rand, :randn) |
| 39 | + randfun! = Symbol(randfun, :!) |
| 40 | + @eval begin |
| 41 | + # converting `codomain` and `domain` into `HomSpace` |
| 42 | + function AMDGPU.$randfun(codomain::TensorSpace{S}, |
| 43 | + domain::TensorSpace{S}) where {S<:IndexSpace} |
| 44 | + return AMDGPU.$randfun(codomain ← domain) |
| 45 | + end |
| 46 | + function AMDGPU.$randfun(::Type{T}, codomain::TensorSpace{S}, |
| 47 | + domain::TensorSpace{S}) where {T,S<:IndexSpace} |
| 48 | + return AMDGPU.$randfun(T, codomain ← domain) |
| 49 | + end |
| 50 | + function AMDGPU.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 51 | + codomain::TensorSpace{S}, |
| 52 | + domain::TensorSpace{S}) where {T,S<:IndexSpace} |
| 53 | + return AMDGPU.$randfun(rng, T, codomain ← domain) |
| 54 | + end |
| 55 | + |
| 56 | + # accepting single `TensorSpace` |
| 57 | + AMDGPU.$randfun(codomain::TensorSpace) = AMDGPU.$randfun(codomain ← one(codomain)) |
| 58 | + function AMDGPU.$randfun(::Type{T}, codomain::TensorSpace) where {T} |
| 59 | + return AMDGPU.$randfun(T, codomain ← one(codomain)) |
| 60 | + end |
| 61 | + function AMDGPU.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 62 | + codomain::TensorSpace) where {T} |
| 63 | + return AMDGPU.$randfun(rng, T, codomain ← one(domain)) |
| 64 | + end |
| 65 | + |
| 66 | + # filling in default eltype |
| 67 | + AMDGPU.$randfun(V::TensorMapSpace) = AMDGPU.$randfun(Float64, V) |
| 68 | + function AMDGPU.$randfun(rng::Random.AbstractRNG, V::TensorMapSpace) |
| 69 | + return AMDGPU.$randfun(rng, Float64, V) |
| 70 | + end |
| 71 | + |
| 72 | + # filling in default rng |
| 73 | + function AMDGPU.$randfun(::Type{T}, V::TensorMapSpace) where {T} |
| 74 | + return AMDGPU.$randfun(Random.default_rng(), T, V) |
| 75 | + end |
| 76 | + |
| 77 | + # implementation |
| 78 | + function AMDGPU.$randfun(rng::Random.AbstractRNG, ::Type{T}, |
| 79 | + V::TensorMapSpace) where {T} |
| 80 | + t = ROCTensorMap{T}(undef, V) |
| 81 | + AMDGPU.$randfun!(rng, t) |
| 82 | + return t |
| 83 | + end |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +# converters |
| 88 | +# ---------- |
| 89 | +function Base.convert(::Type{ROCTensorMap}, d::Dict{Symbol,Any}) |
| 90 | + try |
| 91 | + codomain = eval(Meta.parse(d[:codomain])) |
| 92 | + domain = eval(Meta.parse(d[:domain])) |
| 93 | + data = SectorDict(eval(Meta.parse(c)) => ROCArray(b) for (c, b) in d[:data]) |
| 94 | + return TensorMap(data, codomain, domain) |
| 95 | + catch e # sector unknown in TensorKit.jl; user-defined, hopefully accessible in Main |
| 96 | + codomain = Base.eval(Main, Meta.parse(d[:codomain])) |
| 97 | + domain = Base.eval(Main, Meta.parse(d[:domain])) |
| 98 | + data = SectorDict(Base.eval(Main, Meta.parse(c)) => ROCArray(b) |
| 99 | + for (c, b) in d[:data]) |
| 100 | + return TensorMap(data, codomain, domain) |
| 101 | + end |
| 102 | +end |
| 103 | + |
0 commit comments