Repository navigation
a2a_gemm: all-to-all fused into the fp8 GEMM that consumes it - #868
Conversation
…nsumes it Ulysses ends attention with every rank holding all tokens for its own head columns; the o-projection needs this rank's tokens over all of K. mori.ops.a2a_gemm does the head-to-token exchange and the GEMM in one kernel, the reverse of gemm_a2a: * send/recv live in the window as [world][m_local][k_shard] slabs, so a destination's rows go out as one put per band and the GEMM reads A one K segment at a time from wherever it lives (row stride k_shard, a base per segment); the 8-wave main loop is gemm_ar's, unchanged. * block 0 posts every band back to back, each put with a trailing copy-engine atomic on the peer's signal counter (counters snapshotted once by "arm"), then computes its tile and only then quiets. * a tile computes its own segment first, then the other sources rank-rotated, waiting per segment (default) or once per tile for its band. * a barrier-only kernel closes every call and advances the epoch. ptpc with the per-token scale taken over all heads before quantization (as ATOM's fp8 head exchange does), so the scale never travels. MI355X, all cases validated at the fp8 floor, two operand sets each, plus back-to-back calls with a rank held: fused beats split SDMA by 8.5-31.6% and RCCL + the same GEMM by 15.5-37.3% on MiniMax-M3's o-projection at SP4 (K=N=6144), and by 4.1-14.0% / 12.2-21.2% on Llama-70B's at P=8 (K=N=8192), where the transfer slows under the GEMM for a reason not yet identified. Co-Authored-By: Claude <[email protected]>
The kernel gains a diagnostic timeline (`_timeline`, off by default, with a stamp region the config reserves only when asked): block 0 stamps when every (source, band) lands here, every tile its start and end; `_grid` runs block 0 alone for an idle baseline. timeline_a2a_gemm.py drives it. It overturns the README's reading of the 70B numbers. The copy engines are not slowed by the GEMM: every band lands within a few us of where it lands on an idle GPU, and the exchange is done ~90-130us into a 183us GEMM at m_local=4096. What costs is the first wave of tiles holding its CUs while bands 1-3 arrive (~50us per CU at c8), which a 1/8 own segment cannot fill. Co-Authored-By: Claude <[email protected]>
…ropped Computing every tile's own K segment in a first kernel (fp32 partials to scratch) before waiting for any band won one P=8 cell by 4.7% and lost the rest, by 40-70us at m_local=8192: the partials' round trip and the second pipeline fill cost more than the 1/8 of the GEMM it moves forward. Co-Authored-By: Claude <[email protected]>
jhchouuu
left a comment
There was a problem hiding this comment.
Tested at 237475fc, merged onto current main, on 8x MI355X (gfx950, ROCm 7.14, FlyDSL 0.4.0). The numbers reproduce and every validation passes. I found two ways the band wait goes wrong; both come from how the expected signal count is formed.
The perf reproduces. These are the best configs from your table, 3 interleaved rounds, --acquire none, all validated. Times in us.
| shape | m_local | gemm-only | split-rccl | split-sdma | fused | vs split-sdma | vs RCCL |
|---|---|---|---|---|---|---|---|
| M3, P=4 | 2048 | 61.8 | 144.6 | 132.1 | 121.0 segment c1 |
-8.4% | -16.3% |
| 4096 | 124.8 | 268.5 | 243.6 | 179.5 segment c2 |
-26.3% | -33.1% | |
| 8192 | 208.5 | 478.2 | 434.3 | 299.7 segment c4 |
-31.0% | -37.3% | |
| 70B, P=8 | 2048 | 97.7 | 158.1 | 148.7 | 136.6 segment c1 |
-8.2% | -13.6% |
| 4096 | 187.8 | 298.3 | 274.4 | 266.5 tile c2 |
-2.9% | -10.7% | |
| 8192 | 372.0 | 561.4 | 522.7 | 448.4 tile c8 |
-14.2% | -20.1% |
Tests and the default path.
-
test_a2a_gemm.pypasses (9 passed). -
That test only runs
--acquire none, so I also ran the defaultacquire="inv"with--back-to-back 8. It validated every call:- at P=4, for chunks 1/2/4 × segment/tile;
- at P=8, for chunks 1/2 × segment/tile.
It would be worth adding
invto the test, since it is whatcompile_a2a_gemmuses by default.
1. The band wait breaks when the signal counter crosses 2^32
_wait_band spins with gemm_ar's _spin_until, which is an unsigned cur < target on the low 32 bits. Its target is sig_base + (epoch - 1) * chunks + band + 1. That counter is the communicator's, it is never reset, and it advances by one per band, so it does cross 2^32. Once a waiter's target is 0xFFFFFFFF and the next band has already landed, the waiter reads 0 and spins forever. (_spin_until's "unsigned so a wrapped counter still terminates" holds for flags compared against epochs counted from 0, not for targets offset by an arbitrary base.)
I checked this by storing a value into each source's signal slot before arm, then running five validated fused calls with chunks=4 at P=4:
| counter preset | result |
|---|---|
0x00000010 |
all 5 calls OK |
0x7FFFFFF0 |
all 5 calls OK (so the compare is unsigned, as written) |
0xFFFFFFF0 |
calls 0-2 OK; call 3, whose bands cross 2^32, hangs |
0xFFFFFFFE |
the first call hangs |
As an example of how close this is in service: with 8 bands per call and one call per layer across 61 layers, a counter gains about 500 per forward pass, so it reaches 2^32 after about 9M forward passes: days to weeks of serving, not years.
A wrap-safe compare fixes it: (int32_t)(cur - target) < 0, or compare the full 64-bit slot the way sdma.wait_signal does.
2. The expected count assumes every call adds exactly this config's chunks
(epoch - 1) * chunks holds only if every call since arm posted the same number of bands on that (src, queue) slot. I armed once, then alternated configs on one window, with fresh operands every call:
| sequence | result |
|---|---|
c4, c2, c4, c2 |
call 0 OK; the c2 call reads before its bands land (relL2 1.23); the next call hangs |
c2, c4, c2, c4 |
call 0 OK; the c4 call hangs |
The tables' best configs already differ by M (c1/c2/c4/c8), and the README's next step is "a per-M chunk choice", so a wrapper would hit this on its first change of M. The same arithmetic also breaks if anything else on the communicator posts remote signals on the same slot: a second a2a_gemm, or any put(remote_signal=True) to this rank on that queue. Signal slots and SDMA queues belong to the communicator.
Two possible fixes:
- Re-snapshot
sig_baseat the start ofbarrier1, before it signals the peers. At that point every band of this call has landed, since the GEMM waited for all of them. No peer can post the next call's bands until it has seen this rank's arrival, which comes after the snapshot. The target then becomessig_base + band + 1, whateverchunksthe previous call used. - Document that the op must own its
(src, queue)signal slots. Otherwise, take a queue id that nothing else on the communicator uses.
Two ways the band wait went wrong, both in how the expected signal count was formed (found in review): * The target was compared with gemm_ar's _spin_until, an unsigned cur < target on the low 32 bits. The copy engines' signal counters belong to the communicator and are never reset, so once a target sits just below 2^32 and the counter has wrapped, the waiter spins forever. Waits now compare (int32)(cur - target) < 0 (_spin_reach), barrier flags included. * The target was sig_base + (epoch - 1) * chunks + band + 1, which holds only while every call since arm posted the same number of bands on that slot. A change of chunk count read a band early and then hung. barrier1 now re-takes the snapshot before it tells its peers the call is over -- every band of the call has landed, and no peer can post the next call's until it has seen that arrival -- and the target is sig_base + band + 1. The (src, queue) signal slot must carry nothing but this op's bands; the kernel docs and README say so. Tests: the every-mode GPU test runs acquire="inv" (the default) as well as "none", and a new worker presets the signal counters to 0xFFFFFFF0 before six c4 calls, and alternates c4/c2 calls on one arm, each call validated. Both new cases hang on the previous kernels and pass here (14 passed on 8x MI355X); perf is unchanged. Co-Authored-By: Claude <[email protected]>
|
Thanks -- both were real, and both are fixed in 49bdb4c.
Tests:
Perf is unchanged by the extra snapshot. On this node: M3 P=4 m=4096 segment c2 is 180.5us against 245.5us for split-sdma. 70B P=8 tile c8 is 263.9us at m=4096 and 447.4us at m=8192, against 276.2 and 522.6us for split-sdma. These match your table. |
jhchouuu
left a comment
There was a problem hiding this comment.
Re-tested at 49bdb4cb merged onto current main, on 8x MI355X (ROCm 7.14, FlyDSL 0.4.0). Both fixes hold.
-
Your new tests.
test_a2a_gemm.pypasses: 14 tests, now includingacquire="inv"and the new worker's wrap/mixed cases. -
My first-round repros now pass, all validated:
- signal counter preset to
0xFFFFFFF0: six c4 calls, the fourth crossing 2^32; - preset to
0xFFFFFFFE: three calls; - armed once, then
c4,c2,c4,c2,c4; - armed once, then
c2,c4,c2,c4,c2.
On
237475fcthese hung or returned relL2 1.23. - signal counter preset to
-
The re-snapshot in
barrier1looks right. Each lane takessig_base[src]before it stores its arrival intosrc. By then every band of the call has landed, through the GEMM's waits orbarrier0, andsrccannot post the next call's bands until it has seen that arrival. -
Perf is unchanged.
- M3 P=4, m=4096,
segment c2: 180.1 us, against 244.6 us for split-sdma. - 70B P=8, m=8192,
tile c8: 434.1 us, against 520.2 us for split-sdma.
Three rounds each, all validated.
- M3 P=4, m=4096,
Nothing further from me. This is ready from my side once it leaves draft.
# Conflicts: # python/mori/ops/__init__.py
jhchouuu
left a comment
There was a problem hiding this comment.
Re-tested at 9fa74b78, merged onto current main, on 8x MI355X. LGTM.
test_a2a_gemm.pypasses (14 tests). Both earlier repros validate on every call: the counter preset across 2^32, and alternating c4/c2.- I also ran
GemmA2AOp(#695) and a fuseda2a_gemmalternately on one communicator, as a Ulysses layer's QKV and o-projection would. Thea2a_gemmchunk count alternated 4/2, and every call of both ops landed at the fp8 floor at P=4 and P=8. test_gemm_a2a{,_op}.pystill passes with this merged (48 tests).- 70B P=8, m=8192,
tile c8: 435.2 us against 522.3 us for split-sdma, unchanged.
The timeline gains per-tile waves (start quantiles, end, duration per wave of --cus tiles), --block-m, and a nopost variant -- the fused kernel with neither posts nor waits -- which prices the kernel's structure against gemm-only: it costs nothing. The README records the P=8 result: fused time is the last band the first wave needs plus two tiles, i.e. the first wave's half of the bytes at the link's rate (~36 GB/s per link at eight bands). Uneven bands (-1.3% at best), BLOCK_M=128 (slower GEMM, slower tiles under the transfer) and two or four copy-engine queues per pair (slower in every cell) were measured and dropped. What is left is posting before the GEMM, from attention's epilogue. Co-Authored-By: Claude <[email protected]>
A public wrapper over the fused all-to-all + GEMM, shaped like GemmA2AOp: it owns and zeroes its window, refuses a communicator without SDMA queues or with ranks outside one LSA domain, arms the signal-counter snapshot once, and keeps a pinned plan (GEMM, phases, output) per M up to m_max. Bands per call and the wait mode default per M from the measured tables (default_chunks / default_wait) and can be overridden. send_buffer() exposes the exchange's source slab so a caller can quantize straight into it. self_test() checks that the copy engines actually move bytes. tests/python/cco/test_a2a_gemm_op.py runs it on 8 ranks: four M on one instance (1 and 4 bands, both wait modes), the in-place send_buffer path, graph replay, back-to-back calls with a different rank late each time, and the constructor's rejections, including SDMA off. A new ci_cco.yml step runs it together with test_a2a_gemm.py. Co-Authored-By: Claude <[email protected]>
Adds
mori.ops.a2a_gemm: the all-to-all fused into the fp8 GEMM that consumes it -- Ulysses' head-to-token exchange straight into the o-projection. It is the reverse ofgemm_a2a(#695), which fuses the QKV projection's GEMM into the exchange that follows it, and followsag_gemm(#809) for the poster and the closing barrier.After attention every rank holds all
world*m_localtokens for its ownK/worldhead columns; the o-projection needs this rank'sm_localtokens over all of K:How it works
sendandrecvlive in the window as[world][m_local][k_shard]slabs, so a destination's rows go out as one put per band, and the GEMM reads A one K segment at a time from wherever it lives (send[rank]for its own columns,recv[s]otherwise). Only the A addressing changes; the 8-wave main loop isgemm_ar's.build_phases()["arm"]), so seeing the count is seeing the band. Block 0 then computes its own tile and only then quiets.wait="segment", default) or once up front (wait="tile").Results
MI355X, fp8 ptpc, 256x256 tile, FlyDSL 0.4.0. Every case in one process, CUDA graph replayed back to back for >= 3 s (median launch gap, max over ranks), 3 interleaved rounds, spread <= 2%. Every case validated every row of C at the fp8 floor (relL2 1.66e-3) for two operand sets; the test adds back-to-back calls with a rank held.
segment c1segment c2segment c4segment c1tile c2tile c8The README (
python/mori/ops/a2a_gemm/README.md) has every configuration and the three things that cost more than the overlap saved on the way here: a full system fence after each wait, aquietper band, and block 0 draining before its own tile.Op and CI
A2aGemmOp(comm, n=, k=, m_max=, mode="fused"|"split"), shaped likeGemmA2AOp: owns and zeroes its window, refuses a communicator without SDMA or spanning more than one LSA domain, keeps a pinned plan per M, and picks bands per call and the wait mode per M from the measured tables (default_chunks/default_wait, overridable).send_buffer(m)exposes the exchange's source slab so attention can quantize straight into it;self_test()checks the copy engines move bytes. Through the op, 70B at P=8: 136.6 / 262.0 / 430.9 us fused atm_local2048 / 4096 / 8192, within 1.3% of the best fused cells.tests/python/cco/test_a2a_gemm_op.py(8 ranks): four M on one instance (1 and 4 bands, both wait modes), the in-placesend_bufferpath, graph replay, back-to-back calls with a different rank 20 ms late each time, and the constructor's rejections including SDMA off. A newci_cco.ymlstep runs it withtest_a2a_gemm.py. Both pass on MI355X (7 + 14), every check at relL2 1.66e-3.Known limits
gemm-onlyby ~70-80us. The kernel itself costs nothing (anopostcontrol matchesgemm-only) and the copy engines are not slowed by the GEMM; the gap is the first wave's half of the bytes at the link's rate -- fused time is close to the last band the first wave needs plus two tiles (66 + 2 x 87 = 240 against 252 us atm_local=4096). Uneven bands,BLOCK_M=128and two or four copy-engine queues per pair were measured and do not close it (README, "What bounds it"); what is left is posting before the GEMM, from attention's epilogue.acquire="none", used by the tables, relies on cco's default uncached window; the kernel default isacquire="inv".🤖 Generated with Claude Code