← Coding labs/

Implement a DPO Training Step

Implementationhard~40 min
Objective

Implement the core DPO algorithm: compute sequence log probabilities, the DPO loss with a frozen reference model, and a gradient-based parameter update that increases the margin between chosen and rejected sequences.

Background

Your team wants to align a language model to human preferences without the complexity of RLHF. DPO reformulates the preference learning problem as a simple classification loss over pairs of (chosen, rejected) responses. Given a policy model and a frozen reference model, DPO adjusts the policy so that chosen responses become more likely relative to rejected ones, while staying close to the reference. You're given a SimpleLanguageModel with embedding and output projection. Implement the sequence log probability computation, the DPO loss formula, and a gradient step that updates only the policy model's parameters. The reference model must remain frozen throughout training.

Requirements
  1. 1.Implement log_prob_of_sequence using log_softmax and teacher-forced next-token prediction
  2. 2.Implement dpo_loss using the standard DPO formula with policy and reference log probs
  3. 3.Apply the sigmoid/log-sigmoid transform to the reward difference scaled by beta
  4. 4.Implement a numerical gradient step that perturbs policy parameters
  5. 5.Ensure the reference model parameters are never modified during training
Evaluation (100 points)
Sequence log probability
Uses log_softmax and sums log probs indexed by next tokens
25pt
DPO loss formula
Computes 4 log probs (chosen/rejected x policy/ref) and uses beta
25pt
Sigmoid/log-sigmoid transform
Applies log(1 + exp(-x)) or equivalent sigmoid loss
20pt
Numerical gradient step
Perturbs parameters by epsilon to compute numerical gradients
15pt
Reference model is frozen
ref_model parameters are never modified during training
15pt
Hints
Select a file to start editing
Terminal
$
AI Assistant50K tokens left

Ask me about the code, bugs, or concepts.
I'll guide you in plain English, no code output.
Budget: 50K tokens per lab