Splitting a Large Transformer: Intra-Layer vs Inter-Layer Sharding
Machine LearningModel answerLast updated
Contrast sharding weights within each transformer layer against assigning consecutive layers to different devices, covering communication, bubbles, memory, and inference latency.
You have a transformer language model that does not fit on a single accelerator. Two common ways to spread it across devices are to shard the weight matrices inside each layer (tensor parallelism) or to assign contiguous groups of layers to different devices (pipeline parallelism).
Write up a comparison that covers:
- What exactly is partitioned in each scheme, using a transformer block (attention plus MLP) as the running example.
- What communication each scheme needs, how often it happens, and how it depends on the interconnect.
- How each behaves in training versus autoregressive inference, including utilization, idle time, memory, and latency.
- When you would combine them, and what else you might add.
No model size, cluster topology, or throughput target is given, so state your assumptions explicitly. Do not claim that one approach is universally faster. Before answering, say which clarifying questions you would ask, for example: is the bottleneck weights, optimizer state, or activations? What are the link bandwidths, batch size, sequence length, and latency budget?
Hints
Hint 1
Think about the MLP block: if you split the first matrix by columns and the second by rows, how many synchronization points per block do you need, and what kind of collective is it?
Hint 2
For pipelines, count the idle fraction. With p stages and m microbatches, the bubble is roughly (p-1)/(m+p-1) of the schedule. Ask what this means for small batches and for single-token decoding.
Hint 3
Match each scheme to the link it tolerates: frequent, latency-sensitive collectives want fast intra-node links, while infrequent point-to-point activation transfers can cross slower inter-node links.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Clarify first
I would ask what actually fails to fit: weights, optimizer state (Adam typically adds about 8 bytes/param in fp32 moments plus master weights), or activations. I would also ask about link bandwidth (NVLink inside a node versus InfiniBand or Ethernet across nodes), batch size, sequence length, and whether the target is training throughput or serving latency. The right answer depends on these.
What is partitioned
Tensor parallelism (TP) splits the matrices inside every layer across t devices.
- MLP: split the first projection by columns, so each device computes its slice of the hidden activation independently (the nonlinearity is elementwise). Split the second projection by rows, so each device produces a partial sum of the output.
- Attention: split by heads. Each device holds a subset of the Q, K, V projections and its share of the output projection (row-split).
- Every device processes the same tokens, but holds only about 1/t of each layer's weights, and also 1/t of the KV cache in inference.
Pipeline parallelism (PP) splits the model by depth. With p stages, stage k holds layers [k·L/p, (k+1)·L/p). Each device holds full layers but only a fraction of them. Embeddings go on the first stage and the output head and loss on the last.
Communication
TP: The row-split matmuls produce partial sums that must be combined, so each block needs an all-reduce in the forward pass for attention and one for the MLP, and matching ones in the backward pass. That is about 4 all-reduces per layer per training step (2 forward, 2 backward). Each moves an activation tensor of size batch × seq × hidden. These are on the critical path and cannot be hidden easily, so TP needs high bandwidth and low latency, which in practice means staying inside one NVLink-connected node (typically t ≤ 8). Sequence parallelism replaces the all-reduce with reduce-scatter plus all-gather and also shards layer-norm and dropout activations.
PP: Only the activation tensor at a stage boundary is sent (point-to-point), forward, and its gradient backward. That is one transfer per microbatch per boundary, far less volume than TP. This tolerates slower inter-node links well.
Training behavior
TP keeps all devices busy on the same layer at the same time, so there is no bubble, but a fraction of time is spent in collectives. Efficiency drops as t grows or links slow down. It reduces per-device weights, gradients, optimizer state, and (with sequence parallelism) activations.
PP has a pipeline bubble. With p stages and m microbatches under a GPipe-style schedule, the idle fraction is about (p−1)/(m+p−1). For p=8, m=32 that is roughly 18%; for m=8 it is about 47%. Remedies:
- Use many microbatches (limited by global batch size and convergence).
- Use 1F1B scheduling, which has the same bubble but caps in-flight activations to about
pmicrobatches instead ofm, cutting memory. - Use interleaved (virtual) stages, which shrink the bubble by a factor of the number of chunks per device at the cost of more communication.
- Balance stages carefully, since the slowest stage sets the pace. Embeddings and the loss head make the first and last stages uneven.
PP reduces weight and optimizer memory per device by about 1/p, but early stages hold more in-flight activations. Activation recomputation is commonly paired with it.
Inference behavior
Autoregressive decoding generates one token at a time and is typically memory-bandwidth bound.
- TP reduces latency per token, because each device reads only 1/t of the weights per step. The cost is two all-reduces per layer on tiny tensors, so latency (not bandwidth) of the collective dominates. Gains flatten as t grows.
- PP does not reduce single-request latency. A token still passes through all layers sequentially, plus stage-to-stage hops. It improves throughput when many requests are in flight, because different stages work on different requests, and it lets you fit a model across nodes. With a single stream or small batch, most stages idle.
- The KV cache is split by heads under TP and by layers under PP.
When to combine
Typical large-scale layout, a 3D (or 4D) decomposition:
- TP within a node (fast links) to cut per-layer memory and compute.
- PP across nodes (slower links) to span depth, since it needs only boundary activations.
- Data parallelism (ideally sharded, like ZeRO/FSDP) across replicas to scale batch size.
- Optionally expert parallelism for MoE and sequence/context parallelism for long contexts.
Example: 175B-class model on 1,024 GPUs with t=8, p=16, data-parallel degree 8. Choose the smallest t and p that make the model fit, then spend the remainder on data parallelism, since it scales most efficiently.
For serving, a common choice is TP across the GPUs of one node, adding PP only if the model exceeds a node, and replicating for throughput.
Trade-off summary
| Aspect | Tensor parallel | Pipeline parallel |
|---|---|---|
| Splits | Weights within a layer | Layers across depth |
| Communication | All-reduce per layer, frequent, large volume | Point-to-point at boundaries, infrequent |
| Link need | Very high bandwidth, low latency | Moderate |
| Idle time | None structural | Bubble ≈ (p−1)/(m+p−1) |
| Single-token latency | Improves | Does not improve |
| Complexity | Model code changes | Scheduling, stage balancing |
Common mistakes
- Saying TP is "faster" or PP is "faster" without reference to the interconnect.
- Pushing TP across nodes over slow links.
- Forgetting the bubble depends on microbatch count, and that decode batches are small.
- Ignoring activation memory and optimizer state when deciding what must be sharded.
- Claiming PP lowers latency for a single inference request.
- Neglecting load imbalance from embedding and head layers.