←Contents目錄
Chapter 1第 1 章
Chapter 1 of 13 · Part I · Foundations
第 1 章(共 13 章)· 第一部 · 基礎

What training computes, and why it needs thousands of GPUs訓練在算什麼,又為何需要上千顆 GPU

Learn what one training step does, how much arithmetic a large model needs, why thousands of GPUs must work in lockstep, and how to measure how well they are used.了解一個訓練步驟做了什麼、大型模型需要多少算術、為何上千顆 GPU 必須同步前進,以及如何衡量它們被用得多好。

≈ 30 min · 2 panels · 1 animations≈ 30 分鐘 · 2 個面板 · 1 個動畫Jump to the quiz ↓跳到測驗 ↓
Before you start
開始之前

Nothing. This chapter assumes no background in networking or machine learning.

不需要任何背景。本章不假設你懂網路或機器學習。

New terms in this chapter (19)本章新名詞(19)

Each term is defined in the text where it first appears; the link jumps there. Terms from earlier chapters are listed under "Before you start".

每個名詞都在它第一次出現的段落中定義,點連結可跳過去。前面各章的名詞列在「開始之前」。

  • Parameter參數 One number in the model that training adjusts. A large language model has billions of them. 模型中由訓練調整的一個數字。大型語言模型有數十億個參數。
  • Token詞元(token) A piece of text, about three quarters of a word, that the model reads as one unit. 文字的一小段,約四分之三個英文單字,模型把它當成一個單位讀入。
  • Training step訓練步驟 One forward pass, one backward pass and one optimizer step over one batch. A run repeats it millions of times. 對一個批次做一次前向傳播、一次反向傳播與一次最佳化器更新。整個訓練重複它數百萬次。
  • Batch批次(batch) The set of training examples (tokens) that one training step processes. 一個訓練步驟處理的一組訓練資料(詞元)。
  • GPUGPU Graphics processing unit: the chip that does the arithmetic of training. An NVIDIA H100 is one. 圖形處理器:負責訓練中算術運算的晶片,例如 NVIDIA H100。
  • Forward pass前向傳播 The model reads the batch layer by layer and computes its prediction and its error (the loss). 模型逐層讀入批次,算出預測與誤差(損失)。
  • Loss損失(loss) One number that says how wrong the model's prediction was. 表示模型預測錯得多嚴重的一個數字。
  • Backward pass反向傳播 The model walks the layers in reverse and computes a gradient for every parameter. 模型反向走過各層,為每個參數算出梯度。
  • Gradient梯度 How sensitive the loss is to each parameter: the rate of change of the loss with respect to it. Its opposite direction lowers the loss fastest locally. 描述損失對每個參數有多敏感,也就是損失函數對參數的變化率;其反方向是局部降低損失最快的方向。
  • Optimizer step優化器步驟 The part of a step that applies the gradients to the parameters. Adam is the common optimizer. 把梯度套用到參數上的那一段。常用的優化器是 Adam。
  • Synchronization同步 The part of a step in which GPUs exchange results so that every copy of the model stays the same. 一個步驟中 GPU 交換結果、讓每份模型副本保持一致的那一段。
  • Rankrank One process of a distributed job, numbered 0, 1, 2 …; in training, one rank drives one GPU. 分散式工作中的一個行程,編號 0、1、2……;訓練中一個 rank 驅動一顆 GPU。
  • FLOP, FLOP/sFLOP、FLOP/s A floating-point operation (one multiply or one add). FLOP/s is the rate; a TFLOP/s is 10¹² per second. 一次浮點運算(一次乘法或加法)。FLOP/s 是速率;1 TFLOP/s 是每秒 10¹² 次。
  • Peak rate峰值速率 The most FLOP/s a GPU can do in theory. Real training reaches 30–60% of it. GPU 理論上最多能做的 FLOP/s。實際訓練約達到其 30–60%。
  • GPU-hourGPU 小時 One GPU busy for one hour; the unit in which training runs are bought and priced. 一顆 GPU 忙碌一小時;訓練用量以此單位購買與計價。
  • MFUMFU Model FLOPs utilization: the arithmetic the model needs, per second, divided by the GPUs' peak rate. 模型 FLOPs 使用率:模型所需的運算量每秒除以 GPU 峰值速率。
  • HFUHFU Hardware FLOPs utilization: like MFU, but it also counts recomputed work, so HFU ≥ MFU. 硬體 FLOPs 使用率:與 MFU 類似,但也計入重算的運算,所以 HFU ≥ MFU。
  • Straggler落後者(straggler) A rank or machine that is persistently slower than the others, and so sets the pace of the whole job. 持續比其他 rank 慢的 rank 或機器,因而決定整個工作的速度。
  • Barrier柵欄(barrier) A point that no rank may pass until every rank has reached it. The step lasts as long as the slowest rank. 所有 rank 都抵達前,誰都不能通過的點。步驟時間由最慢的 rank 決定。

1.1What one training step does一個訓練步驟做了什麼

A language model is a large set of numbers, called parameters. Llama 3 405B, the example this book uses most, has 405 billion of them. The model reads text in pieces called tokens; a token is about three quarters of an English word. Training adjusts the parameters so that the model predicts the next token better. It does so in small steps. Each training step works on one batch: a fixed set of training text, millions of tokens for a large model. The arithmetic runs on GPUs (graphics processing units), chips built to do many multiplications at once. The NVIDIA H100 is the GPU in most of the systems in this book.

