Skip to content

[Triton/Gluon] sage_quant: use each tensor's own strides for Q/K/V inputs and outputs - #6400

Merged
azaidy merged 2 commits into
ROCm:mainfrom
Shan2L:fix/sage-quant-strides
Oct 13, 2026
Merged

azaidy merged 2 commits into
ROCm:mainfrom
Shan2L:fix/sage-quant-strides

Conversation

@Shan2L

@Shan2L Shan2L commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #6222.

Problem

sage_quant / sage_quant_kernel (used by fav3_sage_wrapper_func) computed one offset vector per tensor and used it for both the load from the bf16 input and the store to the int8 / fp8 output. Those offsets came from the strides of the original q and k, and V was addressed with K's strides. That is only correct when every input is non-overlapping and dense and Q/K/V share one layout:

  • torch.empty_like gives a non-dense input a contiguous output, so the output was written at the input's offsets (wrong rows, and writes past the end of the allocation).
  • smooth_k / q_smooth / Hadamard rotation replace q / k with new tensors, but the old strides were still used to read them.
  • V was read with K's strides, so a V with a different layout from K was read wrongly.

sage_quant_v_kernel (V path of sage_quant_mxfp4 and sage_quant_mxfp6 f6f8) had the same output-stride issue.

This is hit in practice by fused QKV projections: SGLang Diffusion's MiniMax-H3 DiT passes q, k, v = qkv.split(...) views to AITER_SAGE, and the generated video was noise.

Fix

  • sage_quant_kernel takes separate (batch, head, seq, dim) strides for Q in/out, K in/out and V in/out. The wrapper takes them from the tensors actually passed to the kernel (after smoothing / rotation, and from q_int8 / k_int8 / v_fp8).
  • sage_quant_v_kernel takes separate output strides; both call sites pass them.
  • The head-dim stride is now passed instead of assuming 1. Triton specializes strides equal to 1, so dense inputs compile to the same addressing.

The attention kernel is unchanged; it already uses each quantized tensor's own strides.

Tests

New test_sage_non_dense_inputs in op_tests/triton_tests/attention/test_fav3_sage.py: sequence-sliced K/V, slices of a fused QKV tensor, and contiguous K with sliced V; bhsd / bshd; smooth_k on/off; q_smooth on/off. Each case must match the result for contiguous copies of the same inputs exactly (atol=0, rtol=0).

Results on Radeon AI PRO R9700 (gfx1201), PyTorch 2.11 / ROCm 7.15:

check before after
test_sage_non_dense_inputs (24 cases) GPU memory access fault in sage_quant_kernel 24 passed
repro.py wrong from #6222, sliced K/V, max err per head 4e7 .. NaN 0.010 .. 0.013 (same as contiguous control)
repro.py quant, k_int8 / v_fp8 per head vs contiguous [True, False, False, False] all True
repro.py crash memory access fault (exit 134) no fault
sage_quant_mxfp4 / sage_quant_mxfp6 (f6f8) V output, sliced V vs contiguous differs bitwise equal
full op_tests/triton_tests/attention/test_fav3_sage.py - 426 passed, 571 skipped (MXFP4 tests need gfx950 / gfx1250), 0 failed
dense inputs (bshd, bhsd permuted view; 3 shapes incl. D=64, smooth_k, q_smooth, causal; attention outputs and all sage_quant outputs) - 84/84 tensors bitwise equal to before the fix

gfx942 / gfx950 were not available to me; the change is arch-independent Triton code, so CI on those archs should cover it.

…nd outputs

sage_quant_kernel addressed the int8/fp8 outputs with the input strides, read V with K's strides, and kept the strides of the original q/k after smoothing or rotation replaced them. Non-dense inputs (sequence slices, slices of a fused QKV projection) were therefore quantized from and to the wrong offsets, giving wrong attention and out-of-bounds writes. sage_quant_v_kernel (MXFP4 / MXFP6 V path) had the same output-stride issue.

