Skip to content

[FlyDSL] MegaMoE gfx1250 S1F: fix the stage-1 plan's LDS overflow on large arenas (hipErrorInvalidValue) and run SiTUv2 in the stage-1 kernel - #8483

Open
zejunchen-zejun wants to merge 3 commits into
mainfrom
zejun/mega-moe-s1f-plan-lds-situv2

Conversation

@zejunchen-zejun

@zejunchen-zejun zejunchen-zejun commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two fixes so that MegaMoE's stage-1 fusion (S1F: dispatch + GEMM1 in one kernel, AITER_MEGA_S1F=1, the default) runs Kimi-K3 EP8 decode on gfx1250:

  1. The stage-1 compact plan overflows LDS on a large arena. It sizes an LDS table by the arena's row capacity, so Kimi-K3 EP8 (16384 tokens a rank) asks for 572 KiB of LDS at 16-row tiles. A gfx1250 workgroup has 320 KiB, so the plan's first launch fails with hipErrorInvalidValue.
  2. S1F rejects SiTUv2, Kimi-K3's MoE activation, although the stage-1 kernel's embedded GEMM1 already has the SiTUv2 epilogue.

Motivation

Kimi-K3 DP8EP8 with MegaMoE (compact dispatch, fp4 wire, fused combine) cannot run with AITER_MEGA_S1F=1 on gfx1250, so it is served with AITER_MEGA_S1F=0. With S1F on, every rank prints hipModuleLaunchKernel(...) failed with 'hipErrorInvalidValue' on the first decode step, and torch then raises CUDA error: invalid argument at an unrelated op.

Root cause. compile_tdm_compact_plan(s1f=True) keeps t2e, one i32 per destination m-tile, in LDS: ceil(compact_cap / tile_m) entries. _CompactDispatch._plan_for compiled it with the arena's row count (geo.cap): about 2.1M rows for Kimi-K3 EP8 (8 ranks x 16384 tokens x top-16), at the 16-row tile Kimi-K3's grouped CSV picks for decode.

Kimi-K3 EP8, 16384 tokens / rank Plan LDS before (arena rows) After (S1F receive cap)
align 16 572 KiB (over 320 KiB) 76 KiB
align 32 314 KiB 66 KiB
align 64 185 KiB 61 KiB

Only decode steps fail: a step whose receive bound exceeds max_recv() (4096 at EP8 top-16) runs the S1F=0 plan, which has no t2e. DSV4.1 EP8 (48 experts a rank, top-6) runs S1F at decode because its arena is about 5x smaller.

SiTUv2. _Stage1Fusion.eligible() admitted only Silu and Swiglu. build_gemm_a8w4_tdm_task already has the SiTUv2 epilogue (stage1_act 3), but make_s1f_stage1 called it with both betas fixed at 1.0.

