# Examples

The [`examples/`](https://github.com/huggingface/trl/tree/main/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_chess`), 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`](https://github.com/huggingface/trl/tree/main/trl/scripts) and are exposed through the [command line interface](clis) (`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/deepspeed_configs`](https://github.com/huggingface/trl/tree/main/examples/deepspeed_configs): DeepSpeed ZeRO configuration files, passed with `--deepspeed`, used by many examples.
- [`examples/fsdp_configs`](https://github.com/huggingface/trl/tree/main/examples/fsdp_configs): FSDP configuration files, passed with `--fsdp --fsdp_config` (a bare `--fsdp` on the command line needs transformers 5.10 or later; with older versions, set `fsdp=True` in the training config).
- [`examples/datasets`](https://github.com/huggingface/trl/tree/main/examples/datasets): the scripts used to generate the `trl-lib` datasets used across the examples.

**Getting Started**

Install TRL and additional dependencies as follows:

```bash
pip install --upgrade trl[quantization]
```

Check for additional optional dependencies [here](https://github.com/huggingface/trl/blob/main/pyproject.toml). 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](#distributed-training) below).

## Index

| Example | Description | Open in Colab |
| --- | --- | --- |
| [`async_distillation_math`](https://github.com/huggingface/trl/tree/main/examples/async_distillation_math) | Async on-policy distillation on GSM8K with [experimental.async_distillation.AsyncDistillationTrainer](/docs/trl/v1.19.0/en/async_distillation_trainer#trl.experimental.async_distillation.AsyncDistillationTrainer): the teacher is served over HTTP with vLLM. | |
| [`async_distillation_mopd`](https://github.com/huggingface/trl/tree/main/examples/async_distillation_mopd) | Multi-teacher on-policy distillation (MOPD) with [experimental.async_distillation.AsyncDistillationTrainer](/docs/trl/v1.19.0/en/async_distillation_trainer#trl.experimental.async_distillation.AsyncDistillationTrainer): fuses the math, code and instruction-following experts of Open-MOPD into their SFT checkpoint: 17.5 to 24.2 on AIME24 and 67.5 to 73.0 on IFEval in 60 steps (script, Hugging Face Jobs launcher and README with the results). | |
| [`async_grpo_harbor`](https://github.com/huggingface/trl/tree/main/examples/async_grpo_harbor) | AsyncGRPO training on a [Harbor](https://openenv.github.io/OpenEnv/main/environments/harbor) task suite through any installed coding agent (`mini-swe-agent`, `opencode`, `claude-code`, ...) served over [OpenEnv](openenv): the agent owns its own loop in a sandbox and TRL trains on the token ids and logprobs recorded by the capture proxy. | |
| [`async_grpo_math`](https://github.com/huggingface/trl/tree/main/examples/async_grpo_math) | Asynchronous GRPO on GSM8K with [AsyncGRPOTrainer](/docs/trl/v1.19.0/en/async_grpo_trainer#trl.AsyncGRPOTrainer), decoupling generation (vLLM server) from training, including a LoRA variant that syncs only the adapter to the vLLM server. | |
| [`async_grpo_s3_aggregation`](https://github.com/huggingface/trl/tree/main/examples/async_grpo_s3_aggregation) | Multi-agent team RL with [AsyncGRPOTrainer](/docs/trl/v1.19.0/en/async_grpo_trainer#trl.AsyncGRPOTrainer): a replication of the information-aggregation task from [s3](https://huggingface.co/papers/2611.08615), where four Qwen3-0.6B agents message each other to pool private numbers and one rollout is a whole team episode. 50 updates take held-out team success from 4.7% to 60.9% (harness, training and evaluation scripts, and README with the results). | |
| [`distillation_data_analysis_env`](https://github.com/huggingface/trl/tree/main/examples/distillation_data_analysis_env) | On-policy distillation of a data-analysis agent with [DistillationTrainer](/docs/trl/v1.19.0/en/distillation_trainer#trl.DistillationTrainer) and `environment_factory`: a Qwen3.5-2B student answers questions about real data files over up to 10 turns in the [OpenEnv](openenv) data-analysis environment, and a Qwen3.5-9B teacher scores every token it writes, with the tool results as context and masked out of the loss. On 61 checked tasks, the student goes from 0.19 to 0.29 of the questions answered correctly when only its first turn is distilled, and to 0.35 when whole episodes are, with three seeds (script, evaluation script and README with the results). | |
| [`dpo_reduce_hallucinations`](https://github.com/huggingface/trl/tree/main/examples/dpo_reduce_hallucinations) | DPO fine-tuning of a Vision Language Model to reduce hallucinations using the [openbmb/RLAIF-V-Dataset](https://huggingface.co/datasets/openbmb/RLAIF-V-Dataset) dataset. | |
| [`gold_chatbot_arena`](https://github.com/huggingface/trl/tree/main/examples/gold_chatbot_arena) | General Online Logit Distillation (GOLD) of a Qwen2 teacher into a Llama 3.2 student (cross-tokenizer) on chatbot_arena_completions with [experimental.gold.GOLDTrainer](/docs/trl/v1.19.0/en/gold_trainer#trl.experimental.gold.GOLDTrainer), with full-training and LoRA variants. | |
| [`gold_qwen3_vl`](https://github.com/huggingface/trl/tree/main/examples/gold_qwen3_vl) | General Online Logit Distillation (GOLD) of Qwen3-VL-8B into smaller VLM students with [experimental.gold.GOLDTrainer](/docs/trl/v1.19.0/en/gold_trainer#trl.experimental.gold.GOLDTrainer), covering same-family (JSD loss) and cross-family (ULD loss) distillation. | |
| [`grpo_2048`](https://github.com/huggingface/trl/tree/main/examples/grpo_2048) | GRPO with tool calling to teach a model to play the 2048 game. | |
| [`grpo_browsergym`](https://github.com/huggingface/trl/tree/main/examples/grpo_browsergym) | GRPO with the BrowserGym [OpenEnv](openenv) environment, with LLM and VLM variants. | |
| [`grpo_carla`](https://github.com/huggingface/trl/tree/main/examples/grpo_carla) | GRPO with the CARLA autonomous-driving [OpenEnv](openenv) environment, with LLM and VLM variants (multimodal camera-image tool responses). | |
| [`grpo_catch`](https://github.com/huggingface/trl/tree/main/examples/grpo_catch) | GRPO with the Catch (OpenSpiel) [OpenEnv](openenv) environment. | |
| [`grpo_continuous_batching`](https://github.com/huggingface/trl/tree/main/examples/grpo_continuous_batching) | GRPO with transformers' continuous batching engine for faster generation on large batches with variable completion lengths. | |
| [`grpo_echo`](https://github.com/huggingface/trl/tree/main/examples/grpo_echo) | Minimal GRPO training with the Echo [OpenEnv](openenv) environment. | |
| [`grpo_harbor`](https://github.com/huggingface/trl/tree/main/examples/grpo_harbor) | GRPO training against a Harbor task suite with a pluggable base agent (`bash` / `jupyter` / `terminal_notes` harnesses). See the [Harbor Integration](harbor) guide. | |
| [`grpo_ministral3_vl`](https://github.com/huggingface/trl/tree/main/examples/grpo_ministral3_vl) | GRPO Ministral 3 with QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_ministral3_vl/grpo_ministral3_vl.ipynb) |
| [`grpo_multi_env`](https://github.com/huggingface/trl/tree/main/examples/grpo_multi_env) | Multi-environment GRPO training: Wordle + Catch [OpenEnv](openenv) environments in the same training run. | |
| [`grpo_qlora`](https://github.com/huggingface/trl/tree/main/examples/grpo_qlora) | GRPO using QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_qlora/grpo_qlora.ipynb) |
| [`grpo_qwen3_vl`](https://github.com/huggingface/trl/tree/main/examples/grpo_qwen3_vl) | GRPO Qwen3-VL with QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_qwen3_vl/grpo_qwen3_vl.ipynb) |
| [`grpo_rnj_1_instruct`](https://github.com/huggingface/trl/tree/main/examples/grpo_rnj_1_instruct) | GRPO on rnj-1-instruct with QLoRA on Colab to add reasoning capabilities. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_rnj_1_instruct/grpo_rnj_1_instruct.ipynb) |
| [`grpo_seta`](https://github.com/huggingface/trl/tree/main/examples/grpo_seta) | GRPO training against the SETA ORS environment on the openreward.ai catalog. See the [OpenReward Integration](openreward) guide. | |
| [`grpo_sql_agent`](https://github.com/huggingface/trl/tree/main/examples/grpo_sql_agent) | GRPO to train an agent that answers questions by querying a SQL database (script and notebook; not runnable on free Colab due to OOM). | |
| [`grpo_sudoku`](https://github.com/huggingface/trl/tree/main/examples/grpo_sudoku) | GRPO to play Sudoku (TextArena) on an [OpenEnv](openenv) environment, starting from an SFT model: 0, 10 and 22 puzzles out of 30 solved at 26 empty cells for the base, SFT and SFT + GRPO models (script, notebook and README with the results). | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_sudoku/grpo_sudoku.ipynb) |
| [`grpo_visual_math`](https://github.com/huggingface/trl/tree/main/examples/grpo_visual_math) | GRPO fine-tuning of a multimodal model for reasoning using the [lmms-lab/multimodal-open-r1-8k-verified](https://huggingface.co/datasets/lmms-lab/multimodal-open-r1-8k-verified) dataset. | |
| [`grpo_wordle`](https://github.com/huggingface/trl/tree/main/examples/grpo_wordle) | GRPO to play Wordle (TextArena) on an [OpenEnv](openenv) environment, starting from an SFT model: 0, 10 and 20 wins out of 30 for the base, SFT and SFT + GRPO models (script and notebook). | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/grpo_wordle/grpo_wordle.ipynb) |
| [`gspo_math`](https://github.com/huggingface/trl/tree/main/examples/gspo_math) | GSPO via the [GRPOTrainer](/docs/trl/v1.19.0/en/grpo_trainer#trl.GRPOTrainer) for math reasoning on the [AI-MO/NuminaMath-TIR](https://huggingface.co/datasets/AI-MO/NuminaMath-TIR) dataset. | |
| [`gspo_visual_math`](https://github.com/huggingface/trl/tree/main/examples/gspo_visual_math) | GSPO via the [GRPOTrainer](/docs/trl/v1.19.0/en/grpo_trainer#trl.GRPOTrainer) to fine-tune a multimodal model for reasoning using the [lmms-lab/multimodal-open-r1-8k-verified](https://huggingface.co/datasets/lmms-lab/multimodal-open-r1-8k-verified) dataset. | |
| [`lfm2_5_moe_research_agent`](https://github.com/huggingface/trl/tree/main/examples/lfm2_5_moe_research_agent) | LiquidAI/LFM2.5-8B-A1B in two steps: full SFT on xLAM function calling on one 8xH100 node with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer), sharded at load time by transformers' `DistributedConfig` (expert parallelism with token dispatch, plus FSDP), then GRPO of a multi-turn research agent on HotpotQA with [GRPOTrainer](/docs/trl/v1.19.0/en/grpo_trainer#trl.GRPOTrainer) and `environment_factory` (`search` / `read` tools, exact-match reward). | |
| [`mpo_visual_preferences`](https://github.com/huggingface/trl/tree/main/examples/mpo_visual_preferences) | MPO via the [DPOTrainer](/docs/trl/v1.19.0/en/bema_for_reference_model#trl.DPOTrainer) to align a multimodal model based on preferences using the [HuggingFaceH4/rlaif-v_formatted](https://huggingface.co/datasets/HuggingFaceH4/rlaif-v_formatted) dataset and a set of loss weights. | |
| [`online_dpo_visual_math`](https://github.com/huggingface/trl/tree/main/examples/online_dpo_visual_math) | Online DPO fine-tuning of a Vision Language Model with [experimental.online_dpo.OnlineDPOTrainer](/docs/trl/v1.19.0/en/online_dpo_trainer#trl.experimental.online_dpo.OnlineDPOTrainer). | |
| [`rloo_math`](https://github.com/huggingface/trl/tree/main/examples/rloo_math) | RLOO with the [RLOOTrainer](/docs/trl/v1.19.0/en/rloo_trainer#trl.RLOOTrainer) for math reasoning on the [AI-MO/NuminaMath-TIR](https://huggingface.co/datasets/AI-MO/NuminaMath-TIR) dataset with vLLM. | |
| [`rloo_visual_math`](https://github.com/huggingface/trl/tree/main/examples/rloo_visual_math) | RLOO fine-tuning of a multimodal model for reasoning using the [lmms-lab/multimodal-open-r1-8k-verified](https://huggingface.co/datasets/lmms-lab/multimodal-open-r1-8k-verified) dataset. | |
| [`sdft_privileged_context`](https://github.com/huggingface/trl/tree/main/examples/sdft_privileged_context) | Self-distillation fine-tuning with [experimental.sdft.SDFTTrainer](/docs/trl/v1.19.0/en/sdft_trainer#trl.experimental.sdft.SDFTTrainer), distilling privileged (teacher-only) context into the model. | |
| [`sdpo_math`](https://github.com/huggingface/trl/tree/main/examples/sdpo_math) | SDPO with [experimental.sdpo.SDPOTrainer](/docs/trl/v1.19.0/en/sdpo_trainer#trl.experimental.sdpo.SDPOTrainer) using verifiable math rewards and optional environment feedback on [openai/gsm8k](https://huggingface.co/datasets/openai/gsm8k). | |
| [`sft_agent_traces`](https://github.com/huggingface/trl/tree/main/examples/sft_agent_traces) | SFT of Qwen3.5-2B on agent traces: Qwen3.5-397B-A17B works through data-analysis [Harbor](harbor) tasks as an OpenCode agent in Hugging Face Sandboxes, recorded by [OpenEnv](openenv), and the student trains on the solved conversations with assistant-only loss. On tasks whose data no trace touched, about 500 traces take it from 7.6% to 33.3% (rollout, dataset and training scripts, and README with the results). | |
| [`sft_beam`](https://github.com/huggingface/trl/tree/main/examples/sft_beam) | LoRA SFT of reflection/Beam-1.0-501B-A23B on one 8xB300 node with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer), sharded at load time by transformers' `DistributedConfig` (expert parallelism with token dispatch). | |
| [`sft_diffusion_gemma`](https://github.com/huggingface/trl/tree/main/examples/sft_diffusion_gemma) | SFT of the DiffusionGemma block-diffusion language model on GSM8K by extending the [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer) with a block-diffusion objective. | |
| [`sft_gemma3`](https://github.com/huggingface/trl/tree/main/examples/sft_gemma3) | SFT of Gemma 3 on the Codeforces COTS dataset. | |
| [`sft_gemma3_vision`](https://github.com/huggingface/trl/tree/main/examples/sft_gemma3_vision) | SFT of Gemma 3 on vision to text tasks. | |
| [`sft_gpt_oss_chess`](https://github.com/huggingface/trl/tree/main/examples/sft_gpt_oss_chess) | Full fine-tuning of openai/gpt-oss-20b on chess puzzles with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer): TRL loads its MXFP4 experts dequantized, and the fine-tuned experts are quantized back to MXFP4 to serve like the original. | |
| [`sft_ministral3_vl`](https://github.com/huggingface/trl/tree/main/examples/sft_ministral3_vl) | SFT Ministral 3 with QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/sft_ministral3_vl/sft_ministral3_vl.ipynb) |
| [`sft_mistral_large_4`](https://github.com/huggingface/trl/tree/main/examples/sft_mistral_large_4) | LoRA SFT of mistralai/Mistral-Large-4-1T-A52B on top of its FP8 weights on one 8xB300 node with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer), sharded at load time by transformers' `DistributedConfig` (expert parallelism with token dispatch). | |
| [`sft_nemotron_3`](https://github.com/huggingface/trl/tree/main/examples/sft_nemotron_3) | SFT of NVIDIA Nemotron 3 models (script and LoRA notebook). | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/sft_nemotron_3/sft_nemotron_3.ipynb) |
| [`sft_qlora`](https://github.com/huggingface/trl/tree/main/examples/sft_qlora) | SFT using QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/sft_qlora/sft_qlora.ipynb) |
| [`sft_qwen3_8b_1m_context`](https://github.com/huggingface/trl/tree/main/examples/sft_qwen3_8b_1m_context) | SFT of Qwen3-8B on 1,048,576-token sequences with context parallelism, on one 8xH100 node. | |
| [`sft_qwen3_vl`](https://github.com/huggingface/trl/tree/main/examples/sft_qwen3_vl) | SFT Qwen3-VL with QLoRA on free Colab. | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/sft_qwen3_vl/sft_qwen3_vl.ipynb) |
| [`tail_sft_gsm8k`](https://github.com/huggingface/trl/tree/main/examples/tail_sft_gsm8k) | TailSFT on GSM8K by extending [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer) with initial-policy loss filtering. | |
| [`sft_muse_spark_open`](https://github.com/huggingface/trl/tree/main/examples/sft_muse_spark_open) | LoRA SFT of meta-models/Muse-Spark-Open-816B-A42B on 8 nodes with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer), sharded at load time by transformers' `DistributedConfig` (expert parallelism with token dispatch). | |
| [`sft_inkling_2_mini_clocks`](https://github.com/huggingface/trl/tree/main/examples/sft_inkling_2_mini_clocks) | Teach thinkingmachines/Inkling-2-Mini to read analog clocks with LoRA SFT on one H100 with [SFTTrainer](/docs/trl/v1.19.0/en/sft_trainer#trl.SFTTrainer): from none to 87.0% of held-out clocks read to the second. | |
| [`sft_tool_calling`](https://github.com/huggingface/trl/tree/main/examples/sft_tool_calling) | Teaching tool calling to a model without native tool-calling support using SFT with QLoRA (script, chat template, and notebook). | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/huggingface/trl/blob/main/examples/sft_tool_calling/sft_tool_calling.ipynb) |
| [`sft_visual_chat`](https://github.com/huggingface/trl/tree/main/examples/sft_visual_chat) | SFT of a Vision Language Model in a chat setting. Only tested with [LLaVA 1.5](https://huggingface.co/llava-hf/llava-1.5-7b-hf), [LLaVA 1.6](https://huggingface.co/llava-hf/llava-v1.6-mistral-7b-hf), and [Llama-3.2-11B-Vision-Instruct](https://huggingface.co/meta-llama/Llama-3.2-11B-Vision-Instruct); users may see unexpected behaviour in other model architectures. | |
| [`xtoken`](https://github.com/huggingface/trl/tree/main/examples/xtoken) | X-Token off-policy cross-tokenizer knowledge distillation with [experimental.gold.GOLDTrainer](/docs/trl/v1.19.0/en/gold_trainer#trl.experimental.gold.GOLDTrainer), including projection-matrix preparation. | |

## Distributed Training

You can run the example scripts on multiple GPUs with torchrun:

```shell
torchrun --nproc_per_node {NUM_GPUS} path_to_script.py --all_arguments_of_the_script
```

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

```shell
torchrun --nproc_per_node {NUM_GPUS} path_to_script.py --deepspeed examples/deepspeed_configs/zero{1,2,3}.json --all_arguments_of_the_script
```

Adjust `NUM_GPUS` and `--all_arguments_of_the_script` as needed.

