Hand Estimates for a Sharded GEMM: Compute, Memory and Links
Machine LearningModel answerLast 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
Ba weight matrix that stays on the devices, or are both operands activations? - Does
Chave 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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Part 1: a single device
Counts
- FLOPs: each of the
MNoutputs needsKmultiplies andKadds, soF = 2MKN. - Minimum traffic: read
AandBonce, writeConce, soBytes_min = s(MK + KN + MN). - Arithmetic intensity:
I = 2MKN / (s(MK + KN + MN)).
Roofline. Time is at least max(F/P, Bytes/W). The ridge point is I* = P/W. Above it the product is compute-bound, below it memory-bound.
- Square,
M = K = N = n:I = 2n/(3s), i.e.n/3for bf16. It grows withn, so big squares are compute-bound. - Small
M, largeKandN:Bdominates the bytes, soBytes_min ≈ sKNandI ≈ 2M/s, aboutMFLOP/byte in bf16. The product stays memory-bound untilMis near the ridge point. Token-by-token generation is therefore limited by how fast weights can be streamed.
Ideal versus real traffic. The ideal count assumes every element crosses the memory interface once. With finite on-chip memory, tiles are re-read, and the extra traffic shrinks roughly in proportion to tile size. Peak P and W are also seldom reached, so assume 60 to 80 percent of them.
Part 2: sharding over D devices
Ring collective cost per device: all-gather or reduce-scatter of X bytes moves about X(D-1)/D; all-reduce moves about 2X(D-1)/D.
| Shard along | FLOPs / device | Memory / device | Communication |
|---|---|---|---|
M (rows of A, C; B replicated) | 2MKN/D | s(MK/D + KN + MN/D) | None if B is resident and C may stay split; otherwise an all-gather |
N (columns of B, C; A replicated) | 2MKN/D | s(MK + KN/D + MN/D) | None if C may stay split; otherwise all-gather of C, sMN(D-1)/D |
K (columns of A, rows of B) | 2MKN/D | s(MK/D + KN/D + MN) | Full-size partial C on each device: reduce-scatter sMN(D-1)/D, all-reduce double that |
Compute divides evenly in every case, so the options differ in memory and network. M- and N-sharding can need no communication but replicate one operand. K-sharding splits both inputs but must reduce the whole output.
When is K-sharding network-bound? Compare sMN(D-1)/(D·L) against 2MKN/(D·P). Cancelling M, N and one D:
- Reduce-scatter: network-bound when
K < sP(D-1)/(2L). - All-reduce: network-bound when
K < sP(D-1)/L.
The threshold depends only on the ratio P/L and on D. Since P/L is typically in the thousands, K-sharding pays off only when the contraction dimension is very large.
Overlap. Without overlap, T ≈ T_compute + T_network. With ideal pipelining (chunk the product so the collective for chunk i runs while chunk i+1 computes), T ≈ max(T_compute, T_memory, T_network). Real systems land in between because of launch latency and contention for memory bandwidth.
Part 3: numbers
Assume bf16 (s = 2), P = 400 TFLOP/s, W = 1.6 TB/s, L = 100 GB/s per device and D = 8. Then the ridge point is 400e12 / 1.6e12 = 250 FLOP/byte and P/L = 4000.
Case A: M = 16384, K = N = 8192.
F = 2^41 ≈ 2.2e12. Ideal bytes ≈ 671 MB, soI ≈ 3277, far above 250: compute-bound, about 5.5 ms on one device.- Shard
M(weights resident): compute 0.69 ms; memory ≈ 201 MB, i.e. 0.13 ms; no communication. Compute-bound at ≈ 0.69 ms, an ideal 8x speedup. - Shard
K(reduce-scatter): compute 0.69 ms, memory ≈ 319 MB (0.2 ms), network2 · 134.2M · 7/8 ≈ 235 MB, i.e. 2.35 ms. Network-bound and about 3.4x slower thanM-sharding. This matches the threshold:K = 8192is below2 · 4e14 · 7 / (2 · 1e11) = 28000, and28000/8192 ≈ 3.4. - Improvement: prefer
MorNsharding. If memory capacity forcesK-sharding, use hybrid sharding with a smallerKgroup, overlap the reduction with chunked compute, or add link bandwidth.
Case B (decode-like): M = 32, K = N = 8192.
F ≈ 4.3e9, so compute is 10.7 µs. Bytes ≈ 135 MB gives 84 µs andI ≈ 32 ≪ 250. On one device it is memory-bound at about 85 µs.- Shard
N: per-device bytes ≈ 17.4 MB, 10.9 µs; compute only 1.3 µs. Memory-bound at ≈ 11 µs. Gathering the 0.52 MB output moves about 0.46 MB, adding 4.6 µs unless hidden by overlap. - Shard
K: the same 10.9 µs of memory time, plus a reduction: 4.6 µs for reduce-scatter or 9.2 µs for all-reduce. Up to ≈ 20 µs without overlap. - Takeaway: at small
Mthe weight read dominates, so splitting the weights speeds things up through memory bandwidth.N-sharding is preferable because its communication is just a small output gather.
Sanity check. Each estimate is at least the largest single-resource bound and agrees with the roofline. Applying a 70 percent efficiency factor to P and W raises times by roughly 1.4x. In Case B, per-hop latency of a few microseconds is comparable to the transfer time, so the network figures there are optimistic.
Common mistakes
- Using
2MKNFLOPs but forgetting the factor of two when counting multiply plus add. - Mixing bits, bytes and elements, or GB/s and GiB/s.
- Taking communication volume as the whole tensor instead of the
(D-1)/Dfraction, or forgetting that all-reduce costs twice a reduce-scatter. - Concluding "compute-bound" from the single-device ridge point after sharding, when per-device intensity and network terms have changed.
- Assuming perfect overlap without checking that memory bandwidth is not shared by the collective.