Skip to content
quboliu
⚙

Serving LLaMA 3-70B on TPUs在 TPU 上部署 LLaMA 3-70B 推理服务

第 8 章 · How To Scale Your Model (Transformer 推理详解 | 如何对 TPU 程序做性能分析)

Let's take a close look at how we'd serve LLaMA 3-70B models on TPU v5e. How expensive are different models to serve at roofline? How large are their KV caches? What batch sizes should we use? How are the parameters and activations sharded during inference? Let's work through some back-of-the-envelope estimates for latency and throughput in production.

以 TPU v5e 上的 LLaMA 3-70B 为例,估算参数和 KV 缓存的内存需求,比较不同精度、并行配置与批次大小的延迟和吞吐量。

This section will look at what it takes to serve LLaMA-3 and how efficiently it can be done. As in the previous “applied” section, try to work out the answers on your own with a pen and paper before looking them up!

本节将探讨服务 LLaMA-3 需要什么,以及如何高效地完成。与之前的“应用”部分一样,请在查看答案之前尝试自己用笔和纸算出答案!

What’s the LLaMA Serving Story?LLaMA 3-70B 服务的成本和延迟

Let’s remind ourselves what LLaMA 3-70B looks like (see Section 6 for reference):

让我们回顾一下 LLaMA 3-70B 的样子 (参考第 6 节):

hyperparam
超参数
value
值
nlayersn_\text{layers} (L)80
dmodeld_\text{model} (D)8,192
dffd_{ff} (F)28,672
nheadsn_\text{heads} (N)64
nkv headsn_\text{kv heads} (K)8
dqkvd_\text{qkv} (H)128
nembeddingsn_\text{embeddings} (V)128,256

Let’s start with a simple question: what hardware should we serve on? The answer is basically, whichever is cheapest in FLOPs / dollar.1 For this reason, we typically want to serve on TPU v5e, our current dedicated inference chip (cost comes from Google Cloud pricing as of February 2025):

让我们从一个简单的问题开始:我们应该在什么硬件上提供服务? 答案基本上是,无论哪个在 FLOPs/美元方面最便宜。1 因此,我们通常希望在 TPU v5e 上提供服务,这是我们目前专用的推理芯片 (成本来自 2025 年 2 月的 Google Cloud 定价):

TPU type
TPU 类型
bfloat16 FLOPs/sGoogle Cloud USD / hour
Google Cloud 美元/小时
FLOPs / $
H1009.9e14$10.83.3e17
v5p4.59e14$4.23.9e17
v5e1.97e14$1.25.8e17

Each TPU v5e has 16GB of HBM which will require us to shard our model fairly aggressively. Let’s start by thinking about some basic quantities that might matter for us:

每个 TPU v5e 有 16GB 的 HBM,这将要求我们相当积极地对模型进行分片。让我们从思考一些可能对我们很重要的基本量开始:

Question: How large are LLaMA 3-70B’s KV caches per token? You can assume we store them in int8. This determines how large our batch size can be on a given topology.

问题: LLaMA 3-70B 每个 token 的 KV 缓存有多大?你可以假设我们用 int8 存储它们。这决定了我们在给定拓扑上的批次大小能有多大。

Click here once you've thought it through! 点击这里, 在你思考之后!

LLaMA 3-70B has 8 KV heads, so the size per token is 2 * K * H * L = 2 * 8 * 128 * 80 = 160kB.

LLaMA 3-70B 有 8 个 KV 头,所以每个 token 的大小是 2 * K * H * L = 2 * 8 * 128 * 80 = 160kB.

Note just how big this is! If we have a sequence length of 32k tokens (as is common), this uses 160e3 * 32,768 = 5.3GB / sequence. For BS=240, this is 1.3TB! Since TPU v5e only have 16GB a piece, we would need about (70e9 + 1.3e12) / 16e9 = 86 TPU v5e chips to even fit this much memory. Also note how large this is compared to the 70GB of model parameters.

注意这有多大! 如果我们有一个 32k token 的序列长度 (这很常见),这将使用 160e3 * 32,768 = 5.3GB / sequence。对于 BS=240,这是 1.3TB!由于 TPU v5e 每个只有 16GB,我们大约需要 (70e9 + 1.3e12) / 16e9 = 86 个 TPU v5e 芯片才能容纳这么多内存。另请注意,与 70GB 的模型参数相比,这有多大。

Question: Let’s say we want to serve L3 70B at batch size 32 and 8192 sequence length with everything (params and KVs) in int8. How much total memory will this use? What’s the smallest slice we could serve this on?

问题: 假设我们想在批次大小为 32,序列长度为 8192 的情况下,用 int8 服务 L3 70B (参数和 KV)。这将使用多少总内存?我们可以用多小的切片来服务它?

Answer 答案

Since our KVs are 160e3 bytes in int8, our total KV memory is 160e3 * 8192 * 32 = 41.9e9 bytes. Our parameters are 70e9 bytes, since we have 1 byte per parameter. Thus, our total memory usage is 41.9e9 + 70e9 = 112GB.

由于我们的 KV 在 int8 中是 160e3 字节,我们的总 KV 内存是 160e3 * 8192 * 32 = 41.9e9 字节。我们的参数是 70e9 字节,因为我们每个参数有 1 个字节。因此,我们的总内存使用量是 41.9e9 + 70e9 = 112GB.

The smallest slice we could use would have 112e9 / 16e9 = 7 TPUs, or (rounding to an even size), TPU v5e 4x2. This will be a tight fit and we might not be able to quite fit this accounting for other overhead, so we might need a 4x4 at minimum (or to drop the batch size).

我们可以使用的最小切片将有 112e9 / 16e9 = 7 个 TPU,或者 (四舍五入到一个偶数大小), TPU v5e 4x2。这将是一个紧张的配合,我们可能无法完全容纳,考虑到其他开销,所以我们可能至少需要一个 4x4 (或者减小批次大小).

Question: At this batch size and quantization on a TPU v5e 4x2, roughly what latency would we expect per decode step? What throughput (tokens / sec / chip). What about a 4x4? Assume we perform our FLOPs in bfloat16 and everything is fully sharded.

问题: 在这个批次大小和量化下,在 TPU v5e 4x2 上,我们每个解码步骤大约会期望什么样的延迟?吞吐量 (token/秒/芯片) 是多少?4x4 呢?假设我们用 bfloat16 执行 FLOPs,并且一切都完全分片。

Answer 答案

We can invoke the formula from the previous section that

我们可以调用上一节的公式

Theoretical Step Time (General)=Batch Size×KV Cache SizeTotal Memory Bandwidth⏟Attention (always bandwidth-bound)+max⁡(2×Batch Size×Parameter CountTotal FLOPs/s,Parameter SizeTotal Memory Bandwidth)⏟MLP (can be compute-bound)\begin{align*} \tiny \text{Theoretical Step Time (General)} = \underbrace{\frac{\text{Batch Size} \times \text{KV Cache Size}}{\tiny \text{Total Memory Bandwidth}}}_{\text{Attention (always bandwidth-bound)}} + \underbrace{\max\left(\frac{2 \times \text{Batch Size} \times \text{Parameter Count}}{\text{Total FLOPs/s}}, \frac{\text{Parameter Size}}{\text{Total Memory Bandwidth}}\right)}_{\tiny \text{MLP (can be compute-bound)}} \end{align*}

Here our critical batch size will be about 120 since our parameters are in int8 but our FLOPs are in bfloat16. We could also manually calculate the RHS maximum, but that’s basically a calculation we’ve already done several times. So we’re well into the memory-bound regime for both our matmul and our FLOPs.

在这里,我们的临界批次大小大约是 120,因为我们的参数是 int8,但我们的 FLOPs 是 bfloat16。我们也可以手动计算 RHS 的最大值,但这基本上是我们已经做过好几次的计算。所以我们在矩阵乘法和 FLOPs 方面都完全处于受内存限制的状态。

Strictly looking at memory bandwidth then, our step time is basically (KV size + param size) / (8 * HBM bandwidth) = 112e9 / (8 * 8.2e11) = 17ms. So theoretically our step time is about 17ms. Our throughput would be 32 / .017 = 1882 tokens / sec, or 1882 / 8 = 235 tokens / sec / chip.

严格地看内存带宽,我们的单步耗时基本上是 (KV size + param size) / (8 * HBM bandwidth) = 112e9 / (8 * 8.2e11) = 17ms. 所以理论上我们的单步耗时大约是 17ms. 我们的吞吐量将是 32 / .017 = 1882 tokens / sec,或 1882 / 8 = 235 tokens / sec / chip.

There’s one caveat here which is to check if we might be ICI bound on our matmuls. We could dedicate 2 axes to it here, so we’re ICI bound in theory when Y>2∗F/2200=2∗28672/2200=26Y > 2 * F / 2200 = 2 * 28672 / 2200 = 26, so we’re golden!

这里有一个警告,就是要检查我们是否可能在矩阵乘法上受 ICI 限制。我们可以在这里为其分配 2 个轴,所以理论上当 Y>2∗F/2200=2∗28672/2200=26Y > 2 * F / 2200 = 2 * 28672 / 2200 = 26 时,我们受 ICI 限制,所以我们没问题!

If we were to run on a 4x4, we’d still be fine ICI-wise, so our latency would drop to 17 / 2 = 8.5ms, but our throughput per-chip would remain the same.

如果我们要在 4x4 上运行,我们在 ICI 方面仍然没问题,所以我们的延迟会降到 17 / 2 = 8.5ms,但我们每个芯片的吞吐量将保持不变。

Thinking about throughput思考吞吐量

Let’s spend a little time thinking purely about throughput. When we optimize for throughput, we want to be compute bound, meaning we come close to utilizing all the TPU MXU capacity. Typically that means we want the batch size to be as large as possible, so we are doing as much work as possible.

让我们花点时间纯粹地思考吞吐量。当我们优化吞吐量时,我们希望受计算限制,这意味着我们接近于利用所有 TPU MXU 容量。通常这意味着我们希望批次大小尽可能大,以便我们做尽可能多的工作。

Question: On TPU v5e, using bfloat16 weights and activations, how large do our batch sizes need to be for us to be compute-bound in our matmuls? What if we do int8 weights but perform our FLOPs in bfloat16? What about int8 weights with int8 FLOPs?

问题: 在 TPU v5e 上,使用 bfloat16 权重和激活,我们的批次大小需要多大才能在矩阵乘法中受计算限制?如果我们使用 int8 权重但用 bfloat16 执行 FLOPs 呢?如果使用 int8 权重和 int8 FLOPs 呢?

Answer 答案

