TRL documentation

回调

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v1.17.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

回调

RichProgressCallback

类 trl.RichProgressCallback

< >

( )

一个使用 Rich 显示训练或评估进度的 TrainerCallback。

LogCompletionsCallback

类 trl.LogCompletionsCallback

< >

( trainer: Trainergeneration_config: transformers.generation.configuration_utils.GenerationConfig | None = Nonenum_prompts: int | None = Nonefreq: int | None = None )

参数

  • trainer (Trainer) — 回调将附加到的 Trainer。该 trainer 的评估数据集必须包含一个 "prompt" 列,其中包含用于生成补全的提示。
  • generation_config (GenerationConfig, 可选) — 用于生成补全的生成配置。
  • num_prompts (int, 可选) — 要生成补全的提示数量。如果未提供,则默认为评估数据集中的示例数量。
  • freq (int, 可选) — 记录补全的频率。如果未提供,则默认为训练器的 eval_steps。

一个用于将补全记录到 Weights & Biases 和/或 Comet 的 TrainerCallback。

用法:

>>> trainer = DPOTrainer(...)
>>> completions_callback = LogCompletionsCallback(trainer=trainer)
>>> trainer.add_callback(completions_callback)

BEMACallback

类 trl.BEMACallback

< >

( update_freq: int = 400ema_power: float = 0.5bias_power: float = 0.2lag: int = 10update_after: int = 0multiplier: float = 1.0min_ema_multiplier: float = 0.0device: str = 'cpu' )

参数

  • update_freq (int, 可选, 默认值为 400) — 每隔 X 步更新一次 BEMA 权重。在论文中记为 {@html "ϕ \\phi ϕ"} in the paper.
  • ema_power (float, optional, defaults to 0.5) — Power for the EMA decay factor. Denoted {@html "κ \\kappa κ "} 在论文中。若要禁用 EMA,请将其设置为 0.0.
  • bias_power (float, 可选, 默认值为 0.2) — BEMA 缩放因子的幂次。论文中记为 {@html "η \\eta η"}。较大的值(例如 8.0)会使 {@html "αt \\alpha_t αt​"} 衰减至 0,近似于禁用偏差校正;而 0.0 则会在每一步将 {@html "αt \\alpha_t αt​"} 固定为 1(最大、未衰减的校正)。lag (int, 可选, 默认值为 10) — 权重衰减调度中的初始偏移量,通过充当更新的虚拟起始年龄来控制早期阶段的平滑度。论文中记为 {@html "ρ \\rho ρ "}η \\eta η"} in the paper. A large value (e.g. 8.0) makes {@html "αt \\alpha_t αt​"} decay to 0, approximating disabled bias-correction; 0.0 instead pins {@html "αt \\alpha_t αt​"} at 1 for every step (maximum, undecayed correction).
  • lag (int, optional, defaults to 10) — Initial offset in the weight decay schedule that controls early-stage smoothness by acting as a virtual starting age for the updates. Denoted as {@html "ρ \\rho ρ"} 。
  • update_after (int, 可选, 默认为 0) — 开始更新 BEMA 权重之前的预热时间。论文中记为 {@html "τ \\tau τ"}。multiplier (float, 可选, 默认为 1.0) — EMA 衰减因子的初始值。论文中记为 {@html "γ \\gamma γ "}τ \\tau τ"} in the paper.
  • multiplier (float, optional, defaults to 1.0) — Initial value for the EMA decay factor. Denoted as {@html "γ \\gamma γ"} 。
  • min_ema_multiplier (float,可选,默认为 0.0) — EMA 衰减因子的最小值。
  • device (str,可选,默认为 "cpu") — 用于 BEMA 缓冲区的设备,例如 "cpu" 或 "cuda"。请注意,在大多数情况下,该设备应与训练所使用的设备不同,以避免 OOM。

一个实现了 BEMA(偏差校正指数移动平均)的 TrainerCallback,由 Adam Block 和 Cyril Zhang 提出。代码来自 https://github.com/abblock/bema,采用 MIT 许可证。

BEMA 计算按以下方式缩放的模型权重: θt′=αt⋅(θt−θ0)+EMAt \theta_t' = \alpha_t \cdot (\theta_t - \theta_0) + \text{EMA}_t

