Skip to content

The GEMM entry points declare their block, so the driver stops deriving it (#122) - #123

Merged
feitreim merged 2 commits into
mainfrom
diag/gemm-regalloc-pins
Aug 6, 2026
Merged

feitreim merged 2 commits into
mainfrom
diag/gemm-regalloc-pins

Conversation

@feitreim

@feitreim feitreim commented Aug 6, 2026 •

Copy link
Copy Markdown
Owner

Closes the #119 follow-up. The diagnosis is #122; this is the fix and the two
instruments that produced it.

The short version. The build was never the nondeterministic layer. cargo oxide
emits gemm.ptx and cuModuleLoad hands the text to the driver's JIT, so the
registers a GEMM kernel gets are assigned by whatever libcuda the Modal host has —
the one compiler in this chain the tree does not pin. What made #119's split fragile is
separate and fixable: an entry point with no .maxntid does not tell that JIT what
block it will be launched in
, so the JIT allocates freely and then derives the
largest block the result can be launched in. Measured on the split commit, unpinned:

kernel registers derived maxntid
gemm_tcgen05_bf16_optimized 86 640
gemm_tcgen05_f32_optimized 96 640
gemm_tcgen05_f32_accumulate 140 384

Those fit largest multiple of 128 ≤ 65536 / registers. Run it forward and 182
registers derives 256
, below the split's own 320 — max_active_blocks_per_multiprocessor
of 0 and DriverError(701), which is #119's report exactly, and the same 182 derives
256 ≥ 192 and launches fine on main, which is why this is silent today.

What ships

#[launch_bounds(192, 1)] on the three shipped tcgen05 GEMMs. The geometry becomes
an input rather than an output: .maxntid reads exactly 192 instead of a number the
allocation implied, and one resident CTA is a constraint ptxas must satisfy. An
allocation that cannot reach it spills — which the budget gate already fails on —
instead of producing a kernel the driver refuses.

Two harnesses in modal_app.py, which are what settled the layer question:

  • modal run modal_app.py::rebuilds --kernel <k> --rounds N — builds one commit N times,
    each round a fresh copy at the same path, reporting the PTX digest, entry emission
    order, per-function body digests and ptxas -v's allocation. GPU-free. The same path
    matters: cargo's -C metadata disambiguator is derived from the build directory and
    rides inside every mangled generic symbol, so per-round directories give 8 distinct
    PTX digests out of 8 for a reason that has nothing to do with the question (and,
    measured, nothing to do with the allocation either — all 8 still 86/96/140).
  • modal run modal_app.py::jit --kernel <k> — the half that needs a GPU. Prints the PTX
    digest, the driver version, the image ptxas' allocation and cuFuncGetAttribute's
    side by side, so a disagreement between the last two is legible.

Evidence

Registers move, and that is the pin doing its job.

kernel before after spill / frame
gemm_tcgen05_bf16_optimized 132 160 0 / 0
gemm_tcgen05_f32_optimized 168 172 0 / 0
gemm_tcgen05_f32_accumulate 146 146 0 / 0

Upward is the point rather than a cost: a .maxntid of exactly 192 is what lets the
allocator spend the register file this kernel's one CTA an SM already owns. Residency
does not move — at 192 threads even 255 registers is one CTA — and the budgets are
ratcheted to the new numbers.

Rebuild stability, 4 rounds with the pin: gemm.ptx one digest, one entry order,
160/172/146 every round, zero spill. (Unpinned main is equally stable at 132/168/146
over 6 rounds — see #122 — so this is a claim about the declaration, not about
curing a flap.)

maxntid is now declared rather than derived. jit on a B200, driver 580.95.05:

gemm_tcgen05_bf16_optimized 160 regs, 0 spill bytes, maxntid 192
gemm_tcgen05_f32_optimized  172 regs, 0 spill bytes, maxntid 192
gemm_tcgen05_f32_accumulate 146 regs, 0 spill bytes, maxntid 192

against the unpinned 640 / 640 / 384 above.

Gates. gpu/gemm's four parity checks green at unchanged tolerances; the budget
gate green against the ratcheted ceilings; the build-time ptxas -v and the driver's
cuFuncGetAttribute agree on all three numbers.

Throughput. train_ab against main, 30 steps, two rounds alternating in one
container:

arm mean tok/s MFU spread runs
baseline main 91 236.9 40.52% 0.26% 91 353.8, 91 119.9
candidate 91 170.1 40.48% 0.54% 90 924.5, 91 415.8

candidate/baseline = 0.9993, and the rounds cross — the candidate loses round 0 and
wins round 1 — against a within-arm spread wider than the gap. Unchanged, as expected:
the pin does not move residency, only who chooses the number.

Not in this PR

gpu/ops (81 kernels), gpu/tensor-gpu (31) and gpu/flash-attn's lib.rs (12)
declare no bounds either. All are measured reproducible (#122's scope table) and none
is exposed today, but the check is an accident of their register counts rather than
anything in tree. Those crates are other agents' this week; #122 carries the follow-up.

`rebuilds` builds one commit N times and reports, per round, the PTX digest,
the entry emission order, per-function body digests and `ptxas -v`'s
allocation. That is the whole layer bisection in one table: PTX bytes moving
is codegen (rustc + the cuda-oxide backend + `llc`), bytes identical with
registers moving is ptxas. Every round builds at the *same* path, because
cargo's `-C metadata` disambiguator is derived from it and rides inside every
mangled generic symbol — per-round directories make the PTX differ for a
reason that has nothing to do with the question. No GPU: `cargo oxide` links
the driver stub and ptxas is the allocator anyway.

`jit` is the half that needs one. `cargo oxide` emits `<kernel>.ptx` and
`load_kernel_module` hands the text to `cuModuleLoad`, so the registers a
kernel actually gets are assigned by the *driver's* JIT at load time — the one
compiler in the chain this tree does not pin. It prints the PTX digest, the
driver version, the image ptxas' allocation and `cuFuncGetAttribute`'s side by
side, so a disagreement between the last two is legible.
…driver

stops deriving it

`cargo oxide` emits PTX and the driver JITs it at `cuModuleLoad`. An entry
point carrying no `.maxntid` tells that JIT nothing about the block it will be
launched in, so it allocates against the architecture's 255 registers and then
*derives* the largest block the result can be launched in. Measured on the
warpgroup-split commit of #119: 640, 640 and 384 threads, from 86, 96 and 140
registers. A build that had chosen 182 would have derived 256 — below that
kernel's own 320 — which is `max_active_blocks_per_multiprocessor` of 0 and
`DriverError(701)` at the first launch, and is exactly what #119 saw once in
three. The same 182 is fine at 192, which is why this is invisible today.

`#[launch_bounds(192, 1)]` on the three shipped tcgen05 GEMMs makes the
geometry an input instead of an output: `.maxntid` now reads exactly 192 rather
than a number the allocation happened to imply, and one resident CTA is a
constraint ptxas must satisfy. An allocation that cannot reach it spills, which
the budget gate already fails on, instead of producing a kernel the driver
refuses.

Two ceilings move with it — bf16 132 -> 160 and the fp32 store 168 -> 172, the
reduce unchanged at 146 — with zero spill and zero frame throughout, and the
budgets are ratcheted to match. The direction is the point: a `.maxntid` of
exactly 192 is what lets the allocator spend the register file this kernel's
one CTA an SM already owns. Residency does not move, because at 192 threads
even 255 registers is still one CTA.

Stable across four rebuilds at 160/172/146, parity green at unchanged
tolerances.
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