Skip to content

Rewind the allocator after the temporaries of blas_contract! and planarcontract! - #628

Merged
lkdvos merged 2 commits into
mainfrom
lb/blas_contract_checkpoint
Nov 19, 2026
Merged

lkdvos merged 2 commits into
mainfrom
lb/blas_contract_checkpoint

Conversation

@leburgel

@leburgel leburgel commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

blas_contract! takes the permuted copies of A, B and C from the allocator and frees them with
tensorfree!, but never resets the allocator. For a BufferAllocator, tensorfree! does nothing, so
outside @tensor (whose own checkpoint covers this) every contraction with symmetric tensors leaves its
copies claimed in the buffer. That includes every pullback of the tensorcontract! rrule, which passes
the allocator on. Once the offset passes the buffer length, every later temporary becomes an ordinary
allocation, and the buffer is never empty again to be resized.

This puts an allocator_checkpoint!/allocator_reset! pair around the temporaries, as
add_transform_kernel! and TensorOperations' own blas_contract! already do. planarcontract! and its
two BraidingTensor methods had the same gap; they only leak when called directly, outside @planar.

Found in QuantumKitHub/PEPSKit.jl#482, where one PEPS gradient with U(1) tensors at D = 3 left an offset of 7.2 MB in
a 143 KB buffer.

Reproducer
using TensorKit, TensorOperations, ChainRulesCore
const TO = TensorOperations
W = U1Space(0 => 6, 1 => 4, -1 => 4)
A = randn(ComplexF64, W ⊗ W ← W)
B = randn(ComplexF64, W ⊗ W' ← W)
pA, pB, pAB = ((1, 3), (2,)), ((2,), (1, 3)), ((1, 2), (3, 4))
st(b) = "offset $(Int(b.offset)), length $(length(b))"

b = TO.BufferAllocator()
C = TO.tensorcontract(A, pA, false, B, pB, false, pAB, 1.0, TO.DefaultBackend(), b)
println("contraction:      ", st(b))
TO.tensorcontract(A, pA, false, B, pB, false, pAB, 1.0, TO.DefaultBackend(), b)
println("second one:       ", st(b))

b = TO.BufferAllocator()
_, pb = rrule(TO.tensorcontract!, zero(C), A, pA, false, B, pB, false, pAB, 1.0, 0.0, TO.DefaultBackend(), b)
foreach(unthunk, pb(randn(ComplexF64, space(C))))
println("rrule + pullback: ", st(b))

main:

contraction:      offset 25344, length 16384
second one:       offset 50688, length 16384
rrule + pullback: offset 101376, length 16384

This PR:

contraction:      offset 0, length 16384
second one:       offset 0, length 28672
rrule + pullback: offset 0, length 28672

Comment thread test/cuda/tensors.jl Resolved
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/planar/planaroperations.jl 76.50% <100.00%> (+0.28%) ⬆️
src/tensors/braidingtensor.jl 88.88% <100.00%> (+0.28%) ⬆️
src/tensors/tensoroperations.jl 97.34% <100.00%> (+0.02%) ⬆️

... and 1 file with indirect coverage changes

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

@lkdvos
lkdvos merged commit 3d21f41 into main Nov 19, 2026
33 of 35 checks passed
@lkdvos
lkdvos deleted the lb/blas_contract_checkpoint branch November 19, 2026 19:11
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