When a model does not fit in the memory of a single GPU, it is split not only by layers but also within one layer: tensor parallelism divides the weight matrices between GPUs, which combine the intermediate results through communication after each layer.
How a layer is split
The approach was introduced in 2019 in the Megatron-LM paper, where it was called “intra-layer model parallelism”; the name “tensor parallelism” took hold later. A transformer layer is mostly matrix multiplications, and Megatron-LM cuts them so that communication is kept to a minimum. The first multiplication of the MLP block is split by columns: a GPU holds its own strip of weights, multiplies the whole input by it, and applies the activation function right away. The second multiplication is split by rows, so the GPUs combine the output of the block with a single all-reduce. In the attention block the strips of weights are the heads: each attention head lives entirely on its own GPU, and the output layer is again split by rows.
| Part of the layer | How it is split | Communication between GPUs |
|---|---|---|
| First MLP multiplication | weights split by columns, activation applied on each GPU | none |
| Second MLP multiplication | weights split by rows | all-reduce in the forward and backward pass |
| Attention (Q, K, V) | attention heads whole on their own GPUs | all-reduce in the forward and backward pass |
In total a layer takes two all-reduces in the forward pass and two in the backward one; everything else the GPUs compute without communication. The implementation needs no new compiler — in Megatron-LM it is a few communication operations on top of ordinary PyTorch, and the collective communication is handled by NCCL.
The cost: communication at every layer
The more GPUs in the group, the more expensive each all-reduce becomes, so tensor parallelism is used where GPUs are connected by a fast bus — within a single server. In the Megatron-LM experiments the GPUs inside a DGX-2H server were connected by NVSwitch with a bandwidth of 300 GB/s, and the servers among themselves by InfiniBand at 100 GB/s across eight adapters. The authors advise keeping tensor parallelism within a node: if a model does not fit in one server, pipeline parallelism is added between nodes.
vLLM explicitly recommends pipeline parallelism instead of tensor parallelism on GPUs without NVLink, such as the L40S: tensor parallelism communicating over a slow bus eats up the gains from the split. Cross-node tensor parallelism requires a fast network such as InfiniBand; the NCCL logs show whether communication goes through GPUDirect RDMA or through slow TCP sockets.
How to enable it
vllm serve facebook/opt-13b \ --tensor-parallel-size 4--tensor-parallel-size sets the number of GPUs per node, --pipeline-parallel-size the number of nodes; the tensor algorithm in vLLM comes from Megatron-LM. If the model size is not evenly divisible by the number of GPUs, vLLM recommends pipeline parallelism — it can handle uneven pieces. In training, tensor parallelism is combined with data parallelism: the 8.3-billion-parameter model in the Megatron-LM paper was trained on 512 GPUs — eight GPUs in a tensor group within a server and 64 such groups, with a scaling efficiency of 76%.
Sources
the term is written from these pages- Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism (arXiv:1909.08053)arxiv.org/abs/1909.08053(opens in a new tab)
- Efficient Large-Scale Language Model Training on GPU Clusters Using Megatron-LM (arXiv:2104.04473)arxiv.org/abs/2104.04473(opens in a new tab)
- vLLM — Distributed inference and servingdocs.vllm.ai/en/latest/serving/parallelism_scaling.html(opens in a new tab)
Verified 9 Nov