KatInterview

Hand Estimates for a Sharded GEMM: Compute, Memory and Links

Machine LearningModel answer
MediumModel answerMachine Learningrooflinearithmetic-intensitysharding

Last updated

Count FLOPs, memory bytes and network bytes for a matrix product, apply the roofline model on one device, then compare M, N and K sharding and put numbers on both.

You are asked to predict, using only pen and paper, how quickly a matrix product runs on one accelerator and then on a group of them.

The product is C = A × B, with A of shape M × K and B of shape K × N. Every element takes s bytes (for instance s = 2 for bf16). A device can perform at most P FLOP/s and moves data to and from its memory at W bytes/s. Each device can send and receive L bytes/s on the interconnect.

Work from first principles. Keep separate tallies of arithmetic, memory bytes and network bytes, and use roofline reasoning to say which one limits each setup.

Clarifying questions worth asking (if unanswered, state your own assumptions):

  • Is B a weight matrix that stays on the devices, or are both operands activations?
  • Does C have to be fully present on every device, or may it stay split?
  • Can communication run concurrently with computation?
  • Is the interconnect a ring, a switch or something else?
  • Should on-chip reuse be treated as perfect, or is on-chip memory limited?

Part 1: one device. Derive the FLOP count and the minimum memory traffic, and from them the arithmetic intensity. Use the ridge point of the roofline to decide when the product is compute-bound and when it is memory-bound. Evaluate a large square product and a product with small M (a few tokens against a large weight matrix). Explain why real traffic is higher than the ideal figure.

Part 2: D devices. Split the product along M, along N or along K. For each choice, give the per-device FLOPs, the per-device memory traffic and the collective needed, and name the limiting resource. Derive when K-sharding becomes network-bound, and explain how overlapping communication with compute changes the estimate.

Part 3: concrete numbers. Pick an accelerator and interconnect and state the figures as assumptions. Estimate the time of a specific product under at least two sharding choices, name the bottleneck in each, suggest an improvement, and check the answer against the roofline.

Hints

Hint 1

Keep three separate tallies: FLOPs (2MKN), memory bytes per device, and network bytes per device. Each divided by its rate gives a lower bound on time, and the step cannot beat the largest of them.

Hint 2

On a ring, an all-gather or reduce-scatter of X bytes moves about X(D-1)/D bytes per device, and an all-reduce about twice that. Under K-sharding every device holds a partial result the size of all of C, so network cost depends on M and N, not on K.

Hint 3

To test whether K-sharding is network-bound, divide the network time by the compute time. M, N and one factor of D cancel, leaving a threshold on K that involves only s, P, L and D. Then substitute numbers and carry units carefully.