Repository navigation
[Triton] SAGE: compensate softmax_lse for K-smoothing shift to enable ring-attention compatibility - #3334
Merged
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes the softmax_lse returned by the FAv3 SageAttention wrapper when K-smoothing is enabled, by adding a per-query-row correction term so that (out, lse) becomes consistent with FlashAttention-style shard/ring merges.
Changes:
- Extend
sage_quant()withreturn_lseto optionally compute/return an LSE correction term (delta_lse) tied to K-smoothing. - Add
smooth_kplumbed throughfav3_sage_wrapper_func/_FAv3SageWrapperFunc.forward, and applydelta_lseto the kernel-producedsoftmax_lsewhen requested. - Update docs in-place for the new options (but some docstrings remain inconsistent with signatures).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| aiter/ops/triton/quant/sage_attention_quant_wrappers.py | Adds optional return_lse output and computes delta_lse to compensate K-smoothing’s LSE offset. |
| aiter/ops/triton/attention/fav3_sage.py | Threads smooth_k through the wrapper and adjusts returned softmax_lse using delta_lse for ring-merge correctness. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1 task done
vgokhale
approved these changes
Jun 3, 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.
Motivation
fav3_sage_wrapper_funcreturns a softmax_lse that is computed against the K‑smoothed tensor (K − mean(K)), not the original K. Sage relies on the softmax shift‑invariance identity for the output, so the output is correct, but the LSE is offset by a per‑row constant δᵢ = sm_scale · Qᵢ · mean(K)ᵀ.This is invisible in single‑call use, but it breaks any FA‑style merge of (out, lse) across shards (ring attention). Each shard sees a different K slice and uses a different local mean, so each returned lse_s has a different offset → wrong merge weights → structured low‑frequency artifacts in the merged output. We hit this in Wan2.2 video generation through xfuser/xdit; severity scales with ring degree.
Technical Details
With
K = K₀ − c,c = mean(K₀, dim=seq),δᵢ = sm_scale · Qᵢ · cᵀ:qk_centered_ij = qk0_ij − δᵢmᵢ, probabilitiespᵢⱼ, andlᵢ = Σⱼ pᵢⱼare unchanged (sooutis unchanged)lse_centered_i = lse0_i − δᵢSo adding
δᵢto the kernel's LSE recovers the un‑smoothed LSE, making(out, lse)consistent with any FA ring merge.Changes
sage_quant: newreturn_lse: bool = False. When True, additionally returnsδof shape(B, H_q, S_q), fp32, with GQA/MQA broadcasting and both layouts handled.fav3_sage_wrapper_func/_FAv3SageWrapperFunc.forward: newsmooth_k: bool = Trueforwarded tosage_quant; the wrapper addsδto the kernel'ssoftmax_lsewhenreturn_lse=True.Additional kernel-side consistency fixes
Aligns two outlier paths in
sage_fwd(_no_blocksearly-exit and theper-row
causal_maskoverride for rows above the diagonal) to writesoftmax_lse = -infinstead of0, matching the-infalready producedby the regular
invalid_maskbranch. Also castsstart_mtoint32at thecompute_block_maskingcall site to fix a Triton type-unification error under
causal=True,mirroring the cast already used in the mxfp4 path. Adds two regression
tests for both empty-row paths.
Test Plan
E2E Runs with varying degree of ring attention
Test Result
E2E runs now produce correct output videos for varying degrees of ring attention.
Submission Checklist