Level 5 — MasteryScaling & Efficient Architectures

Scaling & Efficient Architectures

2 minutes100 marksprintable — key stays hidden on paper

Time limit: 2 hours 30 minutes Total marks: 100 Instructions: Answer ALL three questions. Show full derivations. Calculators/handwritten pseudocode permitted. Partial credit awarded for correct reasoning.


Question 1 — Chinchilla Compute-Optimal Allocation (36 marks)

The Chinchilla parametric loss law is

L(N,D)=E+ANα+BDβL(N, D) = E + \frac{A}{N^{\alpha}} + \frac{B}{D^{\beta}}

where NN is parameter count, DD is training tokens, and E,A,B,α,β>0E, A, B, \alpha, \beta > 0. Training FLOPs are approximated by C=6NDC = 6ND.

(a) [8] Using a Lagrange multiplier, derive the compute-optimal allocation: minimize L(N,D)L(N,D) subject to 6ND=C6ND = C. Show that at the optimum

αANα=βBDβ.\alpha \frac{A}{N^{\alpha}} = \beta \frac{B}{D^{\beta}}.

(b) [8] Hence show that the optimal parameter count scales as NoptCaN_{\text{opt}} \propto C^{a} and tokens as DoptCbD_{\text{opt}} \propto C^{b}, and derive the exponents a,ba, b in terms of α,β\alpha, \beta. Verify a+b=1a + b = 1 and explain why this must hold.

(c) [6] For the published Chinchilla estimates α=0.34\alpha = 0.34, β=0.28\beta = 0.28, compute aa and bb numerically to 3 decimal places. Comment on whether they are approximately equal and what practical guideline ("tokens per parameter") this supports.

(d) [8] A lab has trained a model with N1=70N_1 = 70B parameters on D1=1.4D_1 = 1.4T tokens. They obtain a compute budget increase of 10×10\times. Using the exponents from (c), by what factor should NN and DD each grow to stay compute-optimal? Give numeric factors.

(e) [6] Critically discuss one assumption in the C=6NDC = 6ND FLOP model that breaks for a Mixture-of-Experts model with EE experts and top-kk routing. Give the corrected effective active-parameter count and its effect on the scaling calculation.


Question 2 — Mixture-of-Experts Routing & Load Balancing (32 marks)

Consider an MoE layer with EE experts and a top-1 gating network. For token xix_i the router produces logits g(xi)REg(x_i) \in \mathbb{R}^{E} and softmax probabilities pi=softmax(g(xi))p_i = \text{softmax}(g(x_i)). A batch has TT tokens.

(a) [6] Define fef_e = fraction of tokens routed to expert ee, and Pe=1Tipi,eP_e = \frac{1}{T}\sum_i p_{i,e} = mean router probability mass on expert ee. Write the Switch-Transformer auxiliary load-balancing loss Laux=αauxEe=1EfePeL_{\text{aux}} = \alpha_{\text{aux}} \cdot E \sum_{e=1}^{E} f_e P_e. Prove that LauxL_{\text{aux}} is minimized (over the simplex) when the load is uniform, and state its minimum value.

(b) [8] Prove that efePe1E\sum_e f_e P_e \geq \frac{1}{E} with equality iff fe=Pe=1/Ef_e = P_e = 1/E for all ee. (Hint: relate to a Cauchy–Schwarz / variance argument, noting fe=Pe=1\sum f_e = \sum P_e = 1.)

(c) [8] With top-1 routing over E=8E=8 experts and T=4096T=4096 tokens, an expert-capacity C=(T/E)cfC = \lceil (T/E)\cdot c_f\rceil with capacity factor cf=1.25c_f = 1.25 is enforced. Compute the capacity per expert. If one expert receives 900 tokens, how many are dropped (overflow)? Explain the throughput vs. quality tradeoff of raising cfc_f.

(d) [10] Write pseudocode (any language) for a top-1 gating forward pass that (i) computes routing, (ii) applies capacity with token dropping, and (iii) returns the combined output and the auxiliary loss. Annotate the tensor shapes at each step for a batch of TT tokens, model dim dd, and EE experts.


Question 3 — State-Space Models & Speculative Decoding (32 marks)

