Skip to content

[DSV4.1Flash][MoE tuning] MoE tuner fixes for DEP - #7005

Open
heachary wants to merge 1 commit into
ROCm:mainfrom
heachary:heachary/dsv41flash/moe-tuner-fixes
Open

heachary wants to merge 1 commit into
ROCm:mainfrom
heachary:heachary/dsv41flash/moe-tuner-fixes

Conversation

@heachary

Copy link
Copy Markdown
Contributor

Motivation

The 2-stage fused-MoE tuner benchmarks a dense batch: it builds topk_ids
with fused_topk over the local expert count, passes no expert_mask and no
num_local_tokens, and so measures a GEMM where every row of the batch is live
and every expert is busy. An expert-parallel rank never sees that shape. It
receives a padded all2all buffer in which only a prefix of rows is real, and the
work per expert is roughly EP times smaller.

That gap is not a rounding error on DEP8, and it is large enough to invert the
ranking.

Why DEP8 specifically

Our deployment is DeepSeek-V4.1-Flash on MI355X at EP8: 384 global experts,
48 local, topk=6. Two things follow:

  1. Most rows are padding. A token reaches this rank only if at least one of
    its 6 global choices is local:
    1 - C(336,6)/C(384,6) = 55.4%. So a buffer of 512 rows carries ~284 live
    ones; the kernel is launched over the 284-row prefix via num_local_tokens,
    not over 512.
  2. Each expert gets ~8x less work. Expected local (token, expert) pairs are
    M * topk / EP. At the 512 key that is ~64 rows per expert dense versus ~8
    under EP.

Eight rows per expert is a different kernel-selection regime than 64. Tiles that
amortise well over a tall per-expert GEMM are mostly padding waste when the GEMM
is short, and the stage-2 atomic accumulate path that EP forces (non-None
expert_mask also disqualifies the fused _adaptive_moe_sort fast path) is not
even exercised by the dense benchmark. The tuner was therefore ranking, and the
--compare accept gate was approving, kernels it had never actually measured in
the configuration the server dispatches.

The symptom: every config the tuner picked was far slower in production than the
heuristic fallback it replaced.

M key heuristic (us) tuner pick, measured in production (us) delta
512 330.08 517.97 -56.9%
1024 340.60 815.52 -139.4%
2048 374.80 772.95 -106.2%

The tuner's own score for those same picks was 241.3 / 294.9 / 444.7 us, i.e. it
underestimated by 2.15x / 2.77x / 1.74x.

The dense ranking is near anti-correlated

Fixing the geometry is necessary but not sufficient, because the tuner ranks on
a dense stage-1 + stage-2 sum of isolated stage timings. Re-scoring the
surviving candidates on the real fused op under EP geometry shows that signal is
not merely noisy, it is close to inverted. At the 512 key:

candidate dense rank dense us measured EP e2e
dense best 1 of 9 239.3 us 533.2 us
dense worst 9 of 9 378.9 us 348.9 us (winner)

The candidate the dense ranking liked most is the second-worst thing you can
actually run, and the one it liked least is the winner. The same pattern holds
at 1024 and 2048: a top-3 dense prefilter would have thrown away the eventual
winner at all three keys. Every EP winner uses block_m 32 or 64, never the
128 the dense ranking favours.

So the EP geometry (fix 3) and the EP re-score (fix 4) are a pair: the first
makes the measurement honest, the second makes the decision use it.

Summary of fixes

All four are opt-in and default to off; with no new env var set, non-EP
tuning behaves exactly as before.

  1. calculate() rejects a non-positive pair time. It previously divided by
    us after only screening for the INVALID_TIME / INF_TIME sentinels, so a
    0 us or negative measurement produced inf TFLOPS and a bogus winner
    rather than being discarded. Now anything that is not > 0 is treated as
    invalid.

  2. AITER_USE_CUDA_EVENT actually reaches perftest. The cuda.Event timing
    path existed but was only reachable through the use_cuda_event parameter,
    which no tuner plumbs through run_perftest. It matters because candidates
    are timed inside forked mp_tuner workers, where roctracer does not
    survive the fork: the torch profiler records no CUDA events and every
    candidate scores 0 us (which is what made fix 1 visible). perftest and
    run_perftest now default use_cuda_event=None, resolved from the env var.
    On a config that scored 0 us via the profiler, the event path reports
    41.6 us.

  3. TUNE_MOE_EP_SIZE builds EP routing geometry. A new module-level
    build_ep_routing(token, topk, expert, ep_size) samples a uniform topk
    subset of the expert * ep_size global experts per token, keeps the rows
    that landed at least one local expert, packs them into the front of the
    buffer, and returns the expert_mask (vLLM convention: int32 of length
    global_num_experts + 1, 1 for local, trailing 0 sentinel) and
    num_local_tokens prefix length. Note it counts what was delivered rather
    than predicting it, so the live-row count is consistent with the ids. The
    reference torch_moe_2stages needs no change: its expert loop runs over
    range(w1.shape[0]), so remote globals and the sentinel contribute zero,
    which is exactly the masked kernel's semantics.

  4. TUNE_MOE_EP_RESCORE_TOPN re-scores the finalists on the production op.
    After the usual dense ranking, the top N merged candidates are each written
    to a scratch fmoe config, the op caches are cleared, and the full fused op is
    benchmarked under the fix-3 geometry. The result lands in a new _us_ep
    column, and select_best_candidate() ranks on _us_ep when it is present.
    us keeps its existing dense meaning so the CSV format is unchanged, and
    _us_ep is dropped before the winning row is written out.

    Caveat worth flagging for review: build_ep_routing assumes uniform
    routing. That is the right default for a tuner, but a production rank with a
    skewed expert distribution will see a different live-row count.

