Level 3 — ProductionScaling & Efficient Architectures

Scaling & Efficient Architectures

45 minutes60 marksprintable — key stays hidden on paper

Time limit: 45 minutes Total marks: 60 Instructions: Show all derivations. Where code is requested, write it from memory (pseudocode acceptable if semantically correct). Use ...... for math.


Q1. Chinchilla compute-optimal derivation (12 marks)

The Chinchilla loss model is L(N,D)=E+ANα+BDβL(N,D) = E + \frac{A}{N^{\alpha}} + \frac{B}{D^{\beta}} where NN = parameters, DD = training tokens, and total compute is approximated by C6NDC \approx 6ND.

(a) Set up the constrained optimization to minimize LL subject to fixed compute C=6NDC=6ND, and derive the relation between the optimal NN^\star and DD^\star (express the ratio of the two variable loss terms at the optimum). (6)

(b) Show that NCaN^\star \propto C^{a} and DCbD^\star \propto C^{b} and give a,ba,b in terms of α,β\alpha,\beta. Verify a+b=1a+b=1. (4)

(c) For the Chinchilla fitted values α=β=0.34\alpha=\beta=0.34, state the numeric exponents a,ba,b and explain in one sentence what this means for scaling data vs parameters. (2)


Q2. MoE routing & load balancing (12 marks)

(a) Write from memory the top-kk gating computation for a token xx over EE experts: the router logits, softmax, top-kk selection, and the combined output. (5)

(b) Define the auxiliary load-balancing loss used in Switch Transformer style routing. Give the formula with fif_i (fraction of tokens dispatched to expert ii) and PiP_i (mean routing probability to expert ii), and explain why both terms are needed rather than penalizing fif_i alone. (5)

(c) A batch has 4 tokens routed (top-1) to experts with these dispatch fractions f=[0.5,0.25,0.25,0]f=[0.5,0.25,0.25,0] and mean probs P=[0.4,0.3,0.2,0.1]P=[0.4,0.3,0.2,0.1] over E=4E=4 experts. Compute the aux loss Laux=αEifiPi\mathcal{L}_{aux}=\alpha E \sum_i f_i P_i with α=1\alpha=1. (2)


Q3. KV-cache memory & speculative decoding (12 marks)

(a) Derive the KV-cache memory (in bytes) for a decoder with LL layers, hidden dim dd, nn heads, sequence length SS, batch BB, stored in fp16. State the formula and explain each factor. (4)

(b) A model has L=32L=32, d=4096d=4096, S=8192S=8192, B=1B=1, fp16. Compute the KV-cache size in GB. (3)

(c) Explain speculative decoding: how a small draft model and large target model interact, and state the accept/reject rule that guarantees the output distribution equals the target model's distribution. (5)


Q4. Explain-out-loud: parallelism & sharding (12 marks)

Answer concisely (2–4 sentences each):

(a) Contrast tensor parallelism vs pipeline parallelism — what dimension of the model is split, and what is the main communication cost of each. (4)

(b) Explain the three ZeRO stages and what each shards across data-parallel ranks. (4)

(c) FSDP: what happens to a layer's parameters in the forward pass (all-gather / free) and why this trades communication for memory. (4)


Q5. State-space models (7 marks)

(a) Write the continuous linear SSM equations and the discretized recurrence. State why the recurrence can also be computed as a convolution, giving the kernel form. (4)

(b) In one sentence each: what is the key innovation of Mamba (selective SSM) over S4, and why does it break the simple convolution view? (3)


Q6. Emergent abilities (5 marks)

Define "emergent ability" in the scaling context. Give one concrete example, and state the main critique (from the "Are emergent abilities a mirage?" argument) about why some emergence may be an artifact of the chosen metric. (5)

Answer keyMark scheme & solutions

Q1 (12 marks)

(a) Minimize L=E+ANα+BDβL=E+A N^{-\alpha}+B D^{-\beta} s.t. C=6NDC=6ND fixed. (1) Substitute D=C/(6N)D=C/(6N): L(N)=E+ANα+B(6N/C)β.L(N)=E+A N^{-\alpha}+B(6N/C)^{\beta}. (1) Differentiate and set to 0: dLdN=αANα1+βB(6/C)βNβ1=0.\frac{dL}{dN}=-\alpha A N^{-\alpha-1}+\beta B (6/C)^\beta N^{\beta-1}=0. (2) So αANα=βBDβ\alpha A N^{-\alpha}=\beta B D^{-\beta}, i.e. at the optimum αANα=βBDβ.\boxed{\alpha \cdot \frac{A}{N^\star{}^{\alpha}} = \beta \cdot \frac{B}{D^\star{}^{\beta}}}. (2) (The two variable loss terms are balanced in proportion α:β\alpha:\beta.)

