Repository navigation
[Triton/Gluon] sage_quant: use each tensor's own strides for Q/K/V inputs and outputs - #6400
Conversation
…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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
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.
There was a problem hiding this comment.
🟡 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]>
…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.


Fixes #6222.
Problem
sage_quant/sage_quant_kernel(used byfav3_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 originalqandk, 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_likegives 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 replaceq/kwith new tensors, but the old strides were still used to read them.sage_quant_v_kernel(V path ofsage_quant_mxfp4andsage_quant_mxfp6f6f8) 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 toAITER_SAGE, and the generated video was noise.Fix
sage_quant_kerneltakes 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 fromq_int8/k_int8/v_fp8).sage_quant_v_kerneltakes separate output strides; both call sites pass them.The attention kernel is unchanged; it already uses each quantized tensor's own strides.
Tests
New
test_sage_non_dense_inputsinop_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_kon/off;q_smoothon/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:
test_sage_non_dense_inputs(24 cases)sage_quant_kernelrepro.py wrongfrom #6222, sliced K/V, max err per headrepro.py quant,k_int8/v_fp8per head vs contiguous[True, False, False, False]Truerepro.py crashsage_quant_mxfp4/sage_quant_mxfp6(f6f8) V output, sliced V vs contiguousop_tests/triton_tests/attention/test_fav3_sage.pybshd,bhsdpermuted view; 3 shapes incl. D=64,smooth_k,q_smooth, causal; attention outputs and allsage_quantoutputs)gfx942 / gfx950 were not available to me; the change is arch-independent Triton code, so CI on those archs should cover it.