What Do We Mean By Scaling?我们所说的扩展是什么意思?
The goal of “model scaling” is to be able to increase the number of chips used for training or inference while achieving a proportional, linear increase in throughput (we call this strong scaling). While performance on a single chip depends on the trade-off between memory bandwidth and FLOPs, performance at the cluster level depends on hiding inter-chip communication by overlapping it with useful FLOPs. This is non-trivial, because increasing the number of chips increases the communication load while reducing the amount of per-device computation we can use to hide it. As we saw in Section 3, sharded matrix multiplications often require expensive AllGathers or ReduceScatters that can block the TPUs from doing useful work. The goal of this section is to find out when these become too expensive.
“模型扩展”的目标是能够在增加用于训练或推理的芯片数量的同时,实现吞吐量的成比例线性增长 (我们称之为强扩展)。虽然单个芯片上的性能取决于内存带宽和 FLOPs 之间的权衡,但集群级别的性能取决于通过将其与有用的 FLOPs 重叠来隐藏芯片间通信。这是不平凡的,因为增加芯片数量会增加通信负载,同时减少我们可以用来隐藏它的每个设备的计算量。正如我们在第 3 节中看到的,分片矩阵乘法通常需要昂贵的 AllGathers 或 ReduceScatters,这会阻止 TPU 进行有用的工作。本节的目标是找出它们何时变得过于昂贵.
In this section, we’ll discuss four common parallelism schemes: (pure) data parallelism, fully-sharded data parallelism (FSDP / ZeRO sharding), tensor parallelism (also known as model parallelism), and (briefly) pipeline parallelism. For each, we’ll show what communication cost we incur and at what point that cost starts to bottleneck our compute cost.1 For this section, you can focus solely on inter-chip communication costs, since as long as we have a large enough single-chip batch size, the transfer of data from HBM to MXU is already overlapped with computation.
在本节中,我们将讨论四种常见的并行方案:(纯) 数据并行,完全分片数据并行 (FSDP / ZeRO 分片), 张量并行 (也称为模型并行),以及 (简要地) 流水线并行。对于每种方案,我们将展示我们产生的通信成本以及该成本在什么时候开始成为我们计算成本的瓶颈。1 对于本节,你可以只关注芯片间通信成本,因为只要我们有足够大的单芯片批次大小,从 HBM 到 MXU 的数据传输就已经与计算重叠了。
We’ll use the following notation to simplify calculations throughout this section.
我们将在本节中使用以下符号来简化计算。
| Notation 符号 | Meaning (model parameters) 含义 (模型参数) |
|---|---|
| D | dmodel (the hidden dimension/residual stream dim) dmodel (隐藏维度/残差流维度) |
| F | dff (the feed-forward dimension) dff (前馈维度) |
| B | Batch dimension (number of tokens in the batch; total, not per-device) 批次维度 (批次中的 token 数量;总计,而非每个设备) |
| T | Sequence length 序列长度 |
| L | Number of layers in the model 模型中的层数 |
| Notation 符号 | Meaning (hardware characteristic) 含义 (硬件特性) |
|---|---|
| C | FLOPS/s per chip 每个芯片的 FLOPs/s |
| W | Network bandwidth (bidirectional, often subscripted as e.g. or ) 网络带宽(双向,通常以下标区分,例如 或 ) |
| X | Number of chips along mesh axis X 网格轴 X 上的芯片数量 |
| Y | Number of chips along an alternate mesh axis, labeled Y 另一个网格轴上的芯片数量,标记为 Y |
| Z | Number of chips along a third mesh axis, labeled Z 第三个网格轴上的芯片数量,标记为 Z |
For simplicity’s sake, we’ll approximate a Transformer as a stack of MLP blocks — attention is a comparatively small fraction of the FLOPs for larger models as we saw in Section 4. We will also ignore the gating matmul, leaving us with the following simple structure for each layer:
为简单起见,我们将 Transformer 近似为一堆 MLP 块 —— 正如我们在第 4 节中看到的,对于较大的模型,注意力是 FLOPs 的一个相对较小的部分。我们还将忽略门控矩阵乘法,为我们留下每层的以下简单结构:
Figure: a simplified Transformer layer. We treat each FFW block as a stack of two matrices Win: bf16[D, F] (up-projection) and Wout: bf16[F, D] (down-projection) with an input In: bf16[B, D].
图: 一个简化的 Transformer 层. 我们将每个 FFW 块视为两个矩阵的堆栈 Win: bf16[D, F] (上投影) 和 Wout: bf16[F, D] (下投影), 输入为 In: bf16[B, D].
Here's the full algorithm for our little Transformer with no parallelism. 这是我们没有并行化的小 Transformer 的完整算法.
Forward pass: need to compute Loss[B]
前向传播: 需要计算 Loss[B]
- Tmp[B, F] = In[B, D] *D Win[D, F]
- Out[B, D] = Tmp[B, F] *F Wout[F, D]
- Loss[B] = …
Backward pass: need to compute dWout[F, D], dWin[D, F]
反向传播: 需要计算 dWout[F, D], dWin[D, F]
- dOut[B, D] = …
- dWout[F, D] = Tmp[B, F] *B dOut[B, D]
- dTmp[B, F] = dOut[B, D] *D Wout[F, D]
- dWin[D, F] = In[B, D] *B dTmp[B, F]
- dIn[B, D] = dTmp[B, F] *F Win[D, F] (needed for previous layers)
- dOut[B, D] = …
- dWout[F, D] = Tmp[B, F] *B dOut[B, D]
- dTmp[B, F] = dOut[B, D] *D Wout[F, D]
- dWin[D, F] = In[B, D] *B dTmp[B, F]
- dIn[B, D] = dTmp[B, F] *F Win[D, F] (前几层需要)
We provide this for comparison to the algorithms with communication added.
我们提供此内容是为了与添加了通信的算法进行比较。
Here are the 4 parallelism schemes we will discuss. Each scheme can be thought of as uniquely defined by a sharding for In, Win, Wout, and Out in the above diagram.
以下是我们即将讨论的 4 种并行方案。每种方案都可以被认为是由上图中 In, Win, Wout,和 Out 的分片唯一确定的。
1. Data parallelism: activations sharded along batch, parameters and optimizer state are replicated on each device. Communication only occurs during the backwards pass.
1. 数据并行: 激活沿批次分片,参数和优化器状态在每个设备上复制。通信仅在反向传播期间发生。
2. Fully-sharded data parallelism (FSDP or ZeRO-3): activations sharded along batch (like pure data parallelism), parameters sharded along same mesh axis and AllGathered just-in-time before use in forward pass. Optimizer state also sharded along batch. Reduces duplicated memory.
2. 完全分片数据并行 (FSDP 或 ZeRO-3): 激活沿批次分片 (如纯数据并行),参数沿同一网格轴分片,并在前向传播中使用前及时进行 AllGather。优化器状态也沿批次分片。减少了重复的内存。
3. Tensor parallelism (also called Megatron sharding or model parallelism): activations sharded along D (), parameters sharded along F (). AllGather and ReduceScatter activations before and after each block. Compatible with FSDP.
3. 张量并行 (也称为 Megatron 分片或模型并行): 激活沿 D () 分片,参数沿 F () 分片。在每个块之前和之后对激活进行 AllGather 和 ReduceScatter。与 FSDP 兼容。
4. Pipeline parallelism: weights sharded along the layer dimension, activations microbatched and rolled along the layer dimension. Communication between pipeline stages is minimal (just moving activations over a single hop). To abuse notation:
4. 流水线并行: 权重沿层维度分片,激活进行微批次并沿层维度滚动。流水线阶段之间的通信是最小的 (只是在单个跳跃上传输激活)。滥用符号:
Data Parallelism数据并行
Syntax:
语法:
When your model fits on a single chip with even a tiny batch size (>240 tokens, so as to be compute-bound), you should always use simple data parallelism. Pure data parallelism splits our activations across any number of TPUs so long as the number of TPUs is smaller than our batch size. The forward pass involves no communication, but at the end of every step, each TPU performs an AllReduce on its local gradients to synchronize them before updating the parameters.
当你的模型可以容纳在单个芯片上,即使批次大小很小 (>240 个 token,以便受计算限制), 你都应该始终使用简单的数据并行。 纯数据并行将我们的激活分布在任意数量的 TPU 上,只要 TPU 的数量小于我们的批次大小。前向传播不涉及通信,但在每个步骤结束时,每个 TPU 都会对其本地梯度执行 AllReduce,以在更新参数之前同步它们。
Figure: a diagram of pure data parallelism (forward pass). Our activations (left) are fully sharded along the batch dimension and our weights are fully replicated, so each TPU has an identical copy of the weights. This means the total memory of our weights is increased by a factor of N, but no communication is required on the forward-pass.
图: 纯数据并行图 (前向传播). 我们的激活 (左) 沿批次维度完全分片, 我们的权重完全复制, 因此每个 TPU 都有一个相同的权重副本. 这意味着我们权重的总内存增加了 N 倍, 但在前向传播中不需要通信.
Here's the full algorithm for the forward and backwards pass. We abuse notation to write dL/dOut as dOut, purely for compactness. 这是前向和反向传播的完整算法. 我们滥用符号将 dL/dOut 写为 dOut, 纯粹是为了简洁.
Pure Data Parallelism Algorithm:
纯数据并行算法:
Forward pass: need to compute Loss[BX]
前向传播: 需要计算 Loss[BX]
- Tmp[BX, F] = In[BX, D] *D Win[D, F]
- Out[BX, D] = Tmp[BX, F] *F Wout[F, D]
- Loss[BX] = …
Backward pass: need to compute dWout[F, D], dWin[D, F]
反向传播: 需要计算 dWout[F, D], dWin[D, F]
- dOut[BX, D] = …
- dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
- dWout[F, D] = AllReduce(dWout[F, D] {UX}) (not on critical path, can be done async)
- dTmp[BX, F] = dOut[BX, D] *D Wout[F, D]
- dWin[D, F] {UX} = In[BX, D] *B dTmp[BX, F]
- dWin[D, F] = AllReduce(dWin[D, F] {UX}) (not on critical path, can be done async)
- dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (needed for previous layers)
- dOut[BX, D] = …
- dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
- dWout[F, D] = AllReduce(dWout[F, D] {UX}) (不在关键路径上,可以异步完成)
- dTmp[BX, F] = dOut[BX, D] *D Wout[F, D]
- dWin[D, F] {UX} = In[BX, D] *B dTmp[BX, F]
- dWin[D, F] = AllReduce(dWin[D, F] {UX}) (不在关键路径上,可以异步完成)
- dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (前几层需要)
We ignore the details of the loss function and abbreviate . Note that, although our final loss is the average AllReduce(Loss[BX]), we only need to compute the AllReduce on the backward pass when averaging weight gradients.
我们忽略了损失函数的细节,并将 缩写。请注意,尽管我们的最终损失是平均 AllReduce(Loss[BX]),但我们只需要在反向传播中计算 AllReduce,当平均权重梯度时。
Note that the forward pass has no communication — it’s all in the backward pass! The backward pass also has the great property that the AllReduces aren’t in the “critical path”, meaning that each AllReduce can be performed whenever it’s convenient and doesn’t block you from performing subsequent operations. The overall communication cost can still bottleneck us if it exceeds our total compute cost, but it is much more forgiving from an implementation standpoint. We’ll see that model/tensor parallelism doesn’t have this property.
请注意,前向传播没有通信 —— 全在反向传播中!反向传播还有一个很好的特性,即 AllReduce 不在“关键路径”上,这意味着每个 AllReduce 可以在方便的时候执行,并且不会阻止你执行后续操作。总体通信成本仍然可能成为我们的瓶颈,如果它超过了我们的总计算成本,但从实现的角度来看,它要宽容得多。我们将看到模型/张量并行不具有此特性。
Why do this? Pure data parallelism reduces activation memory pressure by splitting our activations over the batch dimension, allowing us to almost arbitrarily increase batch size as long as we have more chips to split the batch dimension over. Especially during training when our activations often dominate our memory usage, this is very helpful.
为什么要这样做? 纯数据并行通过将我们的激活分布在批次维度上来减少激活内存压力,只要我们有更多的芯片来分布批次维度,就可以几乎任意地增加批次大小。特别是在训练期间,当我们的激活通常主导我们的内存使用时,这非常有用。
Why not do this? Pure data parallelism does nothing to reduce memory pressure from model parameters or optimizer states, which means pure data parallelism is rarely useful for interesting models at scale where our parameters + optimizer state don’t fit in a single TPU. To give a sense of scale, if we train with parameters in bf16 and optimizer state in fp32 with Adam2, the largest model we can fit has parameters, so e.g. on a TPUv5p chip with 96GB of HBM and pure data parallelism this is about 9B parameters.
为什么不这样做? 纯数据并行对减少模型参数或优化器状态的内存压力没有任何作用,这意味着纯数据并行对于大规模的有趣模型很少有用,因为我们的参数 + 优化器状态无法容纳在单个 TPU 中。为了给你一个规模感,如果我们用 bf16 的参数和 fp32 的 Adam 优化器状态进行训练2,我们可以容纳的最大模型有 个参数,所以例如在具有 96GB HBM 和纯数据并行的 TPUv5p 芯片上,这大约是 9B 个参数。
To make this useful for real models during training, we’ll need to at least partly shard the model parameters or optimizer.
为了在训练期间对真实模型有用,我们至少需要部分地对模型参数或优化器进行分片。
When do we become bottlenecked by communication? As we can see above, we have two AllReduces per layer, each of size (for bf16 weights). When does data parallelism make us communication bound?
我们什么时候会受到通信的瓶颈? 正如我们上面看到的,我们每层有两个 AllReduce,每个大小为 (对于 bf16 权重)。数据并行什么时候会让我们受通信限制?
As in the table above, let = per-chip FLOPs, = bidirectional network bandwidth, and = number of shards across which the batch is partitioned4. Let’s calculate the time required to perform the relevant matmuls, , and the required communication time . Since this parallelism scheme requires no communication in the forward pass, we only need to calculate these quantities for the backwards pass.
如上表所示,设 = 每个芯片的 FLOPs, = 双向网络带宽, = 批次分区的分片数量4。让我们计算执行相关矩阵乘法所需的时间 ,以及所需的通信时间 。由于这个并行方案在前向传播中不需要通信,我们只需要计算反向传播的这些量。
Communication time: From a previous section we know that the time required to perform an AllReduce in a 1D mesh depends only on the total bytes of the array being AllReduced and the ICI bandwidth ; specifically the AllReduce time is . Since we need to AllReduce for both and , we have 2 AllReduces per layer. Each AllReduce is for a weight matrix, i.e. an array of parameters, or bytes. Putting this all together, the total time for the AllReduce in a single layer is
通信时间: 从上一节我们知道,在 1D 网格中执行 AllReduce 所需的时间仅取决于被 AllReduce 的数组的总字节数和 ICI 带宽 ;具体来说,AllReduce 时间是 。由于我们需要对 和 都进行 AllReduce,我们每层有 2 个 AllReduce。每个 AllReduce 都是针对一个权重矩阵,即一个 参数的数组,或 字节。将所有这些放在一起,单层中 AllReduce 的总时间是
Matmul time: Each layer comprises two matmuls in the forward pass, or four matmuls in the backwards pass, each of which requires FLOPs. Thus, for a single layer in the backward pass, we have
矩阵乘法时间: 每层在前向传播中包含两个矩阵乘法,或在反向传播中包含四个矩阵乘法,每个都需要 FLOPs。因此,对于反向传播中的单层,我们有
Since we overlap, the total time per layer is the max of these two quantities:
由于我们重叠,每层的总时间是这两个量的最大值:
We become compute-bound when , or when
计算成为瓶颈的条件是 ,等价于
The upshot is that, to remain compute-bound with data parallelism, we need the per-device batch size to exceed the ICI operational intensity, . This is ultimately a consequence of the fact that the computation time scales with the per-device batch size, while the communication time is independent of this quantity (since we are transferring model weights). Note the resemblance of the condition to the single-device compute-bound rule ; in that case as well, the rule came from the fact that computation time scaled with batch size while data-transfer size was (in the regime) independent of batch size.
结果是,为了在数据并行下保持受计算限制,我们需要每个设备的批次大小 超过 ICI 操作强度 。这最终是由于计算时间随每个设备的批次大小而扩展,而通信时间与此量无关 (因为我们正在传输模型权重) 的结果。请注意 条件与单设备受计算限制规则 的相似之处;在那种情况下,规则也来自于计算时间随批次大小而扩展,而数据传输大小 (在 情况下) 与批次大小无关的事实。
Let’s put in some real numbers to get a sense of scale. For TPUv5p, C=4.6e14 and W=2 * 9e10 for 1D data parallelism over ICI, so our batch size per chip must be at least 2,550 to avoid being communication-bound. Since we can do data parallelism over multiple axes, if we dedicate all three axes of a TPUv5p pod to pure data parallelism, we 3x our bandwidth and can scale down to only BS=850 per TPU or 7.6M tokens per batch per pod (of 8960 chips)! This tells us that it’s fairly hard to become bottlenecked by pure data parallelism!
让我们输入一些真实数字来感受一下规模。对于 TPUv5p, C=4.6e14, W=2 * 9e10 (对于 ICI 上的 1D 数据并行),所以我们每个芯片的批次大小必须至少为 2,550 才能避免受通信限制。由于我们可以在多个轴上进行数据并行,如果我们将 TPUv5p pod 的所有三个轴都用于纯数据并行,我们的带宽 会增加 3 倍,并且可以缩小到每个 TPU 只有 BS=850,或每个 pod (8960 个芯片) 每批 760 万个 token! 这告诉我们,纯数据并行很难成为瓶颈!
Note on multiple mesh axes: We should quickly note how multiple axes affects the available bandwidth. When we use multiple mesh axes for a given parallelism strategy, we get more bandwidth.
关于多个网格轴: 如果一种并行策略横跨多个网格轴,就能利用更多带宽。
- Definition: (, , etc.) is the number of hardware mesh axes that a given parallelism strategy spans.
- Effect (bandwidth-bound): Using axes provides ( times) aggregate link bandwidth, so collective time scales .
- 定义: (以及 、 等)表示某种并行策略横跨的硬件网格轴数。
- 带宽受限时的效果: 使用 个轴可以提供 倍的链路总带宽,集合通信耗时因而满足 。
Fully-Sharded Data Parallelism (FSDP)完全分片数据并行 (FSDP)
Syntax:
语法:
Fully-sharded data parallelism (often called FSDP or ZeRO-sharding[1]) splits the model optimizer states and weights across the data parallel shards and efficiently gathers and scatters them as needed. Compared to pure data parallelism, FSDP drastically reduces per-device memory usage and saves on backward pass FLOPs, with very minimal overhead.
完全分片数据并行 (通常称为 FSDP 或 ZeRO-sharding[1]) 将模型优化器状态和权重分布在数据并行分片上,并根据需要高效地收集和分散它们。与纯数据并行相比,FSDP 大大减少了每个设备的内存使用量,并在反向传播 FLOPs 上节省了开销,开销非常小。
Figure: FSDP shards the contracting dimension of Win and the output dimension of Wout along the data dimension. This reduces memory but (from Section 3) requires us to gather the weights for W before we perform the matmul. Note that the activations (left) are not sharded along the contracting dimension, which is what forces us to gather. Note that our weight optimizer state is likewise sharded along the contracting dimension.
图: FSDP 将 Win 的收缩维度和 Wout 的输出维度沿数据维度进行分片. 这减少了内存, 但 (从第 3 节) 要求我们在执行矩阵乘法之前收集 W 的权重. 请注意, 激活 (左) 没有沿收缩维度进行分片, 这就是迫使我们进行收集的原因. 请注意, 我们的权重优化器状态同样沿收缩维度进行分片.
You’ll remember (from Section 3) that an AllReduce can be decomposed into an AllGather and a ReduceScatter. This means that, instead of doing the full gradient AllReduce for standard data parallelism, we can shard the weights and optimizer states across chips, AllGather them at each layer during the forward pass and ReduceScatter across the weights during the backward pass at no extra cost.
你会记得 (从第 3 节), AllReduce 可以分解为 AllGather 和 ReduceScatter。这意味着,与其为标准数据并行进行完整的梯度 AllReduce,我们可以将权重和优化器状态分布在芯片上,在前向传播的每一层对它们进行 AllGather,并在反向传播期间对权重进行 ReduceScatter,而无需额外成本。
Here's the full algorithm for FSDP. 这是 FSDP 的完整算法.
Fully-Sharded Data Parallelism (FSDP):
完全分片数据并行 (FSDP):
Forward pass: need to compute Loss[BX]
前向传播: 需要计算 Loss[BX]
- Win[D, F] = AllGather(Win[DX, F]) (not on critical path, can do it during previous layer)
- Tmp[BX, F] = In[BX, D] *D Win[D, F] (can throw away Win[D, F] now)
- Wout[F, D] = AllGather(Wout[F, DX]) (not on critical path, can do it during previous layer)
- Out[BX, D] = Tmp[BX, F] *F Wout[F, D]
- Loss[BX] = …
- Win[D, F] = AllGather(Win[DX, F]) (不在关键路径上,可以在前一层期间完成)
- Tmp[BX, F] = In[BX, D] *D Win[D, F] (现在可以丢弃 Win[D, F])
- Wout[F, D] = AllGather(Wout[F, DX]) (不在关键路径上,可以在前一层期间完成)
- Out[BX, D] = Tmp[BX, F] *F Wout[F, D]
- Loss[BX] = …
Backward pass: need to compute dWout[F, DX], dWin[DX, F]
反向传播: 需要计算 dWout[F, DX], dWin[DX, F]
- dOut[BX, D] = …
- dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
- dWout[F, DX] = ReduceScatter(dWout[F, D] {UX}) (not on critical path, can be done async)
- Wout[F, D] = AllGather(Wout[F, DX]) (can be done ahead of time)
- dTmp[BX, F] = dOut[BX, D] *D Wout[F, D] (can throw away Wout[F, D] here)
- dWin[D,F] {UX} = In[BX, D] *B dTmp[BX, F]
- dWin[DX, F] = ReduceScatter(dWin[D, F] {UX}) (not on critical path, can be done async)
- Win[D, F] = AllGather(Win[DX, F]) (can be done ahead of time)
- dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (needed for previous layers) (can throw away Win[D, F] here)
- dOut[BX, D] = …
- dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
- dWout[F, DX] = ReduceScatter(dWout[F, D] {UX}) (不在关键路径上,可以异步完成)
- Wout[F, D] = AllGather(Wout[F, DX]) (可以提前完成)
- dTmp[BX, F] = dOut[BX, D] *D Wout[F, D] (可以在这里丢弃 Wout[F, D])
- dWin[D,F] {UX} = In[BX, D] *B dTmp[BX, F]
- dWin[DX, F] = ReduceScatter(dWin[D, F] {UX}) (不在关键路径上,可以异步完成)
- Win[D, F] = AllGather(Win[DX, F]) (可以提前完成)
- dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (前几层需要) (可以在这里丢弃 Win[D, F])
This is also called “ZeRO Sharding”, from “Zero Redundancy Optimizer” since we don’t perform any unnecessary compute or store any unnecessary state. ZeRO-{1,2,3} are used to refer to sharding the optimizer states, gradients, and weights in this way, respectively. Since all have the same communication cost5, we can basically always do ZeRO-3 sharding, which shards the parameters, gradients, and optimizer states across a set of devices.
这也被称为“ZeRO Sharding”,来自“零冗余优化器”(Zero Redundancy Optimizer),因为我们不执行任何不必要的计算或存储任何不必要的状态。ZeRO-{1,2,3} 分别用于指以这种方式对优化器状态,梯度和权重进行分片。由于所有这些都具有相同的通信成本5,我们基本上总是可以进行 ZeRO-3 分片,它将参数,梯度和优化器状态分布在一组设备上。
Why would we do this? Standard data parallelism involves a lot of duplicated work. Each TPU AllReduces the full gradient, then updates the full optimizer state (identical work on all TPUs), then updates the parameters (again, fully duplicated). For ZeRO sharding (sharding the gradients/optimizer state), instead of an AllReduce, you can ReduceScatter the gradients, update only your shard of the optimizer state, update a shard of the parameters, then AllGather the parameters as needed for your forward pass.
我们为什么要这样做? 标准数据并行涉及大量重复工作。每个 TPU 都对完整的梯度进行 AllReduce,然后更新完整的优化器状态 (所有 TPU 上的工作都相同),然后更新参数 (同样,完全重复)。对于 ZeRO 分片 (对梯度/优化器状态进行分片),你可以对梯度进行 ReduceScatter,而不是进行 AllReduce,只更新你的优化器状态分片,更新一个参数分片,然后根据需要为你的前向传播 AllGather 参数。
When do we become bottlenecked by communication? Our relative FLOPs and comms costs are exactly the same as pure data parallelism, since each AllReduce in the backward pass has become an AllGather + ReduceScatter. Recall that an AllReduce is implemented as an AllGather and a ReduceScatter, each with half the cost. Here we model the forward pass since it has the same FLOPs-to-comms ratio as the backward pass:
我们什么时候会受到通信的瓶颈? 我们的相对 FLOPs 和通信成本与纯数据并行完全相同,因为反向传播中的每个 AllReduce 都变成了 AllGather + ReduceScatter。回想一下,AllReduce 是作为 AllGather 和 ReduceScatter 实现的,每个的成本都是一半。在这里,我们对前向传播进行建模,因为它具有与反向传播相同的 FLOPs-通信比:
Therefore, as with pure data-parallelism, we are compute bound when , i.e. when the per-device batch size exceeds the “ICI operational intensity” (4.59e14 / 1.8e11 = 2550 for v5p). This is great for us, because it means if our per-device batch size is big enough to be compute-bound for pure data-parallelism, we can — without worrying about leaving the compute-bound regime — simply upgrade to FSDP, saving ourselves a massive amount of parameter and optimizer state memory! Though we did have to add communication to the forward pass, this cost is immaterial since it just overlaps with forward-pass FLOPs.
因此,与纯数据并行一样,当 时,我们受计算限制,即当每个设备的批次大小 超过“ICI 操作强度” (4.59e14 / 1.8e11 = 2550 对于 v5p) 时。这对我们来说很棒,因为这意味着如果我们的每个设备的批次大小足够大,以至于对于纯数据并行来说是受计算限制的,我们可以 —— 不用担心离开受计算限制的状态 —— 简单地升级到 FSDP,为我们节省大量的参数和优化器状态内存!尽管我们确实在前向传播中增加了通信,但这个成本是无关紧要的,因为它只是与前向传播的 FLOPs 重叠。
For example, DeepSeek-V2 (one of the only recent strong models to release information about its training batch size) used a batch size of ~40M tokens. This would allow us to scale to roughly 47,000 chips, or around 5 TPUv5 pods, before we hit a bandwidth limit.
例如,DeepSeek-V2 (近期少数公开训练批次大小的高性能模型之一) 使用了约 40M token 的批次大小。这使我们能够扩展到大约 47,000 个芯片,或大约 5 个 TPUv5 pod,然后我们才会达到带宽限制。
For LLaMA-3 70B, which was trained for approximately 6.3e24 (15e12 * 70e9 * 6) FLOPs, we could split a batch of 16M tokens over roughly 16e6 / (2550 / 3) = 18,823 chips (roughly 2 pods of 8960 chips), each with 4.59e14 FLOPs running at 50% peak FLOPs utilization (often called MFU), and train it in approximately 17 days. Not bad! But let’s explore how we can do better.
对于 LLaMA-3 70B,它训练了大约 6.3e24 (15e12 * 70e9 * 6) FLOPs,我们可以将 16M token 的批量分布在大约 16e6 / (2550 / 3) = 18,823 个芯片上 (大约 2 个 8960 个芯片的 pod),每个芯片具有 4.59e14 FLOPs,运行在 50% 的峰值 FLOPs 利用率 (通常称为 MFU), 并在大约 17 天内完成训练。不错!但让我们探讨一下如何做得更好。
Tensor Parallelism张量并行
Syntax: (we use to eventually combine with FSDP)
语法: (我们使用 最终与 FSDP 结合)
In a fully-sharded data-parallel AllReduce we move the weights across chips. We can also shard the feedforward dimension of the model and move the activations during the layer — this is called “1D model parallelism” or Megatron sharding[2]. This can unlock a smaller efficient batch size per pod. The figure below shows an example of a single matrix sharded in this way:
在完全分片的数据并行 AllReduce 中,我们在芯片之间移动权重。我们也可以对模型的前馈维度进行分片,并在层内移动激活 —— 这被称为“1D 模型并行”或 Megatron 分片[2]。这可以解锁每个 pod 更小的有效批次大小。下图显示了以这种方式分片的单个矩阵的示例:
Figure: an example of basic tensor parallelism. Since we’re only sharding our activations over Y (unlike in FSDP where we shard over X), we replicate our activations over X. Using our standard syntax, this is A[B, DY] * B[D, FY] -> C[B, FY]. Because we’re only sharding over one of the contracting dimensions, we typically AllGather the activations A before the matmul.
图: 基本张量并行的示例. 由于我们只在 Y 上对激活进行分片 (与 FSDP 中我们在 X 上分片不同), 我们在 X 上复制我们的激活. 使用我们的标准语法, 这是 A[B, DY] * B[D, FY] -> C[B, FY]. 因为我们只在一个收缩维度上进行分片, 我们通常在矩阵乘法之前对激活 A 进行 AllGather.
As noted, In[B, DY] *D Win[D, FY] *F Wout[FY, D] -> Out[B, DY] means we have to gather our activations before the first matmul. This is cheaper than ZeRO sharding when the activations are smaller than the weights. This is typically true only with some amount of ZeRO sharding added (which reduces the size of the gather). This is one of the reasons we tend to mix ZeRO sharding and tensor parallelism.
如前所述,In[B, DY] *D Win[D, FY] *F Wout[FY, D] -> Out[B, DY] 意味着我们必须在第一个矩阵乘法之前收集我们的激活。当激活比权重小时,这比 ZeRO 分片更便宜。 这通常只有在添加了一定量的 ZeRO 分片 (这减少了收集的大小) 时才成立。这是我们倾向于混合 ZeRO 分片和张量并行的原因之一。
Here's the algorithm for tensor parallelism! 这是张量并行的算法!
Tensor Parallelism:
张量并行:
Forward pass: need to compute Loss[B]
前向传播: 需要计算 Loss[B]
- In[B, D] = AllGather(In[B, DY]) (on critical path)
- Tmp[B, FY] = In[B, D] *D Win[D, FY] (not sharded along contracting, so no comms)
- Out[B, D] {UY} = Tmp[B, FY] *F Wout[FY, D]
- Out[B, DY] = ReduceScatter(Out[B, D] {UY}) (on critical path)
- Loss[B] = …
- In[B, D] = AllGather(In[B, DY]) (在关键路径上)
- Tmp[B, FY] = In[B, D] *D Win[D, FY] (不在收缩维度上分片,所以没有通信)
- Out[B, D] {UY} = Tmp[B, FY] *F Wout[FY, D]
- Out[B, DY] = ReduceScatter(Out[B, D] {UY}) (在关键路径上)
- Loss[B] = …
Backward pass: need to compute dWout[FY, D], dWin[D, FY]
反向传播: 需要计算 dWout[FY, D], dWin[D, FY]
- dOut[B, DY] = …
- dOut[B, D] = AllGather(dOut[B, DY]) (on critical path)
- dWout[FY, D] = Tmp[B, FY] *B dOut[B, D]
- dTmp[B, FY] = dOut[B, D] *D Wout[FY, D] (can throw away dOut[B, D] here)
- In[B, D] = AllGather(In[B, DY]) (this can be skipped by sharing with (1) from the forward pass)
- dWin[D, FY] = In[B, D] *B dTmp[B, FY]
- dIn[B, D] {UY} = dTmp[B, FY] *F Win[D, FY] (needed for previous layers)
- dIn[B, DY] = ReduceScatter(dIn[B, D] {UY}) (on critical path)
- dOut[B, DY] = …
- dOut[B, D] = AllGather(dOut[B, DY]) (在关键路径上)
- dWout[FY, D] = Tmp[B, FY] *B dOut[B, D]
- dTmp[B, FY] = dOut[B, D] *D Wout[FY, D] (可以在这里丢弃 dOut[B, D])
- In[B, D] = AllGather(In[B, DY]) (可以通过与前向传播的 (1) 共享来跳过)
- dWin[D, FY] = In[B, D] *B dTmp[B, FY]
- dIn[B, D] {UY} = dTmp[B, FY] *F Win[D, FY](计算前面各层时需要)
- dIn[B, DY] = ReduceScatter(dIn[B, D] {UY}) (在关键路径上)
One nice thing about tensor parallelism is that it interacts nicely with the two matrices in our Transformer forward pass. Naively, we would do an AllReduce after each of the two matrices. But here we first do In[B, DY] * Win[D, FY] -> Tmp[B, FY] and then Tmp[B, FY] * Wout[FY, D] -> Out[B, DY]. This means we AllGather In at the beginning, and ReduceScatter Out at the end, rather than doing an AllReduce.
张量并行的一个好处是它与我们 Transformer 前向传播中的两个矩阵很好地交互。简单地说,我们会在两个矩阵之后都进行 AllReduce。但在这里,我们首先进行 In[B, DY] * Win[D, FY] -> Tmp[B, FY],然后 Tmp[B, FY] * Wout[FY, D] -> Out[B, DY]。这意味着我们在开始时对 In 进行 AllGather,在结束时对 Out 进行 ReduceScatter,而不是进行 AllReduce.
How costly is this? Let’s only model the forward pass - the backwards pass is just the transpose of each operation here. In 1D tensor parallelism we AllGather the activations before the first matmul, and ReduceScatter them after the second, sending two bytes at a time (bf16). Let’s figure out when we’re bottlenecked by communication.
这有多昂贵? 让我们只对前向传播进行建模 - 反向传播只是这里每个操作的转置。在 1D 张量并行中,我们在第一个矩阵乘法之前对激活进行 AllGather,在第二个矩阵乘法之后对它们进行 ReduceScatter,一次发送两个字节 (bf16)。让我们找出什么时候我们会受到通信的瓶颈。
Noting that we want compute cost to be greater than comms cost, we get:
注意到我们希望计算成本大于通信成本,我们得到:
Thus for instance, for TPUv5p, in bf16, so we can only do tensor parallelism up to . When we have multiple ICI axes, our is reduced by a factor of , so we get .
因此,例如,对于 TPUv5p, bf16 中的 ,所以我们只能进行张量并行,直到 。当我们有多个 ICI 轴时,我们的 会减少 倍,所以我们得到 .
Note that this doesn’t depend on the precision of the computation, since e.g. for int8, on TPUv5p, is instead of but the comms volume is also halved, so the two factors of two cancel.
请注意,这不依赖于计算的精度,因为例如对于 int8,在 TPUv5p 上, 是 而不是 ,但通信量也减半了,所以两个 2 的因子抵消了。
Let’s think about some examples:
让我们考虑一些例子:
- On TPUv5p with LLaMA 3-70B with , we can comfortably do 8-way tensor parallelism, but will be communication bound on 16 way tensor parallelism. The required F for 8-way model sharding is 20k.
- 在 TPUv5p 上,LLaMA 3-70B 的 ,我们可以轻松地进行 8 路张量并行,但在 16 路张量并行上会受到通信限制。8 路模型分片要求 F 达到 20k。
- For Gemma 7B, , so we become communication bound with 19-way tensor parallelism. That means we could likely do 16-way and still see good performance.
- 对于 Gemma 7B, ,所以我们在 19 路张量并行时会受到通信限制。这意味着我们可能可以进行 16 路,并且仍然看到良好的性能。
Combining FSDP and Tensor Parallelism结合 FSDP 和张量并行
Syntax:
语法:
The nice thing about FSDP and tensor parallelism is that they can be combined. By sharding Win and Wout along both axes we both save memory and compute. Because we shard B along X, we reduce the size of the model-parallel AllGathers, and because we shard F along Y, we reduce the communication overhead of FSDP. This means a combination of the two can get us to an even lower effective batch size than we saw above.
FSDP 和张量并行的好处在于它们可以结合使用。通过在两个轴上对 Win 和 Wout 进行分片,我们既节省了内存又节省了计算。因为我们在 X 上对 B 进行分片,我们减少了模型并行 AllGather 的大小,因为我们在 Y 上对 F 进行分片,我们减少了 FSDP 的通信开销。这意味着两者的结合可以让我们达到比上面看到的更低的有效批次大小。
Figure: a diagram combining FSDP and tensor parallelism. Unlike the other cases, there is no duplication of model parameters.
图: 结合 FSDP 和张量并行的图. 与其他情况不同, 这里没有模型参数的重复.
Here's the full algorithm for mixed FSDP + tensor parallelism. While we have a lot of communication, all our AllGathers and ReduceScatters are smaller because we have batch-sharded our activations and tensor sharded our weights much more! 这是混合 FSDP + 张量并行的完整算法. 虽然我们有很多通信, 但我们所有的 AllGather 和 ReduceScatter 都更小, 因为我们对激活进行了批次分片, 对权重进行了更多的张量分片!
Forward pass: need to compute Loss[B]
前向传播: 需要计算 Loss[B]
- In[BX, D] = AllGatherY(In[BX, DY]) (on critical path)
- Win[D, FY] = AllGatherX(Win[DX, FY]) (can be done ahead of time)
- Tmp[BX, FY] = In[BX, D] *D Win[D, FY]
- Wout[FY, D] = AllGatherX(Wout[FY, DX]) (can be done ahead of time)
- Out[BX, D] {UY} = Tmp[BX, FY] *F Wout[FY, D]
- Out[BX, DY] = ReduceScatterY(Out[BX, D] {UY}) (on critical path)
- Loss[BX] = …
- In[BX, D] = AllGatherY(In[BX, DY]) (在关键路径上)
- Win[D, FY] = AllGatherX(Win[DX, FY]) (可以提前完成)
- Tmp[BX, FY] = In[BX, D] *D Win[D, FY]
- Wout[FY, D] = AllGatherX(Wout[FY, DX]) (可以提前完成)
- Out[BX, D] {UY} = Tmp[BX, FY] *F Wout[FY, D]
- Out[BX, DY] = ReduceScatterY(Out[BX, D] {UY}) (在关键路径上)
- Loss[BX] = …
Backward pass: need to compute dWout[FY, DX], dWin[DX, FY]
反向传播: 需要计算 dWout[FY, DX], dWin[DX, FY]
- dOut[BX, DY] = …
- dOut[BX, D] = AllGatherY(dOut[BX, DY]) (on critical path)
- dWout[FY, D] {UX} = Tmp[BX, FY] *B dOut[BX, D]
- dWout[FY, DX] = ReduceScatterX(dWout[FY, D] {UX})
- Wout[FY, D] = AllGatherX(Wout[FY, DX]) (can be done ahead of time)
- dTmp[BX, FY] = dOut[BX, D] *D Wout[FY, D] (can throw away dOut[B, D] here)
- In[BX, D] = AllGatherY(In[BX, DY]) (not on critical path + this can be shared with (2) from the previous layer)
- dWin[D, FY] {UX} = In[BX, D] *B dTmp[BX, FY]
- dWin[DX, FY] = ReduceScatterX(dWin[D, FY] {UX})
- Win[D, FY] = AllGatherX(Win[DX, FY]) (can be done ahead of time)
- dIn[BX, D] {UY} = dTmp[BX, FY] *F Win[D, FY] (needed for previous layers)
- dIn[BX, DY] = ReduceScatterY(dIn[BX, D] {UY}) (on critical path)
- dOut[BX, DY] = …
- dOut[BX, D] = AllGatherY(dOut[BX, DY]) (在关键路径上)
- dWout[FY, D] {UX} = Tmp[BX, FY] *B dOut[BX, D]
- dWout[FY, DX] = ReduceScatterX(dWout[FY, D] {UX})
- Wout[FY, D] = AllGatherX(Wout[FY, DX]) (可以提前完成)
- dTmp[BX, FY] = dOut[BX, D] *D Wout[FY, D] (可以在这里丢弃 dOut[B, D])
- In[BX, D] = AllGatherY(In[BX, DY]) (不在关键路径上 + 这可以与前一层的 (2) 共享)
- dWin[D, FY] {UX} = In[BX, D] *B dTmp[BX, FY]
- dWin[DX, FY] = ReduceScatterX(dWin[D, FY] {UX})
- Win[D, FY] = AllGatherX(Win[DX, FY]) (可以提前完成)
- dIn[BX, D] {UY} = dTmp[BX, FY] *F Win[D, FY](计算前面各层时需要)
- dIn[BX, DY] = ReduceScatterY(dIn[BX, D] {UY}) (在关键路径上)
What’s the right combination of FSDP and TP? A simple but key maxim is that FSDP moves weights and tensor parallelism moves activations. That means as our batch size shrinks (especially as we do more data parallelism), tensor parallelism becomes cheaper because our activations per-shard are smaller.
FSDP 和 TP 的正确组合是什么? 一个简单但关键的准则是,FSDP 移动权重,张量并行移动激活。这意味着随着我们批次大小的缩小 (特别是当我们进行更多的数据并行时),张量并行变得更便宜,因为我们每个分片的激活更小。
- Tensor parallelism performs which shrinks as grows.
- FSDP performs which shrinks as grows.
- 张量并行执行 ,随着 的增长而缩小。
- FSDP 执行 ,随着 的增长而缩小。
Thus by combining both we can push our minimum batch size per replica down even more. We can calculate the optimal amount of FSDP and TP in the same way as above:
因此,通过结合两者,我们可以将每个副本的最小批次大小进一步降低。我们可以用与上面相同的方式计算 FSDP 和 TP 的最佳数量:
Let be the number of chips dedicated to FSDP and be the number of chips dedicated to tensor parallelism. Let be the total number of chips in our slice with . Let and be the number of mesh axes over which we do FSDP and TP respectively (these should roughly sum to 3). We’ll purely model the forward pass since it has the most communication per FLOP. Then adding up the comms in the algorithm above, we have
设 是专用于 FSDP 的芯片数量, 是专用于张量并行的芯片数量。设 是我们切片中的总芯片数量,。设 和 是我们分别进行 FSDP 和 TP 的网格轴数量 (这些应该大致总和为 3)。我们将纯粹对前向传播进行建模,因为它每个 FLOP 的通信量最大。然后将上面算法中的通信相加,我们得到
And likewise our total FLOPs time is
同样,我们的总 FLOPs 时间是
To simplify the analysis, we make two assumptions: first, we allow and to take on non-integer values (as long as they are positive and satisfy ); second, we assume that we can fully overlap comms on the and axis with each other. Under the second assumption, the total comms time is
为了简化分析,我们做两个假设:首先,我们允许 和 取非整数值 (只要它们是正的并且满足 );其次,我们假设我们可以完全重叠 和 轴上的通信。在第二个假设下,总通信时间是
Before we ask under what conditions we’ll be compute-bound, let’s find the optimal values for and to minimize our total communication. Since our FLOPs is independent of and , the optimal settings are those that simply minimize comms. To do this, let’s write above in terms of and (which is held fixed, as it’s the number of chips in our system) rather than and :
在我们询问在什么条件下我们会受计算限制之前,让我们找到 和 的最佳值以最小化我们的总通信。由于我们的 FLOPs 与 和 无关,最佳设置是那些简单地最小化通信的设置。为此,让我们用 和 (固定不变,因为它是我们系统中的芯片数量) 而不是 和 来写出上面的 :
Because is monotonically increasing in , and is monotonically decreasing in , the maximum must be minimized when , which occurs when
因为 在 中是单调递增的,而 在 中是单调递减的,所以当 时,最大值必须被最小化,这发生在
This is super useful! This tells us, for a given , , and , what amount of FSDP is optimal. Let’s get a sense of scale. Plugging in realistic values, namely (corresponding to a 4x4x4 array of chips), , , gives roughly . So we would choose to be 16 and to be 4, close to our calculated optimum.
这超级有用!这告诉我们,对于给定的 , 和 ,最佳的 FSDP 数量是多少。让我们感受一下规模。输入实际值,即 (对应于一个 4x4x4 的芯片阵列), , ,大约得到 。所以我们会选择 为 16, 为 4,接近我们计算的最优值。
Now let’s return to the question we’ve been asking of all our parallelism strategies: under what conditions will we be compute-bound? Since we can overlap FLOPs and comms, we are compute-bound when
现在让我们回到我们一直对所有并行策略提出的问题:在什么条件下我们会受计算限制? 由于计算可与通信重叠,计算成为瓶颈的条件是
By letting , the ICI arithmetic intensity, we can simplify:
通过令 ,即 ICI 算术强度,我们可以简化:
Since we calculated to make the LHS maximum equal, we can just plug it into either side (noting that ), i.e.
由于我们计算了 以使 LHS 最大值相等,我们可以将其代入任何一边 (注意到 ),即
Further simplifying, we find that
进一步简化,我们发现
where the left-hand-side is proportional to the communication time and the right-hand-side is proportional to the computation time. Note that while the computation time scales linearly with the batch size (as it does regardless of parallelism), the communication time scales as the square root of the batch size. The ratio of the computation to communication time thus also scales as the square root of the batch size:
其中左边与通信时间成正比,右边与计算时间成正比。请注意,虽然计算时间与批次大小成线性关系 (无论并行性如何),但通信时间与批次大小的平方根成正比。因此,计算与通信的耗时之比也与批次大小的平方根成正比:
To ensure that this ratio is greater than one so we are compute bound, we require
为了确保这个比率大于一,以便我们受计算限制,我们需要
To get approximate numbers, again plug in , , and (as it must be for a 3D mesh). This gives roughly . This roughly wins us a factor of eight compared to the purely data parallel (or FSDP) case, where assuming a 3D mesh we calculate that must exceed about to be compute bound.
为了得到近似数字,再次代入 , ,和 (对于 3D 网格必须如此)。这大约得到 。这大约比纯数据并行 (或 FSDP) 的情况赢得了八倍,在纯数据并行的情况下,假设一个 3D 网格,我们计算出 必须超过大约 才能受计算限制。
Below we plot the ratio of FLOPs to comms time for mixed FSDP + TP, comparing it both to only tensor parallelism (TP) and only data parallelism (FSDP), on a representative 4x4x4 chip array. While pure FSDP parallelism dominates for very large batch sizes, in the regime where batch size over number of chips is between roughly 100 and 850, a mixed FSDP + TP strategy is required in order to be compute-bound.
下面我们绘制了混合 FSDP + TP 的 FLOPs 与通信时间之比,并将其与仅张量并行 (TP) 和仅数据并行 (FSDP) 在一个代表性的 4x4x4 芯片阵列上进行了比较。虽然纯 FSDP 并行在非常大的批次大小时占主导地位,但在每个芯片的批次大小在约 100 到 850 之间的范围内,需要混合 FSDP + TP 策略才能受计算限制。
Figure: ratio of FLOPs to comms time for optimal mixed FSDP/TP on a TPUv5p 4x4x4 slice with F=30k. As expected, tensor parallelism has a fixed ratio with batch size; ideal mixed FSDP + TP scales with , and FSDP scales with . However, in intermediate batch size regimes, only FSDP + TP achieves a ratio greater than unity.
图: 在 TPUv5p 4x4x4 切片上, F=30k 时, 最佳混合 FSDP/TP 的 FLOPs 与通信时间之比. 正如预期的那样, 张量并行与批次大小具有固定的比率; 理想的混合 FSDP + TP 与 成比例, FSDP 与 成比例. 然而, 在中间批次大小范围内, 只有 FSDP + TP 才能达到大于一的比率.
Here’s another example of TPU v5p 16x16x16 showing the FLOPs and comms time as a function of batch size for different sharding schemes.
这是 TPU v5p 16x16x16 的另一个例子,显示了不同分片方案下 FLOPs 和通信时间作为批次大小的函数。
Figure: time taken for communication with different parallelism schemes. The black dashed line is the time taken by the matrix multiplication FLOPs, so any curve above this line is comms-bound. We note that all strategies become comms-bound below batch size 6e5, which is in line with our expected 4096 * 2550^2 / (2 * 8192 * 4) = 4e5.
图: 不同并行方案的通信时间. 黑色虚线是矩阵乘法 FLOPs 所花费的时间, 因此任何高于此线的曲线都受通信限制. 我们注意到, 所有策略在批次大小低于 6e5 时都变得受通信限制, 这与我们预期的 4096 * 2550^2 / (2 * 8192 * 4) = 4e5 一致.
The black curve is the amount of time spent on model FLOPs, meaning any batch size where this is lower than all comms costs is strictly comms bound. You’ll notice the black curve intersects the green curve at about 4e5, as predicted.
黑色曲线是模型 FLOPs 所花费的时间,这意味着任何批次大小,如果这个值低于所有通信成本,那么它就严格受通信限制。你会注意到黑色曲线在 4e5 左右与绿色曲线相交,正如预测的那样。
Here’s an interactive animation to play with this, showing the total compute time and communication time for different batch sizes:
这是一个交互式动画,可以让你玩转这个,显示不同批次大小的总计算时间和通信时间:
Figure: forward-pass MLP compute and communication time on a TPU v5p 16x16x16 (, ) as a function of batch size, using , and from above. Drag the slider to change the FSDP/TP split. Any batch size in the shaded region is comms-bound.
图: TPU v5p 16x16x16(、)上,MLP 前向传播的计算与通信时间随批次大小的变化。图使用上文的 、 和 。拖动滑块可以调整 FSDP 与张量并行的分配比例。阴影区域中的批次大小都受通信限制。
You’ll notice this generally agrees with the above: the best split, FSDP=512 and TP=8, becomes compute-bound above a batch size of about 4e5, while every split with more tensor parallelism stays comms-bound at all batch sizes. Splits with less tensor parallelism need an even larger batch.
图中的结果与前面的分析基本一致。最佳配置是 FSDP=512、TP=8,批次大小超过约 4e5 时,进入计算受限区间。张量并行度更高的配置在所有批次大小下都受通信限制。张量并行度更低的配置则需要更大的批次才进入计算受限区间。
Pipelining流水线
You’ll probably notice we’ve avoided talking about pipelining at all in the previous sections. Pipelining is a dominant strategy for GPU parallelism that is somewhat less essential on TPUs. Briefly, pipelined training involves splitting the layers of a model across multiple devices and passing the activations between pipeline stages during the forward and backward pass. The algorithm is something like:
你可能已经注意到,我们在前面的章节中一直避免谈论流水线。流水线是 GPU 并行的一种主导策略,在 TPU 上则不那么重要。简而言之,流水线训练涉及将模型的层分布在多个设备上,并在前向和反向传播期间在流水线阶段之间传递激活。算法大致如下:
- Initialize your data on TPU 0 with your weights sharded across the layer dimension ( for pipelining with FSDP and tensor parallelism).
- Perform the first layer on TPU 0, then copy the resulting activations to TPU 1, and repeat until you get to the last TPU.
- Compute the loss function and its derivative .
- For the last pipeline stage, compute the derivatives and , then copy to the previous pipeline stage and repeat until you reach TPU 0.
- 在 TPU 0 上初始化数据,并沿层维度对权重分片(结合 FSDP 和张量并行时,流水线采用 )。
- 在 TPU 0 上执行第一层,然后将结果激活复制到 TPU 1,重复此过程,直到到达最后一个 TPU.
- 计算损失函数及其导数 .
- 对于最后一个流水线阶段,计算导数 和 ,然后将 复制到前一个流水线阶段,重复此过程,直到到达 TPU 0.
Here is some (working) Python pseudo-code 这里有一些 (可运行的) Python 伪代码
This pseudocode should run on a Cloud TPU VM. While it’s not very efficient or realistic, it gives you a sense how data is being propagated across devices.
这个伪代码应该可以在 Cloud TPU VM 上运行。虽然它不是很高效或现实,但它能让你了解数据是如何在设备之间传播的。
batch_size = 32
d_model = 128
d_ff = 4 * d_model
num_layers = len(jax.devices())
key = jax.random.PRNGKey(0)
# Pretend each layer is just a single matmul.
x = jax.random.normal(key, (batch_size, d_model))
weights = jax.random.normal(key, (num_layers, d_model, d_model))
def layer_fn(x, weight):
return x @ weight
# Assume we have num_layers == num_pipeline_stages
intermediates = [x]
for i in range(num_layers):
x = layer_fn(x, weights[i])
intermediates.append(x)
if i != num_layers - 1:
x = jax.device_put(x, jax.devices()[i+1])
def loss_fn(batch):
return jnp.mean(batch ** 2) # make up some fake loss function
loss, dx = jax.value_and_grad(loss_fn)(x)
for i in range(num_layers - 1, -1, -1):
_, f_vjp = jax.vjp(layer_fn, intermediates[i], weights[i])
dx, dw = f_vjp(dx) # compute the jvp dx @ J(L)(x[i], W[i])
weights[i] = weights[i] - 0.01 * dw # update our weights
if i != 0:
dx = jax.device_put(dx, jax.devices()[i-1])
Why is this a good idea? Pipelining is great for many reasons: it has a low communication cost between pipeline stages, meaning you can train very large models even with low bandwidth interconnects. This is often very useful on GPUs since they are not densely connected by ICI in the way TPUs are.
为什么这是个好主意? 流水线有很多好处:它在流水线阶段之间的通信成本很低,这意味着即使在低带宽互连的情况下,你也可以训练非常大的模型。这在 GPU 上通常非常有用,因为它们不像 TPU 那样通过 ICI 密集连接。
Why is this difficult/annoying? You might have noticed in the pseudocode above that TPU 0 is almost always idle! It’s only doing work on the very first and last step of the pipeline. The period of idleness is called a pipeline bubble and is very annoying to deal with. Typically we try to mitigate this first with microbatching, which sends multiple small batches through the pipeline, keeping TPU 0 utilized for at least a larger fraction of the total step time.
为什么这很困难/烦人? 你可能已经在上面的伪代码中注意到,TPU 0 几乎总是空闲的!它只在流水线的第一个和最后一个步骤中工作。空闲期被称为流水线气泡,处理起来非常烦人。通常我们首先尝试用微批次来缓解这个问题,它通过流水线发送多个小批次,至少在总步骤时间的更大部分内保持 TPU 0 的利用率。
A second approach is to carefully overlap the forward matmul , the backward matmul , and the matmul . Since each of these requires some FLOPs, we can overlap them to fully hide the bubble. Here’s a plot from the recent DeepSeek v3 paper[3] showing their “bubble-free” pipeline schedule:
第二种方法是仔细重叠前向矩阵乘法 ,后向 矩阵乘法 ,以及 矩阵乘法 。由于这些都需要一些 FLOPs,我们可以重叠它们以完全隐藏气泡。这是最近 DeepSeek v3 论文[3]中的一张图,显示了他们的“无气泡”流水线调度:
Figure: the DeepSeek v3 pipeline schedule (from their recent paper). Orange is the forward matmul, green is the dL/dx matmul, and blue is the dL/dW matmul. By prioritizing the backwards dL/dx multiplications, we can avoid “stranding” FLOPs.
图: DeepSeek v3 流水线调度 (来自他们最近的论文). 橙色是前向矩阵乘法, 绿色是 dL/dx 矩阵乘法, 蓝色是 dL/dW 矩阵乘法. 通过优先处理后向 dL/dx 乘法, 我们可以避免“搁浅” FLOPs.
Because it is less critical for TPUs (which have larger interconnected pods), we won’t delve into this as deeply, but it’s a good exercise to understand the key pipelining bottlenecks.
因为它对 TPU (具有更大的互连 pod) 不那么重要,我们不会深入探讨这个问题,但理解关键的流水线瓶颈是一个很好的练习。
Scaling Across Pods跨 Pod 扩展
The largest possible TPU slice is a TPU v5p SuperPod with 8960 chips (and 2240 hosts). When we want to scale beyond this size, we need to cross the Data-Center Networking (DCN) boundary. Each TPU host comes equipped with one or several NICs (Network Interface Cards) that connect the host to other TPU v5p pods over Ethernet. As noted in the TPU Section, each host has about 200Gbps (25GB/s) of full-duplex DCN bandwidth, which is about 6.25GB/s full-duplex (egress) bandwidth per TPU.
最大的 TPU 切片是 TPU v5p SuperPod,拥有 8960 个芯片 (和 2240 个主机)。当我们想要扩展到这个规模以上时,我们需要跨越数据中心网络 (DCN) 的边界。每个 TPU 主机都配备了一个或多个 NIC (网络接口卡),通过以太网将主机连接到其他 TPU v5p pod。正如在TPU 部分中指出的,每个主机大约有 200Gbps (25GB/s) 的全双工 DCN 带宽,这大约是每个 TPU 6.25GB/s 的全双工 (出口) 带宽。
Typically, when scaling beyond a single pod, we do some form of model parallelism or FSDP within the ICI domain, and then pure data parallelism across multiple pods. Let be the number of TPUs we want to scale to and be the number of TPUs per ICI-connected slice. To do an AllReduce over DCN, we can do a ring-reduction over the set of pods, giving us (in the backward pass):
通常,当扩展到单个 pod 以上时,我们在 ICI 域内进行某种形式的模型并行或 FSDP,然后在多个 pod 之间进行纯数据并行。设 是我们想要扩展到的 TPU 数量, 是每个 ICI 连接切片中的 TPU 数量。为了在 DCN 上进行 AllReduce,我们可以对 pod 集合进行环形归约,这给了我们 (在反向传播中):
The comms bandwidth scales with , since unlike ICI the total bandwidth grows as we grow our ICI domain and acquire more NICs. Simplifying, we find that when
通信带宽随 扩展,因为与 ICI 不同,总带宽随着我们扩大 ICI 域并获得更多 NIC 而增长。化简后可知, 的条件是
For TPU v5p, the is about 4.59e14 / 6.25e9 = 73,440. This tells us that to efficiently scale over DCN, there is a minimum batch size per ICI domain needed to egress each node.
对于 TPU v5p, 大约是 4.59e14 / 6.25e9 = 73,440。这告诉我们,为了在 DCN 上高效扩展,每个 ICI 域需要一个最小的批次大小才能让各节点向域外发送数据。
How much of a problem is this? To take a specific example, say we want to train LLaMA-3 70B on TPU v5p with a BS of 2M tokens. LLaMA-3 70B has . From the above sections, we know the following:
这有多大的问题? 举一个具体的例子,假设我们想在 TPU v5p 上用 2M token 的 BS 训练 LLaMA-3 70B. LLaMA-3 70B 的 。从上面的章节中,我们知道以下几点:
- We can do Tensor Parallelism up to .
- We can do FSDP so long as . That means if we want to train with BS=2M and 3 axes of data parallelism, we’d at most be able to use chips, roughly a quarter of a TPU v5p pod.
- When we combine FSDP + Tensor Parallelism, become comms-bound when we have , so this lets us scale to roughly 18k chips! However, the maximum size of a TPU v5p pod is 8k chips, so beyond that we have to use DCN.
- 我们可以进行张量并行,直到 .
- 只要 ,我们就可以进行 FSDP。这意味着如果我们想用 BS=2M 和 3 个数据并行轴进行训练,我们最多只能使用 个芯片,大约是 TPU v5p pod 的四分之一。
- 当我们结合 FSDP + 张量并行时,当我们有 时,会受通信限制,所以这让我们能够扩展到大约 18k 个芯片!然而,TPU v5p pod 的最大大小是 8k 个芯片,所以超过这个数量我们必须使用 DCN.
The TLDR is that we have a nice recipe for training with BS=1M, using roughly X (FSDP) = 1024 and Y (TP) = 8, but with BS=2M we need to use DCN. As noted above, we have a DCN arithmetic intensity of , so we just need to make sure our batch size per ICI domain is greater than this. This is trivial for us, since with 2 pods we’d have a per-pod BS of 1M, and a per TPU batch size of 111, which is great (maybe cutting it a bit close, but theoretically sound).
TLDR 是我们有一个很好的配方,可以用 BS=1M 进行训练,大约使用 X (FSDP) = 1024 和 Y (TP) = 8,但用 BS=2M 我们需要使用 DCN。如上所述,我们的 DCN 算术强度为 ,所以我们只需要确保我们每个 ICI 域的批次大小大于这个值。这对我们来说是微不足道的,因为有 2 个 pod,我们每个 pod 的 BS 为 1M,每个 GPU 的批次大小为 111,这很棒 (可能有点接近,但理论上是合理的).
Takeaways from LLM Training on TPUsTPU 上 LLM 训练的要点
- Increasing parallelism or reducing batch size both tend to make us more communication-bound because they reduce the amount of compute performed per chip.
- 增加并行度或减少批次大小都倾向于使我们更加受通信限制,因为它们减少了每个芯片执行的计算量。
- Up to a reasonable context length (~32k) we can get away with modeling a Transformer as a stack of MLP blocks and define each of several parallelism schemes by how they shard the two/three main matmuls per layer.
- 在合理的上下文长度 (~32k) 内,我们可以将 Transformer 建模为一堆 MLP 块,并通过它们如何对每层的两个/三个主要矩阵乘法进行分片来定义几种并行方案。
- During training there are 4 main parallelism schemes we consider, each of which has its own bandwidth and compute requirements (data parallelism, FSDP, tensor parallelism, and mixed FSDP + tensor parallelism).
- 在训练期间,我们考虑 4 种主要的并行方案,每种方案都有自己的带宽和计算要求 (数据并行,FSDP,张量并行).
| Strategy 策略 | Description 描述 |
|---|---|
| Data Parallelism 数据并行 | Activations are batch sharded, everything else is fully-replicated, we all-reduce gradients during the backward pass. 激活是批次分片的,其他一切都是完全复制的,我们在反向传播期间对梯度进行 all-reduce. |
| FSDP | Activations, weights, and optimizer are batch sharded, weights are gathered just before use, gradients are reduce-scattered. 激活,权重和优化器是批次分片的,权重在使用前被收集,梯度被 reduce-scattered. |
| Tensor Parallelism (aka Megatron, Model) 张量并行 (又名 Megatron,模型) | Activations are sharded along , weights are sharded along , activations are gathered before Win, the result reduce-scattered after Wout. 激活沿 分片,权重沿 分片,激活在 Win 之前被收集,结果在 Wout 之后被 reduce-scattered. |
| Mixed FSDP + Tensor Parallelism 混合 FSDP + 张量并行 | Both of the above, where FSDP gathers the model sharded weights. 以上两者,其中 FSDP 收集模型分片权重。 |
And here are the “formulas” for each method:
以下是每种方法的“公式”:
- Each of these strategies has a limit at which it becomes network/communication bound, based on their per-device compute and comms. Here’s compute and comms per-layer, assuming is FSDP and is tensor parallelism.
- 这些策略中的每一种都有一个限制,在这个限制下它会受到网络/通信的限制,基于它们每个设备的计算和通信。以下是每层的计算和通信,假设 是 FSDP, 是张量并行。
- Pure data parallelism is rarely useful because the model and its optimizer state use bytes = 10x parameter count. This means we can rarely fit more than a few billion parameters in memory.
- 纯数据并行很少有用,因为模型及其优化器状态使用的字节数 = 10 倍参数数量。这意味着我们很少能在内存中容纳超过几十亿个参数。
- Data parallelism and FSDP become comms bound when the , the arithmetic intensity of the network. For ICI this is 2,550 and for DCN this is about 71,000. This can be increased with more parallel axes.
- 当 (网络的算术强度) 时,数据并行和 FSDP 会受通信限制。对于 ICI,这个值是 2,550,对于 DCN,这个值是 75,000。这可以通过更多的并行轴来增加。
- Tensor parallelism becomes comms bound when . This is around 8-16 way for most models. This is independent of the batch size.
- 当 时,张量并行会受通信限制。对于大多数模型,这大约是 8-16 路。 这与批次大小无关。
- Mixed FSDP + tensor parallelism allows us to drop the batch size to as low as . This is remarkably low.
- 混合 FSDP + 张量并行允许我们将批次大小降低到 。这是非常低的。
- Data parallelism across pods requires a minimum batch size per pod of roughly 71,000 before becoming DCN-bound.
- 跨 pod 的数据并行要求每个 pod 的最小批次大小约为 75,000,然后才会受 DCN 限制。
- Basically, if your batch sizes are big or your model is small, things are simple. You can either do data parallelism or FSDP + data parallelism across DCN. The middle section is where things get interesting.
- 基本上,如果你的批次大小很大或者你的模型很小,事情就很简单。你可以进行数据并行或者 FSDP + 跨 DCN 的数据并行。中间部分是事情变得有趣的地方。
Some Problems to Work一些待解决的问题
Let’s use LLaMA-2 13B as a basic model for this section. Here are the model details:
让我们使用 LLaMA-2 13B 作为本节的基本模型。以下是模型详细信息:
| hyperparam 超参数 | value 值 |
|---|---|
| L | 40 |
| D | 5,120 |
| F | 13824 |
| N | 40 |
| K | 40 |
| H | 128 |
| V | 32,000 |
LLaMA-2 has separate embedding and output matrices and a gated MLP block.
LLaMA-2 有独立的嵌入和输出矩阵以及一个门控 MLP 块。
Question 1: How many parameters does LLaMA-2 13B have (I know that’s silly but do the math)? Note that, as in Transformer Math, LLaMA-3 has 3 big FFW matrices, two up-projection and one down-projection. We ignored the two “gating” einsum matrices in this section, but they behave the same as Win in this section.
问题 1: LLaMA-2 13B 有多少参数 (我知道这很傻,但请计算一下)? 注意,正如在Transformer 数学中一样,LLaMA-3 有 3 个大的 FFW 矩阵,两个上投影和一个下投影。我们在本节中忽略了两个“门控” einsum 矩阵,但它们的行为与本节中的 Win 相同。
Click here for the answer. 点击这里查看答案.
- FFW parameters: =
8.5e9 - Attention parameters: =
4.2e9 - Vocabulary parameters: =
0.33e9 - Total:
8.5e9 + 4.2e9 + 0.33e9 = 13.0e9, as expected!
- FFW 参数: =
8.5e9 - 注意力参数: =
4.2e9 - 词汇表参数: =
0.33e9 - 总计:
8.5e9 + 4.2e9 + 0.33e9 = 13.0e9,正如预期的那样!
Question 2: Let’s assume we’re training with BS=16M tokens and using Adam. Ignoring parallelism for a moment, how much total memory is used by the model’s parameters, optimizer state, and activations? Assume we store the parameters in bf16 and the optimizer state in fp32 and checkpoint activations three times per layer (after the three big FFW matmuls).
问题 2: 假设我们用 BS=16M token 进行训练,并使用 Adam。暂时忽略并行性,模型的参数,优化器状态和激活总共使用了多少内存?假设我们将参数存储在 bf16 中,优化器状态存储在 fp32 中,并且每层对激活进行三次检查点 (在三个大的 FFW 矩阵之后).
Click here for the answer. 点击这里查看答案.
The total memory used for the parameters (bf16) and the two optimizer states (fp32, the first and second moment accumulators) is (2 + 4 + 4) * 13e9 ~ 130GB. The activations after the first two matmuls are shaped and after the last one (per the Transformer diagram above), so the total memory for bf16 is or 2 * 40 * 16e6 * 5,120 * (1 + 2 * 2.7) ~ 4.2e13 = 42TB, since B=16e6. All other activations are more or less negligible.
参数 (bf16) 和两个优化器状态 (fp32,一阶和二阶矩累加器) 使用的总内存是 (2 + 4 + 4) * 13e9 ~ 130GB。前两个矩阵乘法后的激活形状为 ,最后一个之后为 (根据上面的 Transformer 图),所以 bf16 的总内存是 或 2 * 40 * 16e6 * 5,120 * (1 + 2 * 2.7) ~ 4.2e13 = 42TB,因为 B=16e6。所有其他激活或多或少都可以忽略不计。
Question 3: Assume we want to train with 32k sequence length and a total batch size of 3M tokens on a TPUv5p 16x16x16 slice. Assume we want to use bfloat16 weights and a float32 optimizer, as above.
问题 3: 假设我们想在 TPUv5p 16x16x16 切片上用 32k 序列长度和 3M token 的总批次大小进行训练。假设我们想使用 bfloat16 权重和 float32 优化器,如上所述。
- Can we use pure data parallelism? Why or why not?
- Can we use pure FSDP without being comms-bound? Why or why not? With pure FSDP, how much memory will be used per device (assume we do gradient checkpointing only after the 3 big FFW matrices).
- Can we use mixed FSDP + tensor parallelism? Why or why not? If so, what should and be? How much memory will be stored per device? Using only roofline FLOPs estimates and ignoring attention, how long will each training step take at 40% MFU?
- 我们可以使用纯数据并行吗?为什么或为什么不?
- 我们可以使用纯 FSDP 且不受通信限制吗?为什么? 使用纯 FSDP,每个设备将使用多少内存 (假设我们只在 3 个大的 FFW 矩阵之后进行梯度检查点).
- 我们可以使用混合 FSDP + 张量并行吗?为什么或为什么不?如果可以, 和 应该是什么?每个设备将存储多少内存?仅使用屋顶线 FLOPs 估计并忽略注意力,在 40% MFU 下,每个训练步骤需要多长时间?
Click here for the answer. 点击这里查看答案.
First, let’s write down some numbers. With 32k sequence length and a 3M batch size, we have a sequence batch size of 96. On a TPU v5p 16x16x16 slice, we have 393TB of HBM.
首先,让我们写下一些数字。32k 序列长度和 3M 批次大小,我们的序列批次大小为 96。在 TPU v5p 16x16x16 切片上,我们有 393TB 的 HBM.
- We can’t use pure data parallelism, because it replicates the parameters and optimizer states on each chip, which are already around 130GB (from Q2) which is more HBM than we have per-chip (96GB).
- 纯数据并行会在每个芯片上完整复制参数和优化器状态。按第 2 题的计算,两者合计约 130GB,超过了每个芯片的 96GB HBM,因此无法用这种方式训练。
- Let’s start by looking purely at memory. Replacing BS=16M with 3M in Q2, we get
~7.86e12total checkpoint activations, and with the 1.3e11 optimizer state this brings us to almost exactly 8e12 = 8TB. The TPUv5p slice has393TBof HBM in total, so we are safely under the HBM limit. Next let’s look at whether we’ll be comms or compute-bound. With 4096 chips and 3 axes of parallelism, we can do a minimum batch size of850 * 4096 = 3.48Mtokens. That’s slightly above our 3M batch size. So we’re actually comms-bound, which is sad. So the general answer is no, we cannot do FSDP alone without being comms-bound.
- 让我们从纯粹的内存角度开始。在 Q2 中用 3M 替换 BS=16M,我们得到
~7.86e12的总检查点激活,加上 1.3e11 的优化器状态,这使我们几乎正好达到 8e12 = 8TB. TPUv5p 切片总共有393TB的 HBM,所以我们安全地低于 HBM 限制。接下来让我们看看我们是受通信限制还是受计算限制。有 4096 个芯片和 3 个并行轴,我们可以做的最小批次大小是850 * 4096 = 3.48Mtoken。这略高于我们的 3M 批次大小。所以我们实际上受通信限制,这很遗憾。因此,仅使用 FSDP 会受到通信限制。
- Now we know our primary concern is being comms-bound, so let’s plug in some numbers. First of all, we know from above that our per-chip batch size with mixed FSDP + tensor parallelism needs to be above here. That means we can in theory do this! Let’s figure out how much of each.
- 现在我们知道我们的主要问题是受通信限制,所以让我们输入一些数字。首先,我们从上面知道,我们每个芯片的批次大小与混合 FSDP + 张量并行需要高于 。这意味着我们理论上可以做到这一点!让我们弄清楚每种并行需要多少。
We have the rule , so here we have sqrt(3e6 * 2 * 4096 / 13824) = 1333, meaning we’ll do roughly 1024 way DP and 4 way TP. Per TPU memory will be as in (2), and step time will just be 6 * 3e6 * 13e9 / (4096 * 4.6e14 * 0.4) = 300ms.
我们有规则 ,所以这里我们有 sqrt(3e6 * 2 * 4096 / 13824) = 1333,这意味着我们将大约进行 1024 路 DP 和 4 路 TP。每个 TPU 的内存将如 (2) 中所示,步骤时间将只是 6 * 3e6 * 13e9 / (4096 * 4.6e14 * 0.4) = 300ms.
That’s it for Part 5! For Part 6, which applies this content to real LLaMA models, click here!
第五部分到此结束!第六部分,将此内容应用于真实的 LLaMA 模型,点击这里!
That’s it for Part 5! For Part 6, which applies this content to real LLaMA models, click here!
第五部分到此结束!第六部分,将此内容应用于真实的 LLaMA 模型,点击这里!
Appendix附录
Appendix A: Deriving the backward pass comms附录 A:推导反向传播通信
Above, we simplified the Transformer layer forward pass as Out[B, D] = In[B, D] *D Win[D, F] *F Wout[F, D]. How do we derive the comms necessary for the backwards pass?
上面,我们将 Transformer 层的前向传播简化为 Out[B, D] = In[B, D] *D Win[D, F] *F Wout[F, D]。我们如何推导出反向传播所需的通信?
This follows fairly naturally from the rule in the previous section for a single matmul Y = X * A:
这很自然地遵循上一节中单个矩阵乘法 Y = X * A 的规则:
Using this, we get the following formulas (letting Tmp[B, F] stand for In[B, D] * Win[D, F]):
使用这个,我们得到以下公式 (令 Tmp[B, F] 代表 In[B, D] * Win[D, F]):
- dWout[F, D] = Tmp[B, F] *B dOut[B, D]
- dTmp[B, F] = dOut[B, D] *D Wout[F, D]
- dWin[D, F] = In[B, D] *B dTmp[B, F]
- dIn[B, D] = dTmp[B, F] *F Win[D, F]
Note that these formulas are mathematical statements, with no mention of sharding. The job of the backwards pass is to compute these four quantities. So to figure out the comms necessary, we just take the shardings of all the quantities which are to be matmulled in the four equations above (Tmp, dOut, Wout, Win), which are specified by our parallelization scheme, and use the rules of sharded matmuls to figure out what comms we have to do. Note that dOut is sharded in the same way as Out.
请注意,这些公式是数学陈述,没有提到分片。反向传播的工作是计算这四个量。因此,为了弄清楚所需的通信,我们只需获取上面四个方程中要进行矩阵乘法的所有量的分片 (Tmp, dOut, Wout, Win),这些分片由我们的并行化方案指定,并使用分片矩阵乘法的规则来弄清楚我们必须进行什么通信。请注意,dOut 的分片方式与 Out 相同。
References参考文献
- ZeRO: Memory optimizations toward training Trillion parameter models. Rajbhandari, Samyam and Rasley, Jeff and Ruwase, Olatunji and He, Yuxiong. 2019.
- Megatron-LM: Training multi-billion parameter language models using model parallelism. Shoeybi, Mohammad and Patwary, Mostofa and Puri, Raul and LeGresley, Patrick and Casper, Jared and Catanzaro, Bryan. 2019.
- DeepSeek-V3 Technical Report. DeepSeek-AI and Liu, Aixin and Feng, Bei and Xue, Bing and Wang, Bingxuan and Wu, Bochao and Lu, Chengda and Zhao, Chenggang and Deng, Chengqi and Zhang, Chenyu and Ruan, Chong and Dai, Damai and Guo, Daya and Yang, Dejian and Chen, Deli and Ji, Dongjie and Li, Erhang and Lin, Fangyun and Dai, Fucong and Luo, Fuli and Hao, Guangbo and Chen, Guanting and Li, Guowei and Zhang, H and Bao, Han and Xu, Hanwei and Wang, Haocheng and Zhang, Haowei and Ding, Honghui and Xin, Huajian and Gao, Huazuo and Li, Hui and Qu, Hui and Cai, J L and Liang, Jian and Guo, Jianzhong and Ni, Jiaqi and Li, Jiashi and Wang, Jiawei and Chen, Jin and Chen, Jingchang and Yuan, Jingyang and Qiu, Junjie and Li, Junlong and Song, Junxiao and Dong, Kai and Hu, Kai and Gao, Kaige and Guan, Kang and Huang, Kexin and Yu, Kuai and Wang, Lean and Zhang, Lecong and Xu, Lei and Xia, Leyi and Zhao, Liang and Wang, Litong and Zhang, Liyue and Li, Meng and Wang, Miaojun and Zhang, Mingchuan and Zhang, Minghua and Tang, Minghui and Li, Mingming and Tian, Ning and Huang, Panpan and Wang, Peiyi and Zhang, Peng and Wang, Qiancheng and Zhu, Qihao and Chen, Qinyu and Du, Qiushi and Chen, R J and Jin, R L and Ge, Ruiqi and Zhang, Ruisong and Pan, Ruizhe and Wang, Runji and Xu, Runxin and Zhang, Ruoyu and Chen, Ruyi and Li, S S and Lu, Shanghao and Zhou, Shangyan and Chen, Shanhuang and Wu, Shaoqing and Ye, Shengfeng and Ye, Shengfeng and Ma, Shirong and Wang, Shiyu and Zhou, Shuang and Yu, Shuiping and Zhou, Shunfeng and Pan, Shuting and Wang, T and Yun, Tao and Pei, Tian and Sun, Tianyu and Xiao, W L and Zeng, Wangding and Zhao, Wanjia and An, Wei and Liu, Wen and Liang, Wenfeng and Gao, Wenjun and Yu, Wenqin and Zhang, Wentao and Li, X Q and Jin, Xiangyue and Wang, Xianzu and Bi, Xiao and Liu, Xiaodong and Wang, Xiaohan and Shen, Xiaojin and Chen, Xiaokang and Zhang, Xiaokang and Chen, Xiaosha and Nie, Xiaotao and Sun, Xiaowen and Wang, Xiaoxiang and Cheng, Xin and Liu, Xin and Xie, Xin and Liu, Xingchao and Yu, Xingkai and Song, Xinnan and Shan, Xinxia and Zhou, Xinyi and Yang, Xinyu and Li, Xinyuan and Su, Xuecheng and Lin, Xuheng and Li, Y K and Wang, Y Q and Wei, Y X and Zhu, Y X and Zhang, Yang and Xu, Yanhong and Xu, Yanhong and Huang, Yanping and Li, Yao and Zhao, Yao and Sun, Yaofeng and Li, Yaohui and Wang, Yaohui and Yu, Yi and Zheng, Yi and Zhang, Yichao and Shi, Yifan and Xiong, Yiliang and He, Ying and Tang, Ying and Piao, Yishi and Wang, Yisong and Tan, Yixuan and Ma, Yiyang and Liu, Yiyuan and Guo, Yongqiang and Wu, Yu and Ou, Yuan and Zhu, Yuchen and Wang, Yuduan and Gong, Yue and Zou, Yuheng and He, Yujia and Zha, Yukun and Xiong, Yunfan and Ma, Yunxian and Yan, Yuting and Luo, Yuxiang and You, Yuxiang and Liu, Yuxuan and Zhou, Yuyang and Wu, Z F and Ren, Z Z and Ren, Zehui and Sha, Zhangli and Fu, Zhe and Xu, Zhean and Huang, Zhen and Zhang, Zhen and Xie, Zhenda and Zhang, Zhengyan and Hao, Zhewen and Gou, Zhibin and Ma, Zhicheng and Yan, Zhigang and Shao, Zhihong and Xu, Zhipeng and Wu, Zhiyu and Zhang, Zhongyu and Li, Zhuoshu and Gu, Zihui and Zhu, Zijia and Liu, Zijun and Li, Zilin and Xie, Ziwei and Song, Ziyang and Gao, Ziyi and Pan, Zizheng. 2024.
Footnotes
-
↩
We’ll focus on communication bounds — since while memory capacity constraints are important, they typically do not bound us when using rematerialization (activation checkpointing) and a very large number of chips during pre-training. We also do not discuss expert parallelism here for MoEs — which expands the design space substantially, only the base case of a dense Transformer.
我们将专注于通信限制 —— 因为虽然内存容量限制很重要,但在使用重物质化 (激活检查点) 和预训练期间的大量芯片时,它们通常不会限制我们。我们在这里也不讨论 MoE 的专家并行 —— 这大大扩展了设计空间,只讨论密集 Transformer 的基本情况。
-
↩
Adam stores parameters, first order and second order accumulators. Since the params are in bfloat16 and optimizer state is in float32, this gives us
2 + 8 = 10bytes per parameters.Adam 存储参数,一阶和二阶累加器。由于参数是 bfloat16,优化器状态是 float32,这给了我们每个参数
2 + 8 = 10字节。 -
↩
Note that this doesn’t include gradient checkpoints, so this wouldn’t actually be useful. This is an absolute lower bound with a batch of 1 token.
请注意,这不包括梯度检查点,所以这实际上没有用。这是一个批量为 1 个 token 的绝对下限。
-
↩
We assume this partitioning is done over an ICI mesh, so the relevant network bandwidth is
我们假设这个分区是在 ICI 网格上完成的,所以相关的网络带宽是
-
↩
Technically, FSDP adds communication in the forward pass that pure DP doesn’t have, but this is in the same proportion as the backward pass so it should have no effect on the comms roofline. The key here is that ZeRO-3 turns a backward-pass AllReduce into an AllGather and a ReduceScatter, which have the same total comms volume.
技术上,FSDP 在前向传播中增加了纯 DP 没有的通信,但这与反向传播的比例相同,因此它对通信屋顶线没有影响。这里的关键是 ZeRO-3 将反向传播的 AllReduce 变成了 AllGather 和 ReduceScatter,它们具有相同的总通信量。