Skip to content

Commit f218f28

Browse files
committed
simplify CPU versions again - not needed since GPU's have their own
1 parent 5fc54e2 commit f218f28

2 files changed

Lines changed: 55 additions & 94 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ function add_transform_kernel!(
657657
)
658658
bufsize = buffersize(transformer)
659659
if bufsize == 0 # no recoupling needed: every block consists of a single tree
660-
taskforeach(transformer.data, ntasks) do blk
661-
_add_transform_block!(dst, src, p, conjsrc, blk, nothing, α, β, backend, allocator)
660+
taskforeach(transformer.data, ntasks) do (U, inds_dst, inds_src)
661+
_add_transform_block!(dst, src, p, conjsrc, U, inds_dst, inds_src, nothing, α, β, backend, allocator)
662662
end
663663
else
664664
# One max-sized workspace per task (a single one that is reused by all blocks when
@@ -669,74 +669,54 @@ function add_transform_kernel!(
669669
TO.tensoralloc(storagetype(dst), bufsize, Val(true), allocator)
670670
for _ in 1:min(length(transformer.data), ntasks)
671671
]
672-
taskforeach(transformer.data, buffers) do blk, buffer
673-
_add_transform_block!(dst, src, p, conjsrc, blk, buffer, α, β, backend, allocator)
672+
taskforeach(transformer.data, buffers) do (U, inds_dst, inds_src), buffer
673+
_add_transform_block!(dst, src, p, conjsrc, U, inds_dst, inds_src, buffer, α, β, backend, allocator)
674674
end
675675
foreach(Base.Fix2(TO.tensorfree!, allocator), buffers)
676676
TO.allocator_reset!(allocator, cp)
677677
end
678678
return nothing
679679
end
680680

