Skip to content
quboliu
⚙

How to Parallelize a Transformer for Training

Part 5 of How To Scale Your Model (Part 4: Transformers | Part 6: Training LLaMA)

Here we discuss four main parallelism schemes used during LLM training: data parallelism, fully-sharded data parallelism (FSDP), tensor parallelism, and pipeline parallelism. For each, we calculate at what point we become bottlenecked by communication.

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.

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.

We’ll use the following notation to simplify calculations throughout this section.

NotationMeaning (model parameters)
Ddmodel (the hidden dimension/residual stream dim)
Fdff (the feed-forward dimension)
BBatch dimension (number of tokens in the batch; total, not per-device)
TSequence length
LNumber of layers in the model
NotationMeaning (hardware characteristic)
CFLOPS/s per chip
WNetwork bandwidth (bidirectional, often subscripted as e.g. WiciW_{\text{ici}} or WdcnW_{\text{dcn}})
XNumber of chips along mesh axis X
YNumber of chips along an alternate mesh axis, labeled Y
ZNumber of chips along a third mesh axis, labeled 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 layer

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].

Here's the full algorithm for our little Transformer with no parallelism.

Forward pass: need to compute Loss[B]

  1. Tmp[B, F] = In[B, D] *D Win[D, F]
  2. Out[B, D] = Tmp[B, F] *F Wout[F, D]
  3. Loss[B] = …

Backward pass: need to compute dWout[F, D], dWin[D, F]

  1. dOut[B, D] = …
  2. dWout[F, D] = Tmp[B, F] *B dOut[B, D]
  3. dTmp[B, F] = dOut[B, D] *D Wout[F, D]
  4. dWin[D, F] = In[B, D] *B dTmp[B, F]
  5. dIn[B, D] = dTmp[B, F] *F Win[D, F] (needed for previous layers)

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.

1. Data parallelism: activations sharded along batch, parameters and optimizer state are replicated on each device. Communication only occurs during the backwards pass.

In[BX,D]⋅DWin[D,F]⋅FWout[F,D]→Out[BX,D]\text{In}[B_X, D] \cdot_D W_\text{in}[D, F] \cdot_F W_\text{out}[F, D] \rightarrow \text{Out}[B_X, D]

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.

In[BX,D]⋅DWin[DX,F]⋅FWout[F,DX]→Out[BX,D]\text{In}[B_X, D] \cdot_D W_\text{in}[D_X, F] \cdot_F W_\text{out}[F, D_X] \rightarrow \text{Out}[B_X, D]

3. Tensor parallelism (also called Megatron sharding or model parallelism): activations sharded along D (dmodeld_\text{model}), parameters sharded along F (dffd_{ff}). AllGather and ReduceScatter activations before and after each block. Compatible with FSDP.

In[B,DY]⋅DWin[D,FY]⋅FWout[FY,D]→Out[B,DY]\text{In}[B, D_Y] \cdot_D W_\text{in}[D, F_Y] \cdot_F W_\text{out}[F_Y, D] \rightarrow \text{Out}[B, D_Y]

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:

In[LZ,B,D][i]⋅DWin[LZ,D,F][i]⋅FWout[LZ,F,D][i]→Out[LZ,B,D][i]\text{In}[L_Z, B, D][i] \cdot_D W_\text{in}[L_Z, D, F][i] \cdot_F W_\text{out}[L_Z, F, D][i] \rightarrow \text{Out}[L_Z, B, D][i]

Data Parallelism

Syntax: In[BX,D]⋅DWin[D,F]⋅FWout[F,D]→Out[BX,D]\text{In}[B_X, D] \cdot_D W_\text{in}[D, F] \cdot_F W_\text{out}[F, D] \rightarrow \text{Out}[B_X, D]

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.

data parallelism

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.

Here's the full algorithm for the forward and backwards pass. We abuse notation to write dL/dOut as dOut, purely for compactness.

Pure Data Parallelism Algorithm:

Forward pass: need to compute Loss[BX]

  1. Tmp[BX, F] = In[BX, D] *D Win[D, F]
  2. Out[BX, D] = Tmp[BX, F] *F Wout[F, D]
  3. Loss[BX] = …

Backward pass: need to compute dWout[F, D], dWin[D, F]

  1. dOut[BX, D] = …
  2. dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
  3. dWout[F, D] = AllReduce(dWout[F, D] {UX}) (not on critical path, can be done async)
  4. dTmp[BX, F] = dOut[BX, D] *D Wout[F, D]
  5. dWin[D, F] {UX} = In[BX, D] *B dTmp[BX, F]
  6. dWin[D, F] = AllReduce(dWin[D, F] {UX}) (not on critical path, can be done async)
  7. dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (needed for previous layers)

We ignore the details of the loss function and abbreviate Tmp=Win⋅In\text{Tmp} = W_\text{in} \cdot \text{In}. 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.

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.

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 TPU memory/10\text{TPU memory} / 10 parameters, so e.g. on a TPUv5p chip with 96GB of HBM and pure data parallelism this is about 9B parameters.

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 2DF2DF (for bf16 weights). When does data parallelism make us communication bound?

As in the table above, let CC = per-chip FLOPs, WiciW_{\text{ici}} = bidirectional network bandwidth, and XX = number of shards across which the batch is partitioned4. Let’s calculate the time required to perform the relevant matmuls, TmathT_\text{math}, and the required communication time TcommsT_\text{comms}. Since this parallelism scheme requires no communication in the forward pass, we only need to calculate these quantities for the backwards pass.

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 WiciW_\text{ici}; specifically the AllReduce time is 2⋅total bytes/Wici2 \cdot \text{total bytes} / W_\text{ici}. Since we need to AllReduce for both WinW_\text{in} and WoutW_\text{out}, we have 2 AllReduces per layer. Each AllReduce is for a weight matrix, i.e. an array of DFDF parameters, or 2DF2DF bytes. Putting this all together, the total time for the AllReduce in a single layer is

Tcomms=2⋅2⋅2⋅D⋅FWici.\begin{align} T_\text{comms} &= \frac{2 \cdot 2 \cdot 2 \cdot D \cdot F}{W_\text{ici}}. \\ \end{align}

Matmul time: Each layer comprises two matmuls in the forward pass, or four matmuls in the backwards pass, each of which requires 2(B/X)DF2(B/X)DF FLOPs. Thus, for a single layer in the backward pass, we have

Tmath=2⋅2⋅2⋅B⋅D⋅FX⋅C\begin{align} T_\text{math} &= \frac{2 \cdot 2 \cdot 2 \cdot B \cdot D \cdot F}{X \cdot C} \\ \end{align}

Since we overlap, the total time per layer is the max of these two quantities:

T≈max⁡(8⋅B⋅D⋅FX⋅C,8⋅D⋅FWici)T≈8⋅D⋅F⋅max⁡(BX⋅C,1Wici)\begin{aligned} T &\approx \max(\frac{8 \cdot B \cdot D \cdot F}{X \cdot C}, \frac{8 \cdot D \cdot F}{W_\text{ici}}) \\ T &\approx 8 \cdot D \cdot F \cdot \max(\frac{B}{X \cdot C}, \frac{1}{W_\text{ici}}) \end{aligned}

We become compute-bound when Tmath/Tcomms>1T_\text{math}/T_\text{comms} > 1, or when

BX>CWici.\begin{align} \frac{B}{X} > \frac{C}{W_\text{ici}}. \end{align}

The upshot is that, to remain compute-bound with data parallelism, we need the per-device batch size B/XB / X to exceed the ICI operational intensity, C/WiciC / W_\text{ici}. 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 B/X>C/WiciB/X > C/W_\text{ici} condition to the single-device compute-bound rule B>240B > 240; 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 B≪F,DB \ll F, D regime) independent of batch size.

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 WiciW_\text{ici} 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!

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: MXM_X (MYM_Y, MZM_Z, etc.) is the number of hardware mesh axes that a given parallelism strategy spans.
  • Effect (bandwidth-bound): Using MM axes provides (≈M\approx M times) aggregate link bandwidth, so collective time scales ∝1/MX\propto 1/M_X.

Fully-Sharded Data Parallelism (FSDP)

Syntax: In[BX,D]⋅DWin[DX,F]⋅FWout[F,DX]→Out[BX,D]\text{In}[B_X, D] \cdot_D W_\text{in}[D_X, F] \cdot_F W_\text{out}[F, D_X] \rightarrow \text{Out}[B_X, D]

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

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.

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.

Here's the full algorithm for FSDP.

Fully-Sharded Data Parallelism (FSDP):

Forward pass: need to compute Loss[BX]

  1. Win[D, F] = AllGather(Win[DX, F]) (not on critical path, can do it during previous layer)
  2. Tmp[BX, F] = In[BX, D] *D Win[D, F] (can throw away Win[D, F] now)
  3. Wout[F, D] = AllGather(Wout[F, DX]) (not on critical path, can do it during previous layer)
  4. Out[BX, D] = Tmp[BX, F] *F Wout[F, D]
  5. Loss[BX] = …

Backward pass: need to compute dWout[F, DX], dWin[DX, F]

  1. dOut[BX, D] = …
  2. dWout[F, D] {UX} = Tmp[BX, F] *B dOut[BX, D]
  3. dWout[F, DX] = ReduceScatter(dWout[F, D] {UX}) (not on critical path, can be done async)
  4. Wout[F, D] = AllGather(Wout[F, DX]) (can be done ahead of time)
  5. dTmp[BX, F] = dOut[BX, D] *D Wout[F, D] (can throw away Wout[F, D] here)
  6. dWin[D,F] {UX} = In[BX, D] *B dTmp[BX, F]
  7. dWin[DX, F] = ReduceScatter(dWin[D, F] {UX}) (not on critical path, can be done async)
  8. Win[D, F] = AllGather(Win[DX, F]) (can be done ahead of time)
  9. dIn[BX, D] = dTmp[BX, F] *F Win[D, F] (needed for previous layers) (can throw away Win[D, F] here)

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.

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.

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:

Tmath=2⋅2⋅B⋅D⋅FX⋅CTcomms=2⋅2⋅D⋅FWiciT≈max⁡(4⋅B⋅D⋅FX⋅C,4⋅D⋅FWici)T≈4⋅D⋅F⋅max⁡(BX⋅C,1Wici)\begin{aligned} T_\text{math} &= \frac{2 \cdot 2 \cdot B \cdot D \cdot F}{X \cdot C} \\ T_\text{comms} &= \frac{2 \cdot 2 \cdot D \cdot F}{W_\text{ici}} \\ T &\approx \max\left(\frac{4 \cdot B \cdot D \cdot F}{X \cdot C}, \frac{4 \cdot D \cdot F}{W_\text{ici}}\right) \\ T &\approx 4 \cdot D \cdot F \cdot \max\left(\frac{B}{X \cdot C}, \frac{1}{W_\text{ici}}\right) \end{aligned}

Therefore, as with pure data-parallelism, we are compute bound when B/X>C/WiciB / X > C / W_\text{ici}, i.e. when the per-device batch size B/XB/X exceeds the “ICI operational intensity” C/WiciC/W_\text{ici} (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.

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.

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.

Tensor Parallelism

Syntax: In[B,DY]⋅DWin[D,FY]⋅FWout[FY,D]→Out[B,DY]\text{In}[B, D_Y] \cdot_D W_\text{in}[D, F_Y] \cdot_F W_\text{out}[F_Y, D] \rightarrow \text{Out}[B, D_Y] (we use YY to eventually combine with 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:

model parallelism

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.

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.

Here's the algorithm for tensor parallelism!

Tensor Parallelism:

Forward pass: need to compute Loss[B]

  1. In[B, D] = AllGather(In[B, DY]) (on critical path)
  2. Tmp[B, FY] = In[B, D] *D Win[D, FY] (not sharded along contracting, so no comms)
  3. Out[B, D] {UY} = Tmp[B, FY] *F Wout[FY, D]
  4. Out[B, DY] = ReduceScatter(Out[B, D] {UY}) (on critical path)
  5. Loss[B] = …

Backward pass: need to compute dWout[FY, D], dWin[D, FY]

  1. dOut[B, DY] = …
  2. dOut[B, D] = AllGather(dOut[B, DY]) (on critical path)
  3. dWout[FY, D] = Tmp[B, FY] *B dOut[B, D]
  4. dTmp[B, FY] = dOut[B, D] *D Wout[FY, D] (can throw away dOut[B, D] here)
  5. In[B, D] = AllGather(In[B, DY]) (this can be skipped by sharing with (1) from the forward pass)
  6. dWin[D, FY] = In[B, D] *B dTmp[B, FY]
  7. dIn[B, D] {UY} = dTmp[B, FY] *F Win[D, FY] (needed for previous layers)
  8. dIn[B, DY] = ReduceScatter(dIn[B, D] {UY}) (on critical path)

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.

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.

Tmath=4⋅B⋅D⋅FY⋅CTcomms=2⋅2⋅(B⋅D)WiciT≈max⁡(4⋅B⋅D⋅FY⋅C,2⋅2⋅(B⋅D)Wici)\begin{align} T_\text{math} & = \frac{4 \cdot B \cdot D \cdot F}{Y \cdot C} \\ T_\text{comms} & = \frac{2 \cdot 2 \cdot (B \cdot D)}{W_\text{ici}}\\ \textnormal{T} & \approx \max \left(\frac{4 \cdot B \cdot D \cdot F}{Y \cdot C}, \frac{2 \cdot 2 \cdot (B \cdot D)}{W_\text{ici}}\right) \end{align}

Noting that we want compute cost to be greater than comms cost, we get:

4⋅B⋅D⋅FY⋅C>2⋅2⋅(B⋅D)Wici\begin{align} \frac{4 \cdot B \cdot D \cdot F}{Y \cdot C} > \frac{2 \cdot 2 \cdot (B \cdot D)}{W_\text{ici}} \end{align} FY⋅C>1Wici\begin{align} \frac{F}{Y \cdot C} > \frac{1}{W_\text{ici}} \end{align} F>Y⋅CWici\begin{align} F > Y \cdot \frac{C}{W_\text{ici}} \end{align}

Thus for instance, for TPUv5p, C/Wici=2550C / W_{ici} = 2550 in bf16, so we can only do tensor parallelism up to Y<F/2550Y < F / 2550. When we have multiple ICI axes, our TcommsT_\text{comms} is reduced by a factor of MYM_Y, so we get Y<MY⋅F/2550Y < M_Y \cdot F / 2550.

Note that this doesn’t depend on the precision of the computation, since e.g. for int8, on TPUv5p, Cint8/WiciC_\text{int8} / W_{ici} is 51005100 instead of 25502550 but the comms volume is also halved, so the two factors of two cancel.

Let’s think about some examples:

  • On TPUv5p with LLaMA 3-70B with D=8192,D = 8192, F≈30,000F \approx 30,000, 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.

  • For Gemma 7B, F≈50kF \approx 50k, so we become communication bound with 19-way tensor parallelism. That means we could likely do 16-way and still see good performance.

Combining FSDP and Tensor Parallelism

Syntax: In[BX,DY]⋅DWin[DX,FY]⋅FWout[FY,DX]→Out[BX,DY]\text{In}[B_X, D_Y] \cdot_D W_\text{in}[D_X, F_Y] \cdot_F W_\text{out}[F_Y, D_X] \rightarrow \text{Out}[B_X, D_Y]

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.

mixed fsdp model parallelism

Figure: a diagram combining FSDP and tensor parallelism. Unlike the other cases, there is no duplication of model parameters.

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!

Forward pass: need to compute Loss[B]

  1. In[BX, D] = AllGatherY(In[BX, DY]) (on critical path)
  2. Win[D, FY] = AllGatherX(Win[DX, FY]) (can be done ahead of time)
  3. Tmp[BX, FY] = In[BX, D] *D Win[D, FY]
  4. Wout[FY, D] = AllGatherX(Wout[FY, DX]) (can be done ahead of time)
  5. Out[BX, D] {UY} = Tmp[BX, FY] *F Wout[FY, D]
  6. Out[BX, DY] = ReduceScatterY(Out[BX, D] {UY}) (on critical path)
  7. Loss[BX] = …

Backward pass: need to compute dWout[FY, DX], dWin[DX, FY]

  1. dOut[BX, DY] = …
  2. dOut[BX, D] = AllGatherY(dOut[BX, DY]) (on critical path)
  3. dWout[FY, D] {UX} = Tmp[BX, FY] *B dOut[BX, D]
  4. dWout[FY, DX] = ReduceScatterX(dWout[FY, D] {UX})
  5. Wout[FY, D] = AllGatherX(Wout[FY, DX]) (can be done ahead of time)
  6. dTmp[BX, FY] = dOut[BX, D] *D Wout[FY, D] (can throw away dOut[B, D] here)
  7. In[BX, D] = AllGatherY(In[BX, DY]) (not on critical path + this can be shared with (2) from the previous layer)
  8. dWin[D, FY] {UX} = In[BX, D] *B dTmp[BX, FY]
  9. dWin[DX, FY] = ReduceScatterX(dWin[D, FY] {UX})
  10. Win[D, FY] = AllGatherX(Win[DX, FY]) (can be done ahead of time)
  11. dIn[BX, D] {UY} = dTmp[BX, FY] *F Win[D, FY] (needed for previous layers)
  12. dIn[BX, DY] = ReduceScatterY(dIn[BX, D] {UY}) (on critical path)

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.

  • Tensor parallelism performs AllGatherY([BX,DY])\mathbf{AllGather}_Y([B_X, D_Y]) which shrinks as XX grows.
  • FSDP performs AllGatherX([DX,FY])\mathbf{AllGather}_X([D_X, F_Y]) which shrinks as YY grows.

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:

Let XX be the number of chips dedicated to FSDP and YY be the number of chips dedicated to tensor parallelism. Let NN be the total number of chips in our slice with N=XYN=XY. Let MXM_X and MYM_Y 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

TFSDP comms(B,X,Y)=2⋅2⋅D⋅FY⋅Wici⋅MXT_\text{FSDP comms}(B, X, Y) = \frac{2\cdot 2\cdot D \cdot F}{Y \cdot W_\text{ici} \cdot M_X} TTP comms(B,X,Y)=2⋅2⋅B⋅DX⋅Wici⋅MYT_\text{TP comms}(B, X, Y) = \frac{2 \cdot 2 \cdot B \cdot D}{X \cdot W_\text{ici} \cdot M_Y}

And likewise our total FLOPs time is

Tmath=2⋅2⋅B⋅D⋅FN⋅C.T_\text{math} = \frac{2\cdot 2 \cdot B \cdot D \cdot F}{N \cdot C}.

To simplify the analysis, we make two assumptions: first, we allow XX and YY to take on non-integer values (as long as they are positive and satisfy XY=NXY=N); second, we assume that we can fully overlap comms on the XX and YY axis with each other. Under the second assumption, the total comms time is

Tcomms=max⁡(TFSDP comms,TTP comms)T_\text{comms} = \max\left(T_\text{FSDP comms}, T_\text{TP comms}\right)

Before we ask under what conditions we’ll be compute-bound, let’s find the optimal values for XX and YY to minimize our total communication. Since our FLOPs is independent of XX and YY, the optimal settings are those that simply minimize comms. To do this, let’s write TcommsT_\text{comms} above in terms of XX and NN (which is held fixed, as it’s the number of chips in our system) rather than XX and YY:

Tcomms(X)=4DWicimax⁡(F⋅XN⋅MX,BX⋅MY)T_\text{comms} (X) = \frac{4D}{W_\text{ici}} \max\left(\frac{F \cdot X}{N \cdot M_X}, \frac{B}{X \cdot M_Y}\right)

Because TFSDP commsT_\text{FSDP comms} is monotonically increasing in XX, and TTP commsT_\text{TP comms} is monotonically decreasing in XX, the maximum must be minimized when TFSDP comms=TTP commsT_\text{FSDP comms} = T_\text{TP comms}, which occurs when

FXoptMX=BNXoptMY→Xopt=BFMXMYN\begin{align*} \frac{FX_{opt}}{M_X} = \frac{BN}{X_{opt} M_Y} \rightarrow \\ X_{opt} = \sqrt{\frac{B}{F} \frac{M_X}{M_Y} N} \end{align*}

This is super useful! This tells us, for a given BB, FF, and NN, what amount of FSDP is optimal. Let’s get a sense of scale. Plugging in realistic values, namely N=64N = 64 (corresponding to a 4x4x4 array of chips), B=48,000B=48,000, F=32768F=32768, gives roughly X≈13.9X\approx 13.9. So we would choose XX to be 16 and YY to be 4, close to our calculated optimum.

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

max⁡(TFSDP comms,TTP comms)<Tmath\max\left(T_\text{FSDP comms}, T_\text{TP comms}\right) < T_\text{math}

By letting α≡C/Wici\alpha \equiv C / W_\text{ici}, the ICI arithmetic intensity, we can simplify:

max⁡(FY⋅MX,BX⋅MY)<B⋅FN⋅α\max\left(\frac{F}{Y \cdot M_X}, \frac{B}{X \cdot M_Y}\right) < \frac{B \cdot F}{N \cdot \alpha}

Since we calculated XoptX_{opt} to make the LHS maximum equal, we can just plug it into either side (noting that Yopt=N/XoptY_{opt} = N/X_{opt}), i.e.

FN⋅Wici⋅MXBFMXMYN<B⋅FN⋅C\frac{F}{N \cdot W_\text{ici} \cdot M_X} \sqrt{\frac{B}{F} \frac{M_X}{M_Y} N} < \frac{B \cdot F}{N \cdot C}

Further simplifying, we find that

B⋅FMX⋅MY⋅N<B⋅FN⋅α,\sqrt{\frac{B\cdot F}{M_X \cdot M_Y \cdot N}} < \frac{B \cdot F}{N \cdot \alpha},

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:

TmathTcomms=BFMXMYαN.\frac{T_\text{math}}{T_\text{comms}} = \frac{\sqrt{BF}\sqrt{M_X M_Y}}{\alpha \sqrt{N}}.

To ensure that this ratio is greater than one so we are compute bound, we require

BN>α2MXMYF\frac{B}{N} > \frac{\alpha^2}{M_X M_Y F}

To get approximate numbers, again plug in F=32,768F=32,768, α=2550\alpha=2550, and MXMY=2M_X M_Y=2 (as it must be for a 3D mesh). This gives roughly B/N>99B/N > 99. 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 B/NB/N must exceed about 850850 to be compute bound.

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.

mixed fsdp comms 2

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 B\sqrt{B}, and FSDP scales with BB. However, in intermediate batch size regimes, only FSDP + TP achieves a ratio greater than unity.

Here’s another example of TPU v5p 16x16x16 showing the FLOPs and comms time as a function of batch size for different sharding schemes.

math comms time

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.

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.

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 (D=8192D=8192, F=32768F=32768) as a function of batch size, using TFSDP commsT_\text{FSDP comms}, TTP commsT_\text{TP comms} and TmathT_\text{math} from above. Drag the slider to change the FSDP/TP split. Any batch size in the shaded region is comms-bound.

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.

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:

  1. Initialize your data on TPU 0 with your weights sharded across the layer dimension (Win[LZ,DX,FY]W_\text{in}[L_Z, D_X, F_Y] for pipelining with FSDP and tensor parallelism).
  2. Perform the first layer on TPU 0, then copy the resulting activations to TPU 1, and repeat until you get to the last TPU.
  3. Compute the loss function and its derivative ∂L/∂xL\partial L / \partial x_L.
  4. For the last pipeline stage, compute the derivatives ∂L/∂WL\partial L / \partial W_L and ∂L/∂xL−1\partial L / \partial x_{L-1}, then copy ∂L/∂xL−1\partial L / \partial x_{L-1} to the previous pipeline stage and repeat until you reach TPU 0.
Here is some (working) Python pseudo-code

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.

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.

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.

A second approach is to carefully overlap the forward matmul Wi@xiW_i @ x_i, the backward dxdx matmul Wi@∂L/∂xi+1W_i @ \partial L / \partial x_{i+1}, and the dWdW matmul ∂L/∂xi+1@xi\partial L / \partial x_{i+1} @ x_i. 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:

deepseek pipeline

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.

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.

Scaling Across Pods

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.

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 NN be the number of TPUs we want to scale to and MM 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):

Tmath=2⋅2⋅2⋅BDFN⋅CT_\text{math} = \frac{2 \cdot 2 \cdot 2 \cdot BDF}{N \cdot C} Tcomms=2⋅2⋅2⋅DFM⋅WdcnT_\text{comms} = \frac{2 \cdot 2 \cdot 2 \cdot DF}{M \cdot W_\text{dcn}}

The comms bandwidth scales with MM, since unlike ICI the total bandwidth grows as we grow our ICI domain and acquire more NICs. Simplifying, we find that Tmath>TcommsT_\text{math} > T_\text{comms} when

Bslice>CWdcn\frac{B}{\text{slice}} > \frac{C}{W_\text{dcn}}

For TPU v5p, the CWdcn\frac{C}{W_\text{dcn}} 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.

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 F≈30,000F\approx 30,000. From the above sections, we know the following:

  • We can do Tensor Parallelism up to Y=MY⋅F/2550≈11⋅MYY = M_Y \cdot F / 2550 \approx 11 \cdot M_Y.
  • We can do FSDP so long as B/N>2550/MXB / N > 2550 / M_X. That means if we want to train with BS=2M and 3 axes of data parallelism, we’d at most be able to use ≈2400\approx 2400 chips, roughly a quarter of a TPU v5p pod.
  • When we combine FSDP + Tensor Parallelism, become comms-bound when we have B/N<25502/(2⋅30000)=108B / N < 2550^2 / (2 \cdot 30000) = 108, 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.

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 73,440\text{73,440}, 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).

