← 全部文章← All posts

SparseEngine:面向稀疏注意力的推理引擎

文章目录
  1. 为什么需要一个 Sparse-First 引擎?
  2. 让稀疏计算成为引擎的原生能力
  3. 让稀疏方法参与跨请求的状态管理
  4. 实验结果
  5. 论文与代码

关于 KV Cache 压缩 / 稀疏注意力,已经有非常多的研究工作了。但真正把这些方法部署到 vLLM、SGLang 等推理引擎中,依然存在很多挑战,而这些问题在相关论文中往往很少被讨论。

在我们最近的工作 SparseEngine: Sparse-First Inference Engine 中,我们重新设计了推理引擎的抽象,尝试真正在推理引擎中 Serve 这些稀疏方法。

为什么需要一个 Sparse-First 引擎?

一个主要难点是,不同方法对 KV 的存储、访问和更新方式有很大区别。比如:

  • SnapKV、H2O 需要物理淘汰 KV,涉及不规则的 KV 存储和显存回收。
  • RetroInfer 需要维护基于聚类的 KV 索引,并采用特殊的 Attention 计算流程。
  • KIVI、Palu 改变了 KV 的存储格式,需要额外管理 量化参数或者压缩后的表示。

这意味着很难用一套传统的 KV Cache 抽象来统一适配这些方法。目前也已经有一些工作比如Vortex[1], Tangram[2]开始尝试支持serve一些稀疏方法,但仍局限于一些固定的定义与抽象,使得超出抽象边界的方法难以支持。

让稀疏计算成为引擎的原生能力

因此,我们从头设计了 Sparse-First 推理引擎 SparseEngine,让稀疏计算成为引擎的原生能力。目前,SparseEngine 已支持 4 大类、15 种稀疏方法,包括 SnapKV、H2O、Quest、KVzip、OmniKV 等,同时兼容 Continuous Batching、Prefix Caching 等必要的推理引擎能力。

图 1:方法覆盖与 Serving 能力。
图 1:方法覆盖与 Serving 能力。

我们已经支持了 14 种模型变体,包括 Qwen、Llama、GLM-4.7-Flash、Gemma 4、MiniMax-M2.7 等,覆盖 GQA、MLA、Linear Attention、SWA 等不同架构,以及 Dense 和 MoE 模型。目标是完成一个真正可用的推理引擎。

图 2:SparseEngine 的整体架构,以及 SnapKV、Quest 的实现示例。
图 2:SparseEngine 的整体架构,以及 SnapKV、Quest 的实现示例。

让稀疏方法参与跨请求的状态管理

进一步,我们还让稀疏方法参与跨请求的状态管理。目前在agent场景下,prefix cache命中率是一个重要的考虑因素,在常见agent场景中,如果进行了context compression,prefix cache就无法再正常命中,从而产生比较高额的prefill开销。我们在如下两个方面让稀疏方法介入状态管理。

1. Chain Cache

让经过 KV Eviction 的历史缓存也能跨轮次复用,避免重复 Prefill。在类似SnapKV,KVzip这种静态驱逐的方法中,token会被驱逐,使得prefix cache被破坏,无法正常匹配。为了让这类方法在agent场景下也能高效执行,我们为这类方法定义了一个chain cache的概念,与radix prefix tree不同,我们只维护一个链,并在逻辑上直接匹配token ids(不管对应cache是否被删除),如果能匹配,就直接接续。所以,如果用户不fork对话,理论上就能正常接续(目前codex似乎也维护了一个session id,如果fork对话似乎是要支付prefix miss的费用的,或许也有类似的实现)。

图 3:Radix Prefix Cache 与 Chain Cache。物理 KV 被淘汰后,仍保留逻辑 token 前缀,并复用留下的 KV。
图 3:Radix Prefix Cache 与 Chain Cache。物理 KV 被淘汰后,仍保留逻辑 token 前缀,并复用留下的 KV。

2. Prefix-Cache Pruning

支持对指定历史区域进行 KV Pruning,比如清理较早的工具调用结果,进行上下文的压缩,同时保留前缀匹配能力。比如在agent coding中,tool可能返回超长的log输出,或者是之前浏览过的超长代码,有时已经不再需要,我们支持使用snapkv、kvzip等剪枝方法,去修剪一下已经无用的上下文,从而减少整体的context长度。

图 4:按需裁剪历史 KV,保留前缀缓存复用能力。
图 4:按需裁剪历史 KV,保留前缀缓存复用能力。

实验结果

  • 长上下文场景下,通过 KV 淘汰支持更高并发,解码总吞吐相比 vLLM 提升 10 倍以上。
  • 相同并发下,解码吞吐最高提升 2.5 倍以上。
  • 多轮 Agent 场景下,端到端最高加速 2.2 倍。
图 5:Qwen3-30B-A3B 与 GLM-4.7-Flash 的解码吞吐。上排比较相同并发,下方比较各系统最大成功测试 batch size 下的吞吐。
图 5:Qwen3-30B-A3B 与 GLM-4.7-Flash 的解码吞吐。上排比较相同并发,下方比较各系统最大成功测试 batch size 下的吞吐。
图 6:SparseEngine 在多轮 Agent 场景下的端到端加速效果。
图 6:SparseEngine 在多轮 Agent 场景下的端到端加速效果。

论文与代码