(b) From the balance: ANαBDβA N^{-\alpha}\propto B D^{-\beta} with D=C/(6N)D=C/(6N) gives Nα(C/N)β=CβNβN^{-\alpha}\propto (C/N)^{-\beta}=C^{-\beta}N^{\beta}, so Nα+βCβN^{\alpha+\beta}\propto C^{\beta}: (2) NCβα+β,DCαα+β.N^\star\propto C^{\frac{\beta}{\alpha+\beta}},\qquad D^\star\propto C^{\frac{\alpha}{\alpha+\beta}}. (1) Thus a=βα+β, b=αα+βa=\frac{\beta}{\alpha+\beta},\ b=\frac{\alpha}{\alpha+\beta}, and a+b=α+βα+β=1a+b=\frac{\alpha+\beta}{\alpha+\beta}=1. ✓ (1)

(c) With α=β=0.34\alpha=\beta=0.34: a=b=0.5a=b=0.5. (1) Meaning: parameters and tokens should be scaled equally with compute — roughly double data whenever you double model size (the Chinchilla correction to earlier param-heavy scaling). (1)


Q2 (12 marks)

(a) For token xRdx\in\mathbb{R}^d, router weights WrRE×dW_r\in\mathbb{R}^{E\times d}: (1)

  • logits h=Wrxh=W_r x (1)
  • gate probs g=softmax(h)g=\mathrm{softmax}(h), gi=ehijehjg_i=\frac{e^{h_i}}{\sum_j e^{h_j}} (1)
  • top-kk: keep set T=TopK(g)\mathcal{T}=\text{TopK}(g); renormalize g~i=gi/jTgj\tilde g_i=g_i/\sum_{j\in\mathcal{T}}g_j (1)
  • output y=iTg~iEi(x)y=\sum_{i\in\mathcal{T}}\tilde g_i\, E_i(x) where EiE_i is expert ii's FFN. (1)

