Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version hyperparameter to the checkpoints. #288

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions torchmdnet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import importlib.metadata
import subprocess

try:
__version__ = importlib.metadata.version("torchmd-net")
except importlib.metadata.PackageNotFoundError:
try:
__version__ = (
subprocess.check_output(["git", "describe", "--abbrev=0", "--tags"])
.strip()
.decode("utf-8")
)
RaulPPelaez marked this conversation as resolved.
Show resolved Hide resolved
except:
print("Failed to retrieve the current version, defaulting to 0")
__version__ = "0"
10 changes: 9 additions & 1 deletion torchmdnet/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import torch
from torch.autograd import grad
from torch import nn, Tensor
import torchmdnet
from torchmdnet.models import output_modules
from torchmdnet.models.wrappers import AtomFilter
from torchmdnet.models.utils import dtype_mapping
from torchmdnet import priors
from lightning_utilities.core.rank_zero import rank_zero_warn
import warnings

from packaging import version

def create_model(args, prior_model=None, mean=None, std=None):
"""Create a model from the given arguments.
Expand Down Expand Up @@ -156,6 +157,13 @@ def load_model(filepath, args=None, device="cpu", **kwargs):
if args is None:
args = ckpt["hyper_parameters"]

# Check the version that the checkpoint was created with
ckpt_version = ckpt.get("version", "0.15.2") # Default to the first version that introduced the version key
current_version = torchmdnet.__version__
if version.parse(ckpt_version) != version.parse(current_version):
warnings.warn(
f"Checkpoint was created with version {version}, current version is {current_version}."
)
delta_learning = args["remove_ref_energy"] if "remove_ref_energy" in args else False

for key, value in kwargs.items():
Expand Down
6 changes: 4 additions & 2 deletions torchmdnet/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
from torch.nn.functional import local_response_norm, mse_loss, l1_loss
from torch import Tensor
from typing import Optional, Dict, Tuple

import warnings
from lightning import LightningModule
import torchmdnet
from torchmdnet.models.model import create_model, load_model
from torchmdnet.models.utils import dtype_mapping
import torch_geometric.transforms as T
Expand Down Expand Up @@ -65,7 +66,8 @@ def __init__(self, hparams, prior_model=None, mean=None, std=None):
hparams["charge"] = False
if "spin" not in hparams:
hparams["spin"] = False

if "version" not in hparams:
hparams["version"] = torchmdnet.__version__
self.save_hyperparameters(hparams)

if self.hparams.load_model:
Expand Down
Loading