Skip to content

Choose between direct and buffered recoupling per block - #631

Draft
lkdvos wants to merge 1 commit into
mainfrom
switch-buffered-manipulation
Draft

lkdvos wants to merge 1 commit into
mainfrom
switch-buffered-manipulation

Conversation

@lkdvos

@lkdvos lkdvos commented Nov 21, 2026

Copy link
Copy Markdown
Member

For multi-channel recoupling blocks, index manipulations always went through the buffered pack → matmul → unpack path. This PR decides per block whether to take that path or a direct path (one permuting tensoradd! per nonzero entry of U), using a rough cost model in use_buffered_transform(U, blocksize):

  • direct: nnz(U) * (c₀ + 3 * blocksize)
  • buffered: (rows + cols) * c₀ + g + (3 * cols + 4 * rows) * blocksize + rows * cols * blocksize / 4

with c₀ = 30, g = 200.

Other changes:

  • Unique fusion (scalar U) has its own method.
  • buffersize only counts blocks that take the buffered path, so no buffer is allocated when every block goes direct.
  • The per-block kernel is type stable and allocation-free on both paths.

Benchmarks (rough)

Whole-tensor permute!/braid! on an Apple M1, single-threaded (BLAS threads set to 1). The cases cover SU₂ (spin-½ chains with up to 14 legs, 0⊕½⊕1 spaces, degeneracies 2–8) and Fibonacci anyons (6–12 legs), in Float64 and ComplexF64, with cyclic and random permutations: 80 cases in total. Each number is the slowdown relative to the fastest rule on that case:

rule geomean p90 max
always direct 2.14 8.6 83
always buffered (previous behaviour) 1.11 1.47 1.92
length(U) > 4 1.07 1.26 1.40
nnz(U) > rows + cols 1.06 1.15 1.34
cost model (this PR) 1.04 1.11 1.37

The cost model gains most for Fibonacci anyons (up to ~30% over always buffered). It is ~6–14% slower than always buffered for SU₂ spin-½ chains with 12–14 legs.

Help wanted: tuning

The constants were only roughly tuned, and it would be great if someone could take this over:

  • SU₂ spin-½ chains: the model overestimates how well the direct path does for large sparse U with tiny blocks.
  • Large blocks (e.g. SU₂ with degeneracy 8 and 4 legs): whole-tensor timings vary by up to ~40% between runs, because the fresh work buffer per call makes memory/GC effects dominate. This would need repeated runs, and possibly a different allocator.
  • Not covered yet: multithreading, other sectors (e.g. SU₃), and GPU.

Benchmarking per block is misleading here: the warm, preallocated buffer and cache-resident blocks favour the buffered path. Whole-tensor timings should be used instead.

Assisted by Claude Code

Use a rough cost model to decide, per recoupling block, between the
buffered pack → matmul → unpack path and one direct permuting add per
nonzero coefficient of `U`. `buffersize` only accounts for blocks that
take the buffered path, and unique fusion gets a separate method.

Assisted-by: Claude Code
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.15385% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/tensors/indexmanipulations.jl 95.83% 1 Missing ⚠️
Files with missing lines Coverage Δ
src/tensors/treetransformers.jl 96.68% <100.00%> (+0.68%) ⬆️
src/tensors/indexmanipulations.jl 91.72% <95.83%> (+0.22%) ⬆️

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

1 participant