Papers
Topics
Authors
Recent
Search
2000 character limit reached

Sparse Transformer Algorithms (FlashAttention)

Updated 13 October 2025
  • Sparse transformer algorithms like FlashAttention are methods that reduce the quadratic complexity of attention by limiting token pair computations.
  • They leverage hardware-aware scheduling, tiling, and block-based strategies to achieve significant speedups and memory efficiency.
  • Adaptive and data-driven sparsity techniques dynamically optimize mask representations, enhancing model scalability and expressiveness.

Sparse transformer algorithms address the formidable computational and memory cost of the attention mechanism by exploiting algorithmic sparsity, memory hierarchy, data-dependent masking, and hardware-aware scheduling. FlashAttention and its extensions represent a major line of development in making attention practical at scale, alongside structured sparsification, adaptive and learned mask methods, and graph-centric formulations. This article surveys the principles and methods underpinning state-of-the-art sparse transformer algorithms as exemplified by FlashAttention and its descendants.

1. Computational Motivation and Foundations

The attention mechanism in transformers is dominated by the computation of the scaled dot-product attention: Attention(Q,K,V)=softmax(QKTdk+M)V,\mathrm{Attention}(Q, K, V) = \mathrm{softmax}\left(\frac{QK^T}{\sqrt{d_k}} + M\right) V, where Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k} for a sequence of length NN. The matrix multiplication and subsequent softmax normalization yield O(N2dk)\mathcal{O}(N^2 d_k) complexity for both computational cost and memory footprint—placing severe limits on achievable context length.

Sparse attention algorithms reduce this cost through one or more of the following strategies:

  • Limiting the set of tokens that each query attends to via a fixed or learned mask ("structured sparsity").
  • Exploiting hardware memory hierarchy through tiling and blocking to reduce memory reads/writes ("IO awareness").
  • Replacing dense softmax with sparse variants (e.g., α\alpha-entmax, adaptive masking).
  • Reformulating attention as a graph computation or conditional expectation, allowing for strict computational work minimization.

The goal is to reduce effective complexity, scale context length, and retain model expressiveness.

2. FlashAttention: IO-Aware Exact and Sparse Attention

FlashAttention (Dao et al., 2022) introduced an IO-aware, blockwise approach to exact (dense) softmax attention on GPUs. By partitioning QQ, KK, and VV into tiles that fit in on-chip SRAM, FlashAttention avoids materializing the N×NN \times N attention matrix, reducing global memory (HBM) accesses from Θ(N2)\Theta(N^2) to Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}0, with Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}1 the size of fast memory. Incremental numerically stable softmax computation across blocks maintains exactness, achieving Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}2 speedup and enabling context of Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}3–Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}4K tokens with linear memory scaling.

Block-sparse FlashAttention further skips computation on masked-out blocks, yielding IO complexity of

Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}5

where Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}6 is block density. This approach demonstrated up to Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}7 training speedup for GPT-2 and Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}8 end-to-end improvement for BERT-large, as well as higher-quality long-sequence models (Dao et al., 2022, Dao, 2023).

FlashAttention-2 (Dao, 2023) advances by optimizing non-matmul operations, parallelizing attention over the sequence length, and reducing shared memory use in CUDA blocks, reaching Q,K,V∈RN×dkQ, K, V \in \mathbb{R}^{N \times d_k}9–NN0 of theoretical peak FLOPs/s and enabling up to NN1 TFLOPs/s per A100 GPU in end-to-end training.

3. Algorithmic Extensions: Mask-Awareness, Graph Processing, and Adaptive Sparsity

Mask-Aware and Block-Based Methods

