Skip to content
quboliu
⚙

How to Think About TPUs如何看待 TPU

第 2 章 · How To Scale Your Model (详解屋顶线模型 | 分片矩阵及其乘法)

This section is all about how TPUs work, how they're networked together to enable multi-chip training and inference, and how this affects the performance of our favorite algorithms. There's even some good stuff for GPU users too!

本节全部关于 TPU 的工作原理, 它们如何联网以实现多芯片训练和推理, 以及这如何影响我们最喜欢的算法的性能. 对于 GPU 用户来说, 这里也有一些好东西!

What Is a TPU?什么是 TPU?

A TPU is basically a compute core that specializes in matrix multiplication (called a TensorCore) attached to a stack of fast memory (called high-bandwidth memory or HBM)[1]. Here’s a diagram:

TPU 的主要组成是专用于矩阵乘法的计算核心(称为 TensorCore),以及与之相连的高速内存(称为高带宽内存,简称 HBM)[1]。 下图给出了基本结构。

tpu chip

Figure: the basic components of a TPU chip. The TensorCore is the gray left-hand box, containing the matrix-multiply unit (MXU), vector unit (VPU), and vector memory (VMEM).

图: TPU 芯片的基本组件. TensorCore 是左侧的灰色框,包含矩阵乘法单元 (MXU), 向量单元 (VPU) 和向量内存 (VMEM).

You can think of the TensorCore as basically just being a really good matrix multiplication machine, but it has a few other functions worth noting. The TensorCore has three key units:

你可以将 TensorCore 基本上看作是一台非常擅长矩阵乘法的机器,但它还有一些其他值得注意的功能. TensorCore 有三个关键单元:

  • The MXU (Matrix Multiply Unit) is the core of the TensorCore. On TPU7x, it performs one bf16[8,256] @ bf16[256,256] -> f32[8,256] matrix multiply every 4 cycles using a systolic array (see Appendix B for details).
    • This is about 5.8e14 bf16 FLOPs/s per MXU at 2.2GHz on TPU7x. Each TPU7x chip has 2 TensorCores, and each TensorCore has 2 MXUs, so the total bf16 FLOPs/s for TPU7x is around 2.3e15.
    • TPUs also support lower precision matmuls with higher throughput (e.g. each TPU7x chip can do 4.6e15 fp8 OPs/s).
  • MXU(矩阵乘法单元)是 TensorCore 的核心。TPU7x 上的 MXU 使用脉动阵列,每 4 个周期执行一次 bf16[8,256] @ bf16[256,256] -> f32[8,256] 矩阵乘法(详见附录 B)。
    • TPU7x 的时钟频率为 2.2GHz,每个 MXU 的吞吐量约为 5.8e14 bf16 FLOPs/s。每个 TPU7x 芯片有 2 个 TensorCore,每个 TensorCore 有 2 个 MXU,因此一块芯片的总吞吐量约为 2.3e15 bf16 FLOPs/s。
    • TPU 也支持精度更低、吞吐量更高的矩阵乘法。例如,每块 TPU7x 芯片可达到 4.6e15 fp8 OPs/s。
  • The VPU (Vector Processing Unit) performs general mathematical operations like ReLU activations or pointwise addition or multiplication between vectors. Reductions (sums) are also performed here. Appendix A provides more details.
  • VMEM (Vector Memory) is an on-chip scratchpad located in the TensorCore, close to the compute units. It is much smaller than HBM (for example, 128 MiB per chip on TPU7x vs 192 GiB of HBM) but has a much higher bandwidth to the MXU. VMEM operates somewhat like an L1/L2 cache on CPUs but is much larger and programmer-controlled. Data in HBM needs to be copied into VMEM before the TensorCore can do any computation with it.
  • VPU (向量处理单元) 执行通用的数学运算,如 ReLU 激活或向量之间的逐点加法或乘法. 归约 (求和) 也在这里执行. 附录 A 提供了更多详细信息.
  • VMEM (向量内存) 是位于 TensorCore 内部的片上暂存器,靠近计算单元. 它比 HBM 小得多 (例如,TPU7x 每块芯片有 128 MiB 的 VMEM 和 192 GiB 的 HBM), 但与 MXU 的带宽要高得多. VMEM 的操作有点像 CPU 上的 L1/L2 缓存,但要大得多并且由程序员控制. HBM 中的数据需要先复制到 VMEM 中,然后 TensorCore 才能对其进行任何计算.

TPUs are very, very fast at matrix multiplication. It’s mainly what they do and they do it well. TPU7x, one of the most powerful TPUs to date, can do 1.15e15 bf16 FLOPs / second / core or 2.3e15 bf16 FLOPs / sec / chip. A single pod of 9216 chips can do 21 bf16 exaFLOPs/s. That’s a lot. That’s one of the most powerful supercomputers in the world. And Google has a lot of them.1

TPU 的矩阵乘法速度非常快。 这是它的主要用途。TPU7x 是目前性能最高的 TPU 之一,每个核心可达到 1.15e15 bf16 FLOPs/s,每块芯片可达到 2.3e15 bf16 FLOPs/s。由 9216 块芯片组成的单个 pod 可达到 21 bf16 exaFLOPs/s,跻身世界上算力最强的超级计算机之列。Google 拥有许多这种 pod。1

The diagram above also includes a few other components like SMEM and the scalar unit, which are used for control flow handling and are discussed briefly in Appendix A, but aren’t crucial to understand. On the other hand, HBM is important and fairly simple:

上图还包括一些其他组件,如 SMEM 和标量单元,它们用于控制流处理,并在附录 A中简要讨论,但对于理解来说并不重要. 另一方面,HBM 很重要而且相当简单:

  • HBM (High Bandwidth Memory) is a big chunk of fast memory that stores tensors for use by the TensorCore. HBM usually has capacity on the order of tens to hundreds of gigabytes (for example, the HBM capacity of TPU7x is 192GiB).
  • HBM(高带宽内存)是用于存放 TensorCore 所需张量的大容量高速内存。HBM 容量通常在几十到几百 GB 之间。例如,TPU7x 的 HBM 容量为 192GiB。
  • When needed for a computation, tensors are streamed out of HBM through VMEM (see below) into the MXU and the result is written from VMEM back to HBM.
  • 当需要进行计算时,张量会从 HBM 通过 VMEM (见下文) 流式传输到 MXU, 结果会从 VMEM 写回 HBM.
  • The bandwidth between HBM and the TensorCore (through VMEM) is known as “HBM bandwidth” (around 7TB/sec for TPU7x) and limits how fast computation can be done in memory-bound workloads.
  • HBM 与 TensorCore 之间经由 VMEM 的带宽称为“HBM 带宽”,TPU7x 约为 7TB/秒。它决定了内存受限负载的计算速度上限。

Generally, all TPU operations are pipelined and overlapped. To perform a matmul X⋅A→YX \cdot A \to Y, a TPU would first need to copy chunks of matrices AA and XX from HBM into VMEM, then load them into the MXU which multiplies chunks of 8x256 (for XX) and 256x256 (for AA), then copy the result chunk by chunk back to HBM. To do this efficiently, the matmul is pipelined so the copies to/from VMEM are overlapped with the MXU work. This allows the MXU to continue working instead of waiting on memory transfers, keeping matmuls compute-bound, not memory-bound.

通常,所有 TPU 操作都是流水线化和重叠的. 为了执行矩阵乘法 X⋅A→YX \cdot A \to Y, TPU 首先需要将矩阵 AA 和 XX 的块从 HBM 复制到 VMEM, 然后将它们加载到 MXU 中,MXU 会乘以 8x256 (对于 XX) 和 128x256 (对于 AA) 的块,然后将结果逐块复制回 HBM. 为了高效地执行此操作,矩阵乘法是流水线化的,因此与 VMEM 的复制与 MXU 的工作重叠. 这使得 MXU 可以继续工作,而不是等待内存传输,从而使矩阵乘法受计算限制,而不是受内存限制.

Here’s an example of how you might perform an elementwise product from HBM:

这是一个如何从 HBM 执行逐元素乘积的示例:

pointwise product

Figure: an animation showing a pointwise product performed on TPU, with bytes loaded from HBM. Note how bytes are streamed out of memory in chunks and partial results are pipelined back without waiting for the full array to be materialized.

图: 一个动画,显示了在 TPU 上执行的逐点乘积,字节从 HBM 加载. 请注意字节如何以块的形式从内存中流出,部分结果如何流水线化地返回,而无需等待整个数组被物化.

