Repository navigation
Expand file tree
/
Copy pathTensorKitGPUArraysExt.jl
More file actions
479 lines (422 loc) · 19.7 KB
/
Copy pathTensorKitGPUArraysExt.jl
File metadata and controls
479 lines (422 loc) · 19.7 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
module TensorKitGPUArraysExt
using GPUArrays
using GPUArrays: @allowscalar
using GPUArrays.KernelAbstractions: @kernel, @index, get_backend
using Adapt
using TensorKit.TupleTools
using Strided: StridedViews
using MatrixAlgebraKit, Adapt
using TensorKit
using TensorKit.TensorOperations: linearize, DefaultAllocator
using TensorKit.Factorizations
using TensorKit.Factorizations: AbstractAlgorithm
using TensorKit: SectorDict, tensormaptype, scalar, similarstoragetype, AdjointTensorMap, scalartype, project_symmetric_and_check
using TensorKit: UniqueTreeTransformer, GenericTreeTransformer, @cached, TensorMapSpace, Index2Tuple, IndexTuple
import TensorKit: randisometry, rand, randn, fill_braidingsubblock!, add_transform_kernel!
function TensorKit.fill_braidingsubblock!(data::TD, val) where {T, TD <: Union{<:AnyGPUMatrix{T}, <:StridedViews.StridedView{T, 4, <:AnyGPUArray{T}}}}
# COV_EXCL_START
# kernels are not reachable by coverage
@kernel function fill_subblock_kernel!(subblock, val)
idx = @index(Global, Cartesian)
idx_val = idx[1] == idx[4] && idx[2] == idx[3] ? val : zero(val)
@inbounds subblock[idx] = idx_val
end
# COV_EXCL_STOP
kernel = fill_subblock_kernel!(get_backend(data))
kernel(data, val; ndrange = size(data))
return data
end
const GPUSectorVector{T, I} = TensorKit.SectorVector{T, I, <:AnyGPUVector{T}}
function MatrixAlgebraKit.findtruncated(
values::GPUSectorVector, strategy::MatrixAlgebraKit.TruncationByOrder
)
I = sectortype(values)
dims = similar(values, Base.promote_op(dim, I))
for (c, v) in pairs(dims)
fill!(v, dim(c))
end
isempty(parent(values)) && return similar(values, Bool)
perm = sortperm(parent(values); strategy.by, strategy.rev)
cumulative_dim = cumsum(Base.permute!(parent(dims), perm))
result = similar(values, Bool)
parent(result)[perm] .= cumulative_dim .<= strategy.howmany
return result
end
function MatrixAlgebraKit.findtruncated(
values::GPUSectorVector, strategy::MatrixAlgebraKit.TruncationByError
)
(isfinite(strategy.p) && strategy.p > 0) ||
throw(ArgumentError(lazy"p-norm with p = $(strategy.p) is currently not supported."))
ϵᵖmax = max(strategy.atol^strategy.p, strategy.rtol^strategy.p * norm(values, strategy.p))
ϵᵖ = similar(values, typeof(ϵᵖmax))
# dimensions are all 1 so no need to account for weight
if FusionStyle(sectortype(values)) isa UniqueFusion
parent(ϵᵖ) .= abs.(parent(values)) .^ strategy.p
else
for (c, v) in pairs(values)
v′ = ϵᵖ[c]
v′ .= abs.(v) .^ strategy.p .* dim(c)
end
end
isempty(parent(values)) && return similar(values, Bool)
perm = sortperm(parent(values); by = abs, rev = false)
cumulative_err = cumsum(Base.permute!(parent(ϵᵖ), perm))
result = similar(values, Bool)
parent(result)[perm] .= cumulative_err .> ϵᵖmax
return result
end
function MatrixAlgebraKit.findtruncated_svd(values::GPUSectorVector, strategy::S) where {S <: MatrixAlgebraKit.TruncationStrategy}
# returning a GPUSectorVector wrecks things in truncate_{co}domain
# because of scalar indexing
return Adapt.adapt(Vector, MatrixAlgebraKit.findtruncated(values, strategy))
end
for strat in (:(MatrixAlgebraKit.TruncationByOrder), :(MatrixAlgebraKit.TruncationByError), :(MatrixAlgebraKit.TruncationIntersection), :(TensorKit.Factorizations.TruncationSpace))
@eval function MatrixAlgebraKit.findtruncated_svd(values::GPUSectorVector, strategy::$strat)
# returning a GPUSectorVector wrecks things in truncate_{co}domain
# because of scalar indexing
return Adapt.adapt(Vector, MatrixAlgebraKit.findtruncated(values, strategy))
end
end
function MatrixAlgebraKit.findtruncated_svd(values::GPUSectorVector, strategy::MatrixAlgebraKit.TruncationByValue)
atol = TensorKit.Factorizations.rtol_to_atol(values, strategy.p, strategy.atol, strategy.rtol)
strategy′ = trunctol(; atol, strategy.by, strategy.keep_below)
return SectorDict(c => Adapt.adapt(Vector, MatrixAlgebraKit.findtruncated_svd(d, strategy′)) for (c, d) in pairs(values))
end
function MatrixAlgebraKit.truncation_error!(values::GPUSectorVector, ind::AbstractVector{Bool})
for (c, ind_c) in pairs(ind)
sector_vals = values[c]
@. sector_vals *= !ind_c
end
return norm(values)
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 <: AnyGPUVector{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
# Scalar implementation
#-----------------------
function TensorKit.scalar(t::TensorMap{T, S, 0, 0, <:AnyGPUArray}) where {T, S}
inds = findall(!iszero, t.data)
return isempty(inds) ? zero(scalartype(t)) : @allowscalar @inbounds t.data[only(inds)]
end
# Device-side tree transformers
# -----------------------------
# A `TreeTransformer` stores the mapping between subblock positions (plus recoupling
# coefficients) and the `StridedStructure`s of the source and destination spaces.
# We resolve the positions into sizes/strides/offsets and pack all the information on the
# CPU side into dense vectors of numbers, plus some accounting information so we know how
# to unpack in the kernel. Also, we can permute the source strides "in advance" on the CPU side.
# We also precompute the strides of the subblock each kernel index will work on,
# so that the GPU thread can recover the Cartesian coordinates it will need for input/ouput,
# and a running `work_offsets` count of destination elements, so that a kernel can run one
# thread per output element and recover which input that element belongs with.
# Some possible TODO here:
# - Try cuTILE as this is a classic tile programming problem
# - Use shared memory to coalesce the reads
# - Use a 2D grid for the Generic case
const TreeStructure{N} = Tuple{NTuple{N, Int}, Int}
"""
UniqueTransformerBlock{T, N}
`isbits` descriptor for a subblock that is a *single* scaled permutation:
an entry of a `UniqueTreeTransformer` or a unique (one-tree) block of a
`GenericTreeTransformer`.
"""
struct UniqueTransformerBlock{T, N}
coeff::T
sz::NTuple{N, Int}
dense_strides::NTuple{N, Int}
strides_dst::NTuple{N, Int}
offsets_dst::Int
permuted_strides_src::NTuple{N, Int} # source strides, permuted by `p`
offsets_src::Int
end
# device-side simple struct that GPU kernels can use
struct DeviceUniqueTreeTransformer{VB <: AbstractVector{<:UniqueTransformerBlock}, VO <: AbstractVector{Int}}
blocks::VB
work_offsets::VO
nwork::Int
end
"""
GenericTransformerBlock{N}
Descriptor for a recoupling block of a `GenericTreeTransformer`, indexing into the
flat `coeffs`/`structs_dst`/`structs_src` vectors of a `DeviceGenericTreeTransformer`.
"""
struct GenericTransformerBlock{N}
sz::NTuple{N, Int}
densestrides::NTuple{N, Int}
rows::Int
cols::Int
u_offset::Int # location in the flattened U vector to find this block's U
dst_offset::Int
src_offset::Int
end
# force all the type signatures here to make sure doing something wrong fails
# before the kernel launch. Kernel error dumps are awful and hard to interpret.
struct DeviceGenericTreeTransformer{VO <: AbstractVector{Int}, DA <: DeviceUniqueTreeTransformer{<:Any, VO}, VB <: AbstractVector{<:GenericTransformerBlock}, VC <: AbstractVector{<:Number}, VS <: AbstractVector{<:Tuple{<:Tuple{Vararg{Int}}, Int}}}
unique_blocks::DA # length(U) = 1 blocks, can be handled by unique kernel
blocks::VB
work_offsets::VO
nwork::Int
coeffs::VC # every `U`, concatenated in column-major order
structs_dst::VS
structs_src::VS
end
# strides of a dense array of shape `sz`
_dense_strides(size::Dims) = (1, Base.front(cumprod(size))...)
# `permute(Vsrc, p) == Vdst` is enforced when the transformer is built, so the permuted
# source shape always matches `sz_dst` and the two views share Cartesian inds.
function _unique_block(
coeff::T, (size_dst, strides_dst, offsets_dst), (_, strides_src, offsets_src), p
) where {T}
return UniqueTransformerBlock{T, length(size_dst)}(
coeff, size_dst, _dense_strides(size_dst), strides_dst, offsets_dst,
TupleTools.getindices(strides_src, p), offsets_src
)
end
function _work_offsets(work)
offsets = cumsum(work)
pushfirst!(offsets, 0)
total = pop!(offsets)
return offsets, total
end
function DeviceUniqueTreeTransformer(transformer::UniqueTreeTransformer{T, N}, p) where {T, N}
(; structure_dst, structure_src) = transformer
blocks = UniqueTransformerBlock{T, N}[
_unique_block(coeff, structure_dst[idst], structure_src[isrc], p)
for (coeff, idst, isrc) in transformer.data
]
work_offsets, nwork = _work_offsets(prod(blk.sz) for blk in blocks)
return DeviceUniqueTreeTransformer(blocks, work_offsets, nwork)
end
function DeviceGenericTreeTransformer(
transformer::GenericTreeTransformer{T, N}, p
) where {T, N}
unique_blocks = UniqueTransformerBlock{T, N}[]
blocks = GenericTransformerBlock{N}[]
coeffs = T[]
structs_dst = TreeStructure{N}[]
structs_src = TreeStructure{N}[]
(; structure_dst, structure_src) = transformer
for (U, inds_dst, inds_src) in transformer.data
if length(U) == 1 # same as the unique (Abelian) case
push!(
unique_blocks, _unique_block(
only(U), structure_dst[only(inds_dst)], structure_src[only(inds_src)], p
)
)
else
# all trees in a block share the same subblock size
size_dst = first(structure_dst[first(inds_dst)])
push!(
blocks, GenericTransformerBlock{N}(
size_dst, _dense_strides(size_dst), size(U, 1), size(U, 2),
length(coeffs), length(structs_dst), length(structs_src)
)
)
append!(coeffs, U)
for idst in inds_dst
_, strides_dst, offset_dst = structure_dst[idst]
push!(structs_dst, (strides_dst, offset_dst))
end
for isrc in inds_src
_, strides_src, offset_src = structure_src[isrc]
push!(structs_src, (TupleTools.getindices(strides_src, p), offset_src))
end
end
end
unique_offsets, unique_nwork = _work_offsets(prod(blk.sz) for blk in unique_blocks)
work_offsets, nwork = _work_offsets(blk.rows * prod(blk.sz) for blk in blocks)
return DeviceGenericTreeTransformer(
DeviceUniqueTreeTransformer(unique_blocks, unique_offsets, unique_nwork),
blocks, work_offsets, nwork, coeffs, structs_dst, structs_src
)
end
# Cache the device transformer directly, constructing the cached host version only on a miss.
@cached function TensorKit.treebraider(
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple,
conjsrc::Bool, levels::IndexTuple
) where {TA <: AnyGPUVector}
host = TensorKit.treebraider(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc, levels)
return device_transformer(A, host, linearize(p))
end
@cached function TensorKit.treetransposer(
A::Type{TA}, Vdst::TensorMapSpace, Vsrc::TensorMapSpace, p::Index2Tuple, conjsrc::Bool
) where {TA <: AnyGPUVector}
host = TensorKit.treetransposer(Vector{scalartype(A)}, Vdst, Vsrc, p, conjsrc)
return device_transformer(A, host, linearize(p))
end
const DEFAULT_DEVICECACHE_SIZE = 1000
function __init__()
# extensions can't modify GLOBAL_CACHES during precompilation, so register explicitly
append!(
TensorKit.GLOBAL_CACHES, (
Symbol("TensorKitGPUArraysExt.GLOBAL_TREEBRAIDER_CACHE") => GLOBAL_TREEBRAIDER_CACHE,
Symbol("TensorKitGPUArraysExt.GLOBAL_TREETRANSPOSER_CACHE") => GLOBAL_TREETRANSPOSER_CACHE,
)
)
# device-caches should probably have smaller sizes:
resize!(GLOBAL_TREEBRAIDER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
resize!(GLOBAL_TREETRANSPOSER_CACHE; maxsize = DEFAULT_DEVICECACHE_SIZE)
return nothing
end
# Allocations must outlive any GPUArrays allocation-cache scope around the operation.
function device_transformer(A, transformer::UniqueTreeTransformer, p)
packed = DeviceUniqueTreeTransformer(transformer, p)
return GPUArrays.@uncached begin
proto = A(undef, 0)
# Metadata and coefficients have different element types; preserve each on upload.
upload(x) = copy!(similar(proto, eltype(x), size(x)), x)
DeviceUniqueTreeTransformer(upload(packed.blocks), upload(packed.work_offsets), packed.nwork)
end
end
function device_transformer(A, transformer::GenericTreeTransformer, p)
packed = DeviceGenericTreeTransformer(transformer, p)
return GPUArrays.@uncached begin
proto = A(undef, 0)
upload(x) = copy!(similar(proto, eltype(x), size(x)), x)
unique = packed.unique_blocks
device_unique = DeviceUniqueTreeTransformer(
upload(unique.blocks), upload(unique.work_offsets), unique.nwork
)
DeviceGenericTreeTransformer(
device_unique, upload(packed.blocks), upload(packed.work_offsets), packed.nwork,
upload(packed.coeffs), upload(packed.structs_dst), upload(packed.structs_src)
)
end
end
# COV_EXCL_START
# kernels are not reachable by coverage
# largest `i` with `offsets[i] <= w`. This corresponds to the
# block which this kernel thread will work on. Since this is
# used inside a GPU kernel, searchsortedlast/searchsortedfirst
# won't work.
@inline function _searchblock(offsets, w)
lo, hi = 1, length(offsets)
while lo < hi
mid = (lo + hi + 1) >>> 1
if @inbounds offsets[mid] <= w
lo = mid
else
hi = mid - 1
end
end
return lo
end
# Cartesian coordinates of the `w`-th (0-based) entry of a dense subblock of shape `sz`.
@inline function _coordinates(w, size::NTuple{N, Int}, dense_strides::NTuple{N, Int}) where {N}
return ntuple(n -> (w ÷ dense_strides[n]) % size[n], Val(N))
end
# finds the overall linear index in the output and input arrays corresponding to the **sublock**
# coordinates currently being worked on
@inline function _linear_index(coords::NTuple{N, Int}, strides::NTuple{N, Int}, offset) where {N}
return offset + sum(ntuple(n -> coords[n] * strides[n], Val(N))) + 1
end
# One thread per destination element in `data_dst`. `op` is `identity` or `conj`, and is
# applied to the source data only (not to the coefficients).
@kernel function unique_batched_permute_kernel!(
data_dst, data_src, op, blocks, work_offsets, α, β, nwork, ::Val{N}
) where {N}
# w = @index(Global, Linear) - 1 does not work due to JLArrays bug
w = @index(Global, Linear)
w -= 1
if w < nwork
b = _searchblock(work_offsets, w)
blk = @inbounds blocks[b]
coords = _coordinates(w - (@inbounds work_offsets[b]), blk.sz, blk.dense_strides)
i_dst = _linear_index(coords, blk.strides_dst, blk.offsets_dst)
i_src = _linear_index(coords, blk.permuted_strides_src, blk.offsets_src)
@inbounds data_dst[i_dst] = α * blk.coeff * op(data_src[i_src]) + β * data_dst[i_dst]
end
end
# COV_EXCL_STOP
# One thread per destination element in `data_dst`. This makes much better use of the
# GPU "massive parallelism" as compared to the one-thread-per-subtransformer approach.
# It also more evenly divides the work among threads so the work profile is less
# jagged. Unlike the CPU implementation, there is no extract → recouple → insert process:
# BLAS is not generally reachable from inside a kernel, and fusing the recoupling into
# the strided gather lets us remove the buffer entirely.
# TODO: what about symmetries like SU(3), where the column by column approach is not
# optimal?
@kernel function generic_batched_permute_kernel!(
data_dst, data_src, op, blocks, work_offsets, coeffs, structs_dst, structs_src,
α, β, nwork, ::Val{N}
) where {N}
# w = @index(Global, Linear) - 1 does not work due to JLArrays bug
w = @index(Global, Linear)
w -= 1
if w < nwork
# bookkeeping to figure out where to read from and write to
b = _searchblock(work_offsets, w)
blk = @inbounds blocks[b]
local_w = w - (@inbounds work_offsets[b])
blocksize = prod(blk.sz)
i = local_w ÷ blocksize # 0-based destination tree
coords = _coordinates(local_w % blocksize, blk.sz, blk.densestrides)
st_dst, offs_dst = @inbounds structs_dst[blk.dst_offset + i + 1]
i_dst = _linear_index(coords, st_dst, offs_dst)
# dst_i = β * dst_i + α * Σ_j U[i, j] * permute(src_j, p): each output tree is a
# linear combination of the input trees weighted by the recoupling coefficients.
# The permutation of src_j was already done by permuting its strides before the
# kernel launched.
acc = zero(promote_type(eltype(data_src), eltype(coeffs)))
@inbounds for j in 1:blk.cols
# TODO is there a more efficient way to do this read?
coeff = coeffs[blk.u_offset + 1 + i + (j - 1) * blk.rows]
iszero(coeff) && continue
pst_src, offs_src = structs_src[blk.src_offset + j]
acc += coeff * op(data_src[_linear_index(coords, pst_src, offs_src)])
end
@inbounds data_dst[i_dst] = α * acc + β * data_dst[i_dst]
end
end
function _launch_unique!(data_dst, data_src, op, transformer, α, β, ::Val{N}) where {N}
nwork = transformer.nwork
nwork == 0 && return nothing
unique_batched_permute_kernel!(get_backend(data_dst))(
data_dst, data_src, op, transformer.blocks, transformer.work_offsets, α, β, nwork,
Val(N); ndrange = nwork
)
return nothing
end
function _launch_generic!(data_dst, data_src, op, transformer, α, β, ::Val{N}) where {N}
nwork = transformer.nwork
nwork == 0 && return nothing
generic_batched_permute_kernel!(get_backend(data_dst))(
data_dst, data_src, op, transformer.blocks, transformer.work_offsets,
transformer.coeffs, transformer.structs_dst, transformer.structs_src, α, β, nwork,
Val(N); ndrange = nwork
)
return nothing
end
# we don't want/need the `StridedSubblocks` here because that information is already in the transformer
TensorKit._transform_subblocks(
tdst::TensorMap, tsrc::TensorMap,
transformer::Union{DeviceUniqueTreeTransformer, DeviceGenericTreeTransformer}
) = (tdst.data, tsrc.data)
function TensorKit.add_transform_kernel!(
dst::AnyGPUVector, src::AnyGPUVector, p, conjsrc::Bool,
device::DeviceUniqueTreeTransformer, α, β, backend, allocator, ntasks::Int
)
N = length(linearize(p))
op = conjsrc ? conj : identity
_launch_unique!(dst, src, op, device, α, β, Val(N))
return nothing
end
function TensorKit.add_transform_kernel!(
dst::AnyGPUVector, src::AnyGPUVector, p, conjsrc::Bool,
device::DeviceGenericTreeTransformer, α, β, backend, allocator, ntasks::Int
)
N = length(linearize(p))
op = conjsrc ? conj : identity
# one-tree blocks are a scaled permutation, which the unique kernel already handles; the
# two kernels touch disjoint subblocks so the launch order does not matter
_launch_unique!(dst, src, op, device.unique_blocks, α, β, Val(N))
_launch_generic!(dst, src, op, device, α, β, Val(N))
return nothing
end
end