語言模型是一大組數字,稱為參數。本書最常用的例子 Llama 3 405B 有 4,050 億個參數。模型以稱為 token(詞元)的小段落讀入文字;一個 token 約為英文單字的四分之三。訓練就是調整參數,讓模型更準確地預測下一個 token。訓練一小步一小步地進行,每一個訓練步驟處理一個批次(batch):一組固定的訓練文字,大型模型一次有數百萬個 token。這些算術在 GPU(圖形處理器)上執行;GPU 是專為同時做大量乘法而設計的晶片。本書大多數系統用的是 NVIDIA H100。

One training step turns one batch of tokens into one update of every parameter. It has four phases, and they always run in the same order. The forward pass pushes the batch through every layer and produces a loss: one number that says how wrong the model was. The backward pass walks the layers in reverse and produces a gradient for every parameter: how sensitive the loss is to that parameter, which tells which way the parameter should be adjusted. In other words, it is the rate of change of the loss with respect to the parameter, and its opposite direction is the one in which the loss falls fastest locally. The optimizer step reads each gradient and moves the parameter a small distance. The synchronization makes every copy of the model agree on the result. The first three phases need only computation on a single GPU; the fourth involves communication between GPUs. Chapter 5 shows that the first three phases shrink when you add GPUs and the fourth does not; that is why this book is about the network.

一個訓練步把一批 token 變成對每一個參數的一次更新。它有四個階段,順序永遠一樣。前向傳播把這批資料推過每一層,產生損失值:一個表示模型錯多少的數字。反向傳播反向走過各層,為每個參數算出梯度:描述參數對損失有多敏感,並指出參數應往哪個方向調整。換言之,即損失函數對參數的變化率,其反方向是局部降低損失最快的方向。優化器步驟讀取每個梯度,把參數移動一小段距離。同步讓模型的每一份副本對結果取得一致。前三個階段只需要在單顆 GPU 上運算,第四項則牽扯到 GPU 之間的通訊。第 5 章會說明:加 GPU 時前三個階段會縮小,第四個不會;這就是本書談網路的原因。

Section 1.2 shows that one GPU would need thousands of years for a model of this size, so thousands of GPUs share each step. The simplest way to share it is to give every GPU its own copy of the model and a different part of the batch. Each GPU computes gradients for its own part. Before the optimizer step, the gradients from all GPUs must be added together, so that every copy makes the same update and the copies stay identical. An operation called an all-reduce computes this sum: every GPU sends its gradients in, and every GPU receives the total. That exchange is the synchronization phase, and it is the reason the GPUs need a network. This way of splitting the work is called data parallelism. Chapter 3 introduces it properly, together with four other ways to split a job, and Chapter 4 shows how an all-reduce is done. In a program that runs on many GPUs, the process that drives one GPU is called a rank; ranks are numbered 0, 1, 2 and so on.

第 1.2 節會算出:這種規模的模型若只用一顆 GPU,要花上數千年,所以每一步都由數千顆 GPU 分擔。最簡單的分擔方式,是讓每顆 GPU 各持有一份完整的模型,並處理批次中不同的一部分。每顆 GPU 為自己那一部分算出梯度。在優化器步驟之前,必須把所有 GPU 的梯度加總,讓每份副本做出相同的更新、彼此保持一致。這個加總由一種稱為 all-reduce 的操作完成:每顆 GPU 送出自己的梯度,每顆 GPU 都收到總和。這次交換就是同步階段,也是 GPU 需要網路的原因。這種分工方式稱為資料平行。第 3 章會正式介紹它以及另外四種切分方式,第 4 章說明 all-reduce 實際上怎麼做。在跨多顆 GPU 執行的程式中,驅動一顆 GPU 的行程稱為 rank,編號為 0、1、2 等等。

1.2How much arithmetic: the 6ND rule算術有多少:6ND 法則

Training is arithmetic, so the first question is how much of it there is. Count it in FLOPs, floating-point operations: one multiplication or one addition on numbers with a fractional part. A GPU's speed is counted in FLOP per second, FLOP/s; one TFLOP/s is 101210^{12} of them per second.

訓練就是算術,所以第一個問題是:算術有多少?以 FLOP(浮點運算)計數:對帶小數的數字做一次乘法或一次加法。GPU 的速度以每秒 FLOP 數,即 FLOP/s 計;1 TFLOP/s 是每秒 101210^{12} 次。

Start with one weight matrix with dind_{\text{in}} inputs and doutd_{\text{out}} outputs, applied to one token. The forward pass computes doutd_{\text{out}} sums, each of dind_{\text{in}} products: din×doutd_{\text{in}} \times d_{\text{out}} multiply-adds, which is 2 din dout2\,d_{\text{in}}\,d_{\text{out}} floating-point operations, or exactly 2 FLOPs per weight. Sum over all the weight matrices in the model and the forward pass costs about 2N2N FLOPs per token, where NN is the number of parameters the token passes through. In a dense model that is every parameter. In a mixture-of-experts (MoE) model, each layer holds many small sub-networks and each token passes through only a few of them, so NN is the active parameter count, much smaller than the total (Chapter 3). The backward pass does two matrix multiplies of the same size for each one in the forward pass: one to get the gradient with respect to the layer's input, which the layer below needs, and one to get the gradient with respect to the weights. That is 4N4N. Together, 6N6N FLOPs per token, and for a run over DD tokens

從一個有 dind_{\text{in}} 個輸入、doutd_{\text{out}} 個輸出的權重矩陣開始,作用在一個 token 上。前向算 doutd_{\text{out}} 個和,每個和有 dind_{\text{in}} 個乘積:din×doutd_{\text{in}} \times d_{\text{out}} 次乘加,即 2 din dout2\,d_{\text{in}}\,d_{\text{out}} 次浮點運算,恰好每個權重 2 FLOPs。把模型裡所有權重矩陣加起來,前向每個 token 約 2N2N FLOPs,NN 是這個 token 經過的參數量。dense 模型就是全部參數;混合專家(MoE)模型的每一層含有許多小型子網路,每個 token 只經過其中少數幾個,所以 NN 是啟用參數量,遠小於總參數量(第 3 章)。反向對前向的每一次矩陣乘法都做兩次同樣大小的乘法:一次算對該層輸入的梯度(往回的下一層需要它),一次算對權重的梯度。這是 4N4N。合計每個 token 6N6N FLOPs,跑過 DD 個 token 就是

FLOPs≈6ND \text{FLOPs} \approx 6ND

Figure 1.1 shows the three products for one weight matrix.

圖 1.1 畫出一個權重矩陣的三次乘法。

one weight matrix W (din × dout), one token一個權重矩陣 W(din × dout)、一個 tokenforward前向y = W xoutput for the next layer給下一層的輸出2 FLOP / weight每權重 2 FLOPbackward反向∂x = Wᵀ ∂ygradient for the layer below給下面一層的梯度2 FLOP / weight每權重 2 FLOPbackward反向∂W = ∂y xᵀgradient for the optimizer給優化器的梯度2 FLOP / weight每權重 2 FLOPper weight per token: 2 + 2 + 2 = 6每權重每 token:2 + 2 + 2 = 6× N weights × D tokens → 6ND FLOPs× N 個權重 × D 個 token → 6ND FLOPs
Figure 1.1. One weight matrix and one token. The forward pass makes one multiply-add per weight, which is 2 FLOPs. The backward pass makes two products of the same size: the gradient for the input, which the layer below needs, and the gradient for the weights, which the optimizer needs. Per weight per token that is 2 forward and 4 backward, and over NN weights and DD tokens it is 6ND6ND.圖 1.1。一個權重矩陣、一個 token。前向對每個權重做一次乘加,即 2 FLOPs。反向做兩次同樣大小的乘法:對輸入的梯度(下面一層需要它)與對權重的梯度(優化器需要它)。每權重每 token 前向 2、反向 4,乘上 NN 個權重與 DD 個 token 就是 6ND6ND。Drawn for this primer from the derivation above; attention scores, recomputation and the optimizer are left out, as in the text.

where NN is the number of parameters and DD is the number of training tokens. For Llama 3 405B, with about 15 trillion tokens, this is about 3.8×10253.8 \times 10^{25} floating-point operations. One GPU cannot do this in a human lifetime. The worked example below shows by how much.

其中 NN 是參數量,DD 是訓練 token 數。以 Llama 3 405B、約 15 兆 token 計算,大約是 3.8×10253.8 \times 10^{25} 次浮點運算。一顆 GPU 在人的一生之內做不完。下面的算例會說明差距有多大。

The rule ignores the attention scores, which add about 12×layers×sequence length×hidden size12 \times \text{layers} \times \text{sequence length} \times \text{hidden size} per token and matter when sequences are very long. It ignores recomputation, extra work that some systems do to save memory (Chapter 3). It also ignores the optimizer, which touches each parameter a handful of times per step, keeps about 16 bytes of numbers per parameter (Chapter 3 counts them), and is limited by memory speed, not by arithmetic.

這條法則忽略了注意力分數——每個 token 另外約需 12×層數×序列長度×隱藏維度12 \times \text{層數} \times \text{序列長度} \times \text{隱藏維度},序列很長時才重要。它忽略重算,也就是某些系統為了省記憶體而多做的工作(第 3 章)。它也忽略優化器:優化器每步只碰每個參數少數幾次、每個參數約保留 16 位元組的數字(第 3 章會逐項計算),受限於記憶體速度而非算術。

To turn FLOPs into time, divide by the rate at which a GPU computes. Its peak rate is the most it can do in theory. The table gives the peaks of three GPU generations for BF16, the 16-bit number format of training (Chapter 3). "Dense" means without the 2:1 structured-sparsity figure that product sheets often quote, which training does not use. Chapter 2 explains the memory columns and the NVLink column, the connections between the GPUs inside one machine.

要把 FLOP 換成時間,就除以 GPU 的運算速率。它的峰值速率是理論上的上限。下表列出三個 GPU 世代在 BF16(訓練用的 16 位元數字格式,第 3 章)下的峰值。「稠密」是指不含產品規格表常引用的 2:1 結構化稀疏數字,訓練用不到那個。第 2 章會解釋記憶體兩欄,以及 NVLink(一台機器內 GPU 之間的連接)一欄。

GPUDense BF16 peak稠密 BF16 峰值Memory記憶體Memory bandwidth記憶體頻寬NVLink per GPU每 GPU NVLink
A100 (2020)312 TFLOP/s80 GBabout 2.0 TB/s約 2.0 TB/sabout 600 GB/s約 600 GB/s
H100 SXM (2022)989 TFLOP/s80 GBabout 3.35 TB/s約 3.35 TB/sabout 900 GB/s約 900 GB/s
B200 (2024)about 2.2 PFLOP/s約 2.2 PFLOP/sabout 180 GB約 180 GBabout 8 TB/s約 8 TB/sabout 1.8 TB/s約 1.8 TB/s

Now the worked example: Llama 3 405B, pre-trained on about 15.6 trillion tokens. The arithmetic, one question per row; a GPU-hour is one GPU busy for one hour, the unit in which training time is bought:

現在看實例:Llama 3 405B,以約 15.6 兆 token 預訓練。逐列計算如下;GPU 小時是一顆 GPU 忙碌一小時,也是購買訓練時間的單位:

Question問題Calculation算式Result結果
Training compute (6ND6ND)訓練計算量(6ND6ND)6×405×109×15.6×10126 \times 405 \times 10^{9} \times 15.6 \times 10^{12}≈3.79×1025\approx 3.79 \times 10^{25} FLOP
One H100 at its 989 TFLOP/s peak一顆 H100、989 TFLOP/s 峰值3.79×1025÷9.89×10143.79 \times 10^{25} \div 9.89 \times 10^{14}≈3.8×1010\approx 3.8 \times 10^{10} sabout 1,200 years約 1,200 年
One H100 at the 400 TFLOP/s achieved (Llama 3 Table 4, 16K-GPU row)一顆 H100、實際達到的 400 TFLOP/s(Llama 3 Table 4 的 16K GPU 那一行)3.79×1025÷4.0×10143.79 \times 10^{25} \div 4.0 \times 10^{14}≈9.5×1010\approx 9.5 \times 10^{10} sabout 3,000 years約 3,000 年
Aggregate rate of 16,384 GPUs at 400 TFLOP/s16,384 顆 GPU、每顆 400 TFLOP/s 的總速率16,384×4.0×101416{,}384 \times 4.0 \times 10^{14}≈6.55×1018\approx 6.55 \times 10^{18} FLOP/s
Pure stepping time on those 16,384 GPUs這 16,384 顆 GPU 的純步進時間3.79×1025÷6.55×10183.79 \times 10^{25} \div 6.55 \times 10^{18}≈5.8×106\approx 5.8 \times 10^{6} sabout 67 days約 67 天
Average rate per GPU from the model card's 31 million GPU-hours由模型卡的 3,100 萬 GPU 小時反推每顆 GPU 的平均速率3.79×1025÷(3.1×107×3,600)3.79 \times 10^{25} \div (3.1 \times 10^{7} \times 3{,}600)≈3.4×1014\approx 3.4 \times 10^{14} FLOP/sabout 340 TFLOP/s, 34% of peak約 340 TFLOP/s,峰值的 34%
One H100 GPU-year at 40% of peak一個 H100 GPU 年、40% 峰值0.4×9.89×1014×3.15×1070.4 \times 9.89 \times 10^{14} \times 3.15 \times 10^{7}≈1.2×1022\approx 1.2 \times 10^{22} FLOP

So pure stepping on 16,384 GPUs would take about 67 days. The real run took longer: averaged over everything, each GPU delivered about 340 TFLOP/s, about 34% of peak. The difference is a long-context phase that ran less efficiently (Section 1.3), the restarts after 466 interruptions in 54 days, and the work lost between each failure and the last saved copy of the training state, called a checkpoint (Chapter 9). Keep two numbers from this section: a GPU-year of H100 time at 40% of peak is about 1.2×10221.2 \times 10^{22} FLOP, and a frontier model needs thousands of them.

所以 16,384 顆 GPU 的純步進時間約 67 天。實際的運行更久:全程平均下來,每顆 GPU 只發揮約 340 TFLOP/s,約為峰值的 34%。差距來自效率較低的長上下文階段(第 1.3 節)、54 天內 466 次中斷之後的重啟,以及每次故障與上一份訓練狀態存檔之間損失的工作;這種存檔稱為檢查點(第 9 章)。從這一節記住兩個數字:一個 H100 GPU 年在 40% 峰值下約為 1.2×10221.2 \times 10^{22} FLOP,而一個前沿模型需要數千個。

Figure 1.2 puts the three per-GPU rates side by side, and Panel 1.1 repeats the calculation for other models and clusters.

圖 1.2 把每顆 GPU 的三種速率並排,面板 1.1 則對其他模型與叢集重做這個計算。

02505007501000TFLOP/s per GPU每顆 GPU 的 TFLOP/speak, dense BF16峰值,稠密 BF16989 TFLOP/sachieved in steps步內實際400 · 40% of peak400 · 峰值的 40%gap: slow kernels and time spent waiting差距:較慢的 kernel 與等待時間average of the run全程平均340 · 34% of peak340 · 峰值的 34%gap: also a slower long-context phase,差距還有:較慢的長上下文階段、restarts after 466 interruptions, lost work466 次中斷後的重啟、損失的工作
Figure 1.2. One H100's rate on Llama 3 405B, three ways. The peak is the dense BF16 figure. The achieved rate is the 400 TFLOP/s of Llama 3 Table 4 at 16,384 GPUs. The run average is what the model card's 31 million GPU-hours imply for 3.79×10253.79 \times 10^{25} FLOP. The notes under the bars say where the time went; Section 1.3 explains them.圖 1.2。一顆 H100 在 Llama 3 405B 上的速率,三種算法。峰值是稠密 BF16 數字。實際速率是 Llama 3 Table 4 在 16,384 顆 GPU 時的 400 TFLOP/s。全程平均是模型卡的 3,100 萬 GPU 小時對 3.79×10253.79 \times 10^{25} FLOP 所隱含的值。長條下方的註記說明時間的去向;第 1.3 節會解釋。Drawn for this primer from the table and the worked example above.
Panel 1.1面板 1.1

From 6ND6ND to days on a cluster從 6ND6ND 到叢集上的天數

Training compute, 6ND
訓練計算量 6ND
GPU-hours at this rate
此速率下的 GPU 小時
Pure stepping time
純步進時間
H100 GPU-years at 40%
40% 峰值的 H100 GPU 年

The model is FLOPs=6ND\text{FLOPs} = 6ND and a time of 6ND/(GF)6ND/(GF): pure stepping, with no communication, restarts or checkpoints. Defaults reproduce the table above: Llama 3 405B, 15.6 trillion tokens, 16,384 GPUs at the 400 TFLOP/s of Llama 3 Table 4, about 3.79×10253.79 \times 10^{25} FLOP and 67 days. The last tile counts the result in this section's unit, one H100 GPU-year at 40% of peak (about 1.2×10221.2 \times 10^{22} FLOP).

模型是 FLOPs=6ND\text{FLOPs} = 6ND,時間為 6ND/(GF)6ND/(GF):純步進,不含通訊、重啟或檢查點。預設值重現上面的表:Llama 3 405B、15.6 兆 token、16,384 顆 GPU、Llama 3 Table 4 的 400 TFLOP/s,約 3.79×10253.79 \times 10^{25} FLOP、67 天。最後一格以本節的單位計數:一個 40% 峰值的 H100 GPU 年(約 1.2×10221.2 \times 10^{22} FLOP)。

1.3Utilization: how much of the peak a run uses使用率:一次訓練用到多少峰值

The number that tells you how well a run uses its GPUs is MFU, model FLOPs utilization: the tokens per second the job actually trains, multiplied by 6N6N, divided by the number of GPUs times their peak FLOP/s. It counts only the arithmetic the model needs. HFU, hardware FLOPs utilization, counts every FLOP the hardware executed, including recomputation. When a system saves memory by keeping fewer intermediate results and recomputing them in the backward pass (activation checkpointing, Chapter 3), it performs an extra forward pass, about 8N8N per token instead of 6N6N, and HFU credits it. HFU is therefore always at least MFU, and the gap is extra computation (recomputing the forward pass during the backward pass) traded for less memory use. The PaLM authors, who proposed MFU as the metric to report, gave 46.2% MFU and 57.8% HFU for the same run. The TPU v4 paper quotes the 57.8% figure and reports that TPU v4 sustains about 60% of peak on large language models (TPU v4 guided reading). When you compare systems, check which of the two is being quoted, and against which peak: dense or sparse, BF16 or FP8.

衡量一次訓練把 GPU 用得多好的數字是 MFU(模型 FLOPs 使用率):任務實際訓練的每秒 token 數乘以 6N6N,除以 GPU 數乘上它們的峰值 FLOP/s。它只計入模型需要的算術。HFU(硬體 FLOPs 使用率)計入硬體執行的每一次 FLOP,包括重算。當系統為了省記憶體而少存中間結果、在反向傳播時重算它們(激活值檢查點,第 3 章),它多做一次前向,每個 token 約 8N8N 而非 6N6N,HFU 會把它算進去。所以 HFU 永遠不低於 MFU,兩者的差距,就是用額外的運算(在反向傳播時重算前向)換取更少的記憶體使用。提議以 MFU 作為回報指標的 PaLM 作者,對同一次運行給出 46.2% MFU 與 57.8% HFU。TPU v4 論文引用的是 57.8% 這個數字,並回報 TPU v4 在大型語言模型上持續達到約 60% 的峰值(TPU v4 導讀)。比較系統時,要確認引用的是兩者中的哪一個,以及相對於哪個峰值:稠密或稀疏、BF16 或 FP8。

Figure 1.3 shows what each metric counts.

圖 1.3 畫出兩種指標各計入什麼。

forward 2N前向 2Nbackward 4N反向 4Nrecompute 2N重算 2NMFU counts 6NMFU 計入 6NHFU counts 8N: everything the GPU ranHFU 計入 8N:GPU 執行的一切PaLM 540B, one run: 46.2% MFU, 57.8% HFUPaLM 540B,同一次運行:46.2% MFU、57.8% HFU
Figure 1.3. FLOPs per token for a model of NN parameters trained with activation checkpointing. Before the backward pass uses an intermediate result (activation) that was not kept, it recomputes the forward pass. MFU credits only the 6N6N that the model needs; HFU credits all 8N8N that the hardware ran. So HFU is never below MFU, and the gap is extra computation traded for less memory use.圖 1.3。以 activation checkpointing 訓練、NN 個參數的模型每個 token 的 FLOPs。反向要用到一個沒保留的中間結果(激活值)之前,先重算前向。MFU 只計入模型需要的 6N6N;HFU 計入硬體執行的全部 8N8N。所以 HFU 永遠不低於 MFU,兩者的差距,就是用額外的運算換取更少的記憶體使用。Drawn for this primer from Section 1.3; the PaLM numbers are the ones the TPU v4 digest quotes.
System系統Scale規模Reported回報值Source來源
MegaScale, 175B256 Ampere GPUs256 顆 Ampere GPU65.3% MFUMegaScale Table 2MegaScale Table 2
MegaScale, 175B12,288 Ampere GPUs12,288 顆 Ampere GPU55.2% MFUMegaScale Table 2; Megatron-LM 41.2%MegaScale Table 2;Megatron-LM 41.2%
Llama 3 405B8,192 H1008,192 顆 H10043% BF16 MFULlama 3 Table 4Llama 3 Table 4
Llama 3 405B16,384 H10016,384 顆 H10041% / 38% BF16 MFULlama 3 Table 4; 8K / 128K contextLlama 3 Table 4;8K / 128K 上下文
PaLM 540B6,144 TPU v46,144 顆 TPU v446.2% MFU, 57.8% HFUPaLM paper; TPU v4 paper quotes 57.8%PaLM 論文;TPU v4 論文引用 57.8%
LLMs on TPU v4TPU v4 上的 LLMup to 4,096 chips最多 4,096 顆晶片about 60% of peak約 60% 峰值TPU v4 paper, results sectionTPU v4 論文,結果一節