Tests

op_tests/test_moe_tuner_ep.py, 29 tests, no GPU required except where noted.
It loads gemm_moe_tune.py by path since the tuner is a script rather than an
installed module.

EP routing geometry (fix 3) - 9 tests

  • test_ep_routing_geometry[512|1024|2048] - shapes, dtypes, a strictly
    partial recv (0 < recv < token), and the vLLM mask convention.
  • test_ep_routing_prefix_invariant[512|2048] - every row below
    num_local_tokens holds at least one local expert, and every row above it is
    entirely the sentinel. This is the contract the truncated launch relies on.
  • test_ep_routing_ids_are_global[512|2048] - ids address the global expert
    space, remote routes really are present, and a token's topk choices are
    distinct.
  • test_ep_routing_work_is_ep_times_sparser - local (token, expert) pairs match
    token * topk / EP within 10%, i.e. the benchmark is the ~8x sparser shape
    and not the dense one.
  • test_ep_routing_is_deterministic - same seed reproduces, different seed does
    not.

Non-positive time guard (fix 1) - 6 tests

  • test_calculate_rejects_nonpositive_us[0|-0.5|INVALID|INF|-inf] and
    test_calculate_accepts_positive_us.

EP re-score (fix 4) - 9 tests

  • test_ep_rescore_is_opt_in over 5 env combinations - unset, EP alone,
    TOPN alone, EP=1, TOPN=0 - all leave no _us_ep column.
  • test_ep_rescore_ranks_on_measured_time - a fake run_config returns
    380 us / 790 us against a dense ranking that prefers the 790 us candidate.
    Asserts both candidates were dispatched, _us_ep is populated, us is not
    overwritten, and the measured-time winner is chosen. This is the inversion
    above, in miniature.
  • test_select_best_candidate_prefers_measured_ep_time /
    test_select_best_candidate_falls_back_to_us - the selection path itself, so
    that re-scoring cannot silently stop deciding.
  • test_ep_rescore_restores_config_env - a leaked AITER_CONFIG_FMOE would
    repoint the rest of the run.

cuda.Event timing (fix 2) - 5 tests

  • test_use_cuda_event_env over unset / 0 / 1.
  • test_run_perftest_times_with_cuda_event and
    test_explicit_use_cuda_event_overrides_env - GPU-gated; assert a real
    positive time, and that an explicit argument still beats the env var.

Each fix was mutation-checked: reverting the guard, making the env var inert,
routing over local experts only, dropping the prefix packing, and ranking on
dense us each produce failures in the matching tests.

Run in lmsysorg/sglang-rocm:v0.5.22-rocm10-mi35x-20261026 on MI355X (gfx950):
29 passed.

The tuner benchmarks a dense batch: every row routed, no expert_mask, no
num_local_tokens. An EP rank instead gets a padded all2all receive buffer
that is ~EP times sparser, which selects different kernels -- so on DEP8 the
tuner ranked and accepted configs it had never actually measured.

- run_config builds EP geometry under TUNE_MOE_EP_SIZE, so the --compare
  accept gate scores the kernel pair the server dispatches.
- TUNE_MOE_EP_RESCORE_TOPN settles the top candidates on that same
  production-op benchmark instead of the dense stage-isolated ranking.
- calculate() rejects a non-positive pair time instead of dividing by it.
- AITER_USE_CUDA_EVENT reaches perftest's cuda.Event path, without which
  candidates timed in forked mp_tuner workers all score 0 us.

All four default to off; non-EP tuning is unchanged.

Co-authored-by: Cursor <[email protected]>
@heachary
heachary requested a review from a team October 27, 2026 08:29
@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 (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 gfx1201 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 7005 --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.

@github-actions github-actions Bot changed the title [DSV4.1Flash][MoE tuning] Make the 2-stage fused-MoE tuner EP-aware [CK] [DSV4.1Flash][MoE tuning] Make the 2-stage fused-MoE tuner EP-aware Oct 27, 2026
@github-actions github-actions Bot added the CK label Oct 27, 2026
@heachary heachary changed the title [CK] [DSV4.1Flash][MoE tuning] Make the 2-stage fused-MoE tuner EP-aware [DSV4.1Flash][MoE tuning] Make the 2-stage fused-MoE tuner EP-aware Oct 27, 2026
@heachary heachary changed the title [DSV4.1Flash][MoE tuning] Make the 2-stage fused-MoE tuner EP-aware [DSV4.1Flash][MoE tuning] MoE tuner fixes for EP Oct 27, 2026
@heachary heachary changed the title [DSV4.1Flash][MoE tuning] MoE tuner fixes for EP [DSV4.1Flash][MoE tuning] MoE tuner fixes for DEP Oct 27, 2026
@heachary
heachary requested a review from Bernard-Liu October 27, 2026 11:36
@zufayu zufayu mentioned this pull request Oct 27, 2026
5 of 9 tasks
@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 12, 2026
@zufayu
zufayu requested a review from yzhou103 November 17, 2026 13:26
@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Nov 18, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Dec 3, 2026

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

CK stale The PR hasn't been updated in 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant