sage.utils.checkpoint
Filename : checkpoint.py Description : Short description of the file
Created on 2026-03-22 11:10:40
__author__ = Narenraju Nagarajan __copyright__ = Copyright 2026, ProjectName __license__ = MIT Licence __version__ = 0.0.1 __maintainer__ = Narenraju Nagarajan __affiliation__ = N/A __email__ = N/A __status__ = [‘inProgress’, ‘Archived’, ‘inUsage’, ‘Debugging’]
GitHub Repository: NULL
Documentation: NULL
Classes
Manages saving and loading of training checkpoints. |
Module Contents
- class CheckpointManager(cfg, data_cfg, model, optimizer, scheduler, scaler)[source]
Manages saving and loading of training checkpoints.
On construction, creates the checkpoint directory and writes one-time JSON snapshots of
cfganddata_cfgso that the hyperparameters that produced each checkpoint are always recoverable.Three checkpoint types are maintained:
latest.pt— overwritten every call tosave(); safe resume point after a crash or pre-emption.epoch_{N}.pt— per-epoch copy (optional; controlled bysave_epoch_ckpt). Allows rolling back to any specific epoch.best.pt— copy oflatest.ptwheneverval_lossimproves; the recommended checkpoint for inference.
Each checkpoint file contains the full training state: model weights, optimiser and scheduler states, AMP scaler state, configurations, and all four RNG states (Python, NumPy, CPU torch, CUDA torch) for bit-exact reproducibility.
- Parameters:
cfg (BaseConfig) – Training configuration (provides
export_dir,autocast,dtype).data_cfg (BaseDataConfig) – Data configuration (saved to JSON snapshot only).
model (nn.Module) – The model being trained (may be a
torch.compilewrapper).optimizer (torch.optim.Optimizer)
scheduler (torch.optim.lr_scheduler._LRScheduler)
scaler (torch.amp.GradScaler)
- save(epoch, val_loss=None, save_epoch_ckpt=True)[source]
Save the current training state to disk.
Always writes
latest.pt. Ifval_lossis better than the current best, also copies tobest.pt.
- load_latest(map_location='cpu')[source]
Load latest snapshot and resume training
- Parameters:
map_location (str, optional) – _description_. Defaults to “cpu”.
- Returns:
_description_
- Return type:
_type_
- Usage:
start_epoch = ckpt_mgr.load_latest(map_location=cfg.device) for nepoch in range(start_epoch, cfg.num_epochs): …