Takeaways from LLM Training on TPUs

  • 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.

  • 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).

StrategyDescription
Data ParallelismActivations are batch sharded, everything else is fully-replicated, we all-reduce gradients during the backward pass.
FSDPActivations, weights, and optimizer are batch sharded, weights are gathered just before use, gradients are reduce-scattered.
Tensor Parallelism (aka Megatron, Model)Activations are sharded along dmodeld_\text{model}, weights are sharded along dffd_{ff}, activations are gathered before Win, the result reduce-scattered after Wout.
Mixed FSDP + Tensor ParallelismBoth of the above, where FSDP gathers the model sharded weights.

And here are the “formulas” for each method:

StrategyFormulaDPIn[BX,D]⋅DWin[D,F]⋅FWout[F,D]→Out[BX,D]FSDPIn[BX,D]⋅DWin[DX,F]⋅FWout[F,DX]→Out[BX,D]TPIn[B,DY]⋅DWin[D,FY]⋅FWout[FY,D]→Out[B,DY]TP + FSDPIn[BX,DY]⋅DWin[DX,FY]⋅FWout[FY,DX]→Out[BX,DY]\small \begin{array}{cc} \text{Strategy} & \text{Formula}\\ \hline \text{DP} & \text{In}[B_X, D] \cdot_D W_\text{in}[D, F] \cdot_F W_\text{out}[F, D] \rightarrow \text{Out}[B_X, D] \\ \text{FSDP} & \text{In}[B_X, D] \cdot_D W_\text{in}[D_X, F] \cdot_F W_\text{out}[F, D_X] \rightarrow \text{Out}[B_X, D] \\ \text{TP} & \text{In}[B, D_Y] \cdot_D W_\text{in}[D, F_Y] \cdot_F W_\text{out}[F_Y, D] \rightarrow \text{Out}[B, D_Y] \\ \text{TP + FSDP} & \text{In}[B_X, D_Y] \cdot_D W_\text{in}[D_X, F_Y] \cdot_F W_\text{out}[F_Y, D_X] \rightarrow \text{Out}[B_X, D_Y] \\ \hline \end{array}
  • 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 XX is FSDP and YY is tensor parallelism.
