implement-simpo-loss
/implement-simpo-loss
Use this skill to implement the simpo_loss function in SimPOTrainer based on the SimPO paper. The loss combines a length-normalized reward with a margin gamma and uses BCE loss without a reference model.
- Author
- cxcscmu ↗
- Repository
- SkillLearnBench ↗
- Licence
- MIT
- Last repository change
- 02.10.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 02.10.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Use this skill to implement the simpo_loss function in SimPOTrainer based on the SimPO paper. The loss combines a length-normalized reward with a margin gamma and uses BCE loss without a reference model.
Original author description
Use this skill to implement the simpo_loss function in SimPOTrainer based on the SimPO paper. The loss combines a length-normalized reward with a margin gamma and uses BCE loss without a reference model.
How it works
- Implement SimPO Loss
SKILL.md preview
---
name: implement-simpo-loss
description: Use this skill to implement the simpo_loss function in SimPOTrainer based on the SimPO paper. The loss combines a length-normalized reward with a margin gamma and uses BCE loss without a reference model.
---
## Implement SimPO Loss
The SimPO loss is defined in the paper as:
```
L_SimPO = -E[ log σ( (β/|y_w|) * log π(y_w|x) - (β/|y_l|) * log π(y_l|x) - γ ) ]
```
Where:
- `β` is a scaling factor
- `γ` (gamma) is a target reward margin
- `|y_w|` and `|y_l|` are the lengths of winning and losing responses
- `π(y|x)` is the policy model probability
### Implementation
```python
def simpo_loss(
self,
policy_chosen_logps: torch.FloatTensor,
policy_rejected_logps: torch.FloatTensor,
policy_chosen_logps_avg: torch.FloatTensor = None,
policy_rejected_logps_avg: torch.FloatTensor = None,
) -> Tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor]:
"""Compute the SimPO loss for a batch of policy model log probabilities.
Args:
policy_chosen_logps: Log probabilities of the policy model for chosen responses.
Shape: (batch_size,)
policy_rejected_logps: Log probabilities of the policy model for rejected responses.
Shape: (batch_size,)
policy_chosen_logps_avg: Average log probabilities (per token) for chosen responses.
Shape: (batch_size,)
policy_rejected_logps_avg: Average log probabilities (per token) for rejected responses.
Shape: (batch_size,)
Returns:
A tuple of three tensors: (losses, chosen_rewards, rejected_rewards)
"""
# Use average log probs (length-normalized) as the reward signal
# If avg logps not provided separately, they should be the per-token average
chosen_logratios = policy_chosen_logps_avg if policy_chosen_logps_avg is not None else policy_chosen_logps
rejected_logratios = policy_rejected_logps_avg if policy_rejected_logps_avg is not None else policy_rejected_logps
# SimPO reward: beta * (avg_logp_chosen - avg_logp_rejected) - gamma
# This is the length-normalized version without reference model
pi_logratios = chosen_logratios - rejected_logratios
# Apply beta scaling and subtract gamma margin
logits = self.beta * pi_logratios - self.gamma
# Compute loss: -log(sigmoid(logits))
if self.loss_type == "sigmoid":
losses = -F.logsigmoid(logits)
elif self.loss_type == "hinge":
losses = torch.relu(1 - logits)
else:
raise ValueError(f"Unknown loss type: {self.loss_type}")
# Compute rewards for logging
chosen_rewards = self.beta * chosen_logratios.detach()
rejected_rewards = self.beta * rejected_logratios.detach()
return losses, chosen_rewards, rejected_rewards
```
### Key Points
- **No reference model**: SimPO does not use a reference model, unlike DPO
- **Length normalization**: Uses average log probabilities (divided by sequence length)
- **Gamma margin**: Adds a target reward margin γ to encourage a gap between chosen and rejected
- **Beta scaling**: Controls the strength of the reward signal
Versions
The database has no other skills named implement-simpo-loss.
Database snapshot: 13.11.2026 · package version de0bdc1786a4 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 95simpo-loss-implementation
How to implement the SimPO (Simple Preference Optimization) loss function based on the SimPO paper. Use this when implementing the simpo_loss method in SimPOTrainer. - 100simpo-loss
Implement the SimPO (Simple Preference Optimization) loss function for LLM alignment. Use this skill whenever implementing SimPO training objectives, reference-free reward optimization, or Bradley-Terry preference loss with target reward margin. Triggers on: SimPO, preference… - 95simpo-training
Simple Preference Optimization for LLM alignment. Reference-free alternative to DPO with better performance (+6.4 points on AlpacaEval 2.0). No reference model needed, more efficient than DPO. Use for preference alignment when want simpler, faster training than DPO/PPO. - 100preference-optimization
Align a fine-tuned model with preference data using DPO, ORPO, KTO, or SimPO. Use when preference pairs or thumbs-up/down feedback exist, when choosing between preference-optimization methods, or when a DPO run needs hyperparameters or debugging.