FlashAttention is an IO-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), significantly speeding up training and inference for long sequences. It achieves this through tiling and recomputation, fundamentally restructuring the standard attention computation to be memory-efficient without approximating the result. This makes it a critical optimization for scaling Transformer models to context windows of tens or hundreds of thousands of tokens.
Glossary
FlashAttention

What is FlashAttention?
FlashAttention is a foundational algorithm that dramatically accelerates the attention mechanism in Transformer models, which is the primary computational bottleneck for long-sequence processing.
The algorithm's primary innovation is its awareness of the memory hierarchy between GPU SRAM and HBM. By keeping the bulky intermediate attention matrices in fast SRAM and recomputing parts of the softmax as needed, it reduces the total number of memory reads/writes. This results in up to a 9x speedup in wall-clock time for the attention operation and a 15-20x reduction in memory usage compared to standard implementations, directly enabling more efficient long-context LLMs like GPT-4 and LLaMA.
Key Technical Features of FlashAttention
FlashAttention is an IO-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 memory, significantly speeding up training and inference for long sequences.
IO-Aware Algorithm Design
FlashAttention is fundamentally designed around the memory hierarchy of modern GPUs. It treats High Bandwidth Memory (HBM) as slow, large-capacity storage and Static Random-Access Memory (SRAM) as fast, small-capacity cache. The core innovation is minimizing the number of reads/writes between HBM and SRAM. Instead of materializing the full attention matrix (size O(N²) for sequence length N) in HBM, it performs the softmax operation in SRAM through tiling and recomputation, trading extra FLOPs for drastically reduced memory I/O, which is the true bottleneck.
Tiling and Recomputation
This is the core mechanism enabling IO-awareness.
- Tiling: The large input queries (Q), keys (K), and values (V) matrices are split into smaller blocks that fit into SRAM. Attention is computed block-by-block.
- Online Softmax with Recomputation: The standard softmax requires seeing all scores for normalization. FlashAttention uses a safe, numerically stable online softmax algorithm that processes blocks sequentially. To avoid storing the attention matrix for the backward pass, it recomputes the attention scores during backpropagation using the stored output and the inputs (Q, K, V). This recomputation trades ~10-20% extra compute for a 10-20x reduction in memory usage.
Exact Attention (No Approximation)
A critical distinction from other methods like linear attention or sparse attention is that FlashAttention computes the exact same output as standard attention, down to floating-point precision. It does not approximate the attention pattern or sparsify the matrix. This guarantees no degradation in model quality (perplexity, accuracy) while providing massive speed and memory improvements. It is a algorithmic reformulation, not an approximation.
Memory Complexity Reduction
Standard self-attention has O(N²) memory complexity because it materializes the NxN attention matrix. For a sequence of 64K tokens with FP16 precision, this matrix alone consumes ~8 GB. FlashAttention reduces this to O(N), as it only needs to store the final output (O(N*d_model)) and some small statistics for the online softmax. This enables training and inference with extremely long context windows (e.g., 64K, 128K, 1M tokens) that were previously impossible on consumer hardware.
FlashAttention-2: Enhanced Parallelism
The sequel algorithm, FlashAttention-2, introduced major optimizations:
- Reduced Non-Matmul FLOPs: Better parallelization of the online softmax and rescaling operations.
- Improved Work Partitioning: Better tiling strategies across different GPU thread blocks (warps) to minimize synchronization and communication.
- Increased Occupancy: Better utilization of GPU streaming multiprocessors (SMs) by optimizing how attention is computed across the batch and head dimensions.
- Support for Multi-Query & Grouped-Query Attention: Native, optimized kernels for these memory-efficient attention variants used in models like Llama 2 and Gemini.
Integration with Model Architectures
FlashAttention is not just a standalone kernel; it enables new model designs:
- Longer Context Models: Foundational for models like GPT-4, Claude, and Llama 2 with 32K+ context windows.
- Efficient Attention Variants: It provides optimized implementations for Multi-Query Attention (MQA) and Grouped-Query Attention (GQA), which reduce the size of the KV cache during inference.
- Causal Masking: Includes highly optimized handling of the causal mask for autoregressive language modeling.
- Kernel Fusion: The FlashAttention kernel fuses the entire attention operation (matmuls, masking, softmax, dropout) into a single, custom CUDA kernel, eliminating multiple costly memory round trips.
FlashAttention vs. Standard Attention
A technical comparison of the core algorithmic and performance characteristics between the standard attention mechanism and the IO-aware FlashAttention optimization.
| Feature / Metric | Standard Attention | FlashAttention |
|---|---|---|
Core Algorithm | Explicitly computes and stores the full NxN attention matrix in High Bandwidth Memory (HBM). | Recomputes attention scores on-the-fly within fast SRAM, avoiding materialization of the full matrix to HBM. |
Memory Complexity (IO) | O(N²) HBM reads/writes for the attention matrix. | O(N²d) SRAM operations with only O(Nd) HBM reads/writes, where d is the head dimension. |
Theoretical Speedup | Baseline (1x). | Up to 2-4x for medium sequences (1k-4k), up to 10x+ for very long sequences (16k+). |
Memory Footprint | High. Stores large NxN matrix for backward pass. | Dramatically reduced. No large intermediate matrices are stored; gradients are recomputed. |
Exactness | Exact (numerically identical to the mathematical definition). | Exact (numerically identical, not an approximation). |
Kernel Fusion | Typically requires multiple kernel launches (matmul, softmax, mask). | Fuses the entire attention operation (matmul, masking, softmax, dropout) into a single, custom GPU kernel. |
Support for Attention Masking | Yes. Mask applied before softmax. | Yes. Tiling-aware masking is integrated into the fused kernel. |
Support for Dropout | Yes. Applied after softmax. | Yes. Recomputation enables correct dropout application during backward pass. |
Backward Pass Optimization | Requires storing the attention matrix for gradient calculation. | Uses a recomputation strategy in the backward pass, trading FLOPs for reduced HBM I/O. |
Primary Benefit | Conceptual simplicity; straightforward implementation. | Massive reduction in memory I/O, which is the bottleneck for attention on modern GPUs. |
Primary Limitation | Memory-bound; scales quadratically in memory with sequence length. | Increases FLOP count due to recomputation; implementation complexity is high. |
Typical Use Case | Short sequences, prototyping, educational implementations. | Production training and inference of transformers with long contexts (e.g., LLMs, vision transformers). |
Frameworks and Models Using FlashAttention
FlashAttention's algorithmic breakthrough for efficient long-context modeling has been integrated into major deep learning frameworks and is a foundational component for leading open-source and proprietary large language models.
Transformer LLMs (GPT, Llama, Mistral)
Virtually all modern, large-scale decoder-only Transformer models are trained using FlashAttention or its successor, FlashAttention-2. It is essential for handling long context windows cost-effectively.
- Meta's Llama 2 & 3: Trained with sequences up to 32k tokens, relying on FlashAttention for feasible training times.
- Mistral AI's Models: The Mistral 7B and Mixtral 8x7B (MoE) models utilize FlashAttention for efficient inference and training.
- GPT-4 and Claude: While full training details are proprietary, the ability to support contexts of 128k+ tokens is architecturally dependent on IO-aware attention algorithms like FlashAttention.
- Open-Source Leaders: Models like Falcon, MPT, and Qwen explicitly list FlashAttention as a core dependency for training and inference.
Multimodal and Vision Transformers
FlashAttention's benefits extend beyond pure language models to architectures that process long sequences of visual tokens or fused multimodal data.
- Vision Transformers (ViTs): When processing high-resolution images, the sequence length of patch tokens becomes large. FlashAttention-2 accelerates training for models like ViT-Giant.
- Video Models: Processing multiple frames generates extremely long sequences. Models like VideoPoet and Sora-class architectures likely employ similar IO-aware attention to manage this scale.
- Multimodal LLMs: Models such as LLaVA and Flamingo that interleave image and text tokens use FlashAttention to handle the concatenated, lengthy input sequences efficiently.
FlashAttention-2 and Beyond
The original FlashAttention algorithm has been succeeded by FlashAttention-2, which provides further speedups (up to 2x) and better parallelism, particularly for forward passes and different head dimensions.
- Key Improvements: Better work partitioning to reduce non-matrix-multiplication operations, improved occupancy for GPUs with more thread blocks, and optimized backward pass.
- FlashAttention-3: Introduces support for FP8 data type and dynamic sparse attention, pushing the efficiency frontier further for the latest NVIDIA Hopper GPUs.
- Adoption: FlashAttention-2 is now the standard in updated framework integrations and model codebases, representing the state-of-the-art in exact, IO-aware attention.
Specialized Derivatives (FlashDecoding, etc.)
The core ideas of FlashAttention have inspired specialized algorithms optimized for specific inference scenarios.
- FlashDecoding: Optimizes the attention step during autoregressive decoding (where the query is a single new token, but keys/values are a long cached sequence). It parallelizes across the batch and key/value length to drastically reduce latency.
- FlashInfer: A library providing high-performance kernels for LLM serving, implementing both FlashAttention and FlashDecoding.
- Block-Sparse FlashAttention: Allows for a sparsity pattern in the attention matrix, enabling even faster computation for models that can tolerate or are designed with sparse attention, such as Longformer-type patterns.
Frequently Asked Questions
FlashAttention is a foundational algorithm for accelerating Transformer models. These questions address its core mechanics, benefits, and practical implementation.
FlashAttention is an IO-aware, exact attention algorithm that recomputes attention scores on-the-fly within fast SRAM (Static Random-Access Memory) to avoid reading and writing the large attention matrix to and from slow HBM (High Bandwidth Memory).
It works by employing a tiling and recomputation strategy:
- The input Q (Query), K (Key), and V (Value) matrices are divided into blocks that fit into SRAM.
- For each block, the algorithm loads the necessary tiles of K and V, computes a block of the attention output, and updates a running statistics of softmax normalization in SRAM.
- Crucially, it recomputes the attention sub-blocks during the backward pass from the stored normalization statistics, rather than storing the entire intermediate attention matrix (which is O(N²) in sequence length). This trades extra FLOPs for dramatically reduced memory I/O, which is the primary bottleneck for modern GPUs.
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 core algorithm within a broader ecosystem of techniques designed to make transformer-based models faster and more efficient. These related concepts address different bottlenecks in the inference and training pipeline.
KV Caching
A fundamental inference optimization that stores the computed Key (K) and Value (V) tensors for all previously processed tokens during autoregressive generation. For each new token, the model computes attention scores using the new token's query against the cached K and V from the entire prior context, avoiding the quadratic recomputation of the full attention matrix for the growing sequence. This dramatically reduces FLOPs and latency.
- Primary Benefit: Eliminates redundant computation for the prompt and prior context.
- Challenge: The KV cache consumes significant GPU memory, growing linearly with batch size and sequence length, which is a key problem FlashAttention helps manage.
PagedAttention
A memory management algorithm for the KV Cache, introduced by the vLLM inference engine. It treats the KV cache as non-contiguous, manageable blocks or 'pages' (similar to virtual memory in operating systems). This solves critical problems in high-throughput serving:
- Eliminates Memory Fragmentation: Allows for flexible allocation and sharing of cache blocks between different sequences and requests.
- Enables Efficient Sharing: Supports advanced features like copy-on-write for duplicated prompts (e.g., in chatbot system prompts) and parallel sampling (beam search).
- Supports Longer Contexts: Enables more efficient use of available GPU memory for very long sequences.
While FlashAttention optimizes the computation of attention within a forward pass, PagedAttention optimizes the storage and management of the attention state (KV cache) across many requests.
Continuous Batching
An inference serving technique that dynamically groups incoming requests of varying sequence lengths into a single batch to maximize GPU utilization, rather than waiting for a fixed batch size to accumulate (static batching). As some requests finish generation, new requests are seamlessly added to the ongoing batch.
- Also Known As: Iteration batching or in-flight batching.
- Key Advantage: Dramatically improves GPU throughput and reduces average latency, especially for interactive applications like chatbots.
- Relation to FlashAttention: Continuous batching maximizes GPU throughput by keeping the hardware busy. FlashAttention, when used in the inference kernel, improves the efficiency of each attention operation within that batch, especially for long sequences. They are complementary optimizations.
Operator Fusion
A compiler-level optimization that combines multiple, sequential neural network operations into a single, custom GPU kernel. For example, a linear layer, a bias addition, and a GeLU activation might be fused into one operation.
- Primary Benefit: Reduces the number of memory reads/writes between GPU global memory (HBM) and on-chip registers/SRAM. Intermediate results stay in fast memory.
- Reduces Overhead: Minimizes the launch latency of many small, individual kernels.
- Core Principle of FlashAttention: FlashAttention is a large-scale, algorithmic form of operator fusion. It fuses the entire attention operation (matrix multiplies, masking, softmax, dropout) into one kernel to avoid writing the large, intermediate attention matrix (size N x N) to slow HBM.
Model Quantization
A model compression technique that reduces the numerical precision of a model's weights and sometimes activations. Common reductions are from 32-bit floating-point (FP32) to 16-bit (FP16/BF16), 8-bit integers (INT8), or even 4-bit (NF4).
- Post-Training Quantization (PTQ): Converts a pre-trained model using a calibration dataset. Faster but may have accuracy loss.
- Quantization-Aware Training (QAT): Fine-tunes the model with simulated quantization for better accuracy.
- Relation to FlashAttention: These are complementary techniques. Quantization reduces the memory footprint and computational intensity (bits per operation) of the model weights. FlashAttention reduces the memory bandwidth bottleneck of the attention computation itself. Using quantized models with FlashAttention-optimized kernels yields compound efficiency gains.

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