Why does MFU remain so far below peak GPU performance? Three factors account for most of the gap. First, GPU execution is imperfect. Small matrix operations fail to fully utilize the hardware, while layer normalization, softmax and optimizer updates are often memory-bound, limited by how fast data moves in and out of GPU memory rather than by arithmetic. Second, communication introduces idle time, because GPUs must wait for data transfers across devices (Chapter 4 and Chapter 5). Third, synchronization amplifies stragglers, GPUs that run persistently slower than the rest. Faster GPUs remain idle until the slowest worker finishes (Section 1.4). These overheads reduce MFU within each training step. Beyond that, checkpointing, data loading and failure recovery introduce additional downtime, including the 466 interruptions recorded over 54 days. Because reported MFU typically excludes this downtime, actual end-to-end training efficiency can be even lower. Effective training time, the share of wall-clock time in which the job makes progress it keeps, measures it separately; it was above 90% for both MegaScale and Llama 3 (Chapter 10). Multiply the two to get the fraction of purchased GPU-time that became model: about 0.41×0.9≈37%0.41 \times 0.9 \approx 37\% for the Llama 3 run, which is consistent with the 34% that the model card's GPU-hours imply.

為什麼 MFU 會遠低於 GPU 的峰值效能?差距大部分來自三個因素。第一,GPU 的執行並不完美。小型矩陣運算無法充分利用硬體,而 layer normalization、softmax 與優化器更新往往受記憶體限制,速度取決於資料進出 GPU 記憶體有多快,而不是算術有多快。第二,通訊帶來閒置時間,因為 GPU 必須等待跨裝置的資料傳輸(第 4 章與第 5 章)。第三,同步會放大落後者(持續比其他 GPU 慢的 GPU)的影響。較快的 GPU 必須閒置,直到最慢的那一個完成(第 1.4 節)。這些開銷都在每個訓練步之內降低 MFU。除此之外,檢查點、資料載入與故障恢復還帶來額外的停機時間,包括 54 天內記錄到的 466 次中斷。由於回報的 MFU 通常不含這些停機時間,實際端到端的訓練效率可能更低。有效訓練時間——工作在牆鐘時間中產生可保留進度的比例——把它分開量;MegaScale 與 Llama 3 都高於 90%(第 10 章)。把兩者相乘就得到買來的 GPU 時間中真正變成模型的比例:Llama 3 這次運行約 0.41×0.9≈37%0.41 \times 0.9 \approx 37\%,與模型卡的 GPU 小時所隱含的 34% 一致。

Figure 1.4 follows the purchased GPU time down to that 37%.

圖 1.4 把買來的 GPU 時間一路追到這 37%。

0%25%50%75%100%percent of peak峰值的百分比peak, every GPU-hour每 GPU 小時的峰值100%MFU in steps步內 MFU41%lost inside steps: waiting, slow kernels步內損失:等待、較慢的 kernel× 0.9 effective time× 0.9 有效時間≈ 37%checkpoints, restarts, loading檢查點、重啟、資料載入34% from 31M GPU-hours34%:由 3,100 萬 GPU 小時反推
Figure 1.4. Where the purchased GPU time of the Llama 3 405B run goes. Inside the training steps, MFU keeps 41% of the peak. Effective training time, above 90%, removes the time between steps. The product, about 37%, is close to the 34% that the model card's GPU-hours imply, which the orange line marks.圖 1.4。Llama 3 405B 這次運行買來的 GPU 時間去了哪裡。在訓練步之內,MFU 保住峰值的 41%。有效訓練時間(高於 90%)扣掉步與步之間的時間。乘積約 37%,接近模型卡 GPU 小時所隱含的 34%,也就是圖中橘色直線標示的位置。Drawn for this primer from the numbers of Sections 1.2 and 1.3.

Here is what one MFU point buys on the 3.79×10253.79 \times 10^{25} FLOP of Llama 3 405B. The price is about $2 per H100-hour, a round number in the range of public cloud rates in 2024–2025, and the power is 700 W per GPU:

一個 MFU 百分點值多少,以 Llama 3 405B 的 3.79×10253.79 \times 10^{25} FLOP 算給你看。價格以每 H100 小時約 2 美元計——這是 2024–2025 年公有雲價格範圍內的一個整數——功率以每顆 GPU 700 W 計:

Question問題Calculation算式Result結果
GPU-hours at 100% of the H100 peak在 H100 峰值 100% 下的 GPU 小時3.79×1025÷9.89×1014÷3,6003.79 \times 10^{25} \div 9.89 \times 10^{14} \div 3{,}600≈1.064×107\approx 1.064 \times 10^{7} GPU-habout 10.6 million約 1,060 萬
GPU-hours at 41% MFU41% MFU 時的 GPU 小時1.064×107÷0.411.064 \times 10^{7} \div 0.41≈2.60×107\approx 2.60 \times 10^{7} GPU-habout 26.0 million約 2,600 萬
GPU-hours at 42% MFU42% MFU 時的 GPU 小時1.064×107÷0.421.064 \times 10^{7} \div 0.42≈2.53×107\approx 2.53 \times 10^{7} GPU-habout 25.3 million約 2,530 萬
One MFU point, 41% → 42%一個 MFU 百分點,41% → 42%1.064×107×(1/0.41−1/0.42)1.064 \times 10^{7} \times (1/0.41 - 1/0.42)≈6.2×105\approx 6.2 \times 10^{5} GPU-habout 620,000約 62 萬
The same in hours of the whole 16,384-GPU cluster換算成整個 16,384 顆 GPU 叢集的小時數6.2×105÷16,3846.2 \times 10^{5} \div 16{,}384≈38\approx 38 h
The same in dollars at $2 per H100-hour以每 H100 小時 2 美元計的金額6.2×105×26.2 \times 10^{5} \times 2≈1.2×106\approx 1.2 \times 10^{6} USDabout $1.2 million約 120 萬美元
The same in GPU energy at 700 W (0.7 kW)以 700 W(0.7 kW)計的 GPU 用電6.2×105×0.76.2 \times 10^{5} \times 0.7≈4.3×105\approx 4.3 \times 10^{5} kWhabout 430 MWh約 430 MWh