Part A — SSM recurrence (16 marks)

A discrete linear state-space layer (S4/Mamba-style) obeys

ht=Aˉht1+Bˉxt,yt=Chth_t = \bar{A}\, h_{t-1} + \bar{B}\, x_t, \qquad y_t = C\, h_t

with state htRnh_t \in \mathbb{R}^n and scalar input xtx_t.

(a) [6] By unrolling the recurrence from h0=0h_0 = 0, show that yt=j=1tCAˉtjBˉxjy_t = \sum_{j=1}^{t} C \bar{A}^{\,t-j}\bar{B}\, x_j, and identify the convolution kernel Kˉ=(CBˉ,CAˉBˉ,)\bar{K} = (C\bar{B},\, C\bar{A}\bar{B},\, \dots). Explain how this yields the parallel (convolutional) training mode vs. the sequential (recurrent) inference mode.

(b) [6] Given the continuous parameter AA (scalar, A<0A<0) and step Δ\Delta, the Zero-Order-Hold discretization gives Aˉ=exp(ΔA)\bar{A} = \exp(\Delta A) and Bˉ=(Aˉ1)A1B\bar{B} = (\bar{A}-1)A^{-1} B. For A=0.5A = -0.5, Δ=0.1\Delta = 0.1, B=1B = 1, compute Aˉ\bar{A} and Bˉ\bar{B} to 4 decimals. State the stability condition on Aˉ\bar A for a bounded output and confirm it holds.

(c) [4] Contrast the inference-time memory and compute per token of this SSM against a standard attention layer with a KV-cache, as sequence length LL \to \infty. Give asymptotic (O()O(\cdot)) expressions for both.

Part B — Speculative decoding (16 marks)

A small draft model proposes γ\gamma tokens per step; the large target model verifies them in parallel. Let β\beta = per-token acceptance probability (assume i.i.d. for tractability).

(d) [8] Show that the expected number of tokens accepted per verification step (including the one bonus/corrected token) is

E[tokens]=1βγ+11β.\mathbb{E}[\text{tokens}] = \frac{1 - \beta^{\gamma+1}}{1 - \beta}.

Derive this from the geometric structure of accepting a prefix.

(e) [8] Let cc = cost ratio (draft-forward-pass cost / target-forward-pass cost per step), with one target verification pass per step. The expected speedup over vanilla autoregressive decoding is

S=E[tokens]1+cγ.S = \frac{\mathbb{E}[\text{tokens}]}{1 + c\gamma}.

For β=0.8\beta = 0.8, γ=4\gamma = 4, c=0.2c = 0.2, compute SS to 3 decimals. State whether speculative decoding is beneficial here and identify the regime in β\beta where it fails to help.


Answer keyMark scheme & solutions

Question 1

(a) [8] Lagrangian L=E+ANα+BDβ+λ(6NDC)\mathcal{L} = E + AN^{-\alpha} + BD^{-\beta} + \lambda(6ND - C). Stationarity:

  • N:αANα1+6λD=0\partial_N: -\alpha A N^{-\alpha-1} + 6\lambda D = 0αANα=6λND\alpha A N^{-\alpha} = 6\lambda ND [2]
  • D:βBDβ1+6λN=0\partial_D: -\beta B D^{-\beta-1} + 6\lambda N = 0βBDβ=6λND\beta B D^{-\beta} = 6\lambda ND [2]

Right-hand sides equal (=6λC=6\lambda C), so αANα=βBDβ. [4]\alpha A N^{-\alpha} = \beta B D^{-\beta}. \quad\checkmark \ [4] Why: both marginal loss reductions must balance the same shadow cost of compute.

