KatInterview

Splitting a Large Transformer: Intra-Layer vs Inter-Layer Sharding

Machine LearningModel answer
MediumModel answerMachine Learningdistributed-trainingtensor-parallelismpipeline-parallelism

Last 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:

  1. What exactly is partitioned in each scheme, using a transformer block (attention plus MLP) as the running example.
  2. What communication each scheme needs, how often it happens, and how it depends on the interconnect.
  3. How each behaves in training versus autoregressive inference, including utilization, idle time, memory, and latency.
  4. 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.