Logging to Weights & Biases¶
本深入指南将引导您了解如何在 torchtune 中配置日志记录至 Weights & Biases(W&B)。
如何开始使用 W&B
如何使用
WandBLogger如何将配置、指标和模型检查点记录到 W&B
torchtune 支持将您的训练运行日志记录到 Weights & Biases。 下面的截图展示了一个来自 torchtune 微调运行的 W&B 工作区示例。
注意
您需要安装 wandb 包才能使用此功能。
您可以使用 pip 安装它:
pip install wandb
然后,您需要使用 W&B 命令行工具(CLI)通过 API 密钥登录:
wandb login
指标记录器¶
您唯一需要做的更改是将指标记录器添加到您的配置中。Weights & Biases 将为您记录指标和模型检查点。
# enable logging to the built-in WandBLogger
metric_logger:
_component_: torchtune.training.metric_logging.WandBLogger
# the W&B project to log to
project: torchtune
我们自动从您运行的配方中获取配置并将其记录到 W&B。您可以在 W&B 的概览标签页中找到它,实际文件在 Files 标签页中。
作为一个提示,如果您的任务崩溃或以其他方式退出而没有清理资源,您可能会在后台看到残留的wandb进程。要终止这些残留进程,可以使用类似ps
-aux | grep wandb | awk '{ print $2 }' | xargs kill的命令。
注意
点击此示例 项目以查看 W&B 工作区。 用于训练模型的配置可以在 此处 找到。
将模型检查点记录到 W&B¶
您还可以通过修改所需的脚本 save_checkpoint 方法,将模型检查点记录到 W&B。
建议的方法大致如下:
def save_checkpoint(self, epoch: int) -> None:
...
## Let's save the checkpoint to W&B
## depending on the Checkpointer Class the file will be named differently
## Here is an example for the full_finetune case
checkpoint_file = Path.joinpath(
self._checkpointer._output_dir, f"torchtune_model_{epoch}"
).with_suffix(".pt")
wandb_at = wandb.Artifact(
name=f"torchtune_model_{epoch}",
type="model",
# description of the model checkpoint
description="Model checkpoint",
# you can add whatever metadata you want as a dict
metadata={
training.SEED_KEY: self.seed,
training.EPOCHS_KEY: self.epochs_run,
training.TOTAL_EPOCHS_KEY: self.total_epochs,
training.MAX_STEPS_KEY: self.max_steps_per_epoch,
}
)
wandb_at.add_file(checkpoint_file)
wandb.log_artifact(wandb_at)