(b) [8] From (a): ANα=βαBDβA N^{-\alpha} = \frac{\beta}{\alpha}B D^{-\beta} (∗). Using D=C/(6N)D = C/(6N): ANα=βαB(6N/C)β=βαB6βCβNβ.A N^{-\alpha} = \tfrac{\beta}{\alpha} B (6N/C)^{\beta} = \tfrac{\beta}{\alpha}B\,6^{\beta} C^{-\beta} N^{\beta}. So Nα+βCβN^{\alpha+\beta} \propto C^{\beta}, giving NoptCβ/(α+β)a=βα+β. [3]N_{\text{opt}} \propto C^{\beta/(\alpha+\beta)} \Rightarrow a = \frac{\beta}{\alpha+\beta}. \ [3] Symmetrically (substitute N=C/(6D)N=C/(6D)): DoptCα/(α+β)b=αα+β. [3]D_{\text{opt}} \propto C^{\alpha/(\alpha+\beta)} \Rightarrow b = \frac{\alpha}{\alpha+\beta}. \ [3] a+b=β+αα+β=1a + b = \frac{\beta+\alpha}{\alpha+\beta} = 1. [2] Why it must hold: C=6NDlogC=const+logN+logDC = 6ND \Rightarrow \log C = \text{const} + \log N + \log D; taking N,DCa,CbN,D \propto C^{a},C^{b} forces a+b=1a+b=1 for dimensional consistency.

(c) [6] α+β=0.62\alpha+\beta = 0.62. a=0.28/0.62=0.45160.452a = 0.28/0.62 = 0.4516 \approx 0.452 [2] b=0.34/0.62=0.54840.548b = 0.34/0.62 = 0.5484 \approx 0.548 [2] They are close to 0.50.5 each ⇒ Chinchilla guideline: scale NN and DD roughly equally with compute, giving an approximately fixed ~20 tokens per parameter rule of thumb. [2]

(d) [8] For 10×10\times compute: NN factor =10a=100.452=2.831= 10^{a} = 10^{0.452} = 2.831 [3] DD factor =10b=100.548=3.532= 10^{b} = 10^{0.548} = 3.532 [3] Check product 2.831×3.532=9.999102.831 \times 3.532 = 9.999 \approx 10 ✓ (must equal compute factor). [2]

(e) [6] The C=6NDC=6ND model assumes all NN parameters are active per token. In top-kk MoE only kk of EE experts fire, so active params NactNshared+kENexpertsNtotalN_{\text{act}} \approx N_{\text{shared}} + \frac{k}{E}N_{\text{experts}} \ll N_{\text{total}}. [3] Training/inference FLOPs scale with NactN_{\text{act}}, not NtotalN_{\text{total}}, so an MoE gets more "loss-reducing capacity" (total params) per unit compute — the compute-optimal frontier shifts, and one should use NactN_{\text{act}} in C=6NactDC=6N_{\text{act}}D while counting quality against NtotalN_{\text{total}}. [3]


Question 2

(a) [6] Laux=αauxEefePeL_{\text{aux}} = \alpha_{\text{aux}} E \sum_e f_e P_e. Since it is (up to constant) a dot product of two probability vectors on the simplex, minimizing it drives mass away from correlating high-load and high-probability experts. The uniform point fe=Pe=1/Ef_e = P_e = 1/E gives efePe=E1E2=1/E\sum_e f_e P_e = E\cdot\frac{1}{E^2} = 1/E, so Lauxmin=αauxE1E=αauxL_{\text{aux}}^{\min} = \alpha_{\text{aux}}\cdot E\cdot\frac1E = \alpha_{\text{aux}}. [4] The differentiable surrogate uses the soft PeP_e (gradient path) with the hard fef_e (detached), so gradients push PeP_e toward uniform when experts are overloaded. [2]

(b) [8] Let f,Pf, P be probability vectors (fe=Pe=1\sum f_e = \sum P_e = 1). By Cauchy–Schwarz, (efePe)(e1)(efePe)2\Big(\sum_e f_e P_e\Big)\Big(\sum_e 1\Big) \geq \Big(\sum_e \sqrt{f_e P_e}\Big)^2 is not tight enough; instead use the tighter route: [2] Consider e(fe1/E)(Pe1/E)\sum_e (f_e - 1/E)(P_e - 1/E) . Actually the clean bound: minimize fePe\sum f_e P_e subject to f=Pf=P (as they converge under the balancing pressure). Then fe2(fe)2/E=1/E\sum f_e^2 \geq (\sum f_e)^2/E = 1/E by Cauchy–Schwarz / power-mean, [4] with equality iff all fef_e equal =1/E=1/E. Since at balance Pe=feP_e=f_e, fePe=fe21/E\sum f_e P_e = \sum f_e^2 \geq 1/E, equality iff fe=Pe=1/Ef_e=P_e=1/E. [2]

