Skip to content

Commit 9458d76

Browse files
hz-xiaxzclaude
andauthored
Fix type instabilities in foreachblock (#621)
* 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 <noreply@anthropic.com> * Remove inline comments Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b5db69b commit 9458d76

4 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎docs/src/Changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec
4040
### Performance
4141

4242
- `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))
43+
- `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
4344

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

‎src/factorizations/matrixalgebrakit.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ for (f!, bf!) in ((:svd_compact!, :batched_svd_compact!), (:svd_full!, :batched_
6262
@timeit_debug GLOBAL_TIMER $(string(f!)) begin
6363
U, S, Vᴴ = F
6464
driver = get(alg.kwargs, :driver, MAK.DefaultDriver())
65-
cs = collect(union(blocksectors.((t, U, S, Vᴴ))...))
65+
cs = collect(union(map(blocksectors, (t, U, S, Vᴴ))...))
6666
if !isempty(cs) && MAK.supports_ragged_batch(MAK.$f!, alg, driver, storagetype(t))
6767
As = [block(t, c) for c in cs]
6868
Fs = ([block(U, c) for c in cs], [block(S, c) for c in cs], [block(Vᴴ, c) for c in cs])

‎src/fusiontrees/duality_manipulations.jl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ function foldright(src::FusionTreeBlock)
266266
multi_Fmove(f₁)
267267
end
268268
for (f₁′, coeff₁) in zip(f₁′s, coeffs₁)
269-
b = f₁′.coupled
270-
c = f₁.coupled
269+
local b = f₁′.coupled
270+
local c = f₁.coupled
271271
A = get!(cache₃, (b, c)) do
272272
Asymbol(a, b, c)
273273
end
@@ -353,8 +353,8 @@ function foldleft(src::FusionTreeBlock)
353353
multi_Fmove(f₂)
354354
end
355355
for (f₂′, coeff₂) in zip(f₂′s, coeffs₂)
356-
b = f₂′.coupled
357-
c = f₂.coupled
356+
local b = f₂′.coupled
357+
local c = f₂.coupled
358358
A = get!(cache₃, (b, c)) do
359359
Asymbol(a, b, c)
360360
end

‎src/tensors/blockiterators.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ for c in union(blocksectors.(ts)...)
3030
end
3131
```
3232
"""
33-
function foreachblock(f, t, ts...; scheduler = nothing)
33+
function foreachblock(f, t, ts::Vararg{Any, N}; scheduler = nothing) where {N}
3434
tensors = (t, ts...)
35-
allsectors = union(blocksectors.(tensors)...)
35+
allsectors = union(map(blocksectors, tensors)...)
3636
foreach(allsectors) do c
37-
return f(c, block.(tensors, Ref(c)))
37+
return f(c, map(x -> block(x, c), tensors))
3838
end
3939
return nothing
4040
end

0 commit comments

Comments
 (0)