
Hugging Face
Hugging Face's library for fine-tuning and aligning models.
GitHub Stars
19.0K
Contributors
518
PyPI / Month
3.8M
2%TRL is a Python library maintained by Hugging Face for post-training foundation models using supervised fine-tuning and preference alignment methods such as DPO and GRPO. It is not an inference serving engine. It is a training library that prepares models for deployment on a separate inference engine (vLLM, TGI, llama.cpp, etc.). TRL fills the gap between pretrained base models available on the Hugging Face Hub and production-ready tuned models.
TRL competes with other training frameworks like Axolotl, Unsloth, and the raw Transformers Trainer API. Its design philosophy prioritizes clean, modular building blocks over turnkey solutions. You integrate its trainers into your own Python training scripts. The library was first released in 2020 and has grown to 18,725 GitHub stars, 502 contributors, and over 3 million monthly PyPI downloads — a clear signal of widespread adoption in the open-source fine-tuning ecosystem.
TRL is licensed under Apache 2.0. It works hand in hand with Hugging Face Transformers and supports NVIDIA GPUs, AMD GPUs, quantization, and multi-GPU setups.
TRL provides a set of trainer classes that wrap the standard Transformers training loop. The core abstractions are:
SFTTrainer for supervised fine-tuning on instruction or completion data.DPOTrainer for direct preference optimization, where you provide preferred and rejected completions.GRPOTrainer for group relative policy optimization, an online method where the model generates its own completions during training and receives reward signals.To use TRL, you:
transformers.datasets format).trainer.train(). The library handles loss computation, gradient updates, and optional generation during online methods.TRL does not expose a server or an OpenAI-compatible API. It runs in a Python process, typically on GPU hardware. After training, you save the model weights and load them into a separate inference engine for serving.
For online methods like GRPO, TRL can integrate with vLLM as a generation backend to speed up the inference steps inside the training loop. This integration can be run in co-located mode (vLLM sharing GPU cores with training) or server mode (vLLM on dedicated GPUs, communicating over HTTP). The co-located mode was added in TRL v0.18.0 to reduce GPU idle time during the inference-training ping-pong.
TRL itself is a training library, so performance is measured in training throughput, not inference latency. Key hardware considerations:
accelerate and DeepSpeed is supported.bitsandbytes for 4-bit and 8-bit QLoRA-style fine-tuning, reducing memory and enabling fine-tuning on single GPUs with smaller VRAM.accelerate and DeepSpeed ZeRO stages.Memory usage during training depends on model size, batch size, and whether quantization is used. TRL does not implement its own memory optimization tricks like PagedAttention (that is vLLM’s domain). For large models (e.g., 70B parameters), you need multi-GPU setups with model parallelism or use parameter-efficient fine-tuning (LoRA) via the peft integration.
TRL does not benchmark inference throughput or latency because it is not a serving engine. When researching “TRL throughput benchmark,” you will find studies of training speed, not serving speed.
SFTTrainer handles packing, padding, and formatting automatically.DPOTrainer and GRPOTrainer implement algorithms that align model outputs to human preferences. DPO uses static preference pairs; GRPO uses online generation with a reward model or verifier.Transformers, including Llama, Mistral, Qwen, Gemma, and multimodal models.peft, accelerate, bitsandbytes — all first-class citizens.GRPOTrainer’s environment_factory. This extends TRL into reinforcement learning for agentic systems.TRL does not support structured output generation (e.g., JSON mode), streaming, or continuous batching — those are inference engine features. It also does not provide a built-in serving API. Its sole output is a trained model.
TRL is a poor fit for:
Install TRL with pip:
1pip install trl
For online methods with vLLM support, install extras:
1pip install trl[vllm]
The smallest meaningful path to fine-tune a model:
1 from transformers import AutoModelForCausalLM, AutoTokenizer2 model = AutoModelForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")3 tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
"prompt" and "completion" keys).1 from trl import SFTTrainer2 trainer = SFTTrainer(model=model, tokenizer=tokenizer, train_dataset=dataset)3 trainer.train()
1 trainer.save_model("./my-fine-tuned-model")
You need a GPU with at least 8 GB VRAM for small models in 4-bit mode. For full fine-tuning on 7B or larger, plan for multi-GPU or use LoRA via peft.
Documentation: [huggingface.co/docs/trl](https://huggingface.co/docs/trl)
Community: Hugging Face Discord, GitHub issues
TRL vs. Axolotl: Both are Python libraries for fine-tuning open models on top of Transformers. Axolotl provides a configuration-driven approach (YAML config files) and a CLI, making it easier for users who prefer not to write Python training loops. TRL is more modular and offers direct access to trainers for custom code. If you need a quick fine-tuning run without scripting, Axolotl is simpler. If you want to implement a custom training loop or experiment with alignment methods, TRL gives more control.
TRL vs. Unsloth: Unsloth optimizes the training loop (via custom CUDA kernels) to achieve 2x-4x speedups for fine-tuning and uses less memory. It supports LoRA and QLoRA by default. Unsloth is not a full library — it is a set of patches you apply to your existing fine-tuning code. TRL is a full library with multiple trainers, alignment methods, and ecosystem integration. For maximum training speed on supported models, Unsloth wins. For flexibility and breadth of methods, TRL wins.
TRL vs. vLLM: This is not a direct comparison. vLLM is a serving engine; TRL is a training library. They complement each other — you train with TRL, then serve with vLLM. The confusion arises because TRL integrates with vLLM for generation during training. When teams ask “TRL vs. vLLM,” they are usually evaluating inference engines for serving a fine-tuned model. For that purpose, vLLM is the right choice; TRL is not an inference engine.
TRL vs. Ollama: Ollama is a local model runner with minimal fine-tuning support (via modelfiles). It is not a training library. If you need to fine-tune an open model, TRL is the correct tool. If you just want to run a quantized model locally, use Ollama or llama.cpp.
TRL vs. TGI (Text Generation Inference): TGI is Hugging Face’s own inference server for serving Transformers models at scale. It supports continuous batching, quantization, and OpenAI-compatible API. TRL is the companion training library. A typical pipeline: fine-tune with TRL, deploy with TGI or vLLM.
What the engine gives you out of the box, in plain language.
A trainer for standard fine-tuning on your own data.
Methods like DPO and GRPO align a model to preferred outputs.
Works directly with Hugging Face models and libraries.
The jobs this engine is best suited for.
Tune a model toward preferred answers with DPO or similar.
Build a training pipeline on solid, tested components.
Try new alignment methods with ready-made trainers.

Side-by-Side
Add a second or third engine and see stars, downloads, and capabilities lined up next to each other.
Close alternatives worth a look before you decide.
Fine-tune open models from a simple config file.
Config-driven fine-tuning
pip install axolotlStars
12.3K
PyPI / mo
13.0K
Fine-tune open models faster and on less GPU memory.
Fast, low-memory fine-tuning
pip install unslothStars
69.9K
PyPI / mo
2.2M
Run open models locally with a single command.
One-line local model running
curl -fsSL https://ollama.com/install.sh | shStars
178.2K
PyPI / mo
—
High-throughput GPU serving with an OpenAI-compatible API out of the box.
High-throughput GPU serving
pip install vllmStars
88.8K
PyPI / mo
5.1M