KatInterview

Cost Model for a Two-Matmul MLP Block Across Devices

System DesignModel answer
HardModel answerSystem Designml-systemstensor-parallelismroofline

Last 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 s bytes.
  • Ignore the arithmetic of act unless it turns out to matter.
  • Treat byte counts as consequences of the assumptions you state, not as universal facts.

What to produce

  1. Single-device costs. Give total FLOPs, parameter bytes, and the bytes of X, the intermediate A = act(X·W1) with shape [B, F], and Y.
  2. 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.
  3. Partitioned execution on N devices. 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 (W1 by columns, W2 by rows).
    • Splitting the model dimension H.
  4. Bottleneck analysis. Using a compute rate P (FLOP/s), HBM bandwidth M (B/s), link bandwidth L (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.
  5. 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.