Back to the example's page

flash_attention_fp32

The flash_attention kernel, as the compiler built it for sm_75 at commit 5a313cd: BC = 16, BR = 16, D = 64, Nk = 64, Nq = 64; a grid of 4 CTAs, 128 threads each.

// FlashAttention-2 in fp32: https://tridao.me/publications/flash2/flash2.pdf
@tensorcore
@launch(128)
@autotune(D in [64], BR in [4, 128], BC in [4, 128])
@aligned(Nq = BR, Nk = BC)
kernel flash_attention(Q: tensor<f32>[Nq, D],
K: tensor<f32>[Nk, D],
V: tensor<f32>[Nk, D],
O: tensor<f32>[Nq, D],
scale: f32) {
let pid = ctx.block.x
let row = pid * BR
let q = Q[row :+ BR, :]
var acc: tile<f32>[BR, D] = 0.0 // unnormalized output sum p*v
var m: tile<f32>[BR, 1] = -1.0e30 // running row max, from a stand-in for -inf
var l: tile<f32>[BR, 1] = 0.0 // running denominator sum p
for kt in range(0, Nk, BC) {
let k = K[kt :+ BC, :]
let v = V[kt :+ BC, :]
var s: tile<f32>[BR, BC] = dot_t(q, k) // [BR, BC] = q @ k.T
s = s * scale
// Online softmax update.
var mnew: tile<f32>[BR, 1] = rowmax(s)
mnew = tmax(m, mnew)
s = exp(s - mnew) // unnormalized probabilities
var corr: tile<f32>[BR, 1] = exp(m - mnew) // rescales the carried state
l = l * corr
l += rowsum(s)
acc = acc * corr // rescale carried output
acc += dot(s, v) // add this block's contribution
m = mnew
}
acc = acc / l
O[row :+ BR, :] = acc
}

examples/flash_attention_fp32.ph

The flash_attention kernel, stepped through as the compiler built it for sm_75A 4 grid of CTAs with 128 threads each. The drawing shows the tensors Q, K, V, O in global memory with the picked CTA's current boxes, shared memory as a byte ruler of 13440 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

Keys: left and right arrows step, space plays and pauses; play runs on from the last step to the first. The address of this page keeps the step, the CTA and the zoom.