Skip to content

Count the CTAs instead of asking, and flash_forward's 1 is real for the other reason (#78) - #84

Merged
feitreim merged 2 commits into
mainfrom
tmem-residency-timed
Jul 28, 2026
Merged

feitreim merged 2 commits into
mainfrom
tmem-residency-timed

Conversation

@feitreim

@feitreim feitreim commented Jul 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #78.

The answer, in one line

flash_forward's 1 CTA/SM is REAL — and tcgen05 is not what causes it. Its
shared plan is. Which means #74's advice was right for this kernel by accident, and
#70's conclusion that shared memory was not the lever is backwards: it is the only
lever that kernel has.

Counting, rather than asking or inferring

#78 asks for residency "by TIMING rather than by asking the query", and suggests #51's
grid-cap sweep. There is a better instrument on this hardware, and it is what landed.

%smid names the SM a CTA is running on and %globaltimer is a device-wide nanosecond
clock, so a CTA can write down where it ran and when. device-tests' new
tmem residency census launches 4736 one-warp CTAs, each of which records its
%smid, timestamps entry, allocates, timestamps, spins on the clock for 100 µs, and
timestamps again. The host sweeps those intervals per SM and takes the most ever open
at once.

That is strictly stronger than a grid-cap sweep, for a reason no sweep can fix. Two
intervals are counted:

interval overlap counts
[entered, left] CTAs the SM held a slot for, whatever they were doing
[allocated, left] CTAs holding tensor memory at the same instant

A CTA parked inside a blocking tcgen05.alloc is resident and making no progress. A
throughput curve reads that as absence; the census reads it as the resident column
sitting one above holding. It also needs no calibration run, no assumption that the
work is latency-bound, and no argument about where a curve "flattens".

The measurement

B200, 4736 one-warp CTAs over 148 SMs, 100 µs each, 233472 B shared memory per SM.
budget is min(512 / columns, shared per SM / plan).

rung columns shared B budget resident holding driver says
no tcgen05 — 32 — 32 32 32
alloc 32 32 16 17 16 1
alloc 64 32 8 9 8 1
alloc 128 32 4 5 4 1
alloc 256 32 2 3 2 1
alloc 512 32 1 2 1 1
alloc 512 then free (512) 32 — 27 25 1
alloc_cluster none — 32 — 8 8 8
alloc_cluster 128 32 4 5 4 1
alloc_cluster 512 32 1 2 1 1
flash's plan, no tcgen05 — 147536 1 1 1 1
alloc + half that plan 256 73768 2 3 2 1
flash_forward's envelope 256 147536 1 1 1 1
gemm's envelope (cg2) 128 73792 3 3 3 1

holding equals budget at all fourteen rungs. Exact, not a trend — which is why
it is asserted as an equality rather than reported.

What that settles

