Skip to content

RegTile::mask is a rolled walk #180 did not reach: a 400 B frame and 27% of a kernel, from one right_fill #184

Closed

Description

@feitreim

#180 unrolled every walk in global.rs, ldst.rs and tmem.rs. It did not touch src/reg.rs, which has no __unroll_config anywhere — and one walk in there is still costing a real kernel a .local frame.

The walk

RegTile::mask (src/reg.rs:1255) is the rolled shape #180 was written against, unfixed:

pub fn mask(&mut self, lane: u32, fill: f32, keep: impl Fn(i32, i32) -> bool) {
    let mut slot = 0;
    while slot < L::SLOTS {          // no `const { }`, no `__unroll_config::<0>()`
        let row = L::row_of(lane, slot) as i32;
        let mut value = 0;
        while value < L::VALUES {
            let column = L::col_of(lane, value) as i32;
            if !keep(row, column) {
                self.set(slot, value, fill);   // runtime-indexed write
            }
            value += 1;
        }
        slot += 1;
    }
}

Seven public entry points go through it: tril, triu, make_causal, make_causal_t, make_causal_at, make_causal_t_at, right_fill, left_fill, upper_fill, lower_fill.

What it costs, measured

oxide-train's fused_classifier_forward_tile_bf16 is a warp-per-band softmax over a RegTile<16, 64, BaseLdtm> of packed-bf16 logits. Its only use of mask is one right_fill on the vocabulary's ragged last chunk — one chunk in 786. Against the c648c67 pin (i.e. with #180 in), on a B200:

.visible .entry fused_classifier_forward_tile_bf16(
        .local .align 4 .b8     __local_depot13[400];

400 B, 108 st.local and 70 ld.local, and the stores are inside the vocabulary loop — the whole 32-register tile is homed on every chunk, not just the masked one, because set under a data-dependent condition keeps the slot array addressable. The PTX names the culprit directly:

$L__BB13_5:                             // %bb5.i  (Parent Loop BB13_2 Depth=1)
        st.local.b32    [%rd6], 0;
        st.local.b32    [%rd6+4], 1;
        st.local.b32    [%rd6+8], 8;
        st.local.b32    [%rd6+12], 9;
        and.b64         %rd76, %rd99, 3;
        ...
        ld.local.b32    %r250, [%rd78];
        ...
// %bb.6: // %_RINvXsv_...BaseLdtmINtB6_9RowLayoutKj10_E12get_slot_mutAfB13_...exit.i
        add.s64         %rd23, %rd11, %rd98;
        st.local.b32    [%rd23], -8388608;

Two rolled walks for the price of one: RowLayout's four-entry offset table [0, 1, 8, 9] is re-stored to local memory and read back by a runtime index every iteration, and get_slot_mut writes the fill through a computed local address.

Deleting the single right_fill call from that kernel removes the depot entirely and moves it from 0.442x to 0.563x of the shipped block-per-row kernel (1.4796 ms → 3.3491 ms becomes 2.6287 ms, 740 → 943 GB/s). So the frame is ~27% of that kernel's time.

Honest caveat, because it decides how urgent this is: that kernel loses either way — at 0.563x it is short of CTAs, not of registers, and oxide-train is leaving it flat (feitreim/oxide-train#78). This issue is filed on the library defect, not on a kernel that needs it.

Who else is exposed

gpu/flash-attn's tcgen05 kernels call make_causal_at / make_causal_t_at in five places and do carry frames — flash_attention_backward_kv_tiled 1280 B, flash_attention_backward_q_tiled 640 B, flash_attention_forward_tiled 640 B. Those look like a different problem, not this one: their store/load ratio is 340/48, 172/32, 188/48, which is spilling (#181's band-that-does-not-fit), where this walk's signature is the balanced 108/70 with get_slot_mut in the block names. Worth confirming separately rather than assuming this fix moves them.

Fix

The #180 pattern, applied to mask: while slot < const { L::SLOTS } with __unroll_config::<0>() at the top of each level, so every set resolves to a constant slot and the tile stays in registers. Same question for the rest of reg.rs — the maps and reductions appear to unroll on their own today, since the RMSNorm tile kernels that lean on them hard come out with a zero frame, but nothing in the source makes them.

Activity

  1. feitreim commented on Aug 4, 2026

    @feitreim
    OwnerAuthor

    I wrote the fix and measured it, and it is not the clean win the issue text implies — recording it here rather than opening a PR, because the result wants a judgement call I should not make unilaterally.

    The patch

    The #180 pattern, applied to mask and nothing else:

         pub fn mask(&mut self, lane: u32, fill: f32, keep: impl Fn(i32, i32) -> bool) {
             let mut slot = 0;
    -        while slot < L::SLOTS {
    +        while slot < const { L::SLOTS } {
    +            __unroll_config::<0>();
                 let row = L::row_of(lane, slot) as i32;
                 let mut value = 0;
    -            while value < L::VALUES {
    +            while value < const { L::VALUES } {
    +                __unroll_config::<0>();

    scripts/fmt --check clean, cargo check clean.

    modal run modal_app.py::regcount, before and after

    examples/flash_forward is the only in-repo consumer of mask and the only shipped kernel carrying local memory:

    regs stack st.local ld.local
    c648c67 165 1824 774 451
    with the patch 255 1568 690 383

    The frame drops 256 B and the local traffic ~11%, and the registers go to the cap.

    Which is the fix working, not failing: mask was hiding a band that does not fit behind a .local depot, and unrolling it makes the tile genuinely live in registers, so flash_forward now reports what it actually costs. That is #181's finding arriving at a second kernel, and #181's remedy — stream the band in chunks that fit — is what would collect it. But it is a shipped example crossing an occupancy step, and it is not obviously a net improvement on its own.

    Why I am not pushing it

    The kernel that found this does not benefit. oxide-train's tile classifier is 0.442x with the frame and 0.563x without it, so it stays flat either way (feitreim/oxide-train#78) — I have no consumer that this fix makes faster, and one shipped example that it moves to 255 registers. By this repo's own bar that is a data point, not a change.

    The patch is two lines plus an import if you want it; the call I would not make without you is whether flash_forward should be streamed first so the unroll lands on a band that fits.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions