From cad33efa2f46ee3a70be258fdc8f1195a137297e Mon Sep 17 00:00:00 2001 From: hz-xiaxz <1806656034@qq.com> Date: Tue, 10 Nov 2026 13:44:59 -0500 Subject: [PATCH 1/2] Fix type instabilities in `foreachblock` - `foreachblock(f, t, ts...)`: specialize on `ts` and use `map` instead of broadcasting over the heterogeneous tuple of tensors, so the call infers - svd wrappers: same `map` instead of broadcast for the block sectors - `foldright`/`foldleft`: use fresh locals for `b` and `c` so the closures capturing them do not box them Co-Authored-By: Claude Opus 5.5 --- docs/src/Changelog.md | 1 + src/factorizations/matrixalgebrakit.jl | 2 +- src/fusiontrees/duality_manipulations.jl | 10 ++++++---- src/tensors/blockiterators.jl | 8 +++++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/src/Changelog.md b/docs/src/Changelog.md index 50be1f602..eb041a913 100644 --- a/docs/src/Changelog.md +++ b/docs/src/Changelog.md @@ -40,6 +40,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec ### Performance - `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 ## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30 diff --git a/src/factorizations/matrixalgebrakit.jl b/src/factorizations/matrixalgebrakit.jl index 44bff6cd0..ef6e022c2 100644 --- a/src/factorizations/matrixalgebrakit.jl +++ b/src/factorizations/matrixalgebrakit.jl @@ -62,7 +62,7 @@ for (f!, bf!) in ((:svd_compact!, :batched_svd_compact!), (:svd_full!, :batched_ @timeit_debug GLOBAL_TIMER $(string(f!)) begin U, S, Vᴴ = F driver = get(alg.kwargs, :driver, MAK.DefaultDriver()) - cs = collect(union(blocksectors.((t, U, S, Vᴴ))...)) + cs = collect(union(map(blocksectors, (t, U, S, Vᴴ))...)) if !isempty(cs) && MAK.supports_ragged_batch(MAK.$f!, alg, driver, storagetype(t)) As = [block(t, c) for c in cs] Fs = ([block(U, c) for c in cs], [block(S, c) for c in cs], [block(Vᴴ, c) for c in cs]) diff --git a/src/fusiontrees/duality_manipulations.jl b/src/fusiontrees/duality_manipulations.jl index af3c02452..1dc9caa05 100644 --- a/src/fusiontrees/duality_manipulations.jl +++ b/src/fusiontrees/duality_manipulations.jl @@ -266,8 +266,9 @@ function foldright(src::FusionTreeBlock) multi_Fmove(f₁) end for (f₁′, coeff₁) in zip(f₁′s, coeffs₁) - b = f₁′.coupled - c = f₁.coupled + # fresh locals: reassigning the outer `b` and `c` captured by the closures below would box them + local b = f₁′.coupled + local c = f₁.coupled A = get!(cache₃, (b, c)) do Asymbol(a, b, c) end @@ -353,8 +354,9 @@ function foldleft(src::FusionTreeBlock) multi_Fmove(f₂) end for (f₂′, coeff₂) in zip(f₂′s, coeffs₂) - b = f₂′.coupled - c = f₂.coupled + # fresh locals: reassigning the outer `b` and `c` captured by the closures below would box them + local b = f₂′.coupled + local c = f₂.coupled A = get!(cache₃, (b, c)) do Asymbol(a, b, c) end diff --git a/src/tensors/blockiterators.jl b/src/tensors/blockiterators.jl index 893ad5ee7..d8405a20b 100644 --- a/src/tensors/blockiterators.jl +++ b/src/tensors/blockiterators.jl @@ -30,11 +30,13 @@ for c in union(blocksectors.(ts)...) end ``` """ -function foreachblock(f, t, ts...; scheduler = nothing) +function foreachblock(f, t, ts::Vararg{Any, N}; scheduler = nothing) where {N} + # `Vararg{Any, N}` forces specialization on the types of `ts`; `map` instead of broadcasting + # keeps the heterogeneous tuple of tensors (and their blocks) inferable tensors = (t, ts...) - allsectors = union(blocksectors.(tensors)...) + allsectors = union(map(blocksectors, tensors)...) foreach(allsectors) do c - return f(c, block.(tensors, Ref(c))) + return f(c, map(x -> block(x, c), tensors)) end return nothing end From dd15ba539a92d891192467b01bb14bd96f2cda98 Mon Sep 17 00:00:00 2001 From: hz-xiaxz <1806656034@qq.com> Date: Tue, 10 Nov 2026 13:50:15 -0500 Subject: [PATCH 2/2] Remove inline comments Co-Authored-By: Claude Opus 5.5 --- src/fusiontrees/duality_manipulations.jl | 2 -- src/tensors/blockiterators.jl | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/fusiontrees/duality_manipulations.jl b/src/fusiontrees/duality_manipulations.jl index 1dc9caa05..0065406d3 100644 --- a/src/fusiontrees/duality_manipulations.jl +++ b/src/fusiontrees/duality_manipulations.jl @@ -266,7 +266,6 @@ function foldright(src::FusionTreeBlock) multi_Fmove(f₁) end for (f₁′, coeff₁) in zip(f₁′s, coeffs₁) - # fresh locals: reassigning the outer `b` and `c` captured by the closures below would box them local b = f₁′.coupled local c = f₁.coupled A = get!(cache₃, (b, c)) do @@ -354,7 +353,6 @@ function foldleft(src::FusionTreeBlock) multi_Fmove(f₂) end for (f₂′, coeff₂) in zip(f₂′s, coeffs₂) - # fresh locals: reassigning the outer `b` and `c` captured by the closures below would box them local b = f₂′.coupled local c = f₂.coupled A = get!(cache₃, (b, c)) do diff --git a/src/tensors/blockiterators.jl b/src/tensors/blockiterators.jl index d8405a20b..88583e1bd 100644 --- a/src/tensors/blockiterators.jl +++ b/src/tensors/blockiterators.jl @@ -31,8 +31,6 @@ end ``` """ function foreachblock(f, t, ts::Vararg{Any, N}; scheduler = nothing) where {N} - # `Vararg{Any, N}` forces specialization on the types of `ts`; `map` instead of broadcasting - # keeps the heterogeneous tuple of tensors (and their blocks) inferable tensors = (t, ts...) allsectors = union(map(blocksectors, tensors)...) foreach(allsectors) do c