A matmul would look nearly identical except it would load into the MXU instead of the VPU/Vector unit, and the loads and stores would occur in a different order, since the same weight chunk is used for multiple chunks of activations. You can see chunks of data streaming into VMEM, then into the VREGs (vector registers), then into the Vector Unit, then back into VMEM and HBM. As we’re about to see, if the load from HBM to VMEM is slower than the FLOPs in the Vector Unit (or MXU), we become “bandwidth bound” since we’re starving the VPU or MXU of work.

矩阵乘法看起来几乎完全相同,只是它会加载到 MXU 而不是 VPU/向量单元,并且加载和存储会以不同的顺序发生,因为相同的权重块会用于多个激活块. 你可以看到数据块流式传输到 VMEM, 然后到 VREGs (向量寄存器), 然后到向量单元,然后再回到 VMEM 和 HBM. 正如我们即将看到的,如果从 HBM 到 VMEM 的加载比向量单元 (或 MXU) 中的 FLOPs 慢,我们就会变得“受带宽限制”,因为我们正在让 VPU 或 MXU 缺乏工作.

VMEM and arithmetic intensity: VMEM is much smaller than HBM but it has a much higher bandwidth to the MXU. As we saw in Section 1, this means if an algorithm can fit all its inputs/outputs in VMEM, it’s much less likely to hit communication bottlenecks. This is particularly helpful when a computation has poor arithmetic intensity: VMEM bandwidth is ~7x higher than HBM bandwidth for TPU7x, which means an MXU operation reading from/writing to VMEM requires a ~7x lower arithmetic intensity than an operation using HBM to achieve peak FLOPs utilization. That means if we can fit our weights into VMEM instead of HBM, our matrix multiplications can be FLOPs bound at much smaller batch sizes. And it means algorithms that fundamentally have a lower arithmetic intensity can still be efficient. VMEM is just so small this is often a challenge.2

VMEM 与算术强度: VMEM 的容量远小于 HBM,但它与 MXU 之间的带宽高得多。第 1 节说明,如果算法的输入和输出都能放在 VMEM 中,遇到通信瓶颈的概率就会大幅降低。对于算术强度较低的计算,这一点尤其有用。TPU7x 的 VMEM 带宽约为 HBM 的 7 倍。MXU 直接读写 VMEM 时,达到峰值 FLOPs 利用率所需的算术强度约为读写 HBM 时的七分之一。若权重能放进 VMEM,矩阵乘法在更小的批次下就能进入计算受限区,算术强度较低的算法也能高效运行。难点在于 VMEM 容量很小。2

tpu bandwidth

A TPU chip typically (but not always) consists of two TPU cores which share memory and can be thought of as one large accelerator with twice the FLOPs (known as a “megacore” configuration). This is true for v4, v5, and v6 TPUs (TPU v7 removes megacore and instead has a high-bandwidth link between the two cores). Older TPU chips have separate memory and are regarded as two separate accelerators (TPU v3 and older). Inference-optimized chips like the TPU v5e only have one TPU core per chip.

一块 TPU 芯片通常包含两个共享内存的 TPU 核心,可视作具有两倍 FLOPs 的加速器,这种配置称为 megacore。TPU v4、v5、v6 采用这种配置。TPU v7 移除了 megacore,使用高带宽链路连接两个核心。TPU v3 及更早的芯片则为各核心配备独立的内存,可视作两块独立的加速器。TPU v5e 等面向推理的芯片每块只有一个 TPU 核心。

cores

Chips are arranged in sets of 4 on a ‘tray’ connected to a CPU host via PCIe network. This is the format most readers will be familiar with, 4 chips (8 cores on TPU7x since it has two TensorCores per chip, though some older generations combine these into 4 logical megacores) exposed through Colab or a single TPU-VM. For inference chips like the TPU v6e, we have 2 trays per host, instead of 1, but also only 1 core per chip, giving us 8 chips = 8 cores.3

芯片以 4 个一组装在托盘上,通过 PCIe 网络连接到 CPU 主机。大多数读者接触到的是 Colab 或单个 TPU-VM 中的四芯片配置。以 TPU7x 为例,每个芯片有两个 TensorCore,因此共有 8 个核心。一些较早的代际将这些核心合并为 4 个逻辑 megacore。对于 TPU v6e 等面向推理的芯片,每台主机配备 2 个托盘,每块芯片只有 1 个核心,因此 8 块芯片共有 8 个核心。3

pcie

PCIe bandwidth is limited: Like the HBM ↔\leftrightarrow VMEM link, the CPU ↔\leftrightarrow HBM PCIe connection has a specific bandwidth that limits how quickly you can load from host memory to HBM or vice-versa. PCIe bandwidth for TPU v4 is 16GB / second each way, for example, so close to 100x slower than HBM. We can load/offload data into the host (CPU) RAM, but not very quickly.

PCIe 带宽是有限的: 就像 HBM ↔\leftrightarrow VMEM 链路一样,CPU ↔\leftrightarrow HBM PCIe 连接具有特定的带宽,限制了你可以从主机内存加载到 HBM 或反之亦然的速度. 例如,TPU v4 的 PCIe 带宽为每个方向 16GB/秒,因此比 HBM 慢近 100 倍. 我们可以将数据加载/卸载到主机 (CPU) RAM 中,但速度不是很快.

TPU NetworkingTPU 网络

Chips are connected to each other through the ICI network in a Pod. In Trillium (TPU v6e), ICI (“inter-chip interconnects”) connects each chip to its 4 nearest neighbors (with edge links to form a 2D torus), which is typical for inference-optimized chips. On the other hand, each chip in TPU7x is connected to the nearest 6 neighbors (forming a 3D torus), providing more ICI bandwidth for training workloads. Note these connections do not go through their hosts, they are direct links between chips.

Pod 内的芯片通过 ICI 网络互连。 Trillium(TPU v6e)的 ICI(芯片间互连)将每块芯片与最近的 4 个邻居连接起来。边缘链路使网格构成二维环面,这是面向推理的芯片常用的拓扑。TPU7x 的每块芯片则连接最近的 6 个邻居,构成三维环面,为训练负载提供更多 ICI 带宽。这些都是芯片之间的直连链路,无需经过主机。

ici wraparound

The toroidal structure reduces the maximum distance between any two nodes from NN to N/2N / 2, making communication much faster. TPUs also have a “twisted torus” configuration that wraps the torus in a Mobius-strip like topology to further reduce the average distance between nodes.

环面结构将任意两个节点之间的最大距离从 NN 减少到 N/2N / 2, 使通信速度快得多. TPU 还有一个“扭曲环面”配置,它以类似莫比乌斯带的拓扑结构包裹环面,以进一步减少节点之间的平均距离.

TPU pods (connected by ICI) can get really big: the maximum pod size (called a superpod) for TPU7x consists of 9216 chips, composed of reconfigurable cubes of 4x4x4 chips4 connected by optical wraparound links that we can reconfigure to connect very large topologies.

经 ICI 连接的 TPU pod 可以扩展得很大。 TPU7x 的最大配置称为 superpod,包含 9216 块芯片。其基本单元是由 4x4x4 块芯片组成的可重构立方体。4立方体之间用光学环绕链路相连。重新配置这些链路可以形成大规模拓扑。

tpu rack

Smaller topologies (e.g. 2x2x1, 2x2x2) can also be requested, albeit with no wraparounds. This is an important caveat, since it typically doubles the time of most communication. Any multiple of a full cube (e.g. 4x4x4 or 4x4x8) will have wraparounds provided by the optical switches.5

也可以请求较小的拓扑 (例如 2x2x1, 2x2x2), 尽管没有环绕. 这是一个重要的警告,因为它通常会使大多数通信的时间加倍. 任何完整立方体的倍数 (例如 4x4x4 或 4x4x8) 都将具有由光学开关提供的环绕.5

subslices

TPU v5e and Trillium (TPU v6e) pods consist of a single 16x16 2D torus with wraparounds along any axis of size 16 (meaning an 8x16 has a wraparound on the long axis). For these generations, the topology cannot expand beyond a 16x16 torus but pods can still communicate with each other over standard data-center networking (DCN), which connects TPU hosts to each other. Again, smaller topologies can be requested without wraps on dims <16<16.

TPU v5e 和 Trillium(TPU v6e) pod 由单个 16x16 2D 环面组成,在任何大小为 16 的轴上都有环绕 (这意味着 8x16 在长轴上有环绕). TPU v5e 和 v6e (Trillium) 不能扩展到超过 16x16 的环面,但 pod 仍然可以通过标准的数据中心网络 (DCN) 相互通信,DCN 将 TPU 主机相互连接. 同样,可以请求较小的拓扑,在维度 <16<16 上没有环绕.