One point is therefore about 620,000 GPU-hours, about 38 hours of the whole cluster, about $1.2 million, and about 430 MWh of GPU power alone, before cooling and the rest of the machine. These are the stakes behind the gains in later chapters: 14.9% from a better way of wiring the network at Alibaba (Chapter 6), more than 2× for the exchanges between GPUs from tuning the network and the communication library together at Meta (Chapter 4), and 0.7 points from removing 0.5% of slow machines at ByteDance (Section 1.4). The network is a few percent of a cluster's cost. The utilization it decides is worth far more than the network itself.

所以一個百分點約為 62 萬 GPU 小時,相當於整個叢集約 38 小時,約值 120 萬美元,光是 GPU 電力就約 430 MWh,還不含冷卻與機器的其他部分。這就是後面各章那些增益背後的賭注:Alibaba 靠更好的網路接法得到 14.9%(第 6 章)、Meta 靠同時調校網路與通訊函式庫讓 GPU 之間的交換快了超過 2 倍(第 4 章)、ByteDance 靠移除 0.5% 的慢機器拿回 0.7 個百分點(第 1.4 節)。網路只占叢集成本的幾個百分點。它所決定的使用率,價值遠超過網路本身。

Panel 1.2 recomputes the value of one point for other MFU levels, prices and power draws.

面板 1.2 對其他 MFU、價格與功率重算一個百分點的價值。

Panel 1.2面板 1.2

What one MFU point is worth一個 MFU 百分點值多少

GPU-hours at this MFU
此 MFU 下的 GPU 小時
One more point saves
多一個百分點省下
In dollars
換算金額
In GPU energy
換算 GPU 用電

The model divides the 3.79×10253.79 \times 10^{25} FLOP of Llama 3 405B by the H100's 989 TFLOP/s peak times MFU. One point is the GPU-hours saved by going from the chosen MFU to one point more. Cluster hours assume 16,384 GPUs, and energy counts the GPUs only, not cooling. Defaults reproduce the table above: 41% MFU, about $2 per H100-hour (a round number in the 2024–2025 public cloud range) and 700 W. The curve falls about as 1/MFU21/\mathrm{MFU}^2: a point is worth most where MFU is low.

模型把 Llama 3 405B 的 3.79×10253.79 \times 10^{25} FLOP 除以 H100 的 989 TFLOP/s 峰值乘上 MFU。一個百分點是從所選的 MFU 再提高一點所省下的 GPU 小時。叢集小時以 16,384 顆 GPU 計,用電只計 GPU,不含冷卻。預設值重現上面的表:41% MFU、每 H100 小時約 2 美元(2024–2025 年公有雲價格範圍內的一個整數)、700 W。曲線約按 1/MFU21/\mathrm{MFU}^2 下降:MFU 低的時候,一個百分點最值錢。

1.4Many GPUs, one step: the barrier多顆 GPU、一個步驟:柵欄

Every exchange of this kind is a barrier: a point that no rank can pass until every rank has reached it. The all-reduce cannot produce its result until the last GPU's gradients have arrived, and the optimizer cannot update until the sum is complete. So the time of a step is not the average time of its participants but the maximum. If one GPU in 16,384 is 10% slower, the other 16,383 wait 10% of a step, every step, for as long as that GPU stays in the job. MegaScale found that about 0.5% of its machines were substantially slower than the rest, in one case about 10% longer on identical forward computations, and that removing them regained about 0.7 MFU points and made peak MFU consistent from run to run (MegaScale §6.3). Llama 3 states the rule in one sentence: a single straggler, a rank that is persistently slower than the others, can slow thousands of GPUs. That is why Meta built tools to find the slowest ranks (Llama 3 §3.3.4). The network is one source of such delay. A part of the network that is overloaded makes one rank's data arrive late in exactly the way a slow GPU does, and the rest of the job cannot tell the difference.

每一次這類交換都是一道柵欄(barrier):所有 rank 都抵達之前,誰也不能通過的點。all-reduce 要等最後一顆 GPU 的梯度到達才能產生結果,優化器要等總和完成才能更新。所以一步的時間不是參與者的平均時間,而是最大值。如果 16,384 顆 GPU 中有一顆慢 10%,其他 16,383 顆每一步都等 10% 的步時間,只要那顆 GPU 還在任務裡就一直如此。MegaScale 發現約 0.5% 的機器明顯比其他慢,其中一例在相同的前向計算上多花約 10%,而移除它們拿回約 0.7 個 MFU 百分點,並讓各次運行的峰值 MFU 變得一致(MegaScale §6.3)。Llama 3 用一句話說出這條規則:單一個 straggler(持續比其他 rank 慢的 rank)就能拖慢數千顆 GPU。所以 Meta 建了工具來找出最慢的 rank(Llama 3 §3.3.4)。網路是這類拖慢的來源之一。網路中某個超載的部分讓某個 rank 的資料晚到,效果和一顆慢 GPU 完全相同,任務的其餘部分分不出差別。

Figure 1.5 draws one step of eight ranks with one straggler.

圖 1.5 畫出八個 rank、其中一個是 straggler 的一步。

