06.02 · Walkthrough · Free
Tensor Parallelism
Split a matrix multiply across devices column-wise then row-wise, account for the all-reduce every layer pays, and explain why TP stops being worth it outside one NVLink domain.
Curated for this lesson
The Fleet
Tensor Parallelism
The speaker begins 'So, let me talk about tensor parallelism,' explains splitting each layer's matrix by columns with all-gather/reduce-scatter communication every layer, and concludes that 'tensor parallelism happens within a node on NVLink' because 'there's a lot of communication.'
Tensor parallelism splits each layer’s matrix work across accelerators, usually column-wise for expansion projections and row-wise for contraction projections. It saves weight memory and local matmul time, but introduces activation collectives in every block, so it is mainly useful inside one fast NVLink or NVSwitch domain.
What this lesson answers
- how does tensor parallelism split matrix multiplies
- why does tensor parallelism need all reduce
- when does tensor parallelism stop helping inference
Notes
Tensor parallelism shards the tensors inside one transformer layer across accelerators so a single token’s matmuls are computed cooperatively, with the canonical Megatron-LM scheme from Shoeybi et al., “Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism” (2019). For a linear layer , and , column parallelism partitions along , so device computes and the concatenation needs no immediate reduction. Row parallelism partitions along and , so each device computes and the layer output is , implemented as an all-reduce. In an MLP this pairs naturally: shard the up/gate projections column-wise, keep the expanded activation sharded, then shard the down projection row-wise and all-reduce back to replicated hidden state.
For a Llama-style attention block, TP usually shards Q, K, V, and output projection in the same column-then-row pattern, but grouped-query attention makes the bookkeeping asymmetric. Suppose a 70B model has hidden size , 64 query heads, 8 KV heads, and head dim 128. With tensor-parallel GPUs, each rank owns 8 Q heads and 1 KV head, so the local Q projection has weights while local K and V projections each have weights; attention is computed locally over the local heads, then the output projection consumes the local -wide attention result and contributes a partial -wide residual update that must be all-reduced. vLLM’s PagedAttention paper solved KV-cache paging, not this sharding, but vLLM’s distributed executor uses Megatron-style TP around those per-rank heads; SGLang, TensorRT-LLM, NVIDIA Dynamo, and llm-d expose the same basic “tp size” knob for serving fleets rather than inventing a different algebra.
The cost model is simple enough to do on a whiteboard: TP divides parameter bytes and most matmul FLOPs by , but adds collectives on activation-sized tensors at every block. A ring all-reduce of bytes across ranks transfers bytes per rank, so the communication time is roughly . For decode with batch , hidden , FP16 activations, one residual all-reduce after attention output and one after MLP down projection means bytes each, or MiB per layer before ring overhead. At , ring traffic per all-reduce is MiB per rank; two collectives over 80 layers move MiB per generated token per rank, before latency. On an H100 NVLink/NVSwitch domain with effective hundreds of GB/s per collective, this is tolerable; across 200 Gb/s Ethernet, the same payload alone is about ms per token, ignoring software latency.
The compute-side arithmetic explains why engineers still use TP for oversized models. A 70B FP16 model needs about GB just for weights, so it cannot fit on one 80 GB H100 once KV cache and workspace are included; with the weights become GB per GPU, barely leaving room, and with they are GB. On a single 80 GB H100 at TB/s HBM bandwidth, pure weight streaming for one decode token would take ms if no reuse across batch were possible; with it falls to ms per rank, plus all-reduces. For the earlier , case, each all-reduce moves MiB MiB, two per layer over 80 layers gives MiB per token per rank; at an optimistic GB/s effective NVLink collective bandwidth, payload time is ms, small next to weight bandwidth and GEMM time.
The technique stops paying when the saved local matmul time is smaller than the collective time and launch/synchronization penalty, which happens first outside one NVLink or NVSwitch island and second at small batches or narrow models. With , the all-reduce payload above is only KiB per collective, but 160 collectives per token for an 80-layer model are latency dominated; at even s each, synchronization alone is ms/token, and over TCP/RDMA Ethernet it is often worse and more jittery. With across two 8-GPU nodes, the ring factor barely changes, , but the slowest inter-node hop gates the collective; every layer now waits for the network before the next block can start. TP also becomes awkward when KV heads are fewer than TP ranks: the 8-KV-head example maps cleanly to , but at some ranks cannot own a whole KV head without head splitting, extra gathers, or uneven work, so TensorRT-LLM and vLLM deployments usually prefer pipeline, data, or expert parallelism beyond the NVLink domain.
FlashAttention from Dao et al. attacks the attention kernel’s HBM traffic by tiling QK softmax V, and EAGLE speculative decoding from Li et al. reduces the number of full-model decode steps accepted per emitted token; neither removes the Megatron all-reduce created by row-parallel projections. PagedAttention in vLLM reduces KV fragmentation and enables high-throughput continuous batching, but its block table is per-rank when TP shards heads. SGLang’s RadixAttention/prefix caching, TensorRT-LLM’s fused collectives and in-flight batching, NVIDIA Dynamo’s disaggregated serving runtime, and llm-d’s Kubernetes-oriented distributed inference stack all have to place TP groups as topology-aware units for the same reason: the model layer graph contains a hard collective edge after attention output and after MLP down projection. A correct fleet plan therefore treats TP as an intra-node memory-and-bandwidth tool, not as a general way to smear one request over arbitrary machines.
Common questions
- What is tensor parallelism in inference?
- Tensor parallelism shards the tensors inside a model layer across multiple accelerators. Instead of assigning whole layers to different devices, each device computes part of the same matrix multiply for the same token. The partial results are then concatenated or reduced, depending on how the weight matrix was split.
- Why does tensor parallelism require an all-reduce?
- A row-wise split gives each device only part of the input features and matching weights. Each device produces a partial output for the same hidden state, and those partial outputs must be summed to form the real layer result. That sum is implemented as an all-reduce, and it appears repeatedly through the network.
- Why is tensor parallelism usually kept within one node?
- Tensor parallelism puts collective communication on the critical path of every block. Within an NVLink or NVSwitch island, the bandwidth and latency can make that acceptable. Across ordinary inter-node networking, the synchronisation and data movement often cost more than the matmul time saved by adding more devices.
Short definition: what is Tensor Parallelism?
