Skip to content

[FlyDSL] [Perf] A16W4 MoE: optional fused bf16 shared expert - #7773

Merged
coderfeli merged 7 commits into
mainfrom
vinayak/a16w4-shared-expert
Nov 17, 2026
Merged

coderfeli merged 7 commits into
mainfrom
vinayak/a16w4-shared-expert

Conversation

@vgokhale

@vgokhale vgokhale commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Allows the A16W4 MoE kernels (moe_2stage_a16wmix gemm1/gemm2) to add a dense bf16 shared expert inside the routed launches. The shared expert then needs no side stream. FHMoE supports this, but has separate topk/routing kernels while this assumes that information is available already. This allows for a separately fused topk/routing kernel.

Changes

  • gemm1/gemm2: the shared tiles take the first program ids. The routed tiles and the sort do not change.
  • API: flydsl_moe_stage1/2 take fused_shared_w1 / fused_shared_w1_scale (stage 1), fused_shared_w2 / fused_shared_w2_scale (stage 2) and shared_inter_bf16. fused_moe_2stages passes them in _stage1_extra_args / _stage2_extra_args.
  • The shared weights are MXFP8 (FP8 codes (16, 16)-preshuffled, E8M0 scales in the shuffle_scale layout) or bf16 (no scales). The kernels convert MXFP8 to bf16 in registers before the MFMA, so both formats give the same output; MXFP8 reads half the bytes (M3.1 TP4: 14.6 MB instead of 28.3 MB a layer).
  • The kernel-name suffix _sh{M}x{N}x{K}[kw{W}] sets the shared tile.
  • MiniMax-M3.1 TP4 A16W4 rows: at 8-128 tokens [Config] [Tuning][gfx950] Retune MiniMax-M3.1 TP4 A16W4 decode rows (E=128, top-4) for 8/16/32/64/128 tokens #7414's kernels, with only stage 2 at 8/32/64/128 changed from the reduce to the atomic epilogue (the fused shared expert needs it) and shared tiles tuned with real routing; shared tiles at 256; measured exact-width rows at 40-168.
  • fused_moe does not change: its shared_w1 / shared_w2 already select FHMoE (FP8 shared expert).

Compatibility

Without shared weights, a _sh row builds the same routed kernel (same compile key, name and launch). fused_moe pads M to a power of 2, so only exact-M callers use rows 40-168.

Results

MI355X, MiniMax-M3.1 TP4 A16W4 MoE at 8-128 tokens, real routing (top-4 ids of all 57 MoE layers from an AgentX c16 replay, 114 layer routings per token count, cold weights), wall us per call:

tokens #7414 (main) this PR, routed only this PR + fused shared expert
8 39.4 37.1 37.5
16 56.1 56.1 58.0
32 80.1 78.2 89.0
64 114.5 109.8 112.8
128 142.7 136.9 142.6

The atomic stage 2 is also faster without the shared expert. At 32 tokens #7414's stage-1 kernel hides the shared expert less well (+11 us over routed only).

Earlier measurement, on this PR's previous rows (#6942's routed kernels), not repeated on the current rows: kernel time change in us vs. routed A16W4 + separate MXFP8 shared expert (cold cache, skewed routing):

M 24 32 40 48 64 96 128 192
us -12.1 -13.7 -13.7 -16.3 -16.0 -14.7 -6.1 -14.6

Tests

  • New tests in op_tests/test_flydsl_moe_a16wfp4.py: FP32 reference, routed rows bitwise unchanged, _sh rows without shared weights, M3.1 rows through fused_moe.
  • Tests on main (MI355X, FlyDSL 0.5.0), PR vs. unmodified main:
    • New shared-expert tests: 12/12 pass.
    • Other test_flydsl_moe_a16wfp4.py tests: 234 pass, 1 fail, 35 skip on both. The failure is test_stage2_lds_variants_have_distinct_cache_keys (ImportError in the FlyDSL 0.5.0 wheel).
    • flydsl_tests/test_gemm2_valid_token_count.py and test_moe_mxfp4_inter_dim_align.py: 5/5 pass on both.
    • test_moe_2stage.py --csv-filter _sh: the 14 runnable _sh rows pass strict accuracy with no shared weights (logits_diff < 8.0e-4).
  • Before the port to main, on the AITER v0.1.28.post1 image: 15/15 shared-expert tests passed. The existing a16w4 tests had the same 4 failures with and without the change.

