Expand description
Flash Attention 2 — forward and backward kernels.
Layout: all tensors are stored as [BATCH * N_HEADS, N_CTX, HEAD_DIM]
row-major (contiguous). The caller is responsible for reshaping
[B, H, N, D] PyTorch tensors to this flat 3-D layout before calling.
Algorithm: each CTA processes one (batch, head, q_row) triple.
The kernel iterates over all N_CTX_K key/value rows with the online
softmax recurrence (Flash Attention paper, Dao et al. 2022/2023), so
the full N_CTX_Q × N_CTX_K attention matrix is never materialised.
Memory is O(N_CTX × HEAD_DIM) per CTA rather than O(N_CTX²).
HEAD_DIM must be a power of two and is a compile-time const so the inner vector loads are always fully unmasked.
Structs§
- Flash
Attention2 Backward Dkv - Flash Attention 2 backward: computes
dKanddVfor one key row. - Flash
Attention2 Backward Dq - Flash Attention 2 backward: computes
dQ. - Flash
Attention2 Forward - Flash Attention 2 forward pass.
- Flash
Attention2 Op - Bundles the forward and backward Flash Attention 2 kernels for a given head dimension.
Functions§
- flash_
attention2_ backward_ dkv - Flash Attention 2 backward: computes
dKanddVfor one key row. - flash_
attention2_ backward_ dq - Flash Attention 2 backward: computes
dQ. - flash_
attention2_ forward - Flash Attention 2 forward pass.