evo-simpo-code-reproduction
Overview
Complete skill for reproducing the SimPO (Simple Preference Optimization) loss function from the paper by Meng et al. (2024). Handles implementation of the simpo_loss method, environment setup, unit test execution, and saving results.
Key Concepts (from the paper)
SimPO Loss Formula
L_SimPO = -log(sigmoid(beta/|y_w| * sum(log π(y_w|x)) - beta/|y_l| * sum(log π(y_l|x)) - gamma))
Critical Implementation Details
- Inputs are pre-normalized:
policy_chosen_logpsandpolicy_rejected_logpsare already length-averaged (mean log probs per token) before enteringsimpo_loss. The averaging happens inget_batch_logpswithaverage_log_prob=True.
- Margin factoring: The code computes
logits = pi_logratios - gamma/beta, then appliesbeta * logitsinside logsigmoid. This is algebraically equivalent tobeta * pi_logratios - gamma.
- gamma computation: In the SimPO trainer,
gamma = self.gamma_beta_ratio * self.beta(wheregamma_beta_ratiodefaults to ~0.25, giving gamma≈0.5 for beta=2.0). The paper recommends gamma/beta ≈ 0.5.
- Loss types:
- Sigmoid (default):
losses = -F.logsigmoid(beta * logits) * (1 - label_smoothing) - F.logsigmoid(-beta * logits) * label_smoothing - Hinge:
losses = torch.relu(1 - beta * logits)
- Rewards are detached:
chosen_rewards = beta * chosen_logps.detach(),rejected_rewards = beta * rejected_logps.detach()
- Return shape: Per-example losses of shape
(batch_size,)— reduction happens upstream.
Sigmoid Loss with Label Smoothing (label_smoothing=0 by default)
When label_smoothing=0, the second term vanishes, leaving pure SimPO loss:
losses = -F.logsigmoid(self.beta * logits)
When label_smoothing > 0:
losses = (
-F.logsigmoid(self.beta * logits) * (1 - self.label_smoothing)
- F.logsigmoid(-self.beta * logits) * self.label_smoothing
)
Environment Setup
- Python 3.10+
pip install torch transformers 'trl==0.8.6' datasets accelerate peft numpy- trl>=0.29 removed CPOTrainer; use trl==0.8.6 for compatibility with the SimPO codebase
Usage
import sys
sys.path.insert(0, '/app/environment/skills/evo-simpo-code-reproduction/scripts')
from simpo_loss_impl import implement_simpo_loss
from run_and_save import setup_environment, run_unit_test, log_python_info
# Step 1: Setup environment
setup_environment()
# Step 2: Implement the loss function
implement_simpo_loss('/root/SimPO/scripts/simpo_trainer.py')
# Step 3: Run unit test and save results
run_unit_test()
# Step 4: Log python info
log_python_info()
Common Pitfalls
- Sum vs Mean: SimPO uses MEAN log probs (already done upstream), not sum. Don't re-normalize.
- gamma/beta factoring: The margin is
gamma/betasubtracted from logratios, then multiplied by beta. Don't subtract gamma directly before beta scaling. - Detach rewards: Always
.detach()rewards to prevent memory leaks. - Hinge loss formula: Uses
torch.relu(1 - beta * logits), nottorch.relu(-logits). - Device handling: Some implementations move tensors to accelerator device; in unit tests without accelerator, this may need to be handled gracefully.