#
PRM Trainer

PRM Trainer is an experimental API which is subject to change at any time.

##
Overview

Process-supervised Reward Models (PRM) were proposed in Solving math word problems with process- and outcome-based feedback by Jonathan Uesato, Nate Kushman, Ramana Kumar, Francis Song, Noah Siegel, Lisa Wang, Antonia Creswell, Geoffrey Irving, and Irina Higgins.

The abstract from the paper is the following:

> Recent work has shown that asking language models to generate reasoning steps improves performance on many reasoning tasks. When moving beyond prompting, this raises the question of how we should supervise such models: outcome-based approaches which supervise the final result, or process-based approaches which supervise the reasoning process itself? Differences between these approaches might naturally be expected not just in final-answer errors but also in reasoning errors, which can be difficult to detect and are problematic in many real-world domains such as education. We run the first comprehensive comparison between process- and outcome-based approaches trained on a natural language task, GSM8K. We find that pure outcome-based supervision produces similar final-answer error rates with less label supervision. However, for correct reasoning steps we find it necessary to use processbased supervision or supervision from learned reward models that emulate process-based feedback. In total, we improve the previous best results from 16.8% → 12.7% final-answer error and 14.0% → 3.4% reasoning error among final-answer-correct solutions.

This post-training method was contributed by Gaetan Lopez, Lewis Tunstall, Quentin Gallouédec and Agustín Piqueres.

##
Quick start

This example demonstrates how to train a model using the PRM method. We use the Qwen 0.5B model as the base model. We use the stepwise supervision data from the Math Shepherd dataset. You can view the data in the dataset here:

Below is the script to train the model:

    # train_prm.py
    from datasets import load_dataset
    from trl import PRMConfig, PRMTrainer
    from transformers import AutoModelForTokenClassification, AutoTokenizer

    model = AutoModelForTokenClassification.from_pretrained("Qwen/Qwen2-0.5B", num_labels=2)
    tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B")
    train_dataset = load_dataset("trl-lib/math_shepherd", split="train[:10%]")

    training_args = PRMConfig(output_dir="Qwen2-0.5B-Reward-Math-Sheperd", logging_steps=10)
    trainer = PRMTrainer(model=model, args=training_args, processing_class=tokenizer, train_dataset=train_dataset)
    trainer.train()

Execute the script using the following command:

    accelerate launch train_prm.py

Distributed across 8 GPUs, the training takes approximately 1 hour.

To see how the trained model performs, you can use the following script.

    from datasets import load_dataset
    from transformers import pipeline

    pipe = pipeline("token-classification", model="trl-lib/Qwen2-0.5B-Reward-Math-Sheperd")
    dataset = load_dataset("trl-lib/math_shepherd")
    example = {
        "prompt": "Musa is the class teacher of a class of 45 students. He wants to split them into three groups by age. If a third of the class is under 11 years, and two-fifths are above 11 but under 13, how many students will be in the third group (13 years and above)?",
        "completions": [
            "Step 1: A third of the class is under 11 years because 11 - 1/3 = <<11-1/3=7>>7.",
            "Step 2: Two-fifths of the class are above 11 but under 13 because 2/5 * 11 = <<2/5*11=8>>8.",
            "Step 3: There are 45 students, so the third group will have 45 - 7 - 8 = <<45-7-8=20>>20 students. The answer is: 20",
        ],
        "labels": [True, False, False],
    }

    separator = "\n"  # It's important to use the same separator as the one used during training

    for idx in range(1, len(example["completions"]) + 1):
        steps = example["completions"][0:idx]
        text = separator.join((example["prompt"], *steps)) + separator  # Add a separator between the prompt and each steps
        pred_entity = pipe(text)[-1]["entity"]
        pred = {"LABEL_0": False, "LABEL_1": True}[pred_entity]
        label = example["labels"][idx - 1]
        print(f"Step {idx}\tPredicted: {pred} \tLabel: {label}")

    Step 1  Predicted: True         Label: True
    Step 2  Predicted: False        Label: False
    Step 3  Predicted: False        Label: False

It’s a win!

##
Expected dataset type

PRM requires a stepwise supervision. The dataset should contain the following columns: `prompt`, `completions` and `labels`, where `completions` contains a list of reasoning steps and `labels` a list of booleans or floats indicating the correctness of each step.

The PRMTrainer only supports standard dataset format.

##
Example script

We provide an example script to train a model using the PRM method. The script is available in `examples/scripts/prm.py`