Pass separate input and output strides (including the head-dim stride) for Q, K and V, taken from the tensors actually handed to the kernel. Dense inputs are unchanged bitwise.

Fixes ROCm#6222

Signed-off-by: Sylvan Liu <[email protected]>
@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:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
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: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
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 6400 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@github-actions github-actions Bot changed the title [Triton] sage_quant: use each tensor's own strides for Q/K/V inputs and outputs [Triton/Gluon] sage_quant: use each tensor's own strides for Q/K/V inputs and outputs Oct 11, 2026
Shan2L pushed a commit to Shan2L/sglang that referenced this pull request Oct 11, 2026
aiter's Sage quantization allocates its outputs with empty_like but indexes
them with the input strides, so strided views (e.g. V split from a fused QKV
projection) are quantized from the wrong elements. MiniMax-H3 passes such
views, which turned the video into noise. Upstream issue ROCm/aiter#6222,
fix ROCm/aiter#6400; the copies can be dropped once aiter includes it.
@zufayu
zufayu requested review from a team and Boss2002n October 12, 2026 01:25
@azaidy
azaidy self-requested a review October 12, 2026 16:15
@azaidy azaidy assigned azaidy and unassigned Boss2002n Oct 12, 2026
@Boss2002n
Boss2002n requested a balanced review from Copilot October 12, 2026 17:33

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.

🟡 Changes recommended

The shared V-kernel signature breaks an unchanged MHA v4 caller, and non-unit head-dimension strides remain untested.

2 open findings
What changed in this PR

Fixes #6222 by using tensor-specific input/output strides during SageAttention quantization.

Changes:

  • Separates Q/K/V input and output strides, including head-dimension strides.
  • Updates standalone V quantization stride handling.
  • Adds 24 non-dense Q/K/V regression cases.
File Description
sage_attention_quant.py Uses independent input/output offsets.
sage_attention_quant_wrappers.py Passes each tensor’s actual strides.
test_fav3_sage.py Tests sliced and fused-QKV views.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…trides for delta_s

- quantize_v_fp8 (MHA v4) still launched sage_quant_v_kernel with the old
  argument list, so every per-channel FP8 V quantization failed to launch.
  Pass the output strides of the quantized tensor.
- rotation_smooth_qk computed delta_s from K_rot with the strides of the
  input k. Use K_rot's own strides.
- Tests: quantize_v_fp8 against torch, Sage on Q/K/V with a non-unit
  head-dim stride, and rotation_smooth_qk on non-dense Q/K.

Signed-off-by: Sylvan Liu <[email protected]>

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.

🔵 Needs a closer look

The changes affect GPU kernel addressing across several architectures and require final hardware-backed CI verification.

0 open findings

2 resolved since last review

🧠 Review effort: Balanced

@azaidy azaidy 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!

@azaidy
azaidy merged commit 920e1c6 into ROCm:main Oct 13, 2026
avjves pushed a commit to xdit-project/xDiT that referenced this pull request Dec 2, 2026
…hem (#1060)

* fix(attention): copy Sage v1 inputs only for an AITER that misreads them
  
  Before AITER 0.1.26, sage_quant read Q/K/V through the wrong strides
  (ROCm/aiter#6222, fixed by ROCm/aiter#6400). The Sage v1 backends thus
  copied sequence-sliced Q/K/V, and V whose strides differ from K, before
  each call. The stride canaries now fail, because AITER reads these
  layouts correctly.
  
  No symbol or parameter shows the fix, so the installed amd-aiter
  version decides. Move the workaround to aiter_sage/layout.py, which does
  not import AITER, and return the tensors unchanged when amd-aiter is
  0.1.26 or later. A development build before 0.1.26, a missing version,
  or a version that does not parse keeps the copies.
  
  Remove the canaries. Add a CPU test of the version decision, and an
  accelerator test of a V whose strides differ from K, the one canary
  layout that test_strided_kv_backends.py did not cover.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: sage_quant / fav3_sage_wrapper_func: wrong results and out-of-bounds writes for non-dense Q/K/V

4 participants