As discussed in Section 7, for any bfloat16 matmul for which B≪D,FB \ll D, F we have

正如第 7 节所讨论的,对于任何 B≪D,FB \ll D, F 的 bfloat16 矩阵乘法,我们有

Tmath>Tcomms↔2BDF2DF≥TPU bfloat16 FLOPs/sHBM bandwidth=240\begin{equation*} T_\text{math} > T_\text{comms} \leftrightarrow \frac{2BDF}{2DF} \geq \frac{\text{TPU bfloat16 FLOPs/s}}{\text{HBM bandwidth}} = 240 \end{equation*}

When our weights are in int8, we lose a factor of 2 in the denominator, so we have 2BDF/DF=2B>2402BDF / DF = 2B > 240, or equally B>120B > 120, half the critical batch size from before. That’s really helpful for us! When we do int8 weights and int8 FLOPs, we have to use the int8 value for TPU FLOPs/s, which goes from 1.97e14 for bfloat16 to 3.94e14, nearly double. That means we’re back where we started at about B>240B > 240.

当我们的权重是 int8 时,我们在分母中损失了 2 倍,所以我们有 2BDF/DF=2B>2402BDF / DF = 2B > 240,或者同样地 B>120B > 120,是之前临界批次大小的一半。这对我们真的很有帮助!当我们使用 int8 权重和 int8 FLOPs 时,我们必须使用 TPU FLOPs/s 的 int8 值,它从 bfloat16 的 1.97e14 增加到 3.94e14,几乎翻了一番。这意味着我们回到了大约 B>240B > 240 的起点。

The case of int8 weights and bfloat16 FLOPs is quite common, since quantizing parameters losslessly is often easier than doing low-precision arithmetic.

int8 权重和 bfloat16 FLOPs 的情况相当普遍,因为无损地量化参数通常比进行低精度算术更容易。

Question: What is the smallest TPU v5e topology we could serve LLaMA 3-70B on using bfloat16, int8, and int4 (both KVs and parameters) with 8k context? You can think of KV caches as negligibly small for this one.

问题: 我们可以用 bfloat16, int8 和 int4 (KV 和参数) 在 8k 上下文下服务 LLaMA 3-70B 的最小 TPU v5e 拓扑是什么?你可以认为 KV 缓存对于这个问题可以忽略不计。

Answer 答案

This is easy! If we’re OK with a tiny batch size then the only limit is fitting parameter memory in HBM, i.e. it is just ceil(num_params * sizeof(dtype) / HBM per TPU), or ceil(70e9 * sizeof(dtype) / 16e9) rounded to the nearest reasonable topology (some multiple of 2):

这很简单!如果我们对一个很小的批次大小感到满意,那么唯一的限制就是将参数内存放入 HBM,即它只是 ceil(num_params * sizeof(dtype) / HBM per TPU),或 ceil(70e9 * sizeof(dtype) / 16e9) 四舍五入到最近的合理拓扑 (2 的某个倍数):

dtypeparam size
参数大小
KV size / token (bytes)
KV 大小/token (字节)
min TPU v5es
最小 TPU v5es
actual min slice
实际最小切片
remaining HBM for KV caches
剩余 HBM 用于 KV 缓存
num KV caches @ 8k
8k 时的 KV 缓存数量
bf16140GB324kB8.754x4 = 16 chips11643
int870GB162kB4.384x2 = 8 chips5843
int435GB81kB2.812x2 = 4 chips2943

That’s pretty cool! It tells us we could fit LLaMA 70B on a TPU v5e 2x2 if we wanted to. Except you’ll notice the number of KV caches is very small. That’s our batch size! That means we’ll be getting terrible FLOPs utilization. We’d be very happy to use a larger topology in order to push our batch size up to 240.

这很酷!它告诉我们,如果我们愿意,我们可以将 LLaMA 70B 放在一个 TPU v5e 2x2 上。除了你会注意到 KV 缓存的数量非常少。那是我们的批次大小!这意味着我们将获得糟糕的 FLOPs 利用率。我们会非常乐意使用更大的拓扑来将我们的批次大小推高到 240.

Question: Assume we use the largest batch size that fits on these topologies, what latency could we expect for each generate step?

问题: 假设我们使用适合这些拓扑的最大批次大小,我们每个生成步骤可以期望什么样的延迟?

Answer 答案

This is also easy, since we’re picking our batch size to fill up all our HBM! This is just a question of how long it takes to load a full TPU v5e’s worth of bytes into the MXU. This is just v5e HBM / v5e HBM memory bandwidth = 16GB / 8.2e11 = 19ms, so this is 19ms / step. Assuming our generations have a median length of 512 tokens, that is about 9s for each decode. Note that we could get marginally better latency with a smaller batch size, for instance if we only looked at model parameters in int4 our minimum latency is about 10ms / step, since HBM is no longer full.

这也很简单,因为我们正在选择我们的批次大小来填满我们所有的 HBM!这只是一个问题,即将一个完整的 TPU v5e 的字节加载到 MXU 需要多长时间。这只是 v5e HBM / v5e HBM memory bandwidth = 16GB / 8.2e11 = 19ms,所以这是每步 19ms。假设我们的生成中位长度为 512 个 token,那么每次解码大约需要 9 秒。请注意,我们可以通过更小的批次大小获得稍微更好的延迟,例如,如果我们只看 int4 中的模型参数,我们的最小延迟大约是每步 10ms,因为 HBM 不再是满的。

