From b4d8a4c76e157de159694dbd49cc6ca052e90886 Mon Sep 17 00:00:00 2001 From: Charlelie Laurent Date: Thu, 23 Oct 2025 17:16:12 -0700 Subject: [PATCH 1/2] Improved docstring for from_checkpoint Signed-off-by: Charlelie Laurent --- physicsnemo/models/module.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/physicsnemo/models/module.py b/physicsnemo/models/module.py index 298c0ab45e..1f91241c53 100644 --- a/physicsnemo/models/module.py +++ b/physicsnemo/models/module.py @@ -591,12 +591,16 @@ def from_checkpoint( override_args: Optional[Dict[str, Any]] = None, strict: bool = True, ) -> physicsnemo.Module: - """Simple utility for constructing a model from a checkpoint + r""" + Simple utility for constructing a model from a checkpoint. This method + implements a mechanism to instantiatie the model from a checkpoint file, + such that it is able to instantiate models that are **not already + instantiated**. Parameters ---------- file_name : str - Checkpoint file name + Checkpoint file name. Should be a ``.mdlus`` file. override_args : Optional[Dict[str, Any]], optional, default=None Dictionary of arguments to override the ``__init__`` method's arguments saved in the checkpoint. The override of arguments occurs @@ -627,6 +631,18 @@ class attribute. Attempting to override any other argument will raise IOError If file_name provided does not exist or is not a valid checkpoint + .. important:: + + Calling ``physicsnemo.Module.from_checkpoint`` on a PhysicsNeMo + model checkpoint (``.mdlus`` file) will attempt to automatically infer + the class of the model from the checkpoint file. However, if the + class is known beforehand (say ``MyModuleSubclass``), it is + recommended to use ``MyModuleSubclass.from_checkpoint`` instead. + This applies to both models classes included as part of the + PhysicsNeMo libraray, as well as user-defined subclasses of + ``physicsnemo.Module``. + + Examples -------- Simple argument override: From 6fd9e97596b037bbfca27d0c2f497bde5982dd45 Mon Sep 17 00:00:00 2001 From: Charlelie Laurent Date: Thu, 23 Oct 2025 20:31:47 -0700 Subject: [PATCH 2/2] Improved docstring for from_checkpoint Signed-off-by: Charlelie Laurent --- physicsnemo/models/module.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/physicsnemo/models/module.py b/physicsnemo/models/module.py index 1f91241c53..46469a76b1 100644 --- a/physicsnemo/models/module.py +++ b/physicsnemo/models/module.py @@ -639,8 +639,18 @@ class attribute. Attempting to override any other argument will raise class is known beforehand (say ``MyModuleSubclass``), it is recommended to use ``MyModuleSubclass.from_checkpoint`` instead. This applies to both models classes included as part of the - PhysicsNeMo libraray, as well as user-defined subclasses of - ``physicsnemo.Module``. + PhysicsNeMo library, as well as user-defined subclasses of + ``physicsnemo.Module``. More specifically, the mechanism for inferring + the class attempts to, by order of precedence: + + 1. Use the qualifier (in the example above, + "MyModuleSubclass") as the class. + 2. Lookup the class in the model registry + (:class:`~physicsnemo.registry.model_registry.ModelRegistry`). + 3. Import the class from the module path specified in + the checkpoint file. Note that this will fail if the module + path is modified from the original path (e.g. if the class + definition was moved from one module to another). Examples