Skip to content

fix(fmoe tune): dedup fused/non-fused stage1 separately - #3805

Merged
lalala-sh merged 6 commits into
ROCm:mainfrom
rbrugaro-amd:rbrugaro/fmoe-tuner-fused-dedup-fix
Jul 21, 2026
Merged

lalala-sh merged 6 commits into
ROCm:mainfrom
rbrugaro-amd:rbrugaro/fmoe-tuner-fused-dedup-fix

Conversation

@rbrugaro-amd

Copy link
Copy Markdown
Contributor

Problem

FmoeTuner.post_process under-selects fused (_fp4/_fp8) stage-1 kernels for a4w4/a8w4 MoE, shipping CK/non-fused configs even when fusion is faster on a fair basis.

Root cause

Selection runs in two passes:

Dedup to the best candidate per (stage, block_m, flat) by raw us.
Quant-fairness penalty: the separately-benchmarked dynamic_mxfp4_quant+sort cost is added to the us1 of every non-_fp4/_fp8 stage-1 (fused kernels embed quant+sort in the epilogue, so it's already in their us1).
A fused stage-1 does that extra quant work in-kernel, so its raw us is slightly higher than an equivalent non-fused kernel at the same block_m. Since both share the dedup key, the fused candidate is evicted in pass 1 — before the pass-2 penalty (which would make it win) is ever applied. The fused kernel is no longer in the pool when idxmin(us1+us2) runs.

Fix

Add an _is_fused flag (kernelName endswith _fp4/_fp8) to the dedup key so the best fused and best non-fused per (stage, block_m, flat) both survive into the penalty + final selection. Helper column dropped immediately; downstream merge/columns unchanged. No effect on shapes without fused candidates.

Validation

DeepSeek-R1 MXFP4 shared-expert-fusion shape (expert=33, topk=9, 7168/2048): re-tuning flips the ≥2048 token tiers from CK to fused flydsl stage-1. A cuda-graph BS=256 decode bench shows ~6% lower latency / higher throughput vs the CK selection

Risk

Low — pure selection-logic change in the offline tuner; no kernel/runtime changes. Shapes with only non-fused candidates are unaffected.

…t dropped pre-penalty

FmoeTuner.post_process dedups candidates to the best per (stage, block_m, flat)
by RAW us BEFORE the quant-fairness penalty is applied to non-fused stage1
kernels. A fused (_fp4/_fp8) stage1 kernel embeds the inter-stage quant+sort in
its epilogue, so its raw us is slightly higher than an otherwise-equivalent
non-fused kernel at the same block_m. With both sharing the dedup key, the fused
candidate is evicted in the dedup pass; the penalty that would make it win
(the separately-benchmarked dynamic_mxfp4_quant+sort cost, added only to
non-_fp4/_fp8 us1) is applied afterwards, to a pool the fused kernel is no
longer in. Result: the tuner systematically under-selects fused stage1 kernels
for a4w4/a8w4 MoE and ships CK/non-fused configs even when fusion is faster on
a fair (quant-credited) basis.

Fix: add an _is_fused flag (kernelName endswith _fp4/_fp8) to the dedup key so
the best fused AND best non-fused per (stage, block_m, flat) both survive into
the penalty + final idxmin(us1+us2) selection. The drop column is removed
immediately so downstream merge/columns are unchanged. No behavior change for
shapes without fused candidates.

Validated on DeepSeek-R1 MXFP4 shared-expert-fusion shape (expert=33, topk=9,
7168/2048): re-tuning flips the >=2048 token tiers from CK to fused flydsl
stage1; a cuda-graph BS=256 decode bench shows ~6% lower latency / higher
throughput vs the CK selection.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@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 3805 --add-label <label>

@rbrugaro-amd
rbrugaro-amd marked this pull request as ready for review June 22, 2026 21:04
@rbrugaro-amd
rbrugaro-amd requested a review from a team June 22, 2026 21:04
rbrugaro-amd added a commit to rbrugaro-amd/aiter that referenced this pull request Jun 23, 2026
…tuned set

Replace the (33,9) tuned rows with the set validated (best measured E2E) on the
DeepSeek-R1 MXFP4 harness. The fused flydsl `_fp4` stage-1 was being missed by
the tuner due to a fused-vs-non-fused selection bug (fixed in ROCm#3805);
it is now picked at the larger token buckets. Small-token buckets intentionally
keep the non-fused kernel where it benchmarked better E2E, so stage-1 is a mix of
fused/non-fused by design. Non-(33,9) rows untouched (match main after ROCm#3825
dsv3-v4 retune). 16 primary + 16 flydsl_fallback rows, 26-col schema.

Signed-off-by: Rita Brugarolas Brufau <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
@zufayu
zufayu requested a review from junhaha666 June 26, 2026 08:54
@okorzh-amd

Copy link
Copy Markdown
Contributor

@valarLip could you please review this PR?

@okorzh-amd
okorzh-amd requested a review from valarLip June 30, 2026 22:52
@coderfeli
coderfeli requested a review from lalala-sh July 1, 2026 01:04
@rbrugaro-amd
rbrugaro-amd requested a review from yadaish July 7, 2026 03:57
…-fused-dedup-fix

# Conflicts:
#	csrc/ck_gemm_moe_2stages_codegen/gemm_moe_tune.py
@rbrugaro-amd

Copy link
Copy Markdown
Contributor Author

Rebased onto latest main and re-tuned the DeepSeek-R1 MXFP4 shared-fusion shape (expert=33, topk=9, 7168/2048, per_1x32) to confirm the selection change still behaves. The ≥2048 tiers still flip CK → fused flydsl stage1 with this patch; reverting just the _is_fused dedup key sends 2048 back to a CK 2-stage kernel (the fused candidate is present in the profile but evicted in the pre-penalty dedup — the exact issue this PR fixes).
Checked the new selection/backends landed since I opened this don't interact with this path:

@lalala-sh lalala-sh 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.

Please simplify the code comments.

@lalala-sh lalala-sh 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

rbrugaro-amd and others added 2 commits July 17, 2026 17:17
Address review feedback on ROCm#3805 (lalala-sh): trim the 6-line comment on
the fused/non-fused dedup split to 3 lines while keeping the rationale
(non-fused-only fairness penalty must not be pre-empted by raw-us dedup).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Signed-off-by: Rita Brugarolas Brufau <[email protected]>
@rbrugaro-amd

Copy link
Copy Markdown
Contributor Author

@lalala-sh addressed your feedback

@rbrugaro-amd

Copy link
Copy Markdown
Contributor Author

@lalala-sh @ROCm/team_aiter test passed and is approved, can we merge please?

@lalala-sh
lalala-sh merged commit 542bd03 into ROCm:main Jul 21, 2026
51 of 52 checks passed
@lalala-sh

Copy link
Copy Markdown
Contributor

@lalala-sh @ROCm/team_aiter test passed and is approved, can we merge please?

Done, thanks for your contribution.

valarLip pushed a commit that referenced this pull request Aug 5, 2026
#3739)

* configs: add DSv3-MXFP4 E=33/topk9 fused-MoE shape (shared-expert fusion)

The existing dsv3_fp4 fused-MoE configs cover the routed-only shapes
(model_dim=7168, inter_dim=2048, expert in {32,64}, topk=8) and the
EP-sharded shapes (inter_dim in {256,512}, expert=257, topk=9). There is
no entry for model_dim=7168, inter_dim=2048, expert=33, topk=9.

That shape appears with shared-experts fusion ON: the always-on shared
expert is folded in as the 33rd grouped-GEMM expert and topk goes 8 -> 9,
so every token routes through it. With no tuned row the runtime fmoe
dispatch MISSes and falls back to the generic ck::kernel_moe_mxgemm_2lds
for BOTH stages, which is ~3x slower on stage2 than the flydsl/cktile
kernels that already exist for the neighbouring shapes.

Adds 32 rows (16 token buckets 1..32768, each a primary best-us row plus
a flydsl_fallback row) to dsv3_fp4_tuned_fmoe.csv, and the 16 matching
input rows to dsv3_fp4_untuned_fmoe.csv. The buckets are the contiguous
powers of two (1,2,4,...,32768) that the runtime's nextPow2 token padding
can request, so there is no cache-miss gap. Same FP4X2 x FP4X2 x BF16 +
per_1x32 dtype combo as the existing dsv3 rows.

Data produced by gemm_moe_tune.py end-to-end on 8x MI355X (gfx950,
cu_num=256), aiter v0.1.14-rc0-209-g6740fb791.

Signed-off-by: Rita Brugarolas Brufau <[email protected]>

* configs(dsv3 mxfp4): update E=33/topk=9 stage-1+stage-2 to validated tuned set

Replace the (33,9) tuned rows with the set validated (best measured E2E) on the
DeepSeek-R1 MXFP4 harness. The fused flydsl `_fp4` stage-1 was being missed by
the tuner due to a fused-vs-non-fused selection bug (fixed in #3805);
it is now picked at the larger token buckets. Small-token buckets intentionally
keep the non-fused kernel where it benchmarked better E2E, so stage-1 is a mix of
fused/non-fused by design. Non-(33,9) rows untouched (match main after #3825
dsv3-v4 retune). 16 primary + 16 flydsl_fallback rows, 26-col schema.

Signed-off-by: Rita Brugarolas Brufau <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Signed-off-by: Rita Brugarolas Brufau <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants