📄 Yuan et al. (2025) Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention. (arX⠶2502.11089v1 [cs⠶CL])
Part 1 of a series on the DeepSeek NSA paper.
In this section, we cover:
- Introduction and motivation: Why sparse attention?
- Background: The need for sparse attention in long-context modeling
- Overview of full attention limitations
- Survey of existing sparse attention methods
- DeepSeek’s background and role in developing NSA
1.1 Introduction and Motivation
Transformers have revolutionized NLP, but their ability to handle very long inputs remains a critical challenge. Long-context modeling is increasingly important for advanced language tasks like multi-document reasoning, code context integration, and long dialogues. Next-generation LLMs are expected to process contexts spanning tens of thousands of tokens, far beyond the typical 512 or 2048-token limits of early Transformer models (⇒). However, the standard self-attention mechanism is a major bottleneck – its computational cost scales quadratically with sequence length, making naive extension to long inputs prohibitively expensive ([2004.05150] Longformer: The Long-Document Transformer). In fact, on current hardware a Transformer with full attention is effectively limited to input sequences of only a few hundred tokens before running into memory and speed constraints (⇒). This quadratic explosion in computation and memory is the broader problem that sparse attention mechanisms seek to address.
Sparse attention has emerged as a promising direction to enable efficient long-context modeling ([2502.11089] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention). By restricting each token to attend to only a subset of other tokens (rather than all pairs), sparse attention can dramatically reduce the number of operations and the memory footprint. The key motivation is to maintain model capability on long sequences while sparsifying the attention structure to cut costs. Native Sparse Attention (NSA) is introduced against this backdrop – it is a new attention mechanism designed specifically to handle ultra-long contexts efficiently. The NSA approach integrates algorithmic innovations in how attention sparsity is structured and aligns these choices with hardware for maximum throughput ([2502.11089] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention). The specific motivation behind NSA is to achieve end-to-end efficiency: not only speeding up inference on long sequences, but also enabling efficient training on long contexts from scratch (hence "natively trainable" sparse attention). In short, NSA is motivated by the need for an attention mechanism that can scale to 64K-token contexts and beyond without the untenable cost of full attention, all while preserving (or even improving) the model’s performance compared to dense attention.
1.2 Background – The Need for Sparse Attention
Attention Mechanism Refresher: In a Transformer’s self-attention, each token (position) computes attention weights to every other token, typically via a softmax over dot-product scores. This allows the model to integrate information globally across the sequence. Formally, given a sequence length , standard self-attention computes an matrix of pairwise attention scores. This is powerful for modeling dependencies, but its cost grows as in both computation and memory. For example, doubling the sequence length roughly quadruples the number of attention score computations. For a long sequence (say tokens), there would be on the order of pairwise interactions – clearly infeasible to compute or even store directly.
Why Sparse Attention for Long Contexts: As context lengths stretch into the thousands or tens of thousands, the quadratic cost of full attention becomes the dominant barrier. Sparse attention addresses this by limiting the attention connections for each token, thereby reducing complexity (often to linear or near-linear in ). Intuitively, in long documents or transcripts, a given token likely only needs to attend to a limited subset of other tokens – for example, nearby tokens for local coherence, plus a few key distant tokens that carry relevant global information. By sparsifying attention, we exploit this intuition: the model focuses on the relevant parts of the context rather than wasting computation on all parts. This is critical for long-context modeling because it enables tractable processing of long sequences that would otherwise exhaust memory or computation budgets. In summary, sparse attention provides a way to extend Transformers to long documents by dropping most irrelevant pairwise interactions while hopefully retaining the important ones, thus striking a balance between sequence length and computational feasibility ([2004.05150] Longformer: The Long-Document Transformer). A number of efficient Transformer variants have been proposed to capitalize on this idea, each imposing a different sparse pattern or approximation on the attention matrix to cut down the blow-up.
1.3 Overview of Full Attention Limitations
Standard full self-attention’s inefficiency stems from its complexity in sequence length . Both computation and memory usage scale quadratically: computing the attention matrix involves dot-products, and storing it requires space. This quickly becomes intractable for large . For instance, the original Transformer could comfortably handle sequences of length 512, but processing just 4× longer sequences (2048 tokens) means more attention computations. In practice, even on modern accelerators, memory is often the first limiting factor – self-attention can require storing huge intermediate matrices, preventing long sequences from fitting in GPU memory ([2112.05682] Self-attention Does Not Need Memory). Rabe & Staats (2021) note that while time complexity remains quadratic, reducing attention’s memory footprint can allow processing sequences that would otherwise be impossible ([2112.05682] Self-attention Does Not Need Memory). This memory bottleneck is precisely what recently popular methods like FlashAttention target.
Mathematically, if is the hidden dimension, naive attention computation is and memory is (for the attention weights). The quadratic term dominates for large . Beyond just complexity order, the constant factors are significant: dense matrix multiplication operations can be optimized on GPUs, but an attention matrix for large might not even fit in fast memory. As a concrete example, a 32K-token sequence would produce an attention matrix of size elements per head. Even in half precision (2 bytes per element), that’s ~2 GB of memory just to hold the attention scores for one layer. This is clearly untenable across dozens of layers in a modern LLM. Thus, full self-attention is both compute-heavy and memory-heavy, causing unacceptable slowdowns and memory overflows as context length grows. In summary, full attention’s scaling fundamentally limits the contexts we can model – as the BigBird authors succinctly put it, this quadratic requirement means typical Transformers could only handle 512 tokens on available hardware, greatly limiting their applicability to tasks requiring longer context (⇒).
1.4 Survey of Existing Sparse Attention Methods
Over the past few years, many approaches have been proposed to make attention more efficient by introducing sparsity or other structural approximations. We briefly survey a few key methods and their limitations, to contrast them with NSA:
-
Longformer (Beltagy et al., 2020): Longformer introduced an attention pattern that scales linearly with sequence length, by combining local windowed attention with a few global tokens ([2004.05150] Longformer: The Long-Document Transformer). Each token attends to its nearest neighbors within a fixed window (for local context), and certain special tokens (like [CLS] in classification tasks) attend globally to all tokens. This reduces complexity from to (with the window size). Longformer’s sparse pattern is static and task-motivated – it’s a drop-in replacement for full attention in long documents ([2004.05150] Longformer: The Long-Document Transformer). The approach enabled processing of documents with thousands of tokens and showed improved performance on long-text tasks compared to baseline Transformers. Limitations: Longformer’s fixed window means long-range interactions beyond the window require multiple layers to propagate (multiple hops). Important global dependencies are only captured via the few designated global tokens, which may not generalize to every scenario. Moreover, while the sliding window attention is hardware-friendly (it can be implemented as banded matrices or convolutions), the model still lacks a mechanism to dynamically focus on arbitrary distant tokens that might be relevant – the pattern is pre-defined.
-
BigBird (Zaheer et al., 2020): BigBird builds on ideas from Longformer and extends them with a more theoretically complete sparse pattern. It uses block-sparse attention composed of three types of connections: local (each token attends to a small neighborhood), random sparse connections, and a set of global tokens that every token can attend to (Understanding BigBird's Block Sparse Attention) (Understanding BigBird's Block Sparse Attention). BigBird’s sparse attention was shown to be a universal approximator of sequence functions and even Turing-complete, meaning it can in principle capture the same dependencies as full attention given enough layers (⇒). Importantly, BigBird reduced the attention complexity to linear () and demonstrated the ability to handle sequences 8× longer (up to 4096 tokens) than the full-attention baseline on the same hardware (⇒). To address hardware efficiency, BigBird introduced block sparsity: instead of individual token sparsity (which leads to lots of small, scattered memory accesses), it attends with larger blocks of tokens, enabling more efficient matrix multiplication kernels (Understanding BigBird's Block Sparse Attention). Limitations: While block-sparse patterns improve hardware utilization, they still may not fully match the efficiency of dense matrix ops, especially for extremely long sequences. The pattern in BigBird is mostly fixed (random attention is fixed once chosen). This means the model does not learn which tokens to attend to – some capacity may be wasted on unimportant tokens due to random selection, and some important long-range links might still require several hops unless captured by the random links. Moreover, BigBird and Longformer were primarily validated on tasks like long-text classification or QA and in relatively moderate sequence lengths (e.g. 4k). For very large sequences (e.g. 32k-64k tokens), even linear scaling can become heavy, and careful caching or additional structure might be needed to remain efficient.
-
FlashAttention (Dao et al., 2022): FlashAttention takes a different angle – it does not change the attention pattern at all (it computes exact full attention) but optimizes how it’s computed on hardware (FlashAttention 2: making Transformers 800% faster w/o approximation). FlashAttention is an algorithm that avoids explicitly materializing the attention matrix in memory. Instead, it computes attention in tiled blocks, storing only partial results in on-chip memory and streaming through the sequence in chunks (FlashAttention-2: Faster Attention with Better Parallelism and Work ...). This reduces the memory complexity of attention from to (or in practice, due to tiling) ([2112.05682] Self-attention Does Not Need Memory). In effect, FlashAttention trades off a bit of extra computation (some recomputation of partial results) to drastically cut memory usage and improve cache utilization. The result is a significant speedup and memory savings for reasonably long sequences – it enables training with longer context (e.g. 4K-8K tokens) on GPUs that would otherwise run out of memory, and often yields 2–4× faster attention operations for standard sequence lengths. Limitations: The crucial point is that FlashAttention’s time complexity is still quadratic in ([2112.05682] Self-attention Does Not Need Memory). It alleviates the memory bottleneck and makes full attention practical for moderately longer sequences, but it does not fundamentally solve the issue of scaling to extremely long sequences. At 64K tokens, even if memory were somehow managed by FlashAttention, the sheer number of operations (billions of them) would be too slow. Thus, FlashAttention is complementary to sparsity – it optimizes the implementation of attention (and NSA presumably inherits such optimizations), but we still need sparse patterns to reduce computational load for truly long contexts.
Limitations of Prior Methods: Despite the progress of methods like Longformer, BigBird, and FlashAttention, there remain gaps that motivated NSA. Many sparse attention approaches achieve theoretical speedups, but real-world efficiency gains are sometimes limited. For example, some methods only apply sparsity in certain stages of processing (e.g. only during autoregressive decoding but not for the initial encoding of a prompt), which yields modest actual latency improvement (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI). Others introduce sparsity patterns that are not fully compatible with modern optimized architectures – for instance, techniques that don’t mesh well with multi-query attention (MQA) or grouped-query attention (GQA) (which share key/value projections across heads to save memory) can end up with bottlenecks in memory access (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI). A critical issue is that many earlier works focused on speeding up inference on long sequences, but paid less attention to training efficiency. Applying a sparse attention only at inference or after pretraining can lead to performance degradation, since the model wasn’t optimized with that pattern from the start (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI). In fact, training on long sequences is crucial to fully reap the benefits of extended context, yet some sparse methods either require full attention during pretraining or use discrete operations (like hard token selection or hashing) that break end-to-end differentiability (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI). Non-differentiable or discrete attention components prevent the model from learning optimal sparse patterns via gradient descent. Even among methods that are theoretically trainable, some suffer from inefficient backpropagation – for instance, selecting tokens at fine granularity can cause non-contiguous memory access, leading to poor hardware utilization during gradient computation (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI). These limitations highlight why a new approach was needed: one that provides true speedups in practice (not just in theory), and that supports full training on long contexts without hacks or loss of model quality.
How NSA Improves Upon Prior Work: The NSA mechanism is designed explicitly to overcome the above limitations. It introduces a dynamic hierarchical sparse attention strategy (described more in later sections) that allows the model to learn which tokens or groups of tokens to focus on, rather than relying on a fixed pattern ([2502.11089] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention). By combining a coarse-grained token compression step with fine-grained token selection, NSA preserves global context awareness while still honing in on important local details ([2502.11089] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention). This dynamic approach means the sparsity pattern can adapt to the input content, potentially capturing long-range dependencies more effectively than static patterns (Longformer/BigBird). Moreover, NSA’s algorithm is developed with hardware alignment in mind: it balances arithmetic intensity and uses block-structured operations to avoid the memory access pitfalls that naive sparse methods face (太震撼了!梁文锋携DeepSeek团队丢出注意力新机制重磅论文,网友:这才是真正的OpenAI) (Understanding BigBird's Block Sparse Attention). This ensures that the theoretical speedups translate into real speedups on GPUs. Crucially, as the name “Natively Trainable Sparse Attention” implies, NSA is built to be trained end-to-end from scratch. Its sparse operations are differentiable, and the model can be pretrained using NSA on long sequences without ever falling back to full attention. This avoids any post-hoc drop in performance – in fact, the authors report that a model pretrained with NSA matches or exceeds the quality of a full-attention model on both standard and long-context benchmarks ([2502.11089] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention). In summary, NSA stands out by offering efficient long-context attention throughout the model’s life cycle (training and inference), addressing the shortcomings of previous sparse attention methods.
1.5 DeepSeek’s Background and Role
NSA was developed by the DeepSeek research team, which has a track record of pushing efficiency in large-scale AI models. It’s useful to understand DeepSeek’s prior work, as their expertise in model efficiency directly informed NSA’s design. DeepSeek has been pioneering techniques to train huge models at lower cost – for instance, they introduced a 671B-parameter Mixture-of-Experts language model (DeepSeek V3) that activates only ~37B parameters per token, significantly reducing computation via sparse expert routing (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). This demonstrates the team’s philosophy of using sparsity (in that case, expert sparsity) to scale models effectively. They have also been at the forefront of quantization and low-precision training. DeepSeek adopted 8-bit floating point (FP8) training to halve memory usage compared to FP16, enabling larger batches or longer sequences on the same hardware (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). Their fine-grained quantization approach maintained training stability even at FP8, reportedly achieving an order-of-magnitude reduction in training cost without degrading model performance (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). This focus on numeric efficiency indicates a deep understanding of hardware constraints and how to overcome them.
In addition to MoE and quantization, DeepSeek explored innovations in the attention mechanism itself. Notably, they developed a Multi-Head Latent Attention (MLA) technique in their earlier models, which compresses key/value vectors into a lower-dimensional latent space to speed up attention computations (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). By compressing K/V by a factor of 16, this method drastically reduced memory and computation in the attention layers (at some acceptable cost to exactness) (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). The success of latent KV compression likely influenced NSA’s design of coarse-grained token compression – both involve intelligently reducing the scale of the attention problem (one in feature dimension, the other in sequence length dimension) to gain efficiency. DeepSeek’s repertoire also includes system-level optimizations (overlap of computation and memory transfers, optimized kernels, etc.), all aimed at squeezing maximum performance from modern hardware.
Given this background, it’s clear that DeepSeek’s expertise in efficiency and sparsity paved the way for NSA. They approached the long-context problem with lessons learned from making models efficient: use sparsity to drop unnecessary computation, use hierarchical structure to preserve essential information, and make sure the solution plays well with actual hardware and training dynamics. The “hardware-aligned” aspect of NSA reflects DeepSeek’s practical experience with GPU optimizations (as seen in FlashAttention-like memory tiling or block sparse kernels). The “natively trainable” aspect reflects their understanding that any new mechanism must integrate into end-to-end training – a likely lesson from their quantization work (which required maintaining model accuracy under new numeric formats). Indeed, prior to NSA, the DeepSeek team highlighted that context length was one of the remaining challenges in scaling LLMs efficiently (DeepSeek AI: Revolutionizing Efficiency, Innovation & Affordability in Next-Gen AI). NSA can be seen as a direct response to that challenge, leveraging DeepSeek’s cumulative knowledge to finally enable training and inference on long sequences (up to 64K tokens in experiments) with manageable cost. In summary, DeepSeek’s role was not just as the developers of NSA, but as enablers – their prior innovations in model sparsification and optimization created the foundation upon which NSA’s hardware-friendly, trainable sparse attention is built. Their background ensured that NSA was conceived not in isolation, but as part of a broader strategy of improving both the algorithmic efficiency and the practical execution efficiency of large language models.