Skip to content

Commit 39153db

Browse files
committed
Updates on top of latest release
1 parent adc6ad9 commit 39153db

1 file changed

Lines changed: 43 additions & 33 deletions

File tree

‎ext/TensorKitGPUArraysExt.jl‎

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ using TensorKit.TensorOperations: linearize, DefaultAllocator
1111
using TensorKit.Factorizations
1212
using TensorKit.Factorizations: AbstractAlgorithm
1313
using TensorKit: SectorDict, tensormaptype, scalar, similarstoragetype, AdjointTensorMap, scalartype, project_symmetric_and_check
14+
using TensorKit: StridedSubblocks, UniqueTreeTransformer
1415
import TensorKit: randisometry, rand, randn, fill_braidingsubblock!, add_transform_kernel!
1516

1617
function TensorKit.fill_braidingsubblock!(data::TD, val) where {T, TD <: Union{<:AnyGPUMatrix{T}, <:StridedViews.StridedView{T, 4, <:AnyGPUArray{T}}}}
@@ -124,14 +125,15 @@ end
124125

125126
# Device-side tree transformers
126127
# -----------------------------
127-
# We pack all the information on the CPU side into dense vectors of numbers,
128-
# plus some accounting information so we know how to unpack in the kernel.
129-
# Also, we can permute the source strides "in advance" on the CPU side.
128+
# A `TreeTransformer` stores the mapping between subblock positions (plus recoupling
129+
# coefficients) and the `StridedStructure`s of the source and destination spaces.
130+
# We resolve the positions into sizes/strides/offsets and pack all the information on the
131+
# CPU side into dense vectors of numbers, plus some accounting information so we know how
132+
# to unpack in the kernel. Also, we can permute the source strides "in advance" on the CPU side.
130133
# We also precompute the strides of the subblock each kernel index will work on,
131-
# so that the GPU thread can recover the Cartesian
132-
# coordinates it will need for input/ouput, and a running `work_offsets` count
133-
# of destination elements, so that a kernel can run one thread per output
134-
# element and recover which input that element belongs with.
134+
# so that the GPU thread can recover the Cartesian coordinates it will need for input/ouput,
135+
# and a running `work_offsets` count of destination elements, so that a kernel can run one
136+
# thread per output element and recover which input that element belongs with.
135137

136138
# Some possible TODO here:
137139
# - Try cuTILE as this is a classic tile programming problem
@@ -141,12 +143,12 @@ end
141143
const TreeStructure{N} = Tuple{NTuple{N, Int}, Int}
142144

143145
"""
144-
AbelianTransformerBlock{T, N}
146+
UniqueTransformerBlock{T, N}
145147
146148
`isbits` descriptor for a subblock that is a *single* scaled permutation:
147-
an entry of an `AbelianTreeTransformer`.
149+
an entry of a `UniqueTreeTransformer`.
148150
"""
149-
struct AbelianTransformerBlock{T, N}
151+
struct UniqueTransformerBlock{T, N}
150152
coeff::T
151153
sz::NTuple{N, Int}
152154
dense_strides::NTuple{N, Int}
@@ -157,7 +159,7 @@ struct AbelianTransformerBlock{T, N}
157159
end
158160