more subslices

This nearest-neighbor connectivity is a key difference between TPUs and GPUs. GPUs are connected with a hierarchy of switches that approximate a point-to-point connection between every GPU, rather than using local connections like a TPU. Typically, GPUs within a node (8 GPUs for H100 or as many as 72 for B200 NVL72) are directly connected, while larger topologies require O(log(N)) hops between each GPU. On the one hand, that means GPUs can send arbitrary data within a small number of hops. On the other hand, TPUs are dramatically cheaper (since NVLink switches are expensive), simpler to wire together, and can scale to much larger topologies because the number of links per device and the bandwidth per device is constant. Read more here.

这种最近邻连接是 TPU 与 GPU 的一个关键区别。 GPU 通过分层交换机实现近似任意两块 GPU 之间的点对点互连。节点内的 GPU 通常直接相连,例如 H100 配置中有 8 块 GPU,B200 NVL72 配置可达 72 块。规模更大的拓扑上,两块 GPU 之间的通信需要 O(log(N)) 跳。GPU 因此可以在很少的跳数内传输任意数据。TPU 省去了昂贵的 NVLink 交换机,成本大幅降低,布线也更简单。单台设备的链路数量和带宽恒定,因此能扩展到更大的拓扑。详见网络互连一节。

ICI is very fast relative to DCN, but is still slower than HBM bandwidth. For instance, a TPU7x has:

ICI 比 DCN 快得多,但仍低于 HBM 带宽。 以 TPU7x 为例:

  • 7.4e12 bytes/s (7380 GB/s) of HBM bandwidth per chip.
  • 1.2e12 bytes/s (1200 GB/s) of bidirectional ICI bandwidth per chip, or 4e11 bytes/s (400 GB/s) per axis.6
  • 1.25e10 bytes/s (12.5 GB/s) of DCN (egress) bandwidth per chip.
  • 每块芯片的 HBM 带宽为 7.4e12 字节/秒(7380 GB/s)。
  • 每块芯片的双向 ICI 带宽为 1.2e12 字节/秒(1200 GB/s),每条轴为 4e11 字节/秒(400 GB/s)。6
  • 每块芯片的 DCN 出口带宽为 1.25e10 字节/秒(12.5 GB/s)。

This means that when we split models across multiple chips, we need to be careful to avoid bottlenecking the MXU with slower cross-device communication.

这意味着当我们在多个芯片上拆分模型时,我们需要小心避免用较慢的跨设备通信来瓶颈 MXU.

Multi-slice training: A set of ICI-connected TPUs is called a slice. Different slices can be connected between each other using DCN, for instance to link slices on different pods. Since DCN is a much slower connection than ICI, we should try to limit how much our computation has to wait for data from DCN. DCN is host-to-host, so to transfer buffers from TPU to TPU over DCN, we first need to transfer over PCIe to the host, then egress over the network, then ingress over the target host network, then over PCIe into HBM.

多切片训练: 一组由 ICI 连接的 TPU 称为一个切片. 不同的切片可以使用 DCN 相互连接,例如连接不同 pod 上的切片. 由于 DCN 是比 ICI 慢得多的连接,因此应尽量限制我们的计算等待来自 DCN 的数据的时间. DCN 是主机到主机的,因此要通过 DCN 将缓冲区从 TPU 传输到 TPU, 我们首先需要通过 PCIe 传输到主机,然后通过网络出口,然后通过目标主机网络入口,然后通过 PCIe 进入 HBM.

Key Takeaways关键要点

  • TPUs are simple and can in most cases be thought of as a matrix multiply unit connected to memory (super fast), other chips over ICI (rather fast), and the rest of the datacenter over DCN (somewhat fast).
  • TPU 很简单,在大多数情况下可以被认为是一个连接到内存 (超快), 通过 ICI 连接到其他芯片 (相当快), 以及通过 DCN 连接到数据中心其余部分 (有点快) 的矩阵乘法单元.
  • Communication is limited by our various network bandwidths in order of speed:
    • HBM bandwidth: Between a TensorCore and its associated HBM.
    • ICI bandwidth: Between a TPU chip and its nearest 4 or 6 neighbors.
    • PCIe bandwidth: Between a CPU host and its associated tray(s) of chips.
    • DCN bandwidth: Between multiple CPU hosts, typically hosts not connected by ICI.
  • 通信受到我们各种网络带宽的限制,按速度排序:
    • HBM 带宽:在 TensorCore 和其关联的 HBM 之间.
    • ICI 带宽:在 TPU 芯片和其最近的 4 或 6 个邻居之间.
    • PCIe 带宽:在 CPU 主机和其关联的芯片托盘之间.
    • DCN 带宽:在多个 CPU 主机之间,通常是未通过 ICI 连接的主机.
  • Within a slice, TPUs are only connected to their nearest neighbors via ICI. This means communication over ICI between distant chips in a slice needs to hop over the intervening chips first.
  • 在一个切片内,TPU 仅通过 ICI 连接到其最近的邻居. 这意味着在一个切片中,远距离芯片之间的 ICI 通信需要先跳过中间的芯片.
  • Weight matrices need to be padded to size divisible by 256 (128 on generations prior to TPU v6e) in both dimensions to fill up the MXU (in fact, smaller axes are padded to 256).
  • 权重矩阵的两个维度均需填充到 256 的倍数(早于 TPU v6e 的型号是 128 的倍数),才能填满 MXU。较短的轴会填充至 256。
  • Lower precision matrix multiplication tends to be faster. TPU v6e can do int8/int4 matrix multiplications 2x/4x faster than bfloat16, and TPU7x can do fp8 matrix multiplications with 2x more FLOPs than bfloat16. VPU operations are still performed in fp32 or bf16 (though generations prior to TPU v6e only support fp32 VPU operations).
  • 低精度矩阵乘法通常更快。 TPU v6e 的 int8 和 int4 矩阵乘法速度分别为 bfloat16 的 2 倍和 4 倍。TPU7x 的 fp8 矩阵乘法运算吞吐量则是 bfloat16 的 2 倍。VPU 运算仍采用 fp32 或 bf16,早于 TPU v6e 的型号仅支持 fp32 VPU 运算。
  • To avoid bottlenecking the TPU compute unit, we need to make sure the amount of communication across each channel is proportional to its speed.
  • 为了避免瓶颈 TPU 计算单元,我们需要确保每个通道的通信量与其速度成正比.

TPU specsTPU 规格

Here are some specific numbers for our chips:

以下是我们芯片的一些具体数字:

Model
型号
Pod size
Pod 大小
Host size
主机大小
HBM capacity/chip
HBM 容量/芯片
HBM BW/chip (bytes/s)
HBM 带宽/芯片 (字节/秒)
FLOPs/s/chip (bf16)
FLOPs/s/芯片 (bf16)
FLOPs/s/chip (int8)
FLOPs/s/芯片 (int8)
TPU v332x324x232GiB9.0e111.4e141.4e14
TPU v4p16x16x162x2x132GiB1.2e122.75e142.75e14
TPU v5p16x20x282x2x196GiB2.8e124.59e149.18e14
TPU v5e16x164x216GiB8.2e111.97e143.94e14
TPU v6e16x164x232GiB1.6e129.20e141.84e15
TPU7x4x4x5762x2x1192GiB7.4e122.31e154.61e15 (fp8)

Host size refers to the topology of TPUs connected to a single host (e.g. TPU v5e has a single CPU host connected to 8 TPUs in a 4x2 topology). See the TPU7x documentation for more details on the latest generation. Here are interconnect figures:

主机大小指连接到一台主机的 TPU 拓扑。例如,TPU v5e 的一台 CPU 主机连接 8 块 TPU,拓扑为 4x2。最新一代芯片的详细规格见 TPU7x 文档。以下是互连数据:

Model
型号
ICI BW/link (one-way, bytes/s)
ICI 带宽/链路 (单向,字节/秒)
ICI BW/link (bidi, bytes/s)
ICI 带宽/链路 (双向,字节/秒)
TPU v31.0e112.0e11
TPU v4p4.5e109.0e10
TPU v5p9.0e101.8e11
TPU v5e4.5e109.0e10
TPU v6e9.0e101.8e11
TPU7x9.0e101.8e11

We include both one-way (unidirectional) bandwidth and bidi (bidirectional) bandwidth since unidirectional bandwidth is more true to the hardware but bidirectional bandwidth occurs more often in equations involving a full ring.7

