# What Is FlashAttention?

> What FlashAttention is, why its first two versions give the same answer as normal attention but run faster on long inputs, and how GPU memory explains it, for your AI interview.

**In short:** FlashAttention is a way to run a Transformer's attention step that moves far less data in GPU memory. It works in small tiles held in fast on-chip memory, and its first two versions give the same result as the usual method.

AI and LLM interview question Q057, Transformer architecture. Advanced, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-flashattention

## Say it in 30 seconds

FlashAttention is a faster way to run attention, the step where a Transformer weighs every token against every other. For its first two versions, the result is the same, so it is exact. The trick is memory. Standard attention writes a huge table of scores to big, slow GPU memory and reads it back. FlashAttention works in small tiles inside fast memory on the chip, so the table never gets written out. Long inputs fit and run faster.

## Say it in 2 minutes

Attention is the step where a Transformer lets each token look at every other token to decide what matters. The maths itself is simple. The memory traffic is the hard part. On a GPU, the big memory sits far from the chip's fast working memory. Standard attention builds a table of every token-to-token score, writes it to the big memory, and reads it back. On long inputs that table grows with the square of the length, so it gets huge.

The first FlashAttention does the same maths in small tiles. It loads one block of queries, keys and values into the fast on-chip memory, scores that block there, and keeps a running total so the softmax stays correct across blocks. The finished output goes back to the big memory. The paper calls this IO-aware, meaning it counts the reads and writes between the two memories. For its first two versions, it is exact, so the answers match standard attention up to small rounding differences.

The gain shows up on long documents. In a made-up example, a contract bundle of 40,000 tokens would need a score table of about 3.2 GB per head per layer the standard way. With tiles, only one block of about 33 KB sits on the chip at a time. FlashAttention-2 changes how the work is split across the GPU, and FlashAttention-3 targets newer Hopper GPUs, such as the H100.

So my answer: it is a memory-aware way to run attention, exact in its first two versions, and a big help for long inputs. I would also check that the model's weights still fit on the GPU, because they take memory too.

## What is FlashAttention, in plain words?

Attention is the step where a Transformer, the design behind today's large language models, lets every token weigh how much every other token matters to it. FlashAttention is a faster way to run that same step on a GPU, the chip that does the heavy maths.

On a GPU, the slow part is often moving data in and out of the big memory. A GPU has a big memory that sits away from the calculating units, and a small, very fast memory right on the chip. Standard attention writes a huge table of token-to-token scores to the big memory, then reads it back. FlashAttention cuts the work into small tiles and finishes each one on the chip, so the big table is never written out.

Think of a legal team checking a stack of contracts. The slow way walks to the archive for every page and walks back. The better way brings one small stack to the desk, checks it fully there, and only then fetches the next stack. The answer is the same, and most of the walking is gone.

- **Attention:** The step where each token decides how much to look at every other token.
- **HBM:** GPU high bandwidth memory: the large main memory, slower to reach.
- **SRAM:** Small memory on the chip itself: very quick to reach, but tiny.
- **Tile:** A small block of the input, handled in one go before the next block.
- **Exact attention:** The same result as standard attention. Every score is still computed.

## How does FlashAttention work?

Attention is the main bottleneck on long inputs, because its runtime and memory both grow with the square of the length. The 2022 paper says Transformers are slow and memory-hungry on long sequences, since the time and memory complexity of self-attention are quadratic in sequence length.

*Diagram, Two ways to run the same attention step: For the first two versions, both paths give the same answer. Standard attention writes the full score table to slow GPU memory and reads it back. FlashAttention computes it in tiles that stay in fast on-chip memory, so the table never goes to slow memory.*
1. The inputs for one layer go to the standard method.
2. The full score table is written to the big, slow memory.
3. The table is read back to produce the output.
4. The same inputs are cut into small tiles.
5. Each tile is scored and finished on the chip.
6. The finished output leaves for the answer.

1. **Cut the inputs into tiles:** Split the queries, keys and values into blocks small enough for the fast on-chip memory.
2. **Score one tile on the chip:** Work out that block's scores there, without writing the full table to the big memory.
3. **Keep a running total:** Track a running maximum and sum, so the softmax (the step that turns scores into weights) stays correct across blocks.
4. **Write the result:** After the last tile, write out the finished output. The big table never existed in slow memory.

## What does it save on a long contract bundle?

Take a made-up bundle of 40,000 tokens, sent to a model that compares clauses across contracts. Here is the memory for the score table in the standard way, and for one tile. The numbers are simplified, but the scale is the point.

```python
# A simplified memory check for one long contract bundle (made-up size).
# Standard attention keeps every token-to-token score in big GPU memory.
BYTES_PER_SCORE = 2      # a 16-bit number takes 2 bytes
TILE = 128               # side of a square tile of scores held on the chip at once (made-up)
BUNDLE = 40_000          # tokens in one contract bundle (made-up)

def full_grid_gb(tokens):
    return tokens * tokens * BYTES_PER_SCORE / 1e9

def one_tile_kb():
    return TILE * TILE * BYTES_PER_SCORE / 1e3

print(f"Full score grid: {full_grid_gb(BUNDLE):.1f} GB per head, per layer")
print(f"One tile on chip: {one_tile_kb():.0f} KB, at any length")
print(f"Half the bundle: {full_grid_gb(BUNDLE // 2):.1f} GB, a quarter of the grid")
```