Recent work has focused on making FlashAttention and similar algorithms efficiently process arbitrary or structured sparse masks:

  • Binary Block Masking (Sharma et al., 2024): Preprocesses sparse binary masks into blockwise binary indicators; only nonzero blocks are processed, skipping fully zeroed regions, and achieves up to NN2 runtime improvement for complex masks in sequence packing and tree masking.
  • FlashMask (Wang et al., 2024): Encodes masks as sparse column-wise intervals (e.g., lower/upper triangular ranges), reducing mask memory from NN3 to NN4. This approach allows linear memory and kernel time scaling, efficient block skipping, and achieves NN5–NN6 end-to-end training speedup.
  • Flash Sparse Attention (Yan et al., 25 Aug 2025): Reorders kernel loops over query and key-value blocks to enable efficient execution of native, trainable sparse attention (NSA) for small GQA group sizes, yielding up to NN7 kernel speedup and NN8 faster end-to-end training compared to baseline NSA.

Graph-Centric and Work-Optimal Attention

Longer Attention Span (Tomczak et al., 31 Jan 2025) proposes representing attention as a sparse graph, where tokens are nodes and attention masks define edges. By computing only over explicit edges using COO or CSR storage or via pattern parametrization, and by performing online softmax statistics, the algorithm achieves theoretical and practical work optimality—computing precisely NN9 work, where O(N2dk)\mathcal{O}(N^2 d_k)0 is mask sparsity. On extremely long sequences (up to O(N2dk)\mathcal{O}(N^2 d_k)1 million tokens), this yields up to O(N2dk)\mathcal{O}(N^2 d_k)2 runtime speedup compared to FlashAttention.

Adaptive and Data-Driven Sparsity

SBM-Transformer (Cho et al., 2022) adopts a stochastic block model per attention head, sampling an expected O(N2dk)\mathcal{O}(N^2 d_k)3 set of edges (token pairs) in a data-adaptive fashion, with gradients propagated through the discrete mask using a straight-through estimator. The method achieves state-of-the-art performance for both sequence tasks and natural language understanding benchmarks with significant computational savings.

AdaSplash (Gonçalves et al., 17 Feb 2025) realizes efficient, adaptive sparse attention by combining the expressive O(N2dk)\mathcal{O}(N^2 d_k)4-entmax family (which generalizes softmax/entmax and automatically induces sparsity per input) with a hardware-efficient Halley–bisection solver and Triton kernels. Dynamic skipping at the block level attains speed and memory comparable to FlashAttention-2 for sequence lengths up to O(N2dk)\mathcal{O}(N^2 d_k)5K.

4. Hardware and Kernel Innovations

FlashAttention-inspired algorithms have driven extensive innovation at the kernel and hardware level:

  • Efficient tiling, tiling-based recomputation, and block masking eliminate memory constraints as sequence length grows (Dao et al., 2022, Wang et al., 2024).
  • FLASH-D (Alexandridis et al., 20 May 2025) replaces explicit softmax division and max subtraction in the recursion with a sigmoid-based update, reducing hardware area and power by over O(N2dk)\mathcal{O}(N^2 d_k)6 compared to state-of-the-art parallel accelerators, without compromising on kernel properties or accuracy.
  • SystolicAttention (Lin et al., 15 Jul 2025) fuses all FlashAttention operations—including softmax and non-matrix transformations—directly within a single systolic array by augmenting each PE with comparators and Split units for piecewise exponential approximation. This eliminates data transfer to vector cores and achieves O(N2dk)\mathcal{O}(N^2 d_k)7-O(N2dk)\mathcal{O}(N^2 d_k)8 higher utilization versus AWS NeuronCore-v2 and TPUv5e.
  • FPGA/ASIC-friendly sparse attention operators leveraging low-precision quantized Q/K vectors and Top-k preselection (with exact full-precision computation of only nontrivial entries) allow kernel and energy efficiency of O(N2dk)\mathcal{O}(N^2 d_k)9 over CPU and α\alpha0 over GPU (Peng et al., 2022).

5. Structured and Adaptive Sparsification: Methods and Implications

