LLMS & TRANSFORMERS · INTERVIEW QUESTION
Explain FlashAttention. How does it achieve sub-quadratic memory while maintaining exact attention?
⚫ expert · Transformer Architecture
Hint: Think about tiling, SRAM vs HBM, and the online softmax trick.
Answer
FlashAttention is an IO-aware exact attention algorithm. Standard attention materializes the N×N attention matrix in GPU HBM (slow). FlashAttention:
- Tiles Q, K, V into blocks that fit in SRAM (fast on-chip memory)
- Computes attention block-by-block using the online softmax trick (maintains running max and sum)
- Never materializes the full N×N matrix.
Result: FLOPs (same as standard) but memory instead of , and 2-4x faster due to reduced HBM reads/writes. FlashAttention-2 further optimizes parallelism and work partitioning. This enabled training with much longer context windows (e.g., 128k tokens).
Related questions
- Explain the self-attention mechanism in detail. What are Q, K, V and why do we use scaled dot-product attention?
- Explain positional encoding in Transformers. Compare sinusoidal, learned, RoPE, and ALiBi.
- What are Large Language Models (LLMs) and how do they work?
- How does multi-head attention differ from single-head attention? Why use multiple heads?
- Explain the difference between LoRA, full fine-tuning, and prompt tuning. When would you use each?