News

日本語 · English · 中文

Decoding Qwen’s Sparse Attention (QSA) from the Paper: An Effective “Chunked Selection” Index for Long Contexts

Deciphering the Design of Sparse Attention QSA Adopted in Qwen3.8-Flash-Next from a Paper by the Qwen Team: Index computation is compressed into blocks, and learning proceeds in two stages—distillation and sparse learning. According to the paper, long-context RULER improves from 90.08 to 93.00, and the attention kernel achieves a 7.6x speedup in loading at a 1M context. Limitations are also discussed.

Reasons for Increased Attention Cost with Long Context

Standard attention (softmax-based global attention) is a mechanism that allows a specific token to directly reference all previous tokens. While it has high expressive power, two costs increase linearly with length. One is computational complexity, which grows as the square of the sequence length $n$. The other is the KV cache (storage of past keys and values), which increases in proportion to the length.

In models that handle long contexts, this quadratic term becomes dominant. To address this, sparse attention is employed. Instead of looking at all tokens, it selects and references only seemingly important ones, thereby alleviating the quadratic weight.

An early example cited in the paper is DSA (DeepSeek Sparse Attention, Liu et al. 2025). This method uses a lightweight indexer to select references for each token, significantly speeding up inference. However, the computation of the indexer itself remains $O(n^2)$, making this part unavoidable when the context becomes long. QSA addresses this issue.

The target model, Qwen3.8-Flash-Next, has a structure that mixes GDN (Gated DeltaNet: a layer that compresses and holds the past in a fixed-size state) with global attention on a per-layer basis. Every fourth layer consists of global attention. Since GDN has a finite state, the layer with global attention is responsible for exact extraction on a per-token basis, creating a clear division of roles.

Sparse Attention and the Indexer

The core of sparse attention lies in the "indexer," which determines "where to look." The design choices made here directly impact speed and accuracy.

The indexer of QSA uses MQA (Multi-Query Attention), where there are multiple query heads but only one shared key head. In Flash-Next, there are 4 query heads. This is lighter than a configuration where each head has its own keys.

The unit selected is not a token, but a "block." Keys are divided into non-overlapping blocks of $r$ tokens, and one representative is selected for each block on average (compression). Here, $r=4$. This reduces the sequence length handled by the indexer to $n/r$, shrinking the quadratic term to $1/r$ as detailed below.

There is also an ingenious handling of positional information (RoPE, Rotary Positional Embeddings). Positional information is applied to only 64 of the 128 dimensions for each head of the indexer, and it is assigned as the positional information of the beginning of the block *after* averaging. Since averaging tokens with different rotational phases would blur information, the order is rearranged.

QSA's Innovations: Scoring by Block

The scoring procedure is as follows. The score between a query token $i$ and a block $b$ is the sum of inner products per head after applying ReLU (a function that drops negatives to zero). This is a straightforward aggregation where heads with negative inner products do not contribute. Crucially, only blocks that have been fully read are targeted for scoring. This is a process to prevent mixing future blocks.

Next, the system selects the blocks with the highest scores from among $K$ tokens (i.e., $K/r$ blocks) and maps them back to tokens. The tokens in the last incomplete block are always included. In Flash-Next, with $K=2,048$ and $r=4$, the calculation selects up to 512 blocks plus any remaining tokens.

The core attention (body attention) is then computed only on the selected tokens. The indexer only roughly determines "where to look," while the actual weighted sum is handled by the core attention. This is a two-tier structure.

The paper emphasizes that compression is performed within each layer. Unlike methods that share indices across layers, this avoids relying on similarities between layers. The paper explains that this makes it well-suited for configurations that mix GDN and global attention.

As a result, the computational cost of the indexer is reduced from $O(n^2)$ to $O(n^2/r)$. With $r=4$, this is one-quarter. This provides a direct answer to the "quadratic cost of the indexer" that DSA struggled with.

How It Was Trained

Training was conducted as continued pre-training, divided into two stages with a length of 256K.

