To compare the loss calculations used in the SFT (Supervised Fine-Tuning) trainer with those used in other preference optimization methods like PPO (Proximal Policy Optimization), DPO (Direct Preference Optimization), and GRPO (Group Relative Policy Optimization), we will focus on the SFTTrainer, PPOTrainer, DPOTrainer, and GRPOTrainer classes from the provided code snippet.
1. SFT Loss Code
The SFTTrainer class, which focuses on supervised fine-tuning, does not explicitly show a loss computation method within the provided code. However, it typically relies on the built-in loss computation from the transformers library, which is usually based on cross-entropy for language modeling tasks. The actual loss function is defined in the parent Trainer class, where the loss is normally calculated during the forward pass of the model using the labels provided in the training data.
2. PPO Loss Code
The PPOTrainer class defines its loss calculation in the train method. Here’s a breakdown of how the PPO loss is computed:
PPO Loss Calculation: The loss is calculated using the Proximal Policy Optimization technique. The key steps include:
Policy Loss: It uses the ratio of new to old probabilities (log probabilities) to compute the policy loss with clipping to ensure that updates are not too large.
Value Function Loss: The loss for the value function is calculated separately.
Entropy Bonus: An entropy term is added to encourage exploration.
The relevant code snippet for computing PPO loss is:
python
# Compute new logprobs
new_logprobs = ...
ratio = ...
# PPO clipped loss
pg_losses = -mb_advantage * ratio
pg_losses2 = -mb_advantage * torch.clamp(ratio, 1.0 - args.cliprange, 1.0 + args.cliprange)
pg_loss_max = torch.max(pg_losses, pg_losses2)
pg_loss = pg_loss_max.mean()
# Final loss
loss = pg_loss
3. DPO Loss Code
The DPOTrainer class has a method called compute_loss, which is responsible for calculating the loss during training. The loss is computed based on the rewards assigned to chosen and rejected samples. Here's how it works:
Logits Calculation: It computes the logits for chosen and rejected inputs.
Loss Calculation: The loss is computed based on the difference between the chosen and rejected logits, typically using a binary cross-entropy or logistic loss approach.
The relevant code snippet for computing DPO loss is:
python
rewards_chosen = model(input_ids=inputs["input_ids_chosen"], ...)
rewards_rejected = model(input_ids=inputs["input_ids_rejected"], ...)
# calculate loss
loss = -nn.functional.logsigmoid(rewards_chosen - rewards_rejected).mean()
4. GRPO Loss Code
The GRPOTrainer class computes its loss in a similar manner to DPO but with a focus on group-based comparisons. The key aspects of the GRPO loss computation involve:
Reward Calculation: The rewards are computed for both the chosen and rejected responses.
Loss Calculation: The GRPO loss is also based on the log probabilities of the chosen versus rejected items, leveraging the concept of relative comparisons.
Relevant code snippets for GRPO loss calculation include:
python
loss = self.loss(advantages, self.config.train_clip_range, ratio)
Summary of Comparisons
Trainer
Type of Loss
Key Components
SFTTrainer
Typically Cross-Entropy (not explicitly shown)
Relies on the parent Trainer class for standard loss calculation; uses the model's logits against labels.
PPOTrainer
Clipped Policy Gradient Loss
Uses ratios of probabilities, includes value function loss and an entropy bonus to encourage exploration.
DPOTrainer
Logistic Loss (Binary Cross-Entropy)
Compares logits of chosen vs rejected items to compute rewards and subsequently the loss.
GRPOTrainer
Similar to DPO, relative comparisons
Rewards are computed for chosen and rejected responses, focusing on relative performance.
Conclusion
SFT is primarily about fitting the model to labeled data, leveraging standard loss functions like cross-entropy.
PPO and DPO focus on optimizing policies based on rewards, with PPO implementing a clipping mechanism to stabilize learning.
GRPO extends this concept further by considering group comparisons, which enhances the robustness of the learned policy.
These differences highlight the specialized nature of each method in addressing the unique challenges of supervised fine-tuning versus preference optimization in reinforcement learning contexts.