Skip to content

[Triton] SAGE: compensate softmax_lse for K-smoothing shift to enable ring-attention compatibility - #3334

Merged
ksikiric merged 12 commits into
mainfrom
sage_lse_fix
Jun 3, 2026
Merged

ksikiric merged 12 commits into
mainfrom
sage_lse_fix

Conversation

@ksikiric

@ksikiric ksikiric commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

fav3_sage_wrapper_func returns 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ᵢ, probabilities pᵢⱼ, and lᵢ = Σⱼ pᵢⱼ are unchanged (so out is 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: new return_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: new smooth_k: bool = True forwarded to sage_quant; the wrapper adds δ to the kernel's softmax_lse when return_lse=True.

Additional kernel-side consistency fixes

Aligns two outlier paths in sage_fwd (_no_blocks early-exit and the
per-row causal_mask override for rows above the diagonal) to write
softmax_lse = -inf instead of 0, matching the -inf already produced
by the regular invalid_mask branch. Also casts start_m to int32 at the compute_block_masking
call 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

@ksikiric
ksikiric requested review from a team and Copilot May 25, 2026 12:13
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 3334 --add-label <label>

@ksikiric
ksikiric requested review from juuso-oskari and nsakkine May 25, 2026 12:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() with return_lse to optionally compute/return an LSE correction term (delta_lse) tied to K-smoothing.
  • Add smooth_k plumbed through fav3_sage_wrapper_func / _FAv3SageWrapperFunc.forward, and apply delta_lse to the kernel-produced softmax_lse when 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.

Comment thread aiter/ops/triton/attention/fav3_sage.py Resolved
Comment thread aiter/ops/triton/attention/fav3_sage.py Resolved
Comment thread aiter/ops/triton/attention/fav3_sage.py Resolved
@ksikiric ksikiric changed the title compensate softmax_lse for K-smoothing shift to enable ring-attention compatibility [Triton] SAGE: compensate softmax_lse for K-smoothing shift to enable ring-attention compatibility May 26, 2026
@ksikiric ksikiric mentioned this pull request May 26, 2026
1 task done

@juuso-oskari juuso-oskari left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ksikiric
ksikiric merged commit acfcfe6 into main Jun 3, 2026
55 checks passed
@ksikiric
ksikiric deleted the sage_lse_fix branch June 3, 2026 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants