Skip to content

[FlyDSL] [AMD][DSV4] fix: drop expert -1 pad routes in the fused TP route/psumkernel - #7365

Merged
valarLip merged 3 commits into
ROCm:mainfrom
karverma-amd:amd/fused-tp-route-pad-ids
Nov 5, 2026
Merged

valarLip merged 3 commits into
ROCm:mainfrom
karverma-amd:amd/fused-tp-route-pad-ids

Conversation

@karverma-amd

@karverma-amd karverma-amd commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Make the fused TP route kernel moe_route_psum_fused drop routes to expert -1 (pad rows), the same way the split-kernel route already does. Without this, pad routes corrupt the per-expert counter and produce out-of-range rows, which fault the GPU in the route kernel or in the stage-1 quant scatter that consumes its output.

Motivation

For small pure-TP batches (token_num * topk <= 2048, E <= 512), grouped MoE uses fused_route_psum_remap, a single-workgroup kernel that does route, psum and remap in one launch. It uses the raw expert id as an index into its LDS counter. When the router leaves a pad row unrouted, its expert id is -1, so the kernel:

  1. bumps a counter slot outside the array, and
  2. writes a garbage row into topids_to_rows, which Phase D then decodes into an expert index and uses to read starts[] out of bounds.

The split-kernel route (moe_route_maps.py) already handles this. Such routes get DROPPED_ROUTE_ROW, take no slot, and every consumer skips them: psum remap, the stage-1 route-indexed quant scatter, and gather-reduce. The fused kernel never implemented that contract.

We hit this on DeepSeek-V4.1-Flash with pure TP4, DSpark speculative decoding and FULL CUDA graphs on gfx1250 (4x MI455X). It showed up as two different faults from the same cause:

  • moe_route_psum_fused memory fault while capturing the DSpark draft graph (draft MoE, E=128, topk=3).
  • moe_fused_quant_preshuffle_routeks_* memory fault about 20 s into serving (main MoE, E=384, topk=6). This kernel scatters to whatever row the route gives it.

A standalone fused_route_psum_remap call with -1 entries in topk_ids reproduces the route-kernel fault.

Changes

  • aiter/ops/flydsl/kernels/moe_contiguous_psum.py (route_psum_fused_kernel):
    • Phase B: write DROPPED_ROUTE_ROW for every route first. Only routes with 0 <= e < E take an LDS slot and get a real row.
    • Phase D: skip negative rows instead of decoding them.
  • op_tests/flydsl_tests/test_route_psum_fused.py: new test for this kernel.

Behaviour for valid expert ids is unchanged: counts, psum, starts and row placement are identical when no route is -1. The extra cost is one compare and one store per route in a single-workgroup launch.

Test / Validation

Unit test (gfx1250): pytest op_tests/flydsl_tests/test_route_psum_fused.py gives 30 passed.

  • Shapes: main model (E=384, topk=6) and DSpark draft (E=128, topk=3).
  • Token counts across the decode range: 1, 5, 26, 64, 341.
  • Pad fraction: 0%, 50% and 100% of rows set to expert -1.
  • Checks: pad routes are DROPPED_ROUTE_ROW; masked_m equals the per-expert counts of valid routes; psum equals the tile-aligned starts plus counts; every valid row lies inside its expert's span, is unique, and is below contiguous_m.

No existing test called this kernel directly. The bug is easy to miss: a pad route corrupts LDS silently and only faults when the bad row lands on an unmapped page.

Additional sweep: every token_num from 1 to 341 at both shapes, valid ids only, gives the same invariants as the split path.

End to end: applied on the 2026-11-02 nightly image with DeepSeek-V4.1-Flash, TP4, DSpark, FULL CUDA graphs, MLPerf agentic c8 and a 600 s issue window.

Before After
Boot / draft-graph capture memory fault completes
Serving memory fault after ~20 s 1105/1105 requests, 0 failed, 0 faults
Throughput n/a 420.0 tok/s/GPU
Interactivity n/a 365.7
TTFT p50 / TPOT p50 n/a 49.6 ms / 2.30 ms

