To compare the loss functions in SFT (Supervised Fine-Tuning) with PPO, DPO, and GRPO, we need to look at the respective implementations in the provided files from the repository. Here is a breakdown of the loss calculations for each:
1. SFT (Supervised Fine-Tuning) Loss
In SFT, the objective is to maximize the likelihood of the correct output sequence. This corresponds to minimizing the cross-entropy loss between the predicted distribution and the ground truth tokens.
Code Reference: sft_trainer.py, in the _prepare_non_packed_dataloader method
Loss Function: SFT relies on a straightforward cross-entropy loss for sequence modeling.
python
outputs = processing_class(
element[dataset_text_field] if formatting_func is None else formatting_func(element),
add_special_tokens=add_special_tokens,
truncation=True,
padding=False,
max_length=max_seq_length,
return_overflowing_tokens=False,
return_length=False,
)
The labels are added as a copy of input_ids, with padding tokens set to -100 (ignored in loss computation).
Loss Objective: Cross-Entropy Loss.
2. PPO (Proximal Policy Optimization) Loss
In PPO, the goal is to optimize the policy while ensuring that updates do not deviate too far from the reference policy. The objective includes an advantage term and a KL constraint to balance exploration and exploitation.
Code Reference: ppo_trainer.py, in the train method.
Loss Function: PPO uses an adapted policy gradient loss with clipping.
Policy Loss: Enforces updates based on the advantage.
Value Function Loss: Ensures accurate value approximation.
Entropy Bonus: Encourages exploration (optional).
3. DPO (Direct Preference Optimization) Loss
In DPO, the goal is to align the language model using preference data, optimizing the log-likelihood of the preferred completion while penalizing the likelihood of the rejected one.
Code Reference: dpo_trainer.py, in the compute_loss method.
Loss Function: Binary cross-entropy for preference alignment.
python
loss = F.logsigmoid(beta * (chosen_logits - rejected_logits)).mean()
Components:
Chosen Logits: Logits for preferred responses.
Rejected Logits: Logits for non-preferred responses.
Beta Scaling: Adjusts the preference margin.
4. GRPO (Group Relative Policy Optimization) Loss
In GRPO, the objective is to maximize the relative advantage of completions while maintaining proximity to the reference policy using KL divergence.
Code Reference: grpo_trainer.py, in the compute_loss method.
Loss Function: Advantage-weighted log-probability loss with KL penalty.