Likewise, in the FLOPs-bound regime (e.g. training or big-batch inference), we can use the Total FLOPs/(N⋅C)=2⋅param count⋅B/(N⋅C)\text{Total FLOPs} / (N \cdot C) = 2 \cdot \text{param count} \cdot B / (N \cdot C) lower bound, which assumes no communication.

同样,在受 FLOPs 限制的情况下 (例如,训练或大批量推理),我们可以使用 Total FLOPs/(N⋅C)=2⋅param count⋅B/(N⋅C)\text{Total FLOPs} / (N \cdot C) = 2 \cdot \text{param count} \cdot B / (N \cdot C) 下限,这假设没有通信。

Question: For each of these, what throughput per chip does this give us (in terms of queries / chip)? You can assume our median decode length is 512 tokens.

问题: 对于这些中的每一个,每个芯片的吞吐量是多少 (以查询/芯片为单位)? 你可以假设我们的中位解码长度是 512 个 token.

Answer 答案

This is an important question because it’s exactly correlated with cost / token.

这是一个重要的问题,因为它与每个 token 的成本完全相关。

With our assumption about median decode length, our throughput is just B/(per-step latency⋅median steps⋅N)≈43/(0.019∗512∗N)B / (\text{per-step latency} \cdot \text{median steps} \cdot N) \approx 43 / (0.019 * 512 * N). This gives us roughly (4.42/N)(4.42 / N) QPS, so plugging in NN we get:

根据我们对中位解码长度的假设,我们的吞吐量就是 B/(per-step latency⋅median steps⋅N)≈43/(0.019∗512∗N)B / (\text{per-step latency} \cdot \text{median steps} \cdot N) \approx 43 / (0.019 * 512 * N)。这给了我们大约 (4.42/N)(4.42 / N) QPS,所以代入 NN 我们得到:

dtypeQPS / chip
QPS / 芯片
bfloat160.27
int80.55
int41.11

Note that this is rather optimistic since it totally ignores the working memory of the forward pass (memory allocated to activations and attention). This is not ridiculous with Flash Attention, but it is also not realistic. The real numbers are likely around 1/2 of this. For absolutely maximum throughput we would probably want to more than double the number of chips and increase the batch size significantly as well.

请注意,这是相当乐观的,因为它完全忽略了前向传播的工作内存 (分配给激活和注意力的内存)。这对于 Flash Attention 来说并非荒谬,但也不现实。真实的数字可能大约是这个的一半。为了获得绝对最大的吞吐量,我们可能需要将芯片数量增加一倍以上,并显著增加批次大小。

Question: How would our peak throughput change if we doubled our topology for each of the above examples?

问题: 如果我们将上述每个示例的拓扑加倍,我们的峰值吞吐量会如何变化?

Answer 答案

If we used a 4x8 slice in bfloat16, we would have 372GB remaining for KV caches, which would let us increase our batch size to 140. Then since our step time would remain the same, we would have a throughput of 14.39 / num_chips, or

如果我们我们在 bfloat16 中使用 4x8 切片,我们将有 372GB 的剩余空间用于 KV 缓存,这将使我们的批次大小增加到 140。然后,由于我们的单步耗时将保持不变,我们的吞吐量将是 14.39 / num_chips,或

dtypeQPS / chip
QPS / 芯片
bfloat16 (on 4x8)
bfloat16 (在 4x8 上)
0.44
int8 (on 4x4)
int8 (在 4x4 上)
0.90
int4 (on 2x4)
int4 (在 2x4 上)
1.80

A further increase would give an even bigger win! The big takeaway is that the smallest topology is not the most performant topology in all cases, if we’re limited by KV cache size.

进一步增加将带来更大的胜利!重要的结论是,在所有情况下,最小的拓扑并非性能最高的拓扑,如果我们受 KV 缓存大小的限制。

Question: Now let’s dig into the question of sharding. Let’s say we wanted to serve in bfloat16 on a TPU v5e 4x8. What sharding would we use for our model on a TPU v5e 4x8 during generation? Can we avoid being communication bound?

问题: 现在让我们深入探讨分片的问题。假设我们想在 TPU v5e 4x8 上用 bfloat16 提供服务。在生成期间,我们将在 TPU v5e 4x8 上为我们的模型使用什么分片?我们能避免受通信限制吗?

Answer 答案

As discussed in the previous section, we only really have one option for sharding during generation: model parallelism. How much can we do before we become communication bound? As we’ve discussed in the previous section, our models become communication bound roughly when

正如上一节所讨论的,我们在生成期间实际上只有一个分片选项:模型并行。在我们变得受通信限制之前,我们可以做多少?正如我们在上一节中讨论的,通信成为瓶颈的近似条件是

Y>F⋅MY2200Y > \frac{F \cdot M_Y}{2200}

For LLaMA 3-70B we have F = 28,672, so if we do 2 axes of model sharding this gives us roughly Y=28672⋅2/2200=26Y = 28672 \cdot 2 / 2200 = 26, so in general we could scale up to about 16 chips without being communication bound, which lets us use a 4x4 but not a 4x8. Generally, since we do not perfectly overlap computation, even this estimate is overly optimistic.

对于 LLaMA 3-70B,我们有 F = 28,672,所以如果我们进行 2 个轴的模型分片,这给了我们大约 Y=28672⋅2/2200=26Y = 28672 \cdot 2 / 2200 = 26,所以一般来说,我们可以扩展到大约 16 个芯片而不受通信限制,这让我们使用 4x4 而不是 4x8。通常,由于我们不能完美地重叠计算,即使这个估计也过于乐观。

