@padstage
Shared memory is split into banks, and two threads that hit the same bank in
the same access wait for each other. The bank of an address repeats every 128
bytes, so in a tile whose rows are a multiple of 128 bytes long, the same
column of every row lies in the same bank. Reading a column, as a dot or a
transpose does, then serializes.
@padstage adds a few unused elements to the end of each row of such a tile.
The rows then start in different banks, and a column spreads across them.
@padstageIt takes no arguments. See the Attributes table in the spec.
Which tiles it reaches
Section titled “Which tiles it reaches”Only staging tiles, and only those whose rows would collide:
- The tile is a copy of a tensor slice:
var t = <tensor slice>. Aletview of a tensor that adotreads more than once is staged by the compiler anyway, and that staging tile counts too. - It has more than one row. A single row has no other row to collide with.
- Its row pitch, the last dimension times the element size, is a static multiple of 128 bytes.
Every other tile is unchanged: a tile filled from an expression, an accumulator, or a staging tile whose rows are, say, 64 bytes.
The padding is 8 elements per row, or 16 bytes for an element narrower than 2 bytes, so rows stay 16-byte aligned. It is invisible to the source: the tile keeps its shape, and only its allocation and its row stride grow.
A minimal kernel
Section titled “A minimal kernel”@padstagekernel stage(K: tensor<f16>[64, 128], O: tensor<f16>[8, 128]) { var k = K[0 :+ 8, 0 :+ 128] O[0 :+ 8, 0 :+ 128] = k}k is [8, 128] f16, 256 bytes per row, a multiple of 128. Under
@padstage its allocation is [8, 136]. With rows of 32 f16 (64 bytes)
instead, the tile would stay as declared.
The pair below is that kernel without and with @padstage. k is a tile of
8 x 128 f16, 2048 bytes. Its rows are a multiple of 128
bytes long, so under @padstage the same tile takes 2176 bytes. Step to
the copy on either side to see the stage filled.
without @padstage
// Eight f16 rows of 256 bytes each, staged into shared memory. Under// @padstage each row is padded by eight elements.kernel stage(K: tensor<f16>[64, 128], O: tensor<f16>[8, 128]) { var k = K[0 :+ 8, 0 :+ 128] O[0 :+ 8, 0 :+ 128] = k}- global memory
- shared memory
- threads and lanes
- extra 128-byte lines, masked elements
with @padstage
// Eight f16 rows of 256 bytes each, staged into shared memory. Under// @padstage each row is padded by eight elements.@padstagekernel stage(K: tensor<f16>[64, 128], O: tensor<f16>[8, 128]) { var k = K[0 :+ 8, 0 :+ 128] O[0 :+ 8, 0 :+ 128] = k}phobos-lang/examples/stage_rows.ph@padstage addedOpen full view
- global memory
- shared memory
- threads and lanes
- extra 128-byte lines, masked elements
Cost and interactions
Section titled “Cost and interactions”The padding costs shared memory: 8 * rows elements per padded tile. A
kernel at the edge of the 48 KB budget can fail to fit, or lose double
buffering under @pipeline, once its stages
grow.
A kernel benefits when it reads its staged tiles by column. Attention, which
contracts a staged key tile with dot_t, is the typical case: the GGUF
backend’s attention kernel carries @padstage. A kernel that only streams
rows through elementwise sweeps reads every row whole and gains nothing.