Skip to content

[CK] [FlyDSL] Optimize MI308 MoE Down kernels and add BF16 support (2/3) - #5932

Merged
coderfeli merged 21 commits into
ROCm:mainfrom
luocheng25:luocheng/moe_gemm_308_opt2
Oct 20, 2026
Merged

coderfeli merged 21 commits into
ROCm:mainfrom
luocheng25:luocheng/moe_gemm_308_opt2

Conversation

@luocheng25

@luocheng25 luocheng25 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 2/3, follows #5795 (PR1, merged). Optimize FP8 MoE Down projection on MI308X/gfx942 and enable BF16 whole-MoE dispatch through the existing default kernels.

Changes

  • Add four-wave M64×N256 and eight-wave M256×N128 Down kernels, exposed as the whole-MoE configs 1x4_64x256 and 8x1_compact. Compact dispatch builds M256 full tasks and M64 tail tasks on the GPU without host readback.
  • Specialized Down writes padded, non-atomic output that a stride-aware sorted reduction combines; the M64 Gate/Up metadata layout is unchanged.
  • BF16 weights (QuantType.No) support batch1, fixed BN64 decode and default prefill. BF16 tile/LDS limits are checked before compilation, specialized Down stays FP8-only, and unsupported BF16 configs are filtered during tuning.
  • The new Down kernels in kernels/moe_gemm_2stage/gfx9_fp8/ are grouped by wave layout: gemm2_1x4.py (four-wave M64×N256), gemm2_8x1.py and gemm2_8x1_compact.py (eight-wave and compact). Shared helpers live in common.py; gemm2.py keeps main's Down kernels and only adds the down_path dispatch.
  • Update six model/precision CSVs (202 shapes), adding Qwen3.8-Flash-Next BF16 and FP8. All five 64K rows select specialized Down; the runtime token-bucket policy is unchanged (B65536 runs with the B32768 config, which uses the same kernel).

Performance

MI308X/gfx942 (80 CUs, PTL format:VECTOR,F8), ROCm 7.2.4, FlyDSL 0.3.4.1, default MoE sorting, AITER_FLYDSL_MOE_BF16_RTA_SIMPLIFIED=1. Values are the per-shape GPU kernel-time sums that --run_config reports (10 warmups, 51 iterations, rotating inputs, IQR filtering); Down and aux split the same trace by kernel. Each value is the geometric mean over shapes of the median of three alternating-order rounds; negative means faster.

