Distributed Training: Sharding the Memory
You'll learn to
- -Work out why training needs about 16 bytes per parameter with mixed-precision Adam
- -Explain why plain data parallelism does not reduce memory per GPU
- -Describe the three ZeRO stages and compute memory per GPU for each
- -Explain tensor parallelism as splitting a layer's matrix multiply, and check it on a small example
Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.
Running a model needs its weights. Training one needs far more: the weights, a gradient for every weight, and the optimizer's own bookkeeping. A model that fits on one GPU for inference can be several times too large to train on that same GPU. Distributed training is the set of techniques for splitting that memory, and the computation, across many devices.
An Analogy: A Group Project
Five students write a long report. They could each keep a full copy of the whole document and edit different pages, then merge changes: nobody has less to carry, but the work is shared. Or they could split the document so each student holds only one chapter, and swap pages when someone needs another part. The first is data parallelism. The second is sharding, and it is what lets a document too big for one laptop be written at all.
Where the Memory Goes
The ZeRO paper does the arithmetic for mixed-precision training with the Adam optimizer. For a model with a given number of parameters, the 16-bit weights take 2 bytes each and the 16-bit gradients 2 bytes each. Adam then keeps a 32-bit copy of the weights, a momentum value and a variance value, 4 bytes each, which is 12 bytes. Together that is 2 plus 2 plus 12, or 16 bytes per parameter. A 7 billion parameter model therefore needs about 112 GB for these states alone, before counting activations, so it will not fit on an 80 GB GPU.
Data parallelism gives every GPU a full copy of all of that and a different slice of the data, then averages the gradients. It speeds up training, but, as the ZeRO paper puts it, basic data parallelism does not reduce memory per device. ZeRO's idea is to stop keeping 64 identical copies. It shards the redundant parts across the data-parallel GPUs in three stages: first the optimizer states, then also the gradients, then also the parameters.
For the 7.5 billion parameter example in the paper, on 64 GPUs, the output matches its table: 120 GB with no sharding, 31.4 GB with the optimizer states sharded, 16.6 GB with gradients sharded too and 1.9 GB with everything sharded. All but the first fit in an 80 GB GPU. A 70 billion parameter model on 64 GPUs only fits with everything sharded, at 17.5 GB, and 256 GPUs lowers that to 4.4 GB. These figures count model states only. Activations, the buffers kept for the backward pass, need memory on top.
Splitting a Layer: Tensor Parallelism
ZeRO shards memory across data-parallel GPUs. Model parallelism splits the model itself. Tensor parallelism, as in Megatron-LM, splits the matrices inside each layer, so each GPU holds a slice, with a few communication steps to combine the pieces. The code proves the two ways to split a matrix multiply on a small example: by columns, where the pieces are joined, and by rows, where the pieces are added.
Both splits reproduce the full result exactly, and each of the two devices holds 12 of the 24 weights. The catch is communication: a column split ends with a gather and a row split ends with a sum across devices, and in a real model this happens in every layer. That is why tensor parallelism is kept inside one server, where links are fast, while pipeline parallelism, which places different layers on different devices, and sharded data parallelism handle the traffic between servers.
Real training runs combine all three: data parallelism with sharding, tensor parallelism inside a node, and pipeline parallelism across nodes. When you size a run, start with the memory arithmetic, then measure the network, because the fastest split on paper is often limited by communication.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!