Skip to content

The occupancy step is 168, not 170, and regcount now fails the run on it (#95) - #100

Merged
feitreim merged 1 commit into
mainfrom
regcount-occupancy-gate
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
regcount-occupancy-gate

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Closes #95.

The answer, in one line

The gate is in regcount and it fails the run on the step. The step is 168,
not 170
— so gemm_cg2 has one register of headroom, not three — and
regcount was running in no workflow at all, so the gate had nowhere to
fire from until this PR put it on tier 2.

1. The gate

modal_app.py::regcount grew a fourth table, and it is the only thing in this
repo that fails a run on a register count:

  the occupancy step (#95) — registers against the residency each kernel's
  own grid is sized for. `ceiling` is derived, not written down anywhere:
    kernel               threads  wants  regs  ceiling  headroom  allows
    gemm_cg2                 128      3   167      168         1       3

  every gated kernel is inside its step.

Nothing in that row is a constant in modal_app.py. threads is the exact
block shape out of #[launch_contract(block = (128, 1, 1))] — the same literal
the host launch is validated against — and wants is const CTAS_PER_SM: u32 = 3, the residency gemm_cg2's own grid is sized from, bisected on a clock by
#83 and counted with %smid by #84. Both are read out of examples/src/gemm.rs
at run time, so if THREADS or the residency target moves, the gate moves with
it. If either declaration goes missing the gate raises rather than quietly
watching nothing. A kernel joins GATED_KERNELS by declaring both; gemm_cg2
is the only kernel in the repo that does today.

It gates on the step, not on a count. 167 → 165 passes. 167 → 169 fails,
because 169 is the register that costs a CTA.

And the comment says what the number does not mean. Crossing the step is not
a claim that the kernel got slower per thread — this repo has five occasions
where the register column ordered time backwards (#47, #63, #67, #76, #94), and
#94 spent 40 → 167 registers on this very kernel for +13.7%. Occupancy is the
one consequence of a count that does not depend on a theory of what the
registers are for, and for gemm_cg2 it is not even abstract: the grid is
literally SMS * CTAS_PER_SM / RANKS clusters, so a residency it no longer has
is a persistent grid that oversubscribes.

2. The step is 168, and #95's table is wrong by two

#95 gives ≤170 → 3 CTAs. The real arithmetic is coarser, and in two ways at
once — this is #95's own "if the step arithmetic is more complicated than the
table, that is a finding":

  • registers are allocated to a warp, rounded up to 256;
  • the file is split across 4 sub-partitions, and a warp is assigned to one
    whole.

Both read off cuda_occupancy.h for computeMajor == 10
(cudaOccRegAllocationGranularity → 256, cudaOccSubPartitionsPerMultiprocessor
→ 4). So 169 registers a thread is 5408 a warp, which rounds to 5632, of
which a 16384-register sub-partition holds two warps and not three — 8 warps
an SM, 2 CTAs of 128 threads. 168 is 5376 exactly, which fits three.

At 128 threads on sm_100a:

registers CTAs/SM
≤ 128 4
≤ 168 3
≤ 256 2

gemm_cg2 is at 167. One register of headroom, not three.

I did not want to ship a transcription on my own reading of a header, so the
gate checks it. _check_occupancy_model writes a ~25-line C++ file that
includes the toolkit's cuda_occupancy.h, fills in only the arch and the
register file, calls NVIDIA's own cudaOccMaxBlocksPerSMRegsLimit at every
register count ptxas can emit, and raises on any disagreement. It needs no
driver and no GPU, which is the whole reason it can live in a CPU-only gate:

  the occupancy model, against the toolkit's own (`cuda_occupancy.h`,
  compiled here — it needs no driver, and neither does this whole run):
     128 threads: identical at all 255 register counts ptxas can emit

The one hardware number that stays hardcoded is the 64 K register file, named
and sourced. ptxas is the only NVIDIA tool in the container that might have
been asked instead, and it does not know.

Two smaller things the header says that the naive model does not: ptxas' own
255 binds before the header's maxRegsPerThread of 256 for compute major ≥ 7,
and from Hopper onward block barriers are an occupancy term too
(cudaOccMaxBlocksPerSMBlockBarrierLimit; ctaLimitBlocks * 2 available at
10.0). The gate does not model barriers — ptxas -v reports them and they are
nowhere near binding here — but it is a real fifth term and worth knowing exists.

3. regcount was in no workflow

This is the part of #95's premise that did not hold, and it mattered more than
the arithmetic. regcount is referenced in README.md, GAPS.md and
examples/README.md and appears in no file under .github/. Tier 2 ran
::build and nothing else. So the only thing standing between gemm_cg2 and a
silent third of its occupancy was somebody remembering to run an entry point by
hand and read a column of a fifty-row table.

cuda.yml now has a second job that runs modal run modal_app.py::regcount. A
separate job rather than a step in build, because it goes red for a different
reason — a register count, not a compile — and that belongs on the check list
rather than inside a log; and because it is independent work that can run beside
build. The cost is honest and stated in CI.md: it builds both kernel crates
in its own container, so tier 2 costs roughly twice what it did. Still
CPU-minutes.

4. The positive control, and its actual red output

Per #67 and #76: a check nobody has seen fail is not a check.

groupnorm_tile in examples/src/layernorm.rs is a real kernel at a real 236
registers
and 128 threads — the one that file's header deliberately leaves
holding the whole band. Temporarily giving it #[launch_contract(domain = 1, block = (128, 1, 1))], giving the file const CTAS_PER_SM: u32 = 3;, and adding
it to GATED_KERNELS — three edits, all reverted, none of them in this diff —
modal run modal_app.py::regcount exits 1:

  the occupancy step (#95) — registers against the residency each kernel's
  own grid is sized for. `ceiling` is derived, not written down anywhere:
    kernel               threads  wants  regs  ceiling  headroom  allows
    gemm_cg2                 128      3   167      168         1       3
    groupnorm_tile           128      3   236      168       -68       2

RuntimeError: a kernel crossed its occupancy step: it now costs a CTA per SM.
  groupnorm_tile: 236 registers at 128 threads leaves 2 CTAs/SM where the
  kernel is built for 3. 168 is the most a thread may use and keep 3 — it is
  68 over.

This is not a claim that the kernel got slower per thread — the register
column has ordered time backwards five times here (#47, #63, #67, #76, #94),
and #94 spent 40 -> 167 registers on `gemm_cg2` for +13.7%. It is the one
consequence of a register count that does not depend on any theory of what the
registers are for: CTAs come off the SM. A kernel whose grid is sized from
CTAS_PER_SM (`gemm_cg2`'s is `SMS * CTAS_PER_SM / RANKS` clusters) is then
sized for a residency it does not have. If the trade is worth it, say so and
move CTAS_PER_SM — but move it having measured, not having seen this go red.

gemm_cg2 stays green in the same table, which is the half of a control that is
easy to leave out.

What this control does not do is inflate gemm_cg2 itself, and the reason is
itself a finding — §6.
I could not make that kernel exceed 168 by adding live
state, and I did not touch examples/src/gemm.rs to fake it.

5. Is the 40 → 167 jump load-bearing? Measured, CPU-only

-maxrregcount on the same PTX — no rebuild, so nothing else varies — says
what a smaller budget costs this kernel:

cap regs spill st spill ld frame CTAs/SM by registers
none 167 0 0 528 3
255 234 0 0 528 2
200 200 0 0 528 2
176 176 0 0 528 2
168 168 0 0 528 3
160 160 0 0 528 3
152 152 0 0 528 3
144 144 0 0 528 3
136 136 44 44 560 3
128 128 172 200 632 4
96 96 840 1000 928 5
64 64 1664 1952 1216 8
40 40 2376 2756 1424 12

Three things fall out of that table.

167 is not what the kernel needs — it is what ptxas chose. The identical
PTX compiles at 144 registers with zero spill and an unchanged 528-byte
frame
. So at least 23 of the 167 are allocator headroom, not live state. The
first real cost is at 136 (44 bytes each way), and getting back to 40 — the
pre-#94 number — costs 2376 bytes of spill stores. The old 40 was never this
kernel compiled cheaper; it was a different amount of work.

167 is not a ceiling either. Given a 255 budget ptxas takes 234 on the
same PTX. Both directions are available and it picks 167.

The band is not what moved. #94's own table has the frame at 528 bytes on
both sides
of the change, and a [32, 128] fp32 band is 512 bytes. The band
lived in local memory before #94 and still does. Whatever the 127 registers are,
they are not the accumulator.

So: the jump bought 13.7% and I have no reason to doubt that. The number 167
is not load-bearing — there is a 23-register gap between what ptxas allocated
and what it needs to compile this kernel without spilling, and that gap is free
headroom for #15, #87 and #88 if they want it.

I did not establish the mechanism of 40 → 167. #94's agent's guess (the
memory clobber) was measured and wrong, and I have not replaced it with a
measured one. What I can say is that the store loop's shape moves the number
by about 20 (§7) and the allocator's target sets the rest, and neither of those
is "what the extra live state is". I would rather leave that open than add a
sixth wrong theory to the five this repo has already collected.

6. ptxas pins this kernel at 168, and that resets what the gate is for

I tried three times to push gemm_cg2 over its step from src/global.rs, which
I was cleared to change. This is what happened:

tree gemm_cg2 global_copy_probe_128_movers frame
baseline 167 168 528
+ 8 fp32 forced live across the drain (SSA, opaque ld.global.v2, memory clobber both ends) 168 164 528
+ 32 fp32 the same way 168 168 528 → 784
store loop transposed (§7) 148 146 528

Eight registers of live state that ptxas cannot fold, sink or rematerialise
moved it one register. Thirty-two moved the frame by 256 bytes and left
the count at 168.

And look where everything lands: gemm_cg2 167, flash_forward 168,
global_copy_probe_128_movers 168, global_copy_probe_128_open_coded 168. Four
unrelated kernels within one register of each other, and 168 is exactly the
largest count that keeps twelve warps on an SM
. ptxas' default allocation
target sits on this kernel's occupancy step.

That is worth stating plainly, because it changes what #95's risk actually is.
"#15 or #87 adds live state and the count drifts past 170" is not the failure
mode
— ptxas will spill before it does that, and #87's bigger pair tile will
show up as spill bytes and a growing frame, not as a count over the step. What
will fire this gate is THREADS or CTAS_PER_SM moving (#87 is a plausible
source of both), a #[launch_bounds] or -maxrregcount being introduced, or a
toolkit upgrade moving ptxas' default. Those are all real, all silent today,
and the gate is now the thing that notices. That paragraph is in the gate's own
comment, not only here.

It also means the gate is close to vacuous only for this one geometry, not in
general: at 256 threads a kernel wanting 2 CTAs/SM has a ceiling of 128, well
under ptxas' target, and the gate bites immediately.

7. Reclaimable headroom, for whoever takes #15 / #87 / #88

Two levers, both measured, neither shipped here, each with the reason.

(a) 23 registers, free, no source change. -maxrregcount=144 compiles this
kernel with zero spill and an unchanged frame. The cleaner spelling is
cuda-oxide's #[launch_bounds], which emits .maxntid/.minnctapersm and makes
the residency target a compiler input rather than a CI check — in
examples/src/gemm.rs, which I am not touching:

     #[kernel]
     #[cluster_launch(2, 1, 1)]
+    // 128 threads and CTAS_PER_SM CTAs an SM, told to ptxas rather than
+    // checked after it. `-maxrregcount=144` on this kernel's own PTX is
+    // zero spill on an unchanged 528-byte frame; the default 167 is 23
+    // registers of allocator headroom over that.
+    #[launch_bounds(THREADS, CTAS_PER_SM)]
     #[launch_contract(

Caveat, and the reason this is a sketch and not a diff: .minnctapersm makes
ptxas spill rather than exceed, so it converts an occupancy loss into
local-memory traffic, and #47 priced exactly that trade at 2.6× on softmax. It
needs a clock. regcount shows the static half for free.

(b) ~20 registers from the store loop's shape. Transposing
store_rows_in_runs' nest — value outer, slot inner, the same stores to the same
addresses in a different order — moves gemm_cg2 167 → 148 and
global_copy_probe_128_movers 168 → 146. Two kernels, same direction, same
magnitude:

-        let mut slot = 0usize;
-        while slot < L::SLOTS {
-            let start = dest.at(row + L::row_of(lane, slot), column);
-            let mut value = 0usize;
-            while value < L::VALUES {
-                let at = start.add(L::col_of(lane, value) as usize);
+        let mut value = 0usize;
+        while value < L::VALUES {
+            let mut slot = 0usize;
+            while slot < L::SLOTS {
+                let at = dest
+                    .at(row + L::row_of(lane, slot), column)
+                    .add(L::col_of(lane, value) as usize);

src/global.rs was cleared for me to change, and I deliberately did not. Every
instruction touches the same 32 lane addresses either way, so per-store sector
utilisation — the whole of #94's win — is unchanged; what changes is the order
the stores are issued, and therefore row locality across successive
instructions. The epilogue is 20–22% of the 8192³ launch by #94's own fit. That
is a timing claim, this issue booked no B200, and shipping an untimed reordering
of the hot epilogue is exactly the move this repo keeps writing issues about. It
belongs to whoever next runs --case gemm-depth.

Scope

examples/src/gemm.rs and src/pipeline.rs are untouched, as is all of src/,
device-tests/ and the rest of examples/. #97 is unaffected.

  • modal_app.py: the occupancy model, the ceiling, the source-parsed launch
    geometry, the cuda_occupancy.h cross-check and the gate. ~250 lines, most of
    it comment.
  • .github/workflows/cuda.yml: a regcount job on tier 2.
  • CI.md, README.md: what the gate is, what it costs, and what 168 is.

Verification

  • modal run modal_app.py::regcount — exit 0 on this tree; table in §1, model
    cross-check green at all 255 counts, ladder and timed-twins tables unchanged
    from main (identical at all 20 rungs).
  • modal run modal_app.py::regcount on the control tree — exit 1, output in
    §4, reverted.
  • modal run modal_app.py::build — exit 0, every step including the real
    sm_100a codegen of both kernel crates and the cublas feature.
  • No GPU booked. Everything above is CPU-only, including the SASS and
    -maxrregcount probe behind §5 and §6 (temporary, not in this diff).

Measured vs inferred. Measured: every register, spill and frame figure; the
step table, against NVIDIA's own model, compiled and run; the three inflation
attempts; the transpose. Inferred, and flagged as such in §6: that the 167/168
clustering across four kernels is ptxas' default occupancy target rather than
four coincidences. Not established, and stated as such in §5: what the 127
registers #94 added actually hold.

`modal_app.py::regcount` now derives, per gated kernel, the most registers a
thread may use and still hold the CTAs per SM the kernel's own grid is sized
for, and raises when `ptxas` went over. The ceiling comes from the launch and
the register file, never from a constant: `#[launch_contract(block = ...)]` and
`const CTAS_PER_SM` are read out of the kernel's own source, and the arithmetic
is transcribed from `cudaOccMaxBlocksPerSMRegsLimit` and checked against the
toolkit's `cuda_occupancy.h` on every run.

At 128 threads the step is 168, not the 170 a naive `file / threads / CTAs`
gives: registers are allocated per warp in units of 256 across four
sub-partitions. `gemm_cg2` is at 167, so its headroom is one register.

`regcount` also ran in no workflow at all, so nothing between `gemm_cg2` and a
silent third of its occupancy was automatic. It is a tier-2 job now.
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.

gemm_cg2 is three registers from losing a third of its occupancy, and nothing is watching

1 participant