Repository navigation
Conversation
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]>
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
One backend per PR: PR title tags & labels: |
|
No activity for 15 days, so this is now labelled |
|
No activity for 15 days, so this is now labelled |
Motivation
The 2-stage fused-MoE tuner benchmarks a dense batch: it builds
topk_idswith
fused_topkover the local expert count, passes noexpert_maskand nonum_local_tokens, and so measures a GEMM where every row of the batch is liveand 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
EPtimes 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:its 6 global choices is local:
1 - C(336,6)/C(384,6) = 55.4%. So a buffer of 512 rows carries ~284 liveones; the kernel is launched over the 284-row prefix via
num_local_tokens,not over 512.
M * topk / EP. At the 512 key that is ~64 rows per expert dense versus ~8under 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
atomicaccumulate path that EP forces (non-Noneexpert_maskalso disqualifies the fused_adaptive_moe_sortfast path) is noteven exercised by the dense benchmark. The tuner was therefore ranking, and the
--compareaccept gate was approving, kernels it had never actually measured inthe configuration the server dispatches.
The symptom: every config the tuner picked was far slower in production than the
heuristic fallback it replaced.
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:
usThe 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_m32 or 64, never the128 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.
calculate()rejects a non-positive pair time. It previously divided byusafter only screening for theINVALID_TIME/INF_TIMEsentinels, so a0 usor negative measurement producedinfTFLOPS and a bogus winnerrather than being discarded. Now anything that is not
> 0is treated asinvalid.
AITER_USE_CUDA_EVENTactually reachesperftest. The cuda.Event timingpath existed but was only reachable through the
use_cuda_eventparameter,which no tuner plumbs through
run_perftest. It matters because candidatesare timed inside forked
mp_tunerworkers, where roctracer does notsurvive the fork: the torch profiler records no CUDA events and every
candidate scores
0 us(which is what made fix 1 visible).perftestandrun_perftestnow defaultuse_cuda_event=None, resolved from the env var.On a config that scored
0 usvia the profiler, the event path reports41.6 us.
TUNE_MOE_EP_SIZEbuilds EP routing geometry. A new module-levelbuild_ep_routing(token, topk, expert, ep_size)samples a uniformtopksubset of the
expert * ep_sizeglobal experts per token, keeps the rowsthat landed at least one local expert, packs them into the front of the
buffer, and returns the
expert_mask(vLLM convention:int32of lengthglobal_num_experts + 1,1for local, trailing0sentinel) andnum_local_tokensprefix length. Note it counts what was delivered ratherthan predicting it, so the live-row count is consistent with the ids. The
reference
torch_moe_2stagesneeds no change: its expert loop runs overrange(w1.shape[0]), so remote globals and the sentinel contribute zero,which is exactly the masked kernel's semantics.
TUNE_MOE_EP_RESCORE_TOPNre-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_epcolumn, and
select_best_candidate()ranks on_us_epwhen it is present.uskeeps its existing dense meaning so the CSV format is unchanged, and_us_epis dropped before the winning row is written out.Caveat worth flagging for review:
build_ep_routingassumes uniformrouting. 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.pyby path since the tuner is a script rather than aninstalled module.
EP routing geometry (fix 3) - 9 tests
test_ep_routing_geometry[512|1024|2048]- shapes, dtypes, a strictlypartial
recv(0 < recv < token), and the vLLM mask convention.test_ep_routing_prefix_invariant[512|2048]- every row belownum_local_tokensholds at least one local expert, and every row above it isentirely the sentinel. This is the contract the truncated launch relies on.
test_ep_routing_ids_are_global[512|2048]- ids address the global expertspace, remote routes really are present, and a token's
topkchoices aredistinct.
test_ep_routing_work_is_ep_times_sparser- local (token, expert) pairs matchtoken * topk / EPwithin 10%, i.e. the benchmark is the ~8x sparser shapeand not the dense one.
test_ep_routing_is_deterministic- same seed reproduces, different seed doesnot.
Non-positive time guard (fix 1) - 6 tests
test_calculate_rejects_nonpositive_us[0|-0.5|INVALID|INF|-inf]andtest_calculate_accepts_positive_us.EP re-score (fix 4) - 9 tests
test_ep_rescore_is_opt_inover 5 env combinations - unset,EPalone,TOPNalone,EP=1,TOPN=0- all leave no_us_epcolumn.test_ep_rescore_ranks_on_measured_time- a fakerun_configreturns380 us / 790 us against a dense ranking that prefers the 790 us candidate.
Asserts both candidates were dispatched,
_us_epis populated,usis notoverwritten, 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, sothat re-scoring cannot silently stop deciding.
test_ep_rescore_restores_config_env- a leakedAITER_CONFIG_FMOEwouldrepoint the rest of the run.
cuda.Event timing (fix 2) - 5 tests
test_use_cuda_event_envover unset /0/1.test_run_perftest_times_with_cuda_eventandtest_explicit_use_cuda_event_overrides_env- GPU-gated; assert a realpositive 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
useach produce failures in the matching tests.Run in
lmsysorg/sglang-rocm:v0.5.22-rocm10-mi35x-20261026on MI355X (gfx950):29 passed.