Skip to content

a2a_gemm: all-to-all fused into the fp8 GEMM that consumes it - #868

Merged
yangyuhuiling merged 7 commits into
mainfrom
xiangch/a2a-gemm
Nov 3, 2026
Merged

yangyuhuiling merged 7 commits into
mainfrom
xiangch/a2a-gemm

Conversation

@yangyuhuiling

@yangyuhuiling yangyuhuiling commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

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 of gemm_a2a (#695), which fuses the QKV projection's GEMM into the exchange that follows it, and follows ag_gemm (#809) for the poster and the closing barrier.

After attention every rank holds all world*m_local tokens for its own K/world head columns; the o-projection needs this rank's m_local tokens over all of K:

C_d = concat_s(A_s[d*m_local:(d+1)*m_local]) @ B.T     # [m_local, N], concat along K

How it works

  • send and 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 (send[rank] for its own columns, recv[s] otherwise). Only the A addressing changes; the 8-wave main loop is gemm_ar's.
  • Block 0 posts every band to every peer back to back. Each put carries a trailing copy-engine atomic on the peer's signal counter for this rank (counters snapshotted once by build_phases()["arm"]), so seeing the count is seeing the band. Block 0 then computes its own tile and only then quiets.
  • A tile computes its own K segment first, then the other sources rank-rotated, waiting for a remote band per segment (wait="segment", default) or once up front (wait="tile").
  • 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 in feat(sp): add Ulysses sequence parallelism and optimize M3 prefill communication ATOM#2364), so the scale never travels.

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.

shape m_local gemm-only split-rccl split-sdma best fused vs split-sdma vs RCCL
MiniMax-M3, P=4, K=N=6144 2048 63.0 144.0 132.9 121.7 segment c1 -8.5% -15.5%
4096 125.1 264.9 244.0 184.1 segment c2 -24.5% -30.5%
8192 208.6 479.0 438.7 300.2 segment c4 -31.6% -37.3%
Llama-70B, P=8, K=N=8192 2048 94.3 156.2 145.8 134.4 segment c1 -7.8% -14.0%
4096 182.6 294.6 269.6 258.6 tile c2 -4.1% -12.2%
8192 356.4 552.5 506.4 435.2 tile c8 -14.0% -21.2%

The 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, a quiet per band, and block 0 draining before its own tile.

Op and CI

  • A2aGemmOp(comm, n=, k=, m_max=, mode="fused"|"split"), shaped like GemmA2AOp: 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 at m_local 2048 / 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-place send_buffer path, graph replay, back-to-back calls with a different rank 20 ms late each time, and the constructor's rejections including SDMA off. A new ci_cco.yml step runs it with test_a2a_gemm.py. Both pass on MI355X (7 + 14), every check at relL2 1.66e-3.

Known limits

  • At P=8 fused trails gemm-only by ~70-80us. The kernel itself costs nothing (a nopost control matches gemm-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 at m_local=4096). Uneven bands, BLOCK_M=128 and 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 is acquire="inv".

🤖 Generated with Claude Code

xiangch and others added 3 commits October 28, 2026 15:57
…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 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.

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.py passes (9 passed).

  • That test only runs --acquire none, so I also ran the default acquire="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 inv to the test, since it is what compile_a2a_gemm uses 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_base at the start of barrier1, 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 becomes sig_base + band + 1, whatever chunks the 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]>
@yangyuhuiling

Copy link
Copy Markdown
Contributor Author

Thanks -- both were real, and both are fixed in 49bdb4c.

  1. Wrap at 2^32. Band waits (and the barrier flags) now spin on (int32)(cur - target) < 0 instead of gemm_ar's unsigned cur < target.
  2. Chunk count changing between calls. Took your first suggestion: barrier1 re-takes the sig_base snapshot for each source before it signals that peer. At that point every band of the call has landed, and no peer can post the next call's bands until it has seen this rank's arrival. The target is now sig_base + band + 1, so a call's chunk count is its own, and the epoch is gone from it. The kernel docs and the README now say the (src, queue) signal slot must carry nothing but this op's bands, so a second user of the communicator needs its own queue.

Tests:

  • The every-mode test now runs acquire="inv" as well as "none".
  • A new worker (tests/python/cco/a2a_gemm_worker.py) reproduces both of your cases, with fresh operands and a validated result on every call. wrap presets each source's signal counter to 0xFFFFFFF0 before six c4 calls, so the fourth call crosses 2^32. mixed arms once and then alternates c4 and c2. Both run with segment and tile waits.
  • As a negative control, I swapped the previous kernels.py back in: both new cases hang there (killed at the 240 s timeout). With the fix, test_a2a_gemm.py gives 14 passed on 8x MI355X.

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 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 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.py passes: 14 tests, now including acquire="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 237475fc these hung or returned relL2 1.23.

  • The re-snapshot in barrier1 looks right. Each lane takes sig_base[src] before it stores its arrival into src. By then every band of the call has landed, through the GEMM's waits or barrier0, and src cannot 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.

Nothing further from me. This is ready from my side once it leaves draft.

# Conflicts:
#	python/mori/ops/__init__.py
@yangyuhuiling
yangyuhuiling marked this pull request as ready for review November 2, 2026 03:50

@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 at 9fa74b78, merged onto current main, on 8x MI355X. LGTM.

  • test_a2a_gemm.py passes (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 fused a2a_gemm alternately on one communicator, as a Ulysses layer's QKV and o-projection would. The a2a_gemm chunk 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}.py still passes with this merged (48 tests).
  • 70B P=8, m=8192, tile c8: 435.2 us against 522.3 us for split-sdma, unchanged.

xiangch and others added 2 commits November 2, 2026 08:36
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]>
@yangyuhuiling
yangyuhuiling merged commit bb5ed34 into main Nov 3, 2026
14 checks passed
@QizhouZhang97
QizhouZhang97 deleted the xiangch/a2a-gemm 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