Buckets:

hf-doc-build/doc-dev / trl /pr_5607 /en /cpo_trainer.md
HuggingFaceDocBuilder's picture
|
download
raw
23 kB

CPO Trainer

model badge

Overview

Contrastive Preference Optimization (CPO) as introduced in the paper Contrastive Preference Optimization: Pushing the Boundaries of LLM Performance in Machine Translation by Haoran Xu, Amr Sharaf, Yunmo Chen, Weiting Tan, Lingfeng Shen, Benjamin Van Durme, Kenton Murray, and Young Jin Kim. At a high level, CPO trains models to avoid generating adequate, but not perfect, translations in Machine Translation (MT) tasks. However, CPO is a general approximation of the DPO loss and can be applied to other domains, such as chat.

CPO aims to mitigate two fundamental shortcomings of SFT. First, SFT’s methodology of minimizing the discrepancy between predicted outputs and gold-standard references inherently caps model performance at the quality level of the training data. Secondly, SFT lacks a mechanism to prevent the model from rejecting mistakes in translations. The CPO objective is derived from the DPO objective.

Quick start

This example demonstrates how to train a model using the CPO method. We use the Qwen 0.5B model as the base model. We use the preference data from the UltraFeedback dataset. You can view the data in the dataset here:

Below is the script to train the model:

# train_cpo.py
from datasets import load_dataset
from trl.experimental.cpo import CPOConfig, CPOTrainer
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
train_dataset = load_dataset("trl-lib/ultrafeedback_binarized", split="train")

training_args = CPOConfig(output_dir="Qwen2-0.5B-CPO")
trainer = CPOTrainer(model=model, args=training_args, processing_class=tokenizer, train_dataset=train_dataset)
trainer.train()

Execute the script using the following command:

accelerate launch train_cpo.py

Expected dataset type

CPO requires a preference dataset. The experimental.cpo.CPOTrainer supports both conversational and standard dataset formats. When provided with a conversational dataset, the trainer will automatically apply the chat template to the dataset.

Example script

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

To test the CPO script with the Qwen2 0.5B model on the UltraFeedback dataset, run the following command:

accelerate launch examples/scripts/cpo.py \
    --model_name_or_path Qwen/Qwen2-0.5B-Instruct \
    --dataset_name trl-lib/ultrafeedback_binarized \
    --num_train_epochs 1 \
    --output_dir Qwen2-0.5B-CPO

Logged metrics

While training and evaluating, we record the following reward metrics:

  • rewards/chosen: the mean log probabilities of the policy model for the chosen responses scaled by beta
  • rewards/rejected: the mean log probabilities of the policy model for the rejected responses scaled by beta
  • rewards/accuracies: mean of how often the chosen rewards are > than the corresponding rejected rewards
  • rewards/margins: the mean difference between the chosen and corresponding rejected rewards
  • nll_loss: the mean negative log likelihood loss of the policy model for the chosen responses

CPO variants

Simple Preference Optimization (SimPO)

Simple Preference Optimization (SimPO) by Yu Meng, Mengzhou Xia, and Danqi Chen proposes a simpler and more effective preference optimization algorithm than DPO without using a reference model. The key designs in SimPO are (1) using length-normalized log likelihood as the implicit reward, and (2) incorporating a target reward margin in the Bradley-Terry ranking objective. The official code can be found at princeton-nlp/SimPO.

The abstract from the paper is the following:

