Search

Search papers and lessons

Model Parallelism

Level 3 · 13 min read

Model parallelism splits one neural network across several devices, either inside each layer or by giving each device a run of layers, so a model too big for one device can still be trained or served.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Calculus
  • Linear algebra
  • Probability
  • Optimizers
  • ReadReady to readNot yet

TL;DR

Model parallelism splits one neural network across several devices. Each device holds only part of the weights and does only part of the arithmetic, so a model that is too large for one device can still be trained or served. The two main ways to cut the model are inside a layer (tensor parallelism) and between layers (pipeline parallelism).

Builds on: Data Parallelism, GPU Memory Hierarchy, All-Reduce · Leads to: Megatron-LM, ZeRO, Activation Checkpointing

Where it's used

Training GPT-scale language models. The 175-billion-parameter GPT-3 Transformer model stores about 350 GB of weights in 16-bit numbers (175 billion times 2 bytes). That is several times the memory of an 80 GB GPU, so no single device can even hold the weights. Large runs split each layer across a group of GPUs inside one server, and then split the stack of layers across groups of servers.

The Megatron-LM training runs. The Megatron-LM team (NVIDIA, 2019) trained an 8.3-billion-parameter transformer on 512 GPUs. They split each layer across 8 GPUs that sit in the same server, because those GPUs share a fast link. The paper is the standard reference for tensor parallelism in transformers.

Serving large models. A 70-billion-parameter model in 16-bit precision needs about 140 GB for its weights, which is more than one 80 GB GPU can hold. Inference servers therefore spread the weights over at least two GPUs and pass activations between them for every token generated.

How it works

We will trace one tiny layer, a matrix multiplication, split across two devices. Device 1 and device 2 each hold half of the weight matrix.

Setup: the input is the row vector X=[1,  2]X = [1, \; 2]. The weight matrix is A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}. Without splitting, the output is Y=XAY = XA.

  1. Compute the whole layer on one device as a reference. Y=[1⋅1+2⋅3,  1⋅2+2⋅4]=[7,  10]Y = [1 \cdot 1 + 2 \cdot 3, \; 1 \cdot 2 + 2 \cdot 4] = [7, \; 10]. Check: the answer has one number per column of AA, so two numbers.
  2. Cut AA by columns. Device 1 gets the first column [1,3]T[1, 3]^T and device 2 gets the second column [2,4]T[2, 4]^T. Each device holds half the weights. Check: together they hold all four numbers of AA, and neither holds all of them.
  3. Both devices receive the full input X=[1,2]X = [1, 2], because each needs all of it. Device 1 computes 1⋅1+2⋅3=71 \cdot 1 + 2 \cdot 3 = 7. Device 2 computes 1⋅2+2⋅4=101 \cdot 2 + 2 \cdot 4 = 10. Check: each device did half the multiplications.
  4. Put the pieces side by side. The result is [7,  10][7, \; 10], the same as step 1. No addition across devices was needed here, because each output number came from exactly one device. Check: the gathered result matches the reference.
  5. Now the next layer. Suppose it multiplies by a matrix BB with one column, B=(12)B = \begin{pmatrix} 1 \\ 2 \end{pmatrix}, so the output is Z=YBZ = YB. Cut BB by rows. Device 1 gets row 11 and device 2 gets row 22. Device 1 already holds YY's first entry, 77, and device 2 holds the second, 1010. Each device multiplies only what it has: device 1 computes 7⋅1=77 \cdot 1 = 7, and device 2 computes 10⋅2=2010 \cdot 2 = 20.
  6. The devices now add their partial results with an All-Reduce, which gives every device the sum 7+20=277 + 20 = 27. Check: the unsplit answer is Z=7⋅1+10⋅2=27Z = 7 \cdot 1 + 10 \cdot 2 = 27.