(b) Laux=αEi=1EfiPi\mathcal{L}_{aux}=\alpha\, E\sum_{i=1}^{E} f_i P_i (2) where fi=1Bx1[argmaxg(x)=i]f_i=\frac{1}{|B|}\sum_{x}\mathbf{1}[\arg\max g(x)=i] (dispatch fraction, non-differentiable) and Pi=1Bxgi(x)P_i=\frac{1}{|B|}\sum_x g_i(x) (mean soft prob, differentiable). (1) Why both: fif_i carries the true load but has no gradient (it's an argmax count); PiP_i is the differentiable surrogate. Multiplying fiPif_i P_i makes overloaded experts receive gradient pressure to lower their routing probability. Penalizing fif_i alone gives no gradient; penalizing PiP_i alone ignores actual dispatch imbalance. (2)

(c) fiPi=0.5(0.4)+0.25(0.3)+0.25(0.2)+0=0.2+0.075+0.05=0.325\sum f_i P_i = 0.5(0.4)+0.25(0.3)+0.25(0.2)+0 = 0.2+0.075+0.05=0.325. (1) Laux=140.325=1.3\mathcal{L}_{aux}=1\cdot4\cdot0.325=1.3. (1)


Q3 (12 marks)

(a) KV-cache stores K and V for every layer, every token, every head: bytes=2×B×L×S×d×(bytes/elem).\text{bytes}=2 \times B \times L \times S \times d \times (\text{bytes/elem}). (2) Factor 2 = keys + values; d=ndheadd=n\cdot d_{head} so summing over heads = full hidden dim; fp16 = 2 bytes. (2) (So bytes=22BLSd=4BLSd\text{bytes}=2\cdot2\cdot B L S d = 4BLSd for fp16.)

(b) =4×1×32×8192×4096=4 \times 1 \times 32 \times 8192 \times 4096 bytes =4×32×8192×4096=4,294,967,296=4\times32\times8192\times4096 = 4,294,967,296 bytes =4=4 GB (using 1GB=2301\text{GB}=2^{30}). (3)

(c) Speculative decoding: a cheap draft model autoregressively proposes kk tokens; the target model then scores all kk in a single parallel forward pass. (2) Accept/reject rule: for proposed token with draft prob q(x)q(x) and target prob p(x)p(x), accept with probability min ⁣(1,p(x)q(x))\min\!\left(1,\frac{p(x)}{q(x)}\right). (2) On the first rejection, resample from the normalized residual distribution max(0,p(x)q(x))xmax(0,pq)\frac{\max(0,p(x)-q(x))}{\sum_x\max(0,p-q)}; this makes the marginal output distribution exactly the target's, while amortizing the target's cost over several tokens. (1)


Q4 (12 marks)

(a) Tensor parallelism splits the model within a layer — e.g. weight matrices sharded across the hidden/head dimension; requires an all-reduce per layer (high-bandwidth, intra-node). Pipeline parallelism splits across layers into stages; main cost is pipeline bubble/idle time and point-to-point activation transfers between stages. (4) (2 each)

(b) ZeRO-1 shards optimizer states; ZeRO-2 additionally shards gradients; ZeRO-3 additionally shards parameters — each rank holds only its slice and gathers others on demand. (4)

(c) In FSDP, each rank stores only a shard of a layer's params. Before the forward of that layer, an all-gather reconstructs the full weights; after use they are freed (re-gathered again in backward). This holds full weights only momentarily, cutting memory to 1/N\sim1/N, at the cost of extra all-gather communication each pass. (4)


Q5 (7 marks)

(a) Continuous: h(t)=Ah(t)+Bx(t), y(t)=Ch(t)+Dx(t)h'(t)=Ah(t)+Bx(t),\ y(t)=Ch(t)+Dx(t). (1) Discretized (step Δ\Delta): ht=Aˉht1+Bˉxt, yt=Chth_t=\bar A h_{t-1}+\bar B x_t,\ y_t=C h_t with Aˉ=eΔA\bar A=e^{\Delta A}, Bˉ=(ΔA)1(eΔAI)ΔB\bar B=(\Delta A)^{-1}(e^{\Delta A}-I)\Delta B. (1) Because A,B,CA,B,C are time-invariant, unrolling gives yt=j0CAˉjBˉxtjy_t=\sum_{j\ge0} C\bar A^{j}\bar B\, x_{t-j}, a convolution with kernel (1) Kˉ=(CBˉ, CAˉBˉ, CAˉ2Bˉ,).\bar K=(C\bar B,\ C\bar A\bar B,\ C\bar A^2\bar B,\dots). (1)

(b) Mamba makes B,C,ΔB,C,\Delta input-dependent (selective), so the model can gate/forget based on content. (1.5) This makes the SSM time-varying, so Aˉj\bar A^j no longer factors into a fixed kernel — the convolution view breaks, and Mamba instead uses a hardware-aware parallel scan. (1.5)


Q6 (5 marks)

An emergent ability is a capability absent in smaller models that appears (often sharply) once scale crosses a threshold, not predictable by smooth extrapolation from small models. (2) Example: few-shot arithmetic / multi-step reasoning / instruction following appearing only past some parameter count. (1) Critique (Schaeffer et al., "mirage"): apparent sharp emergence can be an artifact of discontinuous/nonlinear metrics (e.g. exact-match accuracy that is 0 until the whole answer is right); switching to a smooth metric (token edit distance, log-likelihood) shows steady, predictable improvement — the ability was scaling continuously all along. (2)


[
  {"claim":"Chinchilla optimal exponents a=b=0.5 for alpha=beta=0.34, and a+b=1",
   "code":"alpha=Rational(34,100); beta=Rational(34,100); a=beta/(alpha+beta); b=alpha/(alpha+beta); result=(a==Rational(1,2)) and (b==Rational(1,2)) and (a+b==1)"},
  {"claim":"MoE aux loss = 1.3 for given f,P,E=4,alpha=1",
   "code":"f=[Rational(1,2),Rational(1,4),Rational(1,4),0]; P=[Rational(4,10),Rational(3,10),Rational(2,10),Rational(1,10)]; E=4; alpha=1; L=alpha*E*sum(fi*Pi for fi,Pi in zip(f,P)); result=(L==Rational(13,10))"},
  {"claim":"KV cache = 4 GB for L=32,d=4096,S=8192,B=1,fp16 (bytes=4*B*L*S*d)",
   "code":"B=1; L=32; S=8192; d=4096; by=4*B*L*S*d; result=(by==2**32) and (by/2**30==4)"}
]