FlashAttention is an IO-aware exact attention algorithm that computes the standard self-attention output without approximation while dramatically reducing the number of memory reads and writes to GPU high-bandwidth memory (HBM). By fusing operations into a single CUDA kernel and tiling the computation to keep intermediate matrices in fast on-chip SRAM, it achieves significant speedups and reduces the memory footprint from quadratic to linear in sequence length.
Glossary
FlashAttention

What is FlashAttention?
An algorithm that accelerates the self-attention mechanism by minimizing reads and writes between GPU high-bandwidth memory and on-chip SRAM.
This innovation is critical for domain-specific legal pre-training, where processing long documents like multi-page contracts and judicial opinions requires extended sequence lengths. By eliminating the memory bottleneck of traditional attention, FlashAttention enables the pre-training of legal language models on full documents rather than truncated chunks, preserving cross-paragraph context essential for accurate statutory interpretation and citation network analysis.
Key Features of FlashAttention
FlashAttention is an algorithm that computes exact self-attention with dramatically reduced memory reads/writes, making it feasible to pre-train models on very long legal documents.
IO-Awareness: Tiling the Computation
FlashAttention's core innovation is IO-awareness—it accounts for the speed hierarchy between GPU memory levels. Instead of materializing the full N×N attention matrix in slow High Bandwidth Memory (HBM), it:
- Tiles the Q, K, and V matrices into blocks that fit in fast SRAM
- Computes softmax attention incrementally within each block
- Writes only the final output back to HBM
This reduces HBM reads/writes from O(N²) to O(N²/d), where d is the head dimension. For a 4K-token legal document, this can mean a 7.6x reduction in memory operations.
Online Softmax with Rescaling
Traditional softmax requires two passes over the data: one to find the maximum value for numerical stability, and another to compute the exponential sum. FlashAttention fuses these into a single online softmax algorithm that:
- Maintains running statistics (m, ℓ) as it iterates through blocks
- Rescales previously computed partial sums when a new maximum is encountered
- Produces mathematically identical output to the two-pass version
This is critical for legal models, where attention scores across long documents can have extreme dynamic ranges, and numerical precision directly impacts citation accuracy.
Recomputation Over Storage
In the backward pass, standard attention implementations store the full attention matrix from the forward pass, consuming O(N²) memory. FlashAttention recomputes attention scores on-the-fly during backpropagation:
- Stores only the softmax normalization statistics (m, ℓ) from the forward pass
- Recalculates QK^T values in SRAM during the backward pass
- Trades a small increase in FLOPs for a massive reduction in memory footprint
This recomputation strategy enables training on 4-8x longer sequences than standard attention, directly enabling processing of full contracts and multi-page judicial opinions without truncation.
FlashAttention-2: Parallelism and Work Partitioning
FlashAttention-2 improves upon the original by optimizing work partitioning across thread blocks and reducing non-matmul FLOPs:
- Forward pass: Parallelizes over the sequence length dimension instead of batch and head dimensions, increasing occupancy
- Backward pass: Parallelizes over the column dimension, eliminating the need for atomic adds and inter-thread communication
- Reduces the number of non-matrix-multiply operations, which are disproportionately slow on modern GPUs
These optimizations yield a 2x speedup over FlashAttention-1, making long-document legal pre-training economically viable on existing GPU clusters.
FlashAttention-3: Hopper Architecture Exploitation
FlashAttention-3 targets NVIDIA's Hopper GPU architecture (H100) with three new techniques:
- Warp-specialization: Dedicated warps for data movement (TMA) and computation, overlapping asynchronous loads with math
- Ping-pong scheduling: Alternates between two sets of registers to hide memory latency
- FP8 low-precision: Leverages Hopper's native FP8 tensor cores for attention computation, further reducing memory bandwidth
On H100 GPUs, FlashAttention-3 achieves 1.5-2.0x speedup over FlashAttention-2, reaching up to 740 TFLOPS—critical for training legal models on entire case law corpora within reasonable timeframes.
Memory Savings: Linear with Sequence Length
The memory footprint of standard attention scales quadratically with sequence length—a 16K-token legal document requires 256x more attention memory than a 1K-token passage. FlashAttention's memory scales linearly with sequence length:
- Standard Attention: O(N²) memory for the attention matrix
- FlashAttention: O(N) memory for the output and statistics
- Enables processing of 64K+ token sequences on a single GPU
This linear scaling is the key enabler for domain-adaptive pre-training on full legal documents. Without it, models must truncate or chunk documents, breaking cross-reference reasoning between distant sections of a contract or opinion.
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.
Frequently Asked Questions
Clear, technically precise answers to the most common questions about the IO-aware exact attention algorithm that is revolutionizing long-context legal AI.
FlashAttention is an IO-aware exact attention algorithm that computes the standard self-attention operation with significantly reduced memory reads and writes between GPU High Bandwidth Memory (HBM) and on-chip SRAM. It works by tiling the attention matrix computation into smaller blocks that fit entirely in SRAM, performing the softmax reduction incrementally without ever materializing the full N×N attention matrix in HBM. The algorithm uses a numerically stable online softmax technique and a custom CUDA kernel to fuse operations, achieving a 2-4x wall-clock speedup and reducing memory complexity from O(N²) to O(N) for sequence length N. This makes it feasible to train transformers on very long legal documents without approximation or sparsity.
Related Terms
Core concepts that underpin the IO-aware exact attention algorithm and its role in scaling legal language models.
IO-Awareness
The foundational design principle behind FlashAttention. Traditional attention is compute-bound on short sequences but memory-bound on long ones. An IO-aware algorithm minimizes high-bandwidth memory (HBM) reads and writes by fusing operations in fast SRAM. Key mechanism: Tiling the attention matrix into blocks that fit entirely in SRAM, computing softmax incrementally without materializing the full N×N matrix. This reduces HBM access from O(N²) to O(N²/M) where M is SRAM size.
Online Softmax
A numerically stable algorithm for computing softmax over a sequence incrementally, without storing the entire input vector. FlashAttention uses a variant called tiled online softmax that processes attention scores in blocks. How it works: Maintains running statistics (max value and sum) across tiles, rescaling previous partial results when a new maximum is discovered. This is what enables exact attention computation without the O(N²) memory footprint of storing the full attention matrix.
GPU Memory Hierarchy
Understanding FlashAttention requires knowing the GPU memory stack. HBM (High Bandwidth Memory): Large capacity (40-80GB on A100/H100) but high latency. SRAM (Static RAM): Small (192KB per streaming multiprocessor on A100) but ~10x faster bandwidth. Standard attention writes the full attention matrix to HBM. FlashAttention keeps computation in SRAM by splitting the Q, K, V matrices into tiles, computing partial softmax results, and only writing the final output to HBM.
Recomputation in Backward Pass
A critical design choice in FlashAttention that trades compute for memory. During the forward pass, the algorithm does not store the full attention matrix for the backward pass. Instead, it stores only the softmax normalization statistics (m and l). During backpropagation, the attention scores are recomputed on-the-fly from the stored Q, K, V tiles. This avoids the O(N²) memory cost of caching attention weights while remaining numerically exact. The recomputation overhead is offset by the massive memory savings.
FlashAttention-2 & 3
Successive iterations improved on the original algorithm. FlashAttention-2 (2023) reduced non-matmul FLOPs by restructuring the loop order to parallelize over the sequence length dimension, achieving ~2x speedup on A100 GPUs. FlashAttention-3 (2024) leverages Hopper architecture features: asynchronous execution of TMA (Tensor Memory Accelerator) and WGMMA (Warp Group Matrix Multiply-Accumulate) instructions, plus FP8 low-precision computation, reaching 1.5-2.0x over FlashAttention-2 on H100 GPUs.
Block-Sparse FlashAttention
An extension combining IO-awareness with sparsity for further speedups. Instead of computing all pairwise attention scores, a sparsity mask identifies which blocks of the attention matrix are likely to contain meaningful connections. Common patterns include sliding window (local attention), global tokens, and dilated sliding window. Block-sparse FlashAttention skips entire tile computations, reducing the effective sequence length. This is particularly useful for legal documents where attention can be localized to sections.

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