其中 θt \theta_t 是当前模型权重, θ0 \theta_0 是模型权重在第一个 update_after 步时的快照, EMAt \text{EMA}_t 是随步数αt \alpha_t 衰减的缩放因子, t t 其形式为 αt=(ρ+γ⋅t)−η. \alpha_t = (\rho + \gamma \cdot t)^{-\eta}.

EMA 的计算公式为: EMAt=(1−βt)⋅EMAt−1+βt⋅θt \text{EMA}_t = (1 - \beta_t) \cdot \text{EMA}_{t-1} + \beta_t \cdot \theta_t

其中 βt \beta_t 是随步数 t t 衰减的衰减因子,其形式为 βt=(ρ+γ⋅t)−κ. \beta_t = (\rho + \gamma \cdot t)^{-\kappa}.

示例:

>>> from trl import BEMACallback

>>> trainer = Trainer(..., callbacks=[BEMACallback()])

WeaveCallback

类 trl.WeaveCallback

< >

( trainer: Trainerproject_name: str | None = Nonescorers: dict[str, callable] | None = Nonegeneration_config: transformers.generation.configuration_utils.GenerationConfig | None = Nonenum_prompts: int | None = Nonedataset_name: str = 'eval_dataset'model_name: str | None = None )

参数

  • trainer (Trainer) — 回调将附加到的 Trainer。该 trainer 的评估数据集必须包含一个 "prompt" 列,其中包含用于生成补全的提示。
  • project_name (str, 可选) — 用于记录数据的 Weave 项目名称。如果未提供,将尝试使用现有的 weave 客户端,或回退到当前活跃的 wandb run 的项目名称。如果这些均不可用,则会引发错误。
  • scorers (dict[str, Callable], 可选) — 将评分器名称映射到评分器函数的字典。如果为 None,则以追踪模式运行(仅预测)。如果提供,则以评估模式运行(预测 + 分数 + 摘要)。评分器函数的签名应为:scorer(prompt: str, completion: str) -> float | int
  • generation_config (GenerationConfig,可选) — 用于生成补全的生成配置。
  • num_prompts (int, 可选) — 要生成补全的提示数量。如果未提供,则默认为评估数据集中的示例数量。
  • dataset_name (str,可选,默认为 "eval_dataset") — Weave 中数据集元数据的名称。
  • model_name (str, 可选) — Weave 中模型元数据的名称。如果未提供,将尝试从模型配置中提取。

一个用于将跟踪和评估记录到 W&B Weave 的 TrainerCallback。该回调使用 https://weave-docs.wandb.ai/guides/evaluation/evaluation_logger/ 在每个评估步骤记录跟踪和评估。

支持基于 scorers 参数的两种模式:

  • 追踪模式(当 scorers=None 时):记录预测结果,用于数据探索和分析
  • 评估模式(提供评分器时):记录带有评分和汇总指标的预测结果

两种模式均使用 Weave 的 EvaluationLogger 进行结构化且一致的数据记录。

该回调在评估阶段(on_evaluate)而非训练步骤中记录数据,因此更高效且语义上更正确。它会妥善处理 weave 未安装的情况,通过记录警告并跳过 weave 特定功能来实现。此外,在初始化新的 weave 客户端之前,它还会检查是否已存在 weave 客户端。

用法:

# Tracing mode (just log predictions)
trainer = DPOTrainer(...)
weave_callback = WeaveTraceCallback(trainer=trainer)  # project_name optional
trainer.add_callback(weave_callback)

# Or specify a project name
weave_callback = WeaveTraceCallback(trainer=trainer, project_name="my-llm-training")
trainer.add_callback(weave_callback)


# Evaluation mode (log predictions + scores + summary)
def accuracy_scorer(prompt: str, completion: str) -> float:
    # Your scoring logic here (metadata available via eval_attributes)
    return score


weave_callback = WeaveTraceCallback(
    trainer=trainer,
    project_name="my-llm-training",  # optional and needed only if weave client is not initialized
    scorers={"accuracy": accuracy_scorer},
)
trainer.add_callback(weave_callback)

on_train_begin

< >

( argsstatecontrol**kwargs )

在训练开始时初始化 Weave。

Update on GitHub