The trick is in steps 4 to 6. The first layer's output was already split between the devices, and the second layer can use that split directly. Only one communication step (the all-reduce) is needed for the pair of layers. This is exactly the pattern Megatron-LM uses inside each Transformer block.

The other way to cut a model is by depth. Each device gets a run of consecutive layers, and the data flows from device to device like a factory line:

flowchart LR
  I[Input batch] --> S1[Stage 1: layers 1 to 4 on GPU 1]
  S1 --> S2[Stage 2: layers 5 to 8 on GPU 2]
  S2 --> S3[Stage 3: layers 9 to 12 on GPU 3]
  S3 --> S4[Stage 4: layers 13 to 16 on GPU 4]
  S4 --> O[Output]

Each stage waits for the one before it, so the devices sit idle while the pipeline fills and drains. The fix is to cut the batch into small micro-batches and keep several in flight, which the math section quantifies.

The math

Definition: Tensor-parallel linear layers

For a layer Y=XAY = XA, split the weight matrix into tt column blocks, A=[A1  A2  ⋯  At]A = [A_1 \; A_2 \; \cdots \; A_t], one block per device. Device ii computes Yi=XAiY_i = X A_i, and the full output is the concatenation Y=[Y1  Y2  ⋯  Yt]Y = [Y_1 \; Y_2 \; \cdots \; Y_t]. For a following layer Z=YBZ = YB, split BB into tt row blocks, B=[B1;B2;⋯ ;Bt]B = [B_1; B_2; \cdots; B_t], so that Z=∑i=1tYiBiZ = \sum_{i=1}^{t} Y_i B_i. Each device computes one term, and the sum is an All-Reduce.

Step 1 in symbols

Here XX is the input row vector, AA is the weight matrix, and tt is the number of devices sharing one layer. Device ii stores only AiA_i, which is 1/t1/t of the columns of AA, so it stores 1/t1/t of the weights of this layer.

Step 2 in symbols

With X=[1,  2]X = [1, \; 2] and A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, the column blocks are A1=(1,3)TA_1 = (1, 3)^T and A2=(2,4)TA_2 = (2, 4)^T. Then Y1=XA1=1⋅1+2⋅3=7Y_1 = X A_1 = 1 \cdot 1 + 2 \cdot 3 = 7 and Y2=XA2=1⋅2+2⋅4=10Y_2 = X A_2 = 1 \cdot 2 + 2 \cdot 4 = 10. Concatenating gives Y=[7,  10]Y = [7, \; 10], which equals XAXA computed on one device. No communication happens in this step.

Step 5 and 6 in symbols

Here B=(1,2)TB = (1, 2)^T is split into row blocks B1=1B_1 = 1 and B2=2B_2 = 2. Each device computes YiBiY_i B_i: device 1 gets 7⋅1=77 \cdot 1 = 7 and device 2 gets 10⋅2=2010 \cdot 2 = 20. The all-reduce computes Z=7+20=27Z = 7 + 20 = 27, which is YBYB.

Memory per device

If a model has PP parameters stored at bb bytes each, and it is split into tt tensor-parallel ways and pp pipeline stages, each device stores about

Mdevice=P⋅bt⋅pM_{\text{device}} = \frac{P \cdot b}{t \cdot p}

bytes of weights. The product t⋅pt \cdot p is the number of devices that share one copy of the model. This counts weights only. Gradients, optimizer state and activations add more, so the true requirement is larger.

The pipeline bubble

With pp pipeline stages and mm equal micro-batches, the first stage starts on micro-batch 1 and the last stage sits idle until the first micro-batch reaches it. Counting the idle slots in an ideal schedule gives a fraction of time spent idle, the bubble,

bubble=p−1m+p−1.\text{bubble} = \frac{p - 1}{m + p - 1}.

The bubble shrinks as mm grows, because the fixed ramp-up of p−1p - 1 slots is spread over more useful work.

10203040506000.20.40.60.8micro-batches midle fraction
  • 4 stages
  • 8 stages
More micro-batches shrink the idle fraction. With 8 stages and 16 micro-batches, 7 of 23 slots are idle, about 30 percent.

Worked examples

Example
Splitting a 2 by 2 layer across two devices
  1. Input X=[1,  2]X = [1, \; 2], weights A=(1234)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}.
  2. Unsplit reference: the first output is 1⋅1+2⋅3=71 \cdot 1 + 2 \cdot 3 = 7, and the second is 1⋅2+2⋅4=101 \cdot 2 + 2 \cdot 4 = 10. So Y=[7,  10]Y = [7, \; 10].
  3. Device 1 holds column (1,3)T(1, 3)^T. It computes 1⋅1+2⋅3=71 \cdot 1 + 2 \cdot 3 = 7.
  4. Device 2 holds column (2,4)T(2, 4)^T. It computes 1⋅2+2⋅4=101 \cdot 2 + 2 \cdot 4 = 10.
  5. Gather the two numbers in order. The result is [7,  10][7, \; 10], which matches step 2.

The split changed where the arithmetic ran, not the answer. Each device did two multiplications instead of four.

Example
Fitting a 175-billion-parameter model on 8 GPUs
  1. Weights: P=175×109P = 175 \times 10^9 parameters, stored at b=2b = 2 bytes each. Total: 175×109⋅2=350×109175 \times 10^9 \cdot 2 = 350 \times 10^9 bytes, which is 350 GB.
  2. One 80 GB GPU cannot hold this, so the model must be split. Use t=8t = 8 tensor-parallel ways and p=1p = 1 stage.
  3. Per device: 350 GB/8=43.75350 \text{ GB} / 8 = 43.75 GB of weights.
  4. Compare with the 80 GB limit: 80−43.75=36.2580 - 43.75 = 36.25 GB remain for gradients, optimizer state, activations and buffers.
  5. Interpretation: 8-way tensor parallelism makes the weights fit, but the leftover room is limited. Real systems add more devices or techniques such as activation checkpointing to free memory for the rest.

The answer is only a lower bound on the memory needed, because it counts weights and nothing else.

Example
Choosing the number of micro-batches
  1. A model is split into 8 pipeline stages, so p=8p = 8.
  2. With m=16m = 16 micro-batches, the bubble is 8−116+8−1=723≈0.304\frac{8 - 1}{16 + 8 - 1} = \frac{7}{23} \approx 0.304. About 30 percent of the time is idle.
  3. With m=64m = 64, the bubble is 771≈0.099\frac{7}{71} \approx 0.099, about 10 percent.
  4. Quadrupling the micro-batches cut the idle time by about two thirds. Each micro-batch is smaller, so the work per step drops, but the schedule becomes much fuller.
  5. Interpretation: adding micro-batches helps, but it also means more memory for activations and more steps of overhead.

Try it: a pipeline has 4 stages and uses 12 micro-batches. What fraction of time is idle? Answer: 4−112+4−1=315=0.2\frac{4 - 1}{12 + 4 - 1} = \frac{3}{15} = 0.2, so 20 percent of the time is idle.

Common mistakes and tradeoffs

It is tempting to think that model parallelism always makes training faster. In fact it makes a model possible to run at all, and it adds communication, because the devices must exchange partial results at every layer. This is why tensor parallelism is usually kept inside one server, where GPUs share a fast link, and pipeline parallelism is used across servers, where traffic is less frequent.

It is tempting to think that splitting a model across tt devices divides its memory by tt in every respect. In fact only the weights and the matching optimizer state shrink cleanly. Activations are often the larger cost during training, and they are not divided the same way.

It is tempting to think that more pipeline stages are always better. In fact each extra stage adds to the bubble, unless you also raise the number of micro-batches, as the math shows.