我们同时包含了单向 (unidirectional) 带宽和双向 (bidirectional) 带宽,因为单向带宽更符合硬件实际情况,但双向带宽在涉及完整环形的方程中更常出现.7

PCIe bandwidth is typically around 1.6e10 bytes / second per TPU (3.2e10 for TPU v6e), while DCN bandwidth is typically around 6.25e9 bytes / second per TPU (12.5e9 for TPU v6e and TPU7x, and 3.125e9 for TPU v5e).

PCIe 带宽通常约为每个 TPU 1.6e10 字节/秒 (3.2e10 用于 TPU v6e), 而 DCN 带宽通常约为每个 TPU 6.25e9 字节/秒 (12.5e9 用于 TPU v6e 和 TPU7x, 3.125e9 用于 TPU v5e).

Worked Problems已解决的问题

These numbers are a little dry, but they let you make basic roofline estimates for model performance. Let’s work a few problems to explain why this is useful. You’ll see more examples in Part 3.

这些数字有点枯燥,但它们可以让你对模型性能进行基本的屋顶线估计. 让我们解决几个问题来解释为什么这很有用. 你将在第 3 部分看到更多例子.

Question 1 [bounding LLM latency]: Say you want to sample from a 200B parameter model in bf16 that’s split across 32 TPU v6e chips. How long would it take to load all the parameters from HBM into the systolic array? Hint: use the numbers above.

问题 1 [估算 LLM 延迟下限]: 假设你想从一个分布在 32 个 TPU v6e 上的 200B 参数的 bf16 模型中进行采样. 将所有参数从 HBM 加载到脉动阵列需要多长时间?提示:使用上面的数字.

Click here for the answer. 点击查看答案

Answer: We’re loading sizeof(bf16) * 200e9 = 400e9 bytes on 32 chips, meaning 12.5e9 bytes / chip, each with an HBM bandwidth of 1.6e12. So the load takes around 7.8ms.

答案: 将 sizeof(bf16) * 200e9 = 400e9 字节分摊到 32 块芯片,每块芯片要加载 12.5e9 字节。每块芯片的 HBM 带宽为 1.6e12 字节/秒,加载耗时约 7.8 毫秒。

That’s pretty cool, because that’s a reasonable lower bound on the latency of sampling from the model. Each sampling step needs to load all parameters from HBM, so it cannot take less than 7.8 ms. In practice, at small batch sizes, this is close to being achievable.

7.8 毫秒可作为模型采样延迟的合理下限。 每个采样步骤都要从 HBM 加载全部参数,耗时至少 7.8 毫秒。实际运行时,较小的批次可以接近这一估算值。

Question 2 [TPU details]: Consider a full TPU v5e pod. How many total CPU hosts are there? How many TPU TensorCores? What is the total FLOPs/s for the whole pod? What is the total HBM? Do the same exercise for TPU v5p pod.

问题 2 [TPU 细节]: 考虑一个完整的 TPU v5e pod. 总共有多少个 CPU 主机?多少个 TPU TensorCore? 整个 pod 的总 FLOPs/s 是多少?总 HBM 是多少?对 TPU v5p pod 做同样的练习.

Click here for the answer. 点击查看答案

Answer: For TPU v5e, each pod is 16x16 and each host is a 4x2 slice, so we have 16*16 / 8 = 32 hosts. For TPU v5e, each TPU has only one core, so we have 256 TensorCores. The total FLOPs/s is 16*16*2e14 = 5.1e16 in bfloat16. Each chip has 16GiB of HBM, so that’s 256 * 16 = 4TiB of memory.

答案: 对于 TPU v5e, 每个 pod 是 16x16, 每个主机是一个 4x2 的切片,所以我们有 16*16 / 8 = 32 个主机. 对于 TPU v5e, 每个 TPU 只有一个核心,所以我们有 256 个 TensorCore. 在 bfloat16 中,总 FLOPs/s 是 16*16*2e14 = 5.1e16. 每个芯片有 16GiB 的 HBM, 所以总共有 256 * 16 = 4TiB 的内存.

For a full TPU v5p pod, we have 16x20x28 chips and each host is 2x2x1, so we have (16*20*28) / (2*2) = 2,240 hosts. For TPU v5p, each TPU has two TensorCores, so we have 8960 * 2 = 17,920 cores. The total FLOPs/s is 8960 * 4.59e14 = 4.1e18 in bfloat16. Each chip has 96GiB of HBM, so that’s 8960 * 96 = 840TiB of memory.

对于一个完整的 TPU v5p pod, 我们有 16x20x28 个芯片,每个主机是 2x2x1, 所以我们有 (16*20*28) / (2*2) = 2,240 个主机. 对于 TPU v5p, 每个 TPU 有两个 TensorCore, 所以我们有 8960 * 2 = 17,920 个核心. 在 bfloat16 中,总 FLOPs/s 是 8960 * 4.59e14 = 4.1e18. 每个芯片有 96GiB 的 HBM, 所以总共有 8960 * 96 = 840TiB 的内存.

Question 3 [PCIe operational intensity]: Imagine we’re forced to store a big weight matrix AA of type bf16[D,F]\text{bf16}[D, F], and a batch of activations xx of type bf16[B,D]\text{bf16}[B, D] in host DRAM and want to do a matrix multiplication on them. This is running on a single host, and we’re using a single TPU v6e chip attached to it. You can assume B≪DB \ll D, and F=4DF = 4D (we’ll see in future chapters why these are reasonable assumptions). What is the smallest batch size BB we need to remain FLOPs bound over PCIe? Assume PCIe bandwidth of 1.6e10 bytes / second.

问题 3 [PCIe 操作强度]: 想象一下,我们被迫将一个大的权重矩阵 AA (类型为 bf16[D,F]\text{bf16}[D, F]) 和一批激活 xx (类型为 bf16[B,D]\text{bf16}[B, D]) 存储在主机 DRAM 中,并希望对它们进行矩阵乘法. 这在单个主机上运行,我们使用一个连接到它的单个 TPU v6e 芯片. 你可以假设 B≪DB \ll D, 并且 F=4DF = 4D (我们将在后面的章节中看到为什么这些是合理的假设). 我们需要保持受 FLOPs 限制的最小批次大小 BB 是多少?假设 PCIe 带宽为 1.6e10 字节/秒.

Click here for the answer. 点击查看答案

Answer: We have to perform 2BDF2BDF floating point operations, and each chip can perform 9.2e14 floating point operations per second. This then requires 2BDF/9.2e142BDF / 9.2e14 seconds to perform. We have to load 2DF+2BD2DF + 2BD bytes from DRAM, and write 2BF2BF bytes back to it. We are bottlenecked by PCIe transfer speeds, so we need 2⋅(BD+DF+BF)/1.6e102 \cdot (BD + DF + BF) / 1.6e10 seconds to transfer data to and from the TPU. Since we want computation to take longer than weight loading, assuming we can overlap all weight loading with computation, we want 2BDF/9.2e14>2⋅(BD+DF+BF)/1.6e102BDF / 9.2e14 > 2 \cdot (BD + DF + BF) / 1.6e10. We can simplify this using our assumptions that B≪DB \ll D, and F=4DF = 4D, to get

答案: 我们需要执行 2BDF2BDF 次浮点运算,每个芯片每秒可以执行 9.2e14 次浮点运算. 这需要 2BDF/9.2e142BDF / 9.2e14 秒来执行. 我们需要从 DRAM 加载 2DF+2BD2DF + 2BD 字节,并将 2BF2BF 字节写回. 我们受到 PCIe 传输速度的瓶颈,所以我们需要 2⋅(BD+DF+BF)/1.6e102 \cdot (BD + DF + BF) / 1.6e10 秒来将数据传入和传出 TPU. 因为我们希望计算时间比权重加载时间长,假设我们可以将所有权重加载与计算重叠,我们希望 2BDF/9.2e14>2⋅(BD+DF+BF)/1.6e102BDF / 9.2e14 > 2 \cdot (BD + DF + BF) / 1.6e10. 我们可以使用我们的假设 B≪DB \ll D 和 F=4DF = 4D 来简化这个式子,得到

8BD29.2×1014>8D21.6×1010\frac{8BD^2}{9.2 \times 10^{14}} > \frac{8D^2}{1.6 \times 10^{10}}

or

或

B>9.2×10141.6×1010≃57,500B > \frac{9.2 \times 10^{14}}{1.6 \times 10^{10}} \simeq 57{,}500

