simpo-loss-implementation
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 92
- Forks
- 7
03 SKILL.md 正文
name: simpo-loss-implementation description: Guidance on implementing the SimPO (Simple Preference Optimization) loss function in a trainer class. Use this skill when you need to implement or debug the SimPO loss, ensuring correct use of beta, gamma_beta_ratio, and average log probabilities.
SimPO Loss Implementation Guide
SimPO is a reference-free preference optimization algorithm. The key design is using the average log probability of a sequence as the implicit reward and introducing a target reward margin.
Reward Formulation
The implicit reward for a response $y$ given prompt $x$ is: $$r(x, y) = \frac{\beta}{|y|} \log \pi_\theta(y | x)$$ where $|y|$ is the number of tokens in the response.
SimPO Loss Formula
The SimPO loss for a pair of winning ($y_w$) and losing ($y_l$) responses is:
$$L_{SimPO} = -\log \sigma \left( \frac{\beta}{|y_w|} \log \pi_\theta(y_w | x) - \frac{\beta}{|y_l|} \log \pi_\theta(y_l | x) - \gamma \right)$$
In implementation, we often use gamma = gamma_beta_ratio * beta.
Implementation Details
When implementing simpo_loss in a SimPOTrainer class:
- Input Probabilities: Ensure
policy_chosen_logpsandpolicy_rejected_logpsare the average log probabilities (log-likelihood normalized by length). - Margin Calculation: Calculate
gammaasself.gamma_beta_ratio * self.beta. - Logits: Compute the logits as:
logits = self.beta * policy_chosen_logps - self.beta * policy_rejected_logps - gamma - Loss Types:
- Sigmoid:
losses = -F.logsigmoid(logits) - Hinge:
losses = torch.relu(1 - logits)
- Sigmoid:
- Rewards: Return the rewards for logging:
chosen_rewards = self.beta * policy_chosen_logpsrejected_rewards = self.beta * policy_rejected_logps
Example Snippet
def simpo_loss(self, policy_chosen_logps, policy_rejected_logps):
gamma = self.gamma_beta_ratio * self.beta
logits = self.beta * policy_chosen_logps - self.beta * policy_rejected_logps - gamma
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}")
chosen_rewards = self.beta * policy_chosen_logps
rejected_rewards = self.beta * policy_rejected_logps
return losses, chosen_rewards, rejected_rewards
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.