Understanding Flash Attention
Understanding Flash Attention
A step-by-step path from standard attention to SOTA
FlashAttention has had an enormous impact on efficient transformer training and inference. TODO improve
In this blog, I will walk through the fundamentals operations underpinning the attention mechanism, followed by both an algorithmic and systems perspective of the FlashAttention family of algorithms, accompanied by somewhat performant ThunderKittens kernel code. Finally, I will discuss the most recent iterations of FlashAttention (briefly FA-3, mainly FA-4).
The complete Python reference and ThunderKittens kernels live in the companion repository 12.
Part 1: Understanding Attention
The goal of this section is to explain attention enough to motivate the creation of FA in the first place. The goal is NOT to explain why attention works.
At its core, attention is composed of 2 types of operations: matrix multiplies and softmax. First, we start with 3 matrices, known as the query, key, and value matrices:
\[Q, K, V \in \mathbb{R}^{N \times d}\]Here, N is the sequence length and d is the head dimension (unimportant for now). The first operation we perform is a scaled
Then, our second operation is the softmax. For those unfamiliar, softmax turns each row of a matrix into weights that sum to one. We apply this operation to the score matrix.
\[P_{ij} = \operatorname{softmax}(S_i)_j = \frac{e^{S_{ij}}}{\sum_k e^{S_{ik}}}\]In our third operation, those weights are then applied to the value matrix via a matrix multiply:
\[P = \operatorname{softmax}(S), \qquad O = PV\]That is the complete operation: compare queries with keys, normalize the scores, and use those weights to combine the value rows 1.
In most modern use-cases
Part 2: The FA Algorithm
Before getting technical, I want to talk about the motivation behind an algorithm like FlashAttention. From a high level, the algorithm seems quite simple. Just 4 operations, how hard could it be? The big issue is that both the scores S and softmax output P are N-by-N, where N is the context length. This means that both of these matrices grow quadratically as the context lengthens, which was not sustainable before FA. In the past, attention algorithms involved a lot of per-row reading and writing these matrices to HBM
Additionally, it is important to understand the general design of a GPU and how this algorithm exploited said architecture to make attention up to 7.6x faster and over 10x more memory efficient 2. Think about a GPU like a pyramid. At the base of the pyramid, data moves slowly, but there is a lot of space, whereas at the top things move as quickly as they possibly can but there is very little space, and there are a couple of levels in between.

At the bottom is HBM, or High Bandwidth Memory, with tens to hundreds of gigabytes across the GPU. L2 cache is on the order of tens of megabytes, while each SM
Now, we are ready to discuss FlashAttention! FA computes the same output as naive attention while avoiding the full N-by-N intermediates in HBM. There are two main topics from this paper that I want to discuss : tiling, a computation technique that effectively utilizes the GPU architecture we just discussed, and an online numerically-stable softmax calculation.
First, let’s briefly talk about the tiling. FA did not pioneer tiling by any means; it has been around as a computing technique for a long time, but understanding it will help motivate the problem that softmax poses. Tiling breaks a large matrix operation into small blocks that fit in fast on-chip memory. The GPU loads one block from HBM to registers/ SMEM, computes with it, accumulates the partial result, and then moves to the next block. This is straightforward for matrix multiplication, but softmax is harder because normalizing one block requires information from the rest of its row, hence why prior algorithms were doing per-row computation with a ton of loads and stores.

The way that FA got around this is with their online softmax algorithm. The authors essentially found a way to compute softmax such that whole rows at a time were unnecessary and partial results could be stored similarly to matrix multiplication. The algorithm is as follows:
For each score tile, keep a running row maximum, a normalization term, and an unnormalized output accumulator. The complete update is:
\[\begin{aligned} M_j &= \operatorname{rowmax}(S_j), \\ m_j &= \max(m_{j-1}, M_j), \\ \alpha_j &= e^{m_{j-1}-m_j}, \\ P_j &= e^{S_j-m_j}, \\ \ell_j &= \alpha_j \ell_{j-1} + \operatorname{rowsum}(P_j), \\ O_j &= \alpha_j O_{j-1} + P_jV_j. \end{aligned}\]After the final tile, divide once:
\[O = \frac{O_{\mathrm{final}}}{\ell_{\mathrm{final}}}\]Let’s walk through why this algorithm works so well. There is a lot to unpack, so I will break things down into two questions. First, how does this algorithm even work (i.e. how is it equivalent to the original), and second, why is this such a great improvement?
Let’s begin with the first question. To start, the everything up to computing the score matrix and applying the mask is the same. The only difference then is the softmax and the V mmul.
Now, the second and more important question: why is this algorithm so important?
It’s also important to mention the numerical stability change that was used. Softmax was changed to subtract a running maximum score to prevent overflow:
\[\operatorname{softmax}(S_i)_j = \frac{e^{S_{ij}-m_i}}{\sum_k e^{S_{ik}-m_i}}, \qquad m_i = \max_k S_{ik}\]This is mathematically equivalent to the regular softmax because… TODO
The online-softmax update maps almost directly from the equations into PyTorch:
def online_softmax_block(
m, # Running maximum for each query row
ell, # Running softmax denominator
O_tile, # Running unnormalized output
S_tile, # Scores for the current Q/K tile pair
V_tile, # Values for the current K/V tile
):
# Find this tile's maximum and update the running maximum.
m_block = S_tile.amax(dim=-1)
m_new = torch.maximum(m, m_block)
# Rescale everything accumulated under the previous maximum.
alpha = torch.exp(m - m_new)
ell = ell * alpha
O_tile = O_tile * alpha[..., None]
# Add this tile's contribution to the denominator and output.
P_tile = torch.exp(S_tile - m_new[..., None])
ell = ell + P_tile.sum(dim=-1)
O_tile = O_tile + P_tile @ V_tile
return m_new, ell, O_tile
This is a correctness model, not a performant kernel; the slices express the tiled algorithm, but PyTorch still controls the actual kernel launches and memory movement
Tiling the Computation
With the online-softmax update established, we can trace how FlashAttention moves Q, K, and V tiles through the GPU without materializing the full attention matrix.

Writing the ThunderKittens Kernel
Next, we will translate the tiled algorithm directly into a synchronous ThunderKittens kernel and connect each mathematical object to its representation in code.
Optimizing FlashAttention
Once the baseline implementation is correct, we can measure its bottlenecks and add the scheduling, memory, and parallelism improvements introduced by later versions.
From FlashAttention 3 to FlashAttention 4
Finally, we will look at how FlashAttention adapted to Hopper and Blackwell as the hardware bottlenecks shifted from memory movement toward scheduling and non-matmul operations.
References
- Attention Is All You Need - Vaswani et al., 2017
- FlashAttention - Dao et al., 2022
- ELI5: FlashAttention - Aleksa Gordic, 2023
- Online Normalizer Calculation for Softmax - Milakov and Gimelshein, 2018
- GPUs Go Brrr - Hazy Research, 2024
- FlashAttention-2 - Tri Dao, 2023
- Writing Speed-of-Light Flash Attention for 5090 in CUDA C++ - Thien Tran, 2025
- Tuning Flash Attention for Peak Performance in NVIDIA CUDA Tile - Alessandro Morari, 2026
- FlashAttention-3 - Shah et al., 2024
- FlashAttention-4 - Zadouri et al., 2026
- Attention in transformers, step-by-step - 3Blue1Brown, 2024
- fa_stuff - Python and ThunderKittens implementations