Question 4 [general matmul latency]: Let’s say we want to multiply a weight matrix int8[16384, 4096] by an activation matrix of size int8[B, 4096] where B is some unknown batch size. Let’s say we’re on 1 TPU v5e to start.

问题 4 [通用矩阵乘法延迟]: 假设我们想将一个权重矩阵 int8[16384, 4096] 与一个大小为 int8[B, 4096] 的激活矩阵相乘,其中 B 是某个未知的批次大小. 假设我们从 1 个 TPUv5e 开始.

  1. How long will this multiplication take as a function of B? Hint: it may help to calculate how long it will take to load the arrays from HBM and how long the multiplication will actually take. Which is bottlenecking you?
  2. What if we wanted to run this operation out of VMEM? How long would it take as a function of B?
  1. 这个乘法需要多长时间,作为 B 的函数?提示:计算从 HBM 加载数组需要多长时间以及实际乘法需要多长时间可能会有帮助. 哪个是你的瓶颈?
  2. 如果我们想在 VMEM 中运行这个操作呢?作为 B 的函数,它需要多长时间?
Click here for the answer. 点击查看答案

Answer: (1) The number of operations we need to perform is 2⋅4096⋅16384⋅B=1.3×108⋅B2 \cdot 4096 \cdot 16384 \cdot B = 1.3 \times 10^{8} \cdot B. So Tmath=(1.3×108⋅B)/3.94×1014T_{\text{math}} = (1.3 \times 10^{8} \cdot B) / 3.94 \times 10^{14} seconds. We need to load 16384⋅4096+4096⋅B16384 \cdot 4096 + 4096 \cdot B bytes from HBM to VMEM, and write back 16384⋅B16384 \cdot B bytes from VMEM to HBM. This means Tcomms=(6.7×107+2×104⋅B)/8.2×1011T_{\text{comms}} = (6.7 \times 10^{7} + 2 \times 10^{4} \cdot B) / 8.2 \times 10^{11} seconds. Assuming as much overlap of communication and computation as possible, the whole multiplication will take approximately

答案: (1) 我们需要执行的浮点运算次数是 2⋅4096⋅16384⋅B=1.3×108⋅B2 \cdot 4096 \cdot 16384 \cdot B = 1.3 \times 10^{8} \cdot B. 所以 Tmath=(1.3×108⋅B)/3.94×1014T_{\text{math}} = (1.3 \times 10^{8} \cdot B) / 3.94 \times 10^{14} 秒. 我们需要从 HBM 加载 16384⋅4096+4096⋅B16384 \cdot 4096 + 4096 \cdot B 字节到 VMEM, 并将 16384⋅B16384 \cdot B 字节从 VMEM 写回 HBM. 这意味着 Tcomms=(6.7×107+2×104⋅B)/8.2×1011T_{\text{comms}} = (6.7 \times 10^{7} + 2 \times 10^{4} \cdot B) / 8.2 \times 10^{11} 秒. 假设通信和计算尽可能多地重叠,整个乘法大约需要

max⁡{Tmath,Tcomms}=max⁡{1.3×108⋅B3.94×1014,6.7×107+2×104⋅B8.2×1011}\max\{T_{\text{math}}, T_{\text{comms}}\} = \max\left\{\frac{1.3 \times 10^{8} \cdot B}{3.94 \times 10^{14}}, \frac{6.7 \times 10^{7} + 2 \times 10^{4} \cdot B}{8.2 \times 10^{11}}\right\}

We’ll be FLOPs-bound when 6.7×107+2×104⋅B8.2×1011<1.3×108⋅B3.94×1014\frac{6.7 \times 10^{7} + 2 \times 10^{4} \cdot B}{8.2 \times 10^{11}} < \frac{1.3 \times 10^{8} \cdot B}{3.94 \times 10^{14}}, or equivalently, B>267B > 267. This is slightly larger than the 240 number we derive in Section 1 because we factor in the full impact of DD and FF.

当 6.7×107+2×104⋅B8.2×1011<1.3×108⋅B3.94×1014\frac{6.7 \times 10^{7} + 2 \times 10^{4} \cdot B}{8.2 \times 10^{11}} < \frac{1.3 \times 10^{8} \cdot B}{3.94 \times 10^{14}},即 B>267B > 267 时,计算成为瓶颈。这个阈值略高于第 1 节推导出的 240,因为这里完整计入了 DD 和 FF 的影响。

(2) If instead we are loading from VMEM, let’s consider VMEM bandwidth to the MXU as 22 times the HBM ↔\leftrightarrow VMEM bandwidth. This turns our data loading denominator from 8.2e11 to 1.80e13, and we get B>11B > 11. Note that in practice, we cannot dedicate all of our VMEM bandwidth to loading the weight matrix, so in practice it will be closer to 20.

(2) 如果我们改为从 VMEM 加载,让我们将 VMEM 到 MXU 的带宽视为 HBM ↔\leftrightarrow VMEM 带宽的 22 倍. 这将我们的数据加载分母从 8.2e11 变为 1.80e13, 我们得到 B>11B > 11. 请注意,在实践中,我们不能将所有 VMEM 带宽都用于加载权重矩阵,因此在实践中它会更接近 20.

Question 5 [ICI bandwidth]: Let’s say we have a TPU v5e 4x4 slice. Let’s say we want to send an array of type bf16[8, 128, 8192] from TPU{0,0} to TPU{3, 3}. Let’s say the per-hop latency for TPU v5e is 1μs1\mu s.

问题 5 [ICI 带宽]: 假设我们有一个 TPU v5e 4x4 切片. 假设我们想将一个类型为 bf16[8, 128, 8192] 的数组从 TPU{0,0} 发送到 TPU{3, 3}. 假设 TPU v5e 的每跳延迟为 1μs1\mu s.

  1. How soon will the first byte arrive at its destination?
  2. How long will the total transfer take?
  1. 第一个字节到达目的地需要多长时间?
  2. 总传输需要多长时间?
Click here for the answer. 点击查看答案

Answer: In a TPU v5e we have 2D connectivity. Because we have only a 4x4 slice (with no axes of size 16), we have no wraparound connections. Thus there are two ports from which our target chip can receive data, and likewise two ports from which our source chip can send data. The amount of data we have to transfer is 2 * 8 * 128 * 8192 = 1.7e7 bytes. We can transfer from both ports simultaneously (i.e. send half the array right and half down), so we get 2 * 4.5e10 = 9e10 bytes transferred per second, which means it’ll take about 1.7e7 / 9e10 = 188us to transfer the whole array through (assuming we’re bandwidth bound). In a 4x4 slice, we have six hops between chips (0,0)(0, 0) and (3,3)(3, 3), since there are no wraparound links for axes with fewer than 16 chips. Since the latency of each hop is about 1μs1\mu s, the first byte will arrive in about 6us and the total transfer will take about 188 + 6 = 194us, since the last byte must likewise traverse the six hops after it leaves the source (in general, the latency and bandwidth terms are additive, though here the latency is a small correction).

答案: 在 TPUv5e 中,我们有 2D 连接. 因为我们只有一个 4x4 切片 (没有大小为 16 的轴), 所以我们没有环绕连接. 因此,我们的目标芯片可以从两个端口接收数据,同样,我们的源芯片可以从两个端口发送数据. 我们需要传输的数据量是 2 * 8 * 128 * 8192 = 1.7e7 字节. 我们可以同时从两个端口传输 (即,将一半数组向右发送,一半向下发送), 所以我们每秒传输 2 * 4.5e10 = 9e10 字节,这意味着传输整个数组大约需要 1.7e7 / 9e10 = 188us (假设我们受带宽限制). 在一个 4x4 切片中,芯片 (0,0)(0, 0) 和 (3,3)(3, 3) 之间有六次跳,因为轴上少于 16 个芯片时没有环绕链路. 由于每次跳的延迟约为 1μs1\mu s, 第一个字节将在大约 6us 内到达,总传输耗时约为 188 + 6 = 194us,因为最后一个字节离开源芯片后同样需要经过六跳。通常要把延迟和带宽对应的耗时相加。本例中,延迟项只占很小的比例。

Question 6 [pulling it all together, hard]: Imagine you have a big matrix A: int8[128 * 1024, 128 * 1024] sharded evenly across a TPU v5e 4x4 slice but offloaded to host DRAM on each chip. Let’s say you want to copy the entire array to TPU{0, 0} and multiply it by a vector bf16[8, 128 * 1024]. How long will this take? Hint: use the numbers above.

问题 6 [综合题,难]: 想象你有一个大矩阵 A: int8[128 * 1024, 128 * 1024], 它均匀地分片在一个 TPU v5e 4x4 切片上,但卸载到每个芯片的主机 DRAM 中. 假设你想将整个数组复制到 TPU{0, 0} 并将其与一个向量 bf16[8, 128 * 1024] 相乘. 这需要多长时间?提示:使用上面的数字.

Click here for the answer. 点击查看答案

Answer: Let’s start by outlining the operations we have to perform. Our array is about 16GB. From the table above, a TPU v5e host has a 4x2 topology, so a 4x4 has 2 hosts. Thus, since our array is evenly sharded, each host effectively contains a chunk of 1/2 of the array, or 8GB. We need to copy these chunks all to TPU{0,0}, which gives us two options:

答案: 让我们从概述我们需要执行的操作开始. 我们的数组大约是 16GB. 从上表中,一个 TPU v5e 主机有一个 4x2 的拓扑,所以一个 4x4 有 2 个主机. 因此,由于我们的数组是均匀分片的,每个主机实际上包含数组的 1/2, 或 8GB. 我们需要将这些块全部复制到 TPU{0,0}, 这给了我们两个选择:

  1. We can copy over DCN and then load the entire unsharded array over PCIe into HBM.
  2. We can load our sharded arrays onto their corresponding TPUs, then perform a gather over ICI, then perform the matmul on TPU{0,0}.
  1. 我们可以通过 DCN 复制,然后通过 PCIe 将整个未分片的数组加载到 HBM 中.
  2. 我们可以将我们分片的数组加载到它们相应的 TPU 上,然后通过 ICI 执行一个 gather, 然后在 TPU{0,0} 上执行矩阵乘法.

It should be clear that option (2) is better. DCN is slow compared to ICI and we’d much prefer to load a big array over many PCIe links rather than just a few (the 8 on host 0). Here’s a diagram of part of the system. As described above, note that TPUs are connected to their neighbors by ICI (even across hosts), all TPUs are connected to their host CPU (via PCIe), and hosts are connected by DCN.

很明显,选项 (2) 更好. DCN 与 ICI 相比速度较慢,我们更愿意通过许多 PCIe 链路加载一个大数组,而不是仅仅几个 (主机 0 上的 8 个). 这是系统部分的一个图表. 如上所述,请注意 TPU 通过 ICI 连接到它们的邻居 (即使跨主机), 所有 TPU 都通过 PCIe 连接到它们的主机 CPU, 主机通过 DCN 连接.

challenge problem

Each chip actually has its own PCIe link to its host, though for clarity only one is shown here.

每个芯片实际上都有自己的 PCIe 链路到其主机,虽然为了清晰起见,这里只显示了一个.

Now let’s work through how long each piece will take:

现在让我们来看看每个部分需要多长时间:

  1. PCIe load: we’re loading chunks of 16GB over 16 PCIe links, each of which has 1.6e10 bytes/second bandwidth. Thus this will take about 63ms.
  1. 经 PCIe 加载: 16 GB 的数据分块通过 16 条 PCIe 链路加载,每条链路的带宽为 1.6e10 字节/秒,耗时约 63 毫秒。
  1. ICI copy: each TPU now has 16GB / 16 = 1GB of our array. Our ICI bandwidth is 9e10 bytes/second per link bidirectional, and you’ll notice from the above diagram that only 2 of the 4 ICI links on the TPU v5e are in use in this topology for TPU{0,0}. Since TPU{0,0} needs to receive a total of 15GB along 2 axes at 4.5e10 bytes/s/link, we can lower bound the time by 15e9 / (4.5e10 * 2) = 167ms. In practice this probably isn’t achievable because the load is very uneven, but it’s probably within a factor of 2. As you’ll see in Section 3, performing a full AllGather would also take roughly 16e9 / (4.5e10 * 2), so this is close to optimal.
  1. ICI 复制: 每个 TPU 现在有 16GB / 16 = 1GB 的数组. 我们的 ICI 带宽是每个链路 双向 9e10 字节/秒,你会从上图中注意到,在这个拓扑中,TPU v5e 上的 4 个 ICI 链路中只有 2 个在 TPU{0,0} 上使用. 由于 TPU{0,0} 需要沿着 2 个轴以 4.5e10 字节/秒/链路的速度接收总共 15GB, 我们可以通过 15e9 / (4.5e10 * 2) = 167ms 来为时间设定下限. 在实践中,这可能无法实现,因为负载非常不均匀,但可能在 2 倍的范围内. 正如你将在第 3 节中看到的,执行一个完整的 AllGather 也大约需要 16e9 / (4.5e10 * 2), 所以这接近最优.
  1. HBM →\rightarrow MXU load: to perform our final matmul, we need to load these 16e9 bytes plus the bf16[8, 128 * 1024] array (another 2MB, so negligible) over HBM bandwidth into the MXU, which will take 16e9 / 8.2e11 = 20ms.
  1. HBM →\rightarrow MXU 加载: 为了执行我们最后的矩阵乘法,我们需要将这 16e9 字节加上 bf16[8, 128 * 1024] 数组 (另外 2MB, 所以可以忽略不计) 通过 HBM 带宽加载到 MXU 中,这将需要 16e9 / 8.2e11 = 20ms.
  1. FLOPs: we’re performing a total of 2⋅8⋅128⋅1024⋅128⋅1024=2.7×10112 \cdot 8 \cdot 128 \cdot 1024 \cdot 128 \cdot 1024 = 2.7 \times 10^{11} FLOPs, and since we can perform 1.97e14 bf16 FLOPs/s, we get 1.4ms.
  1. FLOPs: 我们总共执行 2⋅8⋅128⋅1024⋅128⋅1024=2.7×10112 \cdot 8 \cdot 128 \cdot 1024 \cdot 128 \cdot 1024 = 2.7 \times 10^{11} FLOPs, 由于我们可以执行 1.97e14 bf16 FLOPs/s, 我们得到 1.4ms.

An upper bound for the total time is the sum of all of these times, but since the TPU can typically overlap these operations, we can think of this as a pipelining problem that’s bottlenecked by the slowest piece. Assuming that’s true, then the answer is at least 167ms, likely closer to 200ms with imperfect overlapping.

总耗时的上限是上述各项耗时之和。但 TPU 通常能重叠执行这些操作,因此可以把它们视为一条由最慢环节决定吞吐量的流水线。在这一假设下,总耗时至少为 167 毫秒。各操作无法完全重叠,实际耗时可能更接近 200 毫秒。

That’s it for Part 2! For Part 3, covering partitioning and cross-TPU communication, click here.

第二部分到此结束!第 3 部分介绍分片和跨 TPU 通信。

Appendix附录

Appendix A: More on TPU internals附录 A: 关于 TPU 内部的更多信息

Here we’ll dive more deeply into the internal operations of a TPU. Unless otherwise noted, we’ll provide specs for a TPU7x.

在这里,我们将更深入地探讨 TPU 的内部操作. 除非另有说明,我们将提供 TPU7x 的规格.

VPUVPU

The VPU is the TPU’s vector arithmetic core. The VPU consists of a two dimensional SIMD vector machine (the VPU) that performs elementwise arithmetic operations like vadd (vector addition) or vmax (elementwise max) and a set of vector registers called VREGs that hold data for the VPU and MXU.

VPU 是 TPU 的向量算术核心. VPU 由一个二维 SIMD 向量机 (VPU) 组成,该机器执行逐元素算术运算,如 vadd (向量加法) 或 vmax (逐元素最大值), 以及一组称为 VREGs 的向量寄存器,用于为 VPU 和 MXU 保存数据.

VREGs: Each TPU7x core has 64 32-bit VREGs (32 in TPU v4), giving us a total of about 64 * 8 * 128 * 4 = 256kB of VREG memory per core (or 2x this for the whole chip since we have two cores). A TPU7x can load 3 registers from VMEM each cycle and write 2 registers to VMEM each cycle, but is limited to 4 total reads/writes, so either 3 reads + 1 write or 2 reads + 2 writes.

VREG: TPU7x 每个核心有 64 个 32 位向量寄存器 VREG(TPU v4 为 32 个)。每个核心的 VREG 容量约为 64 * 8 * 128 * 4 = 256kB,双核芯片的总容量是这个值的两倍。TPU7x 每个周期最多从 VMEM 读取 3 个寄存器,并向 VMEM 写入 2 个寄存器,但每周期最多共执行 4 次读写,可完成 3 次读加 1 次写,或 2 次读加 2 次写。

