Skip to content

Ask the hardware for the schedule, and find out the ragged wave was never the cost (#88) - #97

Merged
feitreim merged 3 commits into
mainfrom
clc-work-stealing
Jul 28, 2026
Merged

feitreim merged 3 commits into
mainfrom
clc-work-stealing

Conversation

@feitreim

@feitreim feitreim commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #88.

pipeline::run_stealing puts the GEMM on Blackwell's hardware work-stealing —
launch one cluster per output tile, and a cluster that finishes cancels one the
scheduler has not launched yet. It sits behind the same Job, so Tile is
byte-identical under both schedulers.

It does not pay for itself, and the reason is more interesting than the
feature.
Two findings about cuda_device::clc are worth more than the
scheduler; they are near the bottom and I would read them first.

The number, against the prediction stated before it was built

Min ms over 30 timed launches, B200. Both schedulers run one shared plan —
the static path carries the 24 unused bytes of the work queue too — so this
compares schedules and not residencies. Without that control, CLC changing
occupancy would masquerade as CLC changing scheduling.

shape tiles waves wave eff predicted static clc measured
2048³ 128 1 57.7% 42.3% 0.0361 0.0362 −0.3%
4096³ 512 3 76.9% 23.1% 0.1517 0.1497 +1.3%
8192³ 2048 10 92.3% 7.7% 0.9075 0.8974 +1.1%
16384³ 8192 37 99.7% 0.3% 6.9275 6.7643 +2.4%

predicted is 1 - tiles/(waves × 222) and is not fitted to anything. #88 named
23% / 8% / ~0 and that is what the column says.

The predicted gain does not arrive. Where the model says 23% the measurement
says 1.3%; where it says 7.7% it says 1.1%.

The wave-efficiency arithmetic is not wrong, and it is worth being precise
about what it does say. At 4096³ the ragged last wave really is 23% of the grid
idling
— 512 tiles over three waves of 222 leaves 154 clusters with nothing to
do. It is simply not 23% of the time, because the clusters that idle are not
the ones on the critical path: the launch ends when the last busy cluster
finishes its tile, and an idle neighbour does not make that cluster faster. The
model is right about a term, and the term is small.
#90 and #94 already
measured the one that is not — the item boundary, at 18–30 µs per output tile
and 20–36% of the launch — and a dynamic schedule removes no item boundary at
all. It only changes which cluster pays them.

16384³'s ~0 is the one thing the model gets right, and it gets it right for the
wrong reason. I am not claiming the +2.4% there: it is the least reproducible row
in this repo's tables by #90's and #94's own statements, and 1–2.4% sits at the
edge of what a 0.6% session spread can resolve.

The prefetch was a regression, and #88 predicted the opposite

#88 says "a steal on the critical path would be a regression" and asks which was
measured. Both were, as two entry points differing in one thing.

The critical-path steal was faster at all four sizes. At 16384³ it is the
6.7643 in the table above, against the prefetched form's 7.3399 — 8.5%, far
outside the 0.6% these numbers repeat to.

The prefetched form has been deleted. run_stealing has no const parameter
and the GEMM has one CLC entry point; the finding survives in pipeline.rs's
module doc and examples/README.md §7, which is where it belongs.

So gemm_cg2_clc is the serial form — the one that issues its try_cancel
after the item it will replace. The name no longer distinguishes it from a
prefetched sibling because that sibling was built, measured and deleted, not
because it was never written. If you are reading this file wondering whether the
prefetched schedule was ever tried: it was, it is the 7.3399 ms above, and it
lost. The plausible
mechanism: prefetching makes every cluster claim its next tile before finishing
its current one, so the order tiles are handed out in stops tracking the order
clusters actually become free — the ragged tail this mechanism exists to fix,
reintroduced by the optimization meant to hide its latency. One measurement,
stated as a hypothesis.
What would test it is recording per-cluster item counts
and comparing their spread.

Should the static run stay?

Yes, and not as redundancy — the two buy different things. The static stride
is the fastest known schedule for this kernel and has a year of measurements
behind it. CLC's case is portability: the grid becomes the problem's own tile
count, and SMS and CTAS_PER_SM leave the path entirely. #84 showed
CTAS_PER_SM is measured and underivable, so a persistent grid picked from it is
right for a B200 and cannot be known to be right for anything else.

Since CLC is not faster, adopting it has to be argued on portability alone — and
the evidence that would settle it is a second device shape, which is exactly what
this repo cannot currently run. Saying so beats asserting it either way. So
gemm::bench and check still default to the static schedule, deliberately:
this PR's ::bench row stays comparable to every earlier one.

The mechanism, checked directly rather than through a GEMM

device-tests gains clc work stealing, which asks the instruction the
three questions the scheduler assumes of it. Over a 4096-cluster grid:

2222 clusters ran, 1874 of them stole a cluster-aligned item, 1874 were cancelled and never launched, both ranks agreed everywhere, no launched rank stalled

  • 2222 + 1874 = 4096 exactly — every cluster either ran or was cancelled by
    one that did, each once. That is the property a dropped or doubled tile breaks.
  • Both ranks of every cluster were told the same thing, so
    .multicast::cluster::all completes the transaction in every CTA of the
    requesting cluster and not only the issuer. run_stealing has each rank wait
    on its own copy, which is only sound because of this.
  • Every stolen coordinate was cluster-aligned, so the cancelled unit is a
    cluster and item = ctaid_x / cluster_size is the right map.

Each rank waits on a deadline rather than on the barrier, so the failure the
case exists to catch reports itself instead of hanging. That was not
hypothetical: the first version of this probe reported 3758 of 8192 CTAs as
stalled, and the bug was in the probe — a cancelled cluster is never launched, so
its row keeps the zeros the buffer was allocated with, which is byte-for-byte
what a stalled rank writes. A launched sentinel written at entry separates
them. The probe is built on the raw cuda_device::clc intrinsics, not on
ClcQueue, so a failure cannot be ambiguous between the instruction and my
wrapper.

Two findings about cuda_device::clc

1. The module's usage example has is_canceled exactly backwards

crates/cuda-device/src/clc.rs, module doc:

let canceled = unsafe { clc_query_is_canceled(lo, hi) };
if canceled == 0 {
    // Work available — get tile coordinates

Eighty lines below, the function's own doc says the opposite:

/// Returns 1 if a CTA was canceled (work IS available — decode coordinates),
/// 0 if no CTA was canceled (no more work — exit the persistent loop).

crates/mir-lower/src/convert/intrinsics/clc.rs settles it:

"clusterlaunchcontrol.query_cancel.is_canceled.pred.b128 %p, %resp; ",
"selp.b32 $0, 1, 0, %p; ",

The return is 1 exactly when the is_canceled predicate is true, and that
predicate is true when the request succeeded. The function doc is right and
the module example is wrong.

This PR uses != 0 as work-available. The evidence that this is the right
way round is not that I read the assembly — it is that ::examples passes and
the probe reports 1874 cancellations carrying coordinates. Inverted, a successful
steal reads as "no work left", the cancelled cluster is never launched, and its
tile is computed by nobody: a wrong C, silently, at every size large enough to
steal. Anyone following the module example gets that, and the failure looks like
a scheduling bug rather than a doc bug.

2. CLC is inline PTX — an LLVM backend gap upstream has already absorbed

crates/mir-lower/src/convert/intrinsics/clc.rs:

Both LLVM 21 and LLVM 22 define CLC intrinsics in IntrinsicsNVVM.td but the
NVPTX backend does NOT lower them to PTX. The backend emits unresolved
call.uni stubs that ptxas rejects. Therefore, we use inline PTX assembly
for all CLC operations.

Against #42's frame, which is the right one to read this through:

CLC is a third thing, and the least demanding. The family is not absent — all
six functions are present and all six lower. The defect is one level below
cuda-oxide, in LLVM's NVPTX backend, and upstream has already routed around it in
its own lowering. So the consequence here is nothing owed and no ptx_asm! in
this repo
: pipeline.rs calls the ordinary cuda_device::clc functions and
never spells a clusterlaunchcontrol instruction. Recorded so the next reader
who notices inline assembly in the lowering does not mistake it for a hazard we
carry, or file #42's argument against it.

Both are candidates to report upstream. Nothing has been filed by me.

Design notes

The item boundary count is unchanged. #90 priced that boundary at a fifth to
a third of the launch, so this needed auditing rather than assuming: run takes
two per item (after init, after work); run_stealing also takes exactly two
(after init, after harvest). The stealing path adds one extra
cluster_sync for the whole kernel — arming the response barrier before any rank
can complete transactions on it — not one per item. Against the ten items a
cluster walks at 8192³, that is one boundary against twenty.

What orders a read of the response against the next request. Every thread
reads the response, so the buffer is read by many threads and overwritten by one.
The harvest is therefore placed before the item boundary that already exists:
that boundary both retires the item and says no thread is still reading the
response the next request will land on. It costs no extra barrier.

On the last item — where the steal fails and there is no next request — the
ordering constraint simply has no partner to violate. The trailing boundary still
runs, every rank leaves the loop together (the multicast guarantees they agree),
and disarm invalidates the barrier with nothing outstanding. Nothing can
overwrite a buffer anyone is still reading because nothing writes again at all.

run_stealing requires Job::RANKS to equal the launch's cluster size,
which run does not. It is documented in # Safety rather than worked around:
the response is multicast to the cluster and read by every thread of it, so the
boundary separating those reads from the next request has to cover the same set.
A RANKS == 1 job under a multi-CTA cluster launch would take a bar.sync there
and race.

Verification

  • modal run modal_app.py::examples — every scheduler exact at both sizes,
    including 4096x4096x256 at 512 clusters under CLC, where 512 clusters
    against ~222 residency means stealing genuinely happens.
  • modal run modal_app.py::device_tests — 32/32, including the new probe.
  • modal run modal_app.py::clc_bench — the table above.
  • modal run modal_app.py::bench — the static control against Two fp32 a store where the layout pairs them, and the boundary intercept says so (#91) #94 on main:
    4096³ 0.1528 vs 0.1534, 8192³ 0.8991 vs 0.8987, 16384³ 6.8301 vs 6.8098. All
    inside the 0.6% spread Two fp32 a store where the layout pairs them, and the boundary intercept says so (#91) #94 documents, blocks still 444. The +24 B of shared
    plan cost nothing and residency is unchanged
    , so the A/B is valid.
  • modal run modal_app.py::build — green, real sm_100a codegen of both GEMM
    entry points and the probe.
  • Locally: cargo clippy --all-targets -- -D warnings, cargo test (78 + 1), and
    cargo fmt across all three manifests — the root one does not reach
    device-tests, which is what made device-surface red on the first push.

::build and ::device_tests were re-run after the prefetch was deleted.
::clc_bench and ::bench were not, and did not need to be: in the
PREFETCH = false monomorphization the two prefetch blocks were already dead and
the live one was charge/issue after the tcgen05 fence, which is exactly the
surviving sequence. A const bool branch is resolved at monomorphization, so the
timed path's codegen is unchanged; everything else the trim touched is host-side
or a symbol rename.

Rebased onto 500927c. My own cpu=/timeout edit is dropped in favour of
#93 — the only Modal change here is the clc_bench entry point. The one edit to
bench.rs, which #86/#90 owns, is making Timings::min pub; it is still
private on main, so it is not a duplicate, and it is the whole of my change to
that file after the rebase onto #96.

What I did not do

softmax and layernorm are untouched — neither is persistent and neither has an
item to steal. flash_forward is the kernel that would most want this and still
has no launcher (#85), so a scheduler cannot be checked for correctness on it,
let alone timed.

@feitreim
feitreim force-pushed the clc-work-stealing branch from 607a590 to e410f5f Compare July 28, 2026 21:12
@feitreim
feitreim merged commit c802627 into main Jul 28, 2026
3 checks passed
feitreim added a commit that referenced this pull request Jul 28, 2026
…a stale checkout (#106)

PR #97 reported cuda_device::clc's module usage example as having is_canceled
inverted against its own function doc. The defect was real at v0.2.1 and
upstream fixed it in 61f4a563 on 2026-06-29, a month before the b099f64c this
repo pins -- which now ships a 13-line clc.rs with no usage example at all.

The claim came from a stale sibling checkout under ~/.cargo/git/checkouts:
of six revisions cargo has left there, only 4514af2 (v0.2.1, 2026-06-10) still
carries it. The file cited as settling the question, crates/mir-lower/.../clc.rs,
exists at no upstream revision.

No code changes -- != 0 was and is correct, established by the probe rather
than by the doc. This corrects the two places that assert the upstream defect
in the present tense, and names the mechanism: a ~/.cargo checkout is not a
citation without git log -1 on that directory.
feitreim added a commit that referenced this pull request Jul 29, 2026
Tier 1 checks the library, device-tests and examples. A root `cargo fmt`
reaches only the first -- the other two are separate workspaces -- so a change
can be clean locally and red in CI on whitespace alone. That has happened three
times (#97, #104, #109), each costing a review round trip on a gate that runs in
twenty seconds.
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.

Cluster Launch Control: hardware work-stealing exists in cuda-oxide and pipeline::run schedules statically

1 participant