Skip to content

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

Closed

Description

@feitreim

Measured while retiring the hand-written RegVec methods in #5.

RegTile::scale_rows and row_map::<Mul> compute the same thing. At 32 columns
they generate identical code. At 128 columns the generic form costs 168 -> 255
registers/thread.

The cause is the calling convention, not the arithmetic: scale_rows rewrites
its tile in place through &mut self, while row_map takes a tile by value and
returns a new one. At a full [32, 128] band the allocator will not prove the
input dead before the output is live, so both tiles are resident at once. At 32
columns there is enough headroom that it does not matter; at 128 it hits the
ceiling.

This is the one place the #5 mechanism is measurably not free, and it is
load-bearing — [32, 128] is the flash output accumulator's shape, so it is the
shape the library most needs to be good at.

Shape

In-place forms alongside the by-value ones:

pub fn unary_map_assign<Op: UnaryOp, ...>(tile: &mut RegTile<M, N, L>);
pub fn bin_map_assign<Op: BinaryOp, ...>(tile: &mut RegTile<M, N, L>, other: RegTile<M, N, L>);
pub fn row_map_assign<Op: BinaryOp, ...>(tile: &mut RegTile<M, N, L>, vec: RegVec<M, L>);

The assign arm added to op_methods! in #5 (for mul_assign / add_assign)
is the pattern to extend — those already generate in-place methods, so this is
mostly widening the macro rather than new machinery.

scale_rows stays hand-written until an in-place row_map_assign measures
equal, at which point it becomes a one-line wrapper.

Do not take this on faith at other shapes

The 32-wide probes in the regcount harness are insensitive — they did not move
even under a change that cost 87 registers at 128 wide. Any claim that an
in-place form is free must be measured at 128, not at 32.

modal run modal_app.py::regcount gives the table. CPU-only, no GPU needed.

Depends on #5.

