Introduction
Welcome back. In our last lesson, we took a deep dive into the quadratic complexity of self-attention, establishing that the O(n²) scaling in both computation and memory is the primary bottleneck for long sequences. Crucially, you learned that on modern GPUs, the problem is often not raw compute power but memory I/O—the time spent shuffling the massive n x n attention matrix between slow high-bandwidth memory (HBM) and fast on-chip SRAM.
Today, we move from the problem to the solutions. The learning outcome is to describe the core ideas behind efficient attention variants like FlashAttention and sparse attention. These are not just incremental improvements; they are fundamental breakthroughs that have enabled the current era of long-context LLMs. We will explore two primary philosophies for tackling the attention bottleneck:
- Hardware-Aware Exact Attention: Re-engineering the computation to be GPU-friendly without changing the mathematical output. This is the domain of FlashAttention.
- Algorithmic Approximate Attention: Changing the algorithm itself to approximate the full attention matrix, trading a small amount of fidelity for significant efficiency gains. This is the world of Sparse Attention.
Understanding these two approaches is essential for any engineer working at the cutting edge of AI.
1. FlashAttention: An I/O-Aware Exact Solution
The core insight of FlashAttention is that standard attention is I/O-bound, not compute-bound. The solution, therefore, isn't to do less math, but to be much smarter about how data moves around the GPU. FlashAttention computes the exact same attention output as the standard algorithm but does so much faster and with far less memory.
To understand how, we must first revisit the GPU memory hierarchy.
How FlashAttention Accelerates Generative AI Revolution
This video from Jia-Bin Huang's channel provides an excellent visual explanation of the memory bottleneck that FlashAttention solves. It clearly contrasts the slow HBM with the fast on-chip SRAM.
Watch the segment from 01:50 to 03:01. Focus on how standard attention requires multiple read/write trips to the slow HBM, which creates the latency bottleneck.
FlashAttention's genius lies in avoiding these costly trips to HBM by fusing operations and using the fast SRAM intelligently.
The Core Ideas of FlashAttention
There are two main technical innovations that make this possible: tiling and online softmax.
1. Tiling and Kernel Fusion:
Instead of processing the entire Q, K, and V matrices at once, FlashAttention breaks them into smaller blocks, or "tiles." These tiles are small enough to fit entirely within the GPU's fast SRAM. The algorithm then loads a block of Q, K, and V into SRAM, performs all the attention calculations for that block (the dot product, scaling, softmax, and multiplication by V), and only writes the final, partial output back to HBM.
By fusing these operations into a single GPU "kernel," FlashAttention never materializes the full n x n attention matrix in HBM. This single change drastically reduces memory reads/writes and is the primary source of its speedup and linear memory scaling (O(n)).

2. Online Softmax:
A key challenge with tiling is the softmax operation. Normally, to compute the softmax for a row, you need all the values in that row to find the maximum (for numerical stability) and the sum (for normalization). This seems to conflict with block-by-block processing.
FlashAttention solves this with a clever "online softmax" algorithm. It maintains running statistics (the current maximum and the normalization denominator) as it iterates through the blocks. When a new block is processed, it can update its previous calculations by rescaling them with the new information. This allows it to compute the mathematically identical softmax result in a single pass over the data, without ever needing to see the full n x n matrix.
To solidify your understanding of these concepts, the following resources provide more detail.
Stanford’s Research Reveals Secret to 3x Faster Transformer Training With FlashAttention
This article from Galileo.ai provides a superb, clear explanation of the technical innovations behind FlashAttention. It's written for a technical audience and aligns perfectly with your background.
Please read the section 'Five technical innovations that enable memory-efficient attention'. Pay close attention to the descriptions of 'IO-aware algorithm design,' 'Tiling strategy,' and 'Online softmax computation.'
How FlashAttention Accelerates Generative AI Revolution
Let's return to the Jia-Bin Huang video, which provides a fantastic visual walkthrough of both the online softmax trick and the full FlashAttention loop.
Please watch from 05:35 to 11:28. The first part (until 08:33) explains the online softmax recurrence relation. The second part shows how this is integrated into the full tiled attention loop.
In summary, FlashAttention transforms attention from a memory-bound problem to a compute-bound one, allowing GPUs to operate closer to their peak performance.
2. Sparse Attention: An Algorithmic Approximation
The second major approach, Sparse Attention, takes a different philosophical path. Instead of optimizing the computation of the full n x n matrix, it argues that the full matrix is often redundant. The core idea is that most of the information in the attention matrix is concentrated in a few key relationships, so we can achieve very similar performance by only computing a "sparse" subset of the n² attention scores.
This is an approximation—the output is not identical to standard attention—but it can dramatically reduce computational complexity from O(n²) to something more manageable, like O(n log n) or O(n√n).

