Reliability of _class_path in Lightning checkpoints when using instantiate_module
#21474
-
|
Hi Lightning team , I have a question regarding the reliability and long-term guarantees of relying on ContextIn my project, I load a Lightning checkpoint and re-instantiate both the model and datamodule using: from lightning.pytorch.cli import instantiate_moduleThis approach is entirely based on the assumption that the saved checkpoint contains a
These class paths are then consumed by Below is a simplified version of what I currently do: from lightning.pytorch.cli import instantiate_module
import torch
ckpt = torch.load(checkpoint_path, map_location="cpu", weights_only=False)
assert "_class_path" in ckpt["hyper_parameters"]
assert "_class_path" in ckpt["datamodule_hyper_parameters"]
model = instantiate_module(
SomeBaseModelClass,
{k: v for k, v in ckpt["hyper_parameters"].items() if k != "_instantiator"},
)
datamodule = instantiate_module(
XYBaseDataModule,
{k: v for k, v in ckpt["datamodule_hyper_parameters"].items() if k != "_instantiator"},
)This works well with recent Lightning versions, and is very convenient for fully config-driven workflows. Questions
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Note: I understand that Lightning provides the standard way to load checkpoints via LightningModule.load_from_checkpoint(...) However, that approach requires the model class to be known explicitly at load time. In my use case, all model and datamodule classes are already available in the environment, and the checkpoint itself contains the corresponding This makes the loading process fully config- and checkpoint-driven, which is why the presence and stability of |
Beta Was this translation helpful? Give feedback.
-
|
To answer some of my own questions:
After inspecting the lightning code and some minor analysis, I came to conclusion that
python my_cli_script.py --model=model.yaml --data=data.yamlwhere model.yaml: classpath: base.MyModeldata.yaml: classpath: database.MyDataClass |
Beta Was this translation helpful? Give feedback.
The idea was that it is not needed to know the class. The requirement is that what is in
_class_pathpoints to a subclass of the class used to callload_from_checkpoint. So, if you always useLightningModule.load_from_checkpoint, then any checkpoint should load because all modules are subclasses ofLightningModule.