Stage 1 is distillation from dense attention. The distribution of the body attention is taken, all heads are summed and normalized, and the maximum value is summarized for each block. The indexer's score distribution is trained via KL divergence (a measure of the distance between distributions) to approximate this distribution. The indexer alone is trained for 1,000 steps, covering approximately 2 billion tokens. This process maps out "what the global attention considers important" in coarse block units.

Stage 2 is sparse learning. The entire model is trained only at the locations selected by the indexer. This involves 8,000 steps and approximately 200 billion tokens. According to the paper, the difference in loss is about $10^{-4}$, showing a loss trajectory almost identical to that of global attention. The key point is that rather than fixing the locations selected by the indexer and adjusting only the body, both the selector and the user are trained simultaneously.

The attention layers for MTP (Multi-Token Prediction) are also replaced with QSA, and indices selected in some stages of speculative decoding (a generation acceleration technique that proposes and verifies multiple tokens together) are reused.

Performance and Speed

The evaluation was conducted using Flash-Next, comparing global attention with QSA. All numbers are from the paper.

EvaluationGlobal AttentionQSA
Average of 8 short-context evaluations75.976.8
RULER 512K–1M90.0893.00
MRCR 512K (8 needles)30.6640.53
MRCR 1M (8 needles)20.7126.44
MRCR 128K (8 needles)97.1495.98
MRCR 256K (8 needles)94.2093.00

In short contexts, QSA matched or exceeded global attention in 7 out of 8 evaluations, with the average rising from 75.9 to 76.8 (MMLU-Pro 72.9→73.7, GSM8K 91.0→92.2). On the other hand, MMMLU dropped slightly from 81.8 to 81.1.

Retrieval in long contexts is an area with significant improvement. RULER 512K–1M improved from 90.08 to 93.00. In MRCR using 8 needles, 512K improved from 30.66 to 40.53, and 1M from 20.71 to 26.44. However, MRCR at 128K dropped slightly from 97.14 to 95.98, and at 256K from 94.20 to 93.00. It can be inferred that the advantages of QSA appear in longer regions.

The average adoption length for MTP was reported as 4.06→4.07 for 4 stages of speculative decoding, effectively unchanged.

Regarding speed, an important caveat exists. The figures below are measured at the kernel level, i.e., only the attention portion, and do not represent the throughput of the entire model. Speed improvements begin to appear at a context of 64K, widening as the context gets longer. At 1M, the input (prefill) is 7.6 times faster, and the output (decode) is 4.9 times faster. The comparison target is explicitly stated as FlashInfer's page-based GQA.

Supplementary experiments are also interesting. In a test with 35B-A3B, comparing different block sizes, QSA reached the same RULER score as global attention even when the indexer slowdown was set to 0.25 times. IndexShare, which shares indices across layers, did not reach that score even at 0.5 times, validating the design of compressing per layer. Other insights include that performance can be maintained with fewer indexer heads (reduced to 4), performance drops if sparsity is applied with dense initialization but recovers with short co-training.

What Changes

According to the paper's positioning, Qwen3.8-Flash-Next is a MoE model with 125B parameters and 6B active tokens per token, released on 2026-08-26, and is considered a precursor to the Qwen4 architecture. QSA is part of this architecture.

From the perspective of those interested in local LLMs, the points to note are the following. First, by reducing the cost of the indexer through block-level compression, the quadratic computation cost of the indexer, which became heavy in long contexts, was reduced to $1/r$. Second, in MRCR at 128K and 256K, some evaluations showed slight decreases. Third, the speed figures are at the kernel level and not end-to-end. These three points need to be read separately.

On the implementation front, an individual developer has published a port where an indexer was added later to Qwen3.8-27B (article in the blog article). Unlike the method in the paper, the body was not trained; only the indexer was added, attracting attention as an attempt to try retroactive integration. The full picture of the mechanism can be confirmed at arXiv 2608.30320.

The foundation for inference handling long contexts is organized in Current State of Local LLMs Part 3 (Prefill and Decode) and Part 4. Furthermore, a comparison with the method of putting in all retrieved documents was handled in Comparison of RAG and Putting in All Context. Sparse attention like QSA is likely easiest to position when read as an improvement that reduces the cost of this "putting in all" side.

This article was translated from the Japanese original. 日本語で読む

← Latest articles