Cost Model for a Two-Matmul MLP Block Across Devices
System DesignModel answerLast updated
Derive FLOPs, bytes moved, and collective traffic for a plain two-matmul MLP under several partitioning schemes, then decide which hardware limit binds.
Problem
You are sizing a Transformer MLP block for inference and need a back-of-the-envelope cost model before touching any profiler.
The block takes an input X of shape [B, H] and two weight matrices W1 of shape [H, F] and W2 of shape [F, H]. It computes:
Y = act(X · W1) · W2
This is a plain, non-gated MLP, so there is only one up-projection. Use these conventions:
- A multiply-add counts as 2 FLOPs.
- Every stored element takes
sbytes. - Ignore the arithmetic of
actunless it turns out to matter. - Treat byte counts as consequences of the assumptions you state, not as universal facts.
What to produce
- Single-device costs. Give total FLOPs, parameter bytes, and the bytes of
X, the intermediateA = act(X·W1)with shape[B, F], andY. - Memory traffic and arithmetic intensity. State what you assume about whether the intermediate is written to HBM or kept on-chip, and whether weights are cached across calls. Give the resulting intensity in FLOPs per byte.
- Partitioned execution on
Ndevices. For each of the schemes below, give the per-device tensor shapes, per-device FLOPs and weight bytes, which collective is needed (if any), and how many bytes each device sends or receives:- Splitting the batch
B(data parallel, weights replicated). - Splitting the expanded dimension
F(W1by columns,W2by rows). - Splitting the model dimension
H.
- Splitting the batch
- Bottleneck analysis. Using a compute rate
P(FLOP/s), HBM bandwidthM(B/s), link bandwidthL(B/s) and a per-collective latencyα, write a roofline-style lower bound on latency and explain when each resource dominates. Discuss where this bound is too optimistic. - Clarifications. List the questions you would ask before committing to numbers (entry/exit layouts, cache residency, materialization, hardware figures).
A worked numeric example (for instance H = 8192, F = 32768, s = 2, N = 8) is welcome but not required.
Hints
Hint 1
Start with the two matmuls separately: each is B x H x F multiply-adds, so write the total in terms of B, H and F, then separately total the weight bytes. Compare their ratio to see when the block is weight-bound.
Hint 2
For the F-split, W1 columns and W2 rows line up, so the activation never needs to move between devices. Ask what each device holds after the second matmul and which collective turns that into the required output layout.
Hint 3
For the roofline, take the max of compute time, memory time and communication time, then add collective latency terms. Afterwards list what this ignores: overlap imperfection, small-batch inefficiency, ring vs tree algorithms, and cache effects.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
1. Single-device costs
Each matmul performs B·H·F multiply-adds, so:
- FLOPs =
2·B·H·F(first matmul) +2·B·F·H(second) =4·B·H·F. - Weight bytes =
2·H·F·s. - Activation bytes:
XisB·H·s, the intermediateAisB·F·s, andYisB·H·s.
Since F is typically 3-4x H, the intermediate is the largest activation.
2. Memory traffic and arithmetic intensity
State the assumptions explicitly.
- Weights are streamed from HBM once per call (not resident in on-chip cache). Traffic:
2HFs. - Intermediate is fused (kept on-chip): traffic is
2HFs + 2BHs(readX, writeY). - Intermediate is materialized: add a write and a read of
A, i.e.+2BFs.
For the fused case with B ≪ H, traffic is dominated by weights, so:
intensity ≈ 4BHF / (2HFs) = 2B/s FLOPs per byte.
With s = 2 this is about B FLOPs/byte. Compare with the hardware ridge point P/M. If P/M is, say, 300 FLOPs/byte, the block is memory-bound until B reaches roughly 300 tokens. That is why small-batch decode is bandwidth-bound while prefill and large-batch serving are compute-bound.
3. Partitioning across N devices
a) Batch split (data parallel).
- Each device holds
X[B/N, H]and the fullW1,W2. - Per-device FLOPs:
4BHF/N. Per-device weight bytes:2HFs(not reduced). - Communication: none in the forward pass.
- Downside: weight traffic per device is unchanged, so at small
Byou gain nothing and pay N copies of memory.
b) Split along F (tensor parallel, the Megatron-style layout).
W1[H, F/N]by columns,W2[F/N, H]by rows.Xis replicated on entry.- Each device computes
A_i = act(X·W1_i)of shape[B, F/N]. Becauseactis elementwise, no communication is needed here. - Then
Y_i = A_i·W2_iis a partial sum of shape[B, H]. The true output isΣY_i, which requires an all-reduce overB·H·sbytes. - Per-device FLOPs:
4BHF/N. Weight bytes:2HFs/N. - Ring all-reduce traffic per device:
2·(N-1)/N · B·H·s. If the next layer can consume a sharded output, use a reduce-scatter instead, which moves(N-1)/N · B·H·s, and later all-gather only if needed. - Critically, communication scales with
B·H, notF, and does not shrink asNgrows.
c) Split along H.
W1[H/N, F]by rows, withXsharded on its last dim asX[B, H/N]. The first matmul yields partial sums of[B, F], so an all-reduce (or reduce-scatter) overB·F·sbytes is needed before the activation, sinceactis nonlinear and cannot be applied to partials.- That is
F/Htimes more traffic than scheme (b). ThenW2[F, H/N]by columns givesY[B, H/N]already sharded with no further collective. - Use this only when the surrounding layout forces H-sharded activations, and prefer to fix the layout instead.
4. Roofline lower bound
For scheme (b), per device:
T_comp = 4BHF / (N·P)T_mem = (2HFs/N + 2BHs [+ 2BFs/N if materialized]) / MT_comm = 2(N-1)/N · BHs / L + α
Lower bound: T ≥ max(T_comp, T_mem, T_comm); with no overlap, the upper estimate is the sum.
Which dominates:
- Small
B:T_mem(weight streaming) dominates, and sharding helps because weight bytes drop byN. - Large
B:T_compdominates. T_commbecomes decisive whenLis low (cross-node or PCIe links). ComparingT_commtoT_compgives~ (2·P·s)/(4·F·L/N), so communication hurts asNgrows (compute shrinks as1/Nwhile all-reduce bytes do not) and whenFis small relative toP/L.- At very small
B, the latency termαdominates, because tiny messages are latency-bound rather than bandwidth-bound.
Numeric example (H=8192, F=32768, s=2, N=8, B=64): FLOPs = 4·64·8192·32768 ≈ 6.9e10 total, 8.6e9 per device. Weights per device = 2·8192·32768·2/8 ≈ 134 MB. The all-reduce moves about 2·(7/8)·64·8192·2 ≈ 1.8 MB. On a 900 GB/s HBM, weights take about 0.15 ms; on a 400 TFLOP/s device compute takes about 0.02 ms; so this is clearly memory-bound, and the 1.8 MB all-reduce at ~100 GB/s (~0.02 ms plus latency) is small but latency-sensitive.
5. Where the bound is too optimistic
- Compute, memory, and communication rarely overlap perfectly. Real latency is between the max and the sum.
- Achievable FLOPs at small tile sizes are well below peak; achievable bandwidth is below spec.
- Collective cost depends on algorithm (ring vs tree), topology, and message size;
αcan exceed the bandwidth term for small messages. - Ignoring the activation function is fine for GELU-like ops fused into the matmul, but not if it is materialized.
- Padding, uneven shards, and quantization (different
sfor weights vs activations) change the numbers. - Pipeline parallelism avoids all-reduces but adds bubbles and only helps throughput, not single-request latency.
6. Clarifying questions
- What are the input and output layouts (replicated or sharded)? This decides all-reduce vs reduce-scatter.
- Are weights resident in cache or streamed on each call?
- Is
Afused or materialized? - What are
P,M,L,α, and the topology? - What is the batch regime: decode, prefill, or both?
Common mistakes
- Forgetting the factor of 2 per multiply-add or counting only one matmul.
- Claiming F-sharding needs an all-gather of the intermediate; it does not.
- Applying a nonlinearity to partial sums in the H-split.
- Saying communication shrinks with
N; for the all-reduce it stays roughly constant. - Presenting the roofline as a prediction rather than a bound.