https://github.com/huggingface/trl
Train transformer language models with reinforcement learning.
https://github.com/huggingface/trl
Last synced: 1 day ago
JSON representation
Train transformer language models with reinforcement learning.
- Host: GitHub
- URL: https://github.com/huggingface/trl
- Owner: huggingface
- License: apache-2.0
- Created: 2020-03-27T10:54:55.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2026-11-24T22:21:27.000Z (6 days ago)
- Last Synced: 2026-11-24T22:27:39.107Z (6 days ago)
- Language: Python
- Homepage: http://hf.co/docs/trl
- Size: 50.8 MB
- Stars: 19,815
- Watchers: 104
- Forks: 3,173
- Open Issues: 297
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Security: SECURITY.md
- Agents: AGENTS.md
- Claude: CLAUDE.md
Awesome Lists containing this project
- awesome-manus - trl - Transformer Reinforcement Learning library (Model Training & Fine-tuning)
- awesome-llm-cost - Hugging Face TRL - Transformer Reinforcement Learning library with student teacher pipelines for distilling large model behaviour into smaller fine tuned models. (Quantization, Distillation, and Compression / Distillation)
- awesome-python - huggingface/trl - Train transformer language models with reinforcement learning. (Other)
- agentsmeetrl - trl - <img src="https://img.shields.io/github/stars/huggingface/trl?style=for-the-badge&logo=github&logoColor=white&labelColor=181717&color=ffd700" alt="Stars"> (🔧 Base Framework)
- awesomeopd - trl - <img src="https://img.shields.io/github/stars/huggingface/trl?style=for-the-badge&logo=github&logoColor=white&labelColor=181717&color=ffd700" alt="Stars"> (🛠️ Frameworks & Toolkits)
- awesome-private-ai - TRL - Hugging Face's library for SFT, DPO, GRPO, and reward-model training on top of transformers. (Fine-Tuning & Adapters)
- awesome-llmops - TRL - Train transformer language models with reinforcement learning. (Training / Foundation Model Fine Tuning)
- Awesome-Long-Chain-of-Thought-Reasoning - Hugging Face TRL - 由 Hugging Face 官方维护的 RLHF 全栈库,集成了 SFT、GRPO、DPO、奖励建模(Reward Modeling)等模块,支持多种模型架构与分布式扩展,是社区中最活跃的 RLHF 工具之一。 (简单易用,快速上手,社区活跃) (4. 算法 / 4.2 Reinforcement Learning)
- awesome-open-weight-models - TRL - 10k+ (Fine-Tuning / Frameworks)
- awesome-mistral - Hugging Face TRL - ⭐ 19k+ – RLHF and DPO training. (Fine-Tuning & Training / Fine-Tuning Frameworks)
- awesome-llm-prod - TRL - Full-stack library to post-train transformers: SFT, DPO, GRPO, PPO and reward modeling (Large Language Models)
- awesome-free-models - Hugging Face TRL - Transformer Reinforcement Learning library. SFT, PPO, DPOTrainer, GRPOTrainer for aligning models. (🎛 Fine-tuning Tools)
- fucking-awesome-python - trl - A library for post-training transformer language models with SFT, DPO, GRPO, and other trainers. (Projects / AI and Agents)
- llmops - TRL - Transformer Reinforcement Learning (Training & Fine-Tuning / Fine-Tuning Tools)
- awesome-nlp-llm-spanish-espa-ol - trl (HuggingFace) - Para entrenamiento de aprendizaje por refuerzo (reinforcement learning) con distintas técnicas de RLHF como SFT, PPO, DPO (🛠️ Librerías para Usar NLP en Español)
- awesome-local-llm - trl - train transformer language models with reinforcement learning (Tools / Training and Fine-tuning)
- awesome-python - trl - A library for post-training transformer language models with SFT, DPO, GRPO, and other trainers. (Projects / AI and Agents)
- awesome-production-machine-learning - TRL -  - Train transformer language models with reinforcement learning. (Industry Strength Reinforcement Learning)
- awesome-ai-safety - TRL - Language-model post-training library with supervised fine-tuning and preference or reinforcement-learning trainers. (Alignment and control / Post-training and preference learning)
- Awesome-AI-Engineering - TRL - HuggingFace's official library for RLHF and post-training — the broadest algorithm coverage (SFT, DPO, GRPO, PPO, RLOO) with first-party OpenEnv integration. (📑 Table of Contents / 🛠️ Hands-on Projects & Tools)
- awesome - huggingface/trl - Train transformer language models with reinforcement learning. (Python)
- awesome-python-data-science - TRL - Train transformer language models with reinforcement learning. (Graph Machine Learning)
- awesome-ai-agent-stack - huggingface/trl - Train transformer language models with reinforcement learning. (Interpretability, Alignment & Research)
- awesome-prompts - TRL -  (Agent Ecosystem / Agent Training (RL Post-Training))
- awesome-local-ai - TRL - RLHF, DPO, PPO training (Fine-tuning & Quantization (18))
- awesome-production-llm - trl - (`HuggingFace`)  Train transformer language models with reinforcement learning. (LLM Training / Finetuning)
- github-ultimate-repos - huggingface/trl - | ⭐ 19,600★ | 🍴 3,098 | 💻 `Python`🔗 <https://github.com/huggingface/trl>📝 Train transformer language models with reinforcement learning.⚙️ `Tokenizer vocabulary encoding -> SafeTensors zero-copy tensor loading -> Transformer multi-head attention graph execution -> loss backward pass / token decoding.` (📜 Master Categorized Repository Directory (100% Clickable Links) / [02] ARTIFICIAL INTELLIGENCE, LARGE LANGUAGE MODELS, FOUNDATION MODELS & QUANTIZATION)
- awesome-llm-tools - TRL (HuggingFace) - HuggingFace library for RL-based LLM training (9. Fine-Tuning / Training Frameworks)
- awesome-nlp - TRL - reference library for SFT, DPO, GRPO, and RLHF. (Language Models for NLP / Instruction Tuning and Preference Optimization)
- awesome-agentic-ai - TRL - Tools for RLHF and other preference-based optimization. (Language Models / Fine-tuning)
- awesome-selfhosted-ai - TRL - Library for post-training language models with SFT, DPO and GRPO on top of Transformers. (Fine-Tuning and Training)
- awesome-local-ai - TRL - Language model alignment with reinforcement learning. (Training)
- awesome-opensource-ai - TRL (Transformers Reinforcement Learning) - Official library for RLHF, SFT, DPO, ORPO.  (7. Training & Fine-tuning Ecosystem)
- awesome-craft-ai - TRL - Train transformer language models with reinforcement learning and related post-training methods.  (Hugging Face)
- awesome-os-llm - TRL - Hugging Face's library for SFT, DPO, PPO, and related post-training. The default open alignment toolkit. (Training, finetune and alignment / Finetune and alignment)
- awesome-vision-ai-stack - TRL - Preference optimization and instruction-tuning workflows. (Training and Fine-Tuning / Libraries)
- awesome-deep-reasoning - https://github.com/huggingface/trl
- awesome-artificial-intelligence-research - TRL - Hugging Face library for SFT, DPO, GRPO, reward modeling, and RLHF post-training. (Foundation Models, LLMs, and Agents / Reasoning, Post-Training, and Synthetic Data)
README
# TRL - Transformers Reinforcement Learning
A comprehensive library to post-train foundation models
## 🎉 What's New
**🔥 Beam:** Fine-tune [Beam](https://huggingface.co/reflection/Beam-1.0-501B-A23B) with [`SFTTrainer`](https://huggingface.co/docs/trl/sft_trainer) on a single node of 8 B300s. See the [LoRA example](https://github.com/huggingface/trl/tree/main/examples/sft_beam).
**🔥 Muse Spark Open:** Fine-tune [Muse Spark Open](https://huggingface.co/meta-models/Muse-Spark-Open-816B-A42B) with [`SFTTrainer`](https://huggingface.co/docs/trl/sft_trainer), sharded across your GPUs at load time. See the [8-node LoRA example](https://github.com/huggingface/trl/tree/main/examples/sft_muse_spark_open).
**🚀 Training on AWS Trainium:** TRL's trainers now run on [AWS Trainium](https://huggingface.co/docs/trl/aws_trainium) through TorchNeuron, PyTorch's native Neuron backend.
## Overview
TRL is a cutting-edge library designed for post-training foundation models using advanced techniques like Supervised Fine-Tuning (SFT), Group Relative Policy Optimization (GRPO), and Direct Preference Optimization (DPO). Built on top of the [🤗 Transformers](https://github.com/huggingface/transformers) ecosystem, TRL supports a variety of model architectures and modalities, and can be scaled-up across various hardware setups.
## Highlights
- **Trainers**: Various fine-tuning methods are easily accessible via trainers like [`SFTTrainer`](https://huggingface.co/docs/trl/sft_trainer), [`GRPOTrainer`](https://huggingface.co/docs/trl/grpo_trainer), [`DPOTrainer`](https://huggingface.co/docs/trl/dpo_trainer), [`KTOTrainer`](https://huggingface.co/docs/trl/kto_trainer) and more.
- **Efficient and scalable**:
- Leverages [🤗 Accelerate](https://github.com/huggingface/accelerate) to scale from single GPU to multi-node clusters using methods like [DDP](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html) and [DeepSpeed](https://github.com/deepspeedai/DeepSpeed).
- Full integration with [🤗 PEFT](https://github.com/huggingface/peft) enables training on large models with modest hardware via quantization and LoRA/QLoRA.
- Integrates [🦥 Unsloth](https://github.com/unslothai/unsloth) for accelerating training using optimized kernels.
- **Command Line Interface (CLI)**: A simple interface lets you fine-tune with models without needing to write code.
## Installation
### Python Package
Install the library using `pip`:
```bash
pip install trl
```
### From source
If you want to use the latest features before an official release, you can install TRL from source:
```bash
pip install git+https://github.com/huggingface/trl.git
```
### Repository
If you want to use the examples you can clone the repository with the following command:
```bash
git clone https://github.com/huggingface/trl.git
```
## Quick Start
For more flexibility and control over training, TRL provides dedicated trainer classes to post-train language models or PEFT adapters on a custom dataset. Each trainer in TRL is a light wrapper around the 🤗 Transformers trainer and natively supports distributed training methods like DDP, DeepSpeed ZeRO, and FSDP.
### `SFTTrainer`
Here is a basic example of how to use the [`SFTTrainer`](https://huggingface.co/docs/trl/sft_trainer):
```python
from trl import SFTTrainer
from datasets import load_dataset
dataset = load_dataset("trl-lib/Capybara", split="train")
trainer = SFTTrainer(
model="Qwen/Qwen2.5-0.5B",
train_dataset=dataset,
)
trainer.train()
```
### `GRPOTrainer`
[`GRPOTrainer`](https://huggingface.co/docs/trl/grpo_trainer) implements the [Group Relative Policy Optimization (GRPO) algorithm](https://huggingface.co/papers/2402.03300) that is more memory-efficient than PPO and was used to train [Deepseek AI's R1](https://huggingface.co/deepseek-ai/DeepSeek-R1).
```python
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.5-0.5B-Instruct",
reward_funcs=accuracy_reward,
train_dataset=dataset,
)
trainer.train()
```
> [!NOTE]
> For reasoning models, use the `reasoning_accuracy_reward()` function for better results.
### `DPOTrainer`
[`DPOTrainer`](https://huggingface.co/docs/trl/dpo_trainer) implements the popular [Direct Preference Optimization (DPO) algorithm](https://huggingface.co/papers/2305.18290) that was used to post-train [Llama 3](https://huggingface.co/papers/2407.21783) and many other models. Here is a basic example of how to use the `DPOTrainer`:
```python
from datasets import load_dataset
from trl import DPOTrainer
dataset = load_dataset("trl-lib/ultrafeedback_binarized", split="train")
trainer = DPOTrainer(
model="Qwen/Qwen3-0.6B",
train_dataset=dataset,
)
trainer.train()
```
### `KTOTrainer`
[`KTOTrainer`](https://huggingface.co/docs/trl/kto_trainer) implements the [Kahneman-Tversky Optimization (KTO) algorithm](https://huggingface.co/papers/2402.01306), which aligns models from simple binary (desirable / undesirable) feedback rather than paired preferences. Here is a basic example of how to use the `KTOTrainer`:
```python
from datasets import load_dataset
from trl import KTOTrainer
dataset = load_dataset("trl-lib/kto-mix-14k", split="train")
trainer = KTOTrainer(
model="Qwen/Qwen3-0.6B",
train_dataset=dataset,
)
trainer.train()
```
### `RewardTrainer`
Here is a basic example of how to use the [`RewardTrainer`](https://huggingface.co/docs/trl/reward_trainer):
```python
from trl import RewardTrainer
from datasets import load_dataset
dataset = load_dataset("trl-lib/ultrafeedback_binarized", split="train")
trainer = RewardTrainer(
model="Qwen/Qwen2.5-0.5B-Instruct",
train_dataset=dataset,
)
trainer.train()
```
## Command Line Interface (CLI)
You can use the TRL Command Line Interface (CLI) to quickly get started with post-training methods like Supervised Fine-Tuning (SFT) or Direct Preference Optimization (DPO):
**SFT:**
```bash
trl sft --model_name_or_path Qwen/Qwen2.5-0.5B \
--dataset_name trl-lib/Capybara \
--output_dir Qwen2.5-0.5B-SFT
```
**DPO:**
```bash
trl dpo --model_name_or_path Qwen/Qwen2.5-0.5B-Instruct \
--dataset_name argilla/Capybara-Preferences \
--output_dir Qwen2.5-0.5B-DPO
```
**KTO:**
```bash
trl kto --model_name_or_path Qwen/Qwen2.5-0.5B-Instruct \
--dataset_name trl-lib/kto-mix-14k \
--output_dir Qwen2.5-0.5B-KTO
```
Read more about CLI in the [relevant documentation section](https://huggingface.co/docs/trl/clis) or use `--help` for more details.
## Development
If you want to contribute to `trl` or customize it to your needs make sure to read the [contribution guide](https://github.com/huggingface/trl/blob/main/CONTRIBUTING.md) and make sure you make a dev install:
```bash
git clone https://github.com/huggingface/trl.git
cd trl/
pip install -e .[dev]
```
## Experimental
A minimal incubation area is available under `trl.experimental` for unstable / fast-evolving features. Anything there may change or be removed in any release without notice.
Example:
```python
from trl.experimental.new_trainer import NewTrainer
```
Read more in the [Experimental docs](https://huggingface.co/docs/trl/experimental_overview).
## Citation
```bibtex
@software{vonwerra2020trl,
title = {{TRL: Transformers Reinforcement Learning}},
author = {von Werra, Leandro and Belkada, Younes and Tunstall, Lewis and Beeching, Edward and Thrush, Tristan and Lambert, Nathan and Huang, Shengyi and Rasul, Kashif and Gallouédec, Quentin},
license = {Apache-2.0},
url = {https://github.com/huggingface/trl},
year = {2020}
}
```
## License
This repository's source code is available under the [Apache-2.0 License](LICENSE).