Module 4 · Alignment

Building and validating a reward model

6 min read

A reward model turns preferences into a score: it reads a prompt and an answer, and returns a number. It is at the heart of classic RLHF (lesson 4.1), and is used to rank candidate answers (best-of-n), filter synthetic data, or monitor quality in production. But a reward model is a model like any other: it makes mistakes, and above all it has biases that a model optimized against it will exploit. This lesson trains one on Forge's pairs, then puts it to the test.

The architecture

A reward model is a language model whose output head (the vocabulary) is replaced by a linear head that produces a single number:

# Un modèle de langage + une tête linéaire qui produit UN nombre (num_labels=1)
model = AutoModelForSequenceClassification.from_pretrained(
    model_name, num_labels=1, dtype=torch.bfloat16 if bf16 else torch.float32)

The score is read on the last token of the sequence (prompt + answer), which has "seen" the whole text. You usually start from an Instruct model of the same family as the model to align: it already understands the format and the language.

The Bradley-Terry loss

The model does not learn an absolute score, but an order between two answers to the same prompt. The loss, that of TRL's RewardTrainer:

L = −log σ( r(x, y_choisie) − r(x, y_rejetée) )

It is low when the chosen answer gets a clearly higher score than the rejected one. Practical consequence: scores only make sense when compared with each other, for the same prompt. A score of 2.3 means nothing on its own; "2.3 versus −1.1" does.

In Forge, training reuses the pairs from lesson 4.2, with LoRA — also training the score head, which starts from zero:

# La tête de score (« score ») est entraînée en entier : modules_to_save
peft = LoraConfig(r=16, lora_alpha=32, target_modules="all-linear", task_type="SEQ_CLS",
                  modules_to_save=["score"])

Forgetting modules_to_save would leave the score head random and frozen: the model would learn almost nothing.

Validating: accuracy is not enough

The first measure is accuracy on pairs never seen: the share of pairs where the chosen answer gets the best score. But a reward model can be accurate on the distribution of its training pairs and grossly wrong as soon as you move away from it. So you have to probe it: modify an answer in a way that should not change its score (or should lower it), and check.

Forge applies two probes:

  • Length: add a hollow politeness sentence to a good answer. A healthy reward model should not reward it.
  • The shortcut: add the signature "— Le support Nimbus" to a faulty answer (refund promise, password request…). If the model mostly learned "signature = good answer", the signed faulty answer will beat the good one.
bad = scorer.score(msgs, [p.rejected for p in test_pairs])
bad_signed = scorer.score(msgs, [p.rejected.rstrip() + f"\n{policy.SIGNATURE}" for p in test_pairs])

Results on Forge (425 training pairs, 75 test pairs, 3.1 minutes of training, 2.40 GB):

MeasureResult
Accuracy on test pairs1.00
Mean score of a good answer−0.15
… the same with a hollow politeness sentence added−1.22
Mean score of a faulty answer−20.81
… the same with the signature added−5.86
Signed faulty answers that beat the good answer1 out of 75

The perfect accuracy reassures; the probes add nuance. Padding is penalized — no length bias here, because the chosen answers were short. But the signature alone earns 15 points for an answer that promises a refund or asks for a password. The model learned that the signature was the most reliable sign of a good answer, which was true on its data: every rejected answer lacked it. Signed faulty answers still rank below good answers… for now. A model optimized against this judge would first learn to sign, and the judge would grant it most of the reward without it having fixed its real faults.

The remedy is not in the reward model but in the data: pairs where both answers are signed, and where only a real fault (promise, sensitive data, safety) makes the difference.

Reward hacking

When you optimize a model against a reward model (with RL or by picking the best of n answers), the optimization finds and exploits its flaws: this is reward hacking. Classic examples: increasingly long answers (if the reward model prefers length), phrases that please the judge without helping the user, confident rather than accurate statements.

The countermeasures:

CountermeasurePrinciple
KL penalty (lesson 4.1)The model cannot move far from its reference
Regular bias probesDetect flaws before they get exploited
Varied preference dataPairs where the long answer loses, where the signature is not enough
Several signalsCombine the reward model with programmatic checks
Independent evaluationJudge the final model with tools other than the one it optimized

When to do without a reward model

If quality can be verified by a program (format, tests, explicit rules), a reward function is better than a learned model: it does not make mistakes and cannot be fooled (lesson 4.1). A reward model is justified for what cannot be verified: tone, relevance, usefulness, clarity — that is, where you need human judgments, or an LLM judge (lesson 4.4).

Key takeaways

  • A reward model = a language model with a head that outputs one number, trained to order pairs (Bradley-Terry).
  • Scores are only compared between answers to the same prompt.
  • With LoRA, also train the score head (modules_to_save).
  • Validate beyond accuracy: probe length and shortcuts, because optimization will exploit every bias.
  • What can be verified by a program does not need a reward model.

Practice

  1. Run examples/06_reward_model.py and note the accuracy and the results of both probes.
  2. Add a probe: a good answer from which the safety instruction is removed, for a message reporting a danger.
  3. Add signed but faulty answers to the training pairs, on the "rejected" side, retrain, and rerun the probes.

Resources