StrategyCompute per layerComms per layer(ignoring gating einsum)(bytes, forward + backward pass)DP4BDF/X+8BDF/X0+8DFFSDP4BDF/X+8BDF/X4DF+8DFTP4BDF/Y+8BDF/Y4BD+4BDFSDP + TP4BDF/(XY)+8BDF/(XY)(4BD/X+4DF/Y)+(8BD/X+8DF/Y)\small \begin{array}{ccc} \text{Strategy} & \text{Compute per layer} & \text{Comms per layer} \\ & \text{(ignoring gating einsum)} & \text{(bytes, forward + backward pass)}\\ \hline \text{DP} & 4BDF/X + 8BDF/X & 0 + 8DF \\ \text{FSDP} & 4BDF/X + 8BDF/X & 4DF + 8DF \\ \text{TP} & 4BDF/Y + 8BDF/Y & 4BD + 4BD \\ \text{FSDP + TP} & 4BDF/(XY) + 8BDF/(XY) & (4BD/X + 4DF/Y) + (8BD/X + 8DF/Y) \\ \hline \end{array}
  • 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.

  • Data parallelism and FSDP become comms bound when the batch size per shard<C/W\text{batch size per shard} < C / W, 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.

  • Tensor parallelism becomes comms bound when ∣Y∣>F/2550\lvert Y\rvert > F / 2550. This is around 8-16 way for most models. This is independent of the batch size.

  • Mixed FSDP + tensor parallelism allows us to drop the batch size to as low as 25502/2F≈1002550^2 / 2F \approx 100. This is remarkably low.

  • Data parallelism across pods requires a minimum batch size per pod of roughly 71,000 before becoming DCN-bound.

  • 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.

