Don't copy or retain C in the TensorOperations rrules when β = Zero() - #555
Merged
Merged
Conversation
leburgel
marked this pull request as draft
September 24, 2026 14:12
leburgel
marked this pull request as ready for review
September 25, 2026 19:17
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
lkdvos
approved these changes
Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Exact TensorKit.jl analogue of QuantumKitHub/TensorOperations.jl#308.
Opening this as a draft until that one lands.
Description
@tensorfills every new or temporary tensor withtensorcontract!(C, …, α, Zero())(likewisetensoradd!,tensortrace!). The rules then copyC, although withβ = Zero()its contentsare never read, and the pullback keeps the original
Calive until the reverse pass, since itreferences
Cfordβeven whendβis not computed. Every@tensorcontraction on the tapethus holds a dead tensor of the output's size.
Change
β === Zero(), write intosimilar(C)instead ofcopy(C). TensorKit's kernels thenoverwrite the destination completely, also for uninitialized non-isbits tensors.
Conly ifdβneeds it.NoTangent()rather thanZeroTangent()for a constantβ, so thattest_rruleaccepts
β = Zero().Effect
One enlarged-corner (issue surfaced in the context of PEPSKit.jl) of real-space
TensorMaps, differentiated with Zygote:"Memory held" is the live heap after a full GC with the pullback alive, minus that before the
forward pass. This lowers peak memory under AD; allocations and GC work are unchanged.
Reproducer
Tests
test/chainrules/tensoroperations.jlgains aβ = Zero()test_rrulecase for each rule.