Direct Preference Optimization (DPO) is a widely used offline preference optimization algorithm that reparameterizes reward functions in reinforcement learning from human feedback (RLHF) to enhance simplicity and training stability. In this work, we propose SimPO, a simpler yet more effective approach. The effectiveness of SimPO is attributed to a key design: using the average log probability of a sequence as the implicit reward. This reward formulation better aligns with model generation and eliminates the need for a reference model, making it more compute and memory efficient. Additionally, we introduce a target reward margin to the Bradley-Terry objective to encourage a larger margin between the winning and losing responses, further enhancing the algorithm's performance. We compare SimPO to DPO and its latest variants across various state-of-the-art training setups, including both base and instruction-tuned models like Mistral and Llama3. We evaluated on extensive instruction-following benchmarks, including AlpacaEval 2, MT-Bench, and the recent challenging Arena-Hard benchmark. Our results demonstrate that SimPO consistently and significantly outperforms existing approaches without substantially increasing response length. Specifically, SimPO outperforms DPO by up to 6.4 points on AlpacaEval 2 and by up to 7.5 points on Arena-Hard. Our top-performing model, built on Llama3-8B-Instruct, achieves a remarkable 44.7 length-controlled win rate on AlpacaEval 2 -- surpassing Claude 3 Opus on the leaderboard, and a 33.8 win rate on Arena-Hard -- making it the strongest 8B open-source model.

The SimPO loss is integrated in the experimental.cpo.CPOTrainer, as it's an alternative loss that adds a reward margin, allows for length normalization, and does not use BC regularization. To use this loss, just turn on loss_type="simpo" and cpo_alpha=0.0 in the experimental.cpo.CPOConfig and set the simpo_gamma to a recommended value.

CPO-SimPO

We also offer the combined use of CPO and SimPO, which enables more stable training and improved performance. Learn more details at CPO-SimPO GitHub. To use this method, simply enable SimPO by setting loss_type="simpo" and a non-zero cpo_alpha in the experimental.cpo.CPOConfig.

AlphaPO

The AlphaPO -- Reward shape matters for LLM alignment (AlphaPO) method by Aman Gupta, Shao Tang, Qingquan Song, Sirou Zhu, Jiwoo Hong, Ankan Saha, Viral Gupta, Noah Lee, Eunki Kim, Jason Zhu, Natesh Pillai, and S. Sathiya Keerthi is also implemented in the experimental.cpo.CPOTrainer. AlphaPO is an alternative method that applies a transformation to the reward function shape in the context of SimPO loss. The abstract from the paper is the following:

Reinforcement Learning with Human Feedback (RLHF) and its variants have made huge strides toward the effective alignment of large language models (LLMs) to follow instructions and reflect human values. More recently, Direct Alignment Algorithms (DAAs) have emerged in which the reward modeling stage of RLHF is skipped by characterizing the reward directly as a function of the policy being learned. Some popular examples of DAAs include Direct Preference Optimization (DPO) and Simple Preference Optimization (SimPO). These methods often suffer from likelihood displacement, a phenomenon by which the probabilities of preferred responses are often reduced undesirably. In this paper, we argue that, for DAAs the reward (function) shape matters. We introduce AlphaPO, a new DAA method that leverages an α-parameter to help change the shape of the reward function beyond the standard log reward. AlphaPO helps maintain fine-grained control over likelihood displacement and overoptimization. Compared to SimPO, one of the best performing DAAs, AlphaPO leads to about 7% to 10% relative improvement in alignment performance for the instruct versions of Mistral-7B and Llama3-8B while achieving 15% to 50% relative improvement over DPO on the same models. The analysis and results presented highlight the importance of the reward shape and how one can systematically change it to affect training dynamics, as well as improve alignment performance.

To use this loss as described in the paper, we can set the loss_type="alphapo" which automatically sets loss_type="simpo" and cpo_alpha=0.0, together with alpha and simpo_gamma to recommended values in the experimental.cpo.CPOConfig. Alternatively, you can manually set loss_type="simpo", cpo_alpha=0.0, together with alpha and simpo_gamma to recommended values. Other variants of this method are also possible, such as setting loss_type="ipo" and alpha to any non-zero value.

Loss functions

The CPO algorithm supports several loss functions. The loss function can be set using the loss_type parameter in the experimental.cpo.CPOConfig. The following loss functions are supported:

loss_type= Description
"sigmoid" (default) Given the preference data, we can fit a binary classifier according to the Bradley-Terry model, and in fact, the DPO authors propose the sigmoid loss on the normalized likelihood via the logsigmoid to fit a logistic regression.
"hinge" The RSO authors propose to use a hinge loss on the normalized likelihood from the SLiC paper. In this case, the beta is the reciprocal of the margin.
"ipo" The IPO authors provide a deeper theoretical understanding of the DPO algorithms and identify an issue with overfitting and propose an alternative loss. In this case, the beta is the reciprocal of the gap between the log-likelihood ratios of the chosen vs the rejected completion pair, and thus the smaller the beta, the larger this gap is. As per the paper, the loss is averaged over log-likelihoods of the completion (unlike DPO, which is summed only).
"simpo" The SimPO method is also implemented in the experimental.cpo.CPOTrainer. SimPO is an alternative loss that adds a reward margin, allows for length normalization, and does not use BC regularization. To use this loss, simply set loss_type="simpo" and cpo_alpha=0.0 in the experimental.cpo.CPOConfig and simpo_gamma to a recommended value.
"alphapo" The AlphaPO method is also implemented in the experimental.cpo.CPOTrainer. This is syntactic sugar that automatically sets loss_type="simpo" and cpo_alpha=0.0. AlphaPO applies a transformation to the reward function shape in the context of SimPO loss when the alpha parameter is non-zero.

For Mixture of Experts Models: Enabling the auxiliary loss

MOEs are the most efficient if the load is about equally distributed between experts.
To ensure that we train MOEs similarly during preference-tuning, it is beneficial to add the auxiliary loss from the load balancer to the final loss.

This option is enabled by setting output_router_logits=True in the model config (e.g., MixtralConfig).
To scale how much the auxiliary loss contributes to the total loss, use the hyperparameter router_aux_loss_coef=... (default: 0.001) in the model config.

CPOTrainer[[trl.experimental.cpo.CPOTrainer]]

trl.experimental.cpo.CPOTrainer[[trl.experimental.cpo.CPOTrainer]]

Source

Initialize CPOTrainer.

traintrl.experimental.cpo.CPOTrainer.trainhttps://github.com/huggingface/trl/blob/vr_5607/transformers/trainer.py#L1323[{"name": "resume_from_checkpoint", "val": ": str | bool | None = None"}, {"name": "trial", "val": ": optuna.Trial | dict[str, Any] | None = None"}, {"name": "ignore_keys_for_eval", "val": ": list[str] | None = None"}]- resume_from_checkpoint (str or bool, optional) -- If a str, local path to a saved checkpoint as saved by a previous instance of Trainer. If a bool and equals True, load the last checkpoint in args.output_dir as saved by a previous instance of Trainer. If present, training will resume from the model/optimizer/scheduler states loaded here.

  • trial (optuna.Trial or dict[str, Any], optional) -- The trial run or the hyperparameter dictionary for hyperparameter search.
  • ignore_keys_for_eval (list[str], optional) -- A list of keys in the output of your model (if it is a dictionary) that should be ignored when gathering predictions for evaluation during the training.0~trainer_utils.TrainOutputObject containing the global step count, training loss, and metrics.

Main training entry point.

Parameters:

model (PreTrainedModel) : The model to train, preferably an AutoModelForSequenceClassification.

args (experimental.cpo.CPOConfig) : The CPO config arguments to use for training.

data_collator (DataCollator) : The data collator to use for training. If None is specified, the default data collator (experimental.utils.DPODataCollatorWithPadding) 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 (Dataset) : The dataset to use for training.

eval_dataset (Dataset) : The dataset to use for evaluation.

