Skip to content

[FlyDSL] refactor(flydsl): share the token-multidest quant context - #8496

Closed
lirui927 wants to merge 2 commits into
ROCm:mainfrom
lirui927:fuse/f9a-multidest-quant-ctx
Closed

lirui927 wants to merge 2 commits into
ROCm:mainfrom
lirui927:fuse/f9a-multidest-quant-ctx

Conversation

@lirui927

@lirui927 lirui927 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

What it does

A refactor of the gfx1250 MoE A1 quant kernels, with no change in behaviour. It is split out of #7543 at review, so that #7543's diff stays reviewable and this part can be checked against main on its own.

  • _quant_ctx. In build_moe_token_multidest_quant_module, a closure now builds the per-warp _emit_quant_block_loop context: destination descriptors, scale bases and layout constants for one token and its routes' rows. The token-multidest kernel only loads its routes' rows and passes them in, together with the hidden-row staging. This replaces about 60 lines of locals and the inline context in the kernel.
  • _hidden_pk8_load. The pk8 hidden-row load of _emit_quant_block_loop (one aligned dwordx4 per lane per iteration) becomes a helper.

Why: #7543 adds a second entry point to this builder, the TP route-in-quant kernel. It needs the same quant context and the same hidden-row load, which it issues early. With this PR in place, #7543 adds only its route step. #7547's route decode then fits between the row loads and _quant_ctx in the default entry point.

Benefit

There is no runtime change. Every quant kernel that goes through _emit_quant_block_loop compiles to byte-identical gfx1250 ISA on main and on this branch (below), so no GPU timing is needed. The gain is in review: #7543 shrinks to the route-in-quant change itself.

Testing

  • ISA equality with main (bf59948).
    • Method: FlyDSL compile-only build for gfx1250 on CPU (aiter.aot.flydsl.dry_run, COMPILE_ONLY, FLYDSL_DUMP_IR=1), driven through the public wrappers. Each kernel's final ISA (22_final_isa.s) is compared with cmp. These are FlyDSL kernels, so there is no hipcc step; this dump is the LLVM AMDGPU backend output that the runtime loads.
    • Coverage: 96 builds, which produce 81 distinct kernels. All 81 are byte-identical; the kernel name lists match too.
      • Token-multidest quant: fp4/fp8 and wmma_rep 1/2, at K = 1024 and 6144. Both the K-split builds (ks2/3/4/12) and the TDM-staged builds (hidtdm4).
      • Route-indexed (routeks) quant: with and without K-split, plus prequantized and row-major-scale EP builds.
      • Compact-scale quant pair.
      • Row-indexed quant, plain and with the fused activation.
      • The route + quant + scatter kernels (generic, single-token K-split, and route + psum).
  • black and ruff are clean.
  • CPU pytest of the neighbouring tests (flydsl_tests/test_flydsl_grouped_gemm.py, flydsl_tests/test_route_psum_fused.py, test_grouped_gemm_stream.py, flydsl_tests/test_ep_e8m0_scale.py): 52 failed, 9 passed, 45 skipped, the same as main. The failures are the GPU cases with no device (no ROCm-capable device).

Not covered

  • No GPU run. The ISA is identical, and the GPUs were not available for this round.

Dependencies

None. This PR is based on main. #7543 builds on it.

PR Self-Review

Reviewed revision

32de0e6

Behavior and intent

Validation and unverified scope

  • gfx1250 ISA on CPU (FlyDSL compile-only): 81/81 kernels from 96 builds are byte-identical to main. They cover token-multidest (K-split and TDM-staged), routeks (K-split, no K-split, prequantized, row-major), the compact pair, row-indexed (with and without activation), and the route + quant + scatter kernels, for fp4/fp8, wmma_rep 1/2 and K = 1024/6144.
  • black and ruff are clean. The CPU pytest results are identical to main.
  • 517e25b6 against the previous head f41bda5f:
    • Change: _quant_ctx takes the hidden-row staging as seven required keyword-only parameters instead of a **staging catch-all (the review thread on moe_fused_route_quant_scatter.py:2237). A missing or misspelled name now raises TypeError when the kernel is built. Nothing else changes: one commit, +19/-3.
    • Re-checked on CPU with FlyDSL 0.5.3, the pinned version: the same 96 builds give 81/81 kernels whose final ISA is byte-identical to f41bda5f and to main (the base bf599489, and ffbd9c6a with this branch merged in). Against f41bda5f the traced IR and the emitted GPU object are identical too, so no FlyDSL cache key moves. An AOT replay of 11 gfx1250 grouped rows writes the same 48 cache artifacts byte for byte, and --check passes.
    • The fix: with hidden_chunks misspelled at the call site, all 30 token-multidest builds stop with TypeError. At f41bda5f the same typo built without an error and changed the ISA of the six TDM-staged kernels.
    • black and ruff are clean. The CPU pytest results are the same as main test by test (52 failed, 9 passed, 45 skipped).
  • This revision (32de0e65) is 517e25b6 rebased onto main e6d2d310 (under Triton 3.9 the old base fails op_tests/test_gemm_a4w4.py in CI); no conflicts. Re-checked on a host without a GPU, FlyDSL 0.5.3: the same 96 compile-only builds give 81/81 kernels whose final ISA is byte-identical to main e6d2d310, and whose final ISA and LLVM IR are byte-identical to 517e25b6; main did not change these kernels between bf599489 and e6d2d310. The CPU pytest outcomes of the four neighbouring test files are the same as main e6d2d310 test by test (66). black and ruff are clean. The AOT replay was not repeated.
  • Unverified: no GPU run (not needed for identical ISA).

