FlashAttention is an optimized algorithm for computing the attention mechanism in Transformer models that reduces memory usage from quadratic to linear with respect to sequence length. It achieves this through tiling to keep data in fast SRAM and recomputation (backward pass only) to avoid storing the large attention matrix, dramatically speeding up both training and inference, especially for long sequences. This is a core technique for enabling efficient large language models on edge hardware.
Glossary
FlashAttention

What is FlashAttention?
FlashAttention is a foundational algorithm for accelerating Transformer models by optimizing the memory and compute patterns of the attention mechanism.
The algorithm's efficiency stems from its hardware-aware design, which minimizes slow High-Bandwidth Memory (HBM) accesses—the primary bottleneck for attention. By fusing operations and leveraging kernel optimization, FlashAttention provides exact attention outputs with significant speedups. Its successors, FlashAttention-2 and FlashAttention-3, further refine these principles. This optimization is critical for on-device inference, reducing latency and power consumption for real-time applications.
Core Technical Features of FlashAttention
FlashAttention is an I/O-aware, exact attention algorithm that uses tiling and recomputation to achieve significant speed and memory improvements over the standard implementation.
IO-Aware Exact Attention
FlashAttention is designed to be I/O-aware, meaning it explicitly optimizes for the memory hierarchy of modern hardware (GPU SRAM vs. HBM). It minimizes the number of slow reads/writes to high-bandwidth memory (HBM) by keeping data in fast SRAM as long as possible. Crucially, it computes the exact softmax attention with no approximations, maintaining the same numerical output as the standard algorithm.
- Key Insight: The bottleneck for standard attention on long sequences is not compute but memory bandwidth.
- Mechanism: By fusing the entire attention operation (matrix multiplies, masking, softmax, dropout) into a single GPU kernel, it avoids writing and reading large intermediate matrices to HBM.
Tiling (Forward Pass)
Tiling is the core technique that enables FlashAttention to process long sequences with linear memory complexity. The algorithm splits the input Query (Q), Key (K), and Value (V) matrices into smaller blocks that fit into SRAM.
- Process: It loads a block of Q and a block of K/V into SRAM, computes a 'patch' of the attention scores, applies the softmax locally, and updates the output block for that Q segment.
- Result: The full attention matrix, which is size O(N²), is never materialized in HBM. Only the final output O of size O(N) is written, reducing memory usage from quadratic to linear in sequence length.
Recomputation (Backward Pass)
To achieve its memory savings during training, FlashAttention uses recomputation (also known as gradient checkpointing) in the backward pass. Instead of storing the large intermediate attention matrices needed for gradients, it recomputes them on-the-fly from the stored outputs.
- Trade-off: It trades off extra FLOPs for dramatically reduced memory usage. The recomputation is fast because it is also I/O-optimized and performed chip-by-chip.
- Benefit: This allows training Transformers with much longer sequence lengths on the same hardware, as the peak memory is dominated by the model's activations, not the attention intermediates.
Memory Complexity: O(N)
The primary achievement of FlashAttention is reducing the memory complexity of the attention computation with respect to sequence length (N).
- Standard Attention: Requires O(N²) memory to store the attention matrix for the forward pass, which becomes the bottleneck for long sequences (e.g., 16K, 32K tokens).
- FlashAttention: Requires only O(N) memory because it never fully materializes the NxN matrix. It streams through the computation, keeping only blocks in SRAM.
- Impact: This linear scaling enables training and inference on context lengths previously infeasible, directly enabling longer-context Large Language Models (LLMs).
Causal Masking & Dropout Support
FlashAttention natively and efficiently supports essential Transformer training features like causal masking and dropout within its fused kernel.
- Causal Masking: For decoder-only models (like GPT), the attention mask prevents tokens from attending to future tokens. FlashAttention applies this mask during the tiled computation without needing to create a full NxN mask matrix.
- Dropout: Attention dropout, which randomly zeros attention scores during training for regularization, is applied block-wise during the softmax calculation within the kernel. This maintains the correct Bernoulli randomness per attention score while staying within the tiled framework.
FlashAttention vs. Standard Attention
A technical comparison of the standard attention algorithm and its optimized variant, FlashAttention, focusing on memory, speed, and hardware utilization for Transformer models.
| Feature / Metric | Standard Attention | FlashAttention | Primary Impact |
|---|---|---|---|
Algorithmic Complexity (Memory) | O(N²) | O(N) | Enables longer sequence lengths |
Memory Access Pattern | HBM-Bound (Inefficient) | SRAM-Optimized (Efficient) | Reduces I/O bottleneck |
Core Optimization Technique | None (Naive Implementation) | Tiling & Recomputation | Trades FLOPs for memory bandwidth |
Exactness of Output | ✅ Exact | ✅ Exact (Numerically Equivalent) | No loss of accuracy |
Supports Dropout & Masking | ✅ Yes | ✅ Yes | Full training compatibility |
Backward Pass Optimization | ❌ No (Stores full NxN matrix) | ✅ Yes (Recomputes on-the-fly) | Dramatically reduces training memory |
Hardware Requirement | None (General) | Fast SRAM (e.g., GPU L1/L2 Cache) | Leverages modern accelerator architecture |
Typical Speedup (Training, Long Seq) | 1x (Baseline) | 2-4x | Faster iteration cycles |
Primary Constraint | GPU Memory Capacity | SRAM Size per Streaming Multiprocessor | Dictates tile/block size |
Frameworks and Libraries Using FlashAttention
FlashAttention's core algorithm has been integrated into major deep learning frameworks and specialized inference engines, enabling developers to leverage its memory and speed optimizations with minimal code changes.
Specialized Inference Servers: vLLM & TGI
High-performance inference servers implement FlashAttention to optimize the Key-Value (KV) Cache, a major bottleneck in LLM serving.
- vLLM: Uses a modified version of FlashAttention as part of its PagedAttention algorithm to manage the KV cache efficiently in non-contiguous memory, enabling high throughput.
- Text Generation Inference (TGI): The backend for Hugging Face's inference endpoints, it utilizes FlashAttention-2 to reduce memory bandwidth usage and improve token generation speed.
- Impact: These systems demonstrate FlashAttention's critical role in production serving, where memory efficiency directly translates to cost and latency savings.
JAX/Flax with `fla`
The JAX ecosystem has native implementations of FlashAttention, offering high performance on TPUs and GPUs.
flalibrary: A standalone JAX library providing FlashAttention and related variants (FlashAttention-2, Blockwise Attention).- Integration Path: Can be used directly in models built with Flax or pure JAX.
- Advantage: Benefits from JAX's just-in-time (JIT) compilation, allowing the FlashAttention kernel to be further optimized and fused with surrounding operations for a specific computational graph.
Frequently Asked Questions
FlashAttention is a foundational algorithm for efficient Transformer execution. These FAQs address its core mechanisms, benefits, and practical applications in on-device and server-side inference.
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 intermediate attention matrix to slow HBM (High Bandwidth Memory). It works through two key techniques: tiling and recomputation. The algorithm splits the input queries, keys, and values into blocks, loads them from HBM to SRAM, computes the local attention output for that block, and then updates the final output with a running statistics method (online softmax). This process eliminates the need to materialize the full, quadratic-sized attention matrix in HBM, trading off extra FLOPs for significantly reduced memory reads/writes, which is the true bottleneck.
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 for efficient Transformer execution. These related concepts are essential for understanding the broader ecosystem of inference optimization techniques.
Memory Footprint
The total amount of system memory (RAM) required to load and execute a machine learning model, including its parameters, activations, and intermediate buffers. FlashAttention directly targets the massive memory footprint of the attention mechanism's intermediate matrices (QK^T and softmax output), which scale quadratically (O(N²)) with sequence length. By using tiling and recomputation, it reduces this to linear (O(N)) memory usage, enabling the processing of much longer sequences within the same hardware constraints.
PagedAttention
An attention algorithm used in systems like vLLM that manages the Key-Value (KV) cache in non-contiguous, paged memory blocks. While FlashAttention optimizes the computation of attention during a single forward pass, PagedAttention optimizes the memory management of cached attention states across multiple decoding steps in autoregressive generation.
- Key Difference: FlashAttention is a training/inference kernel; PagedAttention is a serving-system memory manager.
- Synergy: They address complementary bottlenecks. An optimized serving system might use FlashAttention-style kernels internally while employing PagedAttention for efficient KV cache allocation across requests.
Kernel Optimization
The hand-tuning or auto-generation of low-level code (kernels) for fundamental operations like matrix multiplication to maximize performance on specific hardware. FlashAttention is a premier example of algorithmic kernel fusion.
- Fuses multiple steps (matrix multiply, masking, softmax, dropout, second matrix multiply) into one monolithic CUDA kernel.
- Avoids expensive reads/writes of intermediate tensors to and from High-Bandwidth Memory (HBM).
- Leverages hardware hierarchy by keeping data in fast SRAM (shared memory/registers) during the tiled computation. This contrasts with naive implementations that launch separate kernels for each step, creating significant memory I/O overhead.
Operator Fusion
A compiler optimization that combines multiple sequential neural network operations into a single kernel to reduce memory accesses and kernel launch overhead. FlashAttention implements a manual, algorithmic form of operator fusion specifically for the attention block.
- Standard Fusion: A compiler might fuse a convolution, batch norm, and ReLU activation.
- FlashAttention Fusion: Manually fuses the entire, complex attention computation (QK^T, mask, softmax, dropout, attention * V).
- Benefit: Eliminates the need to write the gigantic intermediate attention matrix (size N x N) to main memory, which is the primary source of its speed and memory savings.
Mixed Precision
A computational technique using different numerical precisions within a single model to balance speed, memory, and stability. FlashAttention-2 introduced significant optimizations for mixed-precision training.
- It reduces the number of non-matrix-multiply operations (like softmax rescaling), which are bottlenecks in lower precision.
- It better overlaps the flashy matrix multiplies (on Tensor Cores) with other operations.
- It implements online softmax with tiling to maintain numerical stability when processing long sequences in FP16/BF16, preventing overflows in the softmax computation.
Continuous Batching
An inference optimization for autoregressive models where new requests are dynamically added to a running batch as previous requests finish generation. While distinct, it shares a high-level goal with FlashAttention: maximizing hardware utilization.
- FlashAttention: Maximizes utilization within a single forward/backward pass for one sequence by optimizing memory I/O and kernel execution.
- Continuous Batching: Maximizes utilization across multiple sequences/requests by ensuring the GPU is never idle, dynamically scheduling tokens.
- Combined Use: High-performance inference servers (e.g., using vLLM with PagedAttention) often employ both continuous batching for request scheduling and FlashAttention-optimized kernels for the core attention computation.

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