CheckpointSaver#
- class CheckpointSaver(save_freq=None, evaluator_every_n_steps=None)[source]#
Bases:
HookBaseSave epoch/metric-oriented checkpoints during and after training.
Maintains a running global-step counter (seeded from resumed trainer progress in
before_train) and, inafter_step, saves a checkpoint whenever either cadence fires: a periodic save everysave_freqsteps, or an evaluator step everyevaluator_every_n_stepssteps (only whencfg.evaluateis set). On an evaluator step it also reads the evaluator’scurrent_metric_valuefromtrainer.comm_infoand, if it improves ontrainer.best_metric_value, writes amodel_bestsnapshot. A final checkpoint is written inafter_train. All actual save logic is delegated toCheckpointManager. Registered asCheckpointSaver.- Parameters:
save_freq (int, optional) – Save a rolling checkpoint every this many global steps.
Nonedisables periodic saving. Defaults toNone.evaluator_every_n_steps (int, optional) – Treat every this many steps as an evaluation step, consulting the metric and updating
model_beston improvement. Requirescfg.evaluate.Nonedisables. Defaults toNone.
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
hookslist so the metric is available when the saver runs.Example
Add to
cfg.hooksafter an evaluator; it saves a rolling checkpoint everysave_freqsteps and, on evaluator steps, promotesmodel_bestwhenever 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