Although this implementation is simple, it suffers from the inefficiencies mentioned above. scores A tensor of shape (batch_size, seq_len, seq_len) can become prohibitively large for long sequences.
Enter Flash Attention
Flash Attention, presented by Tri Dao and colleagues in a 2022 paper, is an approach to computational attention that significantly reduces memory usage and improves computational efficiency. The main ideas behind Flash Attention are:
- Tiling: Partition large attention matrices into small tiles that fit into fast on-chip SRAM.
- RecalculationInstead of storing the entire attention matrix, we recompute parts of it as needed during the backward pass.
- IO-enabled implementation: Optimize algorithms to minimize data movement between different levels of the GPU memory hierarchy.
Flash Attention Algorithm
Flash Attention essentially rethinks how attention mechanisms are computed: instead of computing the entire attention matrix at once, it processes it in blocks, leveraging the memory hierarchy of modern GPUs.

Here's a brief overview of the algorithm:
- Input: Matrices Q, K, V in HBM (High Bandwidth Memory) and an on-chip SRAM of size M.
- The block size is calculated based on the available SRAM.
- Initialization of the output matrix O and auxiliary vectors l and m.
- The algorithm divides the input matrix into blocks that fit into SRAM.
- Two nested loops process these blocks.
- The outer loop loads the K and V blocks.
- The inner loop loads the Q block and performs the calculations.
- On-chip computations include matrix multiplication, softmax, and output computation.
- After each block is processed, the results are written back to the HBM.
This block-wise computation allows Flash Attention to compute accurate attention while keeping the memory footprint significantly smaller.
The Math Behind Flash Attention
The key to making Flash Attention work is a mathematical trick that allows us to compute a softmax on a block-by-block basis. The paper introduces two key formulas:
- Softmax decomposition:
softmax(x) = exp(x - m) / Σexp(x - m)where m is the maximum value of x.
- Softmax merging:
softmax(x ∪ y) = softmax(softmax(x) * e^(m_x - m), softmax(y) * e^(m_y - m))where m = max(m_x, m_y)
These formulas allow Flash Attention to calculate the partial softmax results for each block and then correctly combine them to get the final result.
Implementation details
Let's take a closer look at a simplified implementation of Flash Attention to explain its core concepts.
import torch
def flash_attention(Q, K, V, block_size=256):
batch_size, seq_len, d_model = Q.shape
# Initialize output and running statistics
O = torch.zeros_like(Q)
L = torch.zeros((batch_size, seq_len, 1))
M = torch.full((batch_size, seq_len, 1), float('-inf'))
for i in range(0, seq_len, block_size):
Q_block = Q[:, i:i+block_size, :]
for j in range(0, seq_len, block_size):
K_block = K[:, j:j+block_size, :]
V_block = V[:, j:j+block_size, :]
# Compute attention scores for this block
S_block = torch.matmul(Q_block, K_block.transpose(-2, -1)) / (d_model ** 0.5)
# Update running max
M_new = torch.maximum(M[:, i:i+block_size], S_block.max(dim=-1, keepdim=True).values)
# Compute exponentials
exp_S = torch.exp(S_block - M_new)
exp_M_diff = torch.exp(M[:, i:i+block_size] - M_new)
# Update running sum
L_new = exp_M_diff * L[:, i:i+block_size] + exp_S.sum(dim=-1, keepdim=True)
# Compute output for this block
O[:, i:i+block_size] = (
exp_M_diff * O[:, i:i+block_size] +
torch.matmul(exp_S, V_block)
) / L_new
# Update running statistics
L[:, i:i+block_size] = L_new
M[:, i:i+block_size] = M_new
return O
Although this implementation is simplified, it captures the essence of Flash Attention: it processes the input in blocks, maintains running statistics (M and L), and correctly computes the softmax across all blocks.
The effect of flash attention
The introduction of Flash Attention has had a huge impact on the field of machine learning, especially in large language models and long context applications. Its main benefits include:
- Reduced memory usage: Flash Attention reduces memory complexity from O(N^2) to O(N), where N is the length of the sequence, allowing us to process much longer sequences on the same hardware.
- Speed improvementsBy minimizing data movement and better utilizing the computational power of the GPU, Flash Attention achieves significant speedups: the authors report training GPT-2 up to 3x faster compared to standard implementations.
- Accurate calculations: Unlike other attention optimization techniques, Flash Attention computes accurate attention rather than an approximation.
- ScalabilityThe reduced memory footprint allows us to scale to much longer sequences, up to millions of tokens.
Real-world impact
The impact of Flash Attention is not limited to academic research: it is being rapidly adopted by many popular machine learning libraries and models.
- Hug Face Transformer: The popular Transformers library has Flash Attention integrated, allowing users to easily take advantage of it.
- GPT-4 and Beyond: Though unconfirmed, there is speculation that advanced language models like GPT-4 may use techniques similar to Flash Attention to process longer contexts.
- Long Context Model: Flash Attention has enabled a new generation of models that can handle very long contexts, including models that can process entire books or long videos.
Flash Spotlight: Recent Developments
Standard Attention and Flash Attention
Flash Attention-2
Building on the success of the original Flash Attention, the same team introduced FlashAttention-2 in 2023. This updated version brings several improvements.
- Further optimizationFlashAttention-2 delivers even better GPU utilization, reaching up to 70% of the A100 GPU's theoretical peak FLOPS.
- Improved backward pass: The backward pass is optimized to be nearly as fast as the forward pass, significantly speeding up training.
- Support for various featured variations: FlashAttention-2 extends support for different attention variants, including grouped query attention and multi-query attention.
Flash Attention-3
FlashAttention-3, released in 2024, represents the latest advancements in this research field, introducing several new techniques to further improve performance.
- Asynchronous Computation: Exploiting the asynchronicity of new GPU instructions to overlap different computations.
- FP8 Support: Uses low-precision FP8 calculations to achieve even faster processing.
- Inconsistent Processing: A technique for reducing quantization errors when using lower precision formats.
Below is a simplified example of how FlashAttention-3 leverages asynchronous computation.
import torch
from torch.cuda.amp import autocast
def flash_attention_3(Q, K, V, block_size=256):
with autocast(dtype=torch.float8): # Using FP8 for computation
# ... (similar to previous implementation)
# Asynchronous computation example
with torch.cuda.stream(torch.cuda.Stream()):
# Compute GEMM asynchronously
S_block = torch.matmul(Q_block, K_block.transpose(-2, -1)) / (d_model ** 0.5)
# Meanwhile, on the default stream:
# Prepare for softmax computation
# Synchronize streams
torch.cuda.synchronize()
# Continue with softmax and output computation
# ...
return O
This code snippet shows how FlashAttention-3 leverages asynchronous computation and FP8 precision. Note that this is a simplified example and a real implementation will be much more complex and hardware-specific.
Implementing Flash Attention in your project
If you're interested in leveraging Flash Attention in your own projects, you have a few options:
- Use an existing libraryMany popular libraries, such as Hugging Face Transformers, now include implementations of Flash Attention, so updating to the latest version and enabling the appropriate flags may be enough.
- Custom implementation: For more control or specialized use cases, we recommend implementing Flash Attention yourself. The xformers library provides a good reference implementation.
- Hardware-specific optimizations: If you have specific hardware (such as an NVIDIA H100 GPU), we recommend that you take advantage of hardware-specific features to maximize performance.
Here is an example of how to use Flash Attention with the Hugging Face Transformers library:
from transformers import AutoModel, AutoConfig
# Enable Flash Attention
config = AutoConfig.from_pretrained("bert-base-uncased")
config.use_flash_attention = True
# Load model with Flash Attention
model = AutoModel.from_pretrained("bert-base-uncased", config=config)
# Use the model as usual
# ...
Challenges and future directions
Although flash attention has made great strides in improving the efficiency of attention mechanisms, challenges and areas for future research remain.
- Hardware quirksCurrent implementations are often optimized for specific GPU architectures, and generalizing these optimizations to different hardware remains a challenge.
- Integration with other technologies: Combining Flash Attention with other optimization techniques such as pruning, quantization, and model compression is an active area of research.
- Extending to other domains: Flash Attention has seen great success in NLP, but there are ongoing efforts to extend its benefits to other domains such as computer vision and multimodal models.
- Theoretical understanding: A deeper theoretical understanding of why Flash Attention works so well could lead to even more powerful optimizations.
Conclusion
By cleverly leveraging the GPU memory hierarchy and employing mathematical tricks, Flash Attention achieves significant improvements in both speed and memory usage, without sacrificing accuracy.
As we have seen in this article, the impact of Flash Attention goes far beyond just being an optimization technique: it has enabled the development of more powerful and efficient models.
