Skip to main content
TRL provides a set of TrainerCallback subclasses that extend Hugging Face Trainer with reinforcement-learning-specific features such as exponential moving average weight tracking, reference model synchronization, completion logging, and third-party observability integrations. Import all callbacks from the top-level trl package:

BEMACallback

BEMACallback implements Bias-Corrected Exponential Moving Average (BEMA), introduced in Block & Zhang (2025). It maintains a running shadow model whose weights track the training model via a bias-corrected EMA scheme: θt=αt(θtθ0)+EMAt\theta_t' = \alpha_t \cdot (\theta_t - \theta_0) + \text{EMA}_t where αt=(ρ+γt)η\alpha_t = (\rho + \gamma \cdot t)^{-\eta} decays with the step count. The EMA itself is updated as: EMAt=(1βt)EMAt1+βtθt,βt=(ρ+γt)κ\text{EMA}_t = (1 - \beta_t) \cdot \text{EMA}_{t-1} + \beta_t \cdot \theta_t, \quad \beta_t = (\rho + \gamma \cdot t)^{-\kappa} At the end of training the shadow model is saved to {output_dir}/bema/.
The BEMA buffers live on a separate device (default "cpu") to avoid out-of-memory errors on the training accelerator.

Signature

Parameters

int
default:"400"
Update the BEMA shadow model every this many steps. Denoted ϕ\phi in the paper.
float
default:"0.5"
Exponent κ\kappa controlling the EMA decay factor βt\beta_t. Set to 0.0 to disable EMA.
float
default:"0.2"
Exponent η\eta controlling the BEMA scaling factor αt\alpha_t. Set to 0.0 to disable bias correction.
int
default:"10"
Initial offset ρ\rho in the weight decay schedule. Controls smoothness in early training by acting as a virtual starting age.
int
default:"0"
Burn-in steps τ\tau before BEMA updates begin. The snapshot θ0\theta_0 is taken at this step.
float
default:"1.0"
Step multiplier γ\gamma applied to the step count inside the decay schedule.
float
default:"0.0"
Floor value for the EMA decay factor βt\beta_t.
str
default:"'cpu'"
Device for BEMA buffers. Should differ from the training device to avoid OOM errors.

Example


SyncRefModelCallback

SyncRefModelCallback periodically synchronizes a reference model toward the current training model using an exponential moving average blend controlled by ref_model_mixup_alpha. It is used by trainers such as DPOTrainer when a soft-update reference policy is desired. The sync is triggered at every step where global_step % args.ref_model_sync_steps == 0.
DeepSpeed ZeRO Stage 3 is handled automatically: parameters are gathered across ranks before the blend is applied.

Signature

Parameters

PreTrainedModel | torch.nn.Module
The reference model to keep synchronized with the training model.
Accelerator | None
Accelerate Accelerator instance used to unwrap the model before syncing. Pass None if not using Accelerate.
The sync frequency (ref_model_sync_steps) and blend coefficient (ref_model_mixup_alpha) are read from TrainingArguments at runtime, not from the callback constructor.

Example


LogCompletionsCallback

LogCompletionsCallback generates model completions for prompts from the evaluation dataset at regular intervals and logs them as a table to Weights & Biases and/or Comet ML. This makes it easy to track qualitative output quality throughout training.
The trainer must have an evaluation dataset with a "prompt" column. A ValueError is raised at construction time if the dataset is absent.

Signature

Parameters

Trainer
The trainer instance to attach the callback to. Used to access the model, tokenizer, accelerator, and evaluation dataset.
GenerationConfig
Generation configuration used when producing completions. If not provided the model’s default config is used.
int
Number of prompts sampled from the evaluation dataset. Defaults to the full evaluation dataset.
int
Logging frequency in steps. Defaults to trainer.args.eval_steps.

Example


RichProgressCallback

RichProgressCallback replaces the default tqdm-based progress display with a Rich layout that shows training and evaluation progress bars alongside a live metrics table grouped by prefix.
This callback requires the rich package: pip install rich.

Signature

No constructor arguments are required.

Example


WeaveCallback

WeaveCallback logs completions and optional scorer evaluations to Weights & Biases Weave during evaluation steps. It supports two modes:
  • Tracing mode (scorers=None): logs predictions for data exploration.
  • Evaluation mode (scorers provided): logs predictions with per-scorer scores and summary statistics.
Both modes use Weave’s EvaluationLogger for structured logging.
The trainer must have an evaluation dataset with a "prompt" column. A ValueError is raised at construction time if absent.

Signature

Parameters

Trainer
Trainer instance to attach the callback to.
str
Weave project name for logging. If not provided, the callback tries the existing Weave client, then the active wandb run. Raises a ValueError if none is available.
dict[str, Callable]
Mapping of scorer names to scorer functions with signature scorer(prompt: str, completion: str) -> float | int. When provided, enables evaluation mode.
GenerationConfig
Generation configuration for producing completions.
int
Number of evaluation prompts to use. Defaults to the full evaluation dataset.
str
default:"'eval_dataset'"
Name label for the dataset metadata in Weave.
str
Name label for the model metadata in Weave. Extracted automatically from model.config._name_or_path if not provided.

Example