Skip to content

store_rows writes one f32 at a time, and the layout pairs every other value #91

Closed

Description

@feitreim

#86 measured the GEMM's item boundary at 27–36% of the 8192³ launch, 28–37 µs
a tile, and named its parts: the accumulator drain, LDTM, and store_rows'
scattered scalar fp32 stores. #15's lcsf overlaps that cost; this issue
shrinks it.
They are complementary and touch different code.

global::store_rows writes one f32 per instruction:

let start = dest.at(row + L::row_of(lane, slot), column);
let mut value = 0usize;
while value < L::VALUES {
    start.add(L::col_of(lane, value) as usize).write(tile.get(slot, value));
    value += 1;
}

For the GEMM's [128, 128] fp32 accumulator that is SLOTS = 16 by
VALUES = 32 — 512 scalar stores a lane, 16384 a CTA.

It is vectorizable, and the layout says so

BaseLdtm::column(lane, value) = 2*(lane%4) + 16*(value/4) + [0,1,8,9][value%4]

So consecutive values pair up: value 0 and 1 land on columns c and c+1,
values 2 and 3 on c+8 and c+9. Every even/odd value pair is contiguous,
which is exactly a st.global.v2.f32 — halving the store count with no change
to the layout or to what lands where.

Not v4: the [0, 1, 8, 9] pattern breaks after two. Do not force it.

Coalescing looks favourable too and is worth confirming rather than assuming:
2*(lane%4) puts lanes 0–3 on columns 0, 2, 4, 6, so four lanes cover columns
0–7 contiguously and a warp covers a wide run.

Scope

  1. Vectorize store_rows to v2 where the layout permits. It is a
    FragmentLayout property, not a BaseLdtm one, so express the pairing
    through the trait rather than hardcoding the current impl's formula — RegTile's shape set is closed to out-of-tree kernels #23
    opened the shape set and a second layout must not silently get wrong stores.
    If the trait cannot express it, say so and propose what it would take.
  2. Do the same for load_rows if the same pairing holds there.
  3. Measure, do not assume. ptxas reports what was allocated, never what it
    cost (Is a streamed local-memory band actually slower? ptxas cannot say, and the ladder now makes the question concrete #63), and the register column has ordered time backwards four times
    here (softmax is latency-bound, not bandwidth-bound: 362 GB/s ceiling and a flat 51us floor #47, Is a streamed local-memory band actually slower? ptxas cannot say, and the ladder now makes the question concrete #63, layernorm_rows sits at 255 registers with spills and gets 2 blocks/SM where softmax gets 6 #67, softmax is now the slow kernel by 5.8x, and its CHUNK rung is only a quarter of it #76). Halving the instruction count is an argument; the
    clock decides.

The alternative worth pricing, not necessarily building

TMEM → shared → TMA store, which is the path softmax already uses (#9,
tma_store / tma_store_commit / tma_store_wait). It replaces the scattered
stores with one engine-driven bulk transfer, and tma_store_wait_read releases
the shared buffer as soon as the engine has read it — which is what would let an
epilogue overlap the next tile's first K loads, and is the same mechanism #15
needs. Bigger change, bigger prize, and it costs shared memory the GEMM's plan
may not have (#84: residency is 3 CTAs/SM, capped by exactly that plan).

Price both. Ship the cheap one if the expensive one does not pay.

Acceptance

gemm exact at every size before any timing (modal run modal_app.py::examples),
before/after on ::bench at 8192³ and 16384³, and the boundary cost
re-derived from the depth sweep #86 added — if this shrinks the fixed cost, the
K-sweep intercept moves and that is the cleanest evidence available. Registers,
spill and frame reported but not treated as the result.

Related: #86 (the boundary measurement and the depth sweep), #15 (lcsf, which
overlaps rather than shrinks), #84 (the shared-memory budget the TMA route would
spend), #9, #63.

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:memory

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions