PJ's Guide to Large Language Models

Chapter 4: Post-Training Alignment — RLHF, DPO, GRPO & LoRA

4.1 From Base Document Completer to Aligned Policy

A pre-trained base model models the raw internet distribution ppre(y|x): prompted with a question, it may happily continue with a list of similar questions rather than an answer. Post-training transforms this base model into an assistant and reasoner through Supervised Fine-Tuning (SFT) (masking prompt tokens from cross-entropy loss) followed by Reinforcement Learning against human preference or programmatic verifiers.

4.2 Bradley-Terry Rewards & KL-Regularized RLHF (PPO)

Under the Bradley-Terry preference model, the probability that completion yw is preferred over yl given prompt x is modeled via a latent scalar reward rφ(x, y):

PJ evaluating reward hacking and belly-rub alignment
Fig 4.1 — Reward Alignment
r(x,y) − β DKL(πθ || πref)

Prof. PJ demonstrates optimal policy alignment when rewarded with continuous belly scratches and zero KL drift.

P(yw ≻ yl | x) = σ(rφ(x, yw) − rφ(x, yl)) = 11 + exp(−(rφ(x,yw) − rφ(x,yl)))
(4.1)

Maximizing expected reward directly causes severe reward hacking (Goodhart’s Law: degenerating into high-reward gibberish). Classical RLHF therefore optimizes the policy πθ via clipped PPO with a reverse KL-divergence penalty anchoring πθ to the SFT reference policy πref:

maxπθ Ex∼𝒟, y∼πθ [ rφ(x, y) − β log πθ(y|x)πref(y|x) ]
(4.2)

4.3 Closed-Form Gibbs Policy & Direct Preference Optimization (DPO)

Observe that Eq. (4.2) is strictly concave in π and minimizes the KL divergence between π(y|x) and the exponential-tilted Gibbs distribution π*(y|x) = Z(x)−1πref(y|x) exp(r(x, y)/β). Inverting this relation expresses the ground-truth reward purely as a function of the optimal policy and intractable partition function Z(x):

r(x, y) = β log π*(y|x)πref(y|x) + β log Z(x)
(4.3)

Substituting Eq. (4.3) into the Bradley-Terry difference r(x, yw) − r(x, yl) causes the intractable β log Z(x) term to cancel identically! This yields the Direct Preference Optimization (DPO) objective (Rafailov et al., 2023), optimizing an LLM directly from preference pairs without training a separate reward model or value critic:

ℒDPO(θ) = −E(x,yw,yl) [ log σ ( β log πθ(yw|x)πref(yw|x) − β log πθ(yl|x)πref(yl|x) ) ]
(4.4)

4.4 Reasoning Models: RLVR & Group Relative Policy Optimization (GRPO)

In domains with deterministic programmatic verifiers (mathematics, formal proofs, unit-tested code), Reinforcement Learning with Verifiable Rewards (RLVR) replaces noisy learned reward models with exact binary correctness scores ri ∈ {0, 1}. To eliminate the massive memory footprint of PPO’s learned critic network Vψ (often as large as the policy itself), GRPO (Shao et al., 2024; DeepSeek-R1) samples a group of G rollout trajectories {o1, …, oG} for each prompt q and computes normalized group-relative advantages:

Âi = ri − mean({r1..rG})std({r1..rG}) + ε
(4.5)

Under pure RLVR with GRPO, models spontaneously emerge sophisticated test-time compute behaviors—self-verification, backtracking, hypothesis branching, and extended chain-of-thought reasoning—trading inference tokens for monotonic accuracy gains on hard reasoning benchmarks.

4.5 Parameter-Efficient Adaptation: LoRA & QLoRA

Empirical intrinsic-dimension analyses show that task-specific weight updates ΔW ∈ ℝdout×din occupy a low-rank subspace. Low-Rank Adaptation (LoRA) freezes pretrained weights W0 and optimizes two narrow bottleneck matrices A ∈ ℝr×din (Gaussian init) and B ∈ ℝdout×r (zero init, so ΔW = 0 at step 0) with rank r ≪ min(din, dout):

h = W0x + αr BAx   ⇒   Wmerged = W0 + αr BA  (zero inference overhead!)
(4.6)

QLoRA quantizes the frozen base weights W0 into 4-bit NormalFloat (NF4) with double quantization of scaling constants while backpropagating BF16 gradients exclusively through A and B, allowing a 65B–70B model to fine-tune on a single 48 GB GPU.

Prof. PJ's Bench Note: My humans attempted classical RLHF on me using chicken jerky rewards. Unfortunately, I immediately discovered a reward-hacking exploit: barking loudly at an imaginary squirrel, stopping on command, and collecting infinite jerky with zero actual behavioral alignment.
« Previous: Ch. 3 — Scaling Laws & 3D Parallel [ Table of Contents ] Next: Ch. 5 — Inference, MoE & Quantization »