TRL documentation

Examples

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

and get access to the augmented documentation experience

to get started

Examples

The examples/ directory contains a collection of self-contained examples that demonstrate how to use the TRL library for various applications. Each example lives in its own folder named after the method and the task it demonstrates (e.g. grpo_wordle, sft_gpt_oss), and holds everything the example needs: scripts, notebooks, prompts, chat templates, and evaluation code.

Basic single-trainer training scripts are not examples: they live in trl/scripts and are exposed through the command line interface (trl sft, trl dpo, trl grpo, …). Each trainer’s documentation page also contains a complete runnable snippet.

Shared resources sit at the root of examples/:

  • examples/accelerate_configs: 🤗 Accelerate configuration files for multi-GPU, DeepSpeed ZeRO, FSDP, and context-parallel setups, used by many examples.
  • examples/datasets: the scripts used to generate the trl-lib datasets used across the examples.

Getting Started

Install TRL and additional dependencies as follows:

pip install --upgrade trl[quantization]

Check for additional optional dependencies here. Notebook-based examples are self-contained and can run on free Colab; script-based examples run on single-GPU, multi-GPU, or DeepSpeed setups (see Distributed Training below).

Index

ExampleDescriptionOpen in Colab
async_distillation_mathAsync on-policy distillation on GSM8K with experimental.async_distillation.AsyncDistillationTrainer: the teacher is served over HTTP with vLLM, including a multi-teacher (MOPD) math + code variant.
async_grpo_mathAsynchronous GRPO on GSM8K with experimental.async_grpo.AsyncGRPOTrainer, decoupling generation (vLLM server) from training.
async_grpo_opencodeAsyncGRPO training of the real opencode coding agent on an OpenEnv environment (loop-owning: the external agent runs its own tool loop and TRL trains on its captured proxy trace), with a local subprocess sandbox or remote Hugging Face sandboxes.
dpo_reduce_hallucinationsDPO fine-tuning of a Vision Language Model to reduce hallucinations using the openbmb/RLAIF-V-Dataset dataset.
gold_chatbot_arenaGeneral Online Logit Distillation (GOLD) of a Qwen2 teacher into a Llama 3.2 student (cross-tokenizer) on chatbot_arena_completions with experimental.gold.GOLDTrainer, with full-training and LoRA variants.
gold_qwen3_vlGeneral Online Logit Distillation (GOLD) of Qwen3-VL-8B into smaller VLM students with experimental.gold.GOLDTrainer, covering same-family (JSD loss) and cross-family (ULD loss) distillation.
grpo_2048GRPO with tool calling to teach a model to play the 2048 game.
grpo_browsergymGRPO with the BrowserGym OpenEnv environment, with LLM and VLM variants.
grpo_carlaGRPO with the CARLA autonomous-driving OpenEnv environment, with LLM and VLM variants (multimodal camera-image tool responses).
grpo_catchGRPO with the Catch (OpenSpiel) OpenEnv environment.
grpo_continuous_batchingGRPO with transformers’ continuous batching engine for faster generation on large batches with variable completion lengths.
grpo_echoMinimal GRPO training with the Echo OpenEnv environment.
grpo_harborGRPO training against a Harbor task suite with a pluggable base agent (bash / jupyter / terminal_notes harnesses). See the Harbor Integration guide.
grpo_ministral3_vlGRPO Ministral 3 with QLoRA on free Colab.Open In Colab
grpo_multi_envMulti-environment GRPO training: Wordle + Catch OpenEnv environments in the same training run.
grpo_qloraGRPO using QLoRA on free Colab.Open In Colab
grpo_qwen3_vlGRPO Qwen3-VL with QLoRA on free Colab.Open In Colab
grpo_rnj_1_instructGRPO on rnj-1-instruct with QLoRA on Colab to add reasoning capabilities.Open In Colab
grpo_setaGRPO training against the SETA ORS environment on the openreward.ai catalog. See the OpenReward Integration guide.
grpo_sql_agentGRPO to train an agent that answers questions by querying a SQL database (script and notebook; not runnable on free Colab due to OOM).
grpo_sudokuGRPO to play Sudoku on an OpenEnv environment (script and notebook).Open In Colab
grpo_visual_mathGRPO fine-tuning of a multimodal model for reasoning using the lmms-lab/multimodal-open-r1-8k-verified dataset.
grpo_wordleGRPO to play Wordle (TextArena) on an OpenEnv environment (script and notebook).Open In Colab
gspo_mathGSPO via the GRPOTrainer for math reasoning on the AI-MO/NuminaMath-TIR dataset.
gspo_visual_mathGSPO via the GRPOTrainer to fine-tune a multimodal model for reasoning using the lmms-lab/multimodal-open-r1-8k-verified dataset.
mpo_visual_preferencesMPO via the DPOTrainer to align a multimodal model based on preferences using the HuggingFaceH4/rlaif-v_formatted dataset and a set of loss weights.
online_dpo_visual_mathOnline DPO fine-tuning of a Vision Language Model with experimental.online_dpo.OnlineDPOTrainer.
ppo_sentimentPPO with experimental.ppo.PPOTrainer to continue text with positive sentiment or physically descriptive language.
ppo_tldrPPO with experimental.ppo.PPOTrainer to generate TL;DR summaries.
rloo_mathRLOO with the RLOOTrainer for math reasoning on the AI-MO/NuminaMath-TIR dataset with vLLM.
rloo_visual_mathRLOO fine-tuning of a multimodal model for reasoning using the lmms-lab/multimodal-open-r1-8k-verified dataset.
sdft_privileged_contextSelf-distillation fine-tuning with experimental.sdft.SDFTTrainer, distilling privileged (teacher-only) context into the model.
sdpo_mathSDPO with experimental.sdpo.SDPOTrainer using verifiable math rewards and optional environment feedback on openai/gsm8k.
sft_diffusion_gemmaSFT of the DiffusionGemma block-diffusion language model on GSM8K by extending the SFTTrainer with a block-diffusion objective.
sft_gemma3SFT of Gemma 3 on the Codeforces COTS dataset.
sft_gemma3_visionSFT of Gemma 3 on vision to text tasks.
sft_gpt_ossSFT of openai/gpt-oss-20b.
sft_ministral3_vlSFT Ministral 3 with QLoRA on free Colab.Open In Colab
sft_nemotron_3SFT of NVIDIA Nemotron 3 models (script and LoRA notebook).Open In Colab
sft_qloraSFT using QLoRA on free Colab.Open In Colab
sft_qwen3_vlSFT Qwen3-VL with QLoRA on free Colab.Open In Colab
sft_tool_callingTeaching tool calling to a model without native tool-calling support using SFT with QLoRA (script, chat template, and notebook).Open In Colab
sft_visual_chatSFT of a Vision Language Model in a chat setting. Only tested with LLaVA 1.5, LLaVA 1.6, and Llama-3.2-11B-Vision-Instruct; users may see unexpected behaviour in other model architectures.
ssd_codegenSimple Self-Distillation for code generation with experimental.ssd.SSDTrainer, plus evaluation on LiveCodeBench.
tpo_ultrafeedbackTriple Preference Optimization with experimental.tpo.TPOTrainer using the tpo-alignment/triple-preference-ultrafeedback-40K dataset.

Distributed Training

You can run the example scripts on multiple GPUs with 🤗 Accelerate:

accelerate launch --config_file=examples/accelerate_configs/multi_gpu.yaml --num_processes {NUM_GPUS} path_to_script.py --all_arguments_of_the_script

For DeepSpeed ZeRO-{1,2,3}:

accelerate launch --config_file=examples/accelerate_configs/deepspeed_zero{1,2,3}.yaml --num_processes {NUM_GPUS} path_to_script.py --all_arguments_of_the_script

Adjust NUM_GPUS and --all_arguments_of_the_script as needed.

Update on GitHub