Not verified: MI250X and MI300X, since this kernel is gfx1250-only. EP paths don't use this kernel.

ruff and black are clean on both files.

PR Self-Review

Reviewed revision

30f0c09

Behavior and intent

moe_route_psum_fused drops routes whose expert id is outside [0, E). Those routes get DROPPED_ROUTE_ROW, take no LDS slot, and Phase D leaves them alone. Before this, a pad id of -1 indexed the per-expert counter out of range and wrote a wild row, which faulted the route kernel during DSpark graph capture and the stage-1 quant scatter during serving. Valid ids are unchanged. A valid route is stored once; the dropped sentinel is the else branch, matching moe_route_maps.route_kernel.

Validation and unverified scope

pytest op_tests/flydsl_tests/test_route_psum_fused.py on gfx1250: the original 30 cases (E=384/topk=6 and E=128/topk=3, token counts 1/5/26/64/341, pad fractions 0/0.5/1) plus mixed in-row cases of expert -1 and ids >= E. End to end on 4x MI455X, DeepSeek-V4.1-Flash, pure TP4, DSpark, FULL CUDA graphs, MLPerf agentic c8, 600 s: capture completes and 1105/1105 requests finish, 420.0 tok/s/GPU. Not verified: MI250X and MI300X (this kernel is gfx1250-only) and EP, which does not use it. The split route in moe_route_maps.py still only checks e >= 0; ids >= E there are a separate follow-up.

PR-specific question

What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test? Before this PR a pad route of expert -1, or an id >= E, corrupts the LDS counter and can write a row past the buffer. No existing test called moe_route_psum_fused directly, so that failure was only visible as a GPU fault when the wild row left a mapped page.

Changed op tests

op_tests/flydsl_tests/test_route_psum_fused.py feeds the fused kernel pad rows and, in one row, a mix of a valid id, expert -1, and an id >= E. It checks DROPPED_ROUTE_ROW, per-expert counts, psum, and that kept rows stay inside their span and are unique. Nothing else launches this kernel, so the coverage is not redundant.

… kernel

moe_route_psum_fused is the single-workgroup route + psum + remap kernel
that pure-TP grouped MoE takes for small batches (token_num * topk <= 2048,
E <= 512). It indexes its per-expert LDS counter with the raw expert id, so
a route to expert -1 -- a pad row the router left unrouted -- bumps a slot
outside the counter and leaves a wild row in topids_to_rows. Phase D then
decodes that row into an expert index and reads starts[] out of bounds.

The split-kernel route already has a contract for exactly this case: such
routes get DROPPED_ROUTE_ROW, claim no slot, and every consumer (psum remap,
the stage-1 route-indexed quant scatter, gather-reduce) skips them. The fused
kernel never implemented it. This change makes it do so: Phase B writes
DROPPED_ROUTE_ROW first and only routes with 0 <= e < E take a slot; Phase D
leaves negative rows untouched. Valid-id behaviour is unchanged -- counts,
psum, starts and row placement are bit-identical when no route is -1.

Found on DeepSeek-V4.1-Flash, pure TP4 with DSpark speculative decoding and
FULL CUDA graphs on gfx1250 (4x MI455X). Two faults, one cause: a memory
fault in moe_route_psum_fused while capturing the DSpark draft graph (draft
MoE, E=128, topk=3), and, once past that, a fault in the stage-1
moe_fused_quant_preshuffle_routeks kernel about 20 s into serving (main MoE,
E=384, topk=6), which scatters to whatever row the route handed it. A
standalone fused_route_psum_remap call with -1 entries reproduces the route
fault directly.

With the fix, the same configuration boots, captures every draft graph and
completes the MLPerf agentic c8 run (600 s issue window): 420.0 tok/s/GPU,
interactivity 365.7, TTFT p50 49.6 ms, TPOT p50 2.30 ms, 1105/1105 requests,
zero faults. The kernel change is one compare and one store per route in a
single-workgroup launch.

Adds op_tests/flydsl_tests/test_route_psum_fused.py. Nothing exercised this
kernel directly, and it fails badly rather than loudly: an unguarded pad
route corrupts LDS and only faults when the wild row happens to land off a
mapped page. The test feeds 0%, 50% and 100% pad routes at the main-model
(E=384, topk=6) and draft (E=128, topk=3) shapes across the decode token
range, and checks that pad routes carry DROPPED_ROUTE_ROW and that counts,
psum and row placement stay exact.

Co-authored-by: Cursor <[email protected]>
@karverma-amd
karverma-amd requested a review from a team November 3, 2026 00:30
@github-actions github-actions Bot changed the title [AMD][DSV4] fix: drop expert -1 pad routes in the fused TP route/psumkernel [FlyDSL] [AMD][DSV4] fix: drop expert -1 pad routes in the fused TP route/psumkernel Nov 3, 2026
@github-actions github-actions Bot added the FlyDSL label Nov 3, 2026
@aiter-bot

aiter-bot commented Nov 3, 2026 •

Copy link
Copy Markdown

NOT READY FOR HUMAN REVIEW

PR Self-Review

Revision 30f0c097bdab · 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 behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  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 — validation answer must explicitly state what remains unverified (or say none with a reason)

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

@github-actions

github-actions Bot commented Nov 3, 2026

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 7365 --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.

valarLip
valarLip previously approved these changes Nov 4, 2026

@valarLip valarLip left a comment

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.

LGTM. Phase B now gives -1 / out-of-range expert ids DROPPED_ROUTE_ROW without touching the LDS counter, and Phase D skips negative rows, which matches the split route + contiguous_psum_remap contract. Valid ids are unaffected and the extra cost on the hot path is negligible.

Non-blocking:

  • Nit: an if/else (as in moe_route_maps.route_kernel) would avoid the extra global store per valid route.
  • Follow-up: the split route (moe_route_maps.py) only checks e >= 0, so ids >= E can still go out of bounds there.
  • Test: consider adding mixed in-row -1 / id >= E cases and a bitwise compare against the split path. The new test is gfx1250-only, so CI currently skips it.

A valid route was written as DROPPED_ROUTE_ROW and then overwritten. The else branch matches the split route kernel and skips that extra store. The test now mixes expert -1 with ids at or past E in the same row.
@karverma-amd

Copy link
Copy Markdown
Contributor Author

Addressed the non-blocking notes in 30f0c09.

  • Phase B now uses the same if/else as moe_route_maps.route_kernel, so a valid route is stored once instead of being written as DROPPED_ROUTE_ROW and then overwritten.
  • The test adds mixed in-row cases: a valid id, expert -1, and an id >= E in the same row. Both -1 and >= E come back as DROPPED_ROUTE_ROW, and the counts and spans still match.
  • I did not add a bitwise compare against the split route. That kernel still only checks e >= 0, so an id >= E is a different contract there (the follow-up you noted). The fused kernel already rejects those ids. Slot order inside an expert comes from the atomic, so exact row ids are not a stable comparison; the test checks the dropped sentinel, the counts, and the spans.
  • The self-review section is in the description for 30f0c09.

@valarLip
valarLip merged commit d0fd393 into ROCm:main Nov 5, 2026
RElbers added a commit that referenced this pull request Nov 6, 2026
- Restores the import of DROPPED_ROUTE_ROW from moe_route_maps, which the
  route remap uses for routes with no grouped row.

#7494 dropped the import on a branch where only comments still named the
constant; #7365, merged in between, had added a use in code, so the merged
file raised NameError and test_flydsl_moe_aux.py failed on main.
ruanjm pushed a commit that referenced this pull request Nov 10, 2026
- Restores the import of DROPPED_ROUTE_ROW from moe_route_maps, which the
  route remap uses for routes with no grouped row.

#7494 dropped the import on a branch where only comments still named the
constant; #7365, merged in between, had added a use in code, so the merged
file raised NameError and test_flydsl_moe_aux.py failed on main.
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.

4 participants