Output:

```text
Full score grid: 3.2 GB per head, per layer
One tile on chip: 33 KB, at any length
Half the bundle: 0.8 GB, a quarter of the grid
```

The full table grows with the square of the length, so doubling the bundle makes it four times bigger. A tile stays the same size whatever the length. That is the gap FlashAttention is built to close. It does not remove the quadratic work, though: the number of calculations still grows with the square of the length. What it removes is the big table, so the memory need grows in a straight line.

## Does FlashAttention change the answer?

For the first two versions, the answer is no. FlashAttention-1 and FlashAttention-2 run the same attention maths and skip no scores. FlashAttention-3 adds a low-precision FP8 mode for speed. In that mode, its results can differ slightly from standard attention. The first paper's title and abstract call the method exact, and the second version says it has no approximation.

> "We propose FlashAttention, an IO-aware exact attention algorithm that uses tiling to reduce the number of memory reads/writes between GPU high bandwidth memory (HBM) and GPU on-chip SRAM." (FlashAttention paper, 2022)
>
> "FlashAttention exploits the asymmetric GPU memory hierarchy to bring significant memory saving (linear instead of quadratic)" (FlashAttention-2 paper, 2023)

So for the first two versions, an answer that says FlashAttention makes the model worse, or skips parts of attention, has it wrong. The model's output is the same, up to small rounding differences from the arithmetic.

## What changed in FlashAttention-2 and FlashAttention-3?

The second version keeps the same idea and splits the work across the GPU in a better way. The third version targets newer Hopper GPUs (NVIDIA's Hopper line, such as the H100), and it says the older version was not using the chip well.

> "However, it has yet to take advantage of new capabilities present in recent hardware, with FlashAttention-2 achieving only 35% utilization on the H100 GPU." (FlashAttention-3 paper, 2024)

### How would you fit a long bundle on a GPU that runs out of memory?

- **Standard method: The table does not fit.** The score table grows with the square of the length, so the job fails or slows right down.
- **Tiled method: Scores stay on the chip.** Scores are worked out in tiles and the table is never written out. Then check the model's weights still fit.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: attention maths run in tiles, so less data moves in GPU memory.
- [ ] Why moving data is often the slow part.
- [ ] That the first two versions are exact: the output matches standard attention.
- [ ] Tiles and on-chip memory, each in one sentence.
- [ ] It changes speed and memory. The model and its weights stay the same.
- [ ] The versions: FlashAttention-2 and FlashAttention-3, and what each one added.

**Common traps**

- Trap: "FlashAttention is a smaller model that runs faster." Better: It runs attention in a different way. The model and its weights stay the same size.
- Trap: "It skips some scores to save time." Better: It keeps every score, so the first two versions are exact. The change is where the work happens: in tiles on the chip.
- Trap: "Faster means more accurate." Better: Same output in the first two versions, less memory traffic. The gain is speed and memory only.

## Can you spot the wrong claim?

**Spot the wrong claim:** Which claim about FlashAttention would fail a senior review?

A. In versions 1 and 2, it gives the same attention output as the standard method, up to rounding.
B. It works in tiles that stay in fast on-chip memory.
C. It shrinks the model, so it fits on a smaller GPU.
D. It cuts the trips the GPU makes to its big, slow memory.

**Answer:** C. FlashAttention doesn't change the model or its weights. It changes how the same attention step runs on the GPU, so the model's size stays put.

**Quick check:** A contract team's long bundles run out of GPU memory during attention. Which change is the most direct fix?

A. Lower the sampling temperature for the answer
B. Use a tiled attention method that avoids the full score table
C. Add more example contracts to the prompt window
D. Retrain the model on a larger pile of legal text

**Answer:** B. Temperature only changes how the next word is picked. More examples make the input longer. Retraining changes the weights and leaves how attention runs in memory unchanged. Tiling attacks the memory directly.

## Can you answer these from memory?

**Flash cards**

- **What is FlashAttention?** A way to run attention in tiles that moves less data in GPU memory.
- **Does it change the answer?** No, for versions 1 and 2. The output matches standard attention, up to small rounding differences.
- **What cost does it fight?** Writing the full score table to slow GPU memory and reading it back.
- **What is a tile?** A small block of the input that is finished on the chip before the next one.
- **Does it make the model smaller?** No. The weights stay the same. Only how attention runs on the GPU changes.
- **What came after the first version?** FlashAttention-2, then FlashAttention-3, which targets Hopper GPUs.

## Likely follow-up questions

- Q056 Why is attention quadratic in sequence length? https://designervenkat.online/interview/ai/why-is-attention-quadratic-in-sequence-length
- Q055 What is the KV cache, and why does it speed up generation? https://designervenkat.online/interview/ai/what-is-the-kv-cache
- Q058 Multi-query vs grouped-query attention? https://designervenkat.online/interview/ai/multi-query-vs-grouped-query-attention
- Q059 Sliding-window and sparse attention? (full answer coming)