To use the PRM script with the Qwen2 0.5B model on the Math Shepherd dataset, run the following command:

    accelerate launch examples/scripts/prm.py \
        --model_name_or_path Qwen/Qwen2-0.5B \
        --dataset_name trl-lib/math_shepherd \
        --num_train_epochs 1 \
        --logging_steps 25 \
        --output_dir Qwen2-0.5B-Reward-Math-Sheperd

##
PRMTrainer

###
class trl.PRMTrainer

< source >

( model: typing.Union[transformers.modeling_utils.PreTrainedModel, torch.nn.modules.module.Module, NoneType] = None args: typing.Optional[trl.trainer.prm_config.PRMConfig] = None data_collator: typing.Optional[transformers.data.data_collator.DataCollator] = None train_dataset: typing.Optional[datasets.arrow_dataset.Dataset] = None eval_dataset: typing.Union[datasets.arrow_dataset.Dataset, dict[str, datasets.arrow_dataset.Dataset], NoneType] = None processing_class: typing.Union[transformers.tokenization_utils_base.PreTrainedTokenizerBase, transformers.image_processing_utils.BaseImageProcessor, transformers.feature_extraction_utils.FeatureExtractionMixin, transformers.processing_utils.ProcessorMixin, NoneType] = None model_init: typing.Optional[typing.Callable[[], transformers.modeling_utils.PreTrainedModel]] = None compute_metrics: typing.Optional[typing.Callable[[transformers.trainer_utils.EvalPrediction], dict]] = None callbacks: typing.Optional[list[transformers.trainer_callback.TrainerCallback]] = None optimizers: tuple = (None, None) preprocess_logits_for_metrics: typing.Optional[typing.Callable[[torch.Tensor, torch.Tensor], torch.Tensor]] = None peft_config: typing.Optional[dict] = None )

Parameters

- model (`transformers.PreTrainedModel`) — The model to train, preferably an `AutoModelForTokenClassification`.
- args (`PRMConfig`) — The arguments to use for training.
- data_collator (`transformers.DataCollator`) — The data collator to use for training. If None is specified, the default data collator (`DataCollatorForTokenClassification`) will be used which will pad the sequences to the maximum length of the sequences in the batch, given a dataset of paired sequences.
- train_dataset (`datasets.Dataset`) — The dataset to use for training.
- eval_dataset (`datasets.Dataset`) — The dataset to use for evaluation.
- processing_class (`PreTrainedTokenizerBase` or `BaseImageProcessor` or `FeatureExtractionMixin` or `ProcessorMixin`, optional) — Processing class used to process the data. If provided, will be used to automatically process the inputs for the model, and it will be saved along the model to make it easier to rerun an interrupted training or reuse the fine-tuned model.
- model_init (`Callable[[], transformers.PreTrainedModel]`) — The model initializer to use for training. If None is specified, the default model initializer will be used.
- compute_metrics (`Callable[[transformers.EvalPrediction], dict]`, optional defaults to `compute_accuracy`) — The metrics to use for evaluation. If no metrics are specified, the default metric (`compute_accuracy`) will be used.
- callbacks (`list[transformers.TrainerCallback]`) — The callbacks to use for training.
- optimizers (`tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LambdaLR]`) — The optimizer and scheduler to use for training.
- preprocess_logits_for_metrics (`Callable[[torch.Tensor, torch.Tensor], torch.Tensor]`) — The function to use to preprocess the logits before computing the metrics.
- peft_config (`dict`, defaults to `None`) — The PEFT configuration to use for training. If you pass a PEFT configuration, the model will be wrapped in a PEFT model.

Initialize PRMTrainer.

####
create_model_card

< source >

( model_name: typing.Optional[str] = None dataset_name: typing.Optional[str] = None tags: typing.Union[str, list[str], NoneType] = None )

Parameters

- model_name (`str` or `None`, optional, defaults to `None`) — Name of the model.
- dataset_name (`str` or `None`, optional, defaults to `None`) — Name of the dataset used for training.
- tags (`str`, `list[str]` or `None`, optional, defaults to `None`) — Tags to be associated with the model card.

Creates a draft of a model card using the information available to the `Trainer`.

####
tokenize_row

< source >

( features tokenizer step_separator max_length max_prompt_length max_completion_length train_on_last_step_only is_eval ) →
`dict[str, list[int]]`

Parameters

- features (`dict[str, str]`) — Row of the dataset, should contain the keys `"prompt"`, `"completions"`, and `"labels"`.
- tokenizer (`PreTrainedTokenizerBase`) — Tokenizer used to process the data.
- step_separator (`str`) — Separator between steps in the completion.
- max_length (`int` or `None`) — Maximum length of the sequences (prompt + completion). If `None`, the sequences are not truncated.
- max_prompt_length (`int` or `None`) — Maximum length of the prompt. If `None`, the prompt is not truncated.
- max_completion_length (`int` or `None`) — Maximum length of the completion sequences. If `None`, the completion sequences are not truncated.
- train_on_last_step_only (`bool`) — Whether to train only on the last step. If `True`, the labels are `-100` for all tokens except the last token of the completion.
- is_eval (`bool`) — Whether the function is used to tokenize samples from a training or an evaluation dataset. Used only if `train_on_last_step_only` is set to `True`.

Returns

`dict[str, list[int]]`

Tokenized sequences with the keys `"input_ids"`, and `“labels”.

Tokenize a row of the dataset.

Example:

    >>> from transformers import AutoTokenizer
    >>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
    >>> features = {"prompt": "Which number is larger, 9.8 or 9.11?",
    ...             "completions": ["11 is greater than 8.",
    ...                             "Hence, 9.11 > 9.8."],
    ...             "labels": [True, False]}
    >>> PRMTrainer.tokenize_row(features, tokenizer, "\n", max_completion_length=None, train_on_last_step_only=False, is_eval=False)
    {'input_ids': [23085, 1372, 374, 8131, 11, 220, 24, 13, 23, 476, 220, 24, 13, 16, 16, 30, 16, 16, 374, 7046, 1091, 220, 23, 13, 198, 39, 763, 11, 220, 24, 13, 16, 16, 861, 220, 24, 13, 23, 13, 198],
     'labels': [-100, -100, -100, -100, -100, -100, -100, -100, 1, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, 0]}

##
PRMConfig

###
class trl.PRMConfig

< source >

( output_dir: typing.Optional[str] = None overwrite_output_dir: bool = False do_train: bool = False do_eval: bool = False do_predict: bool = False eval_strategy: typing.Union[transformers.trainer_utils.IntervalStrategy, str] = 'no' prediction_loss_only: bool = False per_device_train_batch_size: int = 8 per_device_eval_batch_size: int = 8 per_gpu_train_batch_size: typing.Optional[int] = None per_gpu_eval_batch_size: typing.Optional[int] = None gradient_accumulation_steps: int = 1 eval_accumulation_steps: typing.Optional[int] = None eval_delay: typing.Optional[float] = 0 torch_empty_cache_steps: typing.Optional[int] = None learning_rate: float = 1e-05 weight_decay: float = 0.0 adam_beta1: float = 0.9 adam_beta2: float = 0.999 adam_epsilon: float = 1e-08 max_grad_norm: float = 1.0 num_train_epochs: float = 3.0 max_steps: int = -1 lr_scheduler_type: typing.Union[transformers.trainer_utils.SchedulerType, str] = 'linear' lr_scheduler_kwargs: typing.Union[dict, str, NoneType] = <factory> warmup_ratio: float = 0.0 warmup_steps: int = 0 log_level: typing.Optional[str] = 'passive' log_level_replica: typing.Optional[str] = 'warning' log_on_each_node: bool = True logging_dir: typing.Optional[str] = None logging_strategy: typing.Union[transformers.trainer_utils.IntervalStrategy, str] = 'steps' logging_first_step: bool = False logging_steps: float = 500 logging_nan_inf_filter: bool = True save_strategy: typing.Union[transformers.trainer_utils.SaveStrategy, str] = 'steps' save_steps: float = 500 save_total_limit: typing.Optional[int] = None save_safetensors: typing.Optional[bool] = True save_on_each_node: bool = False save_only_model: bool = False restore_callback_states_from_checkpoint: bool = False no_cuda: bool = False use_cpu: bool = False use_mps_device: bool = False seed: int = 42 data_seed: typing.Optional[int] = None jit_mode_eval: bool = False use_ipex: bool = False bf16: bool = False fp16: bool = False fp16_opt_level: str = 'O1' half_precision_backend: str = 'auto' bf16_full_eval: bool = False fp16_full_eval: bool = False tf32: typing.Optional[bool] = None local_rank: int = -1 ddp_backend: typing.Optional[str] = None tpu_num_cores: typing.Optional[int] = None tpu_metrics_debug: bool = False debug: typing.Union[str, list[transformers.debug_utils.DebugOption]] = '' dataloader_drop_last: bool = False eval_steps: typing.Optional[float] = None dataloader_num_workers: int = 0 dataloader_prefetch_factor: typing.Optional[int] = None past_index: int = -1 run_name: typing.Optional[str] = None disable_tqdm: typing.Optional[bool] = None remove_unused_columns: typing.Optional[bool] = True label_names: typing.Optional[list[str]] = None load_best_model_at_end: typing.Optional[bool] = False metric_for_best_model: typing.Optional[str] = None greater_is_better: typing.Optional[bool] = None ignore_data_skip: bool = False fsdp: typing.Union[list[transformers.trainer_utils.FSDPOption], str, NoneType] = '' fsdp_min_num_params: int = 0 fsdp_config: typing.Union[dict, str, NoneType] = None fsdp_transformer_layer_cls_to_wrap: typing.Optional[str] = None accelerator_config: typing.Union[dict, str, NoneType] = None deepspeed: typing.Union[dict, str, NoneType] = None label_smoothing_factor: float = 0.0 optim: typing.Union[transformers.training_args.OptimizerNames, str] = 'adamw_torch' optim_args: typing.Optional[str] = None adafactor: bool = False group_by_length: bool = False length_column_name: typing.Optional[str] = 'length' report_to: typing.Union[NoneType, str, list[str]] = None ddp_find_unused_parameters: typing.Optional[bool] = None ddp_bucket_cap_mb: typing.Optional[int] = None ddp_broadcast_buffers: typing.Optional[bool] = None dataloader_pin_memory: bool = True dataloader_persistent_workers: bool = False skip_memory_metrics: bool = True use_legacy_prediction_loop: bool = False push_to_hub: bool = False resume_from_checkpoint: typing.Optional[str] = None hub_model_id: typing.Optional[str] = None hub_strategy: typing.Union[transformers.trainer_utils.HubStrategy, str] = 'every_save' hub_token: typing.Optional[str] = None hub_private_repo: typing.Optional[bool] = None hub_always_push: bool = False gradient_checkpointing: bool = False gradient_checkpointing_kwargs: typing.Union[dict, str, NoneType] = None include_inputs_for_metrics: bool = False include_for_metrics: list = <factory> eval_do_concat_batches: bool = True fp16_backend: str = 'auto' push_to_hub_model_id: typing.Optional[str] = None push_to_hub_organization: typing.Optional[str] = None push_to_hub_token: typing.Optional[str] = None mp_parameters: str = '' auto_find_batch_size: bool = False full_determinism: bool = False torchdynamo: typing.Optional[str] = None ray_scope: typing.Optional[str] = 'last' ddp_timeout: typing.Optional[int] = 1800 torch_compile: bool = False torch_compile_backend: typing.Optional[str] = None torch_compile_mode: typing.Optional[str] = None include_tokens_per_second: typing.Optional[bool] = False include_num_input_tokens_seen: typing.Optional[bool] = False neftune_noise_alpha: typing.Optional[float] = None optim_target_modules: typing.Union[NoneType, str, list[str]] = None batch_eval_metrics: bool = False eval_on_start: bool = False use_liger_kernel: typing.Optional[bool] = False eval_use_gather_object: typing.Optional[bool] = False average_tokens_across_devices: typing.Optional[bool] = False max_length: typing.Optional[int] = 1024 max_prompt_length: typing.Optional[int] = 512 max_completion_length: typing.Optional[int] = None disable_dropout: bool = True step_separator: str = '\n' train_on_last_step_only: bool = False dataset_num_proc: typing.Optional[int] = None )

Parameters

- learning_rate (`float`, optional, defaults to `1e-5`) — Initial learning rate for `AdamW` optimizer. The default value replaces that of TrainingArguments.
- max_length (`int` or `None`, optional, defaults to `1024`) — Maximum length of the sequences (prompt + completion) used for truncation.
- max_prompt_length (`int` or `None`, optional, defaults to `512`) — Maximum length of the prompt used for truncation.
- max_completion_length (`int` or `None`, optional, defaults to `None`) — Maximum length of the completion used for truncation. The completion is the concatenation of the steps.
- disable_dropout (`bool`, optional, defaults to `True`) — Whether to disable dropout in the model.
- step_separator (`str`, optional, defaults to `"\n"`) — Separator used to separate each step of the reasoning process.
- train_on_last_step_only (`bool`, optional, defaults to `False`) — Whether to train only on the last step.
- dataset_num_proc (`int`, optional, defaults to `None`) — Number of processes to use for processing the dataset.

Configuration class for the PRMTrainer.

Using HfArgumentParser we can turn this class into argparse arguments that can be specified on the command line.

< > Update on GitHub