rank 0rank 1rank 2rank 3rank 4rank 5rank 6rank 7barrier柵欄+10%wait等待01.001.10time in the step步內時間compute計算straggler, 10% slowerstraggler,慢 10%waiting at the barrier在柵欄前等待
Figure 1.5. One step of eight data-parallel ranks, to scale. Compute time varies by a few percent from rank to rank, and rank 5 is a chronic straggler, 10% slower. The all-reduce at the end is a barrier: no rank goes on until rank 5 arrives, so the step lasts 1.10 and the other ranks wait in the grey areas. The small random differences are illustrative; the 10% is MegaScale's straggler case (§6.3).圖 1.5。八個資料平行 rank 的一步,按比例畫。各 rank 的計算時間相差幾個百分點,rank 5 是長期 straggler,慢 10%。最後的 all-reduce 是一道柵欄:rank 5 到達之前沒有 rank 能繼續,所以這一步持續 1.10,其他 rank 在灰色區域等待。這些小的隨機差異只是示意;10% 是 MegaScale 的 straggler 案例(§6.3)。Drawn for this primer.

Animation 1.1 plays the step of Figure 1.5 in time order: each rank reaches the barrier and waits there.

動畫 1.1 依時間順序播放圖 1.5 的那一步:每個 rank 抵達柵欄,然後在那裡等待。

Animation 1.1動畫 1.1

The barrier: eight ranks wait for the slowest柵欄:八個 rank 等最慢的那一個

The rank times are those of Figure 1.5: 0.97 to 1.02 from small random differences, and rank 5 at 1.10, MegaScale's 10% straggler (§6.3). When rank 5 is like the others, it takes 1.00. The time axis is cut between 0 and 0.90 so that a difference of 0.01 is visible. The all-reduce after the barrier is not drawn.

各 rank 的時間取自圖 1.5:抖動造成 0.97 到 1.02,rank 5 為 1.10,即 MegaScale 的 10% straggler(§6.3)。讓 rank 5 和其他 rank 一樣時,它花 1.00。時間軸在 0 與 0.90 之間截斷,好讓 0.01 的差距看得見。柵欄之後的 all-reduce 沒有畫出。

Random variation adds to this. Every rank's step time varies a little from step to step, and the step waits for the largest of thousands of such values, which grows with the number of ranks. Chapter 10 works out how large that effect is, and how operators find and remove stragglers. The network is part of the same story: a part of the network that is slow for one step delays every rank, exactly as a slow GPU does.

隨機變動會讓情況更糟。每個 rank 的步驟時間每一步都略有不同,而一步要等數千個這種數值中的最大者,這個最大值會隨 rank 數增加而變大。第 10 章會算出這個效應有多大,以及營運者如何找出並移除 straggler。網路也在同一個故事裡:只要網路的某一部分在某一步變慢,所有 rank 都會被拖慢,和一顆慢 GPU 的效果完全一樣。

How this book labels numbers
本書如何標示數字

Numbers in this book come from four kinds of evidence, and the text says which one each number is.

measuredObserved on real hardware: in a testbed, or in a production cluster.computedDerived from a formula or a model of the system, such as the 6ND rule. A computed number is only as good as its assumptions.simulatedProduced by a program that imitates the hardware, one small piece of data or one time step at a time.our exampleArithmetic made for this book to show a mechanism, often with round numbers.

Papers mix the four, and so did the first edition of this book. When a paper's figure is computed, this edition says so in the caption. Chapter 12 explains how to judge each kind.

本書的數字來自四種證據,文中會註明每個數字屬於哪一種。

量測在真實硬體上觀察到的數字:測試平台或正式叢集。計算由公式或系統模型推得,例如 6ND 法則。計算所得的數字只和它的假設一樣可靠。模擬由模仿硬體的程式產生,一次處理一小段資料或一個時間步。本書例題本書為說明某個機制而做的算術,常用整數。

論文常混用這四種,本書第一版也是。這一版遇到論文中計算所得的圖時,會在圖說中註明。第 12 章說明如何判讀每一種。

Chapter summary
本章摘要

A training step is a forward pass, a backward pass, an optimizer step and a synchronization. Its arithmetic is about 6 FLOPs per parameter per token, so a model of 405 billion parameters trained on 15.6 trillion tokens needs about 3.8×10253.8 \times 10^{25} FLOPs: thousands of GPU-years. Thousands of GPUs therefore share each step, and they must add their gradients together before every update. MFU measures how much of the GPUs' peak rate a run turns into the arithmetic the model needs; at Llama 3's scale one point of MFU is worth about a million dollars. Because the synchronization is a barrier, a step lasts as long as its slowest GPU, or the slowest part of the network between them.

一個訓練步驟包含前向傳播、反向傳播、優化器步驟與同步。它的算術量約為每個參數、每個 token 6 個 FLOP,所以用 15.6 兆個 token 訓練 4,050 億參數的模型約需 3.8×10253.8 \times 10^{25} FLOP,相當於數千個 GPU 年。因此每一步都由數千顆 GPU 分擔,而且每次更新前都必須把梯度加總。MFU 衡量一次訓練把 GPU 峰值速率的多少轉成模型需要的算術;在 Llama 3 的規模,一個 MFU 百分點約值一百萬美元。由於同步是一道柵欄,一個步驟的長度由最慢的 GPU,或兩者之間網路中最慢的部分決定。

Where these ideas go next
這些概念接下來用在哪裡

Chapter 2 introduces the hardware: the GPU's memory, the links inside a server, the network card and the switch, and the speed of each. Chapter 3 shows how a model is split across GPUs, and Chapter 4 how GPUs exchange data. Chapter 5 adds up the bytes of one step and shows why the network decides how fast training runs.

第 2 章介紹硬體:GPU 的記憶體、伺服器內的鏈路、網路卡與交換器,以及各自的速度。第 3 章說明模型如何切到多顆 GPU 上,第 4 章說明 GPU 如何交換資料。第 5 章把一個步驟的位元組加總,說明為何網路決定了訓練的速度。

Guided readings for this chapter本章相關的論文導讀

Check your understanding檢驗你的理解