simpo-loss
暂无简介
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
03 SKILL.md 正文
SimPO Loss Implementation
Paper Reference
"SimPO: Simple Preference Optimization with a Reference-Free Reward" (Meng et al., 2024)
Core Formula
The SimPO objective (Eq. 6 from paper):
L_SimPO(π_θ) = -E[(x,yw,yl)~D] [log σ(β/|yw| * log π_θ(yw|x) - β/|yl| * log π_θ(yl|x) - γ)]
Where:
β(beta): scaling factor for rewardsγ(gamma): target reward margin =gamma_beta_ratio * beta|yw|,|yl|: sequence lengths of winning/losing responses (used for length normalization)σ: sigmoid function
Key Design: Length-Normalized Reward
The implicit reward is the average log probability (not sum):
r(x, y) = β/|y| * log π_θ(y|x) = β * avg_log_prob(y)
This aligns with the generation metric (average log-likelihood).
Implementation
def simpo_loss(self, policy_chosen_logps, policy_rejected_logps):
"""
Args:
policy_chosen_logps: Average log probs for chosen. Shape: (batch_size,)
policy_rejected_logps: Average log probs for rejected. Shape: (batch_size,)
Note: inputs are already length-normalized (average_log_prob=True upstream)
"""
# Compute rewards (beta * avg_log_prob)
chosen_rewards = self.beta * policy_chosen_logps
rejected_rewards = self.beta * policy_rejected_logps
# gamma = gamma_beta_ratio * beta
gamma = self.gamma_beta_ratio * self.beta
# Reward difference with target margin
logits = chosen_rewards - rejected_rewards - gamma
if self.loss_type == "sigmoid":
# With optional label smoothing
losses = (
-F.logsigmoid(logits) * (1 - self.label_smoothing)
- F.logsigmoid(-logits) * self.label_smoothing
)
elif self.loss_type == "hinge":
losses = torch.relu(1 - logits)
else:
raise ValueError(f"Unknown loss type: {self.loss_type}")
return losses, chosen_rewards, rejected_rewards
Config Parameters
From SimPOConfig:
beta: float = 2.0- reward scalinggamma_beta_ratio: float = 0.25- γ/β ratio, so γ = 0.25 * 2.0 = 0.5 by defaultlabel_smoothing: float = 0- smoothing for sigmoid lossloss_type: Literal["sigmoid", "hinge"] = "sigmoid"
Notes
gamma_beta_ratiorepresents γ/β, so actual gamma =gamma_beta_ratio * beta- The logps passed in are already averaged (from
get_batch_logpswithaverage_log_prob=True) - Rewards are beta-scaled average log probabilities
- No reference model needed (reference-free)
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.