For programs operating on float32 inputs, each 32-bit word of a VREG contains a single float32 element. When working with lower precision datatypes, multiple elements can be packed into a single 32-bit word to more efficiently use VMEM and VREGs, and some operations expect its operands to be packed in this way.

程序使用 float32 输入时,VREG 中的每个 32 位字包含一个 float32 元素。使用精度更低的数据类型时,可以将多个元素打包到一个 32 位字中,以提高 VMEM 和 VREG 的利用效率。有些运算要求操作数采用这种打包形式。

VPU: The VPU is a 2D vector arithmetic unit of shape (8, 128) where the 128 dimension is referred to as lane axis and the dimension of 8 is referred to as the sublane axis. Each (lane, sublane) pair on TPU7x contains 4 standard floating-point ALUs which are independent of each other. The VPU executes most arithmetic instructions in one cycle in each of its ALUs (like vadd or vector add) with a latency of 2 cycles, so e.g. in TPU7x you can add 4 pairs of f32 values together from VREGs in each cycle. A typical VPU instruction might look like {v2 = vadd.8x128.f32 v0, v1} where v0 and v1 are input VREGs and v2 is an output VREG. ALUs for all TPU generations support a set of elementwise integer, bitwise, and logical operations as well, and TPU v6e and TPU7x additionally can perform elementwise arithmetic operations on packed bfloat16 VREGs.

VPU: VPU 是一个形状为 (8, 128) 的二维向量算术单元,其中 128 维度被称为通道轴,8 维度被称为子通道轴. TPU7x 上的每个 (通道,子通道) 对包含 4 个相互独立的标准浮点 ALU. VPU 在其每个 ALU 中以一个周期执行大多数算术指令 (如 vadd 或向量加法), 延迟为 2 个周期,因此例如在 TPU7x 上,你可以在每个周期内将 4 对来自 VREGs 的 f32 值相加. 一个典型的 VPU 指令可能看起来像 {v2 = vadd.8x128.f32 v0, v1}, 其中 v0 和 v1 是输入 VREGs, v2 是输出 VREG. 各代 TPU 的 ALU 都支持一组逐元素整数、按位和逻辑操作。TPU v6e 和 TPU7x 还可对打包在 VREG 中的 bfloat16 数据执行逐元素算术运算。

All lanes and sublanes execute the same program every cycle in a pure SIMD manner, but each ALU can perform a different operation. So we can e.g. process 1 vadd and 1 vsub in a single cycle, each of which operates on two full VREGs and writes the output to a third.

所有通道和子通道都以纯 SIMD 方式每个周期执行相同的程序,但每个 ALU 可以执行不同的操作. 所以我们可以例如在一个周期内处理 1 个 vadd 和 1 个 vsub, 每个都对两个完整的 VREGs 进行操作,并将输出写入第三个.

Pop Quiz [Calculating VPU throughput]: Using the above information, calculate how many vector FLOPs/s a TPU7x can perform. A TPU7x has a clock speed of about 2.2GHz.

小测验[计算 VPU 吞吐量]: 用上述信息计算 TPU7x 每秒可执行的向量浮点运算次数。TPU7x 的时钟频率约为 2.2GHz。

Click here for the answer. 点击查看答案

Answer: Each cycle, each core can execute 4 vector instructions on 8 * 128 ALUs. This gives us 8 * 128 * 4 FLOPs/cycle per core, or 8 * 128 * 4 * 2.2e9 = 9e12 FLOPs/s (1.8e13 bfloat16 FLOPs/s since packed bfloat16 VREGs contain twice as many elements as float32 VREGs). Note how much smaller this is than the MXU FLOPs/s of about 1.2e15 per core (roughly 64x for bfloat16).

答案: 每个核心每周期可在 8 * 128 个 ALU 上执行 4 条向量指令,得到每个核心每周期 8 * 128 * 4 FLOPs,或 8 * 128 * 4 * 2.2e9 = 9e12 FLOPs/s。打包的 bfloat16 VREG 中的元素是 float32 VREG 的两倍,因此 bfloat16 吞吐量为 1.8e13 FLOPs/s。MXU 的吞吐量约为每核 1.2e15 FLOPs/s,按 bfloat16 计算大约是 VPU 的 64 倍。

Reductions: Generally, communication or reduction across the sublane dimension is easier than across the lane dimension. For instance, the VPU supports an intra-lane shuffle operation that can roll along the axis of size 8 in about a cycle. This can be used to perform efficient reductions along the sublane dimension (just shuffle by 4, 2, and 1 and do 3 pairs of elementwise sums).

归约: 通常,跨子通道维度的通信或归约比跨通道维度的更容易. 例如,VPU 支持一个通道内 shuffle 操作,可以在大约一个周期内沿着大小为 8 的轴滚动. 这可以用于在子通道维度上执行高效的归约 (只需 shuffle 4, 2 和 1, 并进行 3 对逐元素求和).

Cross-lane reductions are much harder and involve a separate hardware unit called the XLU or “cross lane unit”, which is slow and fairly expensive.

跨通道归约要困难得多,并且涉及一个称为 XLU 或“跨通道单元”的独立硬件单元,它速度慢且相当昂贵.

Comparison to GPUs: For those familiar with NVIDIA GPUs, each ALU in the VPU is analogous to a CUDA core, and a single VPU lane is analogous to a “Warp Scheduler”, i.e. the set of usually 32 CUDA Cores that perform SIMD arithmetic. Reductions within the lane are pretty easy, but if we need to cross lanes, we need to transit at least VMEM/XLU/SMEM which is much slower. See the GPU section for more details.

与 GPU 的比较: 对于熟悉 NVIDIA GPU 的人来说,VPU 中的每个 ALU 都类似于一个 CUDA 核心,一个 VPU 通道类似于一个“Warp Scheduler”,即通常执行 SIMD 算术的 32 个 CUDA 核心的集合. 通道内的归约非常容易,但如果我们需要跨通道,我们需要至少通过 VMEM/XLU/SMEM, 这要慢得多. 有关更多详细信息,请参见 GPU 部分.

Scalar Core标量核心

The scalar core is the control unit of the TPU. It fetches and dispatches all instructions and executes transfers from HBM into VMEM, and can be programmed to do scalar metadata work. Because the scalar core is single-threaded, one side-effect of this is that each core of the TPU is only capable of creating one DMA request per cycle.

标量核心是 TPU 的控制单元. 它获取并分派所有指令,并执行从 HBM 到 VMEM 的传输,并且可以被编程为执行标量元数据工作. 因为标量核心是单线程的,这的一个副作用是 TPU 的每个核心每个周期只能创建一个 DMA 请求.

To put this in context, a single scalar core controls a VPU (consisting of 4096 ALUs), 2 MXUs, 2 XLUs, and multiple DMA engines. The highly skewed nature of control per unit compute is a source of hardware efficiency, but also limits the ability to do data dependent vectorization in any interesting way.

为了说明这一点,一个标量核心控制一个 VPU (由 4096 个 ALU 组成), 2 个 MXU, 2 个 XLU 和多个 DMA 引擎. 每个计算单元的控制高度倾斜是硬件效率的来源,但也限制了以任何有趣的方式进行数据相关向量化的能力.

Appendix B: How does a systolic array work?附录 B: 脉动阵列是如何工作的?

At the core of the TPU MXU is a 256x256 systolic array (128x128 prior to TPU v6e). When fully saturated the systolic array can perform one bf16[8,256] @ bf16[256,256] -> f32[8,256]8 multiplication per 4 clock cycles9 (8 prior to TPUv6e).

TPU 的 MXU 以 256x256 脉动阵列为核心(早于 TPU v6e 的型号是 128x128)。满负荷时,脉动阵列每 4 个时钟周期即可执行一次 bf16[8,256] @ bf16[256,256] -> f32[8,256]8 矩阵乘法。9早于 TPU v6e 的型号需要 8 个周期。

  • At its core, the systolic array is a 2D 256x256 (=65,536) grid of ALUs each capable of performing a multiply and add operation.
  • Weights (W, the 256x256 input) are passed down from above (called the RHS) while inputs (X, the 8x256 input) are passed in from the left (called the LHS).
  • 脉动阵列本质上是由 256x256(=65,536)个 ALU 组成的二维网格,每个 ALU 都能执行一次乘法和加法。
  • 权重(W,256x256 的输入)从上方向下传递,称为 RHS。输入(X,8x256 的输入)从左侧传入,称为 LHS。