PR-specific question

What could change at run time when the source changes but the ISA does not?

  • Nothing in the kernels: the launchers, kernel names (cache keys) and kernel arguments are unchanged, and the compiled code is byte-identical. Only the Python that builds the IR moved.

Changed op tests

None.

@lirui927
lirui927 requested a review from a team November 23, 2026 10:23
@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 8496 --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.

@aiter-bot

aiter-bot commented Nov 23, 2026 •

Copy link
Copy Markdown

DRAFT — evaluation paused

PR Self-Review

Revision 32de0e654d92 · mode shadow

This pull request is a draft, so readiness evaluation is paused and no verdict applies. Evaluation resumes when it is marked ready for review and the required CI completes.

@zufayu

zufayu commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

@aiter-bot review

@aiter-bot

Copy link
Copy Markdown

aiter-bot — queued behind 6 review(s); expect to start in about 2.2 h. The box reviews one PR at a time.

@zufayu
zufayu requested a review from Bernard-Liu November 25, 2026 02:38

@aiter-bot aiter-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ROCm/aiter] PR #8496 — [FlyDSL] refactor(flydsl): share the token-multidest quant context

Review (advisory): ⚠️ NEEDS WORK
Validation (deterministic): NOT RUN — the PR changes runtime code but ships no test target (auto-validation disabled); the changed path is run-time-executed (gfx1250 MoE stage1 quant), and this review closed the gap by hand: a base-vs-head compile-only recompile against the merge target (flydsl 0.5.0, dry_run, 49 builds covering every builder family in the file) is byte-identical on all 49 final ISA files with identical kernel-name sets
Perf (advisory): NOT RUN — no perf target (the PR changes runtime code but ships no test target); base and head compile to byte-identical gfx1250 ISA on all 49 builds recompiled here, so the shipped kernel binary — and with it its latency — is unchanged by construction
Size: 225 lines / 1 file
Design (advisory): the sharing seam for #7543 — the quant context becomes a builder-level _quant_ctx closure whose **staging catch-all carries how the hidden row arrives, and the pk8 hidden-row load becomes the module-level _hidden_pk8_load
Human: recommend yadaish review whether the **staging catch-all in _quant_ctx (moe_fused_route_quant_scatter.py:2237) is the right contract for #7543, given _emit_quant_block_loop reads those fields via getattr with silent defaults

📝 [verified] aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py changes run-time-executed code (the gfx1250 MoE stage1 quant builders) but the PR ships no test target and ran no GPU test on any arch — the no-behavior-change claim rested only on the author's own ISA diff, which this review independently reproduced (49/49 builds byte-identical against the merge target, covering token-multidest fp4/fp8 × wmma_rep 1/2 × K 1024/6144 × ksplit 1/2/4/12 × tdm 0/4, routeks, compact, row-indexed and all three route+quant+scatter builders), so the residual gap is only that no automated check exercises the refactored builder end-to-end. Reviewer should ask for one gfx1250 run of the existing test_grouped_skips_pad_rows_routed_to_minus_one (op_tests/flydsl_tests/test_flydsl_grouped_gemm.py:970) before merge, or a note that the GPUs are still unavailable.

1 finding(s) posted inline, on the lines they are about:

  • aiter/ops/flydsl/kernels/moe_fused_route_quant_scatter.py:2237 — _quant_ctx receives its staging fields (how the hidden row arrives) through a **staging catch-all…

valid,
lane,
raw_rows,
**staging,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 [verified] _quant_ctx receives its staging fields (how the hidden row arrives) through a **staging catch-all (moe_fused_route_quant_scatter.py:2237), and _emit_quant_block_loop reads them back with silent getattr defaults (hidden_chunks at :438, chunk_prefetch/hidden_lds_load at :441-442, mx_group_base at :372) — this PR's one call site passes all seven names correctly, but a mistyped staging kwarg in #7543's future call would silently take the default and, with hidden_lds_load set but hidden_chunks typo'd, read every chunk from LDS slot 0, silently producing wrong quantized values. Reviewer should ask #7543 to pass the staging fields as explicit parameters (or assert them at build time) so a typo fails loudly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 517e25b6, and carried through #7543 and #7547.

  • _quant_ctx now takes the seven staging fields as required keyword-only parameters, and the **staging catch-all is gone. A missing or misspelled name is a TypeError when the kernel is built, before anything launches.
  • [FlyDSL] perf(flydsl): fold the gfx1250 TP MoE route into the activation quant #7543's route entry passes one more field, hidden_preloaded. There it is the eighth required parameter, and the default entry passes hidden_preloaded=None. No field has a default, so leaving one out fails as well.
  • Checked with the typo from this comment, compile-only on CPU. With hidden_chunks misspelled at the call site, all 30 token-multidest builds stop with TypeError: build_moe_token_multidest_quant_module.<locals>._quant_ctx() got an unexpected keyword argument 'hidden_chunk'. Did you mean 'hidden_chunks'?. At f41bda5f the same typo built 96/96 without an error and changed the ISA of the six TDM-staged kernels.
  • No kernel changes: the same 96 builds give 81/81 kernels byte-identical to f41bda5f and to main, and the traced IR is identical to f41bda5f, so no FlyDSL cache key moves.
  • Not changed: the getattr defaults in _emit_quant_block_loop stay, because the other builders that share the loop leave hidden_chunks, chunk_prefetch and hidden_lds_load to them. build_moe_token_multidest_compact_quant_module still fills the same fields into a SimpleNamespace by hand; that is main's code, and this PR does not touch it.

@lirui927

Copy link
Copy Markdown
Contributor Author

Thanks for the review. 517e25b6 addresses the inline finding.

@lirui927

Copy link
Copy Markdown
Contributor Author

The gfx1250 run asked for above, at 517e25b6 on one idle MI455X GPU (image rocm/fw-bringup:gfx1250-minimax-m3-20261112 with this tree mounted):

python3 -m pytest -q -rfE -p no:cacheprovider op_tests/flydsl_tests/test_flydsl_grouped_gemm.py -k test_grouped_skips_pad_rows_routed_to_minus_one
4 passed, 55 deselected, 2 warnings in 8.65s

lirui927 and others added 2 commits November 25, 2026 11:20
Move the per-warp _emit_quant_block_loop context of the token-multidest
quant into a _quant_ctx closure in its builder. The kernel now only loads
its routes' rows and hands them over with the hidden-row staging. Pull the
pk8 hidden-row load of _emit_quant_block_loop into _hidden_pk8_load.

No behaviour change: every quant build that goes through
_emit_quant_block_loop compiles to the same gfx1250 ISA as before.

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

_quant_ctx took the fields that say how the hidden row arrives through a
**staging catch-all, and _emit_quant_block_loop reads hidden_chunks,
chunk_prefetch, hidden_lds_load and mx_group_base back with getattr
defaults. A misspelled keyword at a call site would take the default
without an error: with hidden_lds_load set and hidden_chunks misspelled,
every chunk would be read from LDS slot 0.

They are now seven required keyword-only parameters, so a missing or
misspelled name raises TypeError when the kernel is built.

No behaviour change: the call site already passes all seven, and every
quant build that goes through _emit_quant_block_loop compiles to the same
gfx1250 ISA as before.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@lirui927
lirui927 force-pushed the fuse/f9a-multidest-quant-ctx branch from 517e25b to 32de0e6 Compare November 25, 2026 11:28
@lirui927
lirui927 marked this pull request as draft November 27, 2026 08:18
@lirui927

lirui927 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing: #7543 is being reworked so that it no longer needs this refactor (the route operands become an optional kernel operand of the existing token-multidest kernel instead of a second entry point that shares the quant context). Nothing here is needed on its own.

@lirui927 lirui927 closed this Dec 2, 2026
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.

3 participants