Fine-Tuning and Alignment: Making Models Follow Instructions
Intuition: generalist first, specialist second
Section titled “Intuition: generalist first, specialist second”Pretraining makes the model a “language generalist,” but it does not automatically answer questions in the way humans expect. Fine-tuning continues training on high-quality instruction-response pairs, teaching the model “conversation format.” Alignment goes further, making outputs align with human values: helpful, honest, and harmless.
An analogy: pretraining is reading all textbooks, fine-tuning is practicing mock interviews, and alignment is learning professional ethics and behavioral norms. All three are necessary.
Engineering view: from SFT to preference optimization
Section titled “Engineering view: from SFT to preference optimization”The typical pipeline has multiple stages:
- SFT (Supervised Fine-Tuning): Train on human-written or distilled high-quality instruction data so the model learns to follow format and style.
- RLHF (Reinforcement Learning from Human Feedback): First train a reward model (RM) to learn human preference rankings, then optimize the policy model with PPO or similar algorithms to maximize reward scores.
- DPO (Direct Preference Optimization): Skip the explicit reward model and optimize the policy directly from preference data, simplifying the pipeline while often achieving comparable results.
Engineering trade-offs include: SFT data quality matters more than quantity; RLHF is sensitive to hyperparameters and can be unstable; DPO is simpler but may underperform on long responses or complex distributions. Constitutional AI and RLAIF attempt to generate preferences with AI rather than humans, reducing cost and improving scalability.
In addition, parameter-efficient fine-tuning methods such as LoRA and QLoRA let small teams fine-tune large models on consumer GPUs, greatly lowering the barrier to application.
Research view: the nature and limits of alignment
Section titled “Research view: the nature and limits of alignment”A fundamental open question is whether “alignment” truly changes the model’s internal objectives, or merely suppresses surface behavior. Evidence suggests that models can sometimes “jailbreak” around safety training, indicating that alignment may not be deeply internalized.
Key research directions include: detecting and defending against reward hacking; addressing artifacts in preference modeling such as length and position bias; maintaining context consistency in multi-turn conversations; and achieving more robust alignment with less human annotation.
🔬 Open Research Questions
Key questions and research directions in this area:
- How can reward hacking in RLHF be fundamentally addressed? Does DPO completely circumvent this issue?
- How can the "capability degeneration" phenomenon after alignment be quantified and mitigated? How to find the optimal balance between alignment and capability?
- Is there a unified alignment framework that can simultaneously handle helpfulness, harmlessness, and honesty?
Training Data Flow: From Rollout to Gradient Backprop (Engineering View)
Section titled “Training Data Flow: From Rollout to Gradient Backprop (Engineering View)”The core loop of RL alignment training can be summarized in one sentence: sample a batch of responses, recalculate per-token probabilities for both old and new policies, clip the ratio to control update magnitude, then backpropagate loss through all parameters. The seven figures below walk through every step of this data flow.
Fig 1 · response_mask: which tokens contribute to loss
Section titled “Fig 1 · response_mask: which tokens contribute to loss”RL training computes loss only on the model-generated response tokens; prompt token losses are masked to zero, otherwise the reward signal would contaminate the input side.
loss = Σt mask[t] · CE(logit[t], label[t]) / Σ mask[t] Fig 2 · rollout: sampling responses (vLLM)
Section titled “Fig 2 · rollout: sampling responses (vLLM)”At the start of each training step, the current policy samples multiple response completions via vLLM (rollout). These responses are scored by the reward model and used for subsequent log_prob calculation.
Each sampled response feeds into log_prob recalculation & reward scoring
Fig 3 · teacher-forcing forward: recalculating log_prob (Megatron)
Section titled “Fig 3 · teacher-forcing forward: recalculating log_prob (Megatron)”With the sampled responses in hand, the full “prompt + response” sequence is fed back into the policy model using teacher-forcing, computing log P for every response token in a single forward pass. This gives log_prob under the current policy π_θ.
Teacher-forcing feeds ground-truth tokens at each step, enabling efficient parallel log_prob recalculation.
Fig 4 · log_prob vs old_log_prob: comparing new and reference policy
Section titled “Fig 4 · log_prob vs old_log_prob: comparing new and reference policy”The log π_θ from the previous step is compared token-by-token against the log π_old recorded during sampling. The difference determines how much the policy has drifted.
The difference Δ = log πθ − log πold determines importance ratio rt = exp(Δ), which PPO/GRPO clips to prevent policy drift.
Fig 5 · ratio clipping: PPO / GRPO
Section titled “Fig 5 · ratio clipping: PPO / GRPO”The importance ratio r_t = exp(log π_θ − log π_old) measures the per-token policy update magnitude. PPO clips r_t to [1−ε, 1+ε] to prevent excessively large updates that could destabilize training.
rt = πθ(at|st) / πold(at|st) = exp(log πθ − log πold)
LCLIP = Et[ min(rt·Ât, clip(rt, 1−ε, 1+ε)·Ât) ]
Fig 6 · seq-mean-token-mean: loss aggregation
Section titled “Fig 6 · seq-mean-token-mean: loss aggregation”Loss is first averaged over response tokens within each sequence (eliminating length bias), then averaged across the mini-batch to produce a scalar loss.
Seq-mean-then-batch-mean: normalize per sequence first, then average across the batch — eliminating length bias.
Fig 7 · softmax Jacobian → gradient backprop → Megatron pipeline
Section titled “Fig 7 · softmax Jacobian → gradient backprop → Megatron pipeline”Starting from a scalar loss, gradients flow back through the softmax Jacobian to the logits, then back through 96 Transformer layers to all 235B parameters. Megatron-LM’s 3D parallelism (PP/TP/DP) distributes the gradient updates across thousands of GPUs.
V ≈ 128k vocab; full J 太大→实际用向量积简化
每层累积 ∂L/∂W via chain rule
layers 1–24
layers 25–48
layers 49–72
layers 73–96
(p − y) 简化为 O(V) 操作。
Megatron 的流水线并行(PP)、张量并行(TP)、数据并行(DP)三维切分让 235B 参数更新分布在数千 GPU 上。
The softmax Jacobian dimension explosion (V×V) is avoided in practice using the simplified gradient
(p − y) — O(V) ops.
Megatron's 3D parallelism (PP/TP/DP) distributes the 235B parameter update across thousands of GPUs.
References
- Training language models to follow instructions with human feedback
InstructGPT introduces the three-stage RLHF pipeline (SFT → reward model → PPO) that transforms language models from "predict next token" to "follow human intent." This is the direct blueprint for ChatGPT and established the dominant approach to LLM alignment.
- Deep Reinforcement Learning from Human Preferences
The foundational RLHF paper. The authors show that training a reward model from human pairwise preferences, then using it to guide reinforcement learning, enables agents to learn complex behaviors that are difficult to specify with explicit reward functions. This framework was directly adopted by InstructGPT/ChatGPT.
- Direct Preference Optimization: Your Language Model is Secretly a Reward Model
DPO (Direct Preference Optimization) shows that the reward model + RL two-step in RLHF can be collapsed into a single supervised learning step: directly optimizing language model parameters on preference data, mathematically equivalent to the optimal RLHF policy. DPO has become the dominant RLHF alternative in alignment research and open-source community.
- Constitutional AI: Harmlessness from AI Feedback
Anthropic's Constitutional AI (CAI): use a set of explicit "constitution" principles to let the model self-critique and revise (SL-CAI phase), then use AI feedback instead of human feedback for RLHF (RLAIF phase). This reduces reliance on human annotation and is the core alignment technique behind the Claude model family.
- RLAIF: Scaling Reinforcement Learning from Human Feedback with AI Feedback
Google systematically proves RLAIF can match RLHF on various tasks, providing engineering evidence for "AI feedback replacing human" as scalable alignment solution.
- LoRA: Low-Rank Adaptation of Large Language Models
LoRA freezes pretrained weights and only trains the product of two low-rank matrices (rank r much smaller than original dimensions), reducing trainable parameters by up to 10,000x. This makes fine-tuning large models on consumer GPUs feasible and has become the dominant parameter-efficient fine-tuning (PEFT) method.