Inferensys

Glossary

FlashAttention

FlashAttention is an I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading and writing the large attention matrix to slow HBM, dramatically speeding up transformer training and inference.
Developer testing AI inference on mobile phone in hand, laptop with optimization code visible, casual tech review moment.
EFFICIENT MODEL ARCHITECTURES

What is FlashAttention?

FlashAttention is a groundbreaking I/O-aware algorithm that redefines the computational efficiency of the transformer's self-attention mechanism.

FlashAttention is an exact, hardware-aware algorithm that dramatically speeds up transformer training and inference by optimizing memory access between GPU memory hierarchies. It computes the standard softmax attention exactly but avoids the costly reading and writing of the large, intermediate attention matrix to slow High-Bandwidth Memory (HBM). Instead, it performs repeated recomputation of attention scores within fast SRAM (Static Random-Access Memory), trading increased FLOPs for a massive reduction in memory I/O, which is the true bottleneck.

The algorithm employs tiling to chunk the attention computation, and online softmax techniques to maintain numerical stability, all while keeping the key-value (KV) cache operations efficient. This I/O complexity reduction from quadratic to linear in sequence length enables processing of much longer contexts. It is a foundational optimization behind modern large language models, directly enabling faster iteration and lower costs for both training and autoregressive inference.

EFFICIENT MODEL ARCHITECTURES

Key Features and Benefits of FlashAttention

FlashAttention is an I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading and writing the large attention matrix to slow HBM, dramatically speeding up training and inference.

03

Dramatic Speedup for Long Sequences

The performance gains of FlashAttention scale quadratically with sequence length due to the avoided O(N²) HBM accesses.

  • Training: Enables training transformers on sequences up to 16k tokens long, unlocking new domains like long-form text, high-resolution image, and audio generation.
  • Inference: Reduces memory footprint of the key-value (KV) cache, allowing for larger batch sizes or longer context windows during generation.
  • Benchmark: Can be 2-4x faster than standard attention for sequences of 2k tokens, with gains exceeding 10x for longer sequences, while using 5-20x less memory.
2-4x
Faster (2k tokens)
5-20x
Less Memory
04

Enabler for Longer Context Models

FlashAttention directly addresses the core bottleneck preventing long-context transformers: the quadratic memory complexity of attention. It has become a foundational component for state-of-the-art large language models (LLMs) that feature extended context windows, such as GPT-4, Claude, and Llama 2. By making it feasible to train and run inference on sequences of 32k, 128k, or even 1M+ tokens, it enables applications like:

  • Legal document analysis
  • Long-context conversation
  • Codebase-wide reasoning
  • Genomic sequence modeling
05

Integration with Other Optimizations

FlashAttention is not a standalone technique but a core primitive that combines synergistically with other efficiency methods:

  • Multi-Query Attention (MQA) & Grouped-Query Attention (GQA): Reduces the size of the KV cache that FlashAttention must manage.
  • PageAttention (vLLM): Uses a similar block-wise approach for managing the KV cache in LLM serving systems.
  • Quantization: FlashAttention-2 supports FP16 and BF16 precision, and research extends it to INT8 quantization.
  • Causal Masking: Optimized implementation for the autoregressive masking used in language model decoders.
I/O-AWARE ALGORITHM

FlashAttention vs. Standard Attention: A Comparison

A technical comparison of the core algorithmic and performance characteristics between the standard Transformer attention mechanism and the FlashAttention optimization.

Feature / MetricStandard AttentionFlashAttention

Algorithmic Complexity

O(N²) in memory & time

O(N²) in time, O(N) in memory

Primary Memory Bottleneck

High Bandwidth Memory (HBM) I/O

Fast On-Chip SRAM (Static RAM)

Attention Matrix Storage

Explicitly materialized in HBM

Never fully materialized; recomputed on-the-fly

Key Technical Method

Compute attention scores, save matrix, apply softmax, multiply with values

Tiling & Recomputation: Process blocks in SRAM, recompute attention scores in backward pass

Memory Footprint (Forward + Backward)

~O(N² + N*d) for storing scores & gradients

~O(N) for storing only output & block-level statistics

Exactness

Exact (up to floating point)

Exact (numerically identical to standard attention)

Typical Speedup (Training, Long Sequences)

1x (Baseline)

2-4x

Typical Memory Reduction (Training)

1x (Baseline)

5-20x

Inference Optimization

Requires full KV cache in HBM

Enables very large KV caches via memory savings

Hardware Requirement

Standard GPU

GPU with sufficient SRAM (e.g., NVIDIA A100, H100)

Integration Complexity

Standard implementation

Requires custom CUDA kernel implementation

PRACTICAL APPLICATIONS

Where is FlashAttention Used?

FlashAttention is not a theoretical algorithm but a foundational engineering breakthrough deployed across the AI stack to overcome the memory bottleneck of standard attention, enabling longer contexts, faster training, and cheaper inference.

06

Enabling Efficient Model Architectures

FlashAttention directly enables other efficient model architectures listed as sibling topics. It provides the computational foundation for:

  • Mixture of Experts (MoE): Efficiently routes long sequences through sparse experts.
  • Grouped-Query & Multi-Query Attention (GQA/MQA): Reduces KV cache size, and FlashAttention optimizes the remaining attention computation.
  • Long-Context Positional Encodings: Methods like ALiBi and RoPE rely on FlashAttention to efficiently apply positional biases over long sequences.
  • Dynamic Inference & Early Exiting: Makes the evaluation of attention at each layer more efficient, improving the viability of conditional computation paths. Thus, FlashAttention is a primitive upon which many other efficiency techniques are built.
3-5x
Typical Training Speedup
O(n²) → ~O(n)
Memory Complexity
EFFICIENT MODEL ARCHITECTURES

Frequently Asked Questions About FlashAttention

FlashAttention is a groundbreaking algorithm that dramatically speeds up transformer training and inference by optimizing memory access. This FAQ addresses the core technical questions developers and architects have about its mechanism, benefits, and applications.

FlashAttention is an I/O-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM to avoid reading and writing the large attention matrix to slow high-bandwidth memory (HBM). It works by tiling the attention computation: the input queries, keys, and values are loaded in blocks from HBM into SRAM, the attention output for that block is computed, and the result is written back to HBM. This recomputation in the backward pass avoids storing the massive O(N²) attention matrix, trading extra FLOPs for significantly reduced memory I/O, which is the primary bottleneck.

Key Mechanism:

  • Tiling: Splits the Q, K, V matrices into blocks that fit in SRAM.
  • Recomputation: In the backward pass, it recomputes attention on-chip instead of reading the saved matrix from HBM.
  • I/O Complexity: Reduces HBM accesses from O(N²) to O(N²d²M⁻¹), where M is SRAM size.
Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.