pytorch-preference-optimization
Guide for implementing preference optimization methods (DPO, SimPO, IPO) in PyTorch. Use when implementing loss functions for RLHF-style training.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- pytorch-preference-optimization
- description
- Guide for implementing preference optimization methods (DPO, SimPO, IPO) in PyTorch. Use when implementing loss functions for RLHF-style training.
03 SKILL.md 正文
PyTorch Preference Optimization
Common Pattern
All preference optimization methods take:
policy_chosen_logps: log probs for preferred responsespolicy_rejected_logps: log probs for rejected responses
DPO Loss
# Requires reference model log probs
logits = (policy_chosen_logps - ref_chosen_logps) - (policy_rejected_logps - ref_rejected_logps)
losses = -F.logsigmoid(beta * logits)
SimPO Loss (Reference-Free)
# No reference model needed - uses length-normalized log probs
pi_logratios = policy_chosen_logps - policy_rejected_logps # already avg'd per token
logits = pi_logratios - gamma_beta_ratio # gamma_beta_ratio = gamma/beta
losses = -F.logsigmoid(beta * logits)
Tips
F.logsigmoid(x)is numerically more stable thantorch.log(torch.sigmoid(x))- For label smoothing:
loss = -logsigmoid(logits)*(1-ls) - logsigmoid(-logits)*ls - Hinge loss:
torch.relu(1 - beta * logits) - Always
.detach()reward tensors to prevent gradient flow through them
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.