跳到主内容
精选75Avi Chawla技巧与观点

LLM 首字延迟优化:算力翻倍为何无效

A good technical LLM interview question:

原文
推荐理由

做 LLM 应用性能优化的同学必看,这篇把首字延迟的构成拆得很透,还给出了拆分部署的参考实现,值得收藏对照自己的链路排查。

A good technical LLM interview question:

一个优秀的技术型LLM面试问题:

Your LLM chatbot takes 12s before it generates the first token, and the users are complaining.

你的LLM聊天机器人生成第一个token需要12秒,用户纷纷抱怨。

So you move the model onto a GPU with 3x the computing power.

于是你将模型迁移到计算能力为原来3倍的GPU上。

The time to first token barely improves.

但首token时间几乎没有改善。

Why did this happen?

这是为什么?

(answer below)

(答案如下)

Latency in an LLM app is a placement problem disguised as a model problem.

LLM应用中的延迟是一个伪装成模型问题的部署位置问题。

If you profile the 12 seconds, the model's prefill itself may only account for around 1.5 seconds of it.

如果你对这12秒进行剖析,模型本身的预填充可能只占其中约1.5秒。

So halving the prefill step saves just 750ms out of 12000, which is under 7%.

因此,将预填充步骤减半只能节省12000毫秒中的750毫秒,这不到7%。

The rest is spread across stages that never touch the GPU.

其余时间分散在从不接触GPU的各个阶段。

The request first travels to whatever region the app runs in, and a cross-continent round trip could cost over a second before any code executes.

请求首先传输到应用运行的任何区域,跨洲往返可能在任何代码执行前就耗费超过一秒。

Then the request handler starts. On a container-based serverless platform under load, this adds several seconds of cold start, paid before auth, rate limiting, or prompt assembly even begins.

然后请求处理器启动。在负载下的基于容器的无服务器平台上,这会增加几秒的冷启动时间,在认证、速率限制或提示组装开始之前就已付出。

Retrieval adds its own hop, and the response streams back across the same distance.

检索增加了自己的跳数,响应则跨越同样的距离流式返回。

Optimizing a stage that was already fast cannot alter the latency that's majorly affected by other stages.

优化一个已经很快的阶段无法改变主要受其他阶段影响的延迟。

Those other stages are slow for a structural reason.

那些其他阶段之所以慢,是出于结构性原因。

An LLM app runs two workloads that want opposite machines.

LLM应用运行两种需要相反机器的工作负载。

  • The request path is short, spiky, and needs to sit close to users - Inference is long-running, GPU-bound, and billed hourly, whether requests arrive or not.
  • 请求路径短、突发性强,需要靠近用户 - 推理是长时间运行、受GPU限制的,并且无论是否有请求到达,都按小时计费。

So the actual decision is not which model to run, but where each of these two workloads runs.

因此,实际的决定不是运行哪个模型,而是这两种工作负载各自在哪里运行。

There are three options, each with its own tradeoffs:

有三种选择,每种都有其权衡:

> A dedicated GPU box removes inference cold starts, but it bills around the clock and lives in one location, so distant users wait out the round trip on every request

> 专用GPU盒子消除了推理冷启动,但它全天候计费,且位于一个位置,因此远处的用户每次请求都要等待往返时间

> Container-based serverless scales to zero, but the request path pays a cold start, and most of these platforms have no GPU behind them.

> 基于容器的无服务器可以缩放到零,但请求路径要付出冷启动代价,而且这些平台大多没有GPU支持。

> Edge runtimes start in under a millisecond, because a WebAssembly module carries no OS or container image to boot. They handle the request path well and cannot hold a model.

> 边缘运行时在不到一毫秒内启动,因为WebAssembly模块不需要启动操作系统或容器镜像。它们能很好地处理请求路径,但无法承载模型。

So the answer is not to pick one, but to split the app across two of them.

所以答案不是选择其中一个,而是将应用拆分到其中两个上。

The request path runs close to users, and inference runs on a dedicated GPU it calls into.

请求路径运行在靠近用户的地方,而推理运行在它调用的专用GPU上。

That also explains the failed upgrade. More compute made a stage that was already fast faster, and left the 10.5 seconds around it untouched.

这也解释了升级失败的原因。更多的计算能力让本已快速的阶段更快,而围绕它的10.5秒则未受影响。

To actually learn how it's done in practice, Akamai's GitHub has a reference implementation for each half.

为了实际了解实践中的做法,Akamai的GitHub上为每一半提供了参考实现。

  • vllm-on-lke serves Qwen2.5-7B-Instruct behind an OpenAI-compatible endpoint on one RTX 4000 Ada GPU in Linode Kubernetes Engine, with Terraform creating the cluster, both firewalls, and the GPU operator in one apply.
  • akamai-functions-llm-chatbot covers the front, where a WebAssembly API checks a KV cache and only calls the GPU-backed instance on a miss.
  • vllm-on-lke在Linode Kubernetes Engine中的一块RTX 4000 Ada GPU上,通过OpenAI兼容端点提供Qwen2.5-7B-Instruct服务,Terraform在一次apply中创建集群、两个防火墙和GPU操作器。
  • akamai-functions-llm-chatbot覆盖前端,其中WebAssembly API检查KV缓存,仅在未命中时才调用GPU支持的实例。

Both are available on Akamai’s new Developer Hub, alongside their tutorials and code samples.

两者均可在Akamai的新开发者中心获取,并附有教程和代码示例。

It also links to Edge Case, their Discord, where four developer advocates architect and deploy a production app live every other Wednesday.

它还链接到他们的Discord频道Edge Case,在那里四位开发者倡导者每隔一个周三直播构建并部署一个生产应用。

If you create a new Akamai Cloud account, you can also get $300 in credits for joining.

如果您创建新的Akamai云账户,还可以获得300美元的积分作为加入奖励。

Join here: https://fandf.co/4w7id0z

在此加入:https://fandf.co/4w7id0z

That said, this post treats generation as a single 1.5s block, but that block has its own structure, and knowing it well tells you whether a model is slow to start or slow to stream.

话虽如此,本文把生成视为一个1.5秒的整体块,但该块有其内部结构,深入了解它可以帮助您判断模型是启动慢还是流式输出慢。

I wrote a first-principles walkthrough of it, covering the prefill and decode split, KV caching, and where the time actually goes inside each one.

我撰写了一篇基于第一性原理的详解,涵盖了预填充和解码的划分、KV缓存,以及每个阶段中时间实际消耗在哪里。

Read it below.

请阅读下文。

Thanks to Akamai Cloud for partnering today!

感谢Akamai云今天的合作!

更进一步:量化金融体系

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

进入量化体系 →

相似阅读

另一事件,读法相近