Transformers documentation

TRL

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.22.2).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

TRL

TRL 是一个用于基础模型的后训练框架。它包含 SFT、GRPO 和 DPO 等方法。每种方法都有一个专用的训练器,基于 Trainer 类构建,可从单 GPU 扩展到多节点集群。

from datasets import load_dataset
from trl import GRPOTrainer
from trl.rewards import accuracy_reward

dataset = load_dataset("trl-lib/DeepMath-103K", split="train")

trainer = GRPOTrainer(
    model="Qwen/Qwen2-0.5B-Instruct",
    reward_funcs=accuracy_reward,
    train_dataset=dataset,
)
trainer.train()

Transformers 集成

TRL 扩展了 Transformers 的 API,并添加了针对特定方法的设置。

  • TRL 训练器基于 Trainer 构建。针对特定方法的训练器(如 GRPOTrainer)会额外提供生成、奖励评分和损失计算功能。配置类在 TrainingArguments 的基础上扩展了特定于该方法的字段。

  • 模型加载使用 AutoConfig.from_pretrained(),然后通过该类的 from_pretrained 从配置实例化模型类。

资源

Update on GitHub