681+
# `U` is either a scalar coefficient with integer positions (unique fusion), or a recoupling
682+
# matrix with vectors of positions (generic).
681683
function _add_transform_block!(
682-
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool,
683-
(coeff, idst, isrc)::UniqueTransformerData, buffer, α, β, backend, allocator
684-
)
685-
return _add_single_block!(dst, src, p, conjsrc, coeff, idst, isrc, α, β, backend, allocator)
686-
end
687-
function _add_transform_block!(
688-
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool,
689-
blk::RecouplingBlock, buffer, α, β, backend, allocator
690-
)
691-
U = blk.U
692-
return U isa Number ?
693-
_add_single_block!(dst, src, p, conjsrc, U, only(blk.inds_dst), only(blk.inds_src), α, β, backend, allocator) :
694-
_add_recoupling_block!(dst, src, p, conjsrc, U, blk.inds_dst, blk.inds_src, buffer, α, β, backend, allocator)
695-
end
696-
697-
# single tree: no matmul needed
698-
function _add_single_block!(
699-
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool, coeff, idst::Int, isrc::Int,
700-
α, β, backend, allocator
701-
)
702-
@timeit_debug GLOBAL_TIMER "dense: tensoradd" @inbounds TO.tensoradd!(
703-
dst[idst], src[isrc], p, conjsrc, α * coeff, β, backend, allocator
704-
)
705-
return nothing
706-
end
707-
708-
# multi-tree block: pack → recoupling matmul → unpack
709-
function _add_recoupling_block!(
710684
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool, U, inds_dst, inds_src, buffer,
711685
α, β, backend, allocator
712686
)
713-
rows, cols = size(U)
714-
sz_src = size(@inbounds(src[first(inds_src)]))
715-
blocksize = prod(sz_src)
716-
ptriv = (ntuple(identity, length(sz_src)), ())
717-
buffer_dst = StridedView(buffer, (blocksize, rows), (1, blocksize), 0)
718-
buffer_src = StridedView(buffer, (blocksize, cols), (1, blocksize), blocksize * rows)
719-
720-
# 1. Extract: copy each source block into column i of buffer_src as a flat vector,
721-
# using a trivial permutation so the layout is canonical before the matmul.
722-
@timeit_debug GLOBAL_TIMER "dense: pack" @inbounds for (i, isrc) in enumerate(inds_src)
723-
TO.tensoradd!(
724-
sreshape(view(buffer_src, :, i), sz_src), src[isrc],
725-
ptriv, conjsrc, One(), Zero(), backend, allocator
687+
if length(U) == 1 # single tree: no matmul needed
688+
@timeit_debug GLOBAL_TIMER "dense: tensoradd" @inbounds TO.tensoradd!(
689+
dst[only(inds_dst)], src[only(inds_src)], p, conjsrc, α * only(U), β, backend, allocator
726690
)
727-
end
728-
729-
# 2. Recoupling: buffer_dst = buffer_src * U^T (each output tree is a linear
730-
# combination of input trees weighted by the recoupling coefficients).
731-
@timeit_debug GLOBAL_TIMER "dense: recouple mul!" _recouple!(buffer, buffer_dst, buffer_src, U)
691+
else # Multi-tree block: pack → recoupling matmul → unpack.
692+
rows, cols = size(U)
693+
sz_src = size(@inbounds(src[first(inds_src)]))
694+
blocksize = prod(sz_src)
695+
ptriv = (ntuple(identity, length(sz_src)), ())
696+
buffer_dst = StridedView(buffer, (blocksize, rows), (1, blocksize), 0)
697+
buffer_src = StridedView(buffer, (blocksize, cols), (1, blocksize), blocksize * rows)
698+
699+
# 1. Extract: copy each source block into column i of buffer_src as a flat vector,
700+
# using a trivial permutation so the layout is canonical before the matmul.
701+
@timeit_debug GLOBAL_TIMER "dense: pack" @inbounds for (i, isrc) in enumerate(inds_src)
702+
TO.tensoradd!(
703+
sreshape(view(buffer_src, :, i), sz_src), src[isrc],
704+
ptriv, conjsrc, One(), Zero(), backend, allocator
705+
)
706+
end
732707

733-
# 3. Insert: scatter column j of buffer_dst into the destination, applying the
734-
# actual index permutation p and the scaling α in the same tensoradd! call.
735-
@timeit_debug GLOBAL_TIMER "dense: unpack" @inbounds for (j, idst) in enumerate(inds_dst)
736-
TO.tensoradd!(
737-
dst[idst], sreshape(view(buffer_dst, :, j), sz_src),
738-
p, false, α, β, backend, allocator
739-
)
708+
# 2. Recoupling: buffer_dst = buffer_src * U^T (each output tree is a linear
709+
# combination of input trees weighted by the recoupling coefficients).
710+
@timeit_debug GLOBAL_TIMER "dense: recouple mul!" _recouple!(buffer, buffer_dst, buffer_src, U)
711+
712+
# 3. Insert: scatter column j of buffer_dst into the destination, applying the
713+
# actual index permutation p and the scaling α in the same tensoradd! call.
714+
@timeit_debug GLOBAL_TIMER "dense: unpack" @inbounds for (j, idst) in enumerate(inds_dst)
715+
TO.tensoradd!(
716+
dst[idst], sreshape(view(buffer_dst, :, j), sz_src),
717+
p, false, α, β, backend, allocator
718+
)
719+
end
740720
end
741721
return nothing
742722
end

‎src/tensors/treetransformers.jl‎

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ function recoupling_scalartype(::Type{A}, ::Type{Tₛ}) where {A, Tₛ <: Number
3333
return Tₛ <: Real ? real(T) : complex(T)
3434
end
3535

36-
# matrix type with scalar type `T` in the same storage as the vector type `A`
37-
Base.@assume_effects :foldable recoupling_matrixtype(::Type{A}, ::Type{T}) where {A, T} =
38-
Core.Compiler.return_type(similar, Tuple{A, Type{T}, Dims{2}})
39-
4036
# (coefficient, destination position, source position)
4137
const UniqueTransformerData{T} = Tuple{T, Int, Int}
4238

@@ -54,34 +50,20 @@ struct UniqueTreeTransformer{T, N} <: TreeTransformer
5450
structure_src::Vector{StridedStructure{N}}
5551
end
5652

57-
"""
58-
RecouplingBlock{T, M}
59-
60-
Recoupling data of a single [`FusionTreeBlock`](@ref), mapping the subblocks at the source
61-
positions `inds_src` onto those at the destination positions `inds_dst`. For blocks consisting of a
62-
single tree, `U::T` is a scalar coefficient, while for other blocks it is a recoupling matrix `U::M`,
63-
where `U[j, i]` maps source `i` onto destination `j`.
64-
"""
65-
struct RecouplingBlock{T, M <: AbstractMatrix{T}}
66-
U::Union{T, M}
67-
inds_dst::Vector{Int}
68-
inds_src::Vector{Int}
69-
# `M` cannot be inferred from a scalar `U`, so the parameters are always specified
70-
RecouplingBlock{T, M}(U, inds_dst, inds_src) where {T, M <: AbstractMatrix{T}} =
71-
new{T, M}(U, inds_dst, inds_src)
72-
end
53+
# (recoupling matrix, destination positions, source positions)
54+
const GenericTransformerData{T} = Tuple{Matrix{T}, Vector{Int}, Vector{Int}}
7355

7456
"""
75-
GenericTreeTransformer{T, M, N} <: TreeTransformer
57+
GenericTreeTransformer{T, N} <: TreeTransformer
7658
7759
Tree transformation for sectors with multiple fusion channels, where the subblocks of a
7860
[`FusionTreeBlock`](@ref) map onto the subblocks of the transformed block through a recoupling
79-
matrix, stored as a [`RecouplingBlock{T, M}`](@ref RecouplingBlock). The subblock structures of the
80-
destination and source spaces are kept alongside, such that the [`StridedSubblocks`](@ref) of both
81-
tensors can be created without further lookups.
61+
matrix, stored as `(U, inds_dst, inds_src)`, where `U[j, i]` maps source `i` onto destination `j`.
62+
The subblock structures of the destination and source spaces are kept alongside, such that the
63+
[`StridedSubblocks`](@ref) of both tensors can be created without further lookups.
8264
"""
83-
struct GenericTreeTransformer{T, M <: AbstractMatrix{T}, N} <: TreeTransformer
84-
data::Vector{RecouplingBlock{T, M}}
65+
struct GenericTreeTransformer{T, N} <: TreeTransformer
66+
data::Vector{GenericTransformerData{T}}
8567
structure_dst::Vector{StridedStructure{N}}
8668
structure_src::Vector{StridedStructure{N}}
8769
end
@@ -120,11 +102,10 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst, Vsrc, conjsrc::Bo
120102
structure_dst = degeneracystructure(Vdst).subblockstructure
121103
structure_src = degeneracystructure(Vsrc).subblockstructure
122104
T = recoupling_scalartype(A, sectorscalartype(sectortype(Vsrc)))
123-
M = recoupling_matrixtype(A, T)
124105

125106
fblocks = @timeit_debug GLOBAL_TIMER "bookkeeping: fusionblocks" fusionblocks(Vsrc′)
126107
nblocks = length(fblocks)
127-
data = Vector{RecouplingBlock{T, M}}(undef, nblocks)
108+
data = Vector{GenericTransformerData{T}}(undef, nblocks)
128109
weights = Vector{Int}(undef, nblocks)
129110

130111
nthreads = get_num_manipulation_threads()
@@ -143,8 +124,8 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst, Vsrc, conjsrc::Bo
143124
return idst
144125
end
145126
end
146-
U = length(U₀) == 1 ? convert(T, only(U₀)) : convert(M, U₀)
147-
data[i] = RecouplingBlock{T, M}(U, inds_dst, inds_src)
127+
U = convert(Matrix{T}, U₀)
128+
data[i] = (U, inds_dst, inds_src)
148129
# cost model: L input blocks each going to L output blocks of a given length
149130
weights[i] = length(U₀) * prod(structure_dst[first(inds_dst)][1])
150131

@@ -157,14 +138,14 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst, Vsrc, conjsrc::Bo
157138

158139
# sort by (approximate) weight to facilitate multi-threading strategies
159140
@timeit_debug GLOBAL_TIMER "bookkeeping: sort" Base.permute!(data, sortperm(weights; rev = true))
160-
transformer = GenericTreeTransformer{T, M, numind(Vdst)}(data, structure_dst, structure_src)
141+
transformer = GenericTreeTransformer{T, numind(Vdst)}(data, structure_dst, structure_src)
161142

162143
Δt = Base.time() - t₀
163144
@debug(
164145
lazy"TreeTransformer for $Vsrc to $Vdst via $p", conjsrc,
165146
nblocks = nblocks,
166-
sz_median = nblocks > 0 ? length(data[cld(end, 2)].inds_dst) : 0,
167-
sz_max = nblocks > 0 ? length(data[1].inds_dst) : 0,
147+
sz_median = nblocks > 0 ? size(data[cld(end, 2)][1], 1) : 0,
148+
sz_max = nblocks > 0 ? size(data[1][1], 1) : 0,
168149
Δt
169150
)
170151

@@ -181,9 +162,9 @@ the recoupling matrix.
181162
buffersize(::UniqueTreeTransformer) = 0
182163
function buffersize(transformer::GenericTreeTransformer)
183164
structure_src = transformer.structure_src
184-
return maximum(transformer.data; init = 0) do blk
185-
blk.U isa Number && return 0
186-
return prod(structure_src[first(blk.inds_src)][1]) * sum(size(blk.U))
165+
return maximum(transformer.data; init = 0) do (U, _, inds_src)
166+
length(U) == 1 && return 0
167+
return prod(structure_src[first(inds_src)][1]) * sum(size(U))
187168
end
188169
end
189170

@@ -192,7 +173,7 @@ function treetransformertype(::Type{A}, Vdst, Vsrc) where {A}
192173
T = recoupling_scalartype(A, sectorscalartype(I))
193174
N = numind(Vdst)
194175
FusionStyle(I) == UniqueFusion() && return UniqueTreeTransformer{T, N}
195-
return GenericTreeTransformer{T, recoupling_matrixtype(A, T), N}
176+
return GenericTreeTransformer{T, N}
196177
end
197178

198179
function TreeTransformer(

0 commit comments

Comments
 (0)