Skip to content

fix(rocm): pass causal and softmax_scale to sageattn in sage_attn2_amd_rocm - #1710

Open
menglcai-amd wants to merge 1 commit into
ModelTC:mainfrom
menglcai-amd:fix/rocm-sage-attn-causal
Open

menglcai-amd wants to merge 1 commit into
ModelTC:mainfrom
menglcai-amd:fix/rocm-sage-attn-causal

Conversation

@menglcai-amd

Copy link
Copy Markdown

What happened

We tested MiniMax-H3-Image on AMD with sage_attn2_amd_rocm as the attention backend of the Qwen3-VL text encoder (qwen3vl_attn_type). The images stopped following the prompt. With the same prompt, torch_sdpa gives the desk scene, but sage_attn2_amd_rocm gave 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:

A cozy wooden desk by a window. On the left, a red ceramic mug with rising steam. On the right, a stack of exactly three blue books. In the middle, a small green cactus in a white pot. On the wall behind, a yellow sticky note that reads HELLO in black marker. Soft morning sunlight, photorealistic.

Same prompt, seed and config. Only qwen3vl_attn_type differs. PSNR is against the torch_sdpa image.

Before this PR:
sage_attn2_amd_rocm (9.3 dB)
After this PR:
sage_attn2_amd_rocm (23.2 dB)
Reference:
torch_sdpa
B_sage_before C_sage_after A_sdpa

After the fix the scene matches the reference. Small details differ because SageAttention quantizes Q and K.

Cause

AmdSageAttn2Weight.apply() gets causal and softmax_scale from the caller, but it calls only sageattn(q, k, v, tensor_layout="NHD"). The Qwen3-VL text encoder calls attention with causal=True and its own softmax_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 passes sm_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=True and softmax_scale), cosmos3 causal_self_attn (causal=True), and common/kvcache/attn/flash.py (softmax_scale).

Fix

Pass both values to sageattn:

output = self.sageattn(q, k, v, tensor_layout="NHD", is_causal=kwargs.get("causal", False), sm_scale=kwargs.get("softmax_scale"))

The defaults are the same as the sageattn defaults (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). Only qwen3vl_attn_type is changed to sage_attn2_amd_rocm; the DiT stays on torch_sdpa, so the text encoder attention is the only difference from the reference. The images are in "Before / After" above.

Kernel check: AmdSageAttn2Weight.apply() against torch.nn.functional.scaled_dot_product_attention with the same is_causal and scale. 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:

kwargs before the fix after the fix
none 0.006 - 0.018 0.006 - 0.018
causal=True 3.23 - 3.41 0.033 - 0.039
softmax_scale=0.03 0.36 - 0.49 0.001 - 0.010
both 3.15 - 3.41 0.008 - 0.016

RDNA: with this fix, causal=True uses 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.

Signed-off-by: menglcai <menglcai@amd.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant