跳到主内容
@wquguru
精选88Avi Chawla技巧与观点

KV-streams:通过直接操作KV缓存加速Agentic RL训练

An interesting new approach to agentic RL:

原文
发到 X
推荐理由

Agent开发者的硬核工程指南,详细拆解了如何修改vLLM底层KV缓存以加速RL训练,流程完整可直接落地压测。

An interesting new approach to agentic RL:

一种用于智能体强化学习(Agentic RL)的新颖有趣方法:

Researchers found a way to train AI agents nearly 2x faster.

研究人员找到了一种使 AI 智能体训练速度几乎快 2 倍的方法。

Agentic RL usually produces long rollouts, and when a rollout reaches its context budget, a compaction policy removes older turns and keeps the prompt, recent turns, or a summary.

智能体强化学习通常会产生较长的 rollout(轨迹/序列),当 rollout 达到其上下文预算时,压缩策略会移除较早的轮次,并保留提示词、最近的轮次或摘要。

Most systems use re-prefill compaction.

大多数系统使用重新预填充(re-prefill)压缩。

They create a shorter sequence and run every retained token through the model again. This rebuilds the KV cache after each compaction, so the same tokens may be processed several times.

它们创建一个更短的序列,并将每个保留的 token 再次输入模型进行计算。这会在每次压缩后重建 KV 缓存,因此相同的 token 可能会被处理多次。

KV-streams is a new approach to solve this.

KV-streams 是解决此问题的新方法。

Instead of rebuilding the shorter sequence, KV-streams removes discarded turns directly from the live KV cache. Generation continues from the entries that survived.

与重建较短序列不同,KV-streams 直接从活动的 KV 缓存中移除被丢弃的轮次。生成过程从幸存的条目继续。

In practice, you cannot just reuse those entries as is. It requires two fixes.

在实践中,你不能直接复用这些条目。这需要两个修复措施。

> First, RoPE has already rotated each cached Key using its original token position. A Key may move from physical slot 1,000 to slot 500 after compaction, but the model must still treat it as position 1,000.

> 首先,RoPE(旋转位置编码)已经根据其原始 token 位置旋转了每个缓存的 Key。在压缩后,一个 Key 可能从物理槽位 1,000 移动到槽位 500,但模型仍必须将其视为位置 1,000。

KV-streams keeps physical storage and logical position separate. Retained Keys keep their rotations. New Queries and Keys continue from the original position count.

KV-streams 将物理存储和逻辑位置分开。保留的 Key 保持其旋转状态。新的 Query 和 Key 从原始的计数位置继续。

> Second, vLLM does not store each KV entry independently. It groups them into fixed 16-token blocks and uses a block table to find those blocks in GPU memory.

> 其次,vLLM 并不独立存储每个 KV 条目。它将它们分组为固定的 16-token 块,并使用块表在 GPU 内存中查找这些块。

KV-streams cannot leave empty slots inside that table because the attention kernel may read the wrong memory. It therefore removes complete blocks and joins the remaining block references together.

KV-streams 不能在该表中留下空槽,因为注意力内核可能会读取错误的内存。因此,它会移除完整的块,并将剩余的块引用连接在一起。

This introduces another problem.

这引入了另一个问题。

A conversation turn may end halfway through a 16-token block. In their vLLM setup, that partial block is not committed to the prefix cache and must be recomputed when the next turn arrives.

一次对话轮次可能在 16-token 块的中间结束。在他们的 vLLM 设置中,这个不完整的块不会提交到前缀缓存中,当下一个轮次到达时必须重新计算。

If compaction happens first, those tokens may have originally attended to context that is no longer present.

如果先发生压缩,这些 token 原本可能关注的是已不再存在的上下文。

For the vLLM experiments, the researchers pad each completion to a multiple of 16 tokens. This commits every generated token to the cache before the next turn or compaction event. The SGLang setup uses single-token blocks and needs no padding.

