Skip to content

@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.

@padstage

It takes no arguments. See the Attributes table in the spec.

Only staging tiles, and only those whose rows would collide:

  • The tile is a copy of a tensor slice: var t = <tensor slice>. A let view of a tensor that a dot reads 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.

@padstage
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
}

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
}

phobos-lang/examples/stage_rows.phOpen full view

The stage kernel, stepped through as the compiler built it for sm_75A 1 grid of CTAs with 256 threads each. The drawing shows the tensors K, O in global memory with the picked CTA's current boxes, shared memory as a byte ruler of 2048 bytes with each buffer at its planned offset, the barriers kept and elided, and how the CTA's threads and warp 0's lanes take part. The readout below describes each step.

CTA 0
  • 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.
@padstage
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
}

phobos-lang/examples/stage_rows.ph@padstage addedOpen full view

The stage kernel with @padstage added, stepped through as the compiler built it for sm_75A 1 grid of CTAs with 256 threads each. The drawing shows the tensors K, O in global memory with the picked CTA's current boxes, shared memory as a byte ruler of 2176 bytes with each buffer at its planned offset, the barriers kept and elided, and how the CTA's threads and warp 0's lanes take part. The readout below describes each step.

CTA 0
  • global memory
  • shared memory
  • threads and lanes
  • extra 128-byte lines, masked elements

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.