FlashAttention-2 changes three things: the computation steps, the parallelization, and the warp partitioning (Section 3 of the paper).
1. Reducing non-matmul operations (Algorithm 1, forward pass)
Suppose Q,K,V∈RN×d are in HBM. Split Q into Tr blocks of Br rows and K,V into Tc blocks of Bc rows, and compute as follows:
- Outer loop: Load the blocks Qi of Q into SRAM one at a time, and initialize Oi=0, ℓi=0, mi=−∞ in SRAM.
- Inner loop: Load the blocks Kj,Vj of K,V into SRAM one at a time.
- Tile matrix product: Compute Si(j)=QiKj⊤ (Br×Bc).
- Update the max and sum: Compute the row-wise max mi(j), P~i(j)=exp(Si(j)−mi(j)), and the sum ℓi(j).
- Update the output: Add P~i(j)Vj to Oi. At this point, do not divide by the sum ℓ. Only correct the scale for the change in the max.
- Divide only once at the end: After the inner loop finishes, divide Oi by ℓi, compute Li=mi+logℓi (the logsumexp), and write Oi and Li to HBM.
Steps 5 and 6 are the following equations (lines 10 and 12 of Algorithm 1).
Oi(j)=diag(emi(j−1)−mi(j))−1Oi(j−1)+P~i(j)Vj,Oi=diag(ℓi(Tc))−1Oi(Tc)
FlashAttention rescaled the output by ℓ for every tile. Division is a non-matmul operation, much slower on a GPU than matrix multiplication. Dividing only once at the end reduces how often it is done. In addition, the statistics saved for the backward pass were reduced from two, the max m and the sum ℓ, to one, the logsumexp L. In the backward pass, the softmax can be recomputed as P=exp(S−L).
Causal masking: Because the computation is split into blocks, blocks whose column indices are all larger than their row indices (about half of them for long sequences) are skipped entirely. Only one block per row, the one on the diagonal, needs the mask applied. According to the paper, this is 1.7 to 1.8 times faster than attention without a mask.
2. Parallelizing along the sequence dimension too
FlashAttention assigned one head to one thread block (a unit of work running on a GPU streaming multiprocessor, or SM) and parallelized only over batch and heads. An A100 has 108 SMs, so with 80 or more batch × head combinations the GPU is almost fully used, but with long sequences and small batches, some SMs sit idle.
Because FlashAttention-2 makes the outer loop run over row blocks of Q, row blocks are independent of each other, and each can be assigned to a separate thread block (Figure 2 left of the paper). In the backward pass, each column block (Kj,Vj) is assigned to a thread block, and only the update of dQ, which is shared across blocks, is combined with atomic adds (Figure 2 right).
3. Changing the warp partitioning
Within one thread block, 4 to 8 warps (groups of 32 threads) split the work. FlashAttention split K,V across warps and shared Q among all warps (what the paper calls the "split-K" scheme). In this scheme, after each warp computes part of QK⊤ and its product with part of V, the results must be written to shared memory, synchronized, and summed.
FlashAttention-2 does the opposite: it splits Q across warps and shares K,V among all warps (Figure 3 of the paper). Each warp can compute the output for its own rows all the way to the end, so no communication between warps is needed.