Skip to content

[FlyDSL] [MoE] Use streaming loads for large FP8 stage-2 reductions - #7940

Closed
jakki-amd wants to merge 3 commits into
ROCm:mainfrom
jakki-amd:k3-stage2-streaming-loads
Closed

jakki-amd wants to merge 3 commits into
ROCm:mainfrom
jakki-amd:k3-stage2-streaming-loads

Conversation

@jakki-amd

@jakki-amd jakki-amd commented Nov 13, 2026 •

Copy link
Copy Markdown

Main work done by @eppaneamd: he found and measured that nontemporal loads speed up the FP8 stage-2 reduction for large Kimi-K3 prefill batches on MI350X. @jakki-amd ported it to main, added the size gate, AOT coverage and tests, and re-measured on MI355X; opening on his behalf.

Summary

The FlyDSL v2 stage-2 wrapper (_flydsl_v2_stage2_wrapper, AITER_FLYDSL_STAGE2_FP8=1) writes FP8 route rows that the topk reduction reads exactly once. This PR passes streaming_loads=True to that reduction when the route buffer exceeds 192 MiB. Output is bit-identical.

Motivation

compile_moe_reduction supports streaming_loads and the Q256 ASM path uses it; the v2 stage-2 FP8 reduction did not. On large buffers the default cache policy costs 6-7 % of stage-2 time (GEMM plus reduction) at Kimi-K3 prefill sizes. On small buffers streaming is slower, hence the size gate.

Changes

  • aiter/fused_moe.py: _use_streaming_reduce_loads(route_out_nbytes, is_fp8_route) enables streaming for FP8 route rows above AITER_FLYDSL_STAGE2_FP8_REDUCE_STREAMING_MIN_MIB (default 192). BF16 and FP4 route reductions are unchanged (not measured).
  • aiter/aot/flydsl/mxfp4_moe.py: FP8 route-out reduce jobs compile both variants (_nt and default), so FLYDSL_RUNTIME_RUN_ONLY=1 finds the one the runtime selects.
  • op_tests/flydsl_tests/test_flydsl_moe.py: threshold test (CPU) and bit-equality test of the two variants (gfx950).

Performance

All numbers time the stage-2 pair as fused_moe calls it (GEMM plus reduction) on one GPU, with AITER_FLYDSL_STAGE2_FP8=1, tuned kernels from the shipped CSVs and random weights. Cold regime: 1 GiB is written before every call.

Kimi-K3

Shape: hidden 3584, inter 384, 896 experts, top-16, MXFP4 a4w4. 12 interleaved rounds of 6 calls, geomean of per-round median ratios; A/A within ±0.7 %.

MI355X:

Tokens Route buffer Before (us) After (us) Change
2048 116 MiB 185.1 186.1 x0.995 (A/A x0.994), gate off
3072 174 MiB 251.9 252.0 gate off (x1.007 if forced on)
4096 232 MiB 275.2 271.0 * x1.015
4608 261 MiB 288.3 283.0 x1.015
5120 290 MiB 300.9 292.6 x1.028
8192 464 MiB 407.3 383.1 x1.068
16384 928 MiB 682.3 634.6 x1.077

* Gate forced on (threshold 0), the same choice the default makes here; "After" computed from the measured ratio.

MI350X (@eppaneamd, same shape, 16-round cold ruler): x0.956 to x0.976 up to 2048 tokens, x1.007-1.010 at 4096, x1.019-1.056 from 5120 to 16384.

All affected tuned rows

MI355X. Each case runs fused_moe with inputs that match a tuned CSV row and checks that the row's kernel ran. Arms: gate off, gate off again (A/A noise), streaming forced on. 10 rounds in shuffled arm order, median of 5 calls per round. "Faster" or "slower" means outside the A/A spread.

  • Shipped rows the gate turns on: 109 rows in 20 CSVs. Output bit-identical in all 109. 45 faster (up to x1.153), 62 within noise, 2 slower (x0.976, x0.987); a duplicate CSV row of each of the two measured x1.017 and x1.011. Geomean x1.032.
  • Gate placement: the tuned rows plus 198 sweep cases around the gate (token counts that hit each shape's tuned rows), 309 cases over all 21 (hidden size, top-k) shapes in the shipped CSVs, pooled by route buffer size:
Route buffer Cases Forced on vs off (geomean)
< 160 MiB 123 x0.988 to x0.991
160-192 MiB 13 x0.996
192-256 MiB 56 x1.010
256-1536 MiB 104 x1.032 to x1.054
> 1536 MiB 13 x0.999
  • 20-round repeat of the cases near the gate (89 with an FP8 route): 160-192 MiB x0.996, 192-256 MiB x1.013. None of the 48 gated cases is slower and none of the 41 ungated cases is faster when forced on. A lower gate would add no gain; a higher one would drop the 192-256 MiB gain.
  • Per shape: below 192 MiB no shape is faster than x1.008; above it, 18 of the 20 shapes with data there are faster (x1.001 to x1.064). The exceptions have 1 and 3 cases (x0.992, x0.994). No trend with hidden size or top-k.
  • Above 1.4 GiB, 20 rounds: flat, geomean x1.005 over 14 cases. The weakest shape is Kimi-K2 FP8/FP4 at top-k 9, about 1 % slower (x0.979 to x1.004 over four 10- and 20-round measurements at 2088 MiB). No upper cap added.
  • The reduction kernel alone flips sign earlier (x0.87 at 145 MiB, x1.09 at 174 MiB) because a copy rewrites the buffer before each call, so it overstates the gain. The pair numbers above set the gate.

Testing

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

The FlyDSL v2 stage-2 path is gfx950-only, so MI250X and MI300X do not apply.

  • pytest op_tests/flydsl_tests/test_flydsl_moe.py -k "streaming or fp8_ep_reduction": 3 passed on MI355X at this PR's head 7dd9c0d (merged with main b9e08da), FlyDSL 0.5.0. Black and ruff 0.16.0 clean.
  • AOT: python -m aiter.aot.flydsl.mxfp4_moe on the 16384-token row of kimik3_a4w4_tuned_fmoe.csv with AITER_FLYDSL_STAGE2_FP8=1 (10/10 compiled); both variants then run with FLYDSL_RUNTIME_RUN_ONLY=1.
  • AOT negative control: with main's AOT script, the streaming variant fails in run-only mode ("no usable AOT cache"). Without the AOT change this PR would break run-only deployments for large batches.

Documentation

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

The new environment variable is a tuning override with a measured default, documented at its definition.

Dependencies

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

Breaking Changes

None. Bit-identical output; default behavior changes only for FP8 route buffers above 192 MiB.

PR Self-Review

Reviewed revision

7dd9c0d

Behavior and intent

With AITER_FLYDSL_STAGE2_FP8=1, the FP8 stage-2 reduction now reads route buffers larger than 192 MiB with nontemporal loads. The buffer is read exactly once, so it gains nothing from being cached. Smaller buffers keep the default cache policy, because streaming is slightly slower there. The gate is set by measured GEMM plus reduction time, and the result is bit-identical in every measured case.

Validation and unverified scope

Validated: see Testing and Performance (every timed output bit-checked); MI350X data from @eppaneamd.

Not verified:

  • full AOT --check replay of the default CSV corpus (one Kimi-K3 row built);
  • end-to-end serving throughput (the stage-2 pair is a fraction of a MoE layer);
  • MI350X for shapes other than Kimi-K3.

PR-specific question

Which configs change behavior? With AITER_FLYDSL_STAGE2_FP8=1, every flydsl_moe2_layout_*_reduce* config whose FP8 route buffer exceeds 192 MiB, regardless of model: 109 rows in 20 shipped CSVs (DeepSeek, GLM, Kimi, Qwen, MiniMax, Mistral and gpt-oss configs). All 109 were run on MI355X with bit-identical output; see "All affected tuned rows". With the default AITER_FLYDSL_STAGE2_FP8=0 nothing changes.

Changed op tests

  • test_stage2_fp8_reduce_streaming_threshold (CPU, new): gate boundary, BF16 never streams, and the Kimi-K3 sizes the default was measured at.
  • test_flydsl_stage2_fp8_reduction_streaming_loads_bit_equal (new): _nt is bit-identical to the default variant for FP8 route rows with deferred route weights at the Kimi-K3 row width. The existing test_flydsl_stage2_fp8_ep_reduction covers only the default.

Main work done by eppaneamd <[email protected]>

The FP8 route rows written by the FlyDSL v2 stage-2 GEMM are read exactly
once by the topk reduction. Pass streaming_loads=True to that reduction
when the route buffer exceeds 192 MiB (configurable with
AITER_FLYDSL_STAGE2_FP8_REDUCE_STREAMING_MIN_MIB); below that size the
nontemporal loads are slower. The output is bit-identical.

FlyDSL AOT now builds both reduction variants for FP8 route-out jobs, so
FLYDSL_RUNTIME_RUN_ONLY finds whichever one the runtime selects.

This is eppaneamd's work: he found and measured that nontemporal loads
speed up the FP8 stage-2 reduction for large Kimi-K3 prefill batches on
MI350X. jakki-amd ported it to main and added the size gate, the AOT
coverage and the tests.

Co-authored-by: eppaneamd <[email protected]>
@jakki-amd
jakki-amd requested a review from a team November 13, 2026 14:07
@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 7940 --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 13, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision e720e8dc6e6d · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA
  • FAIL — validation answer must explicitly state what remains unverified (or say none with a reason)
  • FAIL — changed op test not explained in Changed op tests: op_tests/flydsl_tests/test_flydsl_moe.py

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

@Bernard-Liu

Copy link
Copy Markdown
Contributor

With AITER_FLYDSL_STAGE2_FP8=1, this gate switches every flydsl_moe2_layout_*_reduce* config whose route buffer exceeds 192 MiB to the _nt reduction, not only Kimi-K3. In the shipped CSVs, 109 tuned rows across 20 model configs are affected: DeepSeek-V3/V4/V4.1, GLM-5/5.3-Flash, Kimi-K2/K3, Qwen3.5/3.8, MiniMax-M2.5/M3, Mistral-Large-4 and gpt-oss. The PR only tested Kimi-K3.

Could you run a correctness test (gate on vs. off, bit-identical output) and a performance regression test (the stage-2 GEMM plus reduction, before vs. after) for all affected configs before this merges?

@Bernard-Liu

Copy link
Copy Markdown
Contributor

How was the 192 MiB threshold chosen? The description places it from Kimi-K3 reduction-only measurements on MI355X and says it was tuned on one shape. Has it been validated on the other affected models' shapes? If not, please confirm that the crossover point holds for them too, or explain why the threshold should not depend on the shape (e.g. hidden size and topk).

@jakki-amd

jakki-amd commented Nov 17, 2026 •

Copy link
Copy Markdown
Author

With AITER_FLYDSL_STAGE2_FP8=1, this gate switches every flydsl_moe2_layout_*_reduce* config whose route buffer exceeds 192 MiB to the _nt reduction, not only Kimi-K3. In the shipped CSVs, 109 tuned rows across 20 model configs are affected: DeepSeek-V3/V4/V4.1, GLM-5/5.3-Flash, Kimi-K2/K3, Qwen3.5/3.8, MiniMax-M2.5/M3, Mistral-Large-4 and gpt-oss. The PR only tested Kimi-K3.

Could you run a correctness test (gate on vs. off, bit-identical output) and a performance regression test (the stage-2 GEMM plus reduction, before vs. after) for all affected configs before this merges?

@Bernard-Liu Thanks, both are done on MI355X. Results are in the description under "All affected tuned rows"; summary below.

Method

  • Each case runs fused_moe with AITER_FLYDSL_STAGE2_FP8=1 and inputs that match one tuned CSV row (token count, hidden, inter, experts, top-k, activation, dtypes), and checks that the row's kernel ran.
  • It times the stage-2 GEMM plus reduction (_flydsl_v2_stage2_wrapper) in three arms: gate off, gate off again (A/A, for noise), streaming forced on.
  • Cold cache (1 GiB written before every call), median of 5 calls per round, 10 rounds in shuffled arm order (20 for the repeats).
  • "Faster" or "slower" means outside the A/A spread.

Correctness and regression on all affected configs

The same 109 rows in 20 CSVs that you counted:

  • Output bit-identical to gate off in all 109.
  • 45 faster (up to x1.153), 62 within noise, 2 slower: Kimi-K2 FP8/FP4 at 131072 tokens (x0.976) and GLM-5 FP4 at 4096 tokens (x0.987). A duplicate CSV row of the same shape measured x1.017 and x1.011 respectively, so I read both as noise.
  • Geomean x1.032.

@jakki-amd

Copy link
Copy Markdown
Author

How was the 192 MiB threshold chosen? The description places it from Kimi-K3 reduction-only measurements on MI355X and says it was tuned on one shape. Has it been validated on the other affected models' shapes? If not, please confirm that the crossover point holds for them too, or explain why the threshold should not depend on the shape (e.g. hidden size and topk).

I added 198 sweep cases around the gate (33 to 696 MiB, token counts that hit each shape's tuned rows) and pooled them with the 111 tuned-row cases: 309 cases over all 21 (hidden, top-k) shapes in the shipped CSVs, 33 MiB to 8.2 GiB.

  • Pooled: every size bin below 192 MiB is slightly slower when forced on (x0.988 to x0.996). Every bin from 192 MiB to 1.5 GiB is faster (x1.010 at 192-256 MiB, up to x1.054).
  • Per shape: below 192 MiB no shape is faster than x1.008. Above it, 18 of the 20 shapes with data there are faster; the two exceptions have 1 and 3 cases at x0.992 and x0.994. I see no trend with hidden size or top-k.
  • 20-round repeat near the gate: none of the 48 gated cases is slower, and none of the 41 ungated cases is faster when forced on. Moving the gate down would add no gain; moving it up would drop the 192-256 MiB gain.
  • Above 1.5 GiB the effect is flat (x0.999 pooled). Kimi-K2 FP8/FP4 at top-k 9 is the weakest case at about 1 % slower. I did not add an upper cap because the total over that range is neutral.

Not covered: MI350X for shapes other than Kimi-K3, and end-to-end serving. I also merged main into the branch (conflicts with the new FP4 route reduction; both kept, and FP4 never streams).

@Bernard-Liu

Copy link
Copy Markdown
Contributor

After looking at the results, I'm going to close this PR, for two reasons.

The gain is small. All numbers time only the stage-2 pair (GEMM plus reduction). Across the 109 affected tuned rows the geomean is x1.032. Just above the gate (192–256 MiB) the gain is about 1%, close to the ±0.7% A/A noise, and above 1.5 GiB it is flat. End-to-end serving was not measured. Stage-2 is only part of a MoE layer, so the end-to-end gain would be well below these numbers.

The change is not general. It only takes effect when the following all hold:

  • AITER_FLYDSL_STAGE2_FP8=1, which is off by default;
  • the FP8 route reduction in the FlyDSL v2 stage-2 path; BF16 and FP4 route reductions are unchanged;
  • large prefill batches with a route buffer above 192 MiB, about 3,400 tokens for the Kimi-K3 and DeepSeek-V3 shapes.

The 192 MiB gate is an empirical constant tied to the gfx950 cache size. In return for this narrow gain, the codebase gets a new environment variable, an extra AOT reduction variant per shape, and a threshold that would need re-validation whenever kernels, tuned configs or hardware change.

If end-to-end serving data later shows a meaningful gain in a configuration we ship by default, I'm happy to reopen and revisit.

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