@@ -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
679679end
680680
681+ # `U` is either a scalar coefficient with integer positions (unique fusion), or a recoupling
682+ # matrix with vectors of positions (generic).
681683function _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
742722end
0 commit comments