nvalchemi.training.save_checkpoint#
- nvalchemi.training.save_checkpoint(root_folder, models=None, optimizers=None, schedulers=None, associations=None, checkpoint_index=-1, strategy=None)[source]#
Save a checkpoint with a manifest.
The low-level component form accepts explicit
models,optimizers, andschedulersmappings. The strategy-aware form acceptsstrategy=TrainingStrategy(...)(or the strategy as the second positional argument) and writes additionalstrategy.jsonmetadata with the serializable recipe and restart counters.- Parameters:
root_folder (Path | str) – Root directory for the checkpoint tree.
models (dict[str, tuple[Module, BaseSpec]] | Any | None) – Mapping of model name to
(module, spec)pairs, or aTrainingStrategyinstance.optimizers (dict[str, tuple[Optimizer, BaseSpec]] | None) – Optional mapping of optimizer name to
(optimizer, spec)pairs.schedulers (dict[str, tuple[LRScheduler, BaseSpec]] | None) – Optional mapping of scheduler name to
(scheduler, spec)pairs.associations (dict[str, dict[str, Any]] | None) – Optional model-centric linkage mapping a model name to
{"optimizers": [...], "schedulers": [...]}. WhenNone(default), associations are inferred automatically by matching optimizerparam_groupsto model parameters viadata_ptr()identity, and schedulers to optimizers via object identity.checkpoint_index (int) – Index for the checkpoint files.
-1(default) auto-increments from the manifest’s last index, or starts at0.strategy (Any | None) – Optional training strategy to save as a restartable checkpoint.
- Returns:
The checkpoint index that was written.
- Return type:
int
- Raises:
ValueError – If an existing
spec.jsondisagrees with the spec being saved (ignoringtimestamp).
Examples
>>> import tempfile, torch.nn as nn >>> from nvalchemi.training._spec import create_model_spec >>> with tempfile.TemporaryDirectory() as tmp: ... spec = create_model_spec(nn.Linear, in_features=4, out_features=2) ... save_checkpoint(tmp, models={"main": (nn.Linear(4, 2), spec)}) 0