Skip to content

A work item belongs to a cluster, and the GEMM that proved it got slower (#51) - #80

Merged
feitreim merged 3 commits into
mainfrom
persistent-cluster-pipeline
Jul 28, 2026
Merged

feitreim merged 3 commits into
mainfrom
persistent-cluster-pipeline

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes the scaffold half of #51. The GEMM does not adopt it, and that is the finding.

pipeline::run can schedule a cluster now. The GEMM was ported to it, ran
exact on a B200, and was slower than the kernel it replaced at every size
where the persistent grid's cap binds
. #51 says in terms that this is a
finding and not a success, so the port is in this PR's history (08e5e08) and
not in the tree, and the scaffold lands on its own.

What #51's acceptance clause asks, and where this stands

examples/src/gemm.rs uses pipeline::run, and still runs exact. Re-measure
with bench — a persistent GEMM that is slower than the current one is a
finding, not a success.

  • "uses pipeline::run" — deliberately NOT met in the shipped tree. It was
    met, measured, and reverted. examples/src/gemm.rs here is byte-identical to
    origin/main.
  • "still runs exact" — met, on the ported kernel, post-rebase. Details below.
  • "re-measure" — done, three sweeps, table below. The result is a
    regression, reported as a finding.

I am not softening this to make it mergeable: the acceptance clause as literally
written is unmet, by choice, and #51 names that as a legitimate outcome only if
argued with numbers. The numbers are below and the one number I could not get is
named as missing.

The scaffold

The map was blockIdx.x strided by gridDim.x, which hands the two CTAs of a
cluster different items — exactly wrong for a cooperative MMA. It is now
%clusterid strided by %nclusterid.

That is a strict generalization, not a second scheduler. A launch that
declares no cluster runs clusters of one CTA, where %clusterid is %ctaid
and %nclusterid is %nctaid, so a block-scope job provably gets back the
loop it had rather than a parallel code path. There is one item map, and
blockIdx.x was it written one level too low.

Job::RANKS (default 1) says whether the job's barriers are shared across the
pair, and so whether the item boundary must be barrier.cluster rather than
bar.sync. That is what per-item barrier re-initialization needs across a
cluster: otherwise a leader re-arms a barrier its peer is still filling, or a
peer fills one its leader has not re-armed, and both are silent — the phase
simply never completes. The barrier count is unchanged: the loop has the two
it always had, just taken at a wider scope, and at RANKS == 1 the branch folds
away at compile time.

On #3, which #51 asked me to check before sequencing

The two halves separate, and the rank half never needed #3. #6 found that #3
as filed could not do its job because a block-scope operation needs storage a
{ WARPS, rank(), sync() } trait cannot supply. That finding does not transfer:
cluster::block_rank() is a %cluster_ctarank read with no storage, no
collective and no barrier in it. It is already the primitive, the GEMM already
calls it, and wrapping a special register in a trait would add a name without
adding a fact. No rank API was added.

ClusterSemaphore::arrive is still called by nothing

#50's PR flagged it as the primitive "#51's persistent cluster pipeline is what
will want it". It turned out not to. An item boundary is a rendezvous — every
rank arriving at every rank — and building one from mbarriers hand-rolls
barrier.cluster.arrive/wait at a barrier and a spin per rank. sync.rs now
records that, and what is left for the primitive: a directed signal from one
named rank to another while the rest of the cluster keeps going. An honest
"unused" beats a forced call site.

The measurement

Min ms over 30 timed launches, B200, 148 SMs, modal run modal_app.py::bench,
all three sweeps post-rebase. The cap is in clusters; a cluster is 2 CTAs.
Rows marked = have fewer tiles than the cap, so the grid is byte-identical
to the launch-per-tile grid and each cluster runs exactly one item — a free
control on the scaffold itself.

shape launch per tile cap 74 cap 148
256x128x256 0.0224 = 0.0223 = 0.0234
512x256x256 0.0225 = 0.0225 = 0.0251
1024x1024x1024 0.0283 = 0.0268 = 0.0284
2048x2048x2048 0.0451 0.0593 = 0.0478
4096x4096x4096 0.1891 0.2808 0.2059
8192x8192x8192 1.0169 2.1036 1.2130

TFLOP/s at 8192³: 1081 launch-per-tile, 523 at cap 74, 906 at cap 148.

The controls earn two things. The scaffold costs nothing — at an identical
grid and one item per cluster every control row is within noise. And they carry
the run-to-run offset, which is not negligible: the cap-148 run's controls sit
0.4%–11.6% above baseline, ~5% on average, so its 8192³ figure is nearer 13%
slow than 19%. I am not rounding the regression off in either direction.

The reading

lcf at one item per cluster is the launch-per-tile kernel — same rings,
same barriers, same prologue and epilogue, drained at the same points. So the
only thing a persistent grid changes here is how many clusters exist, and the
controls confirm nothing else moved. The regression is entirely the cap, and the
cap is a residency question.

Which the repo cannot currently answer for this kernel:
cuOccupancyMaxActiveBlocksPerMultiprocessor takes a block shape and no
cluster, which is why main.rs prints cluster in the GEMM's occupancy row.

The one measurement that is owed

Cap 222 — 3 CTAs/SM, the value 228 KiB / 72 KiB of shared memory admits and
the value the throughput curve points at — was never run.
The Modal workspace
hit its spend limit mid-investigation. It is the cap most likely to close the
gap, and possibly to beat the launch-per-tile grid outright, so the conclusion
"the GEMM should not adopt this" is provisional on that one point. Shipping a
cap on an extrapolation is exactly the mistake this table is a record of, which
is why the scaffold lands alone rather than with a guessed constant.

Evidence for #78, since it was asked for explicitly

Extrapolating #77's 1 CTA/SM to the GEMM is false, by at least a factor of
two.
Capping the grid at one CTA per SM makes 8192³ take 2.1036 ms against the
uncapped 1.0169. That cannot happen if the device could only ever hold that many
CTAs — the two schedules would then be the same schedule. Doubling the cap
recovers most of it (973 → 1688 tiles/ms, sublinear and still climbing), so
residency is ≥ 2 CTAs/SM and plausibly the 3 shared memory admits.

So #78's step one has a partial answer, obtained on a clock because the query
cannot see it. I have not investigated further, per instruction.

Verification — which gates, on which tree

The rebase onto bd9e28f happened partway through, when I found #77 and
realized my wave constant was wrong. Everything below is stated relative to it.

Post-rebase, on a B200:

  • modal run modal_app.py::examples — pass gemm 512x256x256 exact (4 tiles over 4 clusters), 4096x4096x256 exact (512 tiles over 74 clusters). The
    74 in that line is the post-rebase constant, which is how you can tell which
    tree it ran on. The second size exists because the first is 4 tiles over a
    grid holding far more, so it never enters the loop twice and would pass
    identically against the kernel being replaced — a check that cannot fail
    against the code you replaced is not a check.
  • Two ::bench sweeps (caps 74 and 148). bench.rs checks every size against
    the CPU reference before timing it, so all six sizes up to 8192³ are
    exactness runs as well. The scaffold is correct on silicon; it is only slow.

Pre-rebase: modal run modal_app.py::build (real cargo oxide build --arch sm_100a of both crates, plus cargo doc --features host -D warnings and
clippy), and one ::examples run. Superseded by the post-rebase runs above.

Locally, on the exact final tree: cargo fmt, cargo clippy --all-targets -- -D warnings, cargo test (74 + 1 passed), RUSTDOCFLAGS=-D warnings cargo doc --no-deps. All green; none of them needs a toolkit.

The gap, plainly: ::build was not re-run on the final tree, because the
spend limit blocks all Modal runs including the CPU-only ones. Expect a red CI
build for that reason. What the final tree differs by is bounded and worth
stating rather than hand-waving: examples/ is byte-identical to
origin/main
, so the examples half of ::build is what main already passes;
the library differs only in pipeline.rs (a generic with no caller, which
monomorphizes to no PTX) and doc comments in sync.rs. The --features host
rustdoc run is the only check not reproduced locally, and every new intra-doc
link resolves under the device-only -D warnings run.

What this leaves

run had no caller before #51 and has none after it, but the reason changed: it
used to be that the cluster kernel could not use the scaffold, and it is now
that the one that can does not want to. pipeline.rs's module docs say so
directly rather than leaving it to be discovered.

#15's lcsf is what would make persistence pay for something rather than
break even. Under lcf the epilogue is inside the item, so store_rows and the
next tile's first K loads are separated by a boundary that drains the pipeline —
a persistent GEMM buys a saved launch and nothing else. tma_store_wait_read
(#9, landed) is the wait that would let them cross.

feitreim added 3 commits July 28, 2026 12:36
`pipeline::run`'s map was `blockIdx.x` strided by `gridDim.x`, which hands the
two CTAs of a cluster different items — exactly wrong for a cooperative MMA,
whose pair has to be on one output tile. It is now `%clusterid` strided by
`%nclusterid`.

That is a strict generalization rather than a second scheduler: a launch that
declares no cluster runs clusters of one CTA, where `%clusterid` *is* `%ctaid`
and `%nclusterid` *is* `%nctaid`, so a block-scope job gets back the exact loop
it had.

`Job::RANKS` (default 1) says whether the job's barriers are shared across the
pair, and so whether the per-item boundary has to be `barrier.cluster` rather
than `bar.sync`. The barrier *count* is unchanged — the loop has the two it
always had — and at `RANKS == 1` the branch folds away at compile time.

The rank half of #51 needed no API. `cluster::block_rank()` reads
`%cluster_ctarank` with no storage, no collective and no barrier in it, which is
precisely what #6 found `Scope` could not supply for the block-reduction half.
The two halves separate; wrapping a special register in a trait would add a name
without adding a fact.

`sync.rs` records that `ClusterSemaphore::arrive` is still called by nothing,
and that #51 turned out not to want it: an item boundary is a rendezvous, and
building one out of mbarriers hand-rolls `barrier.cluster` at a barrier and a
spin per rank.
…ect (#51)

The GEMM as a `Tile: Job` driven by `pipeline::run`: one output tile per work
item, one item per cluster, `RANKS = 2`, TMEM allocated once outside the loop
because `alloc_cluster` is a whole-cluster collective whose address never
changes. `check` gains a second size — 4096x4096x256, 512 tiles — because the
existing 512x256x256 is four tiles over a grid that holds far more, so it never
enters the loop twice and would pass identically against the kernel this
replaces. A check that cannot fail against the code you replaced is not a check.

Verified on a B200: `pass gemm 512x256x256 exact, 4096x4096x256 exact`, and
`::bench` checks all six sweep sizes against the CPU reference before timing
them, up to 8192³. The scaffold is correct.

It is also slower, which is why the next commit takes it back out. This one
exists so that `git show` has the kernel the numbers in `examples/README.md` §7
were taken on, rather than a description of it.
#51)

Reverts the previous commit's adoption. #51 says in terms that a persistent
GEMM slower than the current one is a finding and not a success, and it is
slower: 2.07× at 8192³ with a one-wave cap, ~13% with a two-wave one after
normalizing by that run's own control rows.

The control rows are what make this a finding rather than a shrug. At sizes
with fewer tiles than the cap the grid is byte-identical and each cluster runs
one item, and there the persistent kernel is within noise of the one it
replaced. `lcf` at one item per cluster *is* the launch-per-tile kernel, so the
only thing a persistent grid changes here is how many clusters exist — and
choosing that number is a residency question that
`cuOccupancyMaxActiveBlocksPerMultiprocessor` cannot answer for a
`#[cluster_launch]` kernel.

The value the throughput curve points at, 222 clusters, is unmeasured: the
Modal workspace hit its spend limit. Shipping a cap on an extrapolation is the
mistake §7 is a record of, so the scaffold lands alone.

Bears on #78: capping at one CTA per SM costs 2.07×, which cannot happen if the
device could only hold that many CTAs. Extrapolating #77's 1 CTA/SM to this
kernel is false by at least a factor of two.
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.

1 participant