Takeaway: we cannot actually serve on a 4x8 with pure model parallelism. The best we can do here is a 4x2 or maybe a 4x4.

要点:我们实际上不能用纯模型并行在 4x8 上提供服务。 我们能做的最好的就是 4x2 或者也许是 4x4.

However, as we’ve discussed, when our batch size is small we can often do more model parallelism without significantly hurting throughput, since our model is memory-bandwidth-bound and not FLOPs bound. We said before that this value is roughly Y=F/(8⋅B)Y=F / (8\cdot B), so if we did batch size 64, we could in theory go up to Y = 28,672 / (8 * 64) = 56 way model parallelism before we become ICI-bound. To sanity check this, we can look at Tici commsT_\text{ici comms}, Thbm commsT_\text{hbm comms}, and TmathT_\text{math} for a single matmul. We clearly have:

然而,正如我们所讨论的,当我们的批次大小很小时,我们通常可以进行更多的模型并行,而不会显著影响吞吐量,因为我们的模型受内存带宽限制,而不是受 FLOPs 限制。我们之前说过,这个值大约是 Y=F/(8⋅B)Y=F / (8\cdot B),所以如果我们批次大小为 64,理论上我们可以进行高达 Y = 28,672 / (8 * 64) = 56 路模型并行,然后才会受 ICI 限制。为了验证这一点,我们可以查看单个矩阵乘法的 Tici commsT_\text{ici comms}, Thbm commsT_\text{hbm comms} 和 TmathT_\text{math}。我们清楚地有:

Tici comms=2BDWiciThbm comms=2DFY⋅WhbmTmath=2BDFY⋅C\begin{align*}T_\text{ici comms} = \frac{2BD}{W_\text{ici}} && T_\text{hbm comms} = \frac{2DF}{Y \cdot W_\text{hbm}} && T_\text{math} = \frac{2BDF}{Y \cdot C}\end{align*}

For a 4x8, this would give us Tici commsT_\text{ici comms} = (2 * 64 * 8192) / 9e10 = 11us, Thbm commsT_\text{hbm comms} = (2 * 8192 * 28,672) / (32 * 8.2e11) = 18us, and TmathT_\text{math} = (2 * 64 * 8192 * 28,672) / (32 * 1.97e14) = 4us, so in theory we’re still HBM bandwidth bound, which is great! Note that scaling up from a 4x4 to a 4x8 probably isn’t helpful from a throughput standpoint, but it’ll reduce our latency!

对于 4x8,这将给我们 Tici commsT_\text{ici comms} = (2 * 64 * 8192) / 9e10 = 11us, Thbm commsT_\text{hbm comms} = (2 * 8192 * 28,672) / (32 * 8.2e11) = 18us,以及 TmathT_\text{math} = (2 * 64 * 8192 * 28,672) / (32 * 1.97e14) = 4us,所以理论上我们仍然受 HBM 带宽限制,这很棒!请注意,从 4x4 扩展到 4x8 从吞吐量的角度来看可能没有帮助,但它会减少我们的延迟!

If we look at the int8 and int4 configs, we can do those with pure model parallelism. So we’ve hit a point at which quantization actually gives us a meaningful advantage beyond faster FLOPs: it lets us use a larger batch size before we become comms-bound. So the end of this story is that we can’t achieve peak throughput on a 4x8, but for the int8 and int4 configs we could do pure model parallelism.

如果我们看 int8 和 int4 配置,我们可以用纯模型并行来做这些。所以我们已经到了一个点,量化实际上给了我们一个有意义的优势,超越了更快的 FLOPs:它让我们在使用更大的批次大小之前变得受通信限制。因此在 4x8 上无法达到峰值吞吐量,但采用 int8 或 int4 时仍可使用纯模型并行。

What about prefill?预填充呢?

We’ve mostly ignored prefill here because it’s much simpler. Let’s put a couple of concepts together and think about the end-to-end picture.

我们在这里基本上忽略了预填充,因为它要简单得多。让我们把几个概念放在一起,思考一下端到端的画面。

Question: Assume we achieve a 40% FLOPs utilization during prefill. How long will a prefill of length 8192 take on 16 TPU v5e chips?

问题: 假设预填充时能够达到 40% 的 FLOPs 利用率。在 16 台 TPU v5e 芯片上预填充 8192 个 token 需要多久?

Answer 答案

At 8k tokens, we are solidly compute bound, so we just need to reason about FLOPs. We know our model has 70e9 parameters so each forward pass uses 2 * 70e9 * B FLOPs. Assuming 40% MFU (FLOPs utilization), this gives us a runtime of about 2 * 70e9 * 8192 / (16 * 1.97e14 * 0.4) = 0.91s. Compared to the numbers we’ve been looking at before, that’s actually quite a lot!

预填充 8k token 时,计算是主要瓶颈,只需估算 FLOPs。模型有 70e9 个参数,每次前向传播使用 2 * 70e9 * B FLOPs。在模型 FLOPs 利用率(MFU)为 40% 时,耗时约为 2 * 70e9 * 8192 / (16 * 1.97e14 * 0.4) = 0.91s。相比前面的估算,这个时间其实相当长。

