diff --git a/docs/src/Changelog.md b/docs/src/Changelog.md index 63d293e9b..c8ae48b8c 100644 --- a/docs/src/Changelog.md +++ b/docs/src/Changelog.md @@ -41,6 +41,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 +- `permute`, `braid`, `transpose` and `twist` on a `TensorMap` transform blocks of at most 1024 entries by a plain loop over the data instead of through `tensoradd!` and `mul!`, whose call overhead dominated for tensors with many small subblocks ## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30 diff --git a/src/tensors/indexmanipulations.jl b/src/tensors/indexmanipulations.jl index 78a7d82fe..9c0a3b13e 100644 --- a/src/tensors/indexmanipulations.jl +++ b/src/tensors/indexmanipulations.jl @@ -769,6 +769,76 @@ function _add_transform_block!( return nothing end +# blocks with at most this many entries to compute are transformed by plain loops +const SMALL_BLOCK_LENGTH = 1024 + +# On the CPU, the call overhead of `tensoradd!` and `mul!` dominates for small blocks of a `TensorMap`, +# which are therefore transformed by a plain loop over the flat data for every term of `U`. +function _add_transform_block!( + dst::StridedSubblocks{<:CPUStorage}, src::StridedSubblocks{<:CPUStorage}, p, conjsrc::Bool, + U, inds_dst, inds_src, buffer, α, β, backend, allocator + ) + if length(U) * prod(first(@inbounds src.structure[first(inds_src)])) > SMALL_BLOCK_LENGTH + return @invoke _add_transform_block!( + dst::TransformSubblocks, src::TransformSubblocks, p, conjsrc, U, inds_dst, inds_src, buffer, + α, β, backend, allocator + ) + end + op = conjsrc ? conj : identity + @inbounds for (i, idst) in enumerate(inds_dst), (j, isrc) in enumerate(inds_src) + j == 1 || !iszero(U[i, j]) || continue # the first term also applies `β` + _add_strided!( + dst.data, src.data, dst.structure[idst], src.structure[isrc], linearize(p), + op, α * U[i, j], j == 1 ? β : One() + ) + end + return nothing +end + +# computes `A = β * A + α * permutedims(op.(B), p)` for the strided arrays `A` and `B` within `a` and `b` +function _add_strided!(a, b, (sz, strides_dst, offset_dst), (_, strides_src, offset_src), p, op::F, α, β) where {F} + strides_src′ = TupleTools.getindices(strides_src, p) + # loop over dimensions of size 1 outermost, so that the innermost loop is a nontrivial one + sz, strides_dst, strides_src′ = _permutedims3(_singletons_last(sz), sz, strides_dst, strides_src′) + # fuse dimensions along which both arrays are contiguous, and move the size-1 dimensions this leaves outward + sz = _fusedims(sz, strides_dst, strides_src′) + sz, strides_dst, strides_src′ = _permutedims3(_singletons_last(sz), sz, strides_dst, strides_src′) + if iszero(β) + _stridedloop(sz, strides_dst, strides_src′, offset_dst + 1, offset_src + 1) do i, j + @inbounds a[i] = α * op(b[j]) + end + else + _stridedloop(sz, strides_dst, strides_src′, offset_dst + 1, offset_src + 1) do i, j + @inbounds a[i] = α * op(b[j]) + β * a[i] + end + end + return nothing +end + +_permutedims3(q, ts...) = map(t -> TupleTools.getindices(t, q), ts) +_singletons_last(sz::Dims{N}) where {N} = TupleTools.sortperm(ntuple(k -> k + N * (sz[k] == 1), Val(N))) + +# merges dimension `k + 1` into `k` wherever both arrays are contiguous across them, leaving size 1 at `k + 1` +function _fusedims(sz::Dims{N}, s₁::Dims{N}, s₂::Dims{N}) where {N} + for k in (N - 1):-1:1 + if s₁[k + 1] == sz[k] * s₁[k] && s₂[k + 1] == sz[k] * s₂[k] + sz = Base.setindex(Base.setindex(sz, sz[k] * sz[k + 1], k), 1, k + 1) + end + end + return sz +end + +# calls `f(i, j)` for the positions of all corresponding entries of two strided arrays of size `sz` +@inline _stridedloop(f, ::Tuple{}, ::Tuple{}, ::Tuple{}, i::Int, j::Int) = f(i, j) +@inline function _stridedloop(f, sz::Tuple, strides₁::Tuple, strides₂::Tuple, i::Int, j::Int) + for _ in 1:last(sz) + _stridedloop(f, Base.front(sz), Base.front(strides₁), Base.front(strides₂), i, j) + i += last(strides₁) + j += last(strides₂) + end + return nothing +end + # computes `buffer_dst = buffer_src * transpose(U)`, where both are column-major views into `buffer` _recouple!(buffer, buffer_dst, buffer_src, U) = (mul!(buffer_dst, buffer_src, transpose(StridedView(U))); nothing) diff --git a/test/tensors/indexmanipulations.jl b/test/tensors/indexmanipulations.jl index c4059e00d..9374ff7d4 100644 --- a/test/tensors/indexmanipulations.jl +++ b/test/tensors/indexmanipulations.jl @@ -266,3 +266,18 @@ for V in spacelist end TensorKit.empty_globalcaches!() end + +@timedtestset "Permutations with small and large blocks" begin + # blocks up to `SMALL_BLOCK_LENGTH` entries are transformed by plain loops, larger ones through `tensoradd!` + V = Vect[SU2Irrep](0 => 6, 1 // 2 => 6, 1 => 1) + t = rand(ComplexF64, V ⊗ V ⊗ V ← V) + @test minimum(length ∘ last, subblocks(t)) < TensorKit.SMALL_BLOCK_LENGTH < maximum(length ∘ last, subblocks(t)) + a = convert(Array, t) + for p in (((1, 4), (2, 3)), ((3, 1, 4), (2,))) + a′ = permutedims(a, (p[1]..., p[2]...)) + @test convert(Array, permute(t, p)) ≈ a′ + tdst = rand(ComplexF64, permute(space(t), p)) + @test convert(Array, permute!(copy(tdst), t, p, 0.7im, 0.3)) ≈ 0.7im * a′ + 0.3 * convert(Array, tdst) + end + @test convert(Array, permute(t', ((3, 1), (4, 2)))) ≈ permutedims(convert(Array, t'), (3, 1, 4, 2)) +end