HiSparse in SGLang: up to 5× throughput for sparse attention on long contexts

Published

Sparse attention lets a model read only a small part of its context at each decoding step. The KV cache of the whole context still has to stay in GPU memory, so memory limits how many requests can decode together. This article explains how sparse attention works and how SGLang's HiSparse removes that limit. HiSparse keeps the full KV cache in CPU memory and only a small hot buffer on the GPU. On GLM-5.1, throughput reaches over 3× the baseline at 256 concurrent requests, and up to 5× on long contexts.

Background

Sparse attention

When a model generates a token, attention looks back at the context again and again. The KV cache was invented for this. It stores keys and values for every earlier token at every layer. Full attention reads all of that KV. With a 32K-token context, every step reads 32K entries, so the cost of a step grows with the context.

Sparse attention is an optimization built on the same attention mechanism. It reads only part of the context. DeepSeek-V3.2 and GLM-5.1 use a design called DeepSeek Sparse Attention (DSA). It works in two stages. First, a small module called the indexer gives every earlier token a score for the current step. The indexer has its own compact keys, so scoring the whole context is cheap. Second, the model keeps the k tokens with the highest scores and runs attention on those tokens only. k is fixed. It is 2048 in the configurations used here, however long the context is.

Sparse attention diagramSchematic: top-k = 4 · one cell per context token · bar height is the indexer score
Full attentionKV read this step: 32 / 32Sparse attentionKV read this step: 4 / 32t1t2t3t4t5t6t7t8t9t10t11t12t13t14t15t16t17t18t19t20t21t22t23t24t25t26t27t28t29t30t31t32t33t34t35t36t37t38t39t401816243240Token position
t = 0/7

The diagram shows sparse attention at work. The indexer scores the context at every step. The k tokens with the highest scores are selected, and attention runs on those tokens only. The cost of attention at each step is therefore fixed and does not grow with the context.

Indexer scoring

The indexer answers one question: which earlier tokens are worth reading for the current token. It starts the way attention does. A query is compared with the key of every earlier token by a dot product, and a larger result means more relevant. But it only needs a score, so it has no values and no softmax, and its keys and queries are much smaller. The numbers below are from DeepSeek-V3.2.

Start with the key. Each attention layer has an indexer, and it keeps a 128-dimensional vector for every token, called an index key. A token’s hidden state at that layer has 7,168 dimensions. WKW_K is a 128 × 7,168 matrix, and multiplying the hidden state by it gives the index key. It is computed once and stored on the GPU.

Next, the query. It is not computed directly from the hidden state. DeepSeek’s attention builds its own queries in two steps. It first compresses the 7,168-dimensional hidden state to 1,536 dimensions, then expands that vector into the queries of its heads. The indexer reuses the 1,536-dimensional vector in the middle instead of starting again from the hidden state.

One query can judge relevance by only one criterion. A token can be worth reading for many reasons, so the indexer uses 64 queries at once. These are its 64 heads, with 128 dimensions each. WQW_Q is an 8,192 × 1,536 matrix. It can be read as 64 small matrices of 128 × 1,536 stacked together. One multiplication gives the 64 queries end to end, 8,192 dimensions in total. Cutting the output into 64 parts only separates the queries of the heads. Ordinary multi-head attention builds its queries the same way.

Where the queries come fromReal sizes in DeepSeek-V3.2 · bar lengths are proportional to dimensions
1 · Compress2 · Expand3 · Cut
Current token's hidden state7168
Everything starts from the current token's hidden state, which has 7,168 dimensions.
t = 0/3

The results of the 64 heads have to be combined into one score, so each head also needs a weight that says how much it counts at this step. The weights are computed separately from the queries. WwW_w is a 64 × 7,168 matrix that turns the current token’s hidden state directly into those 64 weights. They take no part in the dot products and are used only when the score is put together.

To score an earlier token, the indexer takes the dot product of its 128-dimensional key with each of the 64 queries. That gives 64 numbers. Negative ones are set to zero. The rest are multiplied by their head weights and added up. The sum is the token’s score. The 2,048 tokens with the highest scores go to attention. There is only one key, shared by all 64 heads, so each token stores just 128 numbers.

Indexer scoring diagramSchematic · bracket numbers are the real sizes in DeepSeek-V3.2 · blue is positive, gray is negative
1 · Queries2 · Weights3 · Dot products4 · ReLU5 · Weighted sum6 · Top-k
Current hidden stateOne column per headwq₁q₂q₃128Index keys = hidden state × WKt10.60.10.3−0.1t2−0.20.3−0.10.2t3−0.4−0.20.50.3t40.20.70.4−0.1t5−0.30.10.10.2t60.50.2−0.1−0.1t7−0.10.80.3−0.1t8−0.30.2−0.3−0.2128×64Score
When each earlier token was processed, its hidden state was multiplied by the indexer's key matrix WK. The result is its index key, stored one row per token.
t = 0/8

