Flash Attention is an IO-aware exact attention algorithm that computes the standard attention operation without approximation while significantly reducing the number of reads and writes to GPU high-bandwidth memory (HBM). By fusing the softmax reduction into a single kernel and tiling the computation to operate entirely within faster SRAM, it avoids materializing the large intermediate attention matrix, which is quadratic in sequence length.
Glossary
Flash Attention

What is Flash Attention?
An algorithm that accelerates the attention mechanism in Transformer models by minimizing reads and writes to high-bandwidth memory through kernel fusion and tiling.
This technique achieves wall-clock speedups of 2-4x and reduces memory consumption from O(N²) to O(N), enabling longer context windows and faster training for large language models. It is a critical optimization in modern self-hosted inference stacks, often integrated into frameworks like vLLM and PyTorch, and is foundational for efficient KV cache management and high-throughput serving.
Key Features of Flash Attention
Flash Attention is an algorithm that computes exact attention with significantly fewer high-bandwidth memory reads and writes by fusing operations and tiling the computation. It delivers wall-clock speedups and memory savings without any approximation.
IO-Awareness and Memory Hierarchy
Flash Attention fundamentally restructures the attention computation to be IO-aware, meaning it accounts for the speed disparity between GPU High Bandwidth Memory (HBM) and on-chip Static Random-Access Memory (SRAM). Standard attention materializes the full N×N attention matrix in slow HBM, creating a quadratic memory bottleneck. Flash Attention instead tiles the computation into blocks that fit entirely in fast SRAM, performing the softmax reduction incrementally without ever writing the full intermediate matrix to HBM. This minimizes data movement, the primary bottleneck in transformer inference and training.
Tiling and Recomputation Strategy
The algorithm employs a tiling strategy that decomposes the query, key, and value matrices into blocks. For each query block, it iterates over key-value blocks, accumulating softmax statistics in SRAM. Crucially, Flash Attention uses recomputation in the backward pass: rather than storing the massive attention matrix from the forward pass, it recalculates it on-the-fly using the stored softmax normalization statistics. This trades a small amount of extra computation for a dramatic reduction in memory footprint, enabling training with longer sequences on existing hardware.
Online Softmax and Numerical Stability
A core mathematical innovation is the online softmax algorithm, also known as tiled softmax. Standard softmax requires two passes over the data: one to find the maximum value for numerical stability and another to compute the exponential sum. Flash Attention fuses these into a single pass per tile by maintaining running statistics. It uses the standard log-sum-exp trick to ensure numerical stability across blocks, rescaling partial sums as new maxima are encountered. This guarantees the output is bitwise identical to the standard attention computation, making it an exact algorithm, not an approximation.
Kernel Fusion and CUDA Implementation
Flash Attention is implemented as a single, highly optimized CUDA kernel that fuses multiple operations into one GPU program. Instead of launching separate kernels for matrix multiplication, masking, softmax, and dropout, the entire attention operation is compiled into a monolithic kernel. This eliminates the overhead of launching multiple kernels and, more importantly, avoids the redundant round-trips to HBM that occur between kernel boundaries. The fused kernel carefully manages shared memory allocation and thread block scheduling to maximize occupancy and hide memory latency.
Impact on Long-Context Training
By reducing the memory footprint of attention from quadratic to linear in sequence length, Flash Attention directly enables training on significantly longer contexts. Models like GPT-4 and Llama 3 rely on this algorithm to scale to context windows of 128k tokens and beyond. Without it, the attention matrix for a 128k sequence would require over 64 GB of memory per attention head, making training infeasible on current hardware. Flash Attention makes long-context training practical on standard GPU clusters, unlocking applications in long-document summarization, codebase analysis, and extended multi-turn conversation.
Frequently Asked Questions
Clear, technically precise answers to the most common questions about the IO-aware exact attention algorithm that has become fundamental to modern transformer efficiency.
Flash Attention is an IO-aware exact attention algorithm that computes the standard attention mechanism without ever materializing the full N x N attention matrix in high-bandwidth memory (HBM). It works by tiling the query, key, and value matrices into smaller blocks that fit entirely within GPU SRAM, performing the softmax reduction incrementally using an online stable algorithm. By fusing the attention computation into a single CUDA kernel and recomputing the attention matrix during the backward pass rather than storing it, Flash Attention reduces HBM reads and writes from O(N²) to O(N² / M), where M is the SRAM size. This delivers 2-4x wall-clock speedups and reduces memory consumption from quadratic to linear in sequence length, enabling transformers to process much longer contexts without approximation.
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
Flash Attention is a foundational algorithm that enables efficient long-context transformers. These related concepts form the ecosystem of memory management, serving infrastructure, and positional encoding techniques that build upon or complement IO-aware attention.
Paged Attention
A memory management technique that partitions the KV cache into non-contiguous, fixed-size blocks, analogous to virtual memory paging in operating systems. This eliminates fragmentation and enables near-optimal memory utilization during serving.
- Handles variable-length sequences without pre-allocation
- Enables memory sharing across sequences for techniques like parallel sampling
- Core innovation behind the vLLM inference engine
- Works synergistically with Flash Attention's tiled computation
KV Cache
A memory buffer that stores the computed Key and Value tensors from all previous tokens during autoregressive generation. Without it, each new token would require recomputing attention over the entire sequence.
- Memory grows linearly with batch size × sequence length × hidden dimensions
- Flash Attention reduces the HBM reads/writes of KV cache operations
- Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) shrink KV cache size by sharing key-value heads
- Primary bottleneck for long-context serving
Grouped-Query Attention (GQA)
An attention mechanism that divides query heads into groups, with each group sharing a single set of Key and Value heads. This interpolates between the quality of Multi-Head Attention (MHA) and the speed of Multi-Query Attention (MQA).
- Used in Llama 2 70B, Llama 3, and Mistral models
- Reduces KV cache size by a factor equal to the number of query heads per group
- Pairs naturally with Flash Attention's memory-efficient tiling
- Maintains near-MHA quality while approaching MQA throughput
RoPE Scaling
A family of techniques that extend the effective context window of models using Rotary Position Embeddings (RoPE) beyond their pre-trained length without full retraining.
- Linear scaling: Divides position indices by a scaling factor
- NTK-aware scaling: Non-linear interpolation preserving high-frequency information
- YaRN: Combines NTK scaling with temperature adjustment for extreme length extension
- Enables Flash Attention to operate on sequences far exceeding original training length
Prefix Caching
An optimization that stores and reuses the computed KV cache for a shared prompt prefix across multiple generation requests. When multiple queries share a common system prompt or few-shot examples, the attention computation for that prefix is performed only once.
- Dramatically reduces Time To First Token (TTFT) for cached prefixes
- Complements Flash Attention by avoiding redundant IO-aware computation
- Critical for chatbot serving with long system prompts
- Implemented in serving engines like vLLM and SGLang

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