nvalchemi.training.load_checkpoint#
- nvalchemi.training.load_checkpoint(root_folder, checkpoint_index=-1, map_location=None, model_names=None, *, adapter=None, adapter_kwargs=None, validators=None, hooks=None, training_fn=None, strategy=None, models=None, **runtime_overrides)[source]#
Load a multi-component checkpoint written by
save_checkpoint().Components are rebuilt in dependency order: models first, then optimizers (which need model parameters), then schedulers (which need an optimizer instance). Associations from the manifest wire each optimizer to the correct model and each scheduler to the correct optimizer.
- Parameters:
root_folder (Path | str) – Root directory containing
manifest.json.checkpoint_index (int) – Index of the checkpoint to load.
-1(default) loads the latest index recorded in the manifest.map_location (str | device | None) – Forwarded to every
torch.load()call. When notNone, each loaded model is additionally moved viamodel.to(map_location). Optimizers and schedulers have their state placed bytorch.loadalone (they lack a standard.to()API). Withstrategyit only stages the load: the live strategy’sdevicesstill decide where the restored models and optimizer state come to rest, and the returnedstrategy_metadatareports those devices rather thanmap_location.model_names (Iterable[str] | None) – If given, load only the models with these names together with the optimizers and schedulers wired to them through
manifest.associations. Accepts any iterable of strings (typically a set).None(default) loads every component on disk. The returned manifest’sassociationsstill reflects the full on-disk mapping, so callers can inspect what was not loaded.adapter (str | None) – Optional foreign-checkpoint adapter name. V1 supports
"mace"for trusted local MACE.ptfiles.adapter_kwargs (Mapping[str, Any] | None) – Adapter-specific options. For
adapter="mace", accepted keys aremodel_name,dtype,enable_cueq,compile_model, andcompile_kwargs.validators (Sequence[Callable[[str, Mapping[str, Any], Mapping[str, Any]], None]] | None) – Optional callbacks invoked as
validator(model_name, entry, loaded)for each high-level loaded model entry. Use these for model-specific chemistry or topology compatibility checks.hooks (Sequence[Any] | None) – Runtime hooks supplied when reconstructing a saved strategy.
training_fn (Any) – Runtime training function override supplied when reconstructing a saved strategy.
strategy (Any | None) – Optional already-constructed strategy to hydrate from the checkpoint. This mode restores model, optimizer, scheduler, runtime-counter, and checkpointable hook state into the live objects instead of rebuilding models from saved specs.
models (BaseModelMixin | dict[str, BaseModelMixin] | ModuleDict | None) – Live models to restore the checkpoint’s weights into, in place of the ones the loader builds from the saved specs: a single model for a checkpoint whose one model is
"main", or a mapping whose names are exactlymanifest.models. Likeruntime_overrides, accepted only when the saved strategy is rebuilt from its metadata. A caller whose other objects already hold the models, such as a propagator built around the student it trains, passes them here so the weights land in those very objects.**runtime_overrides (Any) – Runtime overrides: live objects that a saved spec cannot carry, passed as extra keyword arguments. They are forwarded to the strategy class’s
from_spec_dictwhen a saved strategy is rebuilt from its metadata, and a subclass documents the ones it accepts. They are refused in the modes that rebuild no strategy.
- Returns:
CheckpointManifest – For legacy component-only checkpoints, a hydrated manifest is returned.
dict[str, Any] – For strategy checkpoints or adapter loads, a builtin dict containing
strategy,models,manifest,checkpoint_index, andsourceis returned.
- Raises:
FileNotFoundError – If
manifest.jsonis missing or a checkpoint.ptfile does not exist.KeyError – If any name in
model_namesdoes not appear inmanifest.models.ValueError – If
modelsdoes not name exactly the models inmanifest.models.RuntimeError – If a model spec does not build an
Module.TypeError – If
runtime_overridesormodelsare given but no strategy is rebuilt from metadata, or if a runtime override reaches afrom_spec_dictthat does not accept it.
- Return type:
CheckpointManifest | dict[str, Any]
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)}) ... result = load_checkpoint(tmp) ... isinstance(result.models["main"][0], nn.Linear) True
Loading onto CPU regardless of the original device:
result = load_checkpoint("runs/exp1", map_location="cpu")
Selecting a subset of models (e.g., teacher and student but not the third auxiliary model):
result = load_checkpoint("runs/kd", model_names={"teacher", "student"})