Fix Eagle3 FA cached backward semantics and keep FA2 fallback backend#79
Merged
zhyncs merged 5 commits intolightseekorg:mainfrom Apr 16, 2026
Merged
Fix Eagle3 FA cached backward semantics and keep FA2 fallback backend#79zhyncs merged 5 commits intolightseekorg:mainfrom
zhyncs merged 5 commits intolightseekorg:mainfrom
Conversation
torchspec-bot
approved these changes
Apr 16, 2026
Signed-off-by: Yu Feng <admin@fengyu.org>
Signed-off-by: Yu Feng <admin@fengyu.org>
Signed-off-by: Yu Feng <admin@fengyu.org>
Signed-off-by: Yu Feng <admin@fengyu.org>
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.
Summary
This PR fixes the Eagle3
facached-attention backward path.The same gradient issue can also be avoided by
LlamaFlashAttentionMasked, but that path depends on the masked / cute FAstack.
This PR fixes the standard
fapath instead, so we have a correct backendthat only depends on standard FA2 and can serve as a more compatible
backup backend, especially on older devices or environments where the
masked FA path is unavailable.
Background
For Eagle3 cached attention, the final output is a merge of:
The old
fapath handled block0 and the external merge separately, whichmade block0 backward inconsistent with the final merged normalization.
As a result, cached-path
q/kgradients could diverge fromflex_attention/ masked-attention behavior and contribute to abnormalgradient spikes.
LlamaFlashAttentionMaskeddoes not have this issue because it expressesthe attention pattern inside a masked FA formulation, but it has stricter
backend/runtime requirements.
What this PR changes
In
torchspec/models/draft/llama3_eagle.py:facombined_out / combined_lsefapath around the corrected implementationWhy this is useful
This gives us a corrected
fabackend that:flex_attentionspecialized environments
Validation
Historical dump replay
On a historical spike dump:
grad_norm = 1422.276855total_loss = 14.712839fareplay:grad_norm = 22.8646weighted_loss = 14.633358flex_attentionreplay:grad_norm = 22.7233weighted_loss = 14.633938So after the fix:
faandflex_attentionare essentially alignedPadded batch benchmark
Right-padded batch,
batch=4, mixed valid lengths.Numerical alignment stayed good:
Tests
Updated / added checks in
tests/test_flex_attention.py:flex_attentionflex_attention