Sparsity Patterns
There are many ways to decide which tokens should attend to each other. These methods are called sparsity patterns.
The Evolution of Attention Mechanisms
The following article gives a great overview of different sparsity patterns, which are the heart of sparse attention mechanisms.
Read the section '3. Sparse Attention', up to (but not including) '4. Flash Attention'. Focus on understanding the difference between the common patterns, such as Local (Sliding Window) Attention and Strided Attention.
As you've just read, the patterns generally fall into a few categories:
-
Fixed Patterns: These are predetermined and do not change during training.
- Local (Sliding Window) Attention: Each token attends only to a fixed window of
wneighboring tokens. This is effective for tasks where locality is important, like processing text or images. - Strided (or Dilated) Attention: Each token attends to tokens at a fixed step or interval (e.g., every 4th token). This can help capture longer-range dependencies than a purely local window.
- Global Attention: A few special tokens (like
[CLS]) are designated as "global" and are allowed to attend to all other tokens, while the rest use a sparse pattern. This ensures some global context is preserved.
- Local (Sliding Window) Attention: Each token attends only to a fixed window of
-
Learned or Dynamic Patterns: These are more advanced. Instead of a fixed pattern, the model learns or dynamically decides which tokens are most important for a given query. A great modern example is DeepSeek Sparse Attention (DSA). It uses a highly efficient "lightning indexer" to perform a quick, low-precision search for the most relevant key tokens for a given query. The full, expensive attention calculation is then performed only on this small, selected subset of tokens.
The following video explains this dynamic approach using DSA as a case study.
How Attention Got So Efficient [GQA/MLA/DSA]
This video explains DeepSeek Sparse Attention, which is a state-of-the-art example of a dynamic sparse attention mechanism. This illustrates how far the concept has evolved from simple fixed windows.
Watch from 21:58 to 24:27. Focus on the core idea of the 'lightning indexer': using a fast, approximate method to identify the most relevant tokens before computing the main attention scores.
Sparse attention methods are powerful because they directly attack the n² term in the complexity, making them highly effective for extremely long sequences where even FlashAttention might be too slow.
Test your understanding!
You are tasked with building a model to summarize entire legal contracts, which can be over 100,000 tokens long. Which efficient attention strategy, FlashAttention or Sparse Attention, would be more suitable and why? What are the potential trade-offs?
Show answer
Sparse Attention would be the more suitable strategy.
-
Why: FlashAttention is extremely effective, but its time complexity is still
O(n²). At 100,000 tokens, the number of computations (100,000² = 10 billioninteractions per head, per layer) would be computationally infeasible, even with I/O optimizations. Sparse attention reduces this complexity toO(n log n)or similar, which is manageable at this scale. -
Trade-offs: The primary trade-off is that sparse attention is an approximation. By using a pattern like a sliding window combined with global tokens (similar to Longformer), the model might miss a subtle, long-range dependency between two arbitrary, distant paragraphs. However, this is a necessary trade-off to make the problem computationally tractable at all.
Conclusion
You have now explored the two dominant strategies for making attention efficient. These are not mutually exclusive; in fact, many modern models use versions of FlashAttention to implement their sparse attention patterns, getting the best of both worlds.
Key Takeaways:
- FlashAttention is a hardware-aware, exact method. It doesn't change the math but reorders the computation using tiling and kernel fusion to minimize slow memory I/O, resulting in
O(n)memory usage and significant speedups. - Sparse Attention is an algorithmic, approximate method. It reduces computation from
O(n²)to near-linear by computing attention only on a subset of tokens, defined by fixed (e.g., sliding window) or dynamic (e.g., learned indexer) patterns. - The choice between them depends on the trade-off between exactness and scalability. For moderately long sequences, FlashAttention is ideal. For extremely long sequences, some form of sparsity is often required.
What's Next?
With a solid grasp of how the attention mechanism works and how it's been optimized, you are ready to zoom out and look at how these layers are assembled into complete models. In the next lesson, we will compare the high-level decoder-only (e.g., GPT), encoder-only (e.g., BERT), and encoder-decoder (e.g., T5) architectures. You'll see how the fundamental building blocks we've discussed are used to create models optimized for different kinds of tasks.