(c) [8] Capacity C=(4096/8)×1.25=512×1.25=640=640C = \lceil (4096/8)\times 1.25\rceil = \lceil 512\times1.25\rceil = \lceil 640\rceil = 640. [3] Expert with 900 tokens: drops 900640=260900 - 640 = 260 tokens. [3] Raising cfc_f reduces dropped tokens (better quality/less info loss) but increases per-expert buffer size → more compute & memory and padding waste (lower throughput). [2]

(d) [10] Pseudocode with shapes:

# x: [T, d]
logits = router(x)               # [T, E]
probs  = softmax(logits, axis=-1)# [T, E]
expert = argmax(probs, axis=-1)  # [T]   top-1 choice
gate   = max(probs, axis=-1)     # [T]   gating weight

# load-balancing stats
f = bincount(expert, E) / T      # [E]  hard fraction
P = mean(probs, axis=0)          # [E]  soft mass
L_aux = alpha_aux * E * sum(f * P)          # scalar

# capacity with dropping
cap = ceil((T/E) * c_f)
y = zeros([T, d])
for e in range(E):
    idx = where(expert == e)[:cap]          # keep first `cap`, drop rest
    out = experts[e](x[idx])                # [<=cap, d]
    y[idx] = gate[idx][:,None] * out        # scaled combine
return y, L_aux

Marks: routing+softmax [3], f/P & aux loss [3], capacity/drop logic [2], gated combine + shapes annotated [2].


Question 3

Part A

(a) [6] Unrolling: h1=Bˉx1h_1=\bar B x_1, h2=AˉBˉx1+Bˉx2h_2=\bar A\bar B x_1+\bar B x_2, and by induction ht=j=1tAˉtjBˉxjh_t = \sum_{j=1}^t \bar A^{t-j}\bar B x_j. Then yt=Cht=j=1tCAˉtjBˉxjy_t=Ch_t=\sum_{j=1}^t C\bar A^{t-j}\bar B x_j. [3] This is a convolution y=Kˉxy = \bar K * x with kernel Kˉk=CAˉkBˉ\bar K_k = C\bar A^{k}\bar B. Training: compute the full kernel and use FFT/parallel convolution over the whole sequence at once (parallel). Inference: keep the nn-dim state hh and update recurrently O(1)O(1) per token (sequential). [3]

(b) [6] Aˉ=eΔA=e0.05=0.9512\bar A = e^{\Delta A} = e^{-0.05} = 0.9512. [2] Bˉ=(Aˉ1)A1B=(0.95121)/(0.5)=(0.0488)/(0.5)=0.0975\bar B = (\bar A - 1)A^{-1}B = (0.9512-1)/(-0.5) = (-0.0488)/(-0.5)= 0.0975. [2] Stability: Aˉ<1|\bar A| < 1; here 0.9512<10.9512 < 1 ✓, so the impulse response decays and output is bounded. [2]

(c) [4] SSM: memory O(n)O(n) (fixed state, independent of LL), compute O(n)O(n) per token → O(1)O(1) in LL. [2] Attention + KV-cache: memory O(Ld)O(Ld) (cache grows with length), compute O(Ld)O(Ld) per token. [2] SSM wins asymptotically as LL\to\infty.

Part B

(d) [8] Number of accepted drafted tokens =k=k if the first kk are accepted and the (k+1)(k{+}1)-th rejected: P(k)=βk(1β)P(k)=\beta^k(1-\beta) for k=0..γ1k=0..\gamma-1; all γ\gamma accepted with prob βγ\beta^\gamma. On any rejection the target supplies 1 corrected token; if all accepted, 1 bonus token. So tokens =k+1= k+1. [3] E=k=0γ1(k+1)βk(1β)+(γ+1)βγ.\mathbb E=\sum_{k=0}^{\gamma-1}(k+1)\beta^k(1-\beta) + (\gamma+1)\beta^\gamma. This telescopes; equivalently the number of tokens is 1+k=1γ1[first k accepted]1+\sum_{k=1}^{\gamma}\mathbf 1[\text{first }k\text{ accepted}] with $E=\sum_{