Changes

  • compact_plan.py:
    • compact_plan_lds_bytes() computes the plan's dynamic LDS. It and the kernel take their table sizes from one _plan_geometry, and the kernel checks at trace time, after its last allocation, that the allocator handed out exactly the bytes sized.
    • PLAN_LDS_BYTES is get_lds_capacity_bytes("gfx1250"). compile_tdm_compact_plan raises a ValueError naming the sizes instead of compiling a plan whose launch fails without a reason.
  • s1f_ctl.py:
    • _Stage1Fusion.plan_cap(): the row bound of stage-1 plans, the S1F receive cap's rows (min(config.max_recv, max_recv()) tokens) padded to the arena's tile, capped by the arena's count. A step runs S1F only while its receive bound is within max_recv(), so its rows fit.
    • tile_reasons() keeps the dispatch + GEMM1 path for a tile whose stage-1 plan would not fit, and is worked out once per alignment (reset by arm()).
    • eligible() admits ActivationType.Situv2; the GEMM1 hook accepts stage1_act 3 and passes situ_beta / situ_linear_beta.
  • s1f_stage1.py: the launcher and the kernel take f32_situ_beta / f32_situ_linear_beta and hand them to the embedded GEMM1 (they were fx.Float32(1.0)).
  • stages/dispatch_compact.py: _plan_for compiles stage-1 plans with fusion.plan_cap(); the S1F=0 plan keeps the arena's row count. A plan prefetched for the next layer is judged by the next step's receive bound (_step is restored after _launch_plan).
  • dispatch_tdm.py, s1f_stage1.py: the two other hard-coded gfx1250 LDS sizes use the same query.
  • op_tests/multigpu_tests/bench_mega_moe.py: --max_tokens_per_rank (the arena apart from the step's tokens), the step's receive bound passed as ATOM passes it, and --activation situv2 with --situ_beta / --situ_linear_beta against an fp32 reference.
  • tests/test_mega_moe_gfx1250_recipe.py: the Kimi-K3 EP8 plan LDS at the arena's rows and at plan_cap, tile_reasons' LDS reason, and traced plan kernels for the allocator check.

No new environment variables. Silu / Swiglu kernels compile as before (the betas are unused there). #8189 (open) edits the same S1F files; whichever lands second rebases.

Performance (if applicable)

Pending, on 8 x MI455X (gfx1250): Kimi-K3 EP8 decode per-layer time with S1F on vs off, and the decode step time of a DP8EP8 server.

Hardware Workload/configuration Before After Change
MI455X x8 (gfx1250) Kimi-K3 EP8 MegaMoE decode, S1F on vs off pending pending

Testing

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

gfx1250-only code. See "Validation and unverified scope".

Documentation

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

Internal behaviour of the S1F path only.

Dependencies

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

Breaking Changes

None.

PR Self-Review

Reviewed revision

21120ee4a

Behavior and intent

  • The stage-1 compact plan sizes its per-m-tile LDS table by the S1F receive cap instead of the arena's capacity, so a large arena no longer fails at launch.
  • A tile whose stage-1 plan would still not fit falls back to dispatch + GEMM1, with the reason logged, instead of crashing.
  • SiTUv2 models run the stage-1 kernel with their betas.

Validation and unverified scope

Done:

  • On one MI455X tray (gfx1250, 4 GPUs, image rocm/fw-bringup:gfx1250-atom-k3-20261120, aiter 226aa69a, whose four S1F files equalled main's), bench_mega_moe.py with this PR's options: Kimi-K3 MoE shapes at EP4 (-e 896 -k 16 -hd 3584 -id 3072 -q a4w4_mxfp4 --dispatch_wire fp4 --combine_quant mxfp4 --stage1_fused true --stage2_fused true), 8 tokens a rank, AITER_TDM_TILE_M=16.
    • Before this PR, --max_tokens_per_rank 32768 (plan 556 KiB) reproduces the failure: hipModuleLaunchKernel ... hipErrorInvalidValue, then CUDA error: invalid argument at an unrelated op, as in the Kimi-K3 EP8 server.
    • With the first revision of this PR, a run at the same arena (with AITER_TDM_TILE_M=64) got past the plan launch. It then hung in a platform fault of those trays (MES not responding) that every MegaMoE run on them hit, so it gave no accuracy or timing.
  • The plan LDS numbers above come from compact_plan_lds_bytes; the reviewer's CPU replay of the allocations matched it.
  • black and ruff (the CI versions) pass on every changed file.

Unverified at this head (needs a gfx1250 node with mori and a built aiter):

  • the new tests in tests/test_mega_moe_gfx1250_recipe.py, including the traced plan kernels;
  • SiTUv2 and a Silu control with S1F on vs off against the fp32 reference, and per-layer decode time;
  • Kimi-K3 EP8 across two trays, and an ATOM DP8EP8 server with AITER_MEGA_S1F=1: GSM8K against S1F=0, and the decode step time.

No PR CI lane runs tests/test_mega_moe_gfx1250_recipe.py: split_tests.sh takes only test_flydsl_aot_*.py and the moev2 files from tests/, and the file skips without mori.cco, which only the multi-GPU job installs.

PR-specific question

op_tests/multigpu_tests/bench_mega_moe.py is the only changed op_tests file, and it is a benchmark, not a canonical test. Before this PR it could not reach the failure: its arena was sized by the step's own token count, and it passed no receive bound, so S1F turned itself off. With --max_tokens_per_rank and the step's receive bound it reproduces the hipErrorInvalidValue of the Kimi-K3 server on unpatched code, and --activation situv2 checks the SiTUv2 stage-1 path against its fp32 reference. No other test runs S1F with a large arena or SiTUv2.

Changed op tests

  • op_tests/multigpu_tests/bench_mega_moe.py: adds --max_tokens_per_rank (the arena apart from the step's tokens), passes the step's receive bound as ATOM does so that the step can run S1F, and adds --activation situv2 with --situ_beta / --situ_linear_beta against an fp32 reference. It is the reproduction of this PR's failure and the bench for the S1F on/off comparison; no other op test covers either.

🤖 Generated with Claude Code

@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 8483 --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.

zejunchen-zejun added a commit that referenced this pull request Nov 24, 2026
… head 2026-11-19T13:20:38Z)

Snapshot of #8189 (all 6 files, PR diff
against its main base). For Kimi-K3 it supplies the stage-1 fused kernel's
flag tiles for more than 4 source ranks: without it the S1F stage-1 kernel
fails to compile at EP8 ("vector index 4 out of range for numel 4" in
s1f_stage1.ready_of). Needed with #8483 for S1F on the K3 DP8EP8
decode node (wuhuikx/LLM-Booster#722).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zejunchen-zejun added a commit that referenced this pull request Nov 24, 2026
…nas and run SiTUv2 in the stage-1 kernel (#8483 @ 45fbfb7, head 2026-11-23T06:55:24Z)

Snapshot of #8483. The stage-1 compact
plan sizes its per-m-tile LDS table by the S1F receive cap instead of the
arena (Kimi-K3 EP8 at 16384 tokens a rank: 569 KiB -> 73 KiB, limit
320 KiB), and SiTUv2 runs in the stage-1 kernel with its betas. Validated
with #8189 on two MI455X trays: K3 DP8EP8 serving with
AITER_MEGA_S1F=1, every decode step on s1f_stage1, GSM8K unchanged
(wuhuikx/LLM-Booster#722 comment 7121412936).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zejunchen-zejun
zejunchen-zejun marked this pull request as ready for review November 25, 2026 10:34
@zejunchen-zejun
zejunchen-zejun requested review from a team and a balanced review from Copilot November 25, 2026 10:34

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@aiter-bot

aiter-bot commented Nov 25, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision 21120ee4af48 · mode shadow

Author self-review accepted. Readiness report:

Author answers

  • PASS — current-SHA answers are present and structurally specific
Show the author's self-review answers

Behavior and intent

- The stage-1 compact plan sizes its per-m-tile LDS table by the S1F receive cap instead of the arena's capacity, so a large arena no longer fails at launch.
- A tile whose stage-1 plan would still not fit falls back to dispatch + GEMM1, with the reason logged, instead of crashing.
- SiTUv2 models run the stage-1 kernel with their betas.

Validation and unverified scope

Done:
- On one MI455X tray (gfx1250, 4 GPUs, image `rocm/fw-bringup:gfx1250-atom-k3-20261120`, aiter `226aa69a`, whose four S1F files equalled main's), `bench_mega_moe.py` with this PR's options: Kimi-K3 MoE shapes at EP4 (`-e 896 -k 16 -hd 3584 -id 3072 -q a4w4_mxfp4 --dispatch_wire fp4 --combine_quant mxfp4 --stage1_fused true --stage2_fused true`), 8 tokens a rank, `AITER_TDM_TILE_M=16`.
  - Before this PR, `--max_tokens_per_rank 32768` (plan 556 KiB) reproduces the failure: `hipModuleLaunchKernel ... hipErrorInvalidValue`, then `CUDA error: invalid argument` at an unrelated op, as in the Kimi-K3 EP8 server.
  - With the first revision of this PR, a run at the same arena (with `AITER_TDM_TILE_M=64`) got past the plan launch. It then hung in a platform fault of those trays (MES not responding) that every MegaMoE run on them hit, so it gave no accuracy or timing.
- The plan LDS numbers above come from `compact_plan_lds_bytes`; the reviewer's CPU replay of the allocations matched it.
- black and ruff (the CI versions) pass on every changed file.

Unverified at this head (needs a gfx1250 node with mori and a built aiter):
- the new tests in `tests/test_mega_moe_gfx1250_recipe.py`, including the traced plan kernels;
- SiTUv2 and a Silu control with S1F on vs off against the fp32 reference, and per-layer decode time;
- Kimi-K3 EP8 across two trays, and an ATOM DP8EP8 server with `AITER_MEGA_S1F=1`: GSM8K against S1F=0, and the decode step time.

No PR CI lane runs `tests/test_mega_moe_gfx1250_recipe.py`: `split_tests.sh` takes only `test_flydsl_aot_*.py` and the moev2 files from `tests/`, and the file skips without `mori.cco`, which only the multi-GPU job installs.

PR-specific question

`op_tests/multigpu_tests/bench_mega_moe.py` is the only changed op_tests file, and it is a benchmark, not a canonical test. Before this PR it could not reach the failure: its arena was sized by the step's own token count, and it passed no receive bound, so S1F turned itself off. With `--max_tokens_per_rank` and the step's receive bound it reproduces the `hipErrorInvalidValue` of the Kimi-K3 server on unpatched code, and `--activation situv2` checks the SiTUv2 stage-1 path against its fp32 reference. No other test runs S1F with a large arena or SiTUv2.

Changed op tests

- `op_tests/multigpu_tests/bench_mega_moe.py`: adds `--max_tokens_per_rank` (the arena apart from the step's tokens), passes the step's receive bound as ATOM does so that the step can run S1F, and adds `--activation situv2` with `--situ_beta` / `--situ_linear_beta` against an fp32 reference. It is the reproduction of this PR's failure and the bench for the S1F on/off comparison; no other op test covers either.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot could not review the current head (for example, quota or service failure)

Code and op-test hygiene

  • PASS — no committed artifact or conflict marker detected

Required CI

  • PASS — Black: passed
  • PASS — Ruff: passed
  • PASS — Repository dependency: passed
  • PASS — Aiter Test Gate: passed

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

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

⚠️ Action required before merge (7)

  1. Use aiter's LDS capacity query instead of a hand-written constant.
    • PLAN_LDS_BYTES = 320 * 1024 (compact_plan.py:277-279) copies aiter.jit.utils.chip_info.get_lds_capacity_bytes("gfx1250") (lds_capacity.py:15).
    • aiter/ops/flydsl/AGENTS.md requires the query.
    • dispatch_tdm.py:64 and s1f_stage1.py:812 hold the same value as pre-existing copies.
  2. Tie compact_plan_lds_bytes to the allocator it mirrors.
    • compact_plan.py:306-309 recomputes route_vec/per_block/chunk_cap/merge_routes from :513-524.
    • :312-332 restates the smem.allocate sequence at :664-746 by hand.
    • It matches today; a CPU replay over Kimi-K3/DSV4.1, EP4/8, align 16-256 and blocks 1-16 found 0 mismatches. But #8189 is already editing these allocations. An under-count brings back the silent hipErrorInvalidValue; an over-count gives a false fallback.
    • Fix: share one geometry helper, and assert at trace time, after the last allocate, that the allocator's byte count equals compact_plan_lds_bytes(...).
  3. Memoize the stage-1 LDS verdict per align_m.
    • tile_reasons() (s1f_ctl.py:313-319) now runs a function-level import plus plan_lds_bytes() on every call. A microbench measured +0.79 µs per call.
    • It is called about 5-6 times per MoE layer per eager step (eligible, wants_fused_quant ×2, plan_mode ×2, step_will_run). That is about 0.3 ms per eager step on a 61-layer model, on the default AITER_MEGA_S1F=1 path.
    • Every input is fixed after arm(), so cache the reason list per align_m there.
  4. Post the validation the body lists as pending. This PR turns SiTUv2 on in the default S1F path for every Kimi-K3 config whose plan fits, and those already include EP4 at ≤16384 tokens a rank. Before merge, post:
    • SiTUv2 (and a Silu control) with S1F on vs off against the fp32 reference;
    • per-layer decode time with S1F on vs off;
    • a GSM8K run against S1F=0.
      Please also land the bench_mega_moe.py edits so the case can be reproduced.
  5. Add host-side tests in tests/test_mega_moe_gfx1250_recipe.py, next to test_compact_plan_limits. Say which CI lane runs that file; it is importorskip'd on flydsl/mori. Cover:
    • Kimi-K3 EP8 is over 320 KiB at geo.cap and under it at plan_cap;
    • tile_reasons returns the LDS reason when the plan cannot fit;
    • the item-2 assert.
  6. Judge the ahead plan by the next step's bound.
    • dispatch_compact.py:617-631 restores self._step = saved before _launch_plan. So _plan_for decides plan_s1f with the current step's recv_bound.
    • With this PR, a stage-1 plan built that way is capped at plan_cap and silently drops tok_map rows past it (compact_plan.py:1568).
    • It is unreachable today: only bench_mega_moe.py passes next_topk_ids. Restore _step after _launch_plan instead.
  7. Fix plan_cap and the text around it.
    • max(geo.tile_m, align_m) (s1f_ctl.py:338-341) is always geo.tile_m, because _step_for clamps align_m. Use geo.tile_m and fix the docstring ("at the step's alignment").
    • dispatch_compact.py:870-874 "takes the S1F receive cap's (above)" is missing a noun. The "arena's row count" sentence now holds only for the S1F=0 plan.
    • :861-864 "would size … past" should be "can".
    • The ValueError at compact_plan.py:567-573 reports m-tiles even for non-s1f plans; print that clause only when s1f.

Checked and fine:

  • Dependencies: none. Every symbol used is on main. #7410 and #8189 only overlap (rebase).
  • LDS table: the body's numbers reproduce (572→76 KiB at align 16).
  • Real S1F steps: they stay within plan_cap, including under graph capture and on the profiling step.
  • Compiles: no extra compiles.
  • SiTUv2 betas: they arrive as Python floats, the same scalars the unfused GEMM1 uses.
  • CI: the MI35X failures (test_flydsl_hgemm.py, test_gemm_a4w4.py) are unrelated.

Reviewed at 45fbfb7 (deep, two independent passes, CPU LDS replay + tile_reasons microbench). Already fixed: n/a (first review).

zejunchen-zejun and others added 3 commits November 27, 2026 12:26
… cap; SiTUv2 in the stage-1 kernel

The S1F compact plan keeps one LDS word per destination m-tile (t2e), sized
from the compact rows it is compiled with. It took the arena's row count, so a
large arena (Kimi-K3 EP8: 8 ranks x 16384 tokens x top-16, about 2.1M rows)
asked for 572 KiB of LDS at 16-row tiles, and the plan's first launch failed
with hipErrorInvalidValue. A step runs S1F only while its receive bound is
within max_recv(), so the stage-1 plan now bounds its rows by that (61-76 KiB
for Kimi-K3 EP8). compile_tdm_compact_plan checks its LDS against the 320 KiB
a gfx1250 workgroup has and names the numbers when it does not fit, and
tile_reasons() keeps the dispatch + GEMM1 path for a tile whose stage-1 plan
would not fit.

The stage-1 kernel's GEMM1 already has the SiTUv2 epilogue (stage1_act 3) but
was handed betas of 1.0: make_s1f_stage1 now takes f32_situ_beta and
f32_situ_linear_beta, the GEMM1 hook passes them and accepts stage1_act 3, and
eligible() admits ActivationType.Situv2.

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

- --max_tokens_per_rank sizes the arena apart from -tpr, so a small step runs on
  an arena sized for wider ones, as a serving engine's decode does. That is the
  case where the S1F compact plan used to overflow LDS.
- Each forward passes recv_token_bound = world x -tpr, as ATOM does. Without it
  the step takes the arena's bound and S1F turns itself off past max_recv(). With
  the default arena the two are equal, so default runs are unchanged.
- --activation situv2 (with --situ_beta / --situ_linear_beta, Kimi-K3's 4 / 25)
  runs SiTUv2 on the device and in the fp32 reference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- PLAN_LDS_BYTES, dispatch_tdm's _LDS_BUDGET and the stage-1 CTA check
  take the LDS size from aiter.jit.utils.chip_info.get_lds_capacity_bytes.
- compact_plan_lds_bytes and the plan kernel take their table sizes from
  one _plan_geometry. After its last allocation the kernel checks, at
  trace time, that the allocator handed out exactly the bytes sized.
- _Stage1Fusion.tile_reasons is worked out once per alignment; arm()
  resets the cache.
- The next step's plan is judged by the next step: _step is restored
  after _launch_plan, not before it.
- plan_cap pads to the arena's tile, as no step aligns past it, and takes
  no alignment. Comment and message fixes.
- tests/test_mega_moe_gfx1250_recipe.py: Kimi-K3 EP8 plan LDS at the
  arena's rows and at plan_cap, tile_reasons' LDS reason, and traced plan
  kernels for the allocator check.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@zejunchen-zejun
zejunchen-zejun force-pushed the zejun/mega-moe-s1f-plan-lds-situv2 branch from 45fbfb7 to 21120ee Compare November 27, 2026 12:42
Copilot AI balanced review requested due to automatic review settings November 27, 2026 12:42

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@zejunchen-zejun

Copy link
Copy Markdown
Contributor Author

@valarLip thanks. I rebased onto main 00a00f691. The two conflicts with #7921 kept both sides: _packed_sc next to the plan-LDS cache, and value_attrs= after the SiTUv2 betas. Your items are in 21120ee4a, and the bench edits are in de043bb00.

  1. LDS query. PLAN_LDS_BYTES = get_lds_capacity_bytes("gfx1250"). dispatch_tdm._LDS_BUDGET and the stage-1 CTA check in s1f_stage1.py use the same query.

  2. One geometry, checked against the allocator. compact_plan_lds_bytes and compile_tdm_compact_plan take segs, hist_dwords, row_dwords / use_sparse, route_vec, chunk_cap, merge_routes and n_chunks from one _plan_geometry. After the last smem.allocate, the kernel raises at trace time unless smem.allocated_bytes equals the compact_plan_lds_bytes it was checked with. FlyDSL 0.5.0 has allocated_bytes too.

  3. Memoized. tile_reasons() works out each alignment once (_find_tile_reasons, imports included). It returns a copy, since step_reasons appends to the list. arm() resets this cache together with the plan-LDS one.

  4. Validation. The bench_mega_moe.py edits are in the PR now: --max_tokens_per_rank, the step's receive bound, and --activation situv2 with --situ_beta / --situ_linear_beta against an fp32 reference. The GPU numbers are still pending:

    • SiTUv2 and a Silu control, S1F on vs off, against the fp32 reference;
    • per-layer decode time, S1F on vs off;
    • GSM8K against S1F=0.

    The gfx1250 trays I had hung every MegaMoE run in a platform fault (MES not responding), stock code included. I'll post the numbers once nodes are free.

  5. Host tests in tests/test_mega_moe_gfx1250_recipe.py, next to test_compact_plan_limits:

    • test_s1f_plan_lds_is_bounded_by_the_receive_cap: Kimi-K3 EP8 is over PLAN_LDS_BYTES at geo.cap and within it at plan_cap().
    • test_s1f_tile_reasons_name_a_plan_over_the_lds.
    • test_compact_plan_allocates_what_it_sized traces three plan kernels: Kimi-K3 EP8 with S1F=0, Kimi-K3 EP8 stage-1, and a 4-block plan with the LDS route and send tables. test_compact_plan_refuses_a_size_its_allocator_differs_from checks that the trace raises on a mis-sized plan. Both use COMPILE_ONLY with the FlyDSL cache off.

    No PR CI lane runs this file. split_tests.sh takes only test_flydsl_aot_*.py and the moev2 files from tests/, and the file skips without mori.cco, which only the multi-GPU job installs (amd-mori). I'll run it on the gfx1250 node together with item 4.

  6. Ahead plan. _step now stays on the next step through _launch_plan (try/finally), so _plan_for judges plan_s1f by the next step's receive bound.

  7. plan_cap and the text. plan_cap() pads to geo.tile_m and takes no alignment. The docstring and the two dispatch_compact comments are fixed, and the ValueError names m-tiles only for an s1f plan.

On #7921: its prefill S1F runs only on shapes whose receive cap is unbounded (DSV4.1 Flash EP2 / EP4, 1 << 30 in _S1F_MAX_RECV_BY_SHAPE). For those, plan_cap() equals the arena's row count, as before this PR, and tile_reasons() falls back if a plan would not fit.

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

⚠️ Action required before merge (2)

Thanks for the rework. Items 1, 2, 3, 6 and 7 from round 1 are fixed at this head, and the code for item 5 is in:

  • PLAN_LDS_BYTES and the other two gfx1250 LDS sizes now use get_lds_capacity_bytes.
  • _plan_geometry feeds both compact_plan_lds_bytes and the kernel, and compact_plan.py:867 raises at trace time unless smem.allocated_bytes equals the sized bytes. I compared the size list with every smem.allocate at :705-786: same order and alignment.
  • tile_reasons is worked out once per alignment and reset by arm().
  • The ahead plan keeps _step on the next step through _launch_plan (try/finally).
  • plan_cap() uses geo.tile_m, and the comments and the ValueError are fixed.
  1. Post the validation from round-1 item 4, plus a run of the new tests.
    • S1F is the default, and this PR turns on SiTUv2 + S1F for Kimi-K3. None of these numbers are posted yet: SiTUv2 and a Silu control with S1F on vs off against the fp32 reference, per-layer decode time on vs off, and GSM8K against S1F=0.
    • The new tests in tests/test_mega_moe_gfx1250_recipe.py (plan LDS bound, LDS reason, traced allocator check) have never run: no CI lane runs this file, and the body says they are unverified at this head. Please paste their pytest output from the gfx1250 node.
  2. Nit: reuse the existing SiTUv2 reference in op_tests/multigpu_tests/bench_mega_moe.py:625-631.
    • RefModel._ffn hand-writes b*tanh(g/b)*sigmoid(g) * lb*tanh(u/lb). That is aiter.moev2.reference.torch_moe.situv2 (torch_moe.py:134-142), re-exported as aiter.fused_moe.situv2.
    • Call situv2(gate, up, b, lb) from there.

Root cause vs patch

Root problem: the stage-1 plan sized its per-m-tile LDS table by the arena's rows instead of the rows an S1F step can receive. This head compiles stage-1 plans against the S1F receive cap's rows. The LDS size and the allocations come from one geometry, with a trace-time check, and a plan that still does not fit falls back to dispatch + GEMM1. SiTUv2 is enabled as a generic activation (stage1_act 3 already exists in GEMM1), not a model branch. Verdict: root fix. What is left is validation.

Prior asks: 1, 2, 3, 6 and 7 are fixed. 4 is open (the bench edits landed, the numbers have not). 5 has its code in but has not run.
CI: green. No lane exercises the gfx1250 S1F path. #8189 touches the same files (rebase only).


Reviewed at 21120ee (round 2, pass A; true delta = 45fbfb7 rebased onto the new merge base -> 21120ee). Already fixed: LDS query, shared plan geometry + allocator check, tile_reasons memo, ahead-plan step, plan_cap/text, host tests added.

zejunchen-zejun added a commit that referenced this pull request Dec 2, 2026
…DSpark draft head BF16 tuning, empty-row GEMM fix (#9096)

* MegaMoE gfx1250: decode megakernel (dispatch, GEMM1, GEMM2 and combine in one launch), Kimi-K3 shapes (#7345 @ a559927, head 2026-12-01T13:13:28Z)

Opt-in with AITER_MEGAKERNEL=1. Each step chooses by its receive bound:
- "slots" (fixed per-expert slots) for bounds up to SLOTS_RECV (2048; with
  IOD-placed weights, 3072 for Kimi-K3's 3584 x 3072);
- "compact" up to COMPACT_MAX_TOKENS per rank;
- otherwise the staged path.
Off by default, so nothing changes unless the variable is set. The PR's
tests show the output bitwise equal to the staged path.

Ported onto k3-455-dev as the squashed PR diff (merge base ce334d0). Four
files conflicted:
- s1f_ctl.py: kept k3-455-dev's receive caps (_S1F_MAX_RECV 4096 plus
  #8189's _S1F_MAX_RECV_BY_SHAPE) and its s1f_max_recv(); the PR's base had
  no cap. Both __init__ fields are kept (#8483's _plan_lds, the PR's
  _packed_sc).
- s1f_stage1.py: added the PR's s1f_apre_* helpers. The stage-1 launch keeps
  #8483's SiTUv2 arguments and adds the PR's value_attrs.
- gemm_common_gfx1250.py: kept k3-455-dev's prefetch_kernel_code(max_bytes=...),
  a superset of the PR's.
- dsr1_tuned_grouped_fmoe.csv: kept k3-455-dev's DeepSeek-R1 rows and dropped
  the PR's two DSR1 rows (not Kimi-K3).
Placement is detected from the GEMM builder's signature (BUILDER_LOC), so
without IOD placement the placed paths stay off.

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

* MegaMoE gfx1250 compact plan: re-seal the hist row head from each issuing wave before its TDM store (#8880 @ 4214819, head 2026-11-28T16:07:47Z)

Fixes the intermittent S1F spin trap (HSA_STATUS_ERROR_EXCEPTION 0x1016):
the TDM that sends a hist row to rank w is issued by wave w and could read the
row head before wave 0's LDS tag was visible to it. Every sending wave now
re-seals the tag before its store. Kimi-K3's D runs this compact plan
(dispatch=compact).

(cherry picked from commit 4214819)

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

* bench_mega_moe: SiTUv2, a model-sized instance and a simulated EP size, for Kimi-K3's A/Bs

--act situv2 (with --situ_beta / --situ_linear_beta), --max_tpr / --recv_bound
and --sim_ep N (a rank of an EP-N group on fewer GPUs, e.g. EP8 on one tray
with -e 448 --sim_ep 8), all opt-in. Same commit as on the IOD branch
(zejun/k3-455-dev-1201-iod); here it is merged with #7345's bench: #7345's
routing checks stay, and the step bounds come from resolve_step_bounds, which
equals #7345's inline logic without --sim_ep.

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

* [Config] Kimi-K3 gfx1250 LM head and DSpark draft head BF16 rows (N 163840, K 7168), M 1-256

Neither head had a gfx1250 row, so both ran the default bandwidth-bound
gemm_a16w16 at 3.4-4.3 TB/s (one D decode step at c192: 546-603 us for the
target head, ~360-600 us for the draft head). A FlyDSL hgemm whose M tile
covers every row reads the 2.35 GB weight once: per M bucket (32 / 64 / 96 /
128 / 160 / 192 / 256 rows) the best block_n / block_k / stages / waves of a
tdm or ft sweep, measured with 2 weight copies alternated in a CUDA graph on
one MI455X (bf16 error 3e-3, fp32 3e-6 against an f32 matmul):

  rows      bf16 out          fp32 out
  1-32      1.04-1.16x        1.0-1.12x (rows <= 8 keep production)
  33-64     1.25-1.37x        1.31-1.35x
  65-128    1.43-1.62x        1.53-1.63x
  129-160   2.04-2.44x        2.10-2.43x   (c192's verify / draft rows)
  161-192   2.33-2.49x        2.42-2.66x
  193-256   2.10-2.72x        2.17-2.77x

At c192 (133-175 rows) that is ~300-500 us a head, ~0.6 ms a D step.

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

* tuned_gemm: an empty-row FlyDSL GEMM returns the empty product instead of launching

An idle DP rank's dummy step runs its GEMMs with M = 0, and the lookup pads
M = 0 into a tuned row: with a FlyDSL row there (Kimi-K3's head rows, the
commit before) validate_gemm_gfx1250_config refused the empty dimension and
the engine died at its first prefill ("dimensions, tiles, stages, wave counts
and WMMA sizes must be positive integers"; DP4 one-tray D, 12-02). Return the
[0, N] result (or the caller's buffer) without a launch, as the default
kernel effectively did.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Honglie Yi <[email protected]>
gbyu-amd pushed a commit that referenced this pull request Dec 3, 2026
…nas and run SiTUv2 in the stage-1 kernel (#8483 @ 45fbfb7, head 2026-11-23T06:55:24Z)

Snapshot of #8483. The stage-1 compact
plan sizes its per-m-tile LDS table by the S1F receive cap instead of the
arena (Kimi-K3 EP8 at 16384 tokens a rank: 569 KiB -> 73 KiB, limit
320 KiB), and SiTUv2 runs in the stage-1 kernel with its betas. Validated
with #8189 on two MI455X trays: K3 DP8EP8 serving with
AITER_MEGA_S1F=1, every decode step on s1f_stage1, GSM8K unchanged
(wuhuikx/LLM-Booster#722 comment 7121412936).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 7ecc055)

Rebase onto main 12-03: main now has #8189's final head (the k3 copy of #8189 is dropped). Two textual conflicts, both sides kept: s1f_ctl.py _Stage1Fusion.__init__ sets main's _packed_sc and this PR's _plan_lds; s1f_stage1.py launch passes this PR's f32_situ_beta / f32_situ_linear_beta before main's value_attrs keyword (the PR head's order). Multi-GPU MegaMoE not run on the rebased tree.

Updated 12-03 to the PR head #8483 @ 21120ee: folds in the PR's later commits de043bb bench_mega_moe: --max_tokens_per_rank, the step's receive bound, and SiTUv2; 21120ee mega_moe gfx1250 S1F: address review of the stage-1 plan LDS bound.
gbyu-amd pushed a commit that referenced this pull request Dec 3, 2026
…DSpark draft head BF16 tuning, empty-row GEMM fix (#9096)

* MegaMoE gfx1250: decode megakernel (dispatch, GEMM1, GEMM2 and combine in one launch), Kimi-K3 shapes (#7345 @ a559927, head 2026-12-01T13:13:28Z)

Opt-in with AITER_MEGAKERNEL=1. Each step chooses by its receive bound:
- "slots" (fixed per-expert slots) for bounds up to SLOTS_RECV (2048; with
  IOD-placed weights, 3072 for Kimi-K3's 3584 x 3072);
- "compact" up to COMPACT_MAX_TOKENS per rank;
- otherwise the staged path.
Off by default, so nothing changes unless the variable is set. The PR's
tests show the output bitwise equal to the staged path.

Ported onto k3-455-dev as the squashed PR diff (merge base ce334d0). Four
files conflicted:
- s1f_ctl.py: kept k3-455-dev's receive caps (_S1F_MAX_RECV 4096 plus
  #8189's _S1F_MAX_RECV_BY_SHAPE) and its s1f_max_recv(); the PR's base had
  no cap. Both __init__ fields are kept (#8483's _plan_lds, the PR's
  _packed_sc).
- s1f_stage1.py: added the PR's s1f_apre_* helpers. The stage-1 launch keeps
  #8483's SiTUv2 arguments and adds the PR's value_attrs.
- gemm_common_gfx1250.py: kept k3-455-dev's prefetch_kernel_code(max_bytes=...),
  a superset of the PR's.
- dsr1_tuned_grouped_fmoe.csv: kept k3-455-dev's DeepSeek-R1 rows and dropped
  the PR's two DSR1 rows (not Kimi-K3).
Placement is detected from the GEMM builder's signature (BUILDER_LOC), so
without IOD placement the placed paths stay off.

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

* MegaMoE gfx1250 compact plan: re-seal the hist row head from each issuing wave before its TDM store (#8880 @ 4214819, head 2026-11-28T16:07:47Z)

Fixes the intermittent S1F spin trap (HSA_STATUS_ERROR_EXCEPTION 0x1016):
the TDM that sends a hist row to rank w is issued by wave w and could read the
row head before wave 0's LDS tag was visible to it. Every sending wave now
re-seals the tag before its store. Kimi-K3's D runs this compact plan
(dispatch=compact).

(cherry picked from commit 4214819)

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

* bench_mega_moe: SiTUv2, a model-sized instance and a simulated EP size, for Kimi-K3's A/Bs

--act situv2 (with --situ_beta / --situ_linear_beta), --max_tpr / --recv_bound
and --sim_ep N (a rank of an EP-N group on fewer GPUs, e.g. EP8 on one tray
with -e 448 --sim_ep 8), all opt-in. Same commit as on the IOD branch
(zejun/k3-455-dev-1201-iod); here it is merged with #7345's bench: #7345's
routing checks stay, and the step bounds come from resolve_step_bounds, which
equals #7345's inline logic without --sim_ep.

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

* [Config] Kimi-K3 gfx1250 LM head and DSpark draft head BF16 rows (N 163840, K 7168), M 1-256

Neither head had a gfx1250 row, so both ran the default bandwidth-bound
gemm_a16w16 at 3.4-4.3 TB/s (one D decode step at c192: 546-603 us for the
target head, ~360-600 us for the draft head). A FlyDSL hgemm whose M tile
covers every row reads the 2.35 GB weight once: per M bucket (32 / 64 / 96 /
128 / 160 / 192 / 256 rows) the best block_n / block_k / stages / waves of a
tdm or ft sweep, measured with 2 weight copies alternated in a CUDA graph on
one MI455X (bf16 error 3e-3, fp32 3e-6 against an f32 matmul):

  rows      bf16 out          fp32 out
  1-32      1.04-1.16x        1.0-1.12x (rows <= 8 keep production)
  33-64     1.25-1.37x        1.31-1.35x
  65-128    1.43-1.62x        1.53-1.63x
  129-160   2.04-2.44x        2.10-2.43x   (c192's verify / draft rows)
  161-192   2.33-2.49x        2.42-2.66x
  193-256   2.10-2.72x        2.17-2.77x

At c192 (133-175 rows) that is ~300-500 us a head, ~0.6 ms a D step.

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

* tuned_gemm: an empty-row FlyDSL GEMM returns the empty product instead of launching

An idle DP rank's dummy step runs its GEMMs with M = 0, and the lookup pads
M = 0 into a tuned row: with a FlyDSL row there (Kimi-K3's head rows, the
commit before) validate_gemm_gfx1250_config refused the empty dimension and
the engine died at its first prefill ("dimensions, tiles, stages, wave counts
and WMMA sizes must be positive integers"; DP4 one-tray D, 12-02). Return the
[0, N] result (or the caller's buffer) without a launch, as the default
kernel effectively did.

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

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Honglie Yi <[email protected]>
(cherry picked from commit 4c4e333)

Rebase onto main 12-03: main since has #8189's final head, #8889 (packed A only where its stores fit storecnt, tdm_b_th from the tuned row) and #8737 (LDS-to-TDM flush, C staged at the arena tail). Resolved: s1f_ctl.py keeps main's _s1f_gemm_opts / _s1f_packed_a_ok / s1f_prefill_tile(config, ...) / _s1f_warps(config, ...) (this commit's same-purpose _gemm_opts method and (world_size, topk) helpers are dropped) and adds this commit's IOD placement next to main's tdm_b_th: _launcher / _precompile take both, the launcher key and the precompile keys carry (tdm_b_th, placed), make_s1f_stage1 gets tdm_b_th, b_placed, loc_group. s1f_stage1.py: the producer role is this commit's s1f_produce(_PCtx(...)) (its body is main's inline producer, packed A and WAIT_STEPS included); OP_STORES / the storecnt check use main's s1f_packed_a_op_stores / s1f_packed_a_fits (R is 8 there); make_s1f_stage1 takes tdm_b_th and b_placed / loc_group; imports both. dispatch_compact.py: main's 4-tuple _compact_gemm_tiles. mxfp4_preshuffle_gfx1250_tdm.py: main's single C_OFF staging (arena tail, flush scratch at the head) is kept; this commit's a/c cache modifiers, scatter cache policy and defer_ep_wait are added, defer_ep_wait moving C_OFF past the input stages (and growing the arena) instead of this commit's C_EP_OFF / C_Q_OFF; the kernel name carries both suffixes. kimik3_bf16_tuned_gemm.csv: the 504 LM / draft head rows replayed by key. Multi-GPU MegaMoE / megakernel not run on the rebased tree.

Rebase onto main 12-03: after #8483 was updated to its PR head 21120ee (which adds its own bench_mega_moe flags --max_tokens_per_rank / --activation / --situ_beta), op_tests/multigpu_tests/bench_mega_moe.py keeps this commit's version (--max_tpr / --recv_bound / --act / --situ_beta / --sim_ep cover the same knobs); taking both would define --situ_beta twice.

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.

4 participants