AGENT SKILL

huggingface-trl

Using Hugging Face TRL library for building custom trainers for Preference Optimization methods like DPO, SimPO, etc.

License 许可
MIT
最近更新
2026-10-02
Stars
★ 92
Forks
7

01 前言 Front Matter

name
huggingface-trl
description
Using Hugging Face TRL library for building custom trainers for Preference Optimization methods like DPO, SimPO, etc.

03 SKILL.md 正文

Hugging Face TRL (Transformer Reinforcement Learning)

TRL is a library by Hugging Face that provides tools to train language models using Reinforcement Learning and Preference Optimization (like PPO, DPO, etc.).

Custom Trainers

To implement custom loss functions like SimPO, you can subclass the DPOTrainer and override its specific methods (like dpo_loss or in this case, simpo_loss).

from trl import DPOTrainer
import torch
import torch.nn.functional as F

class CustomTrainer(DPOTrainer):
    def custom_loss(self, policy_chosen_logps, policy_rejected_logps):
        pi_logratios = policy_chosen_logps - policy_rejected_logps
        logits = pi_logratios - self.margin
        losses = -F.logsigmoid(self.beta * logits)
        return losses

Setup & Dependencies

Commonly requires transformers, torch, trl, and accelerate. Make sure you have the matching versions as defined in the project's requirement file (e.g. environment.yml or requirements.txt).

04 同仓库更多 Skills