Skip to content

[MoE] Skip unused intermediate allocation for fused stage-1 quantization - #7172

Merged
yifehuan merged 2 commits into
ROCm:mainfrom
iraj465:moe-skip-fused-quant-intermediate
Nov 26, 2026
Merged

yifehuan merged 2 commits into
ROCm:mainfrom
iraj465:moe-skip-fused-quant-intermediate

Conversation

@iraj465

@iraj465 iraj465 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Skip the unused a2 allocation in fused_moe_2stages when stage 1 fuses quantization. The caller already passes None in this case and takes the intermediate from stage 1's return value.

Motivation

The discarded BF16 buffer is 36 MiB for M8192, top-6, I384. Removing it reduced PyTorch graph-reserved memory by 36 MiB in all four measured prefill pairs.

Changes

  • Add metadata.fuse_quant to the existing allocation-skip condition.
  • Pass a2 directly to stage 1; its value is now already None for fused quantization.

Performance (if applicable)

MI350X/gfx950; PyTorch 2.11.0+rocm7.2; HIP 7.2.26015; native allocator. Prefill: M8192/H7168/I384/E384/topk6, FP8×FP4, BF16 output, per-1×32 quantization, SiLU limit10, INTERLEAVE/GUGU, Opus fused-quant dispatch. Control: M16/H896/I384/E8/topk2, per-tensor FP8×FP8, BF16 output, SiLU, SEPARATED, nonfused CK dispatch.

Each case used four fresh-process pairs in AB/BA/BA/AB order, seed0, 101 samples per process. Allocation is the primary result.

Allocator observation Base Candidate Difference
Prefill: incremental graph-capture reservation 624 MiB 588 MiB −36 MiB in all four pairs
Prefill: eager memory and peak allocated memory Same Same 0 bytes
Nonfused control: every allocated/reserved measurement Same Same 0 bytes

These numbers are from the first revision of this branch (c23d41447); they were not repeated after main was merged on 24 November. The reserved-memory difference remained through replay. These are allocator observations under the recorded conditions; they do not measure total HBM traffic. Timing drift prevents a speedup conclusion.

Exact scripts, source/configuration pins, run commands, raw measurements, and detailed limitations.

Testing

  • At this revision, on an MI355X: python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter flydsl_mxmoe_g1_a8w4_64x256x256_fp8out_xcd2 opus_moe1_afp8_wfp4_bf16_t64x384_gs_qgb3_aff64_k1lead_fp8 passes, as it does on main 6a0a23ff3. Those are 16 tuned DeepSeek-V4 rows whose stage-1 kernel fuses quantization (prefill sizes of 2048 to 16384 tokens, among them 8192 tokens with hidden 7168, intermediate 384, 384 experts and top-6, the shape measured under Performance), plus the fixed checks of the file. In each of the two runs 1,118 of the 1,791 calls of fused_moe_2stages had fused quantization (128 with an FP8 and 990 with an FP4 intermediate), which is the branch this change touches. Every module was built from the tree's sources.
  • First revision: native oracle, startup buffer/dispatch checks, eager and graph checks passed in all 16 processes on MI350X.
  • First revision: exact-source Black 26.3.0, Ruff 0.15.7, AST parsing, and git diff --check passed. The merge changes no line of this pull request.
  • MI250X/MI300X and full repository suite.

In the first-revision experiment all checked outputs were finite. Both arms reported the same reference mismatch fractions (checkAllclose): 0.7746445 for prefill and 0.0626395 for control, with logits_diff 3.6646665e-4 and 4.3923888e-6 respectively. The upstream strict test rejects NaNs and rejects a nonzero mismatch fraction only when logits_diff > 0.01; both cases pass that rule. The correctness evidence is the per-arm finite-output and strict-oracle checks described here; it establishes acceptance under that rule rather than paired tensor equality.

Documentation

  • No API or configuration change; no user documentation update needed.

Dependencies

  • No new dependencies.

Breaking Changes

None.

PR Self-Review

Reviewed revision

dda83547277355204e990bb56ddd2ea71e645095

Behavior and intent

The same fused-quant predicate moves from the call argument to allocation. No intervening code uses a2; stage-1 output ownership and returned-output handling are preserved.

Validation and unverified scope

On 24 November main 6a0a23ff3 was merged into this branch. It was 596 commits behind, 33 of them in aiter/fused_moe.py; the merge is clean and the two hunks are unchanged. I read the merged function again: between the allocation and the stage-1 call nothing uses a2; stage 1 gets the same argument as before in both cases, None with fused quantization and the buffer otherwise; and a2 is then assigned from what stage 1 returns. At this revision and on that main, on an MI355X, python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter flydsl_mxmoe_g1_a8w4_64x256x256_fp8out_xcd2 opus_moe1_afp8_wfp4_bf16_t64x384_gs_qgb3_aff64_k1lead_fp8 passes with every module built from the tree's sources; 1,118 of the 1,791 calls of fused_moe_2stages in each run had fused quantization.

The allocation experiment under Performance measured 6fe1bfc8… with standard CK 96afae1d… plus this allocation patch, on an MI350X, in the specified prefill and nonfused-control cases; exact identities and commands are in the companion. It was not repeated at this revision. Decode was excluded before that experiment because an earlier baseline dispatch attempt failed.

Unverified: the allocation numbers at this revision; decode shapes; MI250X/MI300X; any speed effect (timing drift between the paired processes was larger than a possible gain, so none is claimed); upstream CI at this revision.

PR-specific question

The critical boundary is stage-1 output ownership. Both source versions pass None when metadata.fuse_quant is truthy. Recorded dispatch checks show fused quantization in prefill and unchanged allocation in the nonfused control; both pass the unchanged strict oracle.

Changed op tests

None. Existing native oracle and contract checks were used unchanged.

@iraj465
iraj465 requested a review from a team October 29, 2026 19:07
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7172 --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.

@aiter-bot

aiter-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

READY FOR HUMAN REVIEW

PR Self-Review

Revision dda835472773 · 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 same fused-quant predicate moves from the call argument to allocation. No intervening code uses `a2`; stage-1 output ownership and returned-output handling are preserved.

Validation and unverified scope

On 24 November main `6a0a23ff3` was merged into this branch. It was 596 commits behind, 33 of them in `aiter/fused_moe.py`; the merge is clean and the two hunks are unchanged. I read the merged function again: between the allocation and the stage-1 call nothing uses `a2`; stage 1 gets the same argument as before in both cases, `None` with fused quantization and the buffer otherwise; and `a2` is then assigned from what stage 1 returns. At this revision and on that main, on an MI355X, `python3 op_tests/test_moe_2stage.py --no-legacy --csv-filter flydsl_mxmoe_g1_a8w4_64x256x256_fp8out_xcd2 opus_moe1_afp8_wfp4_bf16_t64x384_gs_qgb3_aff64_k1lead_fp8` passes with every module built from the tree's sources; 1,118 of the 1,791 calls of `fused_moe_2stages` in each run had fused quantization.

The allocation experiment under Performance measured `6fe1bfc8…` with standard CK `96afae1d…` plus this allocation patch, on an MI350X, in the specified prefill and nonfused-control cases; exact identities and commands are in the companion. It was not repeated at this revision. Decode was excluded before that experiment because an earlier baseline dispatch attempt failed.

Unverified: the allocation numbers at this revision; decode shapes; MI250X/MI300X; any speed effect (timing drift between the paired processes was larger than a possible gain, so none is claimed); upstream CI at this revision.

PR-specific question

The critical boundary is stage-1 output ownership. Both source versions pass `None` when `metadata.fuse_quant` is truthy. Recorded dispatch checks show fused quantization in prefill and unchanged allocation in the nonfused control; both pass the unchanged strict oracle.

Changed op tests

None. Existing native oracle and contract checks were used unchanged.

PR state

  • PASS — pull request is marked ready for review

Copilot findings

  • PASS — no active Copilot finding lacks a substantive author reply
  • ADVISORY — Copilot did not review the current head SHA

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.

@iraj465

iraj465 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

All checks on the current head are green, including the Aiter Test Gate. @yifehuan could you take a look when you have a moment? Thank you!

@iraj465

iraj465 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@yifehuan the self-review bot now reports this pull request as ready for human review: the author answers are accepted and Black, Ruff, the repository dependency check and the Aiter Test Gate passed on this head. I added the missing statement of what is unverified to the description; the code is unchanged.

The change is three lines in fused_moe_2stages: it extends the existing skip of the unused stage-1 output buffer (today for the a16w4 path) to fused stage-1 quantization, where the caller already passes None. Measured on MI350X at M=8192, top-6, intermediate size 384: 36 MiB less reserved memory per captured graph, in all four paired runs. No speed-up is claimed. Could you take a look when you have a moment?

@iraj465

iraj465 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@zufayu as the contact for aiter/fused_moe*: this is a three-line change (it skips an intermediate allocation that is never read when stage 1 fuses quantization, 36 MiB at 8192 tokens), all checks are green on c23d4144, and it has not had a review yet. Could you or someone on the team take a look when you have a moment? Thanks!

@iraj465

iraj465 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@yzhou103 could you take a look at this one when you have a moment? It is a three-line change in fused_moe_2stages: it skips the a2 allocation that is never read when stage 1 fuses quantization (36 MiB less reserved memory in the prefill case), and all checks are green. Thanks!

@iraj465

iraj465 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@zufayu @yzhou103 friendly ping: this is the three-line change in fused_moe_2stages that skips the a2 allocation nobody reads when stage 1 fuses the quantization. All checks have been green since last week, it only needs a review. Thanks!

@iraj465
iraj465 marked this pull request as draft November 24, 2026 15:25
@iraj465
iraj465 marked this pull request as ready for review November 24, 2026 15:26
@iraj465

iraj465 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@yifehuan this one had been sitting on a base from October and its CI records were gone, so there was nothing left for anyone to approve. I merged current main (6a0a23ff3) into it. The change is still the same three lines.

What it does: when stage 1 fuses quantization it returns its own output, and fused_moe_2stages already passed None for the buffer in that case, so the buffer allocated just before was never used (36 MiB for an 8192-token DeepSeek-V4 prefill). It is no longer allocated.

At the new head, on an MI355X, I ran the MoE op test on 16 tuned DeepSeek-V4 rows whose stage-1 kernel fuses quantization. It passes exactly as on main, and 1,118 of the 1,791 fused_moe_2stages calls in the run went through the changed branch. Could you approve the workflows and have a look when you get to it?

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

LGTM

@iraj465

iraj465 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@yifehuan thanks for the approval and for the re-run. It is green now: all eight standard shards and the gate have passed, as have Checks, Triton Test and OPUS Test. Could you help merge it?

@yifehuan
yifehuan merged commit 9e5e036 into ROCm:main Nov 26, 2026
103 of 120 checks passed
@qli88 qli88 mentioned this pull request Nov 30, 2026
5 of 10 tasks
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