
Hugging Face's library for fine-tuning and aligning models.
GitHub Stars
19.1K
Contributors
519
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.
Axolotl makes fine-tuning open models straightforward. You describe the model, data, and method in a YAML config, and it handles the training. It supports popular methods like LoRA and QLoRA and scales across multiple GPUs, which has made it a go-to tool for custom models.
Config-driven fine-tuning
pip install axolotlStars
12.5K
PyPI / mo
10.7K
Fine-tune open models faster and on less GPU memory.
Unsloth makes fine-tuning open models dramatically faster while using far less GPU memory. It rewrites the heavy parts of training to be more efficient, so you can adapt a model to your data on a single consumer or cloud GPU instead of a cluster.
Fast, low-memory fine-tuning
pip install unslothStars
76.8K
PyPI / mo
901.3K
Run open models locally with a single command.
Ollama is the easiest way to run open models on your own machine. One command pulls a model and starts a local server with an OpenAI-compatible API. It works on Mac, Windows, and Linux, and handles the messy parts of downloading and quantizing models for you.
One-line local model running
curl -fsSL https://ollama.com/install.sh | shStars
181.7K
PyPI / mo
—
High-throughput GPU serving with an OpenAI-compatible API out of the box.
vLLM is the go-to engine for serving open models on NVIDIA and AMD GPUs at scale. Its PagedAttention memory trick and continuous batching push far more requests through a GPU than a naive setup, and it speaks the OpenAI API so most apps work without code changes.
High-throughput GPU serving
pip install vllmStars
92.6K
PyPI / mo
2.0M
The standard Python library for loading and running open models.
Transformers is the most widely used library for working with open models. If you want to load a model in a few lines of Python and run inference, this is the default starting point. It supports NVIDIA, AMD, CPU, and Apple Silicon, and connects to the huge Hugging Face model hub.
One-shot Python inference and prototyping
pip install transformersStars
166.6K
PyPI / mo
92.2M
Fine-tune over a hundred open models, with a UI or the command line.
LLaMA-Factory is a broad fine-tuning toolkit that supports a wide range of open models and methods. It offers both a command line and a web UI, so you can train without writing code. It covers everything from LoRA to full fine-tuning and preference tuning in one place.
Broad model support with a training UI
pip install llamafactoryStars
75.0K
PyPI / mo
17.9K
An inference engine is the software that runs a language model and turns your prompt into tokens. It loads the model weights, manages memory on your GPU or CPU, and serves the output, usually behind an API.
TRL ships under the Apache 2.0 license. The source code lives on GitHub, so you can read it, fork it, and run it on your own hardware if your team prefers self-hosting.
TRL is primarily a Python project. The implementation language matters less than the hardware it supports and the throughput it delivers, but it does affect how easily your team can extend or debug it.