-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcutensormap.jl
More file actions
178 lines (161 loc) · 6.71 KB
/
cutensormap.jl
File metadata and controls
178 lines (161 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const CuTensorMap{T, S, N₁, N₂} = TensorMap{T, S, N₁, N₂, CuVector{T, CUDA.DeviceMemory}}
const CuTensor{T, S, N} = CuTensorMap{T, S, N, 0}
const AdjointCuTensorMap{T, S, N₁, N₂} = AdjointTensorMap{T, S, N₁, N₂, CuTensorMap{T, S, N₁, N₂}}
function CuTensorMap(t::TensorMap{T, S, N₁, N₂, A}) where {T, S, N₁, N₂, A}
return CuTensorMap{T, S, N₁, N₂}(CuArray{T}(t.data), space(t))
end
function TensorMap{T, S, N₁, N₂, DA}(t::TensorMap{T, S, N₁, N₂, HA}) where {T, S, N₁, N₂, DA <: CuArray{T}, HA <: Array{T}}
return CuTensorMap{T, S, N₁, N₂}(CuArray{T}(t.data), space(t))
end
# project_symmetric! doesn't yet work for GPU types, so do this on the host, then copy
function TensorKit.project_symmetric_and_check(::Type{T}, ::Type{A}, data::AbstractArray, V::TensorMapSpace; tol = sqrt(eps(real(float(eltype(data)))))) where {T, A <: CuVector{T}}
h_t = TensorKit.TensorMapWithStorage{T, Vector{T}}(undef, V)
h_t = TensorKit.project_symmetric!(h_t, Array(data))
# verify result
isapprox(Array(reshape(data, dims(h_t))), convert(Array, h_t); atol = tol) ||
throw(ArgumentError("Data has non-zero elements at incompatible positions"))
return TensorKit.TensorMapWithStorage{T, A}(A(h_t.data), V)
end
for (fname, felt) in ((:zeros, :zero), (:ones, :one))
@eval begin
function CUDA.$fname(
codomain::TensorSpace{S},
domain::TensorSpace{S} = one(codomain)
) where {S <: IndexSpace}
return CUDA.$fname(codomain ← domain)
end
function CUDA.$fname(
::Type{T}, codomain::TensorSpace{S},
domain::TensorSpace{S} = one(codomain)
) where {T, S <: IndexSpace}
return CUDA.$fname(T, codomain ← domain)
end
CUDA.$fname(V::TensorMapSpace) = CUDA.$fname(Float64, V)
function CUDA.$fname(::Type{T}, V::TensorMapSpace) where {T}
t = CuTensorMap{T}(undef, V)
fill!(t, $felt(T))
return t
end
end
end
for randfun in (:curand, :curandn)
randfun! = Symbol(randfun, :!)
@eval begin
# converting `codomain` and `domain` into `HomSpace`
function $randfun(
codomain::TensorSpace{S},
domain::TensorSpace{S} = one(codomain),
) where {S <: IndexSpace}
return $randfun(codomain ← domain)
end
function $randfun(
::Type{T}, codomain::TensorSpace{S},
domain::TensorSpace{S} = one(codomain),
) where {T, S <: IndexSpace}
return $randfun(T, codomain ← domain)
end
function $randfun(
rng::Random.AbstractRNG, ::Type{T},
codomain::TensorSpace{S},
domain::TensorSpace{S} = one(codomain),
) where {T, S <: IndexSpace}
return $randfun(rng, T, codomain ← domain)
end
# filling in default eltype
$randfun(V::TensorMapSpace) = $randfun(Float64, V)
function $randfun(rng::Random.AbstractRNG, V::TensorMapSpace)
return $randfun(rng, Float64, V)
end
# filling in default rng
function $randfun(::Type{T}, V::TensorMapSpace) where {T}
return $randfun(Random.default_rng(), T, V)
end
# implementation
function $randfun(
rng::Random.AbstractRNG, ::Type{T},
V::TensorMapSpace
) where {T}
t = CuTensorMap{T}(undef, V)
$randfun!(rng, t)
return t
end
function $randfun!(rng::Random.AbstractRNG, t::CuTensorMap)
for (_, b) in blocks(t)
$randfun!(rng, b)
end
return t
end
end
end
# Scalar implementation
#-----------------------
function TensorKit.scalar(t::CuTensorMap{T, S, 0, 0}) where {T, S}
inds = findall(!iszero, t.data)
return isempty(inds) ? zero(scalartype(t)) : @allowscalar @inbounds t.data[only(inds)]
end
function LinearAlgebra.isposdef(t::CuTensorMap)
domain(t) == codomain(t) ||
throw(SpaceMismatch("`isposdef` requires domain and codomain to be the same"))
InnerProductStyle(spacetype(t)) === EuclideanInnerProduct() || return false
for (c, b) in blocks(t)
# do our own hermitian check
isherm = MatrixAlgebraKit.ishermitian(b)
isherm || return false
isposdef(Hermitian(b)) || return false
end
return true
end
function Base.promote_rule(
::Type{<:TT₁},
::Type{<:TT₂}
) where {
S, N₁, N₂, TTT₁, TTT₂,
TT₁ <: CuTensorMap{TTT₁, S, N₁, N₂},
TT₂ <: CuTensorMap{TTT₂, S, N₁, N₂},
}
T = TensorKit.VectorInterface.promote_add(TTT₁, TTT₂)
return CuTensorMap{T, S, N₁, N₂}
end
TensorKit.promote_storage_rule(::Type{<:CuArray{T, N}}, ::Type{<:CuArray{T, N}}) where {T, N} =
CuArray{T, N, CUDA.default_memory}
# CuTensorMap exponentation:
function TensorKit.exp!(t::CuTensorMap)
domain(t) == codomain(t) ||
error("Exponential of a tensor only exist when domain == codomain.")
!MatrixAlgebraKit.ishermitian(t) && throw(ArgumentError("`exp!` is currently only supported on hermitian CUDA tensors"))
for (c, b) in blocks(t)
copy!(b, parent(Base.exp(Hermitian(b))))
end
return t
end
# functions that don't map ℝ to (a subset of) ℝ
for f in (:sqrt, :log, :asin, :acos, :acosh, :atanh, :acoth)
sf = string(f)
@eval function Base.$f(t::CuTensorMap)
domain(t) == codomain(t) ||
throw(SpaceMismatch("`$($sf)` of a tensor only exists when domain == codomain"))
!MatrixAlgebraKit.ishermitian(t) && throw(ArgumentError("`$($sf)` is currently only supported on hermitian CUDA tensors"))
T = complex(float(scalartype(t)))
tf = similar(t, T)
for (c, b) in blocks(t)
copy!(block(tf, c), parent($f(Hermitian(b))))
end
return tf
end
end
function TensorKit.add_kernel_nonthreaded!(
tdst::CuTensorMap, tsrc::CuTensorMap, p, transformer::TensorKit.GenericTreeTransformer, α, β, backend...
)
# preallocate buffers
buffers = TensorKit.allocate_buffers(tdst, tsrc, transformer)
for subtransformer in transformer.data
# Special case without intermediate buffers whenever there is only a single block
if length(subtransformer[1]) == 1
TensorKit._add_transform_single!(tdst, tsrc, p, subtransformer, α, β, backend...)
else
cu_subtransformer = tuple(CUDA.adapt(CuArray, subtransformer[1]), subtransformer[2:end]...)
TensorKit._add_transform_multi!(tdst, tsrc, p, cu_subtransformer, buffers, α, β, backend...)
end
end
return nothing
end