跳到主内容
@wquguru
精选88Avi Chawla产品发布/更新

DeepSeek开源推理栈DeepInfer:MLA与3FS实现磁盘KV缓存加速

DeepSeek did it again!

原文
发到 X
推荐理由

做LLM部署的同学必看,这篇把DeepSeek如何用MLA+3FS把KV缓存搬到磁盘的技术细节讲透了,API调用成本优化有直接参考价值。

DeepSeek did it again!

DeepSeek 再次做到了!

They just open-sourced DeepInfer, the inference stack behind cache hits that return the first token up to 26x faster.

他们刚刚开源了 DeepInfer,这是缓存命中推理栈,能将首个 token 的返回速度提升多达 26 倍。

When a request repeats a long prefix, like a system prompt or earlier chat turns, the server can reuse the KV cache it already computed instead of running prefill again.

当请求包含与长前缀(如系统提示词或之前的对话轮次)重复的内容时,服务器可以复用已计算好的 KV 缓存,而无需重新执行预填充(prefill)。

Ideally, you'd expect that cache to sit in GPU memory, since disk is supposed to be far too slow for anything on the request path.

理想情况下,你期望该缓存位于 GPU 内存中,因为磁盘对于请求路径上的任何操作来说都太慢了。

In practice, DeepSeek keeps it on SSDs.

但在实践中,DeepSeek 将其存储在 SSD 上。

When DeepSeek launched this, a 128K-token prompt that hit the cache got its first token in 500ms instead of 13s.

DeepSeek 推出此功能时,一个命中缓存的 128K token 提示词,其首个 token 的生成时间从 13 秒缩短至 500 毫秒。

And in one day of production traffic last year, 56% of all input tokens were served straight from disk.

在去年一天的生产流量中,56% 的所有输入 token 直接从磁盘服务。

So disk isn't the bottleneck here as long as what you store is small enough, and the storage is wide enough.

因此,只要存储的数据量足够小且存储带宽足够宽,磁盘就不是瓶颈。

Let's understand how this works.

让我们了解它是如何工作的。

1) Why reuse the KV cache at all:

1) 为什么要复用 KV 缓存:

Before generating anything, an LLM reads the whole prompt and computes a key and a value for every token in every layer. This is prefill, and on long prompts it's the slow part.

在生成任何内容之前,大语言模型会读取整个提示词,并为每一层中的每个 token 计算键(key)和值(value)。这称为预填充(prefill),在处理长提示词时,这是最耗时的部分。

Many requests start the same way.

许多请求以相同的方式开始。

For instance, a chat resends every earlier turn or an agent resends the same system prompt and tool definitions on every call.

例如,聊天机器人会重发之前的每一轮对话,或者智能体在每次调用时都会重发相同的系统提示词和工具定义。

If those KV are saved, the next request with the same prefix loads them and only computes the new tokens.

如果保存了这些 KV,下一个具有相同前缀的请求只需加载它们并仅计算新 token。

2) Why GPU memory is predominantly used:

2) 为什么主要使用 GPU 内存:

Every token stores KV in every layer, so on a standard model a long prompt's KV cache runs into many GBs.

每个 token 在每一层都存储 KV,因此在标准模型上,长提示词的 KV 缓存可达数 GB。

Reading that much from disk can take longer than recomputing it on the GPU.

从磁盘读取如此大量的数据可能比在 GPU 上重新计算更慢。

So engines keep it in GPU memory. But GPU memory is small, so old prefixes get evicted fast and many repeats miss.

因此引擎将其保留在 GPU 内存中。但 GPU 内存很小,旧的前缀会被快速淘汰,导致许多重复请求无法命中缓存。

3) DeepSeek made the KV cache small:

3) DeepSeek 使 KV 缓存变小:

DeepSeek-V2 introduced Multi-head Latent Attention (MLA).

DeepSeek-V2 引入了多头潜在注意力机制(Multi-head Latent Attention, MLA)。

Instead of storing separate KV for every attention head, MLA stores one small compressed vector per token per layer, and the keys and values are rebuilt from it during attention.

MLA 不为每个注意力头存储单独的 KV,而是为每层的每个 token 存储一个小型压缩向量,并在注意力计算过程中从中重建键和值。

In the V2 paper, this cut the KV cache by 93.3% compared to DeepSeek's earlier 67B model.

在 V2 论文中,与 DeepSeek 早期的 67B 模型相比,这使得 KV 缓存减少了 93.3%。

And MLA shrank the KV cache enough to store it on low-cost disks.

MLA 将 KV 缓存缩小到足以存储在低成本磁盘上。

Fewer bytes per cached token implied fewer bytes to read on every hit, and it made loading from SSD faster than recomputing.

每个缓存 token 的字节数减少意味着每次命中时需要读取的字节数更少,并且使得从 SSD 加载比重新计算更快。

4) 3FS reads it from many SSDs at once:

4) 3FS 同时从多个 SSD 读取数据:

One SSD would still be too slow, so the cache lives on 3FS, DeepSeek's own distributed file system.

单个 SSD 的速度仍然太慢,因此缓存位于 DeepSeek 自研的分布式文件系统 3FS 上。

3FS splits files into chunks and spreads them across thousands of SSDs on many storage nodes. A read pulls chunks from many drives in parallel, so their bandwidth adds up.

3FS 将文件拆分为块(chunks),并将它们分布在许多存储节点上的数千个 SSD 中。读取操作会并行地从多个驱动器中提取块,从而叠加它们的带宽。

When a request arrives, the system matches its prefix against cached blocks, fetches those blocks from 3FS into host memory, and copies them to the GPU layer by layer while the model is already running.

当请求到达时,系统将其前缀与缓存块进行匹配,从 3FS 将这些块获取到主机内存中,并在模型已经运行时逐层将它们复制到 GPU 层。

And since every prefill machine can reach every cached prefix, a request doesn't have to go back to the machine that served it last.

由于每台预填充(prefill)机器都能访问每个缓存的前缀,因此请求不必返回到上次为其服务的机器。

Disk is also cheap, so the cache can stay for days. As per the DeepInfer docs, cached prefixes are retained for 3 days for agent traffic and 7 days for chat.

磁盘成本也很低,因此缓存可以保留数天。根据 DeepInfer 文档,代理流量的缓存前缀保留 3 天,聊天流量保留 7 天。

Disk caching works when the KV cache is small, and the storage is wide. DeepSeek got the first from its attention design and the second from 3FS.

当 KV 缓存较小且存储宽度足够时,磁盘缓存就能发挥作用。DeepSeek 通过其注意力机制设计实现了前者,通过 3FS 实现了后者。

This is also evident from the price. On deepseek-flash today, a cache hit costs 50x less than a miss.

这一点从价格上也显而易见。在 deepseek-flash 上,缓存命中的成本比未命中低 50 倍。

If you use the API, keep the stable parts of your prompt (system prompt, tools, documents) at the front, since only an exact match from the very first token counts as a hit.

如果使用 API,请将提示词中稳定的部分(系统提示、工具、文档)放在前面,因为只有从第一个 token 开始的完全匹配才算作命中。

The visual below depicts what we discussed.

下图描绘了我们讨论的内容。

That said, prefix caching is only one of the caches in LLM serving. I covered all four in an article recently, with code for each.

话虽如此,前缀缓存只是 LLM 服务中众多缓存之一。我最近在一篇文章中介绍了所有四种缓存,并附有每种缓存的代码。

Read it below.

请在下方阅读。

更进一步:量化金融体系

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

进入量化体系 →

相似阅读

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