Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
- `twist!` and `twist` on a `TensorMap`, a `DiagonalTensorMap` or their adjoints use a cached tree transformer and the index manipulation kernel instead of a loop over all fusion trees, which removes the per-subblock bookkeeping and turns a twist of a GPU tensor, diagonal or not, into a single kernel launch ([#595](https://github.com/QuantumKitHub/TensorKit.jl/issues/595))
- `foreachblock` with several tensors is type stable, which removes the dynamic dispatch on every call to it and to the factorizations that go through it
- `taskforeach` with a single worker is a plain loop without an atomic counter, which lowers the cost per subblock of single-threaded index manipulations and planar traces
- Index manipulations with multiple fusion channels choose per recoupling block between the buffered pack → matmul → unpack path and one direct permuting add per nonzero recoupling coefficient, based on a rough cost model; the work buffer is only allocated for blocks that use it

## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30

Expand Down
59 changes: 48 additions & 11 deletions src/tensors/indexmanipulations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -726,20 +726,57 @@ function add_transform_kernel!(
return nothing
end

# `U` is either a scalar coefficient with integer positions (unique fusion), or a recoupling
# matrix with vectors of positions (generic).
# Decide whether the recoupling matrix `U` acting on subblocks of length `blocksize` is applied
# through the buffered pack → matmul → unpack path, rather than through one direct permuting
# `tensoradd!` per nonzero coefficient. This compares a rough cost model of both paths, in units
# of the cost of moving one element: `c₀` is the overhead of a `tensoradd!` call, `g` that of the
# matmul, an add reads/writes ~3 elements per entry (4 when unpacking, which also reads the
# buffer), and the matmul costs ~1/4 per entry of `U` and element.
# TODO: these constants were only roughly tuned (SU₂ and Fibonacci, CPU, single-threaded)
function use_buffered_transform(U::AbstractMatrix, blocksize::Int)
rows, cols = size(U)
rows * cols == 1 && return false
c₀, g = 30, 200
cost_direct = count(!iszero, U) * (c₀ + 3 * blocksize)
cost_buffered = (rows + cols) * c₀ + g + (3 * cols + 4 * rows) * blocksize +
(rows * cols * blocksize) ÷ 4
return cost_buffered < cost_direct
end

# unique fusion: a single source subblock maps onto a single destination subblock
function _add_transform_block!(
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool, U, inds_dst, inds_src, buffer,
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool, U::Number, idst::Int, isrc::Int, buffer,
α, β, backend, allocator
)
if length(U) == 1 # single tree: no matmul needed
@timeit_debug GLOBAL_TIMER "dense: tensoradd" @inbounds TO.tensoradd!(
dst[only(inds_dst)], src[only(inds_src)], p, conjsrc, α * only(U), β, backend, allocator
)
else # Multi-tree block: pack → recoupling matmul → unpack.
rows, cols = size(U)
sz_src = size(@inbounds(src[first(inds_src)]))
blocksize = prod(sz_src)
@timeit_debug GLOBAL_TIMER "dense: tensoradd" @inbounds TO.tensoradd!(
dst[idst], src[isrc], p, conjsrc, α * U, β, backend, allocator
)
return nothing
end
# generic fusion: the subblocks of a fusion block are recoupled by the matrix `U`
function _add_transform_block!(
dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc::Bool, U::AbstractMatrix, inds_dst, inds_src, buffer,
α, β, backend, allocator
)
rows, cols = size(U)
sz_src = size(@inbounds(src[first(inds_src)]))
blocksize = prod(sz_src)
# `buffer === nothing` is known at compile time, `buffersize` uses the same criterion
if isnothing(buffer) || !use_buffered_transform(U, blocksize)
# Direct path: dst_i = β * dst_i + Σ_j α * U[i, j] * permute(src_j)
@timeit_debug GLOBAL_TIMER "dense: tensoradd" @inbounds for i in 1:rows
dst_i = dst[inds_dst[i]]
# the first nonzero term absorbs `β`, the others accumulate (keeps the type of `β` static)
# `U` is unitary, hence every row has a nonzero entry
j₁ = findfirst(j -> !iszero(U[i, j]), 1:cols)::Int
TO.tensoradd!(dst_i, src[inds_src[j₁]], p, conjsrc, α * U[i, j₁], β, backend, allocator)
for j in (j₁ + 1):cols
coeff = U[i, j]
iszero(coeff) && continue
TO.tensoradd!(dst_i, src[inds_src[j]], p, conjsrc, α * coeff, One(), backend, allocator)
end
end
else # Buffered path: pack → recoupling matmul → unpack.
ptriv = (ntuple(identity, length(sz_src)), ())
buffer_dst = StridedView(buffer, (blocksize, rows), (1, blocksize), 0)
buffer_src = StridedView(buffer, (blocksize, cols), (1, blocksize), blocksize * rows)
Expand Down
9 changes: 5 additions & 4 deletions src/tensors/treetransformers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,16 @@ end
"""
buffersize(transformer::TreeTransformer) -> Int

Compute the workspace size required to pack, recouple and unpack the largest multi-tree
block, i.e. `prod(sz_src) * (rows + cols)` where `(rows, cols) = size(U)` is the size of
the recoupling matrix.
Compute the workspace size required to pack, recouple and unpack the largest block that
uses the buffered path (see `use_buffered_transform`), i.e. `prod(sz_src) * (rows + cols)`
where `(rows, cols) = size(U)` is the size of the recoupling matrix.
"""
buffersize(::UniqueTreeTransformer) = 0
function buffersize(transformer::GenericTreeTransformer)
structure_src = transformer.structure_src
return maximum(transformer.data; init = 0) do (U, _, inds_src)
return length(U) == 1 ? 0 : prod(structure_src[first(inds_src)][1]) * sum(size(U))
blocksize = prod(structure_src[first(inds_src)][1])
return use_buffered_transform(U, blocksize) ? blocksize * sum(size(U)) : 0
end
end

Expand Down
Loading