Structured sparsification strategies seek to balance expressiveness, sample efficiency, and computational scaling:

  • Combiner (Ren et al., 2021) achieves full attention with sub-quadratic cost by factorizing α\alpha1 in attention into structured partitions. Direct and pooled abstraction terms allow each token to reach all others, while fixed/log/axial partition patterns yield costs of α\alpha2 or α\alpha3.
  • SPION (Yoon et al., 2023) uses convolutional and flood-filling patterns to determine block-sparse layouts layer-wise, dynamically adapting the sparsity pattern in each transformer layer. This adaption leads to up to α\alpha4 speedup over previous sparse transformers and strong benchmark performance.
  • SBM-Transformer (Cho et al., 2022) and VSA (Zhang et al., 19 May 2025) dynamically learn or predict sparsity patterns that adapt to each sequence. VSA, in particular, deploys a coarse-to-fine selection (mean-pool cubes α\alpha5 top-k critical cubes α\alpha6 block-sparse fine attention) in a design maintaining α\alpha7 FlashAttention MFU with α\alpha8 reduction in attention FLOPs, achieving up to α\alpha9 attention and QQ0 end-to-end speedup in large video DiTs.

The Spark Transformer (You et al., 7 Jun 2025) enforces top-QQ1 activation sparsity in both the FFN and attention blocks, using a statistical thresholding mechanism for hardware-friendly, predictable sparsity. Parameter reallocation creates an integrated, low-cost predictor to select which neurons and tokens should be active, yielding up to QQ2 FLOP reduction and substantial wall-time benefits without quality loss.

6. Stability, Expressivity, and Learning-Theoretic Considerations

Stability and expressivity remain central to sparsification strategies:

  • FlashAttention incurs an order of magnitude higher numeric deviation in BF16 vs. baseline dense attention during the forward pass due to tile-rescaling (Golden et al., 2024). However, this deviation induces at most QQ3–QQ4 smaller weight divergence during training than switching between FP32 and FP16, demonstrating that system-level optimizations remain stable under practical training regimes.
  • Universal approximation properties have been theoretically validated for both structured sparsifiers (e.g., BigBird, Combiner) and data-adaptive alternatives such as SBM-Transformer (Cho et al., 2022), provided certain connectivity and self-loop conditions are met.
  • Chain-of-thought (CoT)–induced sparsity leads to interpretable, nearly one-hot attention patterns (Wen et al., 2024). CoT decomposes computation into steps with minimal dependency—each step attends to only a small, specific subset of prior tokens—leading to polynomial sample complexity in settings where dense attention suffers from exponential inefficiency.

7. Generalization, Domains, and Future Directions

Sparse transformer algorithms increasingly support multi-modal and multi-granularity sparsity patterns:

  • FlashOmni (Qiao et al., 29 Sep 2025) introduces sparse "symbols" for both feature caching and block-sparse skipping, enabling a unified, dynamically decoded sparse attention kernel that can execute diverse strategies on a single efficient engine. Near-linear speedup (matching sparsity ratio) and up to QQ5 acceleration are demonstrated.
  • SFi-Former (Li et al., 29 Apr 2025) employs QQ6-regularized network flow energy minimization to induce sparsity in graph transformer attention, outperforming dense and alternative sparse graph transformers on long-range benchmarks with reduced overfitting.
  • Continued research targets adaptive mask representations, further reductions in scattering non-contiguous memory access overhead, generic hardware-agnostic kernels, and combinations of sparsity with other scaling strategies (e.g., MoE, low-rank, quantized models).

The present landscape of algorithms—from FlashAttention and graph-based implementations to trainable, block-, and adaptive sparse schemes—demonstrates the ongoing, multi-faceted effort to scale transformer models to unprecedented context lengths and domains while preserving accuracy and theoretical capacity. The convergence of algorithmic, hardware, and data-driven advances underpins the practical future of sparse transformers.

Definition Search Book Streamline Icon: https://streamlinehq.com
References (19)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Sparse Transformer Algorithms (Flash Attention).