Reward Model and the TRL Outcome-supervised Reward Modeling Trainer

Reward Models (RMs) are typically trained using supervised learning on datasets containing pairs of preferred and non-preferred responses.

Verified

On this page

Definition

Reward Models (RMs) are typically trained using supervised learning on datasets containing pairs of preferred and non-preferred responses. The goal is to learn a function that assigns higher scores to preferred responses, enabling the model to rank outputs based on preferences. TRL supports the Outcome-supervised Reward Modeling (ORM) Trainer for training reward models. The Outcome-supervised Reward Modeling Trainer, described as a post-training method, was contributed by Younes Belkada.

At a glance

RewardTrainer supports preference datasets with implicit or explicit prompts. RewardTrainer is compatible with both standard and conversational dataset formats. Metrics are recorded while training and evaluating.

Diagram: During training, each example is expected to contain a chosen and rejected field. > RewardTrainer tokenizes each input using the model’s tokenizer. > The loss is defined as the negative log-likelihood of the observed preferences.1During training, each example is expected tocontain a chosen and rejected field.2RewardTrainer tokenizes each input using themodel’s tokenizer.3The loss is defined as the negativelog-likelihood of the observed preferences.

How it works

When provided with a conversational dataset, the trainer will automatically apply the chat template to the dataset. If prompts and completions (chosen and rejected) are provided separately (explicit prompt case), they are concatenated before tokenization. The Bradley-Terry model is underdetermined, meaning that adding a constant to all rewards does not change the preference probabilities.

The paper Helping or Herding? Reward Model Ensembles Mitigate but do not Eliminate Reward Hacking proposes adding an auxiliary loss term that encourages the rewards to be centered around zero. The center_rewards_coefficient parameter in the RewardConfig controls that auxiliary loss term. RewardConfig is the configuration class for the RewardTrainer.

RewardConfig includes only the parameters that are specific to Reward training. In RewardConfig, learning_rate defaults to 1e-4, instead of 5e-5 in TrainingArguments. The logging_steps default is 10 in RewardConfig and 500 in TrainingArguments.

For model initialization through RewardConfig, from_pretrained() keyword arguments can be forwarded; all are supported except num_labels, which is automatically set to 1. The accuracy metric is the proportion of correct predictions, meaning the model assigned a higher score to the chosen response than to the rejected one, averaged over the last logging interval. The margin metric is the average margin, the difference between chosen and rejected rewards, over the last logging interval.

The loss metric is the average loss over the last logging interval. The mean_reward metric is the average reward score assigned by the model over the last logging interval. The min_reward metric is the minimum reward score assigned by the model, averaged over the logging interval.

The global_step metric is the total number of optimizer steps taken so far. The grad_norm metric is the L2 norm of the gradients, computed before gradient clipping.

Example

The quick start example demonstrates how to train a reward model using the RewardTrainer from TRL. In that example, a Qwen 3 0.6B model is trained on the UltraFeedback dataset. If a dataset is not in one of the expected formats, it can be preprocessed to convert it into the expected format. The documentation gives a preprocessing example with the arena-human-preference-55k dataset from lmarena-ai.

From the official source
python
from trl import RewardTrainer
from datasets import load_dataset

trainer = RewardTrainer(
    model="Qwen/Qwen3-0.6B",
    train_dataset=load_dataset("trl-lib/ultrafeedback_binarized", split="train"),
)
trainer.train()
From the official source
python
# Standard preference (implicit prompt)
{"chosen": "The sky is blue.",
 "rejected": "The sky is green."}

# Conversational preference (implicit prompt)
{"chosen": [{"role": "user", "content": "What color is the sky?"},
            {"role": "assistant", "content": "It is blue."}],
 "rejected": [{"role": "user", "content": "What color is the sky?"},
              {"role": "assistant", "content": "It is green."}]}

# Standard preference (explicit prompt)
{"prompt": "The sky is",
 "chosen": " blue.",
 "rejected": " green."}

# Conversational preference (explicit prompt)
{"prompt": [{"role": "user", "content": "What color is the sky?"}],
 "chosen": [{"role": "assistant", "content": "It is blue."}],
 "rejected": [{"role": "assistant", "content": "It is green."}]}
From the official source
python
from datasets import load_dataset
import json

dataset = load_dataset("lmarena-ai/arena-human-preference-55k")

# Filter out ties
dataset = dataset.filter(lambda example: example["winner_tie"] == 0)

# Create 'chosen' and 'rejected' fields based on the winner column
def response_a_b_to_chosen_rejected(example):
    if example["winner_model_a"] == 1:
        example["chosen"] = example["response_a"]
        example["rejected"] = example["response_b"]
    else:
        example["chosen"] = example["response_b"]
        example["rejected"] = example["response_a"]
    return example

dataset = dataset.map(response_a_b_to_chosen_rejected)

# Convert to conversational format
def make_conversation(example):
    prompt = json.loads(example["prompt"])[0]  # '["What color is the sky?"]' -> "What color is the sky?"
    chosen = json.loads(example["chosen"])[0]
    rejected = json.loads(example["rejected"])[0]
    return {
        "chosen": [{"role": "user", "content": prompt}, {"role": "assistant", "content": chosen}],
        "rejected": [{"role": "user", "content": prompt}, {"role": "assistant", "content": rejected}],
    }


dataset = dataset.map(make_conversation)

# Keep only necessary columns
dataset = dataset.select_columns(["chosen", "rejected"])

print(next(iter(dataset["train"])))

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.