159161
# device-side simple struct that GPU kernels can use
160-
struct DeviceAbelianTreeTransformer{VB <: AbstractVector{<:AbelianTransformerBlock}, VO <: AbstractVector{Int}}
162+
struct DeviceUniqueTreeTransformer{VB <: AbstractVector{<:UniqueTransformerBlock}, VO <: AbstractVector{Int}}
161163
blocks::VB
162164
work_offsets::VO
163165
nwork::Int
@@ -169,10 +171,10 @@ _permute_strides(strides::NTuple{N, Int}, p) where {N} = ntuple(n -> strides[p[n
169171

170172
# `permute(Vsrc, p) == Vdst` is enforced when the transformer is built, so the permuted
171173
# source shape always matches `sz_dst` and the two views share Cartesian inds.
172-
function _abelian_block(
174+
function _unique_block(
173175
coeff::T, (size_dst, strides_dst, offsets_dst), (_, strides_src, offsets_src), p
174176
) where {T}
175-
return AbelianTransformerBlock{T, length(size_dst)}(
177+
return UniqueTransformerBlock{T, length(size_dst)}(
176178
coeff, size_dst, _dense_strides(size_dst), strides_dst, offsets_dst,
177179
_permute_strides(strides_src, p), offsets_src
178180
)
@@ -185,12 +187,14 @@ function _work_offsets(work)
185187
return offsets, total
186188
end
187189

188-
function DeviceAbelianTreeTransformer(
189-
transformer::TensorKit.AbelianTreeTransformer{T, N}, p
190-
) where {T, N}
191-
blocks = AbelianTransformerBlock{T, N}[_abelian_block(entry..., p) for entry in transformer.data]
190+
function DeviceUniqueTreeTransformer(transformer::UniqueTreeTransformer{T, N}, p) where {T, N}
191+
(; structure_dst, structure_src) = transformer
192+
blocks = UniqueTransformerBlock{T, N}[
193+
_unique_block(coeff, structure_dst[idst], structure_src[isrc], p)
194+
for (coeff, idst, isrc) in transformer.data
195+
]
192196
work_offsets, nwork = _work_offsets(prod(blk.sz) for blk in blocks)
193-
return DeviceAbelianTreeTransformer(blocks, work_offsets, nwork)
197+
return DeviceUniqueTreeTransformer(blocks, work_offsets, nwork)
194198
end
195199

196200
"""
@@ -209,8 +213,8 @@ function Adapt.adapt_storage(a::StorageAdaptor, x::AbstractArray)
209213
return copyto!(dst, x)
210214
end
211215

212-
function Adapt.adapt_structure(to, t::DeviceAbelianTreeTransformer)
213-
return DeviceAbelianTreeTransformer(
216+
function Adapt.adapt_structure(to, t::DeviceUniqueTreeTransformer)
217+
return DeviceUniqueTreeTransformer(
214218
Adapt.adapt(to, t.blocks), Adapt.adapt(to, t.work_offsets), t.nwork
215219
)
216220
end
@@ -221,6 +225,8 @@ end
221225
# - `transformer.data`
222226
# - the storage type
223227
# - `p`, which is baked into the permuted source strides.
228+
# `conjsrc` is not part of the key: it is applied in the kernel, and the transformer data
229+
# for a conjugated source is already distinct (and cached separately) on the CPU side.
224230
# Using `objectid` avoids walking every recoupling matrix on every lookup.
225231
# TODO: should this live in the main package?
226232
const DEVICE_TRANSFORMER_CACHE = Dict{UInt, Tuple{WeakRef, Dict{Any, Any}}}()
@@ -248,7 +254,7 @@ function device_transformer(proto::AbstractArray, transformer, p)
248254
end
249255
end
250256

251-
_device_transformer(t::TensorKit.AbelianTreeTransformer, p) = DeviceAbelianTreeTransformer(t, p)
257+
_device_transformer(t::UniqueTreeTransformer, p) = DeviceUniqueTreeTransformer(t, p)
252258

253259
# COV_EXCL_START
254260
# kernels are not reachable by coverage
@@ -284,9 +290,10 @@ end
284290
return offset + sum(ntuple(n -> coords[n] * strides[n], Val(N))) + 1
285291
end
286292

287-
# One thread per destination element in `data_dst`.
288-
@kernel function abelian_batched_permute!(
289-
data_dst, data_src, blocks, work_offsets, α, β, nwork, ::Val{N}
293+
# One thread per destination element in `data_dst`. `op` is `identity` or `conj`, and is
294+
# applied to the source data only (not to the coefficients).
295+
@kernel function unique_batched_permute!(
296+
data_dst, data_src, op, blocks, work_offsets, α, β, nwork, ::Val{N}
290297
) where {N}
291298
w = @index(Global, Linear) - 1
292299
if w < nwork
@@ -295,29 +302,32 @@ end
295302
coords = _coordinates(w - (@inbounds work_offsets[b]), blk.sz, blk.dense_strides)
296303
i_dst = _offset(coords, blk.strides_dst, blk.offsets_dst)
297304
i_src = _offset(coords, blk.permuted_strides_src, blk.offsets_src)
298-
@inbounds data_dst[i_dst] = α * blk.coeff * data_src[i_src] + β * data_dst[i_dst]
305+
@inbounds data_dst[i_dst] = α * blk.coeff * op(data_src[i_src]) + β * data_dst[i_dst]
299306
end
300307
end
301308

302309
# COV_EXCL_STOP
303310

304-
function _launch_abelian!(data_dst, data_src, transformer, α, β, ::Val{N}) where {N}
311+
function _launch_unique!(data_dst, data_src, op, transformer, α, β, ::Val{N}) where {N}
305312
nwork = transformer.nwork
306313
nwork == 0 && return nothing
307-
abelian_batched_permute!(get_backend(data_dst))(
308-
data_dst, data_src, transformer.blocks, transformer.work_offsets, α, β, nwork,
314+
unique_batched_permute!(get_backend(data_dst))(
315+
data_dst, data_src, op, transformer.blocks, transformer.work_offsets, α, β, nwork,
309316
Val(N); ndrange = nwork
310317
)
311318
return nothing
312319
end
313320

321+
const GPUStridedSubblocks = StridedSubblocks{<:AnyGPUArray}
322+
314323
function TensorKit.add_transform_kernel!(
315-
data_dst::A, data_src::A, p, transformer::TensorKit.AbelianTreeTransformer{T, N},
316-
α, β, backend, allocator, ntasks::Int
317-
) where {T, N, A <: AnyGPUArray}
324+
dst::GPUStridedSubblocks, src::GPUStridedSubblocks, p, conjsrc::Bool,
325+
transformer::UniqueTreeTransformer{T, N}, α, β, backend, allocator, ntasks::Int
326+
) where {T, N}
318327
# GPU-side object to hold the treetransformer information
319-
device = device_transformer(data_dst, transformer, linearize(p))::DeviceAbelianTreeTransformer
320-
_launch_abelian!(data_dst, data_src, device, α, β, Val(N))
328+
device = device_transformer(dst.data, transformer, linearize(p))::DeviceUniqueTreeTransformer
329+
op = conjsrc ? conj : identity
330+
_launch_unique!(dst.data, src.data, op, device, α, β, Val(N))
321331
return nothing
322332
end
323333

0 commit comments

Comments
 (0)