Level 5 — MasteryAlignment, Prompting & RAG

Alignment, Prompting & RAG

75 minutes60 marksprintable — key stays hidden on paper

Level 5 — Mastery Examination

Time limit: 75 minutes Total marks: 60

Answer all three questions. Show all derivations. Where code is requested, write clean, runnable pseudocode or Python; partial credit for correct structure. Use ...... for math.


Question 1 — From RLHF to DPO: deriving the objective (22 marks)

The standard RLHF fine-tuning objective for a policy πθ\pi_\theta against a reference policy πref\pi_{\text{ref}} with reward r(x,y)r(x,y) is:

maxπθ  ExD,yπθ(x)[r(x,y)]βExD[KL(πθ(x)πref(x))].\max_{\pi_\theta}\; \mathbb{E}_{x\sim\mathcal{D},\,y\sim\pi_\theta(\cdot|x)}\big[r(x,y)\big] - \beta\, \mathbb{E}_{x\sim\mathcal{D}}\Big[\mathrm{KL}\big(\pi_\theta(\cdot|x)\,\|\,\pi_{\text{ref}}(\cdot|x)\big)\Big].

(a) Prove that the optimal policy of this constrained objective has the closed form π(yx)=1Z(x)πref(yx)exp ⁣(1βr(x,y)),\pi^*(y|x)=\frac{1}{Z(x)}\,\pi_{\text{ref}}(y|x)\,\exp\!\Big(\tfrac{1}{\beta}r(x,y)\Big), and give the expression for Z(x)Z(x). State clearly why Z(x)Z(x) is intractable in practice. (8)

(b) Invert the result of (a) to express the reward r(x,y)r(x,y) in terms of π\pi^*, πref\pi_{\text{ref}}, β\beta, and Z(x)Z(x). Then show that under the Bradley–Terry preference model P(ywylx)=σ(r(x,yw)r(x,yl)),P(y_w \succ y_l \mid x)=\sigma\big(r(x,y_w)-r(x,y_l)\big), the partition function Z(x)Z(x) cancels, yielding the DPO per-example loss LDPO=logσ ⁣(βlogπθ(ywx)πref(ywx)βlogπθ(ylx)πref(ylx)).\mathcal{L}_{\text{DPO}} = -\log\sigma\!\Big(\beta\log\tfrac{\pi_\theta(y_w|x)}{\pi_{\text{ref}}(y_w|x)} - \beta\log\tfrac{\pi_\theta(y_l|x)}{\pi_{\text{ref}}(y_l|x)}\Big). (8)

(c) Compute the gradient θLDPO\nabla_\theta \mathcal{L}_{\text{DPO}} and interpret the scalar weighting factor: explain, in one or two sentences, how DPO automatically up-weights examples where the implicit reward model is "wrong." (6)


Question 2 — PPO clipping mechanics and reward hacking (18 marks)

In PPO for LLMs, let rt(θ)=πθ(atst)πθold(atst)r_t(\theta)=\dfrac{\pi_\theta(a_t|s_t)}{\pi_{\theta_{\text{old}}}(a_t|s_t)} and advantage A^t\hat A_t. The clipped surrogate is LCLIP(θ)=Et[min(rtA^t, clip(rt,1ϵ,1+ϵ)A^t)].L^{\text{CLIP}}(\theta)=\mathbb{E}_t\Big[\min\big(r_t\hat A_t,\ \mathrm{clip}(r_t,1-\epsilon,1+\epsilon)\hat A_t\big)\Big].

(a) For ϵ=0.2\epsilon=0.2 and a single token with A^t=+3\hat A_t=+3, plot/describe the surrogate as a function of rtr_t on [0,2][0,2]. Give the exact value of LCLIPL^{\text{CLIP}} at rt=0.5,1.0,1.5r_t=0.5,\,1.0,\,1.5. Repeat for A^t=3\hat A_t=-3. (8)

(b) Explain mathematically why the min\min makes the objective a pessimistic lower bound, and precisely why clipping is one-sided in its effect (i.e., which direction of policy update is left unconstrained for positive vs. negative advantage). (6)

(c) In RLHF, a learned reward model rϕr_\phi is imperfect. The KL penalty coefficient β\beta trades off reward against divergence from πref\pi_{\text{ref}}. A team observes the reward climbing while human ratings fall. Name this failure mode, and state two concrete mitigations tied to quantities in the objective. (4)


Question 3 — RAG retrieval math + system design (20 marks)

A RAG system embeds chunks into unit-normalized vectors and retrieves by cosine similarity.

(a) A query embedding q=(0.6,0.8)q=(0.6,0.8) (already unit norm). Three chunk embeddings (unnormalized) are c1=(3,4),c2=(1,0),c3=(0.8,0.6).c_1=(3,4),\quad c_2=(1,0),\quad c_3=(-0.8,0.6). Compute the cosine similarity of qq with each, and give the top-1 retrieved chunk. Then show that for unit-normalized vectors, ranking by cosine similarity is equivalent to ranking by (ascending) squared Euclidean distance. (8)

(b) A corpus has 1,000,000 chunks. Exact top-kk nearest-neighbour search is O(Nd)O(Nd) per query. Approximate methods (e.g., HNSW) give roughly O(dlogN)O(d\log N). For d=768d=768, compute the ratio of operation counts (exact : approximate) and comment on why recall@k can drop below 100%. (4)

(c) Design a hybrid retrieval + reranking pipeline that combines a BM25 sparse score sbm25s_{\text{bm25}} and a dense score sdenses_{\text{dense}}, then reranks with a cross-encoder. Write the fused score formula (with normalization), state where reranking sits in the pipeline and why a cross-encoder is used only at rerank stage, and name two chunking-strategy choices that materially affect retrieval quality. (8)


End of paper.

Answer keyMark scheme & solutions

Question 1 (22 marks)

(a) Optimal policy derivation (8)

Write the objective per prompt xx as a functional of π(x)\pi(\cdot|x): J(π)=yπ(yx)r(x,y)βyπ(yx)logπ(yx)πref(yx).J(\pi)=\sum_y \pi(y|x)\,r(x,y) - \beta\sum_y \pi(y|x)\log\frac{\pi(y|x)}{\pi_{\text{ref}}(y|x)}. (1 mark for correct setup, KL expanded.)

Maximize subject to yπ(yx)=1\sum_y\pi(y|x)=1. Lagrangian: L=J(π)+λ(1yπ(yx)).\mathcal{L}=J(\pi)+\lambda\Big(1-\sum_y\pi(y|x)\Big). (1) Differentiate w.r.t. π(yx)\pi(y|x): r(x,y)β(logπ(yx)πref(yx)+1)λ=0.r(x,y)-\beta\Big(\log\frac{\pi(y|x)}{\pi_{\text{ref}}(y|x)}+1\Big)-\lambda=0. (2 — the "+1" from ddπ(πlogπ)\frac{d}{d\pi}(\pi\log\pi).)

Solve: logπ(yx)πref(yx)=1βr(x,y)1λβ\log\frac{\pi(y|x)}{\pi_{\text{ref}}(y|x)}=\frac{1}{\beta}r(x,y)-1-\frac{\lambda}{\beta} π(yx)=πref(yx)exp ⁣(1βr(x,y))e1λ/β.\Rightarrow \pi^*(y|x)=\pi_{\text{ref}}(y|x)\exp\!\big(\tfrac{1}{\beta}r(x,y)\big)\cdot e^{-1-\lambda/\beta}. (2) The constant absorbs into normalization; enforcing yπ=1\sum_y\pi^*=1 gives π(yx)=1Z(x)πref(yx)exp ⁣(1βr(x,y)),Z(x)=yπref(yx)exp ⁣(1βr(x,y)).\pi^*(y|x)=\frac{1}{Z(x)}\pi_{\text{ref}}(y|x)\exp\!\big(\tfrac1\beta r(x,y)\big),\quad Z(x)=\sum_y \pi_{\text{ref}}(y|x)\exp\!\big(\tfrac1\beta r(x,y)\big). (1 for ZZ.) Z(x)Z(x) sums over the entire (exponentially large / infinite) space of output sequences yy, so it is intractable to compute or sample from exactly. (1)

(b) Reward inversion + ZZ cancellation (8)

Invert: r(x,y)=βlogπ(yx)πref(yx)+βlogZ(x).(2)r(x,y)=\beta\log\frac{\pi^*(y|x)}{\pi_{\text{ref}}(y|x)}+\beta\log Z(x). \quad (2)

Under Bradley–Terry, only the difference of rewards enters: r(x,yw)r(x,yl)=βlogπ(ywx)πref(ywx)βlogπ(ylx)πref(ylx)+βlogZ(x)βlogZ(x)=0.(3)r(x,y_w)-r(x,y_l)=\beta\log\frac{\pi^*(y_w|x)}{\pi_{\text{ref}}(y_w|x)}-\beta\log\frac{\pi^*(y_l|x)}{\pi_{\text{ref}}(y_l|x)}+\underbrace{\beta\log Z(x)-\beta\log Z(x)}_{=0}. \quad (3) The βlogZ(x)\beta\log Z(x) terms cancel because Z(x)Z(x) depends only on xx, not on yy. (1)

Substituting into logσ()-\log\sigma(\cdot) (negative log-likelihood of the preference), and replacing π\pi^* with the trainable πθ\pi_\theta: LDPO=logσ ⁣(βlogπθ(ywx)πref(ywx)βlogπθ(ylx)πref(ylx)).(2)\mathcal{L}_{\text{DPO}}=-\log\sigma\!\Big(\beta\log\tfrac{\pi_\theta(y_w|x)}{\pi_{\text{ref}}(y_w|x)}-\beta\log\tfrac{\pi_\theta(y_l|x)}{\pi_{\text{ref}}(y_l|x)}\Big). \quad (2)

(c) Gradient and interpretation (6)

Let u=β(logπθ(ywx)πref(ywx)logπθ(ylx)πref(ylx))u=\beta\big(\log\frac{\pi_\theta(y_w|x)}{\pi_{\text{ref}}(y_w|x)}-\log\frac{\pi_\theta(y_l|x)}{\pi_{\text{ref}}(y_l|x)}\big), i.e. the difference of implicit rewards r^θ(yw)r^θ(yl)\hat r_\theta(y_w)-\hat r_\theta(y_l). Since ddu(logσ(u))=(1σ(u))=σ(u)\frac{d}{du}(-\log\sigma(u))=-(1-\sigma(u))=-\sigma(-u): θLDPO=βσ(r^θ(yl)r^θ(yw))[θlogπθ(ywx)θlogπθ(ylx)].(4)\nabla_\theta\mathcal{L}_{\text{DPO}}=-\beta\,\sigma\big(\hat r_\theta(y_l)-\hat r_\theta(y_w)\big)\Big[\nabla_\theta\log\pi_\theta(y_w|x)-\nabla_\theta\log\pi_\theta(y_l|x)\Big]. \quad (4)

Interpretation (2): The scalar σ(r^θ(yl)r^θ(yw))\sigma(\hat r_\theta(y_l)-\hat r_\theta(y_w)) is large (near 1) exactly when the model currently ranks the dispreferred response higher than the preferred one — i.e. the implicit reward is "wrong." So mis-ranked pairs get large gradient weight; correctly-ranked confident pairs get near-zero weight. This is automatic hard-example weighting, no separate reward model needed.


Question 2 (18 marks)

(a) Numeric surrogate values (8)

Positive advantage A^t=+3\hat A_t=+3, ϵ=0.2\epsilon=0.2 so clip range [0.8,1.2][0.8,1.2]: LCLIP=min(r3, clip(r,0.8,1.2)3)L^{\text{CLIP}}=\min(r\cdot3,\ \mathrm{clip}(r,0.8,1.2)\cdot3).

  • r=0.5r=0.5: min(1.5, 0.83=2.4)=1.5\min(1.5,\ 0.8\cdot3=2.4)=\mathbf{1.5} (unclipped term smaller). (1)
  • r=1.0r=1.0: min(3,3)=3.0\min(3,3)=\mathbf{3.0}. (1)
  • r=1.5r=1.5: min(4.5, 1.23=3.6)=3.6\min(4.5,\ 1.2\cdot3=3.6)=\mathbf{3.6} (clipped). (1)

Shape: rises linearly, then flat ceiling of 3.63.6 for r1.2r\ge1.2; below r=0.8r=0.8 it stays the unclipped line (since min picks smaller). So the objective is capped above for large rr. (1)

Negative advantage A^t=3\hat A_t=-3: L=min(3r, clip(r,0.8,1.2)(3))L=\min(-3r,\ \mathrm{clip}(r,0.8,1.2)\cdot(-3)).

  • r=0.5r=0.5: min(1.5, 0.8(3)=2.4)=2.4\min(-1.5,\ 0.8\cdot(-3)=-2.4)=\mathbf{-2.4} (clipped). (1)
  • r=1.0r=1.0: min(3,3)=3.0\min(-3,-3)=\mathbf{-3.0}. (1)
  • r=1.5r=1.5: min(4.5, 1.2(3)=3.6)=4.5\min(-4.5,\ 1.2\cdot(-3)=-3.6)=\mathbf{-4.5} (unclipped). (1)

Shape: floored below at 2.4-2.4 for r0.8r\le0.8; falls linearly for r>1.2r>1.2. (1)

(b) Pessimistic lower bound & one-sidedness (6)

Because LCLIP=min(unclipped, clipped)L^{\text{CLIP}}=\min(\text{unclipped},\ \text{clipped}), it never exceeds the unclipped surrogate — it is a lower bound on the true (unclipped) objective, hence "pessimistic." (2)

Directionality: (4)

  • For A^t>0\hat A_t>0 (good action): we want to increase πθ(at)\pi_\theta(a_t), i.e. increase rtr_t. Clipping caps the gain once rt>1+ϵr_t>1+\epsilon, so it removes incentive to push rtr_t too high. But decreasing rtr_t (below 1) is not clamped — the gradient still flows. So the constraint is one-sided: it limits over-eager increases, not decreases.
  • For A^t<0\hat A_t<0 (bad action): symmetric — clipping limits pushing rtr_t below 1ϵ1-\epsilon, but does not constrain increasing rtr_t. Net effect: only the update direction that would exploit a large ratio to gain surrogate is clamped; the corrective direction remains free.

(c) Reward hacking / over-optimization (4)

Failure mode: reward model over-optimization (reward hacking / Goodhart's law) — the policy exploits errors in rϕr_\phi, so proxy reward rises while true (human) quality falls. (2)

Two mitigations tied to the objective (2, one each):

  1. Increase β\beta (KL penalty) to keep πθ\pi_\theta close to πref\pi_{\text{ref}}, limiting how far it can drift to exploit rϕr_\phi.
  2. Early stopping / KL budget (stop when KL(πθπref)\mathrm{KL}(\pi_\theta\|\pi_{\text{ref}}) exceeds a threshold), or retrain/ensemble the reward model on newly generated on-policy samples to close the exploited gap.

Question 3 (20 marks)

(a) Cosine similarities + equivalence (8)

q=(0.6,0.8)q=(0.6,0.8), q=1\|q\|=1.

  • c1=(3,4)c_1=(3,4), c1=5\|c_1\|=5, c^1=(0.6,0.8)\hat c_1=(0.6,0.8). cos=0.60.6+0.80.8=0.36+0.64=1.0\cos=0.6\cdot0.6+0.8\cdot0.8=0.36+0.64=\mathbf{1.0}. (2)
  • c2=(1,0)c_2=(1,0), c2=1\|c_2\|=1. cos=0.61+0.80=0.6\cos=0.6\cdot1+0.8\cdot0=\mathbf{0.6}. (1)
  • c3=(0.8,0.6)c_3=(-0.8,0.6), c3=1\|c_3\|=1. cos=0.6(0.8)+0.8(0.6)=0.48+0.48=0\cos=0.6(-0.8)+0.8(0.6)=-0.48+0.48=\mathbf{0}. (1)

Top-1: c1c_1 (similarity 1.0 — same direction as qq). (1)

Equivalence (3): For unit vectors a=b=1\|a\|=\|b\|=1, ab2=a2+b22a ⁣ ⁣b=22cos(a,b).\|a-b\|^2=\|a\|^2+\|b\|^2-2a\!\cdot\!b=2-2\cos(a,b). So squared Euclidean distance is a strictly decreasing affine function of cosine similarity; ascending distance order = descending cosine order. Ranking is identical.

(b) Complexity ratio (4)

Exact: O(Nd)=106768=7.68×108O(Nd)=10^6\cdot768=7.68\times10^8 ops. (1) Approx: O(dlog2N)=768log2(106)76819.931.53×104O(d\log_2 N)=768\cdot\log_2(10^6)\approx768\cdot19.93\approx1.53\times10^4 ops. (1) Ratio 7.68×1081.53×1045.0×104\approx \frac{7.68\times10^8}{1.53\times10^4}\approx \mathbf{5.0\times10^4} (≈50,000× fewer ops). (1) Recall < 100% because ANN graph traversal (HNSW) is heuristic/greedy and may miss true neighbours near cluster boundaries — it trades accuracy for the huge speedup. (1)

(c) Hybrid pipe