Skip to content

Sweep the register ladder instead of measuring two widths (#60) - #62

Merged
feitreim merged 1 commit into
mainfrom
register-ladder-sweep
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
register-ladder-sweep

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Two widths was never a budget decision — ptxas is a host compiler and a width
costs 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 the
shape 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) on
a crate that is already nightly-pinned to the backend's toolchain.

The macro-generated kernels are not registered with #[cuda_module] (an
attribute macro cannot see through a macro_rules! invocation), which is
exactly 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, so
the [32, 128] row reproduces the standing two-width table exactly — which is
the sweep's calibration. The fifth is new: the accumulate written in place too,
which nothing in scalar_map_probe does.

Registers / spill store bytes:

 shape        per thread         fused        assign    open_coded       rebound  all_in_place
 [32,  16]            16          32/0          32/0          32/0          32/0          32/0
 [32,  32]            32          48/0          48/0          48/0          48/0          48/0
 [32,  48]            48          72/0          72/0          72/0          72/0          72/0
 [32,  64]            64          94/0          96/0          96/0          96/0          39/0
 [32,  96]            96         128/0          47/0          47/0         251/0          32/0
 [32, 128]           128         168/0         252/0         252/0        255/60          32/0
 [32, 192]           192       255/900      255/1012      255/1012      255/1096         251/0
 [32, 256]           256      255/1704       255/976       255/976      255/2672         255/0
 [16,  64]            32          56/0          56/0          56/0          55/0          56/0
 [48,  64]            96         128/0          40/0          40/0         202/0          32/0
 [64,  64]           128         162/0          32/0          32/0         168/0          32/0
 [16, 128]            64          96/0         127/0         127/0         105/0          32/0
 [48, 128]           192       255/836       255/996       255/996      255/1868         168/0
 [64, 128]           256      255/1352      255/1584      255/1584      255/2904          96/0

regcount prints the stack frames as a second table beside this one, because a
rung 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:

   spelling      first spill                 first 255 regs
   fused         [32, 192] at 192/thread     [32, 192] at 192/thread
   assign        [32, 192] at 192/thread     [32, 192] at 192/thread
   open_coded    [32, 192] at 192/thread     [32, 192] at 192/thread
   rebound       [32, 128] at 128/thread     [32, 128] at 128/thread
   all_in_place  never on this ladder        [32, 256] at 256/thread

What the sweep found

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,
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] are
both 128 fp32 a thread; assign is 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 fused
one. 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 bytes
against fused's 128 on 1536). They did not fit the band; ptxas left it in
local 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 does
not 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.md now 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 -v can answer — a frame is a size, not a traffic count — and nothing
here times it.

Determinism control

modal run modal_app.py::regcount --determinism throws the PTX away, cleans the
harness 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: ptxas
prices the PTX that exists, so a rung dropped from ladder! is invisible to a
table 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

::regcount wall time, same machine, same day: 52s before, 1m36s–1m51s
after
, for 70 more kernels of NVVM codegen. --determinism is ~2m45s since it
builds the harness crate twice. That is not painful, so there is no --quick
subset 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 regs
    at 128 hoisted and 72/256 at 32 hoisted, and this tree measures 168 and
    56/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 regcount columns are re-measured; Settle the lane-passing convention before the op set grows #27's PTX instruction counts
    are left as its own and flagged as not re-checked.
  • GAPS.md §6 gains the sweep as an infrastructure entry, with what is still
    sampled (one op, two widths of row sweep, no shared/MMA path) stated.
  • Root README.md describes the ladder where it describes regcount.

Checks run

  • modal run modal_app.py::regcount — passes, table above
  • modal run modal_app.py::regcount --determinism — passes
  • modal run modal_app.py::build — passes (host clippy + tests + cargo doc --features host -D warnings, device-tests clippy, sm_100a device build,
    examples clippy and device build)
  • CI's five, locally: fmt --check on all three crates, clippy --all-targets --locked -D warnings, cargo test --locked (65 pass), cargo doc with
    -D warnings, cargo metadata --locked on all three crates
  • Not run: anything on a B200. Nothing here needs one.

Closes #60

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
feitreim force-pushed the register-ladder-sweep branch from 26cfc5b to 3e6cb1e Compare July 28, 2026 03:04
@feitreim
feitreim merged commit 53ba3b1 into main Jul 28, 2026
3 checks passed
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.

regcount measures two widths because probes are hand-written, not because measuring is expensive — sweep the ladder

1 participant