Bookmarks

TP — tensor parallelism

A way of splitting one neural network layer across several GPUs: the layer’s matrices are cut into pieces, each GPU computes its share, and the results are combined through communication.

Also: intra-layer model parallelism, intra-layer parallelism

checked 9 Nov

All terms

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 layerHow it is splitCommunication between GPUs
First MLP multiplicationweights split by columns, activation applied on each GPUnone
Second MLP multiplicationweights split by rowsall-reduce in the forward and backward pass
Attention (Q, K, V)attention heads whole on their own GPUsall-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

bashfour GPUs of a single node
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

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • PPpipeline parallelism

    Pipeline parallelism splits the model by layers into stages and tolerates slow cross-node links; tensor parallelism cuts within a layer and requires a fast bus.

  • DPdata parallelism

    With data parallelism each GPU holds a full copy of the model: what is split is not the model but the batch of examples.

In depth in the references

where the topic is covered in detail

Appears in the references