Question: Assume we have a median prefill length of 8192 tokens and a median decode length of 4096 tokens. Say we have a generate batch size of 32. On average how many sequences finish decoding per step? On average how many tokens are evicted from our KV cache each step?

问题: 假设我们的中位预填充长度为 8192 个 token,中位解码长度为 4096 个 token。假设我们的生成批次大小为 32。平均每个步骤有多少个序列完成解码?平均每个步骤从我们的 KV 缓存中驱逐多少个 token?

Answer 答案

This is kind of straightforward. Since we have a median decode length of 4096 tokens, a sequence will finish roughly every 1 / 4096 tokens. Given a batch size of 32, this means we have 32 / 4096 sequences evicted per step. Since our KV cache length is roughly 8192 + 4096, this is 32 * (8192 + 4096) / 4096 = 96 tokens evicted per step. The general formula is B∗(P+G)/GB * (P + G) / G where PP and GG are the prefill and generate lengths.

这有点直接。由于我们的中位解码长度为 4096 个 token,一个序列大约每 1/4096 个 token 完成一次。给定 32 的批次大小,这意味着我们每个步骤驱逐 32 / 4096 个序列。由于我们的 KV 缓存长度大约是 8192 + 4096,这是每个步骤驱逐 32 * (8192 + 4096) / 4096 = 96 个 token。一般公式是 B∗(P+G)/GB * (P + G) / G,其中 PP 和 GG 是预填充和生成长度。

Question: Assume we do disaggregated serving with a median prefill length of 8192 and a median decode length of 512. Assume the prefill and generate latencies calculated above in bfloat16. What ratio of prefill:generate servers will you need to keep both fully saturated.

问题: 假设我们进行分离式服务,中位预填充长度为 8192,中位解码长度为 512。假设上面计算的 bfloat16 中的预填充和生成延迟。你需要多少比例的预填充:生成服务器才能使两者都完全饱和。

Answer 答案

This is kind of a fun question. Let PP be the number of prefill servers and GG be the number of generate servers. So generally speaking, this is a pipeline problem where we feed sequences in at a rate of P / prefill_latency and consume them at a rate of B * G / (generate_latency * median_decode_length). We had calculated 910ms per prefill step and 19ms per decode step at batch size 43 (let’s call that 32). Therefore we need P / 0.91 = 32 * G / (0.019 * 512) or P = 3G, i.e. we need about 3 times more prefill servers than generation servers!

这是一个有趣的问题。设 PP 是预填充服务器的数量,GG 是生成服务器的数量。所以一般来说,这是一个流水线问题,我们以 P / prefill_latency 的速率输入序列,并以 B * G / (generate_latency * median_decode_length) 的速率消耗它们。我们计算出每个预填充步骤为 910ms,每个解码步骤为 19ms,批次大小为 43 (我们称之为 32)。因此我们需要 P / 0.91 = 32 * G / (0.019 * 512) 或 P = 3G,即我们需要大约 3 倍于生成服务器的预填充服务器!

Visualizing the Latency Throughput Tradeoff可视化延迟吞吐量权衡

Sticking with LLaMA 70B for a second, let’s actually look at the latency and throughput for different batch sizes during generation. As we showed in the previous section for PaLM models, this gives us a Pareto frontier for throughput/latency. Let’s assume 16-way tensor parallelism since that’s a reasonable bound on what we can use while staying compute-bound in the MLP blocks. We’ll use a TPU v5e 4x4 topology here. The slider controls the sequence length so you can see the effect of larger KV caches.

继续以 LLaMA 70B 为例,让我们实际看看在生成期间不同批次大小的延迟和吞吐量。正如我们在上一节中为 PaLM 模型展示的那样,这给了我们一个吞吐量/延迟的帕累托前沿。让我们假设 16 路张量并行,因为这是我们在 MLP 块中保持受计算限制的合理界限。我们将在这里使用 TPU v5e 4x4 拓扑。滑块控制序列长度,以便你可以看到更大 KV 缓存的效果。

Figure: throughput per chip against per-token latency for LLaMA 3-70B (int8 weights, 16-way tensor parallelism) on a TPU v5e 4x4, sweeping batch size along each curve. Drag the slider to change the context length; each curve stops at the largest batch whose KV cache fits in HBM.

图: TPU v5e 4x4 上,LLaMA 3-70B(int8 权重、16 路张量并行)的单芯片吞吐量与单 token 延迟。每条曲线对应从小到大的批次,终点是 KV 缓存恰好能放入 HBM 的最大批次。拖动滑块可调整上下文长度。

  • See how dramatic the tradeoff is between cost and latency. At the cost of doubling per-token latency, we can achieve a roughly 100x reduction in per-token cost. Also, our latency can range anywhere from 5.5ms with low batch size to 20 ms with very large batches.
  • Note how at 2k context the throughput effectively plateaus at around 1 token / ms / chip when it hits the BS 120 roofline (120 here because we do int8 weights but bf16 FLOPs). As the sequence length increases, however, we can no longer fit this batch size in memory, so we never hit the point of full saturation.
  • Note how much higher the latency is at large batch sizes for the same throughput, since KV loading becomes dominant (instead of parameter loading).
  • 看看成本和延迟之间的权衡有多么巨大。 以将每个 token 的延迟加倍为代价,我们可以实现每个 token 成本大约 100 倍的降低。此外,我们的延迟可以从低批次大小的 5.5 毫秒到非常大批量的 20 毫秒不等。
  • 请注意,在 2k 上下文下,当达到 BS 120 屋顶线 (这里是 120,因为我们使用 int8 权重但 bf16 FLOPs) 时,吞吐量实际上每个芯片约 1 token/毫秒。然而,随着序列长度的增加,我们无法再将这个批次大小放入内存中,因此我们永远无法达到完全饱和的点。
  • 请注意,在相同吞吐量下,大批次大小的延迟要高得多,因为 KV 加载变得占主导地位 (而不是参数加载).