Use model parallelism when the weights do not fit on one device, or when a single layer is too large to run on one device. Do not use it when Data Parallelism already fits: data parallelism is simpler and needs less communication inside each step. Alternatives include ZeRO-style sharding of optimizer state and gradients, which keeps the model whole on each device but spreads the memory-heavy state across them.

History

For most of the early deep learning era, a model could fit on one device, so the question did not arise. In 2012, AlexNet was split across two GTX 580 GPUs, each with 3 GB of memory, because the network did not fit on one card. The split was done by hand and was specific to that network.

Once language models with hundreds of millions and then billions of parameters appeared, the need became general. GPipe (2019) showed how to pipeline a network across devices with micro-batches. Megatron-LM (2019) showed how to split the matrices inside each transformer layer across GPUs in one server, and it trained an 8.3-billion-parameter model on 512 GPUs. ZeRO (2019) took a different route, sharding memory-heavy state across data-parallel workers. These methods caught on because the largest models kept growing faster than the memory of any single device.

Recap

  • A model is split across devices either within a layer (tensor parallelism) or between layers (pipeline parallelism).
  • Splitting a weight matrix by columns needs no communication for that layer. Splitting the next matrix by rows needs one All-Reduce to sum the partial results.
  • Weights per device shrink in proportion to the number of devices that share a copy of the model.
  • Pipeline stages sit idle during ramp-up. The bubble p−1m+p−1\frac{p - 1}{m + p - 1} falls as micro-batches increase.
  • Read next: Megatron-LM for the transformer version, ZeRO for sharding memory instead of the model, and Activation Checkpointing for trading compute for activation memory.

Check yourself

QuestionWhy does the column split of the first layer need no communication, while the row split of the second layer does?Show answer

In the column split, each device computes a different set of output numbers, and those numbers are simply placed side by side. No device needs another device's result to finish its own outputs. In the row split of the second layer, each device produces only a partial sum over the shared output dimension, so the partial sums must be added together, which is what the all-reduce does.

QuestionA model has 70 billion parameters stored in 2 bytes each, split 4 ways tensor-parallel and 2 pipeline stages. How many GB of weights does each device store?Show answer

Total bytes are 70×109⋅2=140×10970 \times 10^9 \cdot 2 = 140 \times 10^9, which is 140 GB. The number of devices sharing one copy is t⋅p=4⋅2=8t \cdot p = 4 \cdot 2 = 8. Per device: 140/8=17.5140 / 8 = 17.5 GB.

QuestionA pipeline with 6 stages uses 6 micro-batches. What is the bubble fraction, and what would it be with 30 micro-batches?Show answer

With 6 micro-batches the bubble is 6−16+6−1=511≈0.455\frac{6 - 1}{6 + 6 - 1} = \frac{5}{11} \approx 0.455. With 30 micro-batches it is 535=17≈0.143\frac{5}{35} = \frac{1}{7} \approx 0.143. Raising the number of micro-batches from 6 to 30 cuts the idle fraction from about 45 percent to about 14 percent.

QuestionWhy is tensor parallelism usually kept inside one server rather than spread across many servers?Show answer

Tensor parallelism needs an all-reduce for every layer, in both the forward and backward pass, so the devices exchange data very often. Inside one server the GPUs share a fast link, and the cost of those exchanges stays small. Across servers the links are slower, and the same frequency of exchange would stall the computation.

QuestionA team wants to train a 13-billion-parameter model in 16-bit precision. Each GPU has 40 GB of memory. The team plans 2-way tensor parallelism and no pipeline. Will the weights fit on each device, and what is left over?Show answer

Total bytes are 13×109⋅2=2613 \times 10^9 \cdot 2 = 26 GB. Split 2 ways, each device stores 26/2=1326 / 2 = 13 GB of weights. That fits in 40 GB, leaving 40−13=2740 - 13 = 27 GB for gradients, optimizer state and activations. Whether that is enough depends on the optimizer and the batch size, so the weights alone do not settle the question.

Sources