Skip to content

In-place map variants, and what they are actually worth (#31) - #58

Merged
feitreim merged 1 commit into
mainfrom
issue-31-in-place-maps
Jul 28, 2026
Merged

feitreim merged 1 commit into
mainfrom
issue-31-in-place-maps

Conversation

@feitreim

Copy link
Copy Markdown
Owner

Every by-value map on RegTile, RegVec and ColVec gains an _assign twin taking &mut self, generated by widening the assign arm of op_methods! into four arms — assign, scalar_assign, row_assign, col_assign — rather than hand-writing forms. The generic maps come in both spellings too: unary_map_assign, bin_map_assign, scalar_map_assign, ternary_map_assign, row_map_assign, col_map_assign.

RegTile::scale_rows is a one-line wrapper over row_map_assign::<Mul> now. It was hand-written since #5 to dodge the by-value mul_row, and the bar this issue set for deleting it — that an in-place row_map_assign measure equal at both widths — is met exactly.

The claim, measured

modal run modal_app.py::regcount, sm_100a. Before is main at 3a19783.

softmax_probe 32 cols 128 cols
scale_rows, hand-written (before and after) 64 regs / 256 B stack 168 regs / 3072 B
out = out.row_map::<Mul>(f) 64 255
out.mul_row_assign(f) (new) 64 168 / 3072 B
out = out.add(p) 64 168 / 3072 B
out.add_assign(p) (new) 64 168 / 2560 B
scalar_map_probe, 128 cols regs spill stack
bin_map::<Mul>(splat(k)) 255 124 B 2648
scale(k), rebound 255 60 B 2624
add(block.scale(k)), chained (new) 255 108 B 2672
in place, through set 252 — 2560
scale_assign(k) (new) 252 — 2560
out.scale(k).add(block.scale(k)) (new) 168 — 2560

Every 32-column form of scalar_map_probe is 48 registers, no spill, in every spelling — the 32-wide probe is insensitive here, as the issue warned.

The headline reproduces and closes: row_map::<Mul> costs 87 registers/thread at 128 columns and mul_row_assign costs nothing. scale_assign reaches the hand-written in-place floor exactly.

Two things the issue did not predict

RegTile::add_assign buys no registers. It is the name flash_forward was blocked on, but out_acc = out_acc.add(p) rebinds a dead input, so both spellings are 168 registers. What the in-place form buys there is 512 bytes of stack frame and a line that reads like what it does. This is #38's prediction holding.

Peak liveness does not order the spellings. #38's chained form is in the tree now (scalar_map_probe_*_chained) and reproduces its spill — 108 B here, against the 84 B recorded from the uncommitted probe. But the form one step further along the same axis, out_acc.scale(k).add(block.scale(k)) with nothing rebound at all, is the cheapest form in the table by 84 registers and the only one under the cliff. What orders all six is how many whole bands have to be materialized between statements: one expression fuses into a single pass and materializes none; the in-place forms materialize none but make one pass per op; every rebinding form materializes one band per statement. The rule that falls out is say the whole step in one expression where you can, and write it in place where you cannot — which is exactly the accumulator case.

The by-value maps keep their own loop bodies

Routing them through the in-place twins on a copy is the obvious factoring and it is not free: it moved every regcount probe that touches a map, in both directions — mask_probe_128_causal 71 → 32 registers, softmax_probe_32_hand_written 64 → 80, lane_probe_128_hoisted 168 → 128 registers while its stack frame grew 512 bytes. Same reason the reductions spell their folds out. That variant was measured and discarded; the duplication is held honest by in_place_tile_maps_are_their_by_value_twins, not by a shared body.

Examples

flash_forward's error list is now global::store_rows (#11) and nothing else, read off modal run modal_app.py::gaps rather than off the file. Status table, blocked-on column, per-kernel bullets and the "in-place versus by-value" section of examples/README.md are updated to match. layernorm's list is unchanged and still exactly sync::block_reduce_sum (#3).

Verified

  • regcount was confirmed deterministic before anything was concluded from it — the same tree measured twice gave the same 45 kernels to the byte.
  • All 45 pre-existing kernels are byte-identical to main, including the ones now routed through row_map_assign.
  • 71 host tests (::build), including two new exhaustive by-value/in-place equivalence tests covering every generated name on all three families.
  • cargo fmt --check clean in all three crates; clippy --all-targets adds no warning (examples has 7 pre-existing ones in bench.rs/gemm.rs/softmax.rs, untouched here).
  • modal run modal_app.py — 21 of 21 device cases on a B200.

Closes #31

Every by-value map on `RegTile`, `RegVec` and `ColVec` gains an `_assign`
twin taking `&mut self`, generated by widening the `assign` arm of
`op_methods!` into four arms — binary, scalar, row and col — rather than
hand-writing forms. The generic maps come in both spellings too
(`unary_map_assign`, `bin_map_assign`, `scalar_map_assign`,
`ternary_map_assign`, `row_map_assign`, `col_map_assign`).

`scale_rows` is a one-line wrapper over `row_map_assign::<Mul>` now. It was
hand-written since #5 to dodge the by-value `mul_row`, and the bar the issue
set for deleting it — that the in-place map measure equal at both widths —
is met exactly: 64 registers at 32 columns and 168 at 128, same spill, same
stack frame, byte for byte with the loop it replaces.

The issue's headline reproduces and closes. `row_map::<Mul>` costs
168 -> 255 registers/thread at 128 columns; `mul_row_assign` costs nothing.
`scale_assign` reaches the hand-written in-place floor exactly (252 regs, no
spill, against 255/60 B for by-value `scale`).

Two findings the issue did not predict, both now standing forms in
`scalar_map_probe` and `softmax_probe`:

- `RegTile::add_assign` — the name `flash_forward` was blocked on — buys no
  registers over `out_acc = out_acc.add(p)`, because that call site rebinds a
  dead input. It buys 512 bytes of stack and a line that says what it means.
- Peak liveness does not order the spellings. `out_acc.add(block.scale(k))`
  reproduces #38's spill (108 B here) as predicted, but
  `out_acc.scale(k).add(block.scale(k))` — strictly more chained, and so the
  worst form under that reading — is the cheapest in the table at 168
  registers and no spill, 84 below the in-place forms. What orders all six is
  how many whole bands are materialized between statements.

The by-value maps keep their own loop bodies rather than delegating to the
in-place twins on a copy. That factoring is not free: it moved every probe in
`regcount` that touches a map, in both directions (`mask_probe_128_causal`
71 -> 32, `softmax_probe_32_hand_written` 64 -> 80, `lane_probe_128_hoisted`
168 -> 128 registers with 512 more bytes of stack). Same reason the reductions
spell their folds out. The duplication is held honest by tests, not by a
shared body.

Verified: all 45 pre-existing `regcount` kernels are byte-identical to `main`
(`regcount` was confirmed deterministic first — the same tree twice gave the
same table). 71 host tests, `cargo fmt --check` and `clippy --all-targets` in
all three crates, `::build`, and 21 of 21 device cases on a B200.

Closes #31
@feitreim
feitreim merged commit f3abba0 into main Jul 28, 2026
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.

The map mechanism needs in-place variants: by-value costs 87 registers at 128 columns

1 participant