A stored index key never changes. Only the queries and the weights are new at each step. The same token can therefore score high at one step and low at the next.

The memory limit

Sparse attention cuts the work of each step. It does not cut the memory. Any token may be selected later, so the KV of the whole context stays in GPU memory.

GPU memory then decides how many requests can decode together. Once it is full, sending more concurrent requests adds no throughput. The benchmark below shows this as well. The baseline stops scaling at 32 concurrent requests.

Throughput vs concurrencyGLM-5.1-FP8 · 32K input / 8K output · 8×H200 · points traced from the original chart
01,0002,0003,00083264128256Concurrent requestsPeak throughput (tokens/s)Baseline · 8: ≈260 tokens/sBaseline · 16: ≈470 tokens/sBaseline · 32: ≈775 tokens/sBaseline · 64: ≈785 tokens/sBaseline · 128: ≈785 tokens/sBaseline · 256: ≈790 tokens/sBaselineHiSparse · 8: ≈185 tokens/sHiSparse · 16: ≈340 tokens/sHiSparse · 32: ≈600 tokens/sHiSparse · 64: ≈1035 tokens/sHiSparse · 128: ≈1740 tokens/sHiSparse · 256: ≈2645 tokens/sHiSparse

HiSparse

Design and mechanism

SGLang’s HiSparse was built to solve this problem. HiSparse stores the KV cache in two places. CPU memory holds the full KV of every request. GPU memory holds a small buffer for each request, called the hot device buffer, with a fixed number of slots. The indexer’s keys also stay on the GPU, because the indexer still scores the whole context.

Each decoding step then runs five actions:

  1. Score. The indexer scores every earlier token and keeps the top k.
  2. Look up. HiSparse checks which of the top-k tokens are already in the buffer.
  3. Swap in. The KV of each missing token is copied from CPU memory into the buffer. If the buffer is full, it replaces the least recently used token.
  4. Attention. The model reads the top-k KV from the buffer and generates the next token.
  5. Append. The new token’s KV is copied to CPU memory, and its index key joins the index keys on the GPU.

The buffer has more slots than k, so it also keeps tokens that were selected in recent steps. Because of how the indexer and model contexts behave, consecutive steps select many of the same tokens. Most lookups therefore hit the buffer, and each step moves only a few KV entries instead of the whole top-k.

One HiSparse decoding stepSchematic: top-k = 4 · 8-slot buffer · 32-token context
1 · Score2 · Look up3 · Swap in4 · Attention5 · Append
GPU memoryIndex keys · one per tokent1t2t3t4t5t6t7t8t9t10t11t12t13t14t15t16t17t18t19t20t21t22t23t24t25t26t27t28t29t30t31t32t33t34t35t36Hot device buffer · KV of 8 tokenst3t9t17t28t30t12t5t14new tokenCPU memory · full KV of every tokent1t2t3t4t5t6t7t8t9t10t11t12t13t14t15t16t17t18t19t20t21t22t23t24t25t26t27t28t29t30t31t32t33t34t35t3618162432
A decoding step begins. The GPU holds an index key for every token, but KV for only 8 tokens. CPU memory holds the KV of all 32 tokens.
t = 0/5

With HiSparse, a request takes a fixed number of KV slots on the GPU, however long its context is. The same GPU holds many more requests, and the decode batch grows with it.

Swap-in kernel

The swap-in step runs in every decoding step, so its latency matters. SGLang implements it as a single CUDA kernel. The kernel does three things. It finds the top-k tokens that are missing from the buffer. It picks the slots to free with a least-recently-used (LRU) policy. It updates the page table and copies the missing KV from CPU memory to the GPU.

Every miss costs one copy from CPU memory, so fewer misses mean a faster swap-in. Two choices reduce the misses: a larger buffer and a better eviction policy. The figure compares them on DeepSeek-V3.2. Growing the buffer from 2048 to 4096 slots removes a large share of the misses. With 4096 slots, LRU misses less than FIFO and random eviction.

Top-k miss countDeepSeek-V3.2 · top-k = 2048 · LongBench v2 · 100-step rolling mean · curves traced from the original chart
020040060080002004006008001000Decode stepTop-k tokens missed per step2048 slots4096 slots · FIFO4096 slots · Random4096 slots · LRUDecode step 0 2048 slots: ≈545 4096 slots · FIFO: ≈292 4096 slots · Random: ≈287 4096 slots · LRU: ≈275Decode step 50 2048 slots: ≈566 4096 slots · FIFO: ≈313 4096 slots · Random: ≈317 4096 slots · LRU: ≈253Decode step 100 2048 slots: ≈607 4096 slots · FIFO: ≈382 4096 slots · Random: ≈373 4096 slots · LRU: ≈281Decode step 150 2048 slots: ≈693 4096 slots · FIFO: ≈435 4096 slots · Random: ≈403 4096 slots · LRU: ≈330Decode step 200 2048 slots: ≈703 4096 slots · FIFO: ≈420 4096 slots · Random: ≈367 4096 slots · LRU: ≈335Decode step 250 2048 slots: ≈603 4096 slots · FIFO: ≈302 4096 slots · Random: ≈266 4096 slots · LRU: ≈275Decode step 300 2048 slots: ≈513 4096 slots · FIFO: ≈245 4096 slots · Random: ≈215 4096 slots · LRU: ≈195Decode step 350 2048 slots: ≈485 4096 slots · FIFO: ≈260 4096 slots · Random: ≈266 4096 slots · LRU: ≈189Decode step 400 2048 slots: ≈493 4096 slots · FIFO: ≈260 4096 slots · Random: ≈279 4096 slots · LRU: ≈206Decode step 450 2048 slots: ≈459 4096 slots · FIFO: ≈266 4096 slots · Random: ≈249 4096 slots · LRU: ≈206Decode step 500 2048 slots: ≈455 4096 slots · FIFO: ≈296 4096 slots · Random: ≈236 4096 slots · LRU: ≈212Decode step 550 2048 slots: ≈485 4096 slots · FIFO: ≈279 4096 slots · Random: ≈206 4096 slots · LRU: ≈202Decode step 600 2048 slots: ≈478 4096 slots · FIFO: ≈253 4096 slots · Random: ≈204 4096 slots · LRU: ≈197Decode step 650 2048 slots: ≈442 4096 slots · FIFO: ≈238 4096 slots · Random: ≈210 4096 slots · LRU: ≈184Decode step 700 2048 slots: ≈405 4096 slots · FIFO: ≈223 4096 slots · Random: ≈184 4096 slots · LRU: ≈167Decode step 750 2048 slots: ≈425 4096 slots · FIFO: ≈219 4096 slots · Random: ≈172 4096 slots · LRU: ≈159Decode step 800 2048 slots: ≈414 4096 slots · FIFO: ≈208 4096 slots · Random: ≈184 4096 slots · LRU: ≈148Decode step 850 2048 slots: ≈371 4096 slots · FIFO: ≈195 4096 slots · Random: ≈176 4096 slots · LRU: ≈148Decode step 900 2048 slots: ≈354 4096 slots · FIFO: ≈189 4096 slots · Random: ≈174 4096 slots · LRU: ≈142Decode step 950 2048 slots: ≈375 4096 slots · FIFO: ≈195 4096 slots · Random: ≈184 4096 slots · LRU: ≈131Decode step 1000 2048 slots: ≈399 4096 slots · FIFO: ≈197 4096 slots · Random: ≈178 4096 slots · LRU: ≈129

Results

The second benchmark sweeps input and output lengths on GLM-5.1-FP8. HiSparse generates 1,720 to 2,320 tokens/s across the six configurations. The baseline generates 352 to 752 tokens/s. The gap widens as the context grows. It is 3.1× at the shortest configuration and more than 5× at 40K input with 20K output.

Generation throughput by sequence lengthGLM-5.1-FP8 · two H20 nodes, PD-disaggregated · tokens/s
Input / outputBaselineHiSparse
20K / 10K7522,3203.1×
20K / 20K6002,2803.8×
40K / 10K4722,0804.4×
40K / 20K3682,0505.6×
60K / 10K3601,8205.1×
60K / 20K3521,7204.9×

HiSparse is built for high concurrency. At low concurrency it is slower than the baseline, because the extra copies cost more than the memory they save. The concurrency chart above shows this at 32 concurrent requests and below. HiSparse is still an experimental feature. It first supported the DSA model families, DeepSeek-V3.2 and GLM-5.1. The HiSparse guide lists the models and flags supported today.

Acknowledgments

HiSparse was built by Zhiqiang Xie, Zhangheng Huang, and Tingwei Huang. The original article thanks the Alibaba Cloud TairKVCache team and the Ant Group SCT Inference team for their contributions, Shangming Cai, Teng Ma, and Xingyu Ling from Alibaba Cloud and Ziyi Xu from the SGLang community for their support, and Christos Kozyrakis and Kristopher Geda from Stanford and the Baidu Baige AI Team for their feedback.

Try on SGLang Open the SGLang repo and run this in a real inference engine

Further reading

This visualized tutorial was written by Yichi Zhang.