Skip to content

[FlyDSL] [Bugfix] Initialize padded MoE scratch to stop NaN at small token counts (gfx950 a8w4) - #6568

Closed
PerryZhang01 wants to merge 1 commit into
ROCm:mainfrom
PerryZhang01:fix/uninitialized-moe-scratch-nan
Closed

PerryZhang01 wants to merge 1 commit into
ROCm:mainfrom
PerryZhang01:fix/uninitialized-moe-scratch-nan

Conversation

@PerryZhang01

Copy link
Copy Markdown
Contributor

Summary

On gfx950, fused_moe with 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 sorted
length; the producing kernel writes only the rows of valid sorted slots, and the
padded rows keep allocator garbage. 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 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's
ordinary MoE path, not on the mono-kernel path under test.

Issue: #6565

Repro

repro_synthetic.py in this PR is self-contained (random weights and routing,
no vLLM, no model):

$ python repro_synthetic.py
NaN in 31/88 calls over 8 rounds (per round: [3, 2, 5, 2, 5, 5, 4, 5])

$ python repro_synthetic.py --small-only          # no large-M calls first
NaN in 0/48 calls over 8 rounds

$ python repro_synthetic.py --zero-empty          # every torch.empty zeroed
NaN in 0/88 calls over 8 rounds

Shape: model_dim=5120, inter_dim=640 (intermediate_pad=64), E=384, topk=6, Silu, g1u1, stage1 flydsl_moe1_afp8_wfp4_bf16_t32x64x256_w3_gui_kw2_fp8, stage2
flydsl_moe2_layout_afp8_wfp4_bf16_t32x256x128_reduce_sbm32.

What this PR changes

Five allocations that share the pattern switch to torch.zeros:

  1. ops/flydsl/moe_kernels.py, the if out is None: block — stage-1 output values
  2. ops/flydsl/moe_kernels.py:1646 — stage-1 output scale (padded to 256 rows / 8 cols)
  3. fused_moe.py — the a2 stage1->stage2 intermediate (bf16)
  4. ops/quant.py:1161 — activation-quant e8m0 scale scratch
  5. ops/quant.py:586 / :595 — e8m0 scale allocated as uint8 then .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:

check before after
standalone repro, v0.1.25 (24 rounds) 31/88 per 8 rounds 0 / 264
standalone repro, main @ 9bedcd4 (12 rounds) 31/88 per 8 rounds 0 / 132
vLLM CI test test_dsv41_mono_layers.py, TP2+TP4 flaky NaN passing (see PR comments for the running count)

The patch applies cleanly to both v0.1.25 and main.

Cost, and why you may want a different fix

M stock this PR delta
2 0.141 ms 0.158 ms +12.1%
4 0.159 ms 0.176 ms +10.7%
1439 0.537 ms 0.558 ms +3.9%
16384 2.013 ms 2.064 ms +2.5%

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 and
cost 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).

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]>
@PerryZhang01
PerryZhang01 requested a review from a team October 15, 2026 07:30
@github-actions github-actions Bot changed the title [Bugfix] Initialize padded MoE scratch to stop NaN at small token counts (gfx950 a8w4) [FlyDSL] [Bugfix] Initialize padded MoE scratch to stop NaN at small token counts (gfx950 a8w4) Oct 15, 2026
@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:windows Windows JIT op smoke test on the gfx1201 ROCm runner
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 6568 --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.

@PerryZhang01

Copy link
Copy Markdown
Contributor Author

Validation of the fix

Numbers from MI355X (gfx950), ROCm 7.2.3, in the vllm/vllm-openai-rocm:nightly
container. "before" is stock aiter, "after" is with the five allocations
initialized.

Standalone repro (no vLLM)

tree before after
v0.1.25 31/88 NaN over 8 rounds 0 / 264 over 24 rounds
main @ 9bedcd4 31/88 NaN over 8 rounds 0 / 132 over 12 rounds

The per-round counts are identical on both trees ([3, 2, 5, 2, 5, 5, 4, 5]),
and both select the same kernel pair, so the two versions are on the same path.

The vLLM test that found this — tests/models/test_dsv41_mono_layers.py,
unmodified, each run covering TP2 and TP4:

run 1: 2 passed in 309.20s
run 2: 2 passed in 194.49s
run 3: 2 passed in 206.44s
RESULT: pass=3 fail=0   (6 cells, all green)

Before the fix the same test failed intermittently with
AssertionError: (0, 1, (973, 973), 'residual', nan); locally it reproduced in
roughly 2 runs out of 11.

Why all five allocations are needed — with only three of them initialized
(stage-1 values, a2, and the quant.py e8m0 scratch), the standalone repro
still hit 1/88 and the vLLM test still failed 1 run in 10 with a genuine NaN.
Every two-of-three subset failed within 3 runs. The two that are easy to miss
are the stage-1 output scale (the companion to the stage-1 output values) and
the e8m0 scale that is allocated as uint8 and then .view()ed, which a search
by dtype does not find.

A measurement of the zeroing overhead follows in the next comment.

PerryZhang01 pushed a commit to PerryZhang01/vllm that referenced this pull request Oct 15, 2026
…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]>
PerryZhang01 pushed a commit to PerryZhang01/vllm that referenced this pull request Oct 15, 2026
…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]>
PerryZhang01 pushed a commit to PerryZhang01/vllm that referenced this pull request Oct 15, 2026
…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]>
PerryZhang01 pushed a commit to PerryZhang01/vllm that referenced this pull request Oct 15, 2026
…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 divakar-amd 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. Encountered similar issue caused due to torch.empty

Comment thread aiter/fused_moe.py
Comment on lines +4498 to +4500
# zeros(): stage 1 leaves the rows of padded sorted slots untouched and
# stage 2 reads them.
a2 = torch.zeros(

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.

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

azaidy pushed a commit that referenced this pull request Nov 10, 2026
)

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants