Skip to content

perf(gemm-ar): load mxfp8 scales one K step ahead at K >= 4096 - #857

Merged
yangyuhuiling merged 5 commits into
mainfrom
xiangch/gemm-ar-mxfp8-scale-prefetch
Nov 4, 2026
Merged

yangyuhuiling merged 5 commits into
mainfrom
xiangch/gemm-ar-mxfp8-scale-prefetch

Conversation

@yangyuhuiling

@yangyuhuiling yangyuhuiling commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

compile_fused_gemm_scatter gets scale_prefetch: for quant="mxfp8", the A/B ue8m0 scales of K step k+1 are loaded at the top of step k instead of step k+1. It is on by default only at K >= SCALE_PREFETCH_MIN_K (4096); below that the generated kernel is unchanged. scale_prefetch=True/False overrides. The kernel name gets an f tag when it is on.

Why

At the same 256x256 tile, the mxfp8 GEMM measured 15-20% slower than ptpc (e.g. 1087 vs 905us at 16384x10240x8192). None of that is the scaled MFMA: the same kernel with constant scales instead of loaded ones runs at 899us.

The cost is where the scale loads sit. Six buffer_load_dwords per K step are issued after the previous step's tile prefetches, and the first MFMA waits for them with s_waitcnt vmcnt(3). vmcnt retires in issue order, so that wait also retires the tile prefetches the pipeline meant to keep in flight: about half a step of latency hiding lost every step. Loaded one step earlier, the wait becomes vmcnt(17..20) and stops blocking. Two steps ahead gains nothing more.

It is not free: six more VGPRs stay live in a kernel already at 256, which is why it is gated on K.

Measured

On main with #748 and FlyDSL 0.4.0. MI355X, GEMM only (fuse=False), 256x256, CUDA graph of 50 calls with B rotated past the LLC, median of two to three interleaved rounds, relL2 1.66e-3 on every point:

M x N x K off on
1024 x 5120 x 4096 V4.1-Flash wo_b, TP2 49.6 44.4 -10.4%
4096 x 5120 x 4096 V4.1-Flash wo_b, TP2 95.9 90.3 -5.8%
8192 x 5120 x 4096 V4.1-Flash wo_b, TP2 151.5 146.8 -3.1%
16384 x 5120 x 4096 V4.1-Flash wo_b, TP2 274.9 265.2 -3.5%
8192 x 7168 x 4096 204.3 201.7 -1.2%
8192 x 5120 x 8192 V4.1-Flash wo_b, TP1 281.5 271.4 -3.6%
16384 x 5120 x 8192 V4.1-Flash wo_b, TP1 546.1 504.0 -7.7%
4096 x 10240 x 8192 282.8 267.6 -5.4%
16384 x 10240 x 8192 1099.5 989.2 -10.0%
2048 x 18432 x 16384 590.1 527.7 -10.6%
8192 x 18432 x 16384 1939.0 1835.8 -5.3%

Below 4096 it is shape-dependent rather than a win: forced on, V4.1-Flash's TP2 wq_b (16384 x 1280) is 0-5% slower from M=1024 to 8192, and 256x256 shapes at K = 512 and 1280-2048 were 1-8% slower (the same direction #806 reports for this approach at those K); it was faster at K = 256, 640, 1792 and on the 256x128 tile. 8192x7168x4096 is the one shape at the threshold that does not gain, and it is level hot (+0.9%).

This is independent of #806, which adds a separate mxfp8_raw mode staging row-major scales through LDS; it leaves quant="mxfp8" alone, and this PR touches only that mode.

Test

  • test_mxfp8_lands_at_the_fp8_floor now runs scale_prefetch at its default and forced on, at K = 256 (no main-loop step, only the peeled ones), 512, 2048 and 8192.
  • tests/python/cco/test_gemm_ar.py, test_gemm_ar_op.py, test_gemm_ar_bench.py: 100 passed on 8x MI355X with FlyDSL 0.4.0, after merging main.

🤖 Generated with Claude Code

xiangch and others added 3 commits October 27, 2026 10:00
At the same 256x256 tile the mxfp8 GEMM is 15-20% slower than ptpc, and none
of it is the scaled MFMA: with constant scales it runs at ptpc's speed. The
scale buffer_loads are issued after the previous step's tile prefetches, so
the vmcnt wait the first MFMA needs for them also retires those prefetches,
costing about half a step of latency hiding every step.

`scale_prefetch` loads step k+1's scales at the top of step k; the wait
becomes vmcnt(17..20) and no longer blocks. It keeps six more VGPRs live, so
it defaults on only for quant="mxfp8" at K >= 8192, where it measured 3-11%
faster on every shape tried; below that it was shape-dependent. The kernel
name gets an `f` when it is on; at K < 8192 the generated kernel is unchanged.

Co-Authored-By: Claude <[email protected]>
…-scale-prefetch

# Conflicts:
#	python/mori/ops/gemm_ar/kernels_fused.py
Re-measured on main (#748, FlyDSL 0.4.0). DeepSeek-V4.1-Flash's TP2 wo_b
(N=5120, K=4096) gains 3-10% at every M from 1024 to 16384, which the
K >= 8192 threshold left out. Its TP2 wq_b (K=1280) still does not gain, and
the one other K=4096 shape is level, so the threshold moves to 4096. The
K >= 8192 results are unchanged on 0.4.0.

Co-Authored-By: Claude <[email protected]>
@yangyuhuiling yangyuhuiling changed the title perf(gemm-ar): load mxfp8 scales one K step ahead at K >= 8192 perf(gemm-ar): load mxfp8 scales one K step ahead at K >= 4096 Oct 27, 2026
…-scale-prefetch

# Conflicts:
#	python/mori/ops/gemm_ar/README.md
#	python/mori/ops/gemm_ar/kernels_fused.py
#	tests/python/cco/test_gemm_ar.py
yangyuhuiling pushed a commit that referenced this pull request Oct 29, 2026
* GemmA2AOp rejects k < 256 (gemm_ar's MIN_K; the main loop needs two K
  steps), and so does compile_fused_gemm_a2a. K=128 used to construct and
  return relL2 ~1.0.
* The constructor raises when the dev-comm reports fewer SDMA queues than
  asked for -- SDMA off, or an earlier dev-comm on the comm fixed a smaller
  count -- instead of letting every put move nothing; and when lsa_size !=
  world_size, since world ranks are used as LSA ranks.
* The window's zeroing is synchronized and followed by comm.barrier(), so no
  peer's first push or credit can land before it.
* The election counter goes back to zero on every call (the elected tile
  subtracts c_tiles) and the test is seq == c_tiles. A counter left to run on
  and tested modulo c_tiles elects the wrong tile after 2^31 increments, or
  2^32 unless c_tiles is a power of two.
* split-lsa fences every lane before its block barrier and flag store, so the
  flag cannot overtake the payload.
* Signal and buffer helpers come from mori.cco.device.flydsl.signals/.buffer
  rather than through gemm_ar._compat; the pinned main loop notes that #857
  will need mirroring.
* README: the aiter comparison is against r13 (aiter head 474128f, timed
  through SpQkvLarge.run), from the review's measurements.

Tests: the op test checks every election counter is zero after its calls,
and a new case checks the constructor rejects k=128, two queues on a
one-queue comm, and a comm with MORI_ENABLE_SDMA=0. 49 passed on 8x MI355X.

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

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

I tested this on MI355X (gfx950, ROCm 7.14, FlyDSL 0.4.0), with the PR merged onto current main. The 256x256 results look good. There is one ordering problem on the 128-wide tile that I think should be fixed before this turns on by default there.

The gain reproduces. GEMM only, 256x256, cold (B rotated past the LLC), median of 3 interleaved passes. Off and on give bit-identical output at every shape.

M x N x K off on
1024 x 5120 x 4096 47.3 40.9 -13.5%
4096 x 5120 x 4096 94.2 87.2 -7.4%
16384 x 5120 x 4096 274.2 257.9 -6.0%
8192 x 7168 x 4096 199.9 197.3 -1.3%
16384 x 5120 x 8192 566.5 523.2 -7.6%
16384 x 10240 x 8192 1090.2 991.6 -9.0%
2048 x 18432 x 16384 586.6 538.0 -8.3%
2048 x 5120 x 2048 (forced on, below the gate) 28.5 29.8 +4.7%

Tests: test_gemm_ar_op.py (80 passed), test_gemm_ar_bench.py (4 passed), and every new scale_prefetch case in test_gemm_ar.py pass.

1. On the 256x128 tile, prefetch removes the wait that covered a_next1's second DMA

Mxfp8GemmOp compiles BLOCK_N=128 (no permlane) below its grid crossover, which for N=5120 means M <= 1536. It passes no scale_prefetch, so at K >= 4096 this PR turns prefetch on there.

The count. The end-of-step wait_barrier(2 * N_LDS_STEPS_A + N_LDS_STEPS_B) (kernels_fused.py:1611) is meant to make sure a_next1 has landed. Only B + A + B loads are issued after a_next1, though. At 256x256 that is 6 against vmcnt(6), which is exact. At 256x128 (A=2, B=1) it is 4 against vmcnt(5), so a_next1's second load (rows 64..127 of the A1 half, read only by the wave_m == 1 waves) is not covered by this wait.

Why it worked before. With prefetch off, step k+1's own scale loads are issued after a_next1(k). The compiler's wait for them before c00 is vmcnt(3), which retires a_next1(k) before the barrier that the next a_s2r.load(a_cur1) pairs with.

Why it breaks now. With prefetch on, those scales were issued a step earlier, before a_next1(k). The compiler's wait becomes vmcnt(13) and no longer reaches it.

The ISA confirms it. I dumped the 256x128, K=4096 kernel. Each step is:

SCx4 ds_read x10 [a_next1: buffer_load..lds x2] s_barrier W mfma s_barrier ds_read x2 [lds x1] s_barrier W mfma s_barrier ds_read x8 (a_cur1) [lds x2] s_barrier mfma s_barrier [lds x1] vmcnt(5) s_barrier mfma

Here W is vmcnt(3) with prefetch off and vmcnt(13) with it on, which is the only difference. With prefetch on, nothing between a_next1(k) and the a_cur1 reads of step k+1 waits below 10 younger loads.

Not reproduced. I ran 8 narrow-tile configurations under a concurrent 1 GiB copy stream, with M in 256..1536, N in {5120, 7168} and K from 4096 to 16384. That was 12,800 calls with prefetch on, every one bit-identical to off. So this is an ordering hole that happens to be covered in practice by load-completion order, not an observed corruption.

The fix is cheap. Use wait_barrier(min(2A+B, A+2B)). That gives vmcnt(4) at 256x128 and no codegen change at 256x256. Alternatively, keep the default on only for BLOCK_N == 256.

The count itself predates this PR: mxfp8_raw on the narrow tile has the same gap. This PR extends it to the default mxfp8 path, where the scale wait used to cover it.

2. No test compiles the configurations that are on by default

The only prefetch-on kernel the tests build is fuse=False, 256x256, with permlane. Two configurations this PR turns on by default are compiled by no test at K >= 4096:

  • Mxfp8GemmOp's 128-wide tile, which is where item 1 lives;
  • the fused GemmAllReduceOp kernel.

A test_standalone_mxfp8_gemm case at K=4096 or 8192 would cover both tiles, with one M that picks the narrow tile and one that picks the wide one. Comparing on vs off bitwise would be sharper than putting both against the fp8 floor, since the two are bit-identical.

Smaller points

  • README note on spills. It says prefetching costs six live VGPRs and spills more at short K. At the K where the gate turns it on, the 256x256 ISA shows the reverse:

    • K=2048: spills go from 4 to 18.
    • K=4096 and K=8192: spills go from 6 (28 B scratch) to 0.

    It may be worth rewording the cost so it applies only to short K.

  • mxfp8_raw section. The PR inserts this section just before the mxfp8_raw one, so that section's "beat both" now reads as raw beating prefetch, which was not measured. Its "new callers should take mxfp8_raw" also deserves a K qualifier: at K >= 4096 packed-with-prefetch and raw have not been compared.

  • Silently dropped flag. An explicit scale_prefetch=True with ptpc or blockscale is dropped (bool(scale_prefetch) and mxfp8), while mxfp8_raw raises. Raising for every non-mxfp8 quant would be consistent.

Review follow-up.

- The end-of-step wait_barrier(2A + B) is meant to cover a_next1, but only
  B + A + B loads are issued after it: exact at 256x256, one too many at
  Mxfp8GemmOp's 256x128 tile, where a_next1's second load was retired only
  by the wait for the next step's own scales. scale_prefetch issues those
  before a_next1, which removed that cover (vmcnt(3) -> vmcnt(13)). Use
  min(2A + B, A + 2B): vmcnt(4) at 256x128, unchanged code at 256x256.
  Narrow tile, K 4096-16384: prefetch on is 14-21% faster than off and
  bit-identical to it.
- An explicit scale_prefetch=True with a quant that has no mxfp8 scales is
  refused instead of dropped.
- Tests: prefetch on vs off bitwise on both N tiles at K 4096 and 8192;
  Mxfp8GemmOp at K=4096 on both tiles; the fused kernel compiled with the
  default prefetch at K 4096 and 8192 (COMPILE_ONLY); the refusal.
- README: the register cost holds at short K only (spills fall to 0 at
  K >= 4096); the a_next1 wait; mxfp8_raw's comparison and advice are
  qualified by K, since raw was not measured against packed-with-prefetch.

Co-Authored-By: Claude <[email protected]>
@yangyuhuiling

Copy link
Copy Markdown
Contributor Author

Thanks. The a_next1 count was exactly right, and so was the way the prefetch exposed it. Everything is addressed in dfc2532, as a commit on top of the branch.

1. The narrow-tile wait. The end-of-step wait is now wait_barrier(min(2A + B, A + 2B)), with a comment explaining why the scale wait used to cover it. In the dumped K=4096 ISA:

  • 256x128: the end-of-step wait is vmcnt(4) (31 of them), the scale waits stay at vmcnt(13), and there are no spills.
  • 256x256: still vmcnt(6). min() is 6 there, so the code is unchanged.

I measured prefetch on vs off on the narrow tile with the fix: graph replay, B rotated past the LLC, median of 7 rounds. Every pair is bit-identical.

M x N x K (256x128) off on
256 x 5120 x 4096 37.5 31.4 -16%
1024 x 5120 x 4096 39.2 33.2 -15%
1536 x 5120 x 4096 43.0 37.2 -14%
1024 x 7168 x 8192 78.3 67.1 -14%
512 x 5120 x 16384 141.7 111.6 -21%

So the default stays on for both tiles.

2. Tests for the default-on configurations.

  • test_scale_prefetch_is_bitwise_identical_on_both_tiles compares prefetch on vs off with torch.equal. It covers 256x128 (no permlane) and 256x256, at K = 4096 and 8192, with 4 calls each.
  • test_standalone_mxfp8_gemm gets a K=4096 shape. M = 64/192/1024 run the narrow tile and M = 4096 the wide one.
  • test_fused_kernel_compiles_with_the_default_scale_prefetch compiles the fused kernel the way GemmAllReduceOp builds it, at K = 4096 and 8192, with prefetch off as the control. It uses FlyDSL's COMPILE_ONLY, so it needs no communicator: the main loop is the one the bitwise test runs, and only the epilogue's puts would need one.

Smaller points:

  • README:
    • the register cost is now stated for short K only (K=2048 4 -> 18 spills; K >= 4096 6 -> 0);
    • there is a paragraph on the a_next1 wait;
    • the mxfp8_raw section now says it beat the packed and unpacked layouts without the prefetch, and that raw has not been compared against packed-with-prefetch at K >= 4096. The "new callers should take mxfp8_raw" line is limited to K < 4096.
  • An explicit scale_prefetch=True with ptpc or blockscale now raises instead of being dropped.

Testing. I ran this on MI355X, FlyDSL 0.4.0, with the ci-cco image:

  • the new and existing prefetch/mxfp8 tests in test_gemm_ar.py pass;
  • the single-GPU tests in test_gemm_ar_op.py pass (80).

The multi-rank tests (test_gemm_ar_bench.py, the 2-rank cases in test_gemm_ar_op.py) could not start on that machine for an unrelated host reason. There, amdsmi_get_socket_handles reports 0 sockets once HIP is initialized, and the topology loader then exits. They are not affected by this change, and CI will run them.

@yangyuhuiling

Copy link
Copy Markdown
Contributor Author

A correction to the testing note above. The multi-rank suites did not fail for a host reason. My run used the container's system Python instead of /opt/venv, which CI uses. With /opt/venv on the same machine, test_gemm_ar_op.py, test_gemm_ar_bench.py and the full test_gemm_ar.py all pass on dfc2532: 146 passed, 2-rank and 8-rank cases included.

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

Re-tested dfc2532d, merged onto current main, on MI355X (ROCm 7.14, FlyDSL 0.4.0). Everything is addressed. LGTM.

  • The narrow-tile wait. The K=4096 ISA of the 256x128 tile matches what you describe: the end-of-step wait is vmcnt(4) (31 of them), the scale waits stay at vmcnt(13), and there are no spills.

  • The min() also closes the same gap for mxfp8_raw on the narrow tile. On main that tile ends each step at vmcnt(5), and since raw reads its scales from LDS, no later wait covers a_next1. With this PR it is vmcnt(4). Median of 4–5 runs, us:

    M x N x K, raw, 256x128 main this PR
    1024 x 5120 x 2048 21.4 21.4
    1024 x 5120 x 4096 37.4 38.3
    1536 x 5120 x 4096 43.7 45.0
    512 x 5120 x 8192 61.2 61.5

    That is up to 3% at K=4096, a fair price for the hole.

  • Prefetch on vs off with the fix. Kernel alone, a graph of 16 calls over 16 copies of B (past the LLC), median of 7, two rounds averaged, us. Each config's output was bitwise identical across 50 calls:

    M x N x K tile off on
    256 x 5120 x 4096 256x128 37.9 31.7 -16%
    1024 x 5120 x 4096 256x128 39.9 34.7 -13%
    1536 x 5120 x 4096 256x128 45.2 40.8 -10%
    1024 x 7168 x 8192 256x128 78.8 73.4 -7%
    512 x 5120 x 16384 256x128 136.3 107.2 -21%
    4096 x 5120 x 4096 256x256 98.4 91.6 -7%
    4096 x 7168 x 8192 256x256 198.5 194.3 -2%

    Two of the narrow-tile gains are smaller than yours, but every one has the same sign, so on by default for both tiles holds.

  • raw against packed with the prefetch. The README says this comparison has not been made. On the narrow tile at K=4096, packed with the prefetch is about 9% faster: 34.7 vs 38.3 us at M=1024, and 40.8 vs 45.0 us at M=1536. That supports limiting "new callers should take mxfp8_raw" to K < 4096.

  • Tests. test_gemm_ar.py passes (53 passed, 1 skipped on 4 GPUs), and so does test_gemm_ar_op.py (90 passed), new tests included.

@yangyuhuiling
yangyuhuiling merged commit f303c3e into main Nov 4, 2026
13 of 15 checks passed
@QizhouZhang97
QizhouZhang97 deleted the xiangch/gemm-ar-mxfp8-scale-prefetch branch November 10, 2026 13:19
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.

2 participants