Repository navigation
fix(rocm): pass causal and softmax_scale to sageattn in sage_attn2_amd_rocm - #1710
Open
menglcai-amd wants to merge 1 commit into
Open
menglcai-amd wants to merge 1 commit into
menglcai-amd wants to merge 1 commit into
Conversation
Signed-off-by: menglcai <menglcai@amd.com>
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.
What happened
We tested MiniMax-H3-Image on AMD with
sage_attn2_amd_rocmas the attention backend of the Qwen3-VL text encoder (qwen3vl_attn_type). The images stopped following the prompt. With the same prompt,torch_sdpagives the desk scene, butsage_attn2_amd_rocmgave a red and blue plastic object with garbled text: no desk, no window, no mug, no cactus, no note. Nothing crashed, and nothing was logged.Before / After
Prompt:
Same prompt, seed and config. Only
qwen3vl_attn_typediffers. PSNR is against thetorch_sdpaimage.sage_attn2_amd_rocm(9.3 dB)sage_attn2_amd_rocm(23.2 dB)torch_sdpaAfter the fix the scene matches the reference. Small details differ because SageAttention quantizes Q and K.
Cause
AmdSageAttn2Weight.apply()getscausalandsoftmax_scalefrom the caller, but it calls onlysageattn(q, k, v, tensor_layout="NHD"). The Qwen3-VL text encoder calls attention withcausal=Trueand its ownsoftmax_scale. Without the causal mask, each token also sees the tokens after it, so the prompt embedding is wrong.apply_with_lse()in the same class already passessm_scale, so the two methods did not agree. Other callers that need these values get the same wrong result when they select this backend:hidream_o1_image(causal=Trueandsoftmax_scale), cosmos3causal_self_attn(causal=True), andcommon/kvcache/attn/flash.py(softmax_scale).Fix
Pass both values to
sageattn:The defaults are the same as the
sageattndefaults (is_causal=False,sm_scale=None), so callers that pass neither value get the same result as before. Only the ROCm platform loads this file.Validation
Environment: gfx90a (MI250), ROCm 10.3.0a20261111 (TheRock nightly), PyTorch 2.16.0a0, Triton 3.8.0, sageattention 1.0.6.
PLATFORM=amd_rocm.End to end: MiniMax-H3-Image T2I, 1024x1024, seed 42, prompt above,
configs/minimax_h3_image/minimax_h3_image_tp_sp.json(TP2 x SP2, 4 GCDs). Onlyqwen3vl_attn_typeis changed tosage_attn2_amd_rocm; the DiT stays ontorch_sdpa, so the text encoder attention is the only difference from the reference. The images are in "Before / After" above.Kernel check:
AmdSageAttn2Weight.apply()againsttorch.nn.functional.scaled_dot_product_attentionwith the sameis_causalandscale. Random bf16 inputs, H=8, one GCD. Shapes: 3-D (L=1024, D=128), 3-D (L=777, D=128), 4-D (B=2, L=512, D=64). Max abs difference over the three shapes:causal=Truesoftmax_scale=0.03RDNA: with this fix,
causal=Trueuses the causal SageAttention kernel, which this backend did not run before. We ran the same kernel check on gfx1151 (RDNA3.5, torch 2.13.0+rocm7.15, Triton 3.8.0), once as is and once with the num_stages<=2 workaround forced on (the backend enables it only on gfx1100 and gfx1201). The causal kernel compiles in both runs. All 12 cases pass, with the same numbers in both runs (causal max error 0.038 - 0.047). Before the fix, 9 of 12 cases fail, the same as on gfx90a.