Activity

  1. added
    area:compute
    on Jul 27, 2026
  2. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    Acceptance: update examples/ as part of closing this.

    flash_forward and softmax both operate on [32, 128]-class tiles, which is the shape where the by-value form was measured to cost 87 registers/thread.

    The examples crate (#18) exists so that "done" is checkable in the terms a kernel author cares about, rather than as a claim about a primitive nobody has tried to hold. Concretely, on closing this:

    • Delete any workaround or GAP fence in examples/src/ that this issue removes the need for, and replace it with the real API.
    • If a kernel becomes fully unblocked, move it from aspirational to compiles: flip its header, take it out of its feature gate, and confirm modal run modal_app.py::build builds it for sm_100a.
    • Update the status table and the blocked-on lists in examples/README.md.

    If the example still does not compile afterwards, that is a finding worth writing down — say what is still missing rather than leaving the README stale.

  3. feitreim commented on Jul 27, 2026

    @feitreim
    OwnerAuthor

    This issue is scoped around the wrong variable. The cost is peak liveness, not by-value-ness — and that makes the job much narrower.

    Measured in #38, and it reconciles three results that did not fit together under the framing above.

    The evidence

    The cleanest pair holds op, width and by-value-ness fixed and varies only liveness:

    spelling spill at 128 columns
    out_acc.add(block.scale(k)) — chained, both copies live 84 B
    block = block.scale(k) — rebound, input dead at the call 0 B

    Both are by-value. Only one costs anything.

    Why that unifies everything

    By-value-ness is a proxy that correlates, and it breaks in both directions:

    • Over-predicts. block = block.scale(k) is by-value and free, because the old copy is provably dead at the reassignment.
    • Under-predicts. Composed movers: a whole [M, N] band, both memories, both directions #43's composed movers are also by-value and came out 16 registers cheaper than the hand-written loops they replaced (36 vs 52) — because the hand loop kept a band and a fragment live where the composed form keeps one band. Under the old framing that result was inexplicable; under liveness it is the same variable with the sign flipped.

    So #5's row_map::<Mul> cliff (168 -> 255) is a second tile live beside the accumulator it replaces, #38's 60 B is that shape one size smaller, and #43's -16 is the hand-written form being the one holding two things. One phenomenon, three signs.

    What this issue should be

    Not "add in-place variants for every map." It is:

    Add in-place variants where the input is live across the op — and give call sites a criterion for when converting is worth it.

    That is a much shorter list, and it is a property of the call site, not of the API. A by-value map whose input dies at the call needs no in-place twin. scalar_map (#38) belongs inside whatever lands here rather than getting its own form later.

    Caveats to carry, not drop

    1. The 128-column probe sits at the register cliff — all forms land at 252-255, so the register column carries almost no signal there and the spill bytes carry it all. Two earlier probe designs gave non-monotone results at 128 until every form computed the same tile. Do not weight a 128-column register number from that probe beyond "by-value spills, in-place does not."
    2. The 84 B figure is not in the committed tree. It came from an intermediate probe and is reproducible only by re-editing scalar_map_probe. If it should be a standing regression guard it needs a fourth FORM.
    3. This is three data points and a mechanism story, not a measured model. The prediction it implies — a by-value map whose input dies at the call is free — is worth testing directly before this issue is scoped around it. That test is cheap: modal run modal_app.py::regcount, CPU-only, no GPU.

    Suggested first step: run (3). If the prediction holds, the in-place surface needed here may be very small; if it fails, we learn the mechanism is something else before building against it.

  4. feitreim commented on Jul 28, 2026

    @feitreim
    OwnerAuthor

    The premise held; the explanation was wrong twice

    This closed with the API the issue asked for, and the headline claim reproduced: row_map::<Mul> costs 255 registers at 128 columns where scale_rows costs 168. scale_rows is now a one-line wrapper over row_map_assign::<Mul>, which was this issue's own stated bar, and all 45 pre-existing kernels are byte-identical after the change.

    But the model underneath has now been corrected twice, and the second correction is the interesting one.

    1. I first said by-value costs registers. Wrong — Composed movers: a whole [M, N] band, both memories, both directions #43's composed by-value movers were 16 registers cheaper than decomposed ones.
    2. I rewrote this issue around peak liveness, from Scalar-operand maps: scale, shift, and the ops #5 listed but did not ship #38's pair (out.add(b.scale(k)) both live, 84 B spill; b = b.scale(k) rebound, 0). Also wrong.

    The disproof is a form one step further along the liveness axis. out_acc.scale(k).add(block.scale(k)) rebinds nothing and holds the most live at once, so liveness predicts it is the worst. Measured at 128 columns it is the best of six spellings — 168 registers, no spill — 84 below the in-place forms:

    128-column spelling regs spill
    out_acc.scale(k).add(block.scale(k)) 168 —
    scale_assign(k) (in place) 252 —
    in place through set 252 —
    scale(k) rebound 255 60 B
    add(block.scale(k)) chained 255 108 B
    bin_map::<Mul>(splat(k)) 255 124 B

    The variable that orders all six is how many whole bands get materialized between statements. One expression fuses into a single pass and materializes none. An in-place form makes one pass per op and materializes none. Every rebinding form materializes one per statement. Liveness within an expression is free; a band crossing a statement boundary is not.

    Two consequences worth carrying forward

    In-place forms often buy nothing. add_assign — flash's blocker, the thing that motivated filing this — is register-identical to the by-value form at both widths. It buys 512 B of stack and readability. Reported as such rather than dressed up.

    The obvious factoring is actively expensive. Writing each by-value map as let mut out = self; out.X_assign(..); out moved every map-touching probe, in both directions: mask_probe_128_causal 71→32, softmax_probe_32_hand_written 64→80, lane_probe_128_hoisted 168→128 regs but +512 B stack. It was discarded; the by-value bodies stay written out, held honest by equivalence tests rather than by sharing a body. That attribution was only possible because a determinism control (same tree measured twice, identical table) was run first — worth making standard for any regcount claim.

    Corrections to the record

    • The 32-column scalar_map_probe forms are 48 regs / no spill / no stack in every spelling — confirming again that 32-wide probes are insensitive and that both widths must be measured.
    • lane_probe's recorded table is stale in three cells, not one: it claims 198/2816 and 72/256 where main measures 168/2816 and 56/384. Untouched here — that belongs to Settle the lane-passing convention before the op set grows #27.
    • Not measured, and the doc says so: whether ternary_map_assign saves anything at 128 columns. No probe monomorphizes it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compute

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions