HiSparse in SGLang: up to 5× throughput for sparse attention on long contexts
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.
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. 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. 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.
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. 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.
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.
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:
- Score. The indexer scores every earlier token and keeps the top k.
- Look up. HiSparse checks which of the top-k tokens are already in the buffer.
- 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.
- Attention. The model reads the top-k KV from the buffer and generates the next token.
- 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.
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.
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.
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.
Further reading
This visualized tutorial was written by Yichi Zhang.