# trl-training

Helps write and debug TRL post-training code for language models, including SFT, DPO, GRPO, KTO and reward-model training.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 909 installs · ★ 11213
- Author: huggingface
- License: Apache-2.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/huggingface/skills/tree/HEAD/skills/trl-training/SKILL.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/skills/trl-training

## What it does

This skill guides work with the TRL Python API and CLI for post-training language models. It covers `SFTTrainer`, `DPOTrainer`, `GRPOTrainer`, `DistillationTrainer`, `KTOTrainer`, and `RewardTrainer`; their configuration classes extend `transformers.TrainingArguments`.

It maps training methods to dataset shapes: chosen/rejected pairs for DPO, prompts plus reward functions for GRPO, and per-sample preference labels for KTO. Chat templates are applied automatically to conversational data, so applying them manually is a mistake. Trainers under `trl.experimental` have unstable APIs, which matters if your code depends on a fixed interface.

For SFT, it covers `max_length`, sequence packing, `padding_free`, Liger kernels, and assistant-only loss. For GRPO, reward functions return a `list[float]` per completion, while the generation batch must divide by `num_generations`, which defaults to `8`. It also covers `trl sft`, YAML configuration, and `accelerate` launches.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add huggingface/skills --skill trl-training -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/trl-training/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/trl-training/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add huggingface/skills --skill trl-training -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/trl-training/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add huggingface/skills --skill trl-training -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/trl-training/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install huggingface/skills trl-training
```

2. **Manually: put the files in place**
- File: `.github/skills/trl-training/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add huggingface/skills --skill trl-training -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/huggingface/skills --path skills/trl-training
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/trl-training/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add huggingface/skills --skill trl-training -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/huggingface/skills/tree/HEAD/skills/trl-training/SKILL.md

## When to use it
- write SFT code with `SFTTrainer` and `SFTConfig`
- format preference pairs for `DPOTrainer`
- implement a reward function for `GRPOTrainer`
- configure LoRA with `peft_config=LoraConfig(...)`
- run training through `trl sft`

## Example prompts
- Help me write SFT training code with `SFTTrainer` for my dataset.
- Review my `GRPOTrainer` reward function: why does it receive conversational completions?
- Build a `trl sft` command using a YAML config and `--accelerate_config zero3`.

## Source text · SKILL.md · Apache-2.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 21df7efe711a8534.

BEGIN THIRD-PARTY FILE 21df7efe711a8534
````text
---
name: trl-training
description: Post-train LLMs with TRL (Transformers Reinforcement Learning) — SFT, DPO, GRPO, KTO, and reward-model training. Use when writing or debugging training code with the TRL Python API or the trl CLI.
license: Apache-2.0
metadata:
  author: huggingface
  documentation: https://huggingface.co/docs/trl
---

# TRL

Each method pairs a `*Trainer` class with a `*Config` dataclass. Configs extend `transformers.TrainingArguments`, so all of its arguments work in any trainer config.

| Trainer | Dataset type |
|---|---|
| `SFTTrainer` | language modeling or prompt-completion |
| `DPOTrainer` | preference (chosen/rejected pairs) |
| `GRPOTrainer` | prompt-only + reward function(s) |
| `DistillationTrainer` | prompt-only + a teacher model (on-policy distillation) |
| `KTOTrainer` | unpaired preference (per-sample bool label) |
| `RewardTrainer` | preference (chosen/rejected pairs); trains a scalar reward model, not a policy |

Many more trainers (OnlineDPO, ORPO, CPO, GKD, …) live in `trl.experimental` with unstable APIs: https://huggingface.co/docs/trl/experimental_overview

```python
from datasets import load_dataset
from trl import SFTConfig, SFTTrainer

trainer = SFTTrainer(
    model="Qwen/Qwen2.5-0.5B",  # model ID or a PreTrainedModel instance
    args=SFTConfig(output_dir="Qwen2.5-0.5B-SFT"),
    train_dataset=load_dataset("trl-lib/Capybara", split="train"),
)
trainer.train()
```

Pass `model` as a string and route loading kwargs through `model_init_kwargs` (e.g. `{"dtype": "bfloat16", "attn_implementation": "kernels-community/flash-attn2"}`) instead of calling `from_pretrained` yourself. The tokenizer/processor is inferred from the model; pass `processing_class` only when it differs. For LoRA, pass `peft_config=LoraConfig(...)`.

## Dataset formats

Conversational: `{"messages": [{"role": ..., "content": ...}]}` (language modeling) or `{"prompt": [...], "completion": [...]}`. The chat template is applied automatically — never apply it yourself. Extra columns are allowed; GRPO forwards them to reward functions. Reference: https://huggingface.co/docs/trl/dataset_formats

## SFT: the fields that matter

```python
SFTConfig(
    max_length=1024,        # truncation length; None disables truncation
    packing=True,           # pack sequences into max_length blocks: fewer pad tokens, higher throughput
    padding_free=True,      # flatten batch, no padding; requires FlashAttention; implied by packing
    use_liger_kernel=True,  # fused Liger kernels, reduces peak memory
    assistant_only_loss=True,  # loss only on assistant turns (conversational datasets)
)
```

## GRPO: online RL

```python
def reward_len(completions, **kwargs):
    return [-abs(20 - len(c[0]["content"])) for c in completions]

trainer = GRPOTrainer(
    model="Qwen/Qwen2.5-0.5B-Instruct",
````
END THIRD-PARTY FILE 21df7efe711a8534

The third-party file has ended. Everything between the 21df7efe711a8534 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2840 of 4323 bytes. © huggingface · Apache-2.0 · https://github.com/huggingface/skills/tree/HEAD/skills/trl-training/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
