Skip to content

col_map_assign reads its operand SLOTS × VALUES times where row_map_assign reads it SLOTS times, and a transposed attention band pays for it #196

Closed

Description

@feitreim

The asymmetry

RegTile::row_map / row_map_assign hoist the operand read out of the inner loop, and the doc says why:

One row's factor is read once and spent before the next row's is formed, so a RegVec operand costs one live register rather than a second band.

pub fn row_map_assign<Op: BinaryOp>(&mut self, rows: RegVec<M, L>) {
    let mut slot = 0;
    while slot < L::SLOTS {
        let row = rows.get(slot);          // <- hoisted, L::SLOTS reads
        let mut value = 0;
        while value < L::VALUES { ... Op::apply(self.get(slot, value), row) ... }
        slot += 1;
    }
}

col_map / col_map_assign are documented as "the column mirror" but are not written as one — the operand read sits in the inner loop, so it runs L::SLOTS × L::VALUES times instead of L::VALUES:

pub fn col_map_assign<Op: BinaryOp>(&mut self, cols: ColVec<N, L>) {
    let mut slot = 0;
    while slot < L::SLOTS {
        let mut value = 0;
        while value < L::VALUES {
            self.set(slot, value, Op::apply(self.get(slot, value), cols.get(value)));  // <- L::SLOTS x L::VALUES
            value += 1;
        }
        slot += 1;
    }
}

For a RegTile<32, 16, BaseLdtm> chunk that is 16 operand reads against 4. ColVec::get is L::get_value(&self.0, value), an index into [f32; N/4] with a loop variable, and these maps are the ones #166 measured worse unrolled — so the index is a real one and the array is a candidate for the local depot rather than a register.

What it appears to cost

oxide-train's flash_backward_kv (the key-parallel attention backward, dK/dV) and flash_backward_q (query-parallel, dQ) run the same register pass on the same shapes — mask, subtract a saved statistic, exp2, subtract a second statistic, multiply, scale, over a [32, 16] fp32 chunk. The only difference in the arithmetic is that kernel B's band is transposed, so its two subtracts are sub_col_assign where kernel A's are sub_row_assign.

Kernel B's pass, clocked per phase on a B200 at [32,1024,24,128] (oxide-train#94's phase_probe, per key-tile visit):

TREAD (tcgen05.ld) ARITH STORE (store_tile)
kernel A (sub_row_assign) 254 1 252 318
kernel B (sub_col_assign) 241 2 660 449

Kernel B additionally does a second swizzled store_tile per chunk (it stores Pᵀ as well as dSᵀ), and that shows up where it should — STORE +131. TREAD is flat. The ~1 000 extra ticks a visit are in the arithmetic, on identical operation counts, and this loop-order difference is the only candidate I can see in the library. It is ~25% of that kernel's pass column.

The change

The mirror of row_map_assign:

pub fn col_map_assign<Op: BinaryOp>(&mut self, cols: ColVec<N, L>) {
    let mut value = 0;
    while value < L::VALUES {
        let col = cols.get(value);
        let mut slot = 0;
        while slot < L::SLOTS {
            self.set(slot, value, Op::apply(self.get(slot, value), col));
            slot += 1;
        }
        value += 1;
    }
}

and the same in col_map. Every element sees the same operand and the same Op, and the operations are elementwise and independent, so the result is bit-identical — only the order the tile's storage is walked in changes, and RegTile's storage is [[f32; VALUES]; SLOTS] either way.

What is not established

I have not measured this change — the number above is the oxide-side symptom, not an attribution. What would settle it is a device-tests cadence pair (one warp, one CTA, a chained col_map_assign against a chained row_map_assign on the same [32, 16] shape) or a PTX diff showing the ld.local count fall. Filing it with the evidence I have rather than sitting on it; happy to take the PR if the shape above is the one you want.

Related: #166 (the maps are the walks that measured worse unrolled, which is why an indexed operand read inside them is not free).

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