Skip to content

Name the cluster-scope arrival, and settle who charges the stage's bytes (#50) - #71

Merged
feitreim merged 2 commits into
mainfrom
cluster-scope-semaphore-arrival
Jul 28, 2026
Merged

feitreim merged 2 commits into
mainfrom
cluster-scope-semaphore-arrival

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #50.

gemm.rs no longer says "cluster-scope arrival" by issuing a multicast load
with a degenerate mask. It says it with Semaphore::at_rank and
SharedTile::tma_load_2d_arriving_at, and still runs exact on a B200.

if rank == LEADER {
    load.sem(k).expect_tx(RANKS * (ATile::BYTES + BTile::BYTES) as u32);
}
let stage = load.sem(k).at_rank(LEADER);
a_ring.tile(k).tma_load_2d_arriving_at(a_map, column, a_row, stage);
b_ring.tile(k).tma_load_2d_arriving_at(b_map, column, b_row, stage);

The design question, and the answer

#50 is right that this is a decision and not a transcription, so here is the
argument. It is also in src/sync.rs's module docs, which is where a future
cluster kernel will actually look.

Half of it is not a decision at all — it is the ISA, and the API should say
so rather than paper over it.
mbarrier.arrive.expect_tx takes
.shared::cta only; mbarrier.arrive … .shared::cluster takes a remote
address and carries no transaction count. Remote addressing and byte
accounting live in different instructions and do not compose. So a CTA may
charge exactly one barrier — its own — and may arrive at any rank's.

That is why at_rank returns a new type rather than another Semaphore:

pub struct ClusterSemaphore { /* … */ }   // arrive(), raw()

ClusterSemaphore has no wait and no expect_tx because there is no
instruction for either. A Semaphore that happened to hold a remote address
would type-check into two hangs the compiler could have caught. The type is
the statement of the constraint.

The half that is a decision is which CTA's barrier a cluster stage
completes on. Two shapes:

  1. The waiter charges the whole stage. One barrier, in the CTA that waits
    on it. Every rank's TMA completes there, named through at_rank, and that
    CTA charges every byte the stage brings in — including bytes it does not
    issue.
  2. Every rank charges its own, then forwards a ClusterSemaphore::arrive
    to the waiter when its own barrier flips.

This takes (1). What (2) has going for it is that the sum stays local — a
CTA charges exactly what it issued — and that is the one place the argument is
close, because locality of the sum is precisely what #29 wants. It is not
close enough: (2) costs a barrier per rank per stage, a thread per rank
spinning on it, and a second hop on the critical path that carries no
information, since a producer whose own barrier has flipped knows nothing the
TMA engine could not have told the waiter directly.

And the locality is recoverable under (1) anyway, which is what settles it. A
cluster stage is symmetric by construction — every rank stages the same tile
types at the same shared offsets, which is exactly what lets one MMA
descriptor read across the pair — so the whole-stage charge is the local
charge times the rank count, and the rank count is a launch constant. The
GEMM now writes that literally: RANKS * (ATile::BYTES + BTile::BYTES).

On #29, which lands on top of this

Nothing here makes #29 harder, and the RANKS * form is the interface between
them. A charge derived from the loads a producer issued is still derivable
under (1); it is derived once and multiplied. Whichever shape #29 takes —
expect_tiles(&[…]) or a tma_load that returns its own charge — the cluster
case is that same total scaled by the ranks staging into the barrier, not a
new mechanism. Had this gone with (2), #29 would have had to grow a whole
second signalling path to go with its accounting.

One more thing #29 should not have to rediscover: the charge and the
completions commute.
An mbarrier's transaction count is a signed
accumulator, so the peer's loads may land before the expect_tx that expects
them — only the totals have to agree. No sync sits between them in the GEMM
and none is owed.

Addressing: mapa, not arithmetic on the rank bit

Semaphore::multicast_alias is gone. It masked the CTA rank bit and the
sub-word bits out of a local address (& 0xFEFF_FFF8) — correct, and correct
only for "rank 0 of a 2-CTA cluster", since a 4-CTA cluster's rank field is
two bits wide. at_rank(rank) lowers to mapa.shared::cluster, which is the
documented primitive, takes any rank, and stops the DSMEM address encoding
being a thing kernel authors hand-derive. That is also what #49 will want when
it generalizes the geometry.

The load

tma_load_2d_arriving_at owns the own-bit mask that used to sit open-coded in
gemm.rs, with the reasoning documented once in the library: nothing is
replicated, and what the multicast form supplies that the plain one cannot is
the .shared::cluster barrier operand. tma_load_2d_multicast_cg2 keeps its
mask and is now the replication form alone — the #49 story — retyped to take a
ClusterSemaphore since its barrier operand is cluster-addressed either way.

Verification

All run against this branch.

  • modal run modal_app.py::build — passes. Real cargo oxide build --arch sm_100a.
  • modal run modal_app.py::examples — pass gemm 512x256x256 exact on a
    B200, plus pass softmax. This is the gate that matters: the failure mode
    under change here is a deadlock or a wrong GEMM.
  • modal run modal_app.py::device_tests — 29 of 29 cases passed on a B200.
  • cargo fmt, cargo clippy --all-targets -- -D warnings, cargo test (74
    passed) locally.

ClusterSemaphore::arrive is the one thing here no kernel in the repo calls
yet — it is the ISA's own remote-arrival form and the other half of what #24
item 2 asked for (a CTA signalling its peer's progress), and #51's persistent
cluster pipeline is what will want it. Stating it plainly rather than implying
the GEMM exercises it.

GAPS.md still names multicast_alias; left alone to avoid colliding with
concurrent edits there. examples/README.md §6 is updated to closed, matching
how §5 records #46.

feitreim added 2 commits July 28, 2026 10:58
)

`Semaphore::at_rank(rank)` is the leader's copy of a stage barrier, mapped
with `mapa` rather than by masking the rank bit out of a local address, and
it returns a `ClusterSemaphore` — a barrier you may arrive at, or hand to an
engine, and may not wait on or charge. That restriction is the ISA's:
`mbarrier.arrive.expect_tx` is `.shared::cta` only and
`mbarrier.arrive ... .shared::cluster` carries no transaction count, so a CTA
can charge exactly one barrier, its own.

Which leaves one decision, argued in `sync.rs`'s module docs: the waiter
charges the whole stage. The alternative — every rank charges its own barrier
and forwards an arrival — keeps the sum local, which is what #29 wants, and
costs a barrier per rank per stage plus a thread spinning to forward a signal
the TMA engine could have delivered directly. The locality is recoverable
anyway: a cluster stage is symmetric, so the whole-stage charge is the local
charge times the rank count.

`SharedTile::tma_load_2d_arriving_at` is the load that completes on another
CTA's barrier, with the degenerate mask now an implementation detail
documented once instead of open-coded per kernel. `tma_load_2d_multicast_cg2`
keeps the mask and is now the replication form only (#49).
@feitreim
feitreim merged commit a9d8fbf into main Jul 28, 2026
2 checks passed
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.

Semaphore cluster-scope arrival: the GEMM deadlocks without a workaround

1 participant