FlashAttention is an IO-aware, exact algorithm for the attention mechanism that dramatically reduces memory reads and writes by recomputing attention scores on-chip, trading extra FLOPs for significantly less HBM (High-Bandwidth Memory) access. This kernel fusion technique implements the entire softmax and attention operation in a single, optimized CUDA kernel, eliminating the need to materialize the large, intermediate attention matrix to slow global memory. The result is up to 10x faster execution and memory usage that scales linearly, rather than quadratically, with sequence length.
Glossary
FlashAttention

What is FlashAttention?
FlashAttention is a foundational algorithm and IO-aware implementation that redefines the efficiency of the transformer attention operator, enabling longer context windows and faster training and inference.
The algorithm employs tiling to load blocks of the Query, Key, and Value matrices into fast SRAM (shared memory on a GPU), performs the attention computation locally, and writes only the final output. This memory-bound optimization is a prime example of designing algorithms around hardware constraints. Its successor, FlashAttention-2, further optimizes work partitioning and wavefront quantization to better saturate GPU compute units. These innovations are critical for deploying large language models with long contexts cost-effectively.
Key Features of FlashAttention
FlashAttention is a fused algorithm that re-implements the standard attention mechanism to be IO-aware, dramatically reducing memory reads/writes between GPU memory hierarchies. Its core innovations address the primary bottlenecks of transformer scaling.
Memory Complexity Reduction
FlashAttention reduces the memory complexity of the self-attention layer from quadratic to linear in sequence length for the activations that must be stored for the backward pass.
- Standard Attention Memory: $O(N^2)$ to store the attention matrix for gradients.
- FlashAttention Memory: $O(N)$ for storing only the output and a few statistics. This is the key enabler for training and inference with extremely long contexts (e.g., 128K tokens) that were previously impossible due to GPU memory constraints.
Kernel Fusion
It is a prime example of extreme kernel fusion. A single, custom CUDA kernel implements the entire attention operation:
- Matrix multiplications for QK^T.
- Scaling and masking.
- Softmax (with tiling and rescaling).
- Final multiplication with V.
This fusion eliminates multiple kernel launch overheads and, more importantly, prevents the writing and subsequent reading of intermediate tensors (like the attention matrix) to global memory. All intermediate steps occur in registers or shared memory.
Hardware Performance Profile
FlashAttention is optimized for the performance characteristics of modern GPUs (e.g., NVIDIA A100, H100). It specifically targets the large gap in bandwidth and latency between different memory hierarchies.
- HBM Bandwidth: ~1.5 TB/s
- SRAM/Shared Memory Bandwidth: ~19 TB/s By keeping data flow in faster memory, it achieves near-peak GPU utilization for the attention operation. Benchmarks show 2-4x faster wall-clock time for training and 10-20x faster for inference on long sequences compared to standard PyTorch attention implementations.
Enabler for Longer Context Models
By solving the memory bottleneck, FlashAttention directly enabled a new generation of long-context Large Language Models. It made training transformers with sequences of 32K, 128K, or even 1M tokens computationally feasible.
- Models Enabled: GPT-4, Claude, Llama 2 Long, and other state-of-the-art LLMs use FlashAttention or its variants.
- Downstream Impact: Unlocks applications requiring long-context understanding, such as legal document analysis, long-form content generation, and codebase-wide reasoning.
FlashAttention vs. Standard Attention
A technical comparison of the algorithmic and performance characteristics of the IO-aware FlashAttention algorithm against the standard, naive implementation of the attention operator.
| Feature / Metric | Standard Attention | FlashAttention |
|---|---|---|
Core Algorithm | Naive implementation with materialization | IO-aware fused algorithm with recomputation |
Memory Complexity (Forward + Backward) | O(N²) for scores, O(N²) for attention matrix | O(N²) for scores only, O(N) for attention matrix |
Primary Performance Bottleneck | Memory bandwidth (HBM reads/writes) | Arithmetic computation (FLOPS) |
Kernel Fusion Strategy | Multiple kernel launches (matmul, softmax, mask, dropout, matmul) | Single, monolithic fused kernel |
Handles Causal Masking | Requires explicit mask application & storage | Implicitly baked into algorithm tiling |
Handles Attention Dropout | Requires separate dropout mask generation & application | Fused dropout with on-the-fly random number generation |
Numerical Precision | Standard FP32/FP16, prone to softmax overflow in FP16 | Stable softmax implementation with online normalization |
Typical Speedup (Training, 1K-8K Context) | 1x (Baseline) | 2x - 4x |
Typical Memory Reduction (Training) | 1x (Baseline) | 5x - 20x |
Enables Longer Context Training | Limited by O(N²) memory, ~1K-2K tokens on 16GB GPU | Enables 8K+ context on same hardware |
Implementation Complexity | Straightforward, modular PyTorch/TensorFlow ops | Complex, requires low-level CUDA/GPU kernel programming |
Hardware Portability | High (runs on any framework/hardware) | Lower (requires tuning for new GPU architectures) |
Frameworks and Providers Using FlashAttention
FlashAttention's dramatic speed and memory improvements have made it a foundational optimization, integrated directly into major deep learning frameworks and commercial AI platforms.
Google JAX / Flax
Within the JAX ecosystem, FlashAttention is available as a drop-in function, often integrated via libraries like flax or standalone implementations. Its functional, pure nature aligns perfectly with JAX's transformations (jit, grad, vmap). Google's internal infrastructure for training models like PaLM leverages these fused attention implementations, and they are accessible for public research and development on TPUs and GPUs.
Commercial AI Platforms (OpenAI, Anthropic, xAI)
Leading AI service providers implement custom, hardware-optimized variants of FlashAttention and related algorithms in their proprietary inference stacks. For example:
- OpenAI uses fused attention kernels to power the low-latency responses of ChatGPT and its API.
- Anthropic and xAI employ similar optimizations to train and serve their frontier models (Claude, Grok) cost-effectively at scale. These implementations are often further tuned for specific data center GPU clusters (e.g., H100s) and are a critical competitive advantage.
Frequently Asked Questions
FlashAttention is a foundational algorithm for accelerating transformer models. These questions address its core mechanisms, benefits, and practical implications for AI infrastructure.
FlashAttention is an IO-aware, exact algorithm for the attention operator that dramatically speeds up transformer models and reduces memory usage by minimizing reads and writes to high-bandwidth memory (HBM). It works by fusing the entire attention computation (matrix multiplies, masking, softmax) into a single GPU kernel. Instead of writing the large intermediate attention matrix to slow HBM, it uses tiling to load small blocks of the Query, Key, and Value matrices into fast SRAM, computes attention scores for that block, and applies the softmax rescaling online with a technique called safe softmax. This recomputation of attention scores on-chip avoids the expensive memory traffic that is the primary bottleneck for standard attention implementations.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
FlashAttention is a landmark example of operator and kernel fusion. The following terms are core to understanding its design principles and the broader optimization landscape.
Kernel Fusion
A compiler optimization technique that combines multiple low-level computational kernels into a single, unified kernel. This reduces kernel launch overhead and improves data locality by keeping intermediate results in fast on-chip memory (registers, shared memory). It is the foundational principle enabling FlashAttention's performance gains.
- Primary Goal: Minimize trips to high-latency GPU global memory (HBM).
- Example: Fusing elementwise operations like GeLU and dropout.
- Contrast with Operator Fusion: Kernel fusion works at the level of GPU instruction scheduling, while operator fusion is a higher-level graph optimization.
IO-Aware Algorithms
Algorithms designed with explicit consideration for the memory hierarchy and bandwidth constraints of the hardware, rather than just theoretical floating-point operation (FLOP) count. FlashAttention is explicitly IO-aware, minimizing reads/writes to slow High-Bandwidth Memory (HBM).
- Key Insight: Memory access is often the bottleneck, not computation.
- Strategy: Recomputation (tiling) to trade extra FLOPs for drastically reduced memory IO.
- Application: Critical for attention on long sequences where the attention matrix (N x N) is too large to fit in SRAM.
Memory-Bound vs. Compute-Bound
A classification of workloads based on what limits performance. Understanding this distinction is crucial for applying fusion correctly.
- Memory-Bound: Performance is limited by the speed of data movement (memory bandwidth). The attention operation, prior to optimization, is typically memory-bound due to writing/reading the large attention matrix.
- Compute-Bound: Performance is limited by the speed of arithmetic units (FLOP/s). Dense matrix multiplication can be compute-bound.
- FlashAttention's Impact: It transforms the standard attention implementation from a memory-bound operation into a more compute-bound one by fusing kernels and using tiling, better utilizing the GPU's compute resources.
Tiling (Loop Tiling/Blocking)
An optimization technique that breaks a large computation into smaller blocks (tiles) that fit into a fast, local memory cache. FlashAttention uses tiling to compute the attention output in chunks without ever materializing the full N x N attention matrix in HBM.
- Process: Load blocks of the Query (Q), Key (K), and Value (V) matrices into SRAM, compute partial attention results, and iteratively update a running output.
- Benefit: Enables processing sequences longer than available SRAM.
- Trade-off: Requires online softmax and careful numerical stability management, as the full normalization context is not initially known.
Online Softmax
A numerically stable algorithm for computing softmax that can process data in streaming fashion, without needing the full input vector upfront. This is essential for FlashAttention's tiling approach.
- Standard Softmax: Requires seeing all input elements to compute the maximum (for stability) and the normalization sum.
- Online Softmax: Maintains running statistics (maximum and sum) as it processes tiles, allowing correct normalization to be computed incrementally.
- Role in FlashAttention: Allows the softmax step of attention to be computed per-tile within the fused kernel, avoiding a separate pass over the full attention matrix.
Fused Multi-Head Attention
A single, hand-optimized kernel that implements the entire multi-head attention mechanism. FlashAttention is the definitive example, but the term refers to the general class of such kernels.
- Components Fused: Linear projections (Q/K/V), attention score calculation, masking, softmax, dropout, and weighted sum aggregation.
- Advantages:
- Eliminates intermediate materialization of attention scores and outputs.
- Enables longer context lengths by reducing peak HBM usage.
- Can incorporate specialized optimizations like causal masking or alibi biases directly.
- Evolution: Led to FlashAttention-2 (better warp partitioning) and FlashAttention-3 (utilizing new GPU features like FP8 and asynchronous copies).

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us