Skip to content

fix(fused_moe): require KBatch >= 2 for block-fp8 split-k - #3997

Merged
junhaha666 merged 1 commit into
ROCm:mainfrom
ZiguanWang:fix-fp8-moe-kbatch1
Jul 15, 2026
Merged

junhaha666 merged 1 commit into
ROCm:mainfrom
ZiguanWang:fix-fp8-moe-kbatch1

Conversation

@ZiguanWang

Copy link
Copy Markdown
Contributor

Motivation

get_ksplit could pick a split where each K partition owns a single k-tile (KBatch = model_dim / (split * 256) == 1). In that case the CK stage1 kernel still uses atomic-add but skips the output memset, accumulating onto uninitialized memory and producing gibberish at bs=1 (e.g. Qwen3.5-FP8 TP8, K=4096, split=16). Add the KBatch >= 2 condition in get_ksplit, and guard ck_moe_stage1 so splitk from tuned CSV / AITER_KSPLIT cannot bypass it.

Technical Details

Test Plan

Add op_tests/test_moe_blockscale_ksplit.py: exercises the high-level fused_moe() CK 2-stage path (which get_ksplit feeds, unlike test_moe_blockscale) against a bf16 pre-quant reference, asserting row-0 rel error stays within fp8 block-quant noise.

Test Result

Submission Checklist

@ZiguanWang
ZiguanWang requested a review from a team June 29, 2026 12:28
@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:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 3997 --add-label <label>

valarLip
valarLip previously approved these changes Jun 29, 2026
@zufayu
zufayu requested a review from junhaha666 June 30, 2026 02:31
@junhaha666

Copy link
Copy Markdown
Contributor

((model_dim // i) // tilek >= 2) in get_ksplit() can it be removed? I noticed that this issue only exists in the CK backend, and you have already made the fix at the CK interface.

get_ksplit could pick a split where each K partition owns a single k-tile
(KBatch = model_dim / (split * 256) == 1). In that case the CK stage1 kernel
still uses atomic-add but skips the output memset, accumulating onto
uninitialized memory and producing gibberish at bs=1 (e.g. Qwen3.5-FP8 TP8,
K=4096, split=16). Add the KBatch >= 2 condition in get_ksplit, and guard
ck_moe_stage1 so splitk from tuned CSV / AITER_KSPLIT cannot bypass it.

Add op_tests/test_moe_blockscale_ksplit.py: exercises the high-level
fused_moe() CK 2-stage path (which get_ksplit feeds, unlike test_moe_blockscale)
against a bf16 pre-quant reference, asserting row-0 rel error stays within fp8
block-quant noise.
@ZiguanWang

Copy link
Copy Markdown
Contributor Author

((model_dim // i) // tilek >= 2) in get_ksplit() can it be removed? I noticed that this issue only exists in the CK backend, and you have already made the fix at the CK interface.

already removed.

@junhaha666
junhaha666 merged commit 152bdd5 into ROCm:main Jul 15, 2026
53 of 55 checks passed
jin-amd added a commit to jin-amd/aiter that referenced this pull request Sep 30, 2026
…it-K

The default fp8-blockscale 2-stage fused MoE returns wrong output whenever
get_ksplit() asks for model_dim / 256 splits, one 256-wide K tile each. This
happens at small decode batches on gfx942 (cu_num 304), for example at one
token for (model_dim, inter_dim) = (4096, 256) with 288 experts and top-8,
(7168, 128) with 257 experts and top-9, and (2048, 384) with 128 experts and
top-8. Every output element differs from the reference, and the values
change from run to run.

ck_moe_stage1_gemm turns the requested number of splits into CK's KBatch,
the number of KPerBlock tiles each split covers: K / (splitk * KPerBlock).
With one tile per split that is 1, which DeviceMoeGemmBlockScale reads as
"no split": it runs a single z-block over the whole K and skips zeroing the
output. The split-K instance still accumulates into that output atomically,
and fused_moe allocates it with torch.empty. StrideE also keys the split-K
layout (2 * N per row) on KBatch > 1, so the rows overlap as well.

ROCm#3997 fixed this in ck_moe_stage1() by turning split-K off for such
requests, and the MoE refactor in ROCm#4394 dropped that guard. This fix sits at
the KBatch conversion instead, so no caller reaches KBatch == 1 through
split-K, whether the split comes from get_ksplit(), a tuned CSV or
AITER_KSPLIT. It also keeps the split rather than turning it off: when
splitk would leave fewer than two tiles per split, stage 1 uses fewer splits
of at least two tiles, KBatch = 2 or the smallest divisor of the tile count
that is at least 2. At the (4096, 256, E=288, top-8) one-token shape the
operator then takes 32.1 us, against 41.1 us without split-K. A K of a
single tile cannot be split and now fails with an error. Requests that
already give two or more tiles per split are unchanged.

Signed-off-by: Jin Tao <[email protected]>
yifehuan pushed a commit that referenced this pull request Oct 2, 2026
…it-K (#5979)

The default fp8-blockscale 2-stage fused MoE returns wrong output whenever
get_ksplit() asks for model_dim / 256 splits, one 256-wide K tile each. This
happens at small decode batches on gfx942 (cu_num 304), for example at one
token for (model_dim, inter_dim) = (4096, 256) with 288 experts and top-8,
(7168, 128) with 257 experts and top-9, and (2048, 384) with 128 experts and
top-8. Every output element differs from the reference, and the values
change from run to run.

ck_moe_stage1_gemm turns the requested number of splits into CK's KBatch,
the number of KPerBlock tiles each split covers: K / (splitk * KPerBlock).
With one tile per split that is 1, which DeviceMoeGemmBlockScale reads as
"no split": it runs a single z-block over the whole K and skips zeroing the
output. The split-K instance still accumulates into that output atomically,
and fused_moe allocates it with torch.empty. StrideE also keys the split-K
layout (2 * N per row) on KBatch > 1, so the rows overlap as well.

#3997 fixed this in ck_moe_stage1() by turning split-K off for such
requests, and the MoE refactor in #4394 dropped that guard. This fix sits at
the KBatch conversion instead, so no caller reaches KBatch == 1 through
split-K, whether the split comes from get_ksplit(), a tuned CSV or
AITER_KSPLIT. It also keeps the split rather than turning it off: when
splitk would leave fewer than two tiles per split, stage 1 uses fewer splits
of at least two tiles, KBatch = 2 or the smallest divisor of the tile count
that is at least 2. At the (4096, 256, E=288, top-8) one-token shape the
operator then takes 32.1 us, against 41.1 us without split-K. A K of a
single tile cannot be split and now fails with an error. Requests that
already give two or more tiles per split are unchanged.

Signed-off-by: Jin Tao <[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.

3 participants