Repository navigation
perf(gemm-ar): load mxfp8 scales one K step ahead at K >= 4096 - #857
Conversation
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]>
…-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
* 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
left a comment
There was a problem hiding this comment.
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
GemmAllReduceOpkernel.
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_rawsection. The PR inserts this section just before themxfp8_rawone, so that section's "beat both" now reads as raw beating prefetch, which was not measured. Its "new callers should takemxfp8_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=Truewithptpcorblockscaleis dropped (bool(scale_prefetch) and mxfp8), whilemxfp8_rawraises. 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]>
|
Thanks. The 1. The narrow-tile wait. The end-of-step wait is now
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.
So the default stays on for both tiles. 2. Tests for the default-on configurations.
Smaller points:
Testing. I ran this on MI355X, FlyDSL 0.4.0, with the
The multi-rank tests ( |
|
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 |
jhchouuu
left a comment
There was a problem hiding this comment.
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 atvmcnt(13), and there are no spills. -
The
min()also closes the same gap formxfp8_rawon the narrow tile. Onmainthat tile ends each step atvmcnt(5), and since raw reads its scales from LDS, no later wait coversa_next1. With this PR it isvmcnt(4). Median of 4–5 runs, us:M x N x K, raw, 256x128 mainthis 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.pypasses (53 passed, 1 skipped on 4 GPUs), and so doestest_gemm_ar_op.py(90 passed), new tests included.
What
compile_fused_gemm_scattergetsscale_prefetch: forquant="mxfp8", the A/B ue8m0 scales of K stepk+1are loaded at the top of stepkinstead of stepk+1. It is on by default only atK >= SCALE_PREFETCH_MIN_K(4096); below that the generated kernel is unchanged.scale_prefetch=True/Falseoverrides. The kernel name gets anftag 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 withs_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 becomesvmcnt(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:wo_b, TP2wo_b, TP2wo_b, TP2wo_b, TP2wo_b, TP1wo_b, TP1Below 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_rawmode staging row-major scales through LDS; it leavesquant="mxfp8"alone, and this PR touches only that mode.Test
test_mxfp8_lands_at_the_fp8_floornow runsscale_prefetchat 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