Skip to content

[FlyDSL] [Perf] MegaMoE small-M: pipelined GEMM2, lower per-m-tile overhead, merged FP8 combine (stacked on #8358) - #8360

Open
zovonoir wants to merge 4 commits into
ROCm:mainfrom
zovonoir:mega-moe-small-m-perf
Open

zovonoir wants to merge 4 commits into
ROCm:mainfrom
zovonoir:mega-moe-small-m-perf

Conversation

@zovonoir

Copy link
Copy Markdown
Contributor

Draft. Stacked on #8358: the first two commits belong to #8358; review only the last two (344a28530, 65b7ff01d). This PR will be rebased once #8358 merges.

Summary

Two performance changes to the MegaMoE small-M single kernel (mega_moe_small_m.py, plus GEMM2 support in mega_moe_fused_gemm.py). Neither changes the numerics: the output is bit-identical to the previous kernel.

Motivation

On DeepSeek-V4.1-Flash DP8+EP8 DSpark decode (8x MI355X), the single kernel is ~47% of every decode step even after #8358. A profile shows it is latency bound rather than compute bound:

  • Removing the MFMAs saves only 6 / 9 us (GEMM1 / GEMM2).
  • GEMM2 issues each K-tile's W2 loads right before that tile's own MFMAs.
  • Non-uniform base addresses wrap every load in a waterfall loop.
  • Each m-tile pays a large fixed prologue / epilogue cost.

Changes

  1. [FlyDSL] [Perf] MegaMoE small-M: pipelined GEMM2 tile, tile_n 256 GEMM1 units
    • GEMM2 (BM 64 x 64 columns per wave, static inter_dim):
      • The K-loop is fully unrolled, and the W2 of K-tile kt + 1 is issued per 16-column group right after the MFMAs that free its registers (gemm2_compute_v2(b_prefetch=True)). No extra VGPRs are needed.
      • The load descriptors are made uniform with readfirstlane, so the waterfall loops disappear.
      • Each tile first issues its metadata and first W2 loads and claims the CTA's next unit, and only then waits for its m-tile's GEMM1 ready signal (pre_loop). GEMM1 output is still read only after that signal and an L1 invalidate.
      • The per-accumulator MFMA order is unchanged.
    • GEMM1: buckets of 256 or more tokens per rank use tile_n=256, so each m-tile is 18 work units instead of 36. XCD slice ends now fall on groups of 512 // tile_n n-tiles, which still keeps each 256-byte scale block written by a single XCD.
  2. [FlyDSL] [Perf] MegaMoE small-M: cut per-m-tile overhead, merged FP8 combine reduction (found with GEAK's e2e_workflow)
    • Lower fixed cost per m-tile:
      • GEMM1's prologue makes a single memory round trip;
      • LDS addresses use an opaque 0, so the compiler cannot hoist their computation;
      • the TILE_ROWS base is held in SGPRs;
      • the requantizing epilogue processes 8 columns at a time with 8-byte stores;
      • the GEMM2 scatter decodes routes with readlane.
    • Light W1 warm-up while the kernel waits for the plan.
    • Merged FP8 combine reduction:
      • the loads of all 6 slots are issued before their first use;
      • the reduction uses 512 threads, with fewer sync / flag round trips;
      • combine time drops from 16.7 to 7.5 us.

Performance (if applicable)

Single-kernel time on 8x MI355X with CUDA-graph timing. "Real routing" is decode routing captured from a vLLM DeepSeek-V4.1-Flash server; the main model runs 384 tokens/rank and the DSpark draft 320 tokens/rank.

Hardware Workload/configuration Before After Change
8x MI355X V4.1-Flash 384 experts / top-6, real routing, change 1 372.3 us 349.1 us -6.2%
8x MI355X V4.1-Flash fused shared expert 392 / top-7, real routing, change 1 386.5 us 362.3 us -6.3%
8x MI355X V4.1-Flash DSpark draft 128 / top-3, real routing, change 1 228.9 us 222.2 us -2.9%
8x MI355X V4.1-Flash 384 experts / top-6, real routing (40 layers), change 2 351.7 us 319.0 us -9.3%

The per-m-tile cost (linear fit over uniform, zipf 0.6, zipf 1.0 and real routing) drops from ~3.27 us before change 1, to ~2.56 us after change 1, to ~2.2 us after change 2.

End to end (vLLM DP8+EP8 DeepSeek-V4.1-Flash, DSpark nst5, 512 concurrent requests, 8K output, pure-decode ITL, on top of #8358 and other vLLM-side optimizations):

  • Change 1: ITL 31.87 -> 31.13 ms (1K input), 33.31 -> 32.41 ms (100K input).
  • Change 2: +5.16% throughput in a 1K-input A/B. Together with an unrelated sparse-MLA launch change, ITL goes from 31.11 to 29.36 ms (1K) and from 32.39 to 30.63 ms (100K), and gsm8k measures 0.910.

Testing

  • Unit tests added or updated
  • Performance benchmarks run
  • Tested on MI250X
  • Tested on MI300X

Tested on 8x MI355X (gfx950):

  • AITER_MEGA_SMALL_M=1 torchrun --nproc-per-node 8 op_tests/multigpu_tests/test_mega_moe_small_m.py --time-bs 56,448 (V4-Pro shape, unchanged test), and the same test with the network overridden to the V4.1-Flash shapes 384 / top-6, 392 / top-7 and 128 / top-3: [RESULT] ALL PASS for all four. This covers accuracy against the FP32 reference and the main path on uneven, idle and padded ranks, edge cases, 600-step weight rebinding, epoch wrap, and CUDA graph replay.

  • Single-kernel time from the same test, before ([FlyDSL] MegaMoE small-M: DeepSeek-V4.1-Flash, any experts per rank / top-k, fused shared-expert slots #8358 head) -> after (this PR), median of 15, max over ranks, uniform routing. Each "before" and "after" value comes from a separate run:

    Shape bs/rank 56 bs/rank 448
    V4-Pro 384 / top-6 298.5 -> 295.3 us 490.4 -> 462.2 us
    V4.1-Flash 384 / top-6 177.5 -> 180.0 us 310.7 -> 296.0 us
    V4.1-Flash 392 / top-7 185.7 -> 188.9 us 367.7 -> 330.9 us
    V4.1-Flash 128 / top-3 94.6 -> 96.8 us 178.8 -> 171.7 us

    At 448 tokens/rank every shape is faster (-4% to -10%). At 56 tokens/rank, V4.1-Flash is 1-2% slower; this has not yet been separated from run-to-run noise.

  • Bit-exactness:

    • Change 1 matches the previous kernel bit for bit on uniform, zipf 1.0 and real routing.
    • Change 2 has a max difference of 0 against frozen golden outputs (real routing over 40 layers, uniform, zipf 1.0), and also matches bit for bit on 21 additional random configurations.
    • Repeated runs are bit-identical.

Documentation

  • Docstrings updated
  • User guide updated
  • Performance guide updated
  • No documentation change is needed, with the reason stated below

Dependencies

  • No new third-party dependencies added
  • New dependencies are listed and justified below

Breaking Changes

None. The output is bit-identical, and the API is unchanged (gemm2_compute_v2 gains the optional b_prefetch / pre_loop arguments, which default to the old behavior).

PR Self-Review

Reviewed revision

65b7ff0

Behavior and intent

The changes are performance only: the single kernel produces the same output with lower latency.

Validation and unverified scope

  • Validated with FlyDSL 0.4.2 (the version that amd-aiter 0.1.29 ships, in the vllm/vllm-openai-rocm:nightly 9bc16ae6 image); not yet run against the FlyDSL 0.5.0 pin on main.
  • Change 1 spills 28 VGPRs and 16 SGPRs, all in the per-tile pre/post processing and none in the K-loop.
  • Not tested: DeepSeek-V4-Pro end to end with these changes (only its op test above); MI300-class GPUs (the small-M path requires 8-XCD gfx950).
  • The import block of mega_moe_small_m.py was reordered by ruff --fix (one unused import removed); the rest of the module is unchanged at the AST level.

PR-specific question

Changed op tests

None.

- small_m_eligible: accept model_dim 5120 / inter_dim 2304 besides 7168 / 3072.
- small_m_config: buckets <= 8 tokens pick tile_n 256, which gives V4.1-Flash 18 GEMM1
  n-tiles; the XCD split needs a multiple of 4, so fall back to tile_n 128 (36 n-tiles,
  as every larger bucket already uses).

V4.1-Flash uses the default small-input config unchanged; checked against an FP32
reference and against the main path on 8x MI355X.
…d-expert slots

The single kernel hard-coded 48 experts per rank (EP8 x 384) and top-6. Only
_use_direct_fixed_slot really depended on it; the plan scan, route encoding, combine
buffers and loops, LDS / dispatch-table sizing, config selection and the 32-bit buffer
offsets are already generic.

- small_m_eligible / _use_direct_fixed_slot: 1..64 experts per rank (the plan scans one
  wave lane per local expert) and top-1..8.
- SmallMRuntime(shared_experts=N): the last N local slots of every rank hold fused shared
  experts (vLLM's fuse_shared_expert layout: every token adds a weight-1 route to its own
  rank's slot). Those slots hold every local token, so the plan keeps them out of the
  hot-expert test and the FP8-row decision (otherwise every call would switch the rank to
  FP8 combine rows); they still count toward the cached-weight decision.
- The cached-weight row threshold scales with the routed experts of the rank
  (_CACHED_W_MIN_ROWS is per 48 routed experts).

Now the DSpark draft MoE (128 experts / top-3) and the fused-shared-expert layout of
DeepSeek-V4.1-Flash (392 / top-7) run on the single kernel. The 384 / top-6 path is
unchanged.
…M1 units

GEMM2 of the single kernel was latency bound: each K-tile issued its W2 loads right
before its own MFMAs, the base addresses were not uniform (every load sat in a
waterfall loop), and every tile waited serially on 4-5 memory round trips before its
first weight read.

- GEMM2 (BM 64 x 64 columns per wave, static inter_dim): the 9 K-tiles are fully
  unrolled; the W2 of K-tile kt + 1 is issued per 16-column group right after the
  MFMAs that free its registers (no extra VGPRs). The load descriptors are made uniform
  with readfirstlane, so the waterfall loops disappear. The tile issues its metadata
  and first W2 loads, claims the CTA's next unit, and only then waits for its m-tile's
  GEMM1 ready signal; GEMM1 output is still read after the signal and an L1
  invalidate. The per-accumulator MFMA order is unchanged, so the output is
  bit-identical.
- GEMM1: buckets >= 256 tokens per rank use tile_n 256, so an m-tile takes 18 work
  units instead of 36. The XCD slice ends move to groups of 512 // tile_n n-tiles,
  which still keeps every 256-byte scale block written by one XCD.

Captured DeepSeek-V4.1-Flash decode routing, 384 tokens/rank, 8x MI355X: 372.3 ->
349.1 us (-5.5%); the per-m-tile cost goes from ~3.27 to ~2.56 us. The output is
bit-identical to the previous kernel on uniform, zipf and real routing.

Cost: 28 VGPR and 16 SGPR spills, all in the per-tile pre/post processing (none in
the K-loop).
…combine reduction

Found with GEAK's e2e_workflow (profile -> kernel_workflow) on the DeepSeek-V4.1-Flash
DP8+EP8 DSpark decode, where the single kernel was ~47% of the step.

- Lower fixed cost per m-tile (~2.56 -> ~2.2 us):
  - GEMM1's prologue does a single memory round trip;
  - LDS addresses use an opaque 0 so the compiler does not hoist their computation;
  - the TILE_ROWS base lives in SGPRs;
  - the requantizing epilogue handles 8 columns at a time with 8-byte stores;
  - the GEMM2 scatter decodes routes with readlane.
- Light W1 warm-up while waiting for the plan. Heavier W1 / W2 prefetch competes with
  the P2P payload writes and is slower.
- Merged FP8 combine reduction: the loads of all 6 slots are issued before their first
  use, with 512 reducing threads, and fewer sync / flag round trips (combine
  16.7 -> 7.5 us).

Captured decode routing (40 layers), 384 tokens/rank, 8x MI355X: 351.7 -> 319.0 us per
call (1.103x). Max difference against frozen golden outputs (real routing, uniform,
zipf 1.0) is 0, repeated runs are bit-identical, and 21 extra random configurations are
bit-identical too.
@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 8360 --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.

@zovonoir
zovonoir marked this pull request as ready for review November 20, 2026 07:01
@zovonoir
zovonoir requested review from a team and a balanced review from Copilot November 20, 2026 07:01
@aiter-bot

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision 65b7ff01dd19 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What is the most important boundary or failure mode introduced by this change, and what evidence covers it?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Behavior and intent is too short to show concrete reasoning (15 units; need 18)
  • FAIL — PR-specific question has no substantive answer

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

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

Scheduler-sensitive EP8 spin waits and pipelining require correctness, graph-replay, whole-grid residency, and paired-performance validation on 8× gfx950 with pinned FlyDSL 0.5.0, since reported testing used only 0.4.2.

No open issues 🎉

What changed in this PR

This stacked PR optimizes MegaMoE’s small-input FlyDSL kernel for DeepSeek decode workloads on gfx950. Building on #8358’s expanded layout support, it targets lower latency while aiming to preserve bit-identical outputs.

Changes:

  • Pipelines GEMM2 weight loads and overlaps tile setup with the GEMM1 completion wait.
  • Uses wider GEMM1 tiles for larger token buckets and reduces per-tile setup and output-store overhead.
  • Issues mixed FP8/BF16 combine loads earlier and increases combine participation to 512 workgroups.
File Description
aiter/​ops/​flydsl/​kernels/​mega_moe/​mega_moe_v2.py Carries #8358’s eligibility expansion for V4.1-Flash and additional expert/top-k layouts.
aiter/​ops/​flydsl/​kernels/​mega_moe/​mega_moe_small_m.py Optimizes GEMM1, GEMM2 scheduling, scatter, and combine within the single kernel.
aiter/​ops/​flydsl/​kernels/​mega_moe/​mega_moe_fused_gemm.py Adds optional GEMM2 weight prefetching and a pre-loop callback, retaining existing defaults.

Review effort: Balanced

This branch has not been deployed

No deployments
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