随着 KV Cache 压缩和稀疏注意力逐渐被引入基础模型训练,我们希望 SparseEngine 不仅能加速现有模型,也能为后续原生稀疏模型,以及新的 KV Cache 优化方法提供更灵活的基础设施。

论文和代码都已开源,欢迎对推理引擎、稀疏注意力和 Agent Infra 感兴趣的朋友交流!

SparseEngine: Sparse-First Inference EngineCURRENTF/SparseEngine

SparseEngine: An Inference Engine for Sparse Attention

Contents
  1. Why do we need a sparse-first engine?
  2. Making sparse computation a native engine capability
  3. Involving sparse methods in cross-request state management
  4. Experimental results
  5. Paper and code

There is already a substantial body of research on KV cache compression and sparse attention. Yet deploying these methods in inference engines such as vLLM and SGLang remains challenging, and the papers often say little about these practical issues.

In our recent work, SparseEngine: Sparse-First Inference Engine, we redesigned the abstractions of an inference engine to make it possible to serve these sparse methods in practice.

Why do we need a sparse-first engine?

One major challenge is that different methods store, access, and update KV states in very different ways. For example:

  • SnapKV and H2O physically evict KV states, requiring irregular KV storage and GPU memory reclamation.
  • RetroInfer maintains a cluster-based KV index and uses a specialized attention computation workflow.
  • KIVI and Palu change the KV storage format, requiring additional management of quantization parameters or compressed representations.

This makes it difficult to accommodate all these methods within a conventional KV cache abstraction. Recent efforts such as Vortex [1] and Tangram [2] have started to support serving some sparse methods, but their fixed definitions and abstractions still make it difficult to support methods that fall outside those boundaries.

Making sparse computation a native engine capability

We therefore designed SparseEngine, a sparse-first inference engine, from the ground up. It currently supports 15 sparse methods across four categories, including SnapKV, H2O, Quest, KVzip, and OmniKV, while retaining essential serving capabilities such as continuous batching and prefix caching.

Figure 1. Method coverage and serving capabilities.
Figure 1. Method coverage and serving capabilities.

We support 14 model variants, including Qwen, Llama, GLM-4.7-Flash, Gemma 4, and MiniMax-M2.7. These span GQA, MLA, linear attention, and SWA architectures, as well as dense and MoE models. Our goal is to build an inference engine that is usable in practice.

Figure 2. The overall architecture of SparseEngine, with implementation examples for SnapKV and Quest.
Figure 2. The overall architecture of SparseEngine, with implementation examples for SnapKV and Quest.

Involving sparse methods in cross-request state management

We also let sparse methods participate in state management across requests. Prefix cache hit rate is an important consideration for agent workloads. In common agent scenarios, context compression can break prefix cache matching and incur substantial additional prefill cost. We bring sparse methods into state management through two mechanisms.

1. Chain Cache

Chain Cache allows historical caches that have undergone KV eviction to be reused across turns, avoiding repeated prefill. Static eviction methods such as SnapKV and KVzip remove tokens, disrupting conventional prefix cache matching. To make these methods efficient in agent workloads, we introduce a chain cache. Unlike a radix prefix tree, it maintains a single chain and matches logical token IDs directly, regardless of whether their corresponding KV states have been deleted. If the token IDs match, execution can continue from the retained cache. In principle, this allows a conversation to continue as long as it is not forked. Codex also appears to maintain a session ID, and forking a conversation seems to incur a prefix cache miss; it may use a related approach.

Figure 3. Radix Prefix Cache and Chain Cache. The logical token prefix is retained after physical KV eviction, allowing the remaining KV states to be reused.
Figure 3. Radix Prefix Cache and Chain Cache. The logical token prefix is retained after physical KV eviction, allowing the remaining KV states to be reused.

2. Prefix-Cache Pruning

Prefix-Cache Pruning supports KV pruning over specified regions of the history, such as older tool outputs, to compress the context while preserving prefix matching. In coding agents, for example, tools may return very long logs or source files that are no longer needed later. We support pruning methods such as SnapKV and KVzip to trim these historical regions and reduce the overall context footprint.

Figure 4. Pruning historical KV states on demand while preserving prefix cache reuse.
Figure 4. Pruning historical KV states on demand while preserving prefix cache reuse.

Experimental results

  • For long-context workloads, KV eviction enables higher concurrency, delivering more than 10× the aggregate decoding throughput of vLLM.
  • At matched concurrency, the best decoding throughput speedup exceeds 2.5×.
  • For multi-turn agent workloads, the best end-to-end speedup reaches 2.2×.
Figure 5. Decoding throughput for Qwen3-30B-A3B and GLM-4.7-Flash. The top row compares matched concurrency; the bottom row compares throughput at each system’s largest successfully tested batch size.
Figure 5. Decoding throughput for Qwen3-30B-A3B and GLM-4.7-Flash. The top row compares matched concurrency; the bottom row compares throughput at each system’s largest successfully tested batch size.
Figure 6. End-to-end speedups of SparseEngine on multi-turn agent workloads.
Figure 6. End-to-end speedups of SparseEngine on multi-turn agent workloads.

Paper and code

As KV cache compression and sparse attention are increasingly incorporated into foundation model training, we hope SparseEngine can both accelerate existing models and provide more flexible infrastructure for future natively sparse models and new KV cache optimization methods.

The paper and code are publicly available. We welcome discussions with anyone interested in inference engines, sparse attention, and agent infrastructure!

SparseEngine: Sparse-First Inference EngineCURRENTF/SparseEngine