Repository navigation
[FlyDSL] [Bugfix] Initialize padded MoE scratch to stop NaN at small token counts (gfx950 a8w4) - #6568
[FlyDSL] [Bugfix] Initialize padded MoE scratch to stop NaN at small token counts (gfx950 a8w4)#6568PerryZhang01 wants to merge 1 commit into
Conversation
On gfx950, a8w4 fused_moe (FP8 activations x MXFP4 weights, per_1x32) intermittently returned NaN for small token counts (M = 2, 3, 4). Several scratch buffers on this path are allocated with torch.empty and sized for the padded sorted length, while the producing kernel writes only the rows of valid sorted slots. The padded slots keep allocator garbage, and in FP8 / E8M0 a garbage 0xFF byte is NaN, so one unwritten scale row turns a whole token's output row into NaN. It needs a larger-M call earlier in the same process to leave usable garbage in freed blocks, which is why it showed up as a flaky decode failure. Five allocations share the pattern and all five have to be initialized before the fault disappears: - ops/flydsl/moe_kernels.py, the `if out is None:` block: stage-1 output - ops/flydsl/moe_kernels.py: stage-1 output scale (padded 256 rows / 8 cols) - fused_moe.py: the a2 stage1 -> stage2 intermediate - ops/quant.py: the activation-quant e8m0 scale scratch - ops/quant.py: the e8m0 scale allocated as uint8 and .view()ed Verified with a standalone repro (no vLLM): 0 NaN in 264 calls over 24 rounds on v0.1.25 and 0/132 over 12 rounds on main, against 31/88 before. The vLLM CI test that found this (dsv41-mono-decode-numerics-amd, DeepSeek-V4.1-Flash at TP4) also stops failing. This is a stopgap: zeroing costs +11% at decode-sized M, because the scratch is sized by the padded sorted length regardless of M (at M=4 it zeroes ~7.9 MB to serve 24 valid slots). Initializing the padded slots in the sort / stage-1 kernels, or skipping them in the consumer via num_valid_ids, would fix the class at its source and cost nothing. Fixes: ROCm#6565 Co-Authored-By: Claude Opus 5 <[email protected]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
Validation of the fixNumbers from MI355X (gfx950), ROCm 7.2.3, in the Standalone repro (no vLLM)
The per-round counts are identical on both trees ( The vLLM test that found this — Before the fix the same test failed intermittently with Why all five allocations are needed — with only three of them initialized A measurement of the zeroing overhead follows in the next comment. |
…ITER AITER returns NaN from the a8w4 MoE (FP8 activations x MXFP4 weights) at decode-sized batches when a non-zero intermediate_pad is declared. Several scratch buffers on that path are allocated with torch.empty, sized for the padded sorted length, and read where the producing kernel never wrote; in FP8/E8M0 a garbage 0xFF byte is NaN, so a whole token row comes back NaN. Details and a standalone repro: ROCm/aiter#6565 The declaration is redundant for MXFP4 anyway. create_weights allocates w13/w2 and their scales with torch.zeros at the padded intermediate size and the checkpoint fills only the real rows, so the padding trims itself: stage1's padded N rows compute x @ 0 = 0, and stage2's padded K columns contribute anything @ 0 = 0. hidden_pad is deliberately left untouched: the activation columns it covers are not guaranteed to be zero, so dropping it would not be equivalent. This is a workaround; revert it once the AITER fix (ROCm/aiter#6568) is merged and pinned. Co-Authored-By: Claude Opus 5 <[email protected]>
…ITER AITER returns NaN from the a8w4 MoE (FP8 activations x MXFP4 weights) at decode-sized batches when a non-zero intermediate_pad is declared. Several scratch buffers on that path are allocated with torch.empty, sized for the padded sorted length, and read where the producing kernel never wrote; in FP8/E8M0 a garbage 0xFF byte is NaN, so a whole token row comes back NaN. Details and a standalone repro: ROCm/aiter#6565 The declaration is redundant for MXFP4 anyway. create_weights allocates w13/w2 and their scales with torch.zeros at the padded intermediate size and the checkpoint fills only the real rows, so the padding trims itself: stage1's padded N rows compute x @ 0 = 0, and stage2's padded K columns contribute anything @ 0 = 0. hidden_pad is deliberately left untouched: the activation columns it covers are not guaranteed to be zero, so dropping it would not be equivalent. This is a workaround; revert it once the AITER fix (ROCm/aiter#6568) is merged and pinned. Co-Authored-By: Claude Opus 5 <[email protected]>
…ITER AITER returns NaN from the a8w4 MoE (FP8 activations x MXFP4 weights) at decode-sized batches when a non-zero intermediate_pad is declared. Several scratch buffers on that path are allocated with torch.empty, sized for the padded sorted length, and read where the producing kernel never wrote; in FP8/E8M0 a garbage 0xFF byte is NaN, so a whole token row comes back NaN. Details and a standalone repro: ROCm/aiter#6565 The declaration is redundant for MXFP4 anyway. create_weights allocates w13/w2 and their scales with torch.zeros at the padded intermediate size and the checkpoint fills only the real rows, so the padding trims itself: stage1's padded N rows compute x @ 0 = 0, and stage2's padded K columns contribute anything @ 0 = 0. hidden_pad is deliberately left untouched: the activation columns it covers are not guaranteed to be zero, so dropping it would not be equivalent. This is a workaround; revert it once the AITER fix (ROCm/aiter#6568) is merged and pinned. Co-Authored-By: Claude Opus 5 <[email protected]>
…ITER AITER returns NaN from the a8w4 MoE (FP8 activations x MXFP4 weights) at decode-sized batches when a non-zero intermediate_pad is declared. Several scratch buffers on that path are allocated with torch.empty, sized for the padded sorted length, and read where the producing kernel never wrote; in FP8/E8M0 a garbage 0xFF byte is NaN, so a whole token row comes back NaN. Details and a standalone repro: ROCm/aiter#6565 The declaration is redundant for MXFP4 anyway. create_weights allocates w13/w2 and their scales with torch.zeros at the padded intermediate size and the checkpoint fills only the real rows, so the padding trims itself: stage1's padded N rows compute x @ 0 = 0, and stage2's padded K columns contribute anything @ 0 = 0. hidden_pad is deliberately left untouched: the activation columns it covers are not guaranteed to be zero, so dropping it would not be equivalent. This is a workaround; revert it once the AITER fix (ROCm/aiter#6568) is merged and pinned. Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: perzhang <[email protected]>
divakar-amd
left a comment
There was a problem hiding this comment.
LGTM. Encountered similar issue caused due to torch.empty
| # zeros(): stage 1 leaves the rows of padded sorted slots untouched and | ||
| # stage 2 reads them. | ||
| a2 = torch.zeros( |
There was a problem hiding this comment.
Broadening to gfx942:
fp8_tensor_tensor on gfx942/MI300X, zeroed (not inflated) real-row output, triggered via vLLM's own garbage-padding test
This fix will also help with the following vllm test. It failed as all the outputs got zeroed out:
https://buildkite.com/vllm/amd-ci/builds/15028/list?jid=01a1588b-5e12-4cae-be2b-dc20cc45134c&tab=output
) fused_moe_2stages allocates the stage1 output a2 with torch.empty. Stage1 writes only the (token, k) slots that moe_sorting keeps. It does not write slots with topk_ids == -1 (padding tokens) or slots that expert_mask removes (experts on other EP ranks). For QuantType.per_Tensor with a2_scale=None, per_tensor_quant_hip then takes one amax over the full buffer, so stale memory in those slots sets the scale for all tokens. A large finite value rounds every real row to fp8 zero, and inf gives NaN output. Allocate a2 with torch.zeros on this path only. Other paths keep torch.empty. Add op_tests/test_moe_per_tensor_a2_init.py, which leaves 1e30 or inf in freed allocator blocks before the call, for padding rows and for an EP expert_mask. #6568 zeroes a2 on every path for its a8w4 case and also covers this bug. This change touches only the path whose quant reads the full buffer, so other quant types keep the cheaper torch.empty. Signed-off-by: Andreas Karatzas <[email protected]>
Summary
On gfx950,
fused_moewith FP8 activations x MXFP4 weights (QuantType.per_1x32)intermittently writes NaN for small token counts (M = 2, 3, 4). Scratch buffers
on this path are allocated with
torch.empty, sized for the padded sortedlength; the producing kernel writes only the rows of valid sorted slots, and the
padded rows keep allocator garbage. In FP8 / E8M0 a garbage
0xFFbyte is NaN,so one unwritten scale row turns a whole token's output row into NaN.
It needs a larger-M call earlier in the same process to leave usable garbage in
freed blocks, which is why it shows up as a flaky decode failure rather than a
deterministic one.
Found from the vLLM upstream CI job
dsv41-mono-decode-numerics-amd(DeepSeek-V4.1-Flash at TP4), red intermittently with
AssertionError: (0, 1, (973, 973), 'residual', nan). The NaN is on vLLM'sordinary MoE path, not on the mono-kernel path under test.
Issue: #6565
Repro
repro_synthetic.pyin this PR is self-contained (random weights and routing,no vLLM, no model):
Shape:
model_dim=5120, inter_dim=640 (intermediate_pad=64), E=384, topk=6, Silu, g1u1, stage1flydsl_moe1_afp8_wfp4_bf16_t32x64x256_w3_gui_kw2_fp8, stage2flydsl_moe2_layout_afp8_wfp4_bf16_t32x256x128_reduce_sbm32.What this PR changes
Five allocations that share the pattern switch to
torch.zeros:ops/flydsl/moe_kernels.py, theif out is None:block — stage-1 output valuesops/flydsl/moe_kernels.py:1646— stage-1 output scale (padded to 256 rows / 8 cols)fused_moe.py— thea2stage1->stage2 intermediate (bf16)ops/quant.py:1161— activation-quant e8m0 scale scratchops/quant.py:586/:595— e8m0 scale allocated asuint8then.view(fp8_e8m0)All five are required: with only 1, 3 and 4 the standalone repro still hits
1/88 and the vLLM test still failed 1 run in 10 with a real NaN. Every
two-of-three subset of 1, 3, 4 failed within 3 runs.
With all five:
test_dsv41_mono_layers.py, TP2+TP4The patch applies cleanly to both v0.1.25 and main.
Cost, and why you may want a different fix
The cost is worst at decode-sized M, because the scratch is sized by the padded
sorted length regardless of M — at M=4 this zeroes ~7.9 MB (12320 x 640) to
serve 24 valid slots.
Initializing the padded slots inside the sort / stage-1 kernels, or skipping
them in the consumer via
num_valid_ids, would fix the class at its source andcost nothing at decode. We are happy to drop this patch in favour of that. We
have not identified which read in the DSL kernel picks up the unwritten rows.
Environment
vllm/vllm-openai-rocm:nightly(nightly-77154ca6), amd-aiter 0.1.25,ROCm 7.2.3, amdgpu 6.14.14, MI355X (gfx950).