← Back to Pull requests

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

Closed
unknown closed this pull request without merging on Nov 19, 2026 19:10 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 10:45 UTC
Author

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.

<details><summary>Reproducer</summary>

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

</details>

Diff: 276 lines (+207 / -69) across 3 files Reviewer: kshyatt Review duration: 505m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 276 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.336
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #628"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39.6% (MEDIUM) · Higher = larger blast radius
39.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.6% · Safe to merge
15.6%