We can understand this better by breaking down the sources of cost and latency into param loading time, KV loading time, and FLOPs time. The shaded region is where we expect to be compute-bound in our MLP blocks.

我们可以通过将成本和延迟的来源分解为参数加载时间,KV 加载时间和 FLOPs 时间来更好地理解这一点。阴影区域是我们期望在 MLP 块中受计算限制的区域。

Figure: time per decode step for LLaMA 3-70B on a TPU v5e 4x4 as a function of batch size, split into parameter loading, KV cache loading, and FLOPs. The total is the KV loading time plus whichever of parameter loading or FLOPs is larger. Drag the slider to change the context length.

图: TPU v5e 4x4 上,LLaMA 3-70B 的单步解码耗时随批次大小的变化,拆成加载参数、加载 KV 缓存和计算三部分。总耗时等于 KV 缓存加载耗时加上参数加载和计算二者中较长的一项。拖动滑块可调整上下文长度。

This tells quite a story. You can see that initially, parameter loading represents the vast majority of the latency, until the batch size becomes large enough that FLOPs and KV loading become more significant. Notably, at all sequence lengths greater than 2048, we spend more time on KV cache loading than we do on FLOPs! So while we can improve our hardware utilization by increasing batch size, at long context lengths KV loading always dominates the total step time.

这讲述了一个相当长的故事。你可以看到,最初,参数加载占了延迟的绝大部分,直到批次大小变得足够大,FLOPs 和 KV 加载变得更加重要。值得注意的是,在所有大于 2048 的序列长度下,我们在 KV 缓存加载上花费的时间比在 FLOPs 上花费的时间更多!因此,虽然我们可以通过增加批次大小来提高硬件利用率,但在长上下文长度下,KV 加载始终主导总单步耗时。

The code for this is quite simple. 这段代码很简单.

Here’s the code for computing these rooflines:

这是计算这些屋顶线的代码:

import numpy as np

num_chips = 16  # we fix 16 as the amount of total model parallelism we do
bytes_per_param = 1  # int8 means 1 byte per param
param_count = 70e9
param_size = bytes_per_param * param_count
sequence_length = 8192  # can vary this

hbm_bandwidth = 8.20E+11  # v5e
flops = 1.97E+14  # v5e

def kv_cache_size(bs):
    return 2 * bs * 128 * 8 * 80

def min_topology(bytes):
    return 2 ** np.ceil(np.log2(bytes / 16e9))

def get_max_batch_size(
    num_chips: int,
    sequence_length: int,
    param_size: float,
) -> int:
  batch_sizes = np.arange(1, 1024, 4)
  kv_sizes = kv_cache_size(sequence_length * batch_sizes)
  required_chips = min_topology(kv_sizes + param_size)
  max_idx = np.where(required_chips <= num_chips)[0][-1]
  return max_idx

max_idx = get_max_batch_size(
    num_chips=num_chips,
    sequence_length=sequence_length,
    param_size=param_size,
)  # get the largest batch size that can fit
batch_sizes = np.arange(1, 512, 1)[:max_idx]
kv_sizes = kv_cache_size(sequence_length * batch_sizes)

kv_comms_time = kv_sizes / (num_chips * hbm_bandwidth)

param_comms_time = param_size / (num_chips * hbm_bandwidth)
param_comms_time = np.asarray([param_comms_time] * batch_sizes.shape[0])

flops_time = 2 * param_size * batch_sizes / (num_chips * flops)  # roughly true in a 2ND sense

mlp_time = np.maximum(flops_time, param_comms_time)
attn_time = kv_comms_time  # always bandwidth-bound for generate

latency = 1000 * (mlp_time + attn_time)
throughput = batch_sizes / (latency * num_chips)

Note how we very explicitly break out latency into two sources: KV loading and param loading, and how the latency is either bound by FLOPs or comms, whichever is bigger.

请注意我们如何非常明确地将延迟分解为两个来源:KV 加载和参数加载,以及延迟如何受 FLOPs 或通信的限制,以较大者为准。

Worked Problems习题与解答

Here are a few worked problems. Some of these repeat things that are worked above, but might be pedagogically useful.

这里有几个已解决的问题。其中一些重复了上面已经解决的问题,但可能在教学上很有用。

Question 1: How many FLOPs does each forward pass for LLaMA 3-405B use per-token? Assuming we’re FLOPs bound, what is a lower bound on a single forward pass on N chips on TPU v5e? What if we’re comms bound? Ignore the fact that the model does not fit on a single chip.

问题 1: LLaMA 3-405B 的每个前向传播每个 token 使用多少 FLOPs?假设我们受 FLOPs 限制,在 N 个 TPU v5e 芯片上的单个前向传播的下限是多少?如果我们受通信限制呢?忽略模型不适合单个芯片的事实。