Limitations

  • Validated on gfx950 only. Other archs raise NotImplementedError when shared weights are given.
  • Stage 2 must use the atomic epilog.

PR Self-Review

Reviewed revision

c002ffa

Behavior and intent

The FlyDSL A16W4 MoE kernels (moe_2stage_a16wmix gemm1 and gemm2) can now add a dense shared expert in the routed launches, when the caller gives its weights (MXFP8 or bf16) and shared_inter_bf16. Before, the shared expert ran as separate MXFP8 GEMMs on a side stream, and HIP graph replay does not hide that side branch well. In the launches, a MiniMax-M3.1 decode layer is 6 to 16 us faster at M 24 to 192. Without shared weights, the kernels do not change.

Validation and unverified scope

  • On c002ffa (the 8-128 token rows use [Config] [Tuning][gfx950] Retune MiniMax-M3.1 TP4 A16W4 decode rows (E=128, top-4) for 8/16/32/64/128 tokens #7414's kernels again; only stage 2 at 8/32/64/128 changes from reduce to atomic; review by coderfeli), on MI355X: pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds": 39 passed, and op_tests/test_moe_2stage.py --no-legacy --csv-filter _sh: all 16 M3.1 rows (8-168 tokens) pass the strict accuracy check (logits_diff < 1e-3). The CSV values of the changed rows: us1 = [Config] [Tuning][gfx950] Retune MiniMax-M3.1 TP4 A16W4 decode rows (E=128, top-4) for 8/16/32/64/128 tokens #7414's (same stage-1 kernel), us2 / us = [Config] [Tuning][gfx950] Retune MiniMax-M3.1 TP4 A16W4 decode rows (E=128, top-4) for 8/16/32/64/128 tokens #7414's plus the change of the whole op measured in one process (uniform routing), err2 measured.
  • On 106bd1a (the shared-expert arguments renamed fused_shared_w*: op_tests/test_fhmoe.py::test_heterogeneous_moe_uses_a_separate_custom_op_schema keeps FHMoE's shared_w* names off the ordinary MoE APIs and failed in CI), on MI355X: that test passes, and pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds": 39 passed.
  • On d5145d3 (review fixes: the default shared tile width raises ValueError for an N that is not a positive multiple of its step and ends its search at N; the shared-expert builds of gemm1 and gemm2 raise NotImplementedError above the LDS capacity), pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds" on MI355X: 39 passed. The shipped _sh tiles use at most 64 KB of LDS (gemm1) and 32 KB (gemm2).
  • On 797d8cc and again on d6f5260 (main merged in again), pytest op_tests/test_flydsl_moe_a16wfp4.py -k shared on MI355X: 34 passed. The MXFP8 shared intermediate and stage-2 shared tiles are bitwise equal to the bf16 path at the 20 M3.1 TP4 rows (1 to 256).
  • pytest op_tests/test_flydsl_moe_a16wfp4.py on main (MI355X, FlyDSL 0.5.0): the 12 new shared-expert tests pass. The other tests in the file: 234 passed, 1 failed, 35 skipped, the same as unmodified main. The failure is an ImportError in the FlyDSL 0.5.0 wheel.
  • test_moe_2stage.py --csv-filter _sh: the 14 runnable _sh rows pass the strict accuracy check without shared weights.
  • Unverified: architectures other than gfx950 (they raise NotImplementedError when shared weights are given), and stage-2 epilogues other than the atomic epilogue (not supported).

PR-specific question

The rows are in minimax_m3_a16w4_tuned_fmoe.csv: gfx950 with 256 CUs, MiniMax-M3.1 TP4 A16W4 (E=128, top-4, hidden 6144, inter 768). At 8-128 tokens the routed kernels are #7414's; only stage 2 at 8, 32, 64 and 128 tokens uses the atomic epilogue instead of reduce, which is 2-6 us faster per call also without the shared expert (real routing). The rows use the _sh{M}x{N}x{K} kernel-name suffix at 8 to 256 tokens, and there are exact-width rows at 40 to 168 tokens. fused_moe pads M to a power of 2, so only callers that pass an exact M use the exact-width rows. Without shared weights, a _sh row builds the same routed kernel (same compile key, name and launch), and a test checks this. Rows of other models do not change.

Changed op tests

op_tests/test_flydsl_moe_a16wfp4.py: new shared-expert tests. They compare against an fp32 reference, check that the routed rows stay bitwise unchanged, run _sh rows without shared weights, and run the M3.1 rows through fused_moe. New CPU tests check the default shared tile width and that an oversized shared tile raises in gemm1 and gemm2. No other test covers the shared-expert path.

The moe_2stage_a16wmix gemm1/gemm2 kernels can now compute a dense bf16
shared expert in the same launches as the routed experts. The shared
tiles take the first program ids. The routed tiles and the sort do not
change. Without shared weights, the kernels, names and launch signatures
do not change.

- gemm1/gemm2: dense=True body mode and *_shared kernel variants
  (compile parameters shared_tile_n/_m/_k and shared_k_wave).
- moe_kernels: parse the "_sh{M}x{N}x{K}[kw{W}]" kernel-name suffix;
  forward shared_w1_bf16/shared_w2_bf16/shared_inter_bf16.
- fused_moe: the FlyDSL stage wrappers forward the shared tensors.
- MiniMax-M3.1 TP4 A16W4 rows: shared tiles on the 32-256 rows and
  measured exact-width decode rows (40-168).
- Tests in op_tests/test_flydsl_moe_a16wfp4.py.

Signed-off-by: Vinayak Gokhale <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter standard tests on MI35X
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:mi300x Run Aiter standard tests on MI300X in PRs; main branch always runs them
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 Radeon R9700 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 7773 --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.

Reviews:
A reviewer is usually assigned within one working day after the PR is ready for review (see Code Review Process). If nobody has responded for about a week, mention the area contact listed in CONTACTS.md. Want to help review? See REVIEWERS.md.

@vgokhale
vgokhale requested a review from coderfeli November 10, 2026 22:12
@vgokhale
vgokhale marked this pull request as ready for review November 10, 2026 22:12
@vgokhale
vgokhale requested a review from a team November 10, 2026 22:12
@aiter-bot

aiter-bot commented Nov 10, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision c002ffaa3885 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

The FlyDSL A16W4 MoE kernels (`moe_2stage_a16wmix` gemm1 and gemm2) can now add a dense shared expert in the routed launches, when the caller gives its weights (MXFP8 or bf16) and `shared_inter_bf16`. Before, the shared expert ran as separate MXFP8 GEMMs on a side stream, and HIP graph replay does not hide that side branch well. In the launches, a MiniMax-M3.1 decode layer is 6 to 16 us faster at M 24 to 192. Without shared weights, the kernels do not change.

Validation and unverified scope

- On c002ffaa3 (the 8-128 token rows use #7414's kernels again; only stage 2 at 8/32/64/128 changes from reduce to atomic; review by coderfeli), on MI355X: `pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds"`: 39 passed, and `op_tests/test_moe_2stage.py --no-legacy --csv-filter _sh`: all 16 M3.1 rows (8-168 tokens) pass the strict accuracy check (logits_diff < 1e-3). The CSV values of the changed rows: us1 = #7414's (same stage-1 kernel), us2 / us = #7414's plus the change of the whole op measured in one process (uniform routing), err2 measured.
- On 106bd1a08 (the shared-expert arguments renamed `fused_shared_w*`: `op_tests/test_fhmoe.py::test_heterogeneous_moe_uses_a_separate_custom_op_schema` keeps FHMoE's `shared_w*` names off the ordinary MoE APIs and failed in CI), on MI355X: that test passes, and `pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds"`: 39 passed.
- On d5145d3f2 (review fixes: the default shared tile width raises `ValueError` for an N that is not a positive multiple of its step and ends its search at N; the shared-expert builds of gemm1 and gemm2 raise `NotImplementedError` above the LDS capacity), `pytest op_tests/test_flydsl_moe_a16wfp4.py -k "shared or oversized_lds"` on MI355X: 39 passed. The shipped `_sh` tiles use at most 64 KB of LDS (gemm1) and 32 KB (gemm2).
- On 797d8ccb6 and again on d6f526084 (main merged in again), `pytest op_tests/test_flydsl_moe_a16wfp4.py -k shared` on MI355X: 34 passed. The MXFP8 shared intermediate and stage-2 shared tiles are bitwise equal to the bf16 path at the 20 M3.1 TP4 rows (1 to 256).
- `pytest op_tests/test_flydsl_moe_a16wfp4.py` on main (MI355X, FlyDSL 0.5.0): the 12 new shared-expert tests pass. The other tests in the file: 234 passed, 1 failed, 35 skipped, the same as unmodified main. The failure is an ImportError in the FlyDSL 0.5.0 wheel.
- `test_moe_2stage.py --csv-filter _sh`: the 14 runnable `_sh` rows pass the strict accuracy check without shared weights.
- Unverified: architectures other than gfx950 (they raise `NotImplementedError` when shared weights are given), and stage-2 epilogues other than the atomic epilogue (not supported).

PR-specific question

The rows are in `minimax_m3_a16w4_tuned_fmoe.csv`: gfx950 with 256 CUs, MiniMax-M3.1 TP4 A16W4 (E=128, top-4, hidden 6144, inter 768). At 8-128 tokens the routed kernels are #7414's; only stage 2 at 8, 32, 64 and 128 tokens uses the atomic epilogue instead of reduce, which is 2-6 us faster per call also without the shared expert (real routing). The rows use the `_sh{M}x{N}x{K}` kernel-name suffix at 8 to 256 tokens, and there are exact-width rows at 40 to 168 tokens. `fused_moe` pads M to a power of 2, so only callers that pass an exact M use the exact-width rows. Without shared weights, a `_sh` row builds the same routed kernel (same compile key, name and launch), and a test checks this. Rows of other models do not change.

Changed op tests

`op_tests/test_flydsl_moe_a16wfp4.py`: new shared-expert tests. They compare against an fp32 reference, check that the routed rows stay bitwise unchanged, run `_sh` rows without shared weights, and run the M3.1 rows through `fused_moe`. New CPU tests check the default shared tile width and that an oversized shared tile raises in gemm1 and gemm2. No other test covers the shared-expert path.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

vgokhale added a commit to vgokhale/vllm that referenced this pull request Nov 11, 2026
…nputs

On gfx950 at TP4, a pure-decode step of MiniMax-M3.1 runs each MoE
layer's post-attention chain as one persistent FlyDSL launch
(glue/kernel.py): the all-reduce of the o_proj partial (a reduce-scatter
plus all-gather over Lamport inboxes in a peer buffer), the residual
add, the Gemma RMSNorm and its MXFP8 copy, the router (fp32 weights as
exact bf16x3 MFMAs), the biased top-4 x routed scale and AITER's
moe_sorting outputs with a zeroed moe_buf. AITER's A16W4 gemm1 / gemm2
then run on that sort with the tuned row for exactly the step's rows,
with the shared expert inside them when the installed AITER takes a
dense bf16 shared expert (ROCm/aiter#7773, feature-detected); otherwise
the shared expert runs as separate MXFP8 GEMMs. The all-reduce after the
MoE is the stock one (TODO in decode.py: AITER's fused all-reduce + norm
+ MXFP8 quant, once it lands).

No env vars. The glue is on when it applies: ROCm gfx950, TP 4, DP/PP 1,
no EP/LoRA, the M3.1 config, a pure-decode step (eager or FULL graph,
not DBO) whose row count is a compiled width (24-192, multiples of 8:
DSpark 7 at 3-24 requests) and whose tuned A16W4 row takes the glue's
sort; every other step runs the stock path. The setup runs at the first
eager forward on every rank and is agreed across ranks; an epoch reset
runs between steps before the kernel's tag space wraps.

Sync is the M3 monokernel's (specialized_decode/mono/sync.py: bounded
waits, the error record, fail-stop) on deepseek_v41 mono/common
(PeerBuffer, plan, ops). New shared helpers there:
- specialized_decode/mono/lamport.py: the Lamport (sentinel) inbox
  transport: arm, sentinel test and rewrite, bounded spin / probe /
  vector and triple polls.
- sync.Mailbox(lazy_errors=True): polls read the error word only after
  a first miss; put_triple(plain=True).
- mono/common/ops.buf_load / buf_store: buffer ops at a raw address
  (the glue no longer imports AITER's buffer_ops).

Measured on MI355X, TP4, glue vs the stock all-reduce .. sort chain
(us a layer, CUDA graph, max over ranks): 24 rows 19.6 vs 26.3, 64 22.6
vs 37.4, 96 27.6 vs 42.4, 128 31.0 vs 47.0, 160 36.9 vs 50.6, 192 41.4
vs 56.0. Outputs: residual, MXFP8 rows and scales, top-k sets and the
sort bitwise equal to the stock ops.

Refactor check against the kernel the AgentX runs used (same process,
us a launch, max over ranks, graph replay): 24 rows 19.6 vs 17.9, 64
22.6 vs 21.7, 96 27.6 vs 28.2, 128 31.0 vs 31.2, 160 36.9 vs 36.1, 192
41.4 vs 40.7; the compiled ISA of both is the same instruction stream
at every width (only some SGPR numbers differ), so the gaps are
run-to-run noise. Protocol test (mixed widths in graphs, a 57-launch
step replayed, epoch reset, forced fault + reset): 90 checks pass.

Signed-off-by: Vinayak Gokhale <[email protected]>
The dense shared expert of the moe_2stage_a16wmix gemm1/gemm2 kernels can
now read MXFP8 weights (fp8 e4m3 codes + E8M0 1x32 scales) instead of a
bf16 copy, at half the weight bytes. The format follows the dtype of the
shared weight the caller passes; bf16 weights run as before.

- utils.make_b_loader: w_dtype "fp8". Codes in the shuffle_weight (16, 16)
  layout (two dwordx4 per lane and 128-K block, whole-line runs per load
  instruction), scales in the shuffle_scale layout (the routed MXFP4 scale
  path, reused). The codes are scaled to bf16 in registers right before the
  MFMA (v_cvt_scalef32_pk_bf16_fp8). An e4m3 value times a power of two is a
  bf16 value and the K order and MFMA sequence are the bf16 path's, so the
  output is bitwise that of the bf16 weights. gfx950 only.
- gemm1/gemm2: compile parameter shared_w_dtype ("bf16" or "fp8", kernel
  name suffix _mxfp8); the shared kernels take a scale pointer. gemm2
  asserts a power-of-two shared_tile_k (its A-LDS swizzle needs it).
- API: shared_w1 / shared_w1_scale and shared_w2 / shared_w2_scale
  (fused_moe naming) replace shared_w1_bf16 / shared_w2_bf16 in the FlyDSL
  stage wrappers and moe_kernels.
- MiniMax-M3.1 TP4 rows: shared tiles retuned for MXFP8 at the same shared
  k_wave (so bitwise to the bf16 path): g1 rows 32, 48-56, 72-168, g2 rows
  72-80.
- Tests: MXFP8 shared vs bf16 shared bitwise at every M3.1 TP4 decode row
  (1-256; stage 1 and stage-2 shared tiles), MXFP8 in the shared-expert
  correctness tests and the fused_moe_2stages test, argument checks.

MI355X, M3.1 TP4 rows, skewed routing, cold weights, CUDA graph, us per
layer (gemm1 + gemm2), bf16 shared at the previous tiles -> MXFP8 shared:
8 46.1 -> 43.0, 16 68.6 -> 60.2, 32 89.5 -> 86.1, 64 118.7 -> 117.1,
128 160.4 -> 152.4, 160 178.0 -> 161.4, 256 202.9 -> 195.9.

Signed-off-by: Vinayak Gokhale <[email protected]>
Keeps this branch's MiniMax-M3.1 A16W4 rows at 8, 16, 32, 64, 128 and 256 tokens over #7414's: the fused shared expert needs the atomic stage-2 epilogue, and these rows carry the shared tiles measured with it.

Signed-off-by: Vinayak Gokhale <[email protected]>
gfx950,256,144,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,99.4289,flydsl_moe1_abf16_wfp4_bf16_t16x192x128_w2_xcd1_sh16x32x512kw2,0.0%,60.0498,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_bnt2_sh64x64x128,4.3%,159.4787,0,0,0,102.25,11383.83,
gfx950,256,152,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,99.4921,flydsl_moe1_abf16_wfp4_bf16_t16x192x128_w2_xcd1_sh16x32x512kw2,0.0%,59.9655,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_bnt2_sh64x64x128,4.3%,159.4576,0,0,0,107.95,11386.57,
gfx950,256,160,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,99.4203,flydsl_moe1_abf16_wfp4_bf16_t16x192x128_xcd1_sh16x32x512kw2,0.0%,59.775,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_bnt2_xcd4_sh64x64x128,4.3%,159.1953,0,0,0,113.82,11406.56,
gfx950,256,168,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,99.7693,flydsl_moe1_abf16_wfp4_bf16_t16x192x128_w2_xcd4_sh64x32x128kw2,0.0%,60.4182,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_bnt2_sh64x64x128,4.3%,160.1875,0,0,0,118.77,11337.14,

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.

The newly added tokens:40, 48, 56, 72, 80, 112, 136, 144, 152, 160, and 168—cannot enter fused_moe. In aiter/fused_moe.py:2707-2715, get_padded_M rounds the number of tokens up to the next power of 2 (nextPow2).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if we go through fused_moe what you say is true.

But get_2stage_cfgs does not enforce padding up to nextPow2. In vllm, we have removed topk and sort into custom kernel and call stage1 and 2 from AITER directly. That goes through get_2stage_cfgs directly. So when there are 40 rows, it will return config for 40 not 64 as it won't do the padding.

…idth

- a16w4_default_shared_tile_n raises ValueError when N is not a positive
  multiple of step, and starts its search at N or below. Before, the
  search did not stop for such an N, or when tile_n / 4 was above N.
- The shared-expert builds of gemm1 and gemm2 compare their LDS size with
  get_lds_capacity_bytes and raise NotImplementedError above it, as the
  gemm2 reuse_lds build does. The shipped _sh tiles use at most 64 KB
  (gemm1) and 32 KB (gemm2).
- New tests: test_default_shared_tile_n_bounds,
  test_stage1_shared_rejects_oversized_lds and
  test_stage2_shared_rejects_oversized_lds.

Signed-off-by: Vinayak Gokhale <[email protected]>
op_tests/test_fhmoe.py::test_heterogeneous_moe_uses_a_separate_custom_op_schema
keeps FHMoE's argument names (shared_w1, shared_w2, shared_w1_scale,
shared_w2_scale, shared_expert_id, clamp_shared) off the ordinary MoE APIs,
so that the heterogeneous MoE keeps its own custom-op schema. The A16W4
shared expert used shared_w1 / shared_w1_scale / shared_w2 /
shared_w2_scale on flydsl_moe_stage1/2 and the FlyDSL stage wrappers, and
that test failed in CI. They are now fused_shared_w1, fused_shared_w1_scale,
fused_shared_w2 and fused_shared_w2_scale (the shared expert fused into the
routed launches); shared_inter_bf16 and shared_tile do not change.
FHMoE's own arguments in fused_moe do not change.

Signed-off-by: Vinayak Gokhale <[email protected]>
gfx950,256,64,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,92.4968,flydsl_moe1_abf16_wfp4_bf16_t16x32x128_w2_xcd1_kw4,,47.7397,flydsl_moe2_abf16_wfp4_bf16_t16x256x256_reduce_bnt2_xcd4_persist,0.45%,144.4385,0,0,0,50.18,6283.25,
gfx950,256,128,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,106.5570,flydsl_moe1_abf16_wfp4_bf16_t16x32x128_w2_kw4,,54.8963,flydsl_moe2_abf16_wfp4_bf16_t16x256x256_reduce_bnt2_xcd4_persist,0.41%,162.5288,0,0,0,89.19,5593.57,
gfx950,256,256,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,113.1458,flydsl_moe1_abf16_wfp4_bf16_t16x32x128_w2_xcd1_kw4,0.0%,62.2072,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_bnt2,4.4%,175.353,0,0,0,165.33,10368.97,
gfx950,256,8,6144,768,128,4,ActivationType.Swiglu,torch.bfloat16,torch.bfloat16,torch.float4_e2m1fn_x2,QuantType.per_1x32,1,0,16,0,29.1073,flydsl_moe1_abf16_wfp4_bf16_t16x32x128_w4_xcd1_kw4,0.0%,19.54,flydsl_moe2_abf16_wfp4_bf16_t16x256x128_atomic_xcd4,4.3%,48.6473,0,0,0,18.62,37250.49,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These rows (tokens 8–128) are the pre-#7414 #6942 rows, timings included, so this reverts #7414's real-routing retune for all callers, not just shared-expert ones (#7414 measured them 5–13% slower per layer, 64 tokens the worst). Only stage 2 at 8/32/64/128 needs to move from reduce to atomic. Could you restore #7414's stage-1 kernels and its token-16 row (already atomic+persist), and swap only those stage-2 kernels for atomic variants, re-measured with real routing? cc @Fangzhou-Ai

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.

Thanks for the reviewing @coderfeli , yes the #7414 is turned on real workload, though it's slower on the paper but shows observable advantages under our workload. We need to be cautious to revert that tuning before benchmark under production usages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I made the change. The 8-128 rows case uses #7414's kernels again.

I asked my agent to measure with real routing: top-4 IDs of all 57 layers dumped from an AgentX c=16 run.

us per call, #7414 → this PR without the shared expert:
39.4 → 37.1 (8),
56.1 → 56.1 (16),
80.1 → 78.2 (32),
114.5 → 109.8 (64),
142.7 → 136.9 (128).

So the atomic epilogue is also faster for callers without the shared expert. With the fused shared expert: 37.5 / 58.0 / 89.0 / 112.8 / 142.6.

…ge 2

When main was merged in, the MiniMax-M3.1 TP4 A16W4 rows at 8-128 tokens
went back to #6942's kernels and undid #7414's retune for every caller.
They now use #7414's stage-1 kernels and its 16-token row. Only stage 2 at
8, 32, 64 and 128 tokens changes from the reduce to the atomic epilogue
(the fused shared expert needs it), and the rows carry shared-expert tiles
(_sh), which do not change the routed kernels.

Real routing (top-4 ids of all 57 MoE layers from an AgentX c16 replay,
114 layer routings per token count, cold weights, MI355X), wall us a call:

  tokens  #7414  this, routed only  this + shared expert  before + shared
       8   39.4               37.1                  37.5             39.6
      16   56.1               56.1                  58.0             61.5
      32   80.1               78.2                  89.0             84.9
      64  114.5              109.8                 112.8            120.3
     128  142.7              136.9                 142.6            150.7

CSV values: us1 = #7414's (the same stage-1 kernel); us2 and us = #7414's
plus the measured change of the whole op (uniform routing, same process);
err2 measured against the FP32 reference.

Signed-off-by: Vinayak Gokhale <[email protected]>
@coderfeli
coderfeli merged commit 071d5b6 into main Nov 17, 2026
@coderfeli
coderfeli deleted the vinayak/a16w4-shared-expert branch November 17, 2026 02:08
cursor Bot pushed a commit to AFDEAPAC/aiter that referenced this pull request Nov 18, 2026
…942-fp8-mfma

Conflicts with ROCm#7773 (A16W4 fused bf16 shared expert), resolved as the union of both
sides:

- make_b_loader: ROCm#7773 adds MXFP8 W as `_is_fp8 = w_dtype == "fp8"`, the name this
  branch used for fp8 A. The fp8-A flag is now `_a_fp8`, so the MXFP8 shared weight
  takes the e8m0 `load_b_scale` path and the fp8 A path keeps the residual-table
  scale and LUT decode.
- gemm1/gemm2 bodies and builders take both `dense` / `shared_*` and `a_dtype` /
  `arg_a_scale` / `arg_sref`; the shared-expert prologue asserts `a_dtype == "bf16"`
  (it has no A-scale or s_ref arguments), and the stage-1/2 dispatch keeps the fp8
  mode off when a shared expert is fused.
- the wrappers pass the fp8 pointers before the output and the shared pointers after
  it, matching each kernel's launch signature.

Compile-only ISA is unchanged against both parents: 21 gfx942 fp8 kernels vs this
branch, 21 gfx942 bf16 kernels vs main, and 6 gfx950 routed/shared (bf16, MXFP8)
gemm1/gemm2 kernels vs main.

Co-authored-by: MHYangAMD <[email protected]>
cursor Bot pushed a commit to AFDEAPAC/aiter that referenced this pull request Nov 18, 2026
…942-fp8-mfma

Conflicts with ROCm#7773 (A16W4 fused bf16 shared expert), resolved as the union of both
sides:

- make_b_loader: ROCm#7773 adds MXFP8 W as `_is_fp8 = w_dtype == "fp8"`, the name this
  branch used for fp8 A. The fp8-A flag is now `_a_fp8`, so the MXFP8 shared weight
  takes the e8m0 `load_b_scale` path and the fp8 A path keeps the residual-table
  scale and LUT decode.
- gemm1/gemm2 bodies and builders take both `dense` / `shared_*` and `a_dtype` /
  `arg_a_scale` / `arg_sref`; the shared-expert prologue asserts `a_dtype == "bf16"`
  (it has no A-scale or s_ref arguments), and the stage-1/2 dispatch keeps the fp8
  mode off when a shared expert is fused.
- the wrappers pass the fp8 pointers before the output and the shared pointers after
  it, matching each kernel's launch signature.

Compile-only ISA is unchanged against both parents: 21 gfx942 fp8 kernels vs this
branch, 21 gfx942 bf16 kernels vs main, and 6 gfx950 routed/shared (bf16, MXFP8)
gemm1/gemm2 kernels vs main.
amd-bishwoadhikari added a commit to amd-bishwoadhikari/aiter that referenced this pull request Nov 18, 2026
…s pending A/B

The 3 rows this branch changes in minimax_m3_fp4_tuned_fmoe.csv (tokens 32, 64 at
inter_dim 768 and token 8192 at inter_dim 384) were tuned before the FlyDSL MoE
kernel changes in ROCm#7769 and ROCm#7773, and tokens 32/64 raise the stage-2 error ratio
from 1.2% to 4.8-4.9%. Restore origin/main's rows; each is re-applied only if a
same-session A/B on current main shows it is faster at a comparable error ratio.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Bishwo Adhikari <[email protected]>
cpersson-amd added a commit to cpersson-amd/aiter that referenced this pull request Nov 18, 2026
Resolve conflicts with the fused shared expert (ROCm#7773), the MXFP4
route-output intermediate (ROCm#7706) and the scatter LDS pairs option:
keep the per-kernel-name FP8 route-out selection, with the MXFP4
intermediate as the fallback when FP8 is not selected.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Signed-off-by: Carl Persson <[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.

5 participants