Skip to content

Fix type instabilities in foreachblock - #621

Merged
kshyatt merged 2 commits into
QuantumKitHub:mainfrom
hz-xiaxz:type-stability-foreachblock-svd
Nov 12, 2026
Merged

kshyatt merged 2 commits into
QuantumKitHub:mainfrom
hz-xiaxz:type-stability-foreachblock-svd

Conversation

@hz-xiaxz

Copy link
Copy Markdown
Contributor

foreachblock(f, t, ts...) with more than one tensor was not type stable: ts... was not specialized on, and blocksectors.(tensors) broadcasts over a heterogeneous tuple, which does not infer. Every call therefore went through dynamic dispatch, including the calls from the factorizations (svd, qr, ...) that loop over blocks with it. The svd wrappers had the same broadcast in their block-sector loop.

Changes

  • foreachblock: annotate ts::Vararg{Any, N} so it specializes, and use map instead of broadcasting over the tuple of tensors.
  • svd wrappers in matrixalgebrakit.jl: map(blocksectors, (t, U, S, Vᴴ)) instead of blocksectors.((t, U, S, Vᴴ)).
  • foldright / foldleft: the loop reassigned the outer b and c, which are captured by the get! closures, so they were boxed. They are now fresh locals inside the loop.

Benchmarks

Julia 1.13.3, upstream main (b5db69b) vs this PR, same fresh environment. Runtime is the minimum over 30 trials, averaged over two runs, for V ⊗ P ← V ⊗ P tensors with V = U1Space(0 => 3, 1 => 2, -1 => 2), P = U1Space(0 => 1, 1 => 1) and V = SU2Space(0 => 2, 1//2 => 2, 1 => 1), P = SU2Space(1//2 => 1).

U1 main U1 this PR SU2 main SU2 this PR
foreachblock(f, t, U, S, Vᴴ) 3.71 µs 2.55 µs 3.85 µs 2.76 µs
svd_compact! 20.1 µs 18.6 µs 15.5 µs 13.5 µs
qr_compact! 13.9 µs 13.0 µs 11.6 µs 11.3 µs
@tensor contraction 5.15 µs 5.12 µs 5.06 µs 4.80 µs
braid 0.68 µs 0.68 µs 0.64 µs 0.62 µs

Precompilation (full TensorKit build) is unchanged within noise (381 s → 364 s, pkgimage 308 MB → 299 MB); in a fresh session, a few MPS-style contractions plus an svd_compact on spaces/eltypes outside the precompile workload infer fewer frames (403 → 325, 5.1 s → 4.9 s).

🤖 Generated with Claude Code

- `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>
@kshyatt

kshyatt commented Nov 10, 2026

Copy link
Copy Markdown
Member

Not a fan of the inline comments (not sure how helpful they will be in the future) so maybe those could be removed, otherwise LGTM

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hz-xiaxz

Copy link
Copy Markdown
Contributor Author

Done. Thanks for the comment!

@kshyatt

kshyatt commented Nov 12, 2026

Copy link
Copy Markdown
Member

GPU fails aren't related

@kshyatt
kshyatt merged commit 9458d76 into QuantumKitHub:main Nov 12, 2026
29 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants