Skip to content

[FlyDSL] MegaMoEV2: enable the small and large routes for DeepSeek-V4-1-Flash EP8 - #7968

Closed
karverma-amd wants to merge 50 commits into
ROCm:ghu/mega_ep4_port_20261109from
karverma-amd:dsv41-flash-ep8-fused
Closed

karverma-amd wants to merge 50 commits into
ROCm:ghu/mega_ep4_port_20261109from
karverma-amd:dsv41-flash-ep8-fused

Conversation

@karverma-amd

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

Copy link
Copy Markdown
Contributor

Stacked on #7788 (ghu/mega_ep4_port_20261109 @ 110ec632d).

Summary

  • DeepSeek-V4.1-Flash EP8 (8 ranks, 48 experts/rank, hidden 5120, inter 2304, topk 6) was missing from FUSED_LAYOUTS. The fused small-input and large-input routes were allow-listed for EP8 V4-Pro (hidden 7168) and for Flash EP4 / EP2 only, so Flash EP8 always took the staged route and a max_tokens hint did nothing.
  • A rank owns 48 experts, the same count as EP8 V4-Pro, so this layout uses that schedule. The GEMM dims are kernel arguments. (8, 5120, 2304, 384, 6) is added to FUSED_LAYOUTS.
  • No max_tokens keeps the staged route. A decode-sized hint takes the small-input route (capacities 256 / 512). 513+ rows at MTPR 8192 or 16384 take the large-input route.
  • The AOT production profile covers it: 8 ranks, 48 experts/rank, a8w4, MTPRs 128 / 8192 / 16384.
  • EP8 V4-Pro, Flash EP4, and Flash EP2 are unchanged.

Validation (8× MI355X, gfx950)

  • CPU: pytest tests/test_mega_moe_gfx950_route_plan.py tests/test_aot_drivers.py::MegaMoEProductionJobsTest — 78 passed. The route-plan test checks the EP8 Flash gate and that the selected config equals the V4-Pro 48-expert config.
  • torchrun --nproc-per-node 8 op_tests/multigpu_tests/test_mega_moe_v2.py --suite routes --network v41_flash --mtpr 512 --small-m-mtprs 256,512: ALL PASS. Output vs staged relL2 = 0 on every case (uniform, uneven, idle ranks, padded). vs fp32 relL2 ~0.052. Alternation (600 steps), epoch wrap, and CUDA graph replay PASS.
  • --suite size --network v41_flash --max-tok-per-rank 8192 --expect-large-m --bs-list 513,1024,2048: PASS. relL2 ~0.058, CUDA graph replay exact, large-input emitter checks PASS. Not timed against staged.

Not run: large-input timing vs staged; MTPR 16384; --suite routes at MTPR 8192; the AOT build of the new jobs (the job list is tested; the serving run below JIT-compiled the kernels on first use); GSM8K.

Performance (8× MI355X)

Kernel, small-input vs staged

test_mega_moe_v2.py --suite routes, MTPR 512, small-input capacities 256 and 512. Median of 15, max over ranks.

rows/rank staged -> small (µs) x
7 224 -> 129 1.74
56 323 -> 176 1.83
126 362 -> 191 1.89
168 382 -> 210 1.82
224 408 -> 233 1.75
336 433 -> 266 1.63
448 423 -> 318 1.33

End to end, vLLM DEP8

DeepSeek-V4.1-Flash, TP1 / DP8 / EP8, --moe-backend aiter_mega_moe, mori_high_throughput, max_num_batched_tokens=8192, shared experts on the main stream. Both arms use the same image (vLLM #67584 passes the DP-group max rows as max_tokens, aiter is #7788). The PR arm mounts this mega_moe_config.py; the baseline is stock #7788, so EP8 Flash stays staged. One measured run per arm after a discarded warmup. Concurrency 256, 131072 in / 1024 out, 512 requests, distinct prompts, temperature=0, ignore_eos.

staged (#7788) this PR change
output tok/s 839.7 970.2 +15.5%
TPOT median 258.8 ms 219.7 ms −15.1%
TPOT p90 372.2 ms 328.8 ms −11.7%
TTFT median 39.7 s 27.5 s −30.7%
TTFT p90 201.8 s 188.1 s −6.8%
wall, 512 requests 624.4 s 540.4 s −13.5%

Startup confirms the route. The baseline logs staged route only. This PR logs small-input capacities (256, 512), large-input route for the 384-expert layout.

Notes

  • large_m_mtprs_for returns LARGE_M_MTPRS (1024, 2048, 4096, 8192, 16384) for this layout, the V4-Pro 48-expert set. The AOT profile builds 8192 and 16384, the same MTPRs as Flash EP4 / EP2. Capacities 1024..4096 and MTPR 16384 were not run on EP8 Flash. Flash EP4 still keeps 1024..4096 on the staged route; that exclusion is unchanged.
  • The small-input kernel stays resident and waits on the other ranks. A kernel on a second stream next to it (shared experts on alt_stream) can deadlock the group. The DEP8 run sets VLLM_DISABLE_SHARED_EXPERTS_STREAM=1, the same constraint as [FlyDSL] [CI] MegaMoEV2: one route plan for every path, shared arenas, and the DeepSeek-V4.1-Flash EP2 / EP4 / EP8 fused routes #7788.

GwilliamHu and others added 30 commits November 10, 2026 17:41
…config

mega_moe_fused_gemm.py carried private copies of helpers that already exist
in gemm1/gemm2/gemm_util/mega_moe_config. Import those instead:
_LdsF32View, scale_view, scale_mma_atoms, mma_one_j, _resolve_g2_knobs,
_PACK, wait_lds_barrier, REFERENCE_EXPERTS_PER_RANK, _fixed_dispatch_cu.

Remove expert_config_class/_scale_dispatch_cu: experts_per_rank is limited to
<= 64, so both were identities that only widened the config cache key.
Remove the unreferenced _spart_output_tile_index.

Verified: gfx950 ISA of all 88 kernels built for MTPR 8192 (staged, small-M
and large-M; ranks 0 and 7) is byte-identical to main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…M envs

MegaMoEV2 had three routing systems: AITER_MEGA_SMALL_M with an explicit
forward_small_m(max_tokens), AITER_MEGA_LARGE_M that sent every forward to
the large-input kernel once built, and the staged path otherwise. Each had
its own eligibility function (v2, small_m and large_m runtimes).

The bundle plan now owns the routes:
- fused_layout_supported()/small_m_cu_supported() in mega_moe_config are the
  single eligibility rule; MegaMoEV2 and both runtimes use them.
- build_mega_moe_bundle_plan(small_m_mtprs=, large_m=) records the fused
  routes; capacities above the first small bank that covers the instance
  MTPR are unreachable and no longer built.
- MegaMoEBundlePlan.route_for(max_tokens) picks small-input (smallest bank
  >= max_tokens), large-input (>= 513 rows, the measured crossover) or
  staged. forward(..., max_tokens=) takes the EP-wide row bound; without it
  the plan assumes the instance capacity, never the local row count, so all
  ranks take the same route. forward_prequant excludes the bf16-only
  small-input route.
- Routes are built whenever the layout supports them (no env switches);
  small_m_mtprs=() / large_m_enabled=False leave one out.
- forward_small_m and set_large_m_enabled stay as thin wrappers over the
  same selector; tests pin a route through _forward_route.
- The weight attributes (_s1_w1, _s1_w1_scale, w2, w2_scale) are properties
  that rebind every route, and the small-input W2 dispatch table follows the
  current w2 per call, so callers that assign them directly per layer (ATOM)
  no longer leave the fused routes on the construction weights.
- AITER_MEGA_MOE_BOUNDS_CHECK is read through envs; the AOT build drops its
  MTPR-8192 env hack and gets the fused kernels from the plan.

Verified: gfx950 ISA and kernel set of the MTPR 8192 AOT build (88 kernels,
ranks 0/7) byte-identical to main; tests/test_mega_moe_route_plan.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
forward(stream=s) ran prepare/Stage1/Stage2 on s but quantize (fixed-slot)
and the staged combine on torch.cuda.current_stream(), so with s != current
the combine could read the P2P input before Stage2 wrote it. An fx.Stream
or raw handle also crashed in Stage2 (stream.cuda_stream). forward and
forward_prequant now make the caller's stream current for the whole call,
as the large-input route already did; torch_stream() moves to tensor_shim.

test_mega_moe_v2 checks every route with a held-back caller stream for a
torch stream, an fx.Stream and a raw handle.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tput arenas

Each route owned a complete copy of the heavy per-call data: the staged
op, the large-input runtime and every small-input bank allocated their own
symmetric payload (activations, scales, route weights, source map) and their
own GEMM1 output. An instance has one call in flight, and every route
rewrites or masks this data before reading it and finishes reading it
before its call's closing all-rank barrier, so one copy sized for the
largest route serves all of them. Counters, epochs, ready flags and the
combine buffers stay per route.

- group_major_capacity() is the single capacity formula of a group-major
  op; GroupMajorPayloadArena allocates the shared symmetric payload and
  FlyDSLDispatchGroupMajorOp/FlyDSLDispatchCombineIntraNodeOp view it.
- stage1_output_buffers() replaces three copies of the GEMM1 output/scale
  allocation and views a shared arena when given one.
- MegaMoEV2 sizes both arenas from its plan's routes (staged, large-input,
  small-input banks) and passes them to the runtimes; a staged-only
  instance allocates exactly as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The large-input kernel was gated to instances of exactly 8192 tokens per
rank. Nothing in its protocol or geometry depends on that capacity: the
source-encoding (W*mt <= 2^24) and P2P/offset ABI checks that do bound it
stay in place. LARGE_M_MTPRS now lists 1024..16384 and is the one place the
capacity is gated (plan, runtime and fused selector read it).

32768 stays on the staged route: the large-input kernel is slower there
(+11.7% E2E at 32768 uniform tokens per rank) and its route-major payload
would need 10.5 GiB per GPU against the staged indexed payload's 1.75 GiB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… kernels

The fused small-/large-input runtimes kept private copies of TileScheduler,
ATileLoader, AS2RLoader, BWeightLoader, BScaleLoader, AScaleLoader and
MfmaScaleGU. Use the gemm_util ones instead. The two options only the fused
path needs become opt-in keywords whose defaults keep the staged code path:
ATileLoader.for_tile(valid_rows_i32) clamps the tile descriptor so padding
rows read back as zero, and BScaleLoader.load_step(cache_modifier) sets the
B-scale cache policy.

The fused kernels now build their GEMM1 weight, scale and routing
descriptors with tensor_shim.ptr_buf_tensor (same base, alignment and
max-size num_records as before) so the shared classes can index them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the fused copy of the SwiGLU + FP8 quant epilogue. The gemm_util one
gains the three options the fused path needs, all off by default:
out_cache_modifier (large-M stores with sc0|sc1 = 17 so GEMM2 on other CUs
of the same kernel sees the GEMM1 output), a per-call store_cm override and
valid_rows_i32, which clamps the tile output descriptor and drops padding-row
output and scale stores. The fused kernels keep the 2-column store pass
(evec=2) and now build their output descriptors with ptr_buf_tensor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the fused copies of do_tile and build_fused_gemm1 and move the
lookahead K-loop do_tile_la next to gemm1.do_tile. build_fused_gemm1 gains
rows_rsrc/lookahead (small-M runs do_tile_la, clamped to each tile's row
count), out_cache_modifier (forwarded to the epilogue) and tile_zero, which
makes the tail A ds_read offset opaque per tile so it is not hoisted and
spilled (large-M). All default to the staged behaviour.

do_tile's rows_rsrc argument is dropped: only do_tile_la was ever given one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the fused copies of issue_a_load_lds_dt and gemm2_compute_v2. The
gemm2 versions gain the options the fused kernels use, defaulting to the
staged behaviour: NW (CTA wave count; the 1-stage B path is now selected by
a 64-column per-wave slab, BN // NW == 64, which is BN == 256 at NW=4),
valid_rows (bounds the A tile descriptor to the tile's real rows) and
early_b_load (1-stage path: issue the K-tile's B and A-scale loads before
the barrier and the next A-tile DMA).

The fused path now uses gemm2's padded B load (offset 0, then select zero)
instead of load_at(mask=), whose out-of-range offset was not past
num_records 0xFFFFFFFF and so read real memory. It was dead (has_pad is
False in both fused kernels); load_at loses the mask argument.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mega_moe_fused_gemm.py still carried a second config system for the small-
and large-input kernels: its own Stage1Config/Stage2Config/MegaMoEConfig
(field subsets of mega_moe_config's), TOKEN_BUCKETS, nearest_token_bucket,
SMALL_M_FIXED_SLOT_MAX_MTPR and a select_mega_moe_config that shadowed the
staged selector's name.

The fused kernels now use mega_moe_config's types. Their tuning rules move
there unchanged as select_fused_config (small-input per-bucket fixed-slot
configs, one large-input config per capacity); FUSED_TOKEN_BUCKETS keeps
their bucket set (no 2-token variant), nearest_token_bucket takes the bucket
set, and SMALL_M_MAX_MTPR is the one small-input bound. The file keeps only
the buffer helpers of the fused dispatch/combine code (2107 -> 86 lines over
the series).

Verified: ISA of the MTPR 8192 AOT build (ranks 0/7) and of the MTPR 16384
build (rank 0) unchanged by this commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fixed-slot staged route and the large-input route quantized the input
into freshly allocated tensors on every forward, so each call (and each
captured graph) allocated. per_1x32_mx_quant takes an ``out`` pair; the
fixed-slot route writes into the instance's existing quant buffers (only
compact prepare used them) and the large-input runtime into its own,
allocated once. quantize() still returns new tensors for callers that keep
them across calls (forward_prequant).

forward() now states the output contract: a view of the route's combine
buffer, valid until the next call; slice_output=False returns the route's
capacity rows, the rows past this rank's own undefined.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ffers

The large-input runtime and each small-input bank still allocated their own
symmetric combine input (8 * MTPR rows of 2*H bytes: 0.88 GiB per GPU at
MTPR 8192, 1.75 GiB at 16384) and the large-input runtime its own quant
buffers. Every route writes a peer's combine input only after that peer has
entered the same call and reads its own before the call ends (with every
launch on the caller's stream), so they view the staged op's buffers; the
large-input route quantizes into the staged quant buffers. Combine outputs
stay per route: they are the returned views.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review follow-ups:
- The small-input kernel's overrides (non-persistent NT GEMM2, 64-row tiles
  from bucket 256) live in mega_moe_config's _small_m_stage1/_small_m_stage2
  instead of a second layer (small_m_config) in the runtime; the shape
  branches no supported fused layout reaches are gone. A golden test pins
  every fused launch config to its value before the move.
- check_forward_inputs (weights.py) replaces the three per-entry input
  checks (MegaMoEV2 now also checks the shapes, as the small-input runtime
  did); combine_output() on the dispatch/combine op replaces three copies of
  the output view; stage2_p2p_row_nbytes moves to mega_moe_stage2 and replaces
  the inline row-size formulas in MegaMoEV2 and the large-input kernel.
- Assigning w2_scale builds the small-input W2 dispatch table outside graph
  capture, so a capture right after a direct assignment finds it.
- test_mega_moe_small_m compares against the staged route explicitly and
  checks the large-input route's epoch wrap through that route; docstrings
  and comments describe the shared arenas, forward_prequant's route set and
  the staged-only bench.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…EMM2 tiles

The small- and large-input kernels carried their own copies of the GEMM2 ->
P2P scatter epilogue. Keep the staged one in mega_moe_stage2 and give it the
fused callers' knobs: NW (wave count), store_cm / fp8_store_cm (peer store
cache policies), p2p_quant_type="dynamic" with fp8_rows, and
elem_view_stores (address peer stores through store_at's element-stride
view). Defaults reproduce the staged kernel; final ISA is byte-identical for
all 88 MTPR 8192 kernels (ranks 0, 7) and 46 MTPR 16384 kernels (rank 0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… P2P scatters

p2p_scatter_epilog and the aligned-pair Stage2 scatter each inlined the same
lane-group |max| reduction, leader E8M0 scale broadcast and FP8 packing.
Move it into mega_moe_stage2._fp8_quant_1x32; group_lane / leader_gate keep
the aligned-pair half-lane, active-gated leader. Final ISA is byte-identical
(MTPR 8192 ranks 0/7: 88 kernels; MTPR 16384 rank 0: 46 kernels).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent_i32

The large-input kernel kept its own copy of the tile-expected increment over
an AddrBuffer; index a ptr_buf_tensor view with the dispatch helper instead.
Final ISA is byte-identical (MTPR 8192 ranks 0/7: 88 kernels; MTPR 16384
rank 0: 46 kernels).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fused routes viewed the staged op's combine input, sized for the
instance MTPR. A small-input bank can exceed it (an MTPR-128 instance builds
the 256 bank), and construction failed ("the shared combine input is smaller
than this op needs"). MegaMoEV2 now allocates one combine input for the
largest capacity among its routes and every op, the staged one included,
views it; combine_input_numel() is the single sizing formula.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Paired 8-GPU timing (3x10 graph replays, ABBA against main) showed the
large-input kernel 10-40% slower than the staged path below ~512 rows per
rank (e.g. MTPR 8192, 1 row: 308.5 vs 194.4 us) and faster from 512 rows
up. A call without the EP-wide row bound cannot tell the two apart, so it
now keeps main's route for instances above the small-input capacities (the
staged path); instances whose capacity fits a small-input bank still take
it, as it is faster at every size. Callers that pass max_tokens get the
large-input route from 513 rows per rank.

test_mega_moe_v2 passes the instance capacity as max_tokens, so its staged
and --expect-large-m runs keep their routes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
One line per instance (rank 0) names the staged layout and the fused routes
the plan built, so a serving log shows which kernels a deployment can take.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Without the EP-wide row bound a call now runs exactly main's route: the
staged path, fixed-slot or compact by capacity. Instances whose capacity
fits a small-input bank used to take it unhinted (e.g. a framework's
MTPR-128 decode instance); the fused routes now need the caller's
max_tokens, so callers that do not pass it see no change in kernels,
numerics or speed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
SGLang (sgl-project/sglang#47065) drives the fused routes through main's
interface: forward_small_m(max_tokens=) for decode steps, and
set_large_m_enabled(True/False) followed by forward() without max_tokens
for extend steps. With calls without max_tokens pinned to the staged route,
set_large_m_enabled(True) no longer reached the large-input kernel.

The switch is now three-state: never called, a call without max_tokens
stays staged (unchanged default); True sends such calls to the large-input
route, as on main; False keeps the large-input route out of every call.
Calls with max_tokens still choose through route_for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…shared GEMM code

Port the shared parts of 28ee7fc ("Speed up the fused MegaMoE kernel for
DeepSeek-V4.1 Flash on 4 GPUs") onto the deduplicated GEMM modules, every
knob off by default:

- gemm_util: srcmap-mapped A rows and scales (dedup_rows), inline-asm A
  operand reads, packed LDS A scales, and the fused evec 8 epilogue
  (valid rows, cache policy, short tiles, lean/fast-SiLU variants) next to
  the staged one, which stays as it was.
- gemm1: row_map / plan_lookup / a_ahead in do_tile(_la), and the short,
  fold and mid tile runners of the large-input kernel.
- gemm2: the BM64 1-stage knobs (uniform A copies, LDS A scales, folded
  tail rows) and their two copy helpers.
- mega_moe_stage2.p2p_scatter_epilog: lane-fed row ids / peers, per-token
  completion counts, BF16 head tokens, staged scales, the lean FP8 / int8
  route quantizer.
- mega_moe_config: FusedStage1Knobs / FusedStage2Knobs on MegaMoEConfig
  instead of new Stage1Config / Stage2Config fields, so the staged configs
  and the fused config digest are unchanged.

gfx950 ISA of EP8 V4-Pro a8w4 (MTPR 128/1024/8192/16384, ranks 0 and 7,
280 kernels) is byte-identical to the previous commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Port the small-input part of 28ee7fc: the single kernel accepts the EP4
layout (4 ranks, 5120/2304, 96 experts a rank) and runs it with the
measured tune -- one dispatch row per (token, destination), every CTA
building its own plan, direct source flags for 1-2 producers, per-token
completion counts for 1-4 tokens, SGPR pointers, the lean GEMM1 epilogue
and register-fed A rows, early GEMM2 CTAs at 512 tokens.

The protocol state the commit changed for every layout stays EP8-neutral:
the epoch step, the per-token counters and their reset, and the BF16 rows
of the first tokens follow FusedStage1Knobs (direct_done, tok_counters,
bf16_head_tokens), which only the EP4 tune sets; SmallMRuntime asserts that
every bucket of a runtime agrees on them. The debug-only row_poll mode is
not ported (it read environment variables at trace time and relied on
combine-input rows the other routes now share).

The token-row regions of dedup_rows live in the shared payload arena past
the runtime's fixed slots (payload_capacity grows by world * mtpr rows on
such a layout), so their u32 offsets are fixed instead of depending on
where the symmetric heap put a separate allocation.

fused_layout_supported() admits FUSED_LAYOUTS (EP8 V4-Pro, EP4 V4.1-Flash);
large_m_mtprs_for() keeps the large-input route on EP8 only for now.
select_fused_config() takes the layout.

gfx950 ISA (AOT dry-run):
- EP8 V4-Pro, MTPR 128/1024/8192/16384, ranks 0 and 7: 280/280 kernels
  byte-identical to the previous commit.
- EP4 V4.1-Flash, MTPR 8192, ranks 0 and 3, against 28ee7fc: the 58
  staged / quant / combine kernels are byte-identical; the 34 small-input
  kernels have the same instructions and resources, in a different order
  only (GEMM2 B loads through the shared gemm2 buffer view instead of the
  commit's element view; the t64x128 ones: the v_lshlrev move the branch
  already has on EP8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… 8192

Port the large-input part of 28ee7fc: the persistent kernel runs the EP4
layout with the measured tune (8-12 payload producers per destination over
192 dispatch CUs, deduplicated token rows with arrival stamps, a parallel
lean plan with tagged count words and bucketed pair order, presend, short /
mid / folded GEMM1 tail tiles, XCD-affine grouped GEMM2 claims with early
GEMM2 CTAs, staged row scales, LDS A scales, the lean epilogue with int8
route rows, and per-token combine waits).

Every new kernel path sits behind a FusedStage1Knobs / FusedStage2Knobs
flag that only _tune_v41_flash_ep4_large sets and that enters the kernel
spec only when on, so EP8 compile keys and kernel names are unchanged. The
spots where the commit changed the default path stay in their old form
(the payload's peer-row loads keep their order when not hoisted, the
combine's int8 scale sum exists only with route_i8). The epilogue changes
went into the shared mega_moe_stage2.p2p_scatter_epilog in the previous
commits; load_at takes its mask back for the new staged-scale reads.

Host side: the bucketed pair order and the row-arrival / sent-row buffers
are allocated only when the config uses them (on EP8 they keep their old
sizes), and the staged-scale buffer is allocated at init, not in forward.
large_m_mtprs_for() gives EP4 the capacity the tune was measured at (8192).

gfx950 ISA (AOT dry-run):
- EP8 V4-Pro, MTPR 128/1024/8192/16384, ranks 0 and 7: 280/280 kernels
  byte-identical to the previous commit.
- EP4 V4.1-Flash, MTPR 8192, ranks 0 and 3, against 28ee7fc: same kernel
  set and names; staged kernels byte-identical; the large-input kernel has
  the same instructions and resources with one s_mul moved, and the
  small-input kernels as before. Loading GEMM2's B weights through
  load_at's element view (the commit's form) instead of the shared gemm2
  buffer view makes every one of them byte-identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ll-input tests

- tests/test_mega_moe_route_plan.py: the fused layout gate admits EP4
  V4.1-Flash (and no mix of the two shapes); large_m_mtprs_for() gives EP4
  only 8192; every EP8 fused config keeps its fused knobs off, and the EP8
  config digest is unchanged; a golden digest of the EP4 configs (checked
  field by field against 28ee7fc's small_m_config / select_mega_moe_config
  for 38 (mtpr, tokens) cases: no difference); every EP4 small-input bucket
  runs one launch protocol; EP4 dispatch CUs cover two expert waves; the
  dedup token rows fit in the payload arena below the u32 offset limit.
- op_tests/multigpu_tests/test_mega_moe_small_m.py: --network v41_flash
  (run it on 4 ranks) and --check-time-acc, which checks every timed route
  and the large-input one against the fp32 reference at the timed count.
- aiter/aot/flydsl/README.md: the EP4 AOT profile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eir own payload

_build_route_arenas lets every route share one payload arena because each
route finishes reading it before its call's closing all-rank barrier. The
EP4 V4.1-Flash tune drops that barrier: the large-input combine waits only
for this rank's tokens (comb_tok_wait) and the small-input tok_done buckets
reduce a token once its own rows are counted. A rank can then leave and
dispatch its next call (the small-input route has no launch handshake) into
a peer that still runs GEMM1/GEMM2 on the shared arena: a large -> small or
small 256 <-> small 512 switch overwrites rows, srcmap and weights the peer
is reading.

A runtime whose launch can end that way (LargeMRuntime / SmallMRuntime
.ends_all_ranks) now keeps its own payload, as in 28ee7fc; the small-input
runtimes also keep their own combine input, which their combine leaves
poisoned for the first tokens (bf16_head_tokens), so the staged and
large-input routes no longer read those rows. The large-input runtime keeps
sharing the combine input (a peer writes it only after this rank entered
the same call) and every route keeps sharing the rank-local GEMM1 output.
EP8 reaches none of this: its fused runtimes still share every arena, and
its allocations are unchanged. Host-only: no kernel changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…build

production_jobs() built only the EP8 profiles, so every EP4 V4.1-Flash
kernel (staged, small-input and large-input) compiled online on first use,
and a cache-only run failed. Add the EP4 profile (world 4, epr 96,
5120/2304, a8w4) at the MTPRs of ATOM's instances: 128 (decode: staged +
small-input 256), 8192 (prefill: staged + small-input + large-input) and
16384 (staged + small-input). An explicit --mtpr applies to it as well.
The default build grows from 192 to 204 jobs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The pair order is rank-local, and a launch reads only the entries its own
routes wrote (rows below this launch's per-expert counts), so the layer
state slots share one copy. With bucket_pairs (EP4 V4.1-Flash) it holds
total_experts x mtpr routes: 12.6 MB a slot at MTPR 8192, about 768 MB at
the 61 slots of a DeepSeek model, now 12.6 MB. EP8 saves its 196 KB a
slot as well. Host-only: no kernel changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…block_n

A tok_done reducer waits until a token's counter reaches its valid routes x
its own GEMM2 n-blocks, while every tok_done sender adds one per n-block of
its own bucket. Ranks of a launch may pick different buckets, so the
tok_done buckets must split model_dim alike; it held only because buckets
1 and 4 both use block_n 256. SmallMRuntime now asserts it, and the EP4
route-plan test checks it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
GwilliamHu and others added 20 commits November 10, 2026 17:41
test_mega_moe_small_m.py moves into test_mega_moe_v2.py as --suite routes:
the instance production builds (staged, small-input 256/512 and, at its
MTPRs, large-input routes side by side), each call's route picked by
forward(max_tokens=...) and checked against the plan. Its scenarios are
unchanged: accuracy against the fp32 reference and the pinned staged route
on uneven, idle and padded ranks, empty ranks with -1 slots (through
forward_small_m), skewed routes, layer rebinding with interleaved staged
calls, the epoch wrap with zero rows, graph replay against eager, and
paired timing (--check-time-acc / --time-only / --time-bs). Timing now also
times the large-input route when max_tokens selects it, and --check-time-acc
checks the routed call at every timed size.

The per-size checks stay the default (--suite size), flags unchanged. The
routes suite keeps its flags; --mtpr and --slots alias --max-tok-per-rank
and --large-m-state-slots, and --seed / --rtol / the slot count keep each
suite's former default. --network v4_pro|v41_flash picks EP8 or EP4.

The duplicated helpers are gone: one _rel_l2 (now casting the reference),
_all_ranks for the rank-wide flags, and _moe_reference for the instance's
layout. The large-input edge check gates the instance's own layout, so it
also holds on EP4. The helpers external harnesses import keep their names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tests/test_mega_moe_route_plan.py becomes test_mega_moe_gfx950_route_plan.py,
next to test_mega_moe_gfx1250_recipe.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h parity

With plan_local the last WG of the combine zeroes the per-expert receive
counters, relying on peers adding to them only after its flag. A tok_done
reducer skips the flags, so a rank can finish launch N and its N+1
producers can add to a peer's counters before that peer's launch-N combine
zeroes them (the route is lost and the reducer falls back to a poisoned row:
NaN), or before the peer's launch-N plan reads them (the peer counts an N+1
route).

A runtime with tok_counters now keeps two halves of the counters by the
entry-epoch parity: launch N counts into, plans from and zeroes half N & 1.
The next launch a peer can add to that half from is N + 2, which needs this
rank's launch N + 1 sources, so launch N is complete. Runtimes without
tok_counters (EP8) keep the combine op's single counter set.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…by launch parity

On an EP4 small-input runtime a tok_done rank can start launch N+1 while a
peer still runs launch N's GEMM1/GEMM2. Since the expert counters are
double-buffered, launch N+1 slot offsets restart at 0, so its producers
overwrote the srcmap/weight slots of launch N routes the peer had not
consumed yet. The peer then scattered that route to the wrong token, and the
victim token's reducer fell back to the flags and summed a poisoned row
(NaN).

The srcmap and weight slots now live in two fz_epr * fz_cap halves
(runtime-owned symmetric buffers) selected by the same launch parity as the
counters. Gated on tok_counters (EP4 small only); EP8 is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s rely on

A runtime with tok_done buckets must send dedup token rows (its slot-indexed
payload rows are single-buffered), and its srcmap / weight halves need the
non-compact slot layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rtol

EP4 V4.1-Flash's large-input route sums in another order than the staged
route (FP8 combine rows, BF16 head tokens), so it cannot match it bit for
bit; EP8 V4-Pro (default fused knobs) still must.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
large_m_mtprs_for() gives the EP4 layout the EP8 capacities. Its large-input
tune (measured at 8192) is reused unchanged: its thresholds count a launch's
rows a rank and the payload geometry comes from the plan, so the capacity only
sizes buffers. LargeMRuntime now checks the pair order and GEMM2 scale staging
against the 32-bit buffer offsets. The AOT EP4 profile keeps its MTPRs
(128/8192/16384); the 16384 instance now builds the large-input kernel too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…4 only

On MI355X the EP4 large-input kernel faults (illegal memory access) at MTPR
1024, 2048 and 4096; at 8192 and 16384 it passes the size, routes and
61-slot suites and runs 1.1-2.1x faster than staged for 513+ rows a rank
(zipf at 16384 x 16384 rows ties). Those two capacities are the ones ATOM's
prefill instances use. The skewed route-switch check needs a source rank to
saturate five destinations, so it is skipped below five ranks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Formatting only (black stable); the code's AST is unchanged, docstrings
only re-wrapped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every knob in mega_moe/envs.py is hard-wired to its default, and the module
is gone:

- AITER_MEGA_FIXED_SLOT_MAX_MTPR: FIXED_SLOT_MAX_MTPR is the constant 255.
- AITER_MEGA_COMBINE_MASK: forward(mask_invalid_slots=None) leaves the -1
  slot check off; callers that pad with -1 pass True.
- AITER_MEGA_MOE_PRELOAD: building an instance never preloads; callers use
  preload_aot_bundles().
- AITER_MEGA_S1_EPI_EVEC, AITER_MEGA_S1_FIXED_KPAIR,
  AITER_MEGA_DISPATCH_FAST_COPY: Stage1 always takes the 8-column epilogue,
  the paired fixed-slot K loop and the two-row compact payload copy. The
  Int16 Stage1 output view and the row-at-a-time payload copy only the
  fallbacks reached are removed; the evec 2 epilogue and the unpaired K loop
  stay, as FP4 output, the small-input kernels and compact Stage1 use them.
- AITER_MEGA_MOE_BOUNDS_CHECK: the Stage1 bounds_check parameter and its
  bounded V# extents are removed (default off).

Kernel names keep their _ev8b / _fc suffixes, so names and AOT caches are
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-input routes

EP2 V4.1-Flash (192 experts a rank, d5120 / i2304, topk 6, a8w4) joins
FUSED_LAYOUTS next to EP8 V4-Pro and EP4 V4.1-Flash: the small-input route at
MTPR 256 / 512 and the large-input route at MTPR 8192 / 16384.

- Config: V41_FLASH_EP2_LAYOUT, V41_FLASH_EP2_LARGE_M_MTPRS, up to 256 experts
  a rank, and EP2 small / large tunes starting from the EP4 ones: 32-row expert
  tiles at every bucket and 96 producer CTAs (3 x 96 would leave no consumers).
- Small-input kernel: the EP2 fixed-slot layout; the plan finalize scans up to
  4 64-expert chunks (the combine resets one expert counter a thread, asserted).
- Small-input kernel: FusedStage1Knobs.stream_w keeps the streaming GEMM bodies
  at any routed-row count (only a hot expert takes the cached ones).
- Large-input dispatch: bounded_scales bounds the batched token-scale reads by
  the scale buffer. Their masked lanes read 2 GiB past an unbounded buffer,
  which faults on EP2 at every token count (illegal memory access in the
  presend / batched payload copy, found with rocm-debug-agent and bisected to
  batch_rows). EP4 and EP8 keep their kernels.
- AOT: the EP2 profile (MTPR 128, 8192, 16384) in the default build.
- Tests: EP2 admission, capacities, protocol, tile and producer limits, payload
  sharing, route plan and config digest; the EP8 / EP4 digests are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured on 2 MI355X GPUs (MTPR 512; uniform, Zipf and hot-expert routing),
against the EP4-derived EP2 kernel of the previous commit:

- 256 and 512 tokens: tile_n 256 (half the GEMM1 n-tiles, each streaming a
  longer W1 slab) and GEMM2's dispatch-table pointers in SGPRs (g2_uniform; the
  32-row tiles leave the registers the EP4 64-row tiles spilled): -3% uniform,
  -8% Zipf, -12% hot at 256.
- 512 tokens: stream_w. The 3072 routed rows pass the cached-weight threshold
  measured on EP8 / EP4, but each of the 192 experts has ~16 rows in one
  m-tile, so no tile rereads its weights: -8% uniform, -3% Zipf, -3.5% hot
  with the above.

Left out after measuring: tok_done at 8 tokens and tile_n 256 at 32-64 (win
uniform, lose 6-14% hot), producer counts 32-224, direct_done, block_n, early
GEMM2, FP8-row thresholds, cached weights at 128-256; on the large-input
kernel presend_experts 24 (-2..-6% uniform at 4096-16384 tokens, +5..30%
Zipf at 3072-6144), early_p 5-6 (+3-5% at 3072), payload producer counts,
presend_rows, early_p_mid, m_group, xcd_chunk and early_k.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The EP4 V4.1-Flash large-input route (MTPR 8192 / 16384, the production
prefill route) batches its dispatch rows (batch_rows), whose token-scale reads
(presend and payload copy) use masked loads. On an unbounded buffer a masked
lane's load_at index is 0x7FFFFFFF // 4, so it reads about 2 GiB past the scale
buffer and faults wherever nothing is mapped there. EP2 hit it at every token
count; EP4 has the same reads and only survives where the memory map happens to
back that address, so a different allocation can take it down.

bounded_scales (added with EP2) bounds those reads by the scale buffer
(mtpr * scale_n_i32 * 4 bytes, at least the real (tokens, model_dim // 32)
tensor): valid lanes read the same values, masked lanes read 0. It moves from
the EP2 large tune into the EP4 one, which EP2 inherits, so the EP2 config and
kernels are unchanged. EP8 V4-Pro does not batch its rows and keeps its kernels.

Only the EP4 large-input kernel changes (the EP4 config digest is re-pinned;
the table differs only in bounded_scales at MTPR 8192). The EP4 faults at MTPR
1024..4096 are not shown to be this, so those capacities stay staged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…outs

The docstring, the layout comment and the small-input route assertion of
test_mega_moe_v2.py listed only EP8 V4-Pro and EP4 V4.1-Flash. They now point
at FUSED_LAYOUTS and name V4.1-Flash by rank count (4 or 2), with an EP2
routes-suite command, so a further layout only extends that list. The size
suite's skewed route switch is marked as needing 5+ ranks (it skips itself on
EP4 and EP2).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Serving recipes raise the fixed-slot MTPR limit with it (SGLang's and
ATOM's DeepSeek-V4 MegaMoE recipes set 1023 / 2047 so their 512- and
1024-row decode instances dispatch fixed-slot), and vLLM's DeepSeek-V4
MegaMoE reads FIXED_SLOT_MAX_MTPR. Dropping the variable silently moved
those instances to the compact dispatch. It is read once at import with the
same default (255), so default behaviour and codegen are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
….1-Flash EP8

DeepSeek-V4.1-Flash on 8 ranks has 48 experts a rank, the same expert count as
DeepSeek-V4-Pro on 8 ranks, at hidden 5120 / inter 2304. The fused routes were
only allow-listed for the V4-Pro hidden size, so this layout fell back to the
staged route and vLLM's max_tokens hint had no effect.

- mega_moe_config: add the (8, 5120, 2304, 384, 6) layout to FUSED_LAYOUTS. It
  uses the V4-Pro 48-expert schedule; the GEMM dims are kernel arguments.
- aot/flydsl/mega_moe: add the Flash EP8 production profile (MTPRs 128 / 8192 /
  16384, a8w4), so serving does not JIT these kernels online.
- tests: route-plan gate and EP8 Flash config equality with V4-Pro; AOT profile
  test; multi-GPU test messages list EP8.

Stacked on ROCm#7788 (head 110ec63).

Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants