Repository navigation
In-place map variants, and what they are actually worth (#31) - #58
Merged
Merged
Conversation
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
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.
Every by-value map on
RegTile,RegVecandColVecgains an_assigntwin taking&mut self, generated by widening theassignarm ofop_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_rowsis a one-line wrapper overrow_map_assign::<Mul>now. It was hand-written since #5 to dodge the by-valuemul_row, and the bar this issue set for deleting it — that an in-placerow_map_assignmeasure equal at both widths — is met exactly.The claim, measured
modal run modal_app.py::regcount, sm_100a. Before ismainat 3a19783.softmax_probescale_rows, hand-written (before and after)out = out.row_map::<Mul>(f)out.mul_row_assign(f)(new)out = out.add(p)out.add_assign(p)(new)scalar_map_probe, 128 colsbin_map::<Mul>(splat(k))scale(k), reboundadd(block.scale(k)), chained (new)setscale_assign(k)(new)out.scale(k).add(block.scale(k))(new)Every 32-column form of
scalar_map_probeis 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 andmul_row_assigncosts nothing.scale_assignreaches the hand-written in-place floor exactly.Two things the issue did not predict
RegTile::add_assignbuys no registers. It is the nameflash_forwardwas blocked on, butout_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
regcountprobe that touches a map, in both directions —mask_probe_128_causal71 → 32 registers,softmax_probe_32_hand_written64 → 80,lane_probe_128_hoisted168 → 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 byin_place_tile_maps_are_their_by_value_twins, not by a shared body.Examples
flash_forward's error list is nowglobal::store_rows(#11) and nothing else, read offmodal run modal_app.py::gapsrather than off the file. Status table, blocked-on column, per-kernel bullets and the "in-place versus by-value" section ofexamples/README.mdare updated to match.layernorm's list is unchanged and still exactlysync::block_reduce_sum(#3).Verified
regcountwas confirmed deterministic before anything was concluded from it — the same tree measured twice gave the same 45 kernels to the byte.main, including the ones now routed throughrow_map_assign.::build), including two new exhaustive by-value/in-place equivalence tests covering every generated name on all three families.cargo fmt --checkclean in all three crates;clippy --all-targetsadds no warning (exampleshas 7 pre-existing ones inbench.rs/gemm.rs/softmax.rs, untouched here).modal run modal_app.py— 21 of 21 device cases on a B200.Closes #31