CheckpointSaver#

class CheckpointSaver(save_freq=None, evaluator_every_n_steps=None)[source]#

Bases: HookBase

Save epoch/metric-oriented checkpoints during and after training.

Maintains a running global-step counter (seeded from resumed trainer progress in before_train) and, in after_step, saves a checkpoint whenever either cadence fires: a periodic save every save_freq steps, or an evaluator step every evaluator_every_n_steps steps (only when cfg.evaluate is set). On an evaluator step it also reads the evaluator’s current_metric_value from trainer.comm_info and, if it improves on trainer.best_metric_value, writes a model_best snapshot. A final checkpoint is written in after_train. All actual save logic is delegated to CheckpointManager. Registered as CheckpointSaver.

Parameters:
  • save_freq (int, optional) – Save a rolling checkpoint every this many global steps. None disables periodic saving. Defaults to None.

  • evaluator_every_n_steps (int, optional) – Treat every this many steps as an evaluation step, consulting the metric and updating model_best on improvement. Requires cfg.evaluate. None disables. Defaults to None.

Note

Runs on all ranks because a standard-format (DCP) save is a collective operation; the save decision is gated only on rank-consistent step counters, never on the metric (which some evaluators publish on rank 0 only), so ranks cannot diverge and deadlock. Place any evaluator hook before this saver in the hooks list so the metric is available when the saver runs.

Example

Add to cfg.hooks after an evaluator; it saves a rolling checkpoint every save_freq steps and, on evaluator steps, promotes model_best whenever the evaluator’s metric improves:

hooks = [
    dict(type="SemSegEvaluator", every_n_steps=1000),
    dict(type="CheckpointSaver", save_freq=1000,
         evaluator_every_n_steps=1000),
]
# → every 1000 steps writes the rolling model/last/ checkpoint; on each
#   eval step reads comm_info["current_metric_value"] and, if it beats
#   trainer.best_metric_value, writes model/model_best.pth; writes a
#   final checkpoint in after_train
after_epoch()[source]#

Save a last-step checkpoint after epoch-end metrics are logged.

after_step()[source]#

Save after scheduled steps or after evaluator metric updates.

after_train()[source]#

Persist a final checkpoint when training finishes.

before_train()[source]#

Seed internal step count from resumed trainer progress.