对于 vLLM 实验,研究人员将每个补全内容填充至 16 的倍数个 token。这在下一个轮次或压缩事件之前,将每个生成的 token 提交到缓存中。SGLang 设置使用单 token 块,无需填充。

Separately, the researchers found that token-level eviction produced degenerate text and required substantial supervised fine-tuning.

此外,研究人员发现基于 token 级别的驱逐会导致文本退化,并且需要大量的监督微调。

They suspect that removing message-boundary tokens disrupted the chat format learned during training. The agent experiments therefore remove complete turns, including their boundary tags.

他们怀疑移除消息边界 token 破坏了训练期间学到的聊天格式。因此,智能体实验中移除的是完整的轮次,包括其边界标签。

Once inference works this way, training must follow the same process.

一旦推理以这种方式工作,训练也必须遵循相同的过程。

A normal trainer would either process the full rollout or rebuild the shorter sequence after compaction. Both options produce different KV entries from those used during generation.

普通的训练器要么处理完整的 rollout,要么在压缩后重建较短的序列。这两种选项都会产生与生成阶段所使用的不同的 KV 条目。

KV-streams records every compaction event during generation. The trainer then reproduces that history: retained tokens keep the states they formed before compaction, while later tokens are blocked from attending to spans that inference had already removed.

KV-streams 记录了生成期间的每次压缩事件。训练器随后重现该历史:保留的 token 保持它们在压缩前形成的状态,而后续的 token 被禁止关注推理中已经移除的跨度。

In the repository’s validation runs, the difference between training and generation stayed near a mismatch KL of 0.001, indicating that both paths produced almost identical token probabilities.

在仓库的验证运行中,训练与生成之间的差异保持在接近 0.001 的不匹配 KL 散度附近,表明两条路径产生的 token 概率几乎相同。

On SWE-bench Verified, the Markovian Thinker setup with KV-streams reached peak performance in about 32 hours.

在 SWE-bench Verified 上,带有 KV-streams 的 Markovian Thinker 设置约在 32 小时内达到峰值性能。

The same compaction strategy with re-prefill took roughly 60 hours, while both reached comparable final scores.

使用重新预填充(re-prefill)的相同压缩策略大约需要 60 小时,而两者最终得分相当。

The speedup mainly applies to RL training, where one rollout may be compacted several times.

加速主要适用于强化学习(RL)训练,其中一次 rollout 可能被压缩多次。

At production inference, batching can hide some prefill cost, and keeping caches for inactive users reduces serving capacity.

在生产环境推理中,批处理可以隐藏部分预填充成本,而为不活跃用户保留缓存会降低服务能力。

There is one more detail.

还有一个细节。

The retained KV entries were created while the removed turns were still visible. Future tokens cannot attend to those turns directly, but some of their influence may remain in the entries that were not evicted.

保留的 KV 条目是在被移除的轮次仍然可见时创建的。后续 token 无法直接关注这些轮次,但它们的一些影响可能仍存在于未被驱逐的条目中。

In a controlled test, RL recovered an assignment after its original message was removed. Recall reached 100% when enough cache remained.

在一项受控测试中,强化学习在其原始消息被移除后恢复了一个分配任务。当保留足够的缓存时,召回率达到 100%。

KV-streams does not decide what the model should forget. Instead, it makes existing compaction policies cheaper to train by preserving the KV state they choose to retain.

KV-streams 并不决定模型应该遗忘什么。相反,它通过保留它们选择保留的 KV 状态,使现有的压缩策略在训练时更加廉价。

Repo: https://github.com/Emilianopp/KV-streams

仓库地址:https://github.com/Emilianopp/KV-streams

If you want to dive deeper into the broader KV-cache engineering stack, I wrote an article that covers eviction, paging, quantization, offloading, and architectural compression.

如果你想更深入地了解更广泛的 KV 缓存工程栈,我写了一篇文章,涵盖驱逐、分页、量化、卸载和架构压缩。

Read it below.

请在下方阅读。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件