espnet3.components.callbacks.default_callbacks.AverageCheckpointsCallback
espnet3.components.callbacks.default_callbacks.AverageCheckpointsCallback
class espnet3.components.callbacks.default_callbacks.AverageCheckpointsCallback(output_dir, best_ckpt_callbacks)
Bases: Callback
A custom callback for weight averaging over the top-K checkpoints.
This can be useful to smooth out fluctuations in weights across the best-performing models and can lead to improved generalization performance at inference time.
Behavior: : - Loads the state_dict from each of the top-K checkpoints saved by given ModelCheckpoint callbacks.
- Averages every parameter in the checkpoint’s model state_dict. If the keys match the current model’s own state_dict() keys, they are averaged as-is; otherwise a model. prefix is stripped from each key before checking again.
- Ignores or simply accumulates integer-type parameters (e.g., BatchNorm’s num_batches_tracked).
- Saves the averaged model as a .pth file in output_dir.
- Parameters:
- output_dir (str or Path) – The directory where the averaged model will be saved.
- best_ckpt_callbacks (List *[*ModelCheckpoint ]) – A list of ModelCheckpoint callbacks whose top-K checkpoints will be used for averaging. Each callback must have best_k_models populated.
Notes
- The final filename will be: : {monitor_name}.ave_{K}best.pth
- This callback only runs on the global rank 0 process : (for distributed training).
- Raises:KeyError – If checkpoint keys are inconsistent across the top-K checkpoints, or if the averaged keys (with or without a model. prefix) do not match the current model’s state_dict() keys.
Example
>>> avg_ckpt_cb = AverageCheckpointsCallback(
... output_dir="checkpoints/",
... best_ckpt_callbacks=[val_loss_ckpt_cb, acc_ckpt_cb]
... )
>>> trainer = Trainer(callbacks=[avg_ckpt_cb])Initialize AverageCheckpointsCallback object.
on_train_end(trainer, pl_module)
Re-average once training is over, so the last checkpoint is included.
Lightning’s _CallbackConnector._reorder_callbacks moves every ModelCheckpoint to the end of the callback list, so this callback – an ordinary Callback – runs BEFORE them in on_validation_end and sees best_k_models as it was after the previous validation. Left at that, a single-epoch run writes no average at all (the dict is still empty), and a longer run writes one that omits the final checkpoint. Running again here, after the last checkpoint has been written, makes the saved average reflect every checkpoint the run produced.
on_validation_end(trainer, pl_module)
At the end of validation, average the top-K checkpoints and save.