Question 2: Assume we want to serve LLaMA 3-8B with BS240 using int8 weights and int8 KV caches. How many bytes are used by (a) model parameters (b) KV caches and (c) peak working activations (roughly)? What’s the smallest topology we can run this on?

问题 2: 假设我们想用 BS240, int8 权重和 int8 KV 缓存来服务 LLaMA 3-8B. (a) 模型参数 (b) KV 缓存和 (c) 峰值工作激活 (大致) 使用多少字节?我们可以运行这个的最小拓扑是什么?

Question 3: How would you serve LLaMA 3-405B on TPU v5e? Assume int8 weights and bfloat16 FLOPs. Let’s say we have a firm limit of 15ms / token, what’s the highest throughput configuration we could achieve? What is the theoretical minimum step time?

问题 3: 你将如何在 TPU v5e 上服务 LLaMA 3-405B?假设 int8 权重和 bfloat16 FLOPs。假设我们有 15ms/token 的严格限制,我们可以实现的最高吞吐量配置是什么?理论上的最小单步耗时是多少?

Question 4: The best way to learn about LLMs is to implement one from scratch. Building the full training pipeline is annoying and expensive, but turning trained weights you can download from HuggingFace into a working inference implementation is extremely instructive. Roughly speaking, you should try to do the following:

问题 4: 从头实现一个 LLM 是学习它的好方法。构建完整的训练流程既复杂又昂贵,而使用从 Hugging Face 下载的预训练权重,将它们跑成可用的推理实现,很能帮助理解模型。大致可以按以下步骤进行:

  1. Download LLaMA 3 8B weights and load them in Colab. Visualize the weights (I like TreeScope for this) and see if you can identify each tensor. Count the # of parameters. Does it match what you expect? How many are in the MLP vs. attention?
  1. 下载 LLaMA 3 8B 的权重并在 Colab 中加载。用工具(我喜欢用 TreeScope)将权重可视化,试着辨认每个张量。统计参数量,看看结果是否符合预期。MLP 和注意力分别占多少?
  1. Implement the full forward pass of the model. You don’t need to worry about prefill/decode or anything fancy. Just get to the point where you can feed in a sequence and get plausible next-token probabilities out. You should be able to feed a prompt in, get probabilities out, pick the highest probability one, and repeat. This is slow but functional sampling. Try to only use the LLaMA-3 paper as a reference, but the jax-ml/jax-llm-examples/llama3 repo can be a good reference for details (be careful of positional embeddings and causal masking). Your goal is to get coherent tokens out of the model. You can run this all on a single TPU if you can get more than 16GiB of HBM.
  1. 实现完整的前向传播。暂时不用处理预填充、解码或更复杂的优化。只要输入一个序列,就能得到合理的下一 token 概率。输入提示词,取得这些概率,选择概率最高的 token,再重复。这种采样很慢,但能够运行。尽量以 LLaMA 3 论文为参考,也可以查阅 jax-ml/jax-llm-examples/llama3 仓库里的实现细节,尤其要留意位置嵌入和因果掩码。目标是生成连贯的 token 序列。如果单台 TPU 的 HBM 超过 16GiB,这一步可以完全在单台 TPU 上完成。
  1. Now it’s time to make this faster. Implement KV caching, where you can save the key/value activations from a forward pass and attend to them in a subsequent one. This will make your sampling loop much faster.
  1. 接下来可以加快推理。实现 KV 缓存,把一次前向传播的键和值激活保存起来,以供后续计算注意力时使用。这样会大大加速采样循环。
  1. Implement separate prefill and decode servers. You can do these on different sets of chips. Prefill handles just a single prompt at once, then sends it to a batched decode server that handles batches of tokens.
  1. 分别实现预填充服务器和解码服务器。它们可以运行在不同芯片组上。预填充一次处理一个提示,把结果发送给批量解码服务器,由后者按批次处理 token。
  1. Implement Flash Attention in Pallas. This will make attention much more efficient. I think it’s good to try and do this without a reference.
  1. 用 Pallas 实现 Flash Attention,提高注意力计算的效率。可以先试着不看参考实现。

That’s all for Part 8! For Part 9, with a deep dive into XLA and TPU profiling, click here.

第 8 部分到此结束!第 9 部分,深入探讨 XLA 和 TPU 分析,点击 这里.

Footnotes

  1. This isn’t always true, sometimes more HBM or ICI bandwidth is critical rather than FLOPs, but this is a good heuristic.

    这并非总是如此,有时更多的 HBM 或 ICI 带宽比 FLOPs 更重要,但这是一个很好的启发式方法。

    ↩

补充说明

*Work done at Google DeepMind, now at MatX.

*这项工作完成于 Google DeepMind,现任职于 MatX。

引用

For attribution in academic contexts, please cite this work as:

学术写作中请按以下格式引用本书。

Austin et al., "How to Scale Your Model", Google DeepMind, online, 2025.

or as a BibTeX entry:

也可使用以下 BibTeX 条目。

@article{scaling-book,
  title = {How to Scale Your Model},
  author = {Austin, Jacob and Douglas, Sholto and Frostig, Roy and Levskaya, Anselm and Chen, Charlie and Vikram, Sharad
    and Lebron, Federico and Choy, Peter and Ramasesh, Vinay and Webson, Albert and Pope, Reiner},
  publisher = {Google DeepMind},
  howpublished = {Online},
  note = {Retrieved from https://jax-ml.github.io/scaling-book/},
  year = {2025}
}
原版章节与讨论