Repository navigation
fix(attention): preserve chunk prefixes for backward - #579
Merged
lawrence-cj merged 1 commit intoNov 12, 2026
Merged
Conversation
lawrence-cj
approved these changes
Nov 12, 2026
lawrence-cj
left a comment
Collaborator
There was a problem hiding this comment.
Thanks @yuhengcai1, LGTM. The forward is unchanged, and the out-of-place adds keep the chunk prefixes autograd needs.
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.
ChunkCausalAttentionupdates its cumulative KV and K-sum tensors in place after earlier chunks have used them in matrix products. In multi-chunk FP32/FP64 backward, this either raises a tensor-version error or, with non-reentrant checkpointing, returns incorrect gradients while the forward output remains correct.Replace the two in-place updates with out-of-place additions so each chunk retains the prefix values needed by autograd. The forward formula and accumulation order are unchanged.
Add 12 regression cases comparing the actual attention module with an independent dense chunk-causal reference. They cover one/two/three chunks, direct/non-reentrant checkpoint execution, FP64 and FP32 with Q/K normalization, and nontrivial RoPE. Each checks forward values plus input and every learned parameter gradient using the same random output probe.
Validation with repository-pinned PyTorch 2.9.1, torchvision 0.24.1, and timm 0.6.13 on CPU:
git diff --checkpass.The local runner imports the complete, unmodified Sana source modules with namespace parents to bypass unrelated package initializers. It uses the real timm base constructor, normal committed pytest functions, PyTorch autograd, and checkpoint implementation; no attention arithmetic or classes are mocked. Source hashes and JUnit results were captured. Validation is limited to CPU FP32/FP64: no CUDA/mixed-precision or end-to-end model training run is claimed.