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

LLM推理面试题:KV感知路由如何优化首字延迟

A tricky LLM inference interview question

原文
发到 X
推荐理由

深入剖析了LLM推理中KV Cache命中与负载均衡的核心权衡,给出可落地的路由策略思路,做推理服务优化的同学必读。

A tricky LLM inference interview question

一个棘手的 LLM 推理面试题

Three replicas of the same model run behind a load balancer.

负载均衡器后面运行着同一模型的三个副本。

Each request goes to the least busy replica, but time to first token remains high for agent traffic.

每个请求都被分配给负载最轻的副本,但代理流量的首 Token 延迟仍然很高。

What could the balancer be missing?

负载均衡器可能遗漏了什么?

(answer below)

(答案见下文)

The replica with the shortest queue can still have more computation left for that request.

队列最短的副本仍可能为该请求剩余更多的计算量。

Agent requests often share system instructions, tool definitions, and conversation history. Without reusable cached state, prefill must process that input before generation begins.

代理请求通常共享系统指令、工具定义和对话历史。如果没有可复用的缓存状态,预填充必须在生成开始之前处理这些输入。

A replica with the matching prefix cached can reuse its attention keys and values and process only the new portion.

拥有匹配前缀缓存的副本可以复用其注意力键值对,仅处理新增部分。

Another replica may have fewer requests but need to compute the entire prompt.

另一个副本可能请求较少,但需要计算整个提示词。

Request counts alone don't capture that difference. KV-aware routing considers both prefix reuse and existing workload.

仅凭请求数量无法捕捉这种差异。KV 感知路由同时考虑前缀复用和现有工作负载。

The visual shows one event-based design.

图示展示了一种基于事件的设计。

↳ Prompt blocks receive hashes that incorporate their preceding context. Changing an early block breaks the prefix match for later blocks.

↳ 提示块接收包含其前置上下文的哈希值。更改早期块会破坏后续块的前缀匹配。

↳ A cache index records which replicas hold matching blocks. Workers report additions and evictions to keep that view updated.

↳ 缓存索引记录哪些副本持有匹配的块。工作节点报告添加和驱逐操作以保持该视图更新。

↳ The router combines estimated remaining prefill work with a load cost when choosing a replica.

↳ 路由器在选择副本时,将预估的剩余预填充工作量与负载成本相结合。

Consider a prompt with three reusable prefix blocks and one new block.

考虑一个包含三个可复用前缀块和一个新块的提示词。

Using illustrative cost units, with equal weight on prefill and load, the scores are

使用示意性的成本单位,假设预填充和负载权重相等,得分为:

  • Replica 1 has the prefix cached and a load cost of 6, giving 1 + 6 = 7. - Replica 2 has no matching prefix and a load cost of 4, giving 4 + 4 = 8. - Replica 3 has the prefix cached and a load cost of 14, giving 1 + 14 = 15.
  • 副本 1 缓存了前缀且负载成本为 6,得分为 1 + 6 = 7。 - 副本 2 没有匹配的前缀且负载成本为 4,得分为 4 + 4 = 8。 - 副本 3 缓存了前缀且负载成本为 14,得分为 1 + 14 = 15。

Replica 1 wins despite carrying more load than Replica 2. These scores illustrate the tradeoff; they aren't request counts added to milliseconds.

尽管副本 1 承载的负载比副本 2 更重,但它胜出。这些分数说明了权衡关系;它们不是请求数加上毫秒数。

Replica 3 shows why cache affinity also needs limits. Repeatedly favoring a popular prefix can concentrate requests until waiting costs more than recomputing elsewhere.

副本 3 展示了为什么缓存亲和性也需要限制。反复青睐热门前缀会导致请求集中,直到等待成本高于在其他地方重新计算的开销。

Implementations differ under the hood.

底层实现各不相同。

SGLang maintained an approximate cache view rather than relying on worker cache-state synchronization (I'll do a separate post on this soon).

SGLang 维护了一个近似的缓存视图,而不是依赖工作节点的缓存状态同步(我稍后会单独写一篇文章讨论这一点)。

On its shared-prefix benchmark, cache hits increased from 20% to 75%, while throughput rose from 82k to 158k tokens/s, roughly 1.9x.

在其共享前缀基准测试中,缓存命中率从 20% 提升至 75%,吞吐量从每秒 82k token 提升至 158k token,约为 1.9 倍。

The workload used balanced groups of long shared prefixes. Measure the gain on representative traffic, including tail time to first token, rather than assuming a higher hit rate means lower latency.

该工作负载使用了平衡的长共享前缀组。应在具有代表性的流量上衡量收益,包括首 Token 的尾部延迟,而不要假设更高的命中率就意味着更低的延迟。

Prompt layout affects the opportunity for reuse too. Keeping stable instructions and tool definitions before changing content helps preserve matching prefixes.

提示布局也会影响复用的机会。在更改内容之前保持稳定的指令和工具定义,有助于保留匹配的公共前缀。

I covered how prefix caching works and what causes cache misses in a detailed article.

我在详细文章中介绍了前缀缓存的工作原理以及导致缓存未命中的原因。

Read it below.

请在下方阅读。

更进一步:量化金融体系

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

进入量化体系 →

相似阅读

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