Repository navigation
Sweep the register ladder instead of measuring two widths (#60) - #62
Merged
Merged
Conversation
Every register claim in this repo was measured at 32 and 128 columns. The
reason was never cost — `ptxas` is a host compiler and a width is milliseconds
— it was that each width had to be a hand-written probe. So `device-tests`
grows a `ladder!` macro: a rung is one line, and it emits a `#[kernel]` per
(shape, spelling) named after the shape it compiles, using `${concat}` to build
the identifier a PTX entry symbol needs.
The ladder is fourteen shapes by five spellings. Columns 16 to 256 at the
warp's 32 rows, and — for the first time in this repo — the *row* extent too,
at two widths, so four rungs pair off at equal per-thread volume and different
aspect ratio. `[32, 256]` and `[64, 128]` are 256 fp32 a thread, one past the
register file, because a shape that spills is the informative one.
`regcount` prints the sweep as two tables (registers with spill bytes, then the
stack frames beside them) and *locates the cliff* per spelling instead of
leaving it to be read off the rows. It gains `--determinism`, which throws the
PTX away, rebuilds and asserts an identical table — the control #31 ran by hand
and every future diff needs. It passes: 130 kernels, every counter, twice.
What the sweep found, none of it visible from two points:
- `assign` and `open_coded` are identical at all fourteen rungs, every counter.
#31's claim that the `_assign` API compiles to the loop it replaces, at
fourteen shapes instead of two.
- The row extent is not the column extent. `[64, 64]` and `[32, 128]` are both
128 fp32 a thread; `assign` is 32 registers at one and 252 at the other.
- "One expression is the floor" does not survive. At three rungs the in-place
forms are 81 to 130 registers under the fused one, and the stack frames say
why: those shapes did not fit the band, so ptxas left it in local memory and
streamed it. Materialization between statements is real and one-directional,
but it does not order the table — whether the band reaches registers at all
switches on shape, and that effect is bigger.
- The cliff: `rebound` first spills at 128 values a thread, the other three at
192. Nothing at all is visible below 64, which is why a 32-wide probe could
sit through a change worth 87 registers at 128 (#5).
`lane_probe`'s recorded table was stale in three cells (198 -> 168 registers at
128, 72/256 -> 56/384 at 32). Re-measured, and the drift is noted with the part
of #27's finding it costs — hoisting the lane is free at 128 now.
`::regcount` wall time: 52s before, 1m36s-1m50s after, for 70 more kernels of
NVVM codegen. Not enough to be worth a subset mode.
Closes #60
feitreim
force-pushed
the
register-ladder-sweep
branch
from
July 28, 2026 03:04
26cfc5b to
3e6cb1e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two widths was never a budget decision —
ptxasis a host compiler and a widthcosts milliseconds — it was that each width had to be a hand-written probe. So
this is a macro.
ladder_rung!emits one#[kernel]per (shape, spelling), named after theshape it compiles;
ladder!crosses a list of shapes with the five spellings.A rung is one line. Identifier concatenation is what makes that possible — a
PTX entry symbol is the bare function name, so every rung needs a distinct
identifier — and it costs one nightly feature (
macro_metavar_expr_concat) ona crate that is already nightly-pinned to the backend's toolchain.
The macro-generated kernels are not registered with
#[cuda_module](anattribute macro cannot see through a
macro_rules!invocation), which isexactly right for probes: they get no host launcher, nothing launches them, and
the codegen collector roots kernels by symbol name, so they land in the PTX all
the same.
The ladder
Fourteen shapes by five spellings. Columns 16 → 256 at the warp's 32 rows, and
— for the first time in this repo — the row extent too, at two widths, so
four rungs pair off at equal per-thread volume and different aspect ratio.
[32, 256]and[64, 128]are 256 fp32 a thread, one past the register file,deliberately.
Four of the five spellings are
scalar_map_probe's own forms line for line, sothe
[32, 128]row reproduces the standing two-width table exactly — which isthe sweep's calibration. The fifth is new: the accumulate written in place too,
which nothing in
scalar_map_probedoes.Registers / spill store bytes:
regcountprints the stack frames as a second table beside this one, because arung can be cheap in registers because the band fit or because ptxas never
promoted it and is addressing it in local memory — opposite outcomes the
register column cannot tell apart.
And it prints the cliff, rather than leaving it to be eyeballed:
What the sweep found
assignandopen_codedare identical at all fourteen rungs, everycounter. #31's claim that the
_assignAPI compiles to the loop it replaces,now at fourteen shapes instead of two, including shapes that spill a kilobyte.
The row extent is not the column extent.
[64, 64]and[32, 128]areboth 128 fp32 a thread;
assignis 32 registers at one and 252 at the other,on the same 2560-byte frame. Nothing before this could have caught it — every
probe in the repo is 32 rows.
"One expression is the floor" does not survive. At
[32, 96],[48, 64]and
[64, 64]the in-place forms are 81 to 130 registers under the fusedone. The frames say why: at each of those rungs the cheap pair keeps a frame at
least as large as the form it beat (
[32, 96]is 47 registers on 1920 bytesagainst
fused's 128 on 1536). They did not fit the band; ptxas left it inlocal memory and streamed it.
So #31's materialization model half-survives: it is real and
one-directional — rebinding is at least as expensive as the in-place spelling
of the same ops at every rung above 48 values a thread, and dearer than the
fused one everywhere but
[16, 64], where it wins by one register. What it doesnot do is order the table. Whether the band reaches registers at all switches
on shape, and where the two disagree the shape effect is bigger.
The practical rule is intact but narrower, and
examples/README.mdnow says so:at the shape flash actually uses it is still worth 84 registers, but it is
advice about a shape and not a law. Whether a spelling that trades 220
registers for a streamed local-memory band is faster is not a question
ptxas -vcan answer — a frame is a size, not a traffic count — and nothinghere times it.
Determinism control
modal run modal_app.py::regcount --determinismthrows the PTX away, cleans theharness crate, rebuilds and asserts an identical table — raising with a
per-kernel diff if not. Ran it: identical, 130 kernels, every counter. Three
independent runs in this branch also agree kernel-for-kernel, including one on
the committed tree.
Reporting rungs that do not build
The ladder is written out a second time in
modal_app.py. It has to be:ptxasprices the PTX that exists, so a rung dropped from
ladder!is invisible to atable derived from the PTX alone. Any intended rung with no PTX prints as
not built, by name. No rung was dropped — all 70 build.Build cost
::regcountwall time, same machine, same day: 52s before, 1m36s–1m51safter, for 70 more kernels of NVVM codegen.
--determinismis ~2m45s since itbuilds the harness crate twice. That is not painful, so there is no
--quicksubset mode: a sweep you have to opt into is a sweep nobody runs, and the
finding above only exists because every rung was on by default.
Also
lane_probe's recorded table was stale in three cells — it claimed 198 regsat
128 hoistedand 72/256 at32 hoisted, and this tree measures 168 and56/384. Re-measured, with the part of Settle the lane-passing convention before the op set grows #27's finding the drift costs written
down: hoisting the lane is free at 128 now, so that trade is visible only at
32. Only the
regcountcolumns are re-measured; Settle the lane-passing convention before the op set grows #27's PTX instruction countsare left as its own and flagged as not re-checked.
GAPS.md§6 gains the sweep as an infrastructure entry, with what is stillsampled (one op, two widths of row sweep, no shared/MMA path) stated.
README.mddescribes the ladder where it describesregcount.Checks run
modal run modal_app.py::regcount— passes, table abovemodal run modal_app.py::regcount --determinism— passesmodal run modal_app.py::build— passes (host clippy + tests +cargo doc --features host -D warnings,device-testsclippy,sm_100adevice build,examples clippy and device build)
fmt --checkon all three crates,clippy --all-targets --locked -D warnings,cargo test --locked(65 pass),cargo docwith-D warnings,cargo metadata --lockedon all three cratesCloses #60