Some Problems to Work

Let’s use LLaMA-2 13B as a basic model for this section. Here are the model details:

hyperparamvalue
L40
D5,120
F13824
N40
K40
H128
V32,000

LLaMA-2 has separate embedding and output matrices and a gated MLP block.

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.

Click here for the answer.
  • FFW parameters: 3LDF3LDF = 8.5e9
  • Attention parameters: 4DNHL4DNHL = 4.2e9
  • Vocabulary parameters: 2VD2VD = 0.33e9
  • Total: 8.5e9 + 4.2e9 + 0.33e9 = 13.0e9, as expected!

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).

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 BFBF and after the last one BDBD (per the Transformer diagram above), so the total memory for bf16 is 2⋅L⋅(BD+2∗BF)=2LB⋅(D+2F)2 \cdot L \cdot (BD + 2 * BF) = 2LB \cdot (D + 2F) or 2 * 40 * 16e6 * 5,120 * (1 + 2 * 2.7) ~ 4.2e13 = 42TB, since B=16e6. All other activations are more or less negligible.

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.

  1. Can we use pure data parallelism? Why or why not?
  2. 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).
  3. Can we use mixed FSDP + tensor parallelism? Why or why not? If so, what should XX and YY 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?
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.

  1. 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. Let’s start by looking purely at memory. Replacing BS=16M with 3M in Q2, we get ~7.86e12 total checkpoint activations, and with the 1.3e11 optimizer state this brings us to almost exactly 8e12 = 8TB. The TPUv5p slice has 393TB of 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 of 850 * 4096 = 3.48M tokens. 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.

  3. 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 25502/2F=2352550^2 / 2F = 235 here. That means we can in theory do this! Let’s figure out how much of each.