Here is a simplified animation of multiplying a set of weights (blue) with a set of activations (green). You’ll notice that the weights (RHS) are partially loaded first, diagonally, and then the activations are fed in, also diagonally. In each frame below, we multiply all the overlapped green and blue units, sum the result with any residual passed in from above, and then pass the result in turn down one unit.

这是一个简化的动画,显示了一组权重 (蓝色) 与一组激活 (绿色) 的相乘. 你会注意到权重 (RHS) 首先部分加载,对角线加载,然后激活也对角线地送入. 在下面的每一帧中,我们将所有重叠的绿色和蓝色单元相乘,将结果与从上方传入的任何残差相加,然后将结果依次向下一个单元传递.

systolic array

Here’s a more general version of this animation showing the output being streamed out of computation:

这是一个更通用的动画版本,显示了输出从计算中流出:

systolic array2

Here’s a diagram showing how this can be pipelined across multiple RHS and LHS arrays:

这是一个图表,显示了这如何在多个 RHS 和 LHS 数组之间进行流水线化:

systolic array pipelining

There is an initial pipeline bubble as the weights (RHS) and activations (LHS) are loaded. After that initial bubble, new inputs and weights can be loaded in without an additional bubble.

在加载权重 (RHS) 和激活 (LHS) 时,会有一个初始的流水线气泡. 在那个初始气泡之后,可以加载新的输入和权重,而不会产生额外的气泡.

Here’s a bad animation of a bf16[2, 3] x bf16[3, 3] matrix multiplication, which you could imagine as a matmul of a 2x3 weight matrix with an input activation of batch 1 and size 3. This is rotated compared to the previous slides and inputs flow out to the right instead of down, but you can roughly see the structure.

这是一个 bf16[2, 3] x bf16[3, 3] 矩阵乘法的糟糕动画,你可以想象成一个 2x3 权重矩阵与一个批量为 1, 大小为 3 的输入激活的矩阵乘法. 与前面的幻灯片相比,这个是旋转的,输入向右流出而不是向下,但你大致可以看到结构.

systolic array bad

We can efficiently pipeline this to multiply large matrices without too large a pipeline bubble. With that said, it’s important that our matrices have shapes larger than the side dimension of the MXU, which is 128x128 or 256x256. Some TPUs (since TPU v3) have multiple MXUs, either 2 for TPU v3, TPU v6e, and TPU7x, and 4 for TPU v4/5, so we need to ensure tiling dimensions are larger than 256 (or 128 for older generations) * number of MXUs. Here’s a good animation for this.

可以用流水线高效地执行大型矩阵乘法,流水线气泡也不会太大。需要注意的是,矩阵尺寸要大于 MXU 的边长(128x128 或 256x256)。自 TPU v3 以来,一些 TPU 型号配有多个 MXU。TPU v3、TPU v6e 和 TPU7x 有 2 个 MXU,TPU v4 和 TPU v5 有 4 个。因此,分块尺寸要大于 256(旧型号为 128)乘以 MXU 数量。这段视频用动画演示了这一过程。

This blog post has another excellent animation of a systolic array multiplication for a fixed weight matrix.

这篇博客文章 有另一个关于固定权重矩阵的脉动阵列乘法的优秀动画.

References参考文献

  1. TPU v4: An optically reconfigurable supercomputer for machine learning with hardware support for embeddings. Jouppi, Norman P and Kurian, George and Li, Sheng and Ma, Peter and Nagarajan, Rahul and Nai, Lifeng and Patil, Nishant and Subramanian, Suvinay and Swing, Andy and Towles, Brian and Young, Cliff and Zhou, Xiang and Zhou, Zongwei and Patterson, David. 2023.

BibTeX

Footnotes

  1. TPUs, and their systolic arrays in particular, are such powerful hardware accelerators because matrix multiplication is one of the few algorithms that uses O(n3)O(n^3) compute for O(n2)O(n^2) bytes. That makes it very easy for an ordinary ALU to be bottlenecked by compute and not by memory bandwidth.

    TPU 的脉动阵列之所以适合做硬件加速,是因为矩阵乘法是少数几个用 O(n2)O(n^2) 字节的数据执行 O(n3)O(n^3) 计算的算法之一。普通的 ALU 执行矩阵乘法时很容易先耗尽算力。

    ↩
  2. We sometimes talk about VMEM prefetching, which refers to loading weights ahead of time in VMEM so we can mask the cost of loading for our matmuls. For instance, in a normal Transformer we can sometimes load our big feed-forward weights into VMEM during attention, which can hide the cost of the weight load if we’re memory bandwidth bound. This requires our weights to be small enough or sharded enough to fit a single layer into VMEM with space to spare.

    VMEM 预取是指提前把权重加载到 VMEM,以隐藏矩阵乘法的加载开销。例如在常规 Transformer 中,有时可以在执行注意力计算的同时将较大的前馈权重加载到 VMEM。工作负载受内存带宽限制时,这样可以把权重加载的时间隐藏起来。这样做要求权重足够小,或者经过充分分片,让 VMEM 容纳整层并留出一定空间。

    ↩
  3. On Cloud TPU VMs, each tray is exposed as part of a separate VM, so there are once again 4 cores visible.

    Cloud TPU VM 会通过独立的虚拟机分别提供每个托盘,因此在一个 VM 中仍只看到 4 个核心。

    ↩
  4. by reconfigurable, we mean they can be dynamically re-shaped into any 3D topology of 4nx4mx4k using a shared optical circuit switch (OCS). The OCS is simply a reconfigurable connection with the same ICI bandwidth. It just lets us connect cubes while retaining a wraparound link.

    这里的“可重构”是指可通过共享的光路交换机(OCS)将这些立方体动态组合成任意 4n×4m×4k 的三维拓扑。OCS 提供与 ICI 带宽相同的可重构连接,把立方体连起来,同时保留环绕链路。

    ↩
  5. Note that a 2x2x4 won’t have any wraparounds since they are provided by the optical switches which are only available on a full cube. A TPU v6e 8x16 will have a wraparound on the longer axis, however, since it doesn’t use reconfigurable optical networking.

    请注意,2x2x4 不会有任何环绕,因为它们是由光学开关提供的,而光学开关仅在完整立方体上可用. 然而,TPU v6e 8x16在较长的轴上有一个环绕,因为它不使用可重新配置的光学网络.

    ↩
  6. TPU ICI links have slightly different bandwidths depending on the operation being performed. You can generally use the numbers in this doc without worry.

    TPU 的 ICI 链路带宽随操作略有不同。一般可直接使用本文的数值。

    ↩
  7. By bidi (bidirectional) bandwidth we mean the total bytes that can be sent along a single link in both directions, or equally, the total number of outgoing bytes from a single TPU along a particular axis, assuming we can use both links efficiently. This is true when we have a functioning ring, AKA when we have a wraparound connection on the particular axis. This occurs on inference chips when we have a full 16 axis, or on training chips (v*p) when we have an axis which is a multiple of 4. We prefer to use the bidirectional bandwidth because it appears frequently in calculations involving bidirectional comms.

    我们所说的双向 (bidirectional) 带宽是指在两个方向上沿单个链路可以发送的总字节数,或者同样地,假设我们可以有效地使用两个链路,沿特定轴从单个 TPU 发出的总字节数. 当我们有一个功能正常的环形,AKA 当我们在特定轴上有一个环绕连接时,这是正确的. 这发生在推理芯片上,当我们有一个完整的 16 轴时,或者在训练芯片 (v*p) 上,当我们有一个是 4 的倍数的轴时. 我们更喜欢使用双向带宽,因为它在涉及双向通信的计算中经常出现.

    ↩
  8. If you are not familiar with this notation, it means: multiplying a 8x256 matrix with bfloat16 elements by a 256x256 matrix with bfloat16 elements and storing the results in a 8x256 matrix with float32 elements.

    此记法表示,将元素类型为 bfloat16 的 8x256 矩阵与元素类型同为 bfloat16 的 256x256 矩阵相乘,并将结果存到元素类型为 float32 的 8x256 矩阵中。

    ↩
  9. The MXU can load 8 rows of f32 inputs and convert them to bf16 every 4 cycles, or 16 rows of bf16 inputs every 8 cycles. In either case, it amounts to 8 rows of bf16 inputs every 4 cycles.

    MXU 可每 4 个周期加载 8 行 f32 输入并将其转换为 bf16,也可每 8 个周期加载 16 行 bf16 输入。两者均等价于每 4 个周期加载 8 行 bf16 输入。

    ↩

补充说明

*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}
}
原版章节与讨论