1. flash_forward (#78's actual question). Its 256 columns admit 2. Its 147536 B
shared plan admits 1 — measured on the rung with no allocator in the kernel at
all
. Together: 1. So the headline number #74 reported is correct and every reason
given for it was wrong. #70 queried that plan at 147536, 73792, 32800 and 0 bytes, got
1 block/SM at each, and ruled shared memory out; all four answers were the allocator
pinning the query.

2. gemm — an independent confirmation of #83. #83 bisected its grid cap on a clock
and got 3 CTAs/SM. The census at the same envelope counts 3. Two instruments
with nothing in common agreeing on one integer, and the census adds the mechanism: 128
columns allow 4, 73792 B of shared allows 3, the smaller binds. Shared memory caps
that kernel and tensor memory does not.

3. alloc_cluster vs alloc_block — #78's leading hypothesis, refuted. Identical at
equal column counts: 4 and 4 at 128, 1 and 1 at 512. A cta_group::2 allocation is not
split across the pair; each rank pays its full count against its own SM. What actually
differs between the two kernels is their shared plans, 73792 B against 147536 B.

4. What the query is doing. It reacts to the instruction being present, not to a
resource held: the allocate-and-release rung takes all 512 columns, gives them straight
back before the counted interval, runs 25 CTAs an SM, and is still answered 1. Both
queries are otherwise accurate — 32 predicted / 32 counted for the plain control, 8 / 8
for the cluster control — which is exactly what made the 1 easy to believe.

5. cuOccupancyMaxActiveClusters exists. It takes the cluster shape the block query
has no argument for. main.rs's cluster placeholder and #51's "cannot currently be
answered" predate anyone calling it. It is in the driver column above.

A correction to #74/#77 that I am also the author of

Mid-PR I had written that flash was at 2 and its 1 was "the query, not the hardware".
That was true of tensor memory alone and false of the kernel, and only adding the
shared-memory rungs caught it. Both the code and the prose now carry the joint model.
The general claim that survives: shrinking a TMEM allocation to the next legal power
of two doubles the CTAs tensor memory holds
— a real lever where #74 and #77 said
there was none — but it is worth nothing while shared memory is the tighter resource,
which for both kernels here it is.

The probe is a guard

The case fails if holding != budget at any rung (above is impossible, below means the
driver changed its reservation model and the docs are stale), if the control collapses
to 1, if the allocate-and-release rung falls to 1, or if any spin ended on its iteration
guard rather than on the clock — a short hold overlaps less and would read exactly like
low residency, so that check is what stops a hoisted %globaltimer from being reported
as a finding. The table prints on both paths.

tmem occupancy ladder is kept and still asserted; its docs now say it measures the
query's behaviour, and its verdict string no longer draws a hardware conclusion from it.

Not done here, deliberately

flash_forward is untouched. It has no launcher and no CPU reference, so shrinking its
ring cannot be checked for correctness, let alone timed. Filed as #85 with the numbers.
Its module doc still carries #77's "shrinking the allocation buys nothing", which is now
wrong — correcting it needs examples/src/, which #29 owned for this window.

Verification

  • modal run modal_app.py::build — green, real sm_100a codegen of both kernel crates
    including the nine new census kernels.
  • modal run modal_app.py::device_tests — 31/31 on a B200, table above. Reproduced
    identically across five runs.
  • modal run modal_app.py::examples — green; gemm exact, softmax 1.97e-3,
    layernorm 3.87e-3.
  • modal run modal_app.py::regcount — census kernels at 16–20 registers, 0 spills, 0
    frame, so nothing in them can be a register ceiling.
  • cargo fmt, cargo clippy --all-targets -- -D warnings, cargo test (76 + gaps).
  • Rebased onto e913940; the one conflict was examples/README.md's §7, where The cap was the whole story: gemm adopts pipeline::run at a dead heat (#51) #83 had
    rewritten the section — resolved in favour of The cap was the whole story: gemm adopts pipeline::run at a dead heat (#51) #83's text with the census's mechanism
    added under it.

Measured vs inferred. Every number in every table is measured. Inferred: that the
mechanism is a hardware allocator dividing 512 columns rather than a driver reservation
— the behaviour is indistinguishable from that at every rung, and the allocate-and-
release rung rules out a static admission-time reservation, but no driver internal was
inspected. Also inferred and flagged as such in the README: that 116736 B is where
flash's plan would start admitting 2. The census brackets that threshold between 73768 B
(counts 2) and 147536 B (counts 1) and does not locate it.

…he other reason (#78)

`cuOccupancyMaxActiveBlocksPerMultiprocessor` returns 1 for any kernel
containing a `tcgen05.alloc` (#77). #51 then measured `gemm` losing 2.07x to a
one-CTA-per-SM grid cap, which is impossible if 1 were the ceiling, so the query
had been shown once not to predict residency and flash_forward's 1 stopped being
established.

Rather than infer residency from where a throughput curve flattens, count it.
`%smid` names the SM a CTA is running on and `%globaltimer` is a device-wide
nanosecond clock, so each census CTA records where it ran and timestamps both
ends of its TMEM allocation; the host sweeps those intervals per SM for the most
ever open at once. Two intervals are kept, and the pair is the point: a CTA
parked inside a blocking `tcgen05.alloc` is resident and making no progress,
which no grid-cap sweep can distinguish from absence.

Residency is `min(512 / columns, shared per SM / plan)`, exactly, at all
fourteen rungs. So:

- flash_forward's 1 CTA/SM is REAL, and tcgen05 is not the cause. Its 256
  columns admit 2; its 147536 B shared plan admits 1 with no allocator in the
  kernel at all. Shared memory is the only thing capping it — the resource #70
  tried to rule out and could not, because the query it asked was pinned by the
  allocator in all four of its readings.
- gemm's envelope counts 3, reproducing #83's clock-bisected 3 on an instrument
  with nothing in common with a throughput curve.
- alloc_cluster is charged identically to alloc_block at equal columns, so the
  pair does not split the SM's tensor memory. #78's leading hypothesis for why
  the two kernels differ is refuted; what differs is their shared plans.
- The query reacts to the instruction, not to a held resource: a kernel that
  allocates 512 columns and releases them before doing anything runs 25 CTAs an
  SM while the query still answers 1.

`cuOccupancyMaxActiveClusters` does describe a `#[cluster_launch]` kernel, and
matches the census on the cluster control. The docs saying it could not predate
anyone calling it.
Two doc sites said the occupancy question was unanswerable for a
`#[cluster_launch]` kernel. Both are accurate about the function they name and
misleading about the situation, one commit after the census showed
`cuOccupancyMaxActiveClusters` does describe such a kernel and agrees with a
counted census on the cluster control.

- `main.rs`'s `entry` field: `None` now marks a query nobody wired into that
  table rather than a residency nothing can reach.
- `gemm.rs`'s `CTAS_PER_SM`: #83's clock-bisected 3 stays as the measured
  figure and so does the route to it. Added that #78's census counts 3 at the
  same envelope from `%smid` and `%globaltimer` — two methods sharing nothing
  agreeing on one integer — and that it names the resource the bisection could
  not: 128 columns admit 4, the 73792 B shared plan admits 3, so shared memory
  caps this kernel and tcgen05 does not.

No constant re-tuned, no kernel touched.
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.

flash_forward's 1 CTA/SM may be the query rather than the hardware — the GEMM's is not real

1 participant