TRL
Post-train language models with Hugging Face's reference trainers, from the trl CLI for standard runs to Python scripts for custom rewards and collators.
Scope: this one product: install, train, scale out, export, update, troubleshoot. Whether to fine-tune, data prep and method choice belong to fine-tuning-llms.
Axolotl and Unsloth both build on TRL. Choose TRL directly for custom Python (reward functions, losses, collators), the newest method, or a small script.
Work through the steps in order. Load a reference only when a step names it.
Discover first
Read-only. Never print tokens.
command -v trl && trl env 2>/dev/null | head -30
python3 --version; command -v uv accelerate
python3 -c 'import trl, transformers, peft, torch; print(trl.__version__, transformers.__version__, peft.__version__, torch.__version__, torch.cuda.is_available())' 2>/dev/null
pip show vllm 2>/dev/null | grep -E '^(Name|Version)'
nvidia-smi --query-gpu=index,name,memory.total,memory.used --format=csv 2>/dev/null
uname -m; free -h 2>/dev/null | head -2; df -h . ~/.cache/huggingface 2>/dev/null
test -n "$HF_TOKEN" && echo "HF_TOKEN set" || echo "HF_TOKEN not set"
Decision flow
- Standard SFT/DPO/KTO/reward/RLOO/GRPO with built-in rewards ->
trl <method> --config file.yaml(references/cli.md,templates/). - Custom reward function, collator, callback or loss -> a Python script with the trainer class (
references/trainers.md). - Not enough VRAM -> PEFT LoRA,
--load_in_4bit, packing, activation offloading (references/peft-and-memory.md). - Online RL (GRPO, RLOO) -> vLLM server or colocate mode (
references/combos/grpo-vllm.md). - 2+ GPUs -> Accelerate via
--num_processes, or DeepSpeed ZeRO (references/cli.md). - Method sits under
trl.experimental-> expect API churn; pin the TRL version (references/trainers.md). - Error text ->
references/troubleshooting.md.
Rules
- Ask before changing the system. Installs, vLLM servers, long runs and anything that stops other services need approval and an undo path.
- Pin the version. TRL moves trainers between stable and
trl.experimental; tutorials rot. Recordtrl envoutput with every run. - Check the docs for your installed version. Field names change between releases.
- Use instruct models or set a chat template. Base models may lack one; conversational datasets then fail or render wrongly.
- Keep the vLLM server on a trusted network. TRL's server mode needs
VLLM_SERVER_DEV_MODE=1, which exposes development endpoints. - Trainer and vLLM server on separate GPUs in server mode.
- Hold out eval data and compare with the base. Loss curves are not the eval.
- Secrets never touch output.
HF_TOKENandWANDB_API_KEYcome from the environment or0600files.
Workflows
1. Install
-
uv venv && source .venv/bin/activate && uv pip install trl(+peft,bitsandbytes,vllmextras as needed) (references/install.md). - Verify:
trl envprints versions andtorch.cuda.is_available()isTrue.
2. Supervised fine-tuning with LoRA
- Prepare
messagesJSONL with a held-out split (fine-tuning-llmsfor data prep). -
cp templates/sft-lora.yaml sft.yaml; set model, dataset, output dir; runtrl sft --config sft.yaml(references/cli.md). - Or a Python script with
SFTTrainerandpeft_config(references/trainers.md). - Verify: eval loss falls;
output_dirhas adapter files; generate on held-out prompts.
3. Preference tuning
- Preference rows
prompt/chosen/rejected; start from the SFT model;trl dpoorDPOTrainer(references/trainers.md). - With PEFT, no separate
ref_modelis needed (frozen base is used). - Verify: rewards/margins logged; outputs on held-out prompts.
4. GRPO with a reward function
- Write the reward function (
references/trainers.md), test it on literals, then train (references/combos/grpo-vllm.md). - Verify: reward trend and sample completions, not only loss.
5. Scale out
- CLI: add
--num_processes N; scripts:accelerate launch train.py(references/cli.md). - Verify: all processes progress; GRPO batch sizing consistent with
num_generations(references/combos/grpo-vllm.md).
6. Merge and export
- Adapter:
trainer.save_model(dir). Merge: PEFTmerge_and_unload()(references/peft-and-memory.md). - GGUF and serving belong to
llama-cpp,vllmandfine-tuning-llms.
7. Update
- Read release notes, new venv, re-run a smoke run (
references/updating.md).
Reference index
references/install.md: install, extras,trl env, arm64/DGX Spark notes, version constraints.references/cli.md:trlcommands, YAML configs, Accelerate scaling,templates/.references/trainers.md: SFT, DPO, KTO, RLOO, Reward, GRPO, experimental trainers, dataset formats, reward functions.references/peft-and-memory.md: LoRA flags, QLoRA, packing, padding-free, offloading, saving and merging adapters.references/troubleshooting.md: symptoms and fixes.references/updating.md: versions, breaking changes, experimental churn.references/combos/grpo-vllm.md: GRPO with a vLLM server or colocated engine.templates/sft-lora.yaml,templates/grpo.yaml: CLI config starters.
Companion skills
Install with npx skills add ScaleDockHQ/llm-skills --skill <name>:
fine-tuning-llms: decide, prepare data, choose method and hardware, evaluate.axolotl: config-only, multi-GPU runs.unsloth: memory-tight single-GPU runs.vllm: serve the model, adapters, and the GRPO generation server.llm-evals: compare the tuned model with the base.llama-cpp: GGUF conversion.