processing_class (PreTrainedTokenizerBase, BaseImageProcessor, 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.

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.

compute_metrics (Callable[[EvalPrediction], dict], optional) : The function to use to compute the metrics. Must take a EvalPrediction and return a dictionary string to metric values.

Returns:

~trainer_utils.TrainOutput

Object containing the global step count, training loss, and metrics.

save_model[[trl.experimental.cpo.CPOTrainer.save_model]]

Source

Will save the model, so you can reload it using from_pretrained().

Will only save from the main process.

push_to_hub[[trl.experimental.cpo.CPOTrainer.push_to_hub]]

Source

Upload self.model and self.processing_class to the 🤗 model hub on the repo self.args.hub_model_id.

Parameters:

commit_message (str, optional, defaults to "End of training") : Message to commit while pushing.

blocking (bool, optional, defaults to True) : Whether the function should return only when the git push has finished.

token (str, optional, defaults to None) : Token with write permission to overwrite Trainer's original args.

revision (str, optional) : The git revision to commit from. Defaults to the head of the "main" branch.

kwargs (dict[str, Any], optional) : Additional keyword arguments passed along to ~Trainer.create_model_card.

Returns:

The URL of the repository where the model was pushed if blocking=False, or a Future object tracking the progress of the commit if blocking=True.

CPOConfig[[trl.experimental.cpo.CPOConfig]]

trl.experimental.cpo.CPOConfig[[trl.experimental.cpo.CPOConfig]]

Source

Configuration class for the experimental.cpo.CPOTrainer.

This class includes only the parameters that are specific to CPO training. For a full list of training arguments, please refer to the TrainingArguments documentation. Note that default values in this class may differ from those in TrainingArguments.

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

These parameters have default values different from TrainingArguments:

  • logging_steps: Defaults to 10 instead of 500.
  • gradient_checkpointing: Defaults to True instead of False.
  • bf16: Defaults to True if fp16 is not set, instead of False.
  • learning_rate: Defaults to 1e-6 instead of 5e-5.

Parameters:

max_length (int or None, optional, defaults to 1024) : Maximum length of the sequences (prompt + completion) in the batch. This argument is required if you want to use the default data collator.

max_completion_length (int, optional) : Maximum length of the completion. This argument is required if you want to use the default data collator and your model is an encoder-decoder.

beta (float, optional, defaults to 0.1) : Parameter controlling the deviation from the reference model. Higher β means less deviation from the reference model. For the IPO loss (loss_type="ipo"), β is the regularization parameter denoted by τ in the paper.

label_smoothing (float, optional, defaults to 0.0) : Label smoothing factor. This argument is required if you want to use the default data collator.

loss_type (str, optional, defaults to "sigmoid") : Type of loss to use. Possible values are: - "sigmoid": sigmoid loss from the original DPO paper. - "hinge": hinge loss on the normalized likelihood from the SLiC paper. - "ipo": IPO loss from the IPO paper. - "simpo": SimPO loss from the SimPO paper. - "alphapo": AlphaPO loss from the AlphaPO paper. This automatically sets loss_type="simpo" and cpo_alpha=0.0.

disable_dropout (bool, optional, defaults to True) : Whether to disable dropout in the model.

cpo_alpha (float, optional, defaults to 1.0) : Weight of the BC regularizer in CPO training.

simpo_gamma (float, optional, defaults to 0.5) : Target reward margin for the SimPO loss, used only when the loss_type="simpo".

alpha (float, optional, defaults to 0.0) : Alpha parameter that controls reward function shape across all loss types. When alpha=0 (default), uses standard log probability rewards. When alpha != 0, applies AlphaPO transformation: r = (1 - p^(-alpha)) / alpha from the AlphaPO paper. This parameter works with all loss types.

generate_during_eval (bool, optional, defaults to False) : If True, generates and logs completions from the model to W&B or Comet during evaluation.

is_encoder_decoder (bool, optional) : When using the model_init argument (callable) to instantiate the model instead of the model argument, you need to specify if the model returned by the callable is an encoder-decoder model.

model_init_kwargs (dict[str, Any], optional) : Keyword arguments to pass to AutoModelForCausalLM.from_pretrained when instantiating the model from a string.

dataset_num_proc (int, optional) : Number of processes to use for processing the dataset.

Xet Storage Details

Size:
23 kB
·
Xet hash:
de9a71a090f40cee840374ec41d73a5fd02088893bd1331a75950f375e918708

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.