Tuning. Main (42e87267a838, includes #5795) and this PR were tuned on the same shapes from empty CSVs with every kernel family available on gfx942 (TUNE_ONLY=flydslv2,cktile,flydsl,asm), except ASM stage1 (AITER_FMOE_TUNE_EXCLUDE_KERNELS=fmoe_stage1). Several ASM stage1 kernels return NaN or wrong results end to end when device memory is not zeroed; #3987 traced one to padded sorted rows left unwritten. Known cases (prefix fmoe_stage1_bf16_pertokenFp8_g1u1_, NaN-filled memory, E=513 shapes): 16x64_5tg_pf3 (#3987), 16x128_4tg_pf2/pf3, 32x64_4tg_pf2/pf3, 32x128_3tg_pf2/pf3, 64x64_2tg_pf3 and 64x128_2tg_pf2/pf3. ASM 1-stage stays in the search. After the last eight-wave Down change (no register spills up to inter_dim 640), the whole-MoE stage (--e2e_tune) was re-run for the Qwen3.5-35B (inter_dim 512) and Qwen3.8-Flash-Next (inter_dim 640) FP8 PTPC shapes; a changed selection was kept only if it won all six paired timings against the previous one (six rows).

New Down kernels. On the shapes where this PR selects them:

Model / precision Batch Shapes Down Down + aux Full MoE
Hunyuan3 / FP8 per-tensor 512–128K 9/18 -20.97% -11.85% -5.43%
MiniMax-M3 / FP8 PTPC 512–128K 17/36 -8.89% -4.65% -2.01%
Qwen3.5-35B / FP8 PTPC 1K–128K 11/34 -18.34% -10.97% -5.53%
Qwen3.5-397B / FP8 PTPC 2K–128K 7/18 -23.44% -13.46% -6.79%
Qwen3.8-Flash-Next / FP8 PTPC¹ 2K–32K 14/48 -10.99%² -6.94%² -3.18%
All 58/154 -15.15%² -8.70%² -4.09%

"Shapes" counts these shapes against all tuned shapes of the model and "Batch" is their token range: the new Down kernels target large batches (512 tokens and up), while smaller batches keep decode, CK or ASM 1-stage kernels. "Aux" covers compact task construction, inverse routing and sorted reduction.
¹ Main ships no Qwen3.8-Flash-Next config; for this row main was tuned on the same shapes.
² Excludes the two shapes where main selects ASM 1-stage, which has no separate Down kernel.

New Qwen3.8-Flash-Next configs. Main ships none and runs its untuned defaults (CK 2-stage for BF16, mostly ASM 1-stage for FP8). Against main as shipped, the new configs cut MoE latency by 29.35% for BF16 and 16.63% for FP8 PTPC (geometric mean over 48 shapes each).

Reproduce (MI308X, gfx942, 80 CUs)

Run the same commands on main (42e87267a838) and on this PR. Main lacks the two qwen38_flash_next_*_untuned_fmoe.csv files; copy them from this PR.

export AITER_FLYDSL_MOE_BF16_RTA_SIMPLIFIED=1 TUNE_ONLY=flydslv2,cktile,flydsl,asm AITER_FMOE_TUNE_EXCLUDE_KERNELS=fmoe_stage1
T=csrc/ck_gemm_moe_2stages_codegen/gemm_moe_tune.py
for m in hunyuan3_fp8_per_tensor minimax_m3_fp8_ptpc qwen3_5_35b_fp8_ptpc qwen3_5_397b_fp8_ptpc qwen38_flash_next_bf16 qwen38_flash_next_fp8_ptpc; do
  a="-i aiter/configs/model_configs/${m}_untuned_fmoe.csv -o /tmp/${m}_tuned_fmoe.csv --mp 1 --warmup 10 --iters 51 --all"
  python3 $T $a                                 # staged kernels
  python3 $T $a --e2e_tune                      # whole-MoE FlyDSL configs
  python3 $T $a --run_config /tmp/${m}_tuned_fmoe.csv   # per-shape latency, with accuracy check
done

Main as shipped for Qwen3.8-Flash-Next: on main, run python3 $T $a --run_config (no CSV, untuned defaults) for both precisions.

Testing (this PR):

export AITER_FLYDSL_MOE_BF16_RTA_SIMPLIFIED=1
# method 1
python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter impl__flydsl_gfx942__
# method 2
python3 -m unittest op_tests.tuning_tests.test_config_shape_collision -v
C=aiter/configs/model_configs
TUNE_TEST_FAMILY=fmoe \
TUNE_TEST_CONFIG=$C/hunyuan3_fp8_per_tensor_tuned_fmoe.csv:$C/minimax_m3_fp8_ptpc_tuned_fmoe.csv:$C/qwen3_5_35b_fp8_ptpc_tuned_fmoe.csv:$C/qwen3_5_397b_fp8_ptpc_tuned_fmoe.csv:$C/qwen38_flash_next_bf16_tuned_fmoe.csv:$C/qwen38_flash_next_fp8_ptpc_tuned_fmoe.csv \
python3 -m unittest op_tests.tuning_tests.test_run_config.TestRunConfigCustom -v
# method 3
python3 op_tests/test_flydsl_moe_gfx942.py

Co-authors: [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: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; main branch always runs both MI35X and MI300X
multigpu 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: 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 5932 --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.

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Three unresolved critical findings concern compact task-buffer sizing and out-of-range quantization accesses.

Review effort: Lite
Findings: 3 High severity

Open (3)
What changed in this PR

Optimizes gfx942 FlyDSL MoE Down kernels and adds BF16 dispatch support, with refreshed tuning configurations.

Changes:

  • Adds specialized Down kernels and compact GPU task scheduling.
  • Enables BF16 default-kernel dispatch and validation.
  • Updates quantization, reduction helpers, and model tuning CSVs.
File Summary
csrc/​ck_gemm_moe_2stages_codegen/​gemm_moe_tune.py Adds BF16 tuning filters.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​quant.py Adds quantization helpers; critical out-of-range atom accesses remain unresolved.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​moe_reduce.py Adds stride-aware reduction helpers.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​gemm2.py Adds default Down and BF16 support.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​gemm2_common.py Adds specialized kernel primitives.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​gemm2_8x1_compact.py Adds compact task tables; allocator and builder policies remain mismatched.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage/​__init__.py Exposes kernel APIs.
aiter/​ops/​flydsl/​kernels/​moe_gemm_2stage_gfx942_utils.py Updates gfx942 utilities.
aiter/​ops/​flydsl/​fused_moe_gfx942.py Integrates dispatch and BF16 validation.
aiter/​configs/​model_configs/​qwen3_5_397b_fp8_ptpc_tuned_fmoe.csv Refreshes Qwen 397B FP8 tuning.
aiter/​configs/​model_configs/​qwen3_5_35b_fp8_ptpc_tuned_fmoe.csv Refreshes Qwen 35B FP8 tuning.
aiter/​configs/​model_configs/​qwen3_5_125b_fp8_ptpc_untuned_fmoe.csv Adds Qwen 125B FP8 untuned configurations.
aiter/​configs/​model_configs/​qwen3_5_125b_fp8_ptpc_tuned_fmoe.csv Adds Qwen 125B FP8 tuned results.
aiter/​configs/​model_configs/​qwen3_5_125b_bf16_untuned_fmoe.csv Adds Qwen 125B BF16 untuned configurations.
aiter/​configs/​model_configs/​qwen3_5_125b_bf16_tuned_fmoe.csv Adds Qwen 125B BF16 tuned results.
aiter/​configs/​model_configs/​minimax_m3_fp8_ptpc_tuned_fmoe.csv Refreshes MiniMax FP8 tuning.
aiter/​configs/​model_configs/​hunyuan3_fp8_per_tensor_tuned_fmoe.csv Refreshes Hunyuan FP8 tuning.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

A critical device/CU-topology cache issue remains, and the changes require human review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (3)

Comment thread aiter/ops/flydsl/fused_moe_gfx942.py Resolved

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

A critical BF16 quantization-contract issue and missing gfx942 correctness coverage require further review.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (1)

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

New compact task-table and padded-reduction paths lack dedicated correctness coverage.

Review effort: Lite
Findings: None

Resolved since last review (1)

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Critical compact-dispatch and padded-output reduction issues remain unresolved.

Review effort: Lite
Findings: 2 High severity

Open (2)

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Add targeted tests for compact task-table boundary distributions and validate generated task counts and rows.

Review effort: Lite
Findings: 1 High severity

Open (1)
Resolved since last review (2)

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Critical BF16 dispatch and gfx942 LDS-validation issues remain unresolved.

Review effort: Lite
Findings: 2 High severity

Open (2)
Resolved since last review (1)

Comment thread aiter/ops/flydsl/fused_moe_gfx942.py Resolved
Comment thread aiter/ops/flydsl/fused_moe_gfx942.py Resolved
@luocheng25
luocheng25 requested a lite review from Copilot October 7, 2026 22:57

Copilot AI 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.

🔵 Needs a closer look

A moderate tuner eligibility issue remains unresolved, and the kernel/dispatch changes require human review.

0 open findings

🧠 Review effort: Lite

@zufayu
zufayu requested a review from yadaish October 13, 2026 01:42
Copilot AI lite review requested due to automatic review settings October 13, 2026 05:20

Copilot AI 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.

🔵 Needs a closer look

The changes span multiple GPU kernels, runtime dispatch paths, BF16 support, and tuning configurations, requiring final human review.

0 open findings

🧠 Review effort: Lite

Comment thread aiter/ops/flydsl/fused_moe_gfx942.py Resolved
@Bernard-Liu

Copy link
Copy Markdown
Contributor

The new whole-MoE FlyDSL configs all use cu_num=80, so _iter_csv_cases() filters them out on the default MI300X/MI35X CI runners. Passing CI therefore does not validate these paths. Please add automated MI308 numerical regression coverage, including compact-task split thresholds, empty/single-expert cases, E=2048, capacity limits, topology changes, and padded reduction. The existing local checks should be preserved as repeatable regression tests.

Copilot AI lite review requested due to automatic review settings October 19, 2026 11:10

Copilot AI 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.

🟡 Changes recommended

Address the runtime architecture and CU-count helper issues in the specialized dispatch paths.

1 open issue

Review effort: Lite

1, compatible old csv format string
2, cu_count as a runtime arg
3, tail_capacity as a parameter for compact_m64_tasks
4, add test
…lvm.target_features` to workaround `target-features:-packed-fp32-ops`; fix cross-group lds race condition without correct `s_waitcnt lgkmcnt`
Copilot AI lite review requested due to automatic review settings October 19, 2026 14:18

Copilot AI 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.

🔵 Needs a closer look

Fix shared architecture detection and ensure tests enumerate the shipped configuration files.

1 open issue

Review effort: Lite

Copilot AI lite review requested due to automatic review settings October 20, 2026 01:12
@luocheng25

Copy link
Copy Markdown
Contributor Author

The new whole-MoE FlyDSL configs all use cu_num=80, so _iter_csv_cases() filters them out on the default MI300X/MI35X CI runners. Passing CI therefore does not validate these paths. Please add automated MI308 numerical regression coverage, including compact-task split thresholds, empty/single-expert cases, E=2048, capacity limits, topology changes, and padded reduction. The existing local checks should be preserved as repeatable regression tests.

Added op_tests/test_flydsl_moe_gfx942.py. It calls the backend directly, so it runs on any gfx942 runner, whatever the cu_num:

  • all 134 shipped FlyDSL rows (1 to 131072 tokens) against the torch reference, timed in the summary table;
  • E=2048, a single expert and Down padding 0/32/64 as functional checks;
  • compact task tables for 304 and 80 CUs (split thresholds, empty experts, exact tail capacity, E=2048, mismatched buffers), with canaries around every buffer; every compact run is repeated under the other CU count;
  • the padded sorted reduction for padding 0/32/64/128, checked bit-exact.

Copilot AI 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.

🔵 Needs a closer look

Restrict specialized dispatch to supported gfx942 CU topologies or explicitly reject unknown mappings.

No open issues 🎉

1 resolved since last review

Review effort: Lite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants