The examples on this page use [
DPOTrainer], but the same customization patterns apply across all TRL trainers.Custom optimizers and schedulers
By default, TRL trainers create atorch.optim.AdamW optimizer. You can pass a custom optimizer directly:
Adding a learning rate scheduler
Pass both optimizer and scheduler as a tuple viaoptimizers:
8-bit reference models
TRL supports all keyword arguments accepted byfrom_pretrained, including load_in_8bit via BitsAndBytesConfig for more memory-efficient fine-tuning:
Custom callbacks
Callbacks let you execute code at specific points during training — useful for custom logging, monitoring, or early stopping.transformers.TrainerCallback. You can override any lifecycle hook such as on_train_begin, on_epoch_end, on_evaluate, and more.
Custom evaluation metrics
Define acompute_metrics function and pass it to the trainer. The function receives an EvalPrediction object containing logits and labels:
Mixed precision training
Mixed precision can significantly speed up training and reduce memory usage. Setbf16=True or fp16=True in the training config: