interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/5

Unit 8 · AI infrastructure · Lesson 7 of 10

FlashAttention and IO-aware exact attention

FlashAttention computes exact attention faster and with memory linear in sequence length by tiling the computation so the attention matrix never round-trips through GPU high-bandwidth memory. It is the canonical example that memory traffic, not FLOP count, often decides performance.
About 3 hFocus mode

Step 1: Learn the ideas

  • Data movement in standard attention: S = QKᵀ, P = softmax(S), O = PV through HBM
  • Online (streaming) softmax with running maximum and sum
  • Tiling over query and key/value blocks in shared memory
  • Kernel fusion and avoiding the s×s matrix
  • Backward pass by recomputation from saved statistics
  • IO complexity versus FLOPs
  • FlashAttention-2 work partitioning (awareness)
  • Masking, dropout and variable-length batches

Why it matters

Attention dominates memory and time for long sequences. FlashAttention computes exact attention with memory linear in sequence length and far less HBM traffic by restructuring the computation so the full attention matrix never leaves on-chip memory. It is the standard example that bytes moved, not FLOPs, often decide speed, and its ideas carry over to the fused attention kernels used for training and prefill today.

Core ideas

Standard attention moves too much data. Per head, the naive implementation computes S = QKᵀ (an s×s matrix), writes it to HBM, reads it back for masking and softmax, writes P, then reads P and V to form O. Memory is O(s²) per head, and the masking and softmax steps are memory-bound passes over that matrix.

Online softmax. Softmax needs a row's maximum and sum, which seems to require the whole row first. Instead, process keys in blocks and keep, for each query row, a running maximum m, a running normalizer ℓ and an unnormalized output o. When a block arrives with maximum m_b, set m' = max(m, m_b), rescale ℓ and o by exp(m − m'), add the block's exp(scores − m') terms, and continue. Dividing o by ℓ at the end gives exactly the softmax-weighted sum, up to floating-point rounding.

Tiling and fusion. Load a block of queries into shared memory (SRAM), stream key and value blocks through it, update the running statistics, and write only the output block and the per-row softmax statistics back to HBM (FlashAttention-2 keeps a single log-sum-exp per row). One fused kernel replaces several passes, and the s×s matrix is never stored.

Backward by recomputation. Instead of keeping P for the backward pass, recompute the needed blocks from Q, K, V and the saved per-row statistics. This adds FLOPs but saves much more memory traffic, so the backward pass also gets faster.

What changes and what does not. FLOPs stay quadratic in sequence length (slightly higher with recomputation). HBM reads and writes drop sharply and attention memory becomes linear in s. The paper's IO-complexity analysis states this precisely in terms of SRAM size. FlashAttention-2 keeps the algorithm and improves how work is split across thread blocks and warps, including parallelism over the sequence dimension.

Common misconceptions

  • "FlashAttention approximates attention." It computes exact attention; sparse and low-rank methods are the approximate family.
  • "It reduces FLOPs or makes attention subquadratic." FLOPs stay quadratic; the gain comes from avoiding HBM round trips of the s×s matrix.
  • "It speeds up decoding as much as training." The largest gains are in training and prefill. Decoding one query per sequence is dominated by reading the KV cache, which calls for different parallelization.
  • "The memory savings come from lower precision." They come from not storing the attention matrix; precision is an independent choice.

How to approach it

Write naive attention, then a blockwise version with the online softmax in plain PyTorch, and check it against the reference on random inputs, with and without a causal mask. Then read the paper's algorithm and IO analysis with your implementation open. For depth, study a fused kernel (the Triton tutorial or the CS336 systems assignment) and measure memory and time against the naive version as sequence length grows.

Suggested order

  1. 1Implement naive attention and measure memory and time as sequence length doubles.
  2. 2Read the FlashAttention paper’s background and algorithm sections and derive the online softmax update on paper.
  3. 3Implement blockwise attention with the online softmax in PyTorch and verify it against the naive version, including a causal mask.
  4. 4Read the paper’s IO-complexity analysis and explain in your own words how it depends on SRAM size.
  5. 5Read FlashAttention-2 for the work-partitioning changes, then study the Triton fused-attention tutorial or the CS336 systems assignment kernel.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • FlashAttention: Fast and Memory-Efficient Exact Attention with IO-AwarenessStart here

    arXiv

    The algorithm, recomputation in the backward pass and the IO-complexity analysis.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning

    arXiv

    How work partitioning across blocks and warps raises utilization without changing the algorithm.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Triton tutorials (including Fused Attention)

    Triton documentation

    Section: Fused Attention

    A readable fused-attention kernel to study after your own blockwise implementation works.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Stanford CS336 Language Modeling from Scratch

    Stanford University

    Section: Assignment 2: Systems

    Guided implementation of a FlashAttention-2 kernel in Triton with profiling and benchmarking.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Implement the blockwise online-softmax forward pass first and test it against naive attention on random inputs, with and without a causal mask, at several block sizes. Then add a backward pass by recomputation or compare against autograd on the naive version. With a GPU, write or study a Triton kernel and plot memory and time against sequence length for both implementations. Treat each numerical mismatch as a bug to explain, not a tolerance to raise.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1Which two statistics per row does the online softmax keep, and why must the accumulator be rescaled?
  2. Question 2Which tensors does the forward kernel write to HBM?
  3. Question 3Why is the backward pass faster despite extra FLOPs?
  4. Question 4How does SRAM size enter the IO-complexity argument?
  5. Question 5Why are the gains smaller for single-token decoding?

You’re done when you can

  • Derive the online softmax rescaling update from scratch.
  • Implement blockwise attention that matches naive attention within floating-point tolerance, including a causal mask.
  • Explain in two minutes why FlashAttention is faster without reducing FLOPs.
  • State what the backward pass recomputes and why that is a good trade.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Your notes and study time

Log study time