We have the rule Xopt=(B/F)⋅(MX/MY)⋅NX_{opt} = \sqrt{(B / F) \cdot (M_X / M_Y) \cdot N}, 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.

That’s it for Part 5! For Part 6, which applies this content to real LLaMA models, click here!

Appendix

Appendix A: Deriving the backward pass comms

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?

This follows fairly naturally from the rule in the previous section for a single matmul Y = X * A:

dLdA=dLdYdYdA=XT(dLdY)\frac{dL}{dA} = \frac{dL}{dY}\frac{dY}{dA} = X^T \left(\frac{dL}{dY}\right) dLdX=dLdYdYdX=(dLdY)AT\frac{dL}{dX} = \frac{dL}{dY}\frac{dY}{dX} = \left(\frac{dL}{dY}\right) A^T

Using this, we get the following formulas (letting Tmp[B, F] stand for In[B, D] * Win[D, F]):

  1. dWout[F, D] = Tmp[B, F] *B dOut[B, D]
  2. dTmp[B, F] = dOut[B, D] *D Wout[F, D]
  3. dWin[D, F] = In[B, D] *B dTmp[B, F]
  4. 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.

References

  1. ZeRO: Memory optimizations toward training Trillion parameter models. Rajbhandari, Samyam and Rasley, Jeff and Ruwase, Olatunji and He, Yuxiong. 2019.
  2. 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.
  3. 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.

BibTeX

Footnotes

  1. 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. ↩

  2. 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 = 10 bytes per parameters. ↩

  3. 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. ↩

  4. We assume this partitioning is done over an ICI mesh, so the relevant network bandwidth is WiciW_\text{ici} ↩

  5. 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. ↩

Miscellaneous

*Work done at Google DeepMind, now at MatX.

Citation

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:

@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}
}
Original chapter and discussion