From 7145d32dcfc77e467bb48de9515a19c6623a9d21 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 27 Feb 2024 18:25:44 -0500 Subject: [PATCH 1/5] add @version to serialization data At this time, all current versions and the minimal and maximum versions are set to 1. If any changes, whether breaking or not, are made to the serialization data, the version should be bumped. Signed-off-by: Jinzhe Zeng --- .../dpmodel/atomic_model/dp_atomic_model.py | 5 ++++ .../atomic_model/linear_atomic_model.py | 7 +++++ .../atomic_model/pairtab_atomic_model.py | 5 ++++ deepmd/dpmodel/descriptor/se_e2_a.py | 5 ++++ deepmd/dpmodel/descriptor/se_r.py | 5 ++++ deepmd/dpmodel/fitting/general_fitting.py | 5 ++++ deepmd/dpmodel/utils/version.py | 27 +++++++++++++++++++ .../pt/model/atomic_model/dp_atomic_model.py | 5 ++++ .../model/atomic_model/linear_atomic_model.py | 10 +++++++ .../atomic_model/pairtab_atomic_model.py | 7 +++++ deepmd/pt/model/descriptor/se_a.py | 5 ++++ deepmd/pt/model/descriptor/se_r.py | 5 ++++ deepmd/pt/model/task/fitting.py | 5 ++++ deepmd/tf/descriptor/se_a.py | 5 ++++ deepmd/tf/descriptor/se_r.py | 5 ++++ deepmd/tf/fit/dipole.py | 6 +++++ deepmd/tf/fit/ener.py | 6 +++++ deepmd/tf/fit/polar.py | 6 +++++ deepmd/tf/model/model.py | 6 ++++- 19 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 deepmd/dpmodel/utils/version.py diff --git a/deepmd/dpmodel/atomic_model/dp_atomic_model.py b/deepmd/dpmodel/atomic_model/dp_atomic_model.py index 178b286e79..bffdb84cc7 100644 --- a/deepmd/dpmodel/atomic_model/dp_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/dp_atomic_model.py @@ -17,6 +17,9 @@ from deepmd.dpmodel.output_def import ( FittingOutputDef, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from .base_atomic_model import ( BaseAtomicModel, @@ -132,6 +135,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "standard", + "@version": 1, "type_map": self.type_map, "descriptor": self.descriptor.serialize(), "fitting": self.fitting.serialize(), @@ -140,6 +144,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPAtomicModel": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class") data.pop("type") descriptor_obj = BaseDescriptor.deserialize(data["descriptor"]) diff --git a/deepmd/dpmodel/atomic_model/linear_atomic_model.py b/deepmd/dpmodel/atomic_model/linear_atomic_model.py index e1130eaf45..44a4e09347 100644 --- a/deepmd/dpmodel/atomic_model/linear_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/linear_atomic_model.py @@ -19,6 +19,9 @@ get_multiple_nlist_key, nlist_distinguish_types, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from ..output_def import ( FittingOutputDef, @@ -185,6 +188,7 @@ def serialize(models) -> dict: return { "@class": "Model", "type": "linear", + "@version": 1, "models": [model.serialize() for model in models], "model_name": [model.__class__.__name__ for model in models], } @@ -192,6 +196,7 @@ def serialize(models) -> dict: @staticmethod def deserialize(data) -> List[BaseAtomicModel]: data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class") data.pop("type") model_names = data["model_name"] @@ -271,6 +276,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "zbl", + "@version": 1, "models": LinearAtomicModel.serialize([self.dp_model, self.zbl_model]), "sw_rmin": self.sw_rmin, "sw_rmax": self.sw_rmax, @@ -280,6 +286,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPZBLLinearAtomicModel": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class") data.pop("type") sw_rmin = data["sw_rmin"] diff --git a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py index dc3dfaf2ed..cc5d0993f4 100644 --- a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py @@ -13,6 +13,9 @@ FittingOutputDef, OutputVariableDef, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.utils.pair_tab import ( PairTab, ) @@ -109,6 +112,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "pairtab", + "@version": 1, "tab": self.tab.serialize(), "rcut": self.rcut, "sel": self.sel, @@ -117,6 +121,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "PairTabAtomicModel": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class") data.pop("type") rcut = data["rcut"] diff --git a/deepmd/dpmodel/descriptor/se_e2_a.py b/deepmd/dpmodel/descriptor/se_e2_a.py index a28215c35a..865b882940 100644 --- a/deepmd/dpmodel/descriptor/se_e2_a.py +++ b/deepmd/dpmodel/descriptor/se_e2_a.py @@ -3,6 +3,9 @@ import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) @@ -345,6 +348,7 @@ def serialize(self) -> dict: return { "@class": "Descriptor", "type": "se_e2_a", + "@version": 1, "rcut": self.rcut, "rcut_smth": self.rcut_smth, "sel": self.sel, @@ -371,6 +375,7 @@ def serialize(self) -> dict: def deserialize(cls, data: dict) -> "DescrptSeA": """Deserialize from dict.""" data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) data.pop("type", None) variables = data.pop("@variables") diff --git a/deepmd/dpmodel/descriptor/se_r.py b/deepmd/dpmodel/descriptor/se_r.py index 77e43f7d85..47923a20d6 100644 --- a/deepmd/dpmodel/descriptor/se_r.py +++ b/deepmd/dpmodel/descriptor/se_r.py @@ -1,6 +1,9 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.utils.path import ( DPPath, ) @@ -282,6 +285,7 @@ def serialize(self) -> dict: return { "@class": "Descriptor", "type": "se_r", + "@version": 1, "rcut": self.rcut, "rcut_smth": self.rcut_smth, "sel": self.sel, @@ -307,6 +311,7 @@ def serialize(self) -> dict: def deserialize(cls, data: dict) -> "DescrptSeR": """Deserialize from dict.""" data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) data.pop("type", None) variables = data.pop("@variables") diff --git a/deepmd/dpmodel/fitting/general_fitting.py b/deepmd/dpmodel/fitting/general_fitting.py index 152836e928..c50dfb4674 100644 --- a/deepmd/dpmodel/fitting/general_fitting.py +++ b/deepmd/dpmodel/fitting/general_fitting.py @@ -21,6 +21,9 @@ FittingNet, NetworkCollection, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from .base_fitting import ( BaseFitting, @@ -210,6 +213,7 @@ def serialize(self) -> dict: """Serialize the fitting to dict.""" return { "@class": "Fitting", + "@version": 1, "var_name": self.var_name, "ntypes": self.ntypes, "dim_descrpt": self.dim_descrpt, @@ -241,6 +245,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class") data.pop("type") variables = data.pop("@variables") diff --git a/deepmd/dpmodel/utils/version.py b/deepmd/dpmodel/utils/version.py new file mode 100644 index 0000000000..a0b479778d --- /dev/null +++ b/deepmd/dpmodel/utils/version.py @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: LGPL-3.0-or-later +def check_version_compatibility( + current_version: int, + maximum_supported_version: int, + minimal_supported_version: int = 1, +): + """Check if the current version is compatible with the supported versions. + + Parameters + ---------- + current_version : int + The current version. + maximum_supported_version : int + The maximum supported version. + minimal_supported_version : int, optional + The minimal supported version. Default is 1. + + Raises + ------ + ValueError + If the current version is not compatible with the supported versions. + """ + if not minimal_supported_version <= current_version <= maximum_supported_version: + raise ValueError( + f"Current version {current_version} is not compatible with supported versions " + f"[{minimal_supported_version}, {maximum_supported_version}]." + ) diff --git a/deepmd/pt/model/atomic_model/dp_atomic_model.py b/deepmd/pt/model/atomic_model/dp_atomic_model.py index 881ea4c97d..2415c6e17d 100644 --- a/deepmd/pt/model/atomic_model/dp_atomic_model.py +++ b/deepmd/pt/model/atomic_model/dp_atomic_model.py @@ -12,6 +12,9 @@ from deepmd.dpmodel import ( FittingOutputDef, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.pt.model.descriptor.base_descriptor import ( BaseDescriptor, ) @@ -95,6 +98,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "standard", + "@version": 1, "type_map": self.type_map, "descriptor": self.descriptor.serialize(), "fitting": self.fitting_net.serialize(), @@ -103,6 +107,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPAtomicModel": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) descriptor_obj = BaseDescriptor.deserialize(data["descriptor"]) fitting_obj = BaseFitting.deserialize(data["fitting"]) obj = cls(descriptor_obj, fitting_obj, type_map=data["type_map"]) diff --git a/deepmd/pt/model/atomic_model/linear_atomic_model.py b/deepmd/pt/model/atomic_model/linear_atomic_model.py index 68ff303d64..8d573fcc62 100644 --- a/deepmd/pt/model/atomic_model/linear_atomic_model.py +++ b/deepmd/pt/model/atomic_model/linear_atomic_model.py @@ -1,4 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later +import copy import sys from abc import ( abstractmethod, @@ -16,6 +17,9 @@ FittingOutputDef, OutputVariableDef, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.pt.utils import ( env, ) @@ -206,6 +210,7 @@ def fitting_output_def(self) -> FittingOutputDef: def serialize(models) -> dict: return { "@class": "Model", + "@version": 1, "type": "linear", "models": [model.serialize() for model in models], "model_name": [model.__class__.__name__ for model in models], @@ -213,6 +218,8 @@ def serialize(models) -> dict: @staticmethod def deserialize(data) -> List[BaseAtomicModel]: + data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) model_names = data["model_name"] models = [ getattr(sys.modules[__name__], name).deserialize(model) @@ -303,6 +310,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "zbl", + "@version": 1, "models": LinearAtomicModel.serialize([self.dp_model, self.zbl_model]), "sw_rmin": self.sw_rmin, "sw_rmax": self.sw_rmax, @@ -311,6 +319,8 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "DPZBLLinearAtomicModel": + data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) sw_rmin = data["sw_rmin"] sw_rmax = data["sw_rmax"] smin_alpha = data["smin_alpha"] diff --git a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py index 86bfe98c36..58430520d1 100644 --- a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py +++ b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py @@ -1,4 +1,5 @@ # SPDX-License-Identifier: LGPL-3.0-or-later +import copy from typing import ( Dict, List, @@ -12,6 +13,9 @@ FittingOutputDef, OutputVariableDef, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.utils.pair_tab import ( PairTab, ) @@ -124,6 +128,7 @@ def serialize(self) -> dict: return { "@class": "Model", "type": "pairtab", + "@version": 1, "tab": self.tab.serialize(), "rcut": self.rcut, "sel": self.sel, @@ -131,6 +136,8 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "PairTabAtomicModel": + data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) rcut = data["rcut"] sel = data["sel"] tab = PairTab.deserialize(data["tab"]) diff --git a/deepmd/pt/model/descriptor/se_a.py b/deepmd/pt/model/descriptor/se_a.py index 033d640ad8..7f8a592d40 100644 --- a/deepmd/pt/model/descriptor/se_a.py +++ b/deepmd/pt/model/descriptor/se_a.py @@ -11,6 +11,9 @@ import numpy as np import torch +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.pt.model.descriptor import ( DescriptorBlock, prod_env_mat, @@ -182,6 +185,7 @@ def serialize(self) -> dict: return { "@class": "Descriptor", "type": "se_e2_a", + "@version": 1, "rcut": obj.rcut, "rcut_smth": obj.rcut_smth, "sel": obj.sel, @@ -208,6 +212,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "DescrptSeA": data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) data.pop("type", None) variables = data.pop("@variables") diff --git a/deepmd/pt/model/descriptor/se_r.py b/deepmd/pt/model/descriptor/se_r.py index c685640426..6e92fcdb27 100644 --- a/deepmd/pt/model/descriptor/se_r.py +++ b/deepmd/pt/model/descriptor/se_r.py @@ -10,6 +10,9 @@ import torch from deepmd.dpmodel.utils import EnvMat as DPEnvMat +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.pt.model.descriptor import ( prod_env_mat, ) @@ -277,6 +280,7 @@ def serialize(self) -> dict: return { "@class": "Descriptor", "type": "se_r", + "@version": 1, "rcut": self.rcut, "rcut_smth": self.rcut_smth, "sel": self.sel, @@ -302,6 +306,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "DescrptSeR": data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) variables = data.pop("@variables") embeddings = data.pop("embeddings") env_mat = data.pop("env_mat") diff --git a/deepmd/pt/model/task/fitting.py b/deepmd/pt/model/task/fitting.py index 6c395d3800..4fd168a55c 100644 --- a/deepmd/pt/model/task/fitting.py +++ b/deepmd/pt/model/task/fitting.py @@ -12,6 +12,9 @@ import numpy as np import torch +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.pt.model.network.mlp import ( FittingNet, NetworkCollection, @@ -367,6 +370,7 @@ def serialize(self) -> dict: """Serialize the fitting to dict.""" return { "@class": "Fitting", + "@version": 1, "var_name": self.var_name, "ntypes": self.ntypes, "dim_descrpt": self.dim_descrpt, @@ -404,6 +408,7 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data: dict) -> "GeneralFitting": data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) variables = data.pop("@variables") nets = data.pop("nets") obj = cls(**data) diff --git a/deepmd/tf/descriptor/se_a.py b/deepmd/tf/descriptor/se_a.py index e1b7258c63..beefa90d63 100644 --- a/deepmd/tf/descriptor/se_a.py +++ b/deepmd/tf/descriptor/se_a.py @@ -10,6 +10,9 @@ from deepmd.dpmodel.utils.env_mat import ( EnvMat, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -1368,6 +1371,7 @@ def deserialize(cls, data: dict, suffix: str = ""): if cls is not DescrptSeA: raise NotImplementedError("Not implemented in class %s" % cls.__name__) data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) data.pop("@class", None) data.pop("type", None) embedding_net_variables = cls.deserialize_network( @@ -1422,6 +1426,7 @@ def serialize(self, suffix: str = "") -> dict: return { "@class": "Descriptor", "type": "se_e2_a", + "@version": 1, "rcut": self.rcut_r, "rcut_smth": self.rcut_r_smth, "sel": self.sel_a, diff --git a/deepmd/tf/descriptor/se_r.py b/deepmd/tf/descriptor/se_r.py index 1a12befdf0..5fe838b823 100644 --- a/deepmd/tf/descriptor/se_r.py +++ b/deepmd/tf/descriptor/se_r.py @@ -10,6 +10,9 @@ from deepmd.dpmodel.utils.env_mat import ( EnvMat, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -720,6 +723,7 @@ def deserialize(cls, data: dict, suffix: str = ""): if cls is not DescrptSeR: raise NotImplementedError("Not implemented in class %s" % cls.__name__) data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) embedding_net_variables = cls.deserialize_network( data.pop("embeddings"), suffix=suffix ) @@ -763,6 +767,7 @@ def serialize(self, suffix: str = "") -> dict: return { "@class": "Descriptor", "type": "se_r", + "@version": 1, "rcut": self.rcut, "rcut_smth": self.rcut_smth, "sel": self.sel_r, diff --git a/deepmd/tf/fit/dipole.py b/deepmd/tf/fit/dipole.py index 3557d00aa0..4ef8ccebb5 100644 --- a/deepmd/tf/fit/dipole.py +++ b/deepmd/tf/fit/dipole.py @@ -6,6 +6,9 @@ import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -346,6 +349,7 @@ def serialize(self, suffix: str) -> dict: data = { "@class": "Fitting", "type": "dipole", + "@version": 1, "var_name": "dipole", "ntypes": self.ntypes, "dim_descrpt": self.dim_descrpt, @@ -388,6 +392,8 @@ def deserialize(cls, data: dict, suffix: str): Model The deserialized model """ + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) fitting = cls(**data) fitting.fitting_net_variables = cls.deserialize_network( data["nets"], diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index 0cdd1a1676..12923e2512 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -8,6 +8,9 @@ import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.common import ( add_data_requirement, cast_precision, @@ -959,6 +962,8 @@ def deserialize(cls, data: dict, suffix: str = ""): Model The deserialized model """ + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) fitting = cls(**data) fitting.fitting_net_variables = cls.deserialize_network( data["nets"], @@ -984,6 +989,7 @@ def serialize(self, suffix: str = "") -> dict: data = { "@class": "Fitting", "type": "ener", + "@version": 1, "var_name": "energy", "ntypes": self.ntypes, "dim_descrpt": self.dim_descrpt, diff --git a/deepmd/tf/fit/polar.py b/deepmd/tf/fit/polar.py index f5cebf9a39..8a3f5c94ca 100644 --- a/deepmd/tf/fit/polar.py +++ b/deepmd/tf/fit/polar.py @@ -7,6 +7,9 @@ import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -536,6 +539,7 @@ def serialize(self, suffix: str) -> dict: data = { "@class": "Fitting", "type": "polar", + "@version": 1, "var_name": "polar", "ntypes": self.ntypes, "dim_descrpt": self.dim_descrpt, @@ -581,6 +585,8 @@ def deserialize(cls, data: dict, suffix: str): Model The deserialized model """ + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) fitting = cls(**data) fitting.fitting_net_variables = cls.deserialize_network( data["nets"], diff --git a/deepmd/tf/model/model.py b/deepmd/tf/model/model.py index 76310834a7..38728d8027 100644 --- a/deepmd/tf/model/model.py +++ b/deepmd/tf/model/model.py @@ -17,6 +17,9 @@ from deepmd.common import ( j_get_type, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) from deepmd.tf.descriptor.descriptor import ( Descriptor, ) @@ -815,7 +818,7 @@ def deserialize(cls, data: dict, suffix: str = "") -> "Descriptor": The deserialized descriptor """ data = copy.deepcopy(data) - + check_version_compatibility(data.pop("@version", 1), 1, 1) descriptor = Descriptor.deserialize(data.pop("descriptor"), suffix=suffix) fitting = Fitting.deserialize(data.pop("fitting"), suffix=suffix) return cls( @@ -844,6 +847,7 @@ def serialize(self, suffix: str = "") -> dict: return { "@class": "Model", "type": "standard", + "@version": 1, "type_map": self.type_map, "descriptor": self.descrpt.serialize(suffix=suffix), "fitting": self.fitting.serialize(suffix=suffix), From aedc5bca9ce11534243b1e5dafe51d930a58e74d Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 27 Feb 2024 18:39:59 -0500 Subject: [PATCH 2/5] add version info to NN and PairTab Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/utils/network.py | 30 +++++++++++++++++++++++++++++- deepmd/utils/pair_tab.py | 9 +++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/deepmd/dpmodel/utils/network.py b/deepmd/dpmodel/utils/network.py index 2133bc4889..bf42c99406 100644 --- a/deepmd/dpmodel/utils/network.py +++ b/deepmd/dpmodel/utils/network.py @@ -20,6 +20,10 @@ import h5py import numpy as np +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) + try: from deepmd._version import version as __version__ except ImportError: @@ -189,6 +193,8 @@ def serialize(self) -> dict: "idt": self.idt, } return { + "@class": "Layer", + "@version": 1, "bias": self.b is not None, "use_timestep": self.idt is not None, "activation_function": self.activation_function, @@ -208,6 +214,8 @@ def deserialize(cls, data: dict) -> "NativeLayer": The dict to deserialize from. """ data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") variables = data.pop("@variables") assert variables["w"] is not None and len(variables["w"].shape) == 2 num_in, num_out = variables["w"].shape @@ -349,7 +357,11 @@ def serialize(self) -> dict: dict The serialized network. """ - return {"layers": [layer.serialize() for layer in self.layers]} + return { + "@class": "NN", + "@version": 1, + "layers": [layer.serialize() for layer in self.layers], + } @classmethod def deserialize(cls, data: dict) -> "NN": @@ -360,6 +372,9 @@ def deserialize(cls, data: dict) -> "NN": data : dict The dict to deserialize from. """ + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") return cls(data["layers"]) def __getitem__(self, key): @@ -471,6 +486,8 @@ def serialize(self) -> dict: The serialized network. """ return { + "@class": "EmbeddingNetwork", + "@version": 1, "in_dim": self.in_dim, "neuron": self.neuron.copy(), "activation_function": self.activation_function, @@ -490,6 +507,8 @@ def deserialize(cls, data: dict) -> "EmbeddingNet": The dict to deserialize from. """ data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") layers = data.pop("layers") obj = cls(**data) super(EN, obj).__init__(layers) @@ -566,6 +585,8 @@ def serialize(self) -> dict: The serialized network. """ return { + "@class": "FittingNetwork", + "@version": 1, "in_dim": self.in_dim, "out_dim": self.out_dim, "neuron": self.neuron.copy(), @@ -586,6 +607,8 @@ def deserialize(cls, data: dict) -> "FittingNet": The dict to deserialize from. """ data = copy.deepcopy(data) + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") layers = data.pop("layers") obj = cls(**data) T_Network.__init__(obj, layers) @@ -688,6 +711,8 @@ def serialize(self) -> dict: network_type_map_inv = {v: k for k, v in self.NETWORK_TYPE_MAP.items()} network_type_name = network_type_map_inv[self.network_type] return { + "@class": "NetworkCollection", + "@version": 1, "ndim": self.ndim, "ntypes": self.ntypes, "network_type": network_type_name, @@ -703,4 +728,7 @@ def deserialize(cls, data: dict) -> "NetworkCollection": data : dict The dict to deserialize from. """ + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") return cls(**data) diff --git a/deepmd/utils/pair_tab.py b/deepmd/utils/pair_tab.py index b807354171..ab1e805459 100644 --- a/deepmd/utils/pair_tab.py +++ b/deepmd/utils/pair_tab.py @@ -12,6 +12,10 @@ CubicSpline, ) +from deepmd.dpmodel.utils.version import ( + check_version_compatibility, +) + log = logging.getLogger(__name__) @@ -72,6 +76,8 @@ def reinit(self, filename: str, rcut: Optional[float] = None) -> None: def serialize(self) -> dict: return { + "@class": "PairTab", + "@version": 1, "rmin": self.rmin, "rmax": self.rmax, "hh": self.hh, @@ -87,6 +93,9 @@ def serialize(self) -> dict: @classmethod def deserialize(cls, data) -> "PairTab": + data = data.copy() + check_version_compatibility(data.pop("@version", 1), 1, 1) + data.pop("@class") variables = data.pop("@variables") tab = PairTab(None, None) tab.vdata = variables["vdata"] From 334b4265c275c27347c18970be0ae835ba76dde6 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 27 Feb 2024 19:07:28 -0500 Subject: [PATCH 3/5] fix imports Signed-off-by: Jinzhe Zeng --- deepmd/dpmodel/atomic_model/dp_atomic_model.py | 2 +- deepmd/dpmodel/atomic_model/linear_atomic_model.py | 2 +- deepmd/dpmodel/atomic_model/pairtab_atomic_model.py | 6 +++--- deepmd/dpmodel/descriptor/se_e2_a.py | 6 +++--- deepmd/dpmodel/descriptor/se_r.py | 6 +++--- deepmd/dpmodel/fitting/general_fitting.py | 2 +- deepmd/dpmodel/utils/network.py | 2 +- deepmd/pt/model/atomic_model/dp_atomic_model.py | 6 +++--- deepmd/pt/model/atomic_model/linear_atomic_model.py | 6 +++--- deepmd/pt/model/atomic_model/pairtab_atomic_model.py | 6 +++--- deepmd/pt/model/descriptor/se_a.py | 6 +++--- deepmd/pt/model/descriptor/se_r.py | 6 +++--- deepmd/pt/model/task/fitting.py | 6 +++--- deepmd/tf/descriptor/se_a.py | 6 +++--- deepmd/tf/descriptor/se_r.py | 6 +++--- deepmd/tf/fit/dipole.py | 6 +++--- deepmd/tf/fit/ener.py | 6 +++--- deepmd/tf/fit/polar.py | 6 +++--- deepmd/tf/model/model.py | 6 +++--- deepmd/utils/pair_tab.py | 2 +- deepmd/{dpmodel => }/utils/version.py | 0 21 files changed, 50 insertions(+), 50 deletions(-) rename deepmd/{dpmodel => }/utils/version.py (100%) diff --git a/deepmd/dpmodel/atomic_model/dp_atomic_model.py b/deepmd/dpmodel/atomic_model/dp_atomic_model.py index bffdb84cc7..cd349749fa 100644 --- a/deepmd/dpmodel/atomic_model/dp_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/dp_atomic_model.py @@ -17,7 +17,7 @@ from deepmd.dpmodel.output_def import ( FittingOutputDef, ) -from deepmd.dpmodel.utils.version import ( +from deepmd.utils.version import ( check_version_compatibility, ) diff --git a/deepmd/dpmodel/atomic_model/linear_atomic_model.py b/deepmd/dpmodel/atomic_model/linear_atomic_model.py index 44a4e09347..6d8aea499e 100644 --- a/deepmd/dpmodel/atomic_model/linear_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/linear_atomic_model.py @@ -19,7 +19,7 @@ get_multiple_nlist_key, nlist_distinguish_types, ) -from deepmd.dpmodel.utils.version import ( +from deepmd.utils.version import ( check_version_compatibility, ) diff --git a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py index cc5d0993f4..ddece80f2d 100644 --- a/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py +++ b/deepmd/dpmodel/atomic_model/pairtab_atomic_model.py @@ -13,12 +13,12 @@ FittingOutputDef, OutputVariableDef, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.utils.pair_tab import ( PairTab, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .base_atomic_model import ( BaseAtomicModel, diff --git a/deepmd/dpmodel/descriptor/se_e2_a.py b/deepmd/dpmodel/descriptor/se_e2_a.py index 865b882940..b102933ac9 100644 --- a/deepmd/dpmodel/descriptor/se_e2_a.py +++ b/deepmd/dpmodel/descriptor/se_e2_a.py @@ -3,15 +3,15 @@ import numpy as np -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.env import ( GLOBAL_NP_FLOAT_PRECISION, ) from deepmd.utils.path import ( DPPath, ) +from deepmd.utils.version import ( + check_version_compatibility, +) try: from deepmd._version import version as __version__ diff --git a/deepmd/dpmodel/descriptor/se_r.py b/deepmd/dpmodel/descriptor/se_r.py index 47923a20d6..5973c55353 100644 --- a/deepmd/dpmodel/descriptor/se_r.py +++ b/deepmd/dpmodel/descriptor/se_r.py @@ -1,12 +1,12 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import numpy as np -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.utils.path import ( DPPath, ) +from deepmd.utils.version import ( + check_version_compatibility, +) try: from deepmd._version import version as __version__ diff --git a/deepmd/dpmodel/fitting/general_fitting.py b/deepmd/dpmodel/fitting/general_fitting.py index c50dfb4674..752a550849 100644 --- a/deepmd/dpmodel/fitting/general_fitting.py +++ b/deepmd/dpmodel/fitting/general_fitting.py @@ -21,7 +21,7 @@ FittingNet, NetworkCollection, ) -from deepmd.dpmodel.utils.version import ( +from deepmd.utils.version import ( check_version_compatibility, ) diff --git a/deepmd/dpmodel/utils/network.py b/deepmd/dpmodel/utils/network.py index bf42c99406..e43ae829f3 100644 --- a/deepmd/dpmodel/utils/network.py +++ b/deepmd/dpmodel/utils/network.py @@ -20,7 +20,7 @@ import h5py import numpy as np -from deepmd.dpmodel.utils.version import ( +from deepmd.utils.version import ( check_version_compatibility, ) diff --git a/deepmd/pt/model/atomic_model/dp_atomic_model.py b/deepmd/pt/model/atomic_model/dp_atomic_model.py index 2415c6e17d..d2c1743d30 100644 --- a/deepmd/pt/model/atomic_model/dp_atomic_model.py +++ b/deepmd/pt/model/atomic_model/dp_atomic_model.py @@ -12,9 +12,6 @@ from deepmd.dpmodel import ( FittingOutputDef, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.pt.model.descriptor.base_descriptor import ( BaseDescriptor, ) @@ -27,6 +24,9 @@ from deepmd.utils.path import ( DPPath, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .base_atomic_model import ( BaseAtomicModel, diff --git a/deepmd/pt/model/atomic_model/linear_atomic_model.py b/deepmd/pt/model/atomic_model/linear_atomic_model.py index 8d573fcc62..52f5f1d13c 100644 --- a/deepmd/pt/model/atomic_model/linear_atomic_model.py +++ b/deepmd/pt/model/atomic_model/linear_atomic_model.py @@ -17,9 +17,6 @@ FittingOutputDef, OutputVariableDef, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.pt.utils import ( env, ) @@ -28,6 +25,9 @@ get_multiple_nlist_key, nlist_distinguish_types, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .base_atomic_model import ( BaseAtomicModel, diff --git a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py index 58430520d1..c0b7c65d7a 100644 --- a/deepmd/pt/model/atomic_model/pairtab_atomic_model.py +++ b/deepmd/pt/model/atomic_model/pairtab_atomic_model.py @@ -13,12 +13,12 @@ FittingOutputDef, OutputVariableDef, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.utils.pair_tab import ( PairTab, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .base_atomic_model import ( BaseAtomicModel, diff --git a/deepmd/pt/model/descriptor/se_a.py b/deepmd/pt/model/descriptor/se_a.py index 7f8a592d40..6c29636d6d 100644 --- a/deepmd/pt/model/descriptor/se_a.py +++ b/deepmd/pt/model/descriptor/se_a.py @@ -11,9 +11,6 @@ import numpy as np import torch -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.pt.model.descriptor import ( DescriptorBlock, prod_env_mat, @@ -34,6 +31,9 @@ from deepmd.utils.path import ( DPPath, ) +from deepmd.utils.version import ( + check_version_compatibility, +) try: from typing import ( diff --git a/deepmd/pt/model/descriptor/se_r.py b/deepmd/pt/model/descriptor/se_r.py index 6e92fcdb27..bdb7dafe73 100644 --- a/deepmd/pt/model/descriptor/se_r.py +++ b/deepmd/pt/model/descriptor/se_r.py @@ -10,9 +10,6 @@ import torch from deepmd.dpmodel.utils import EnvMat as DPEnvMat -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.pt.model.descriptor import ( prod_env_mat, ) @@ -39,6 +36,9 @@ from deepmd.utils.path import ( DPPath, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .base_descriptor import ( BaseDescriptor, diff --git a/deepmd/pt/model/task/fitting.py b/deepmd/pt/model/task/fitting.py index 4fd168a55c..0c64983f60 100644 --- a/deepmd/pt/model/task/fitting.py +++ b/deepmd/pt/model/task/fitting.py @@ -12,9 +12,6 @@ import numpy as np import torch -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.pt.model.network.mlp import ( FittingNet, NetworkCollection, @@ -46,6 +43,9 @@ to_numpy_array, to_torch_tensor, ) +from deepmd.utils.version import ( + check_version_compatibility, +) dtype = env.GLOBAL_PT_FLOAT_PRECISION device = env.DEVICE diff --git a/deepmd/tf/descriptor/se_a.py b/deepmd/tf/descriptor/se_a.py index beefa90d63..0e15ba13a8 100644 --- a/deepmd/tf/descriptor/se_a.py +++ b/deepmd/tf/descriptor/se_a.py @@ -10,9 +10,6 @@ from deepmd.dpmodel.utils.env_mat import ( EnvMat, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -68,6 +65,9 @@ from deepmd.tf.utils.type_embed import ( embed_atom_type, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .descriptor import ( Descriptor, diff --git a/deepmd/tf/descriptor/se_r.py b/deepmd/tf/descriptor/se_r.py index 5fe838b823..ba1a261390 100644 --- a/deepmd/tf/descriptor/se_r.py +++ b/deepmd/tf/descriptor/se_r.py @@ -10,9 +10,6 @@ from deepmd.dpmodel.utils.env_mat import ( EnvMat, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -41,6 +38,9 @@ from deepmd.tf.utils.tabulate import ( DPTabulate, ) +from deepmd.utils.version import ( + check_version_compatibility, +) from .descriptor import ( Descriptor, diff --git a/deepmd/tf/fit/dipole.py b/deepmd/tf/fit/dipole.py index 4ef8ccebb5..f503789308 100644 --- a/deepmd/tf/fit/dipole.py +++ b/deepmd/tf/fit/dipole.py @@ -6,9 +6,6 @@ import numpy as np -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -33,6 +30,9 @@ one_layer, one_layer_rand_seed_shift, ) +from deepmd.utils.version import ( + check_version_compatibility, +) @Fitting.register("dipole") diff --git a/deepmd/tf/fit/ener.py b/deepmd/tf/fit/ener.py index 12923e2512..106e10839d 100644 --- a/deepmd/tf/fit/ener.py +++ b/deepmd/tf/fit/ener.py @@ -8,9 +8,6 @@ import numpy as np -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.common import ( add_data_requirement, cast_precision, @@ -56,6 +53,9 @@ from deepmd.tf.utils.spin import ( Spin, ) +from deepmd.utils.version import ( + check_version_compatibility, +) if TYPE_CHECKING: pass diff --git a/deepmd/tf/fit/polar.py b/deepmd/tf/fit/polar.py index 8a3f5c94ca..002082ad2e 100644 --- a/deepmd/tf/fit/polar.py +++ b/deepmd/tf/fit/polar.py @@ -7,9 +7,6 @@ import numpy as np -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.common import ( cast_precision, get_activation_func, @@ -37,6 +34,9 @@ one_layer, one_layer_rand_seed_shift, ) +from deepmd.utils.version import ( + check_version_compatibility, +) @Fitting.register("polar") diff --git a/deepmd/tf/model/model.py b/deepmd/tf/model/model.py index 38728d8027..7630dae7dd 100644 --- a/deepmd/tf/model/model.py +++ b/deepmd/tf/model/model.py @@ -17,9 +17,6 @@ from deepmd.common import ( j_get_type, ) -from deepmd.dpmodel.utils.version import ( - check_version_compatibility, -) from deepmd.tf.descriptor.descriptor import ( Descriptor, ) @@ -63,6 +60,9 @@ from deepmd.tf.utils.type_embed import ( TypeEmbedNet, ) +from deepmd.utils.version import ( + check_version_compatibility, +) class Model(ABC): diff --git a/deepmd/utils/pair_tab.py b/deepmd/utils/pair_tab.py index ab1e805459..1b397a3cfa 100644 --- a/deepmd/utils/pair_tab.py +++ b/deepmd/utils/pair_tab.py @@ -12,7 +12,7 @@ CubicSpline, ) -from deepmd.dpmodel.utils.version import ( +from deepmd.utils.version import ( check_version_compatibility, ) diff --git a/deepmd/dpmodel/utils/version.py b/deepmd/utils/version.py similarity index 100% rename from deepmd/dpmodel/utils/version.py rename to deepmd/utils/version.py From 6655ac7eb2a3f0ce4413ef3f0e59917d5e365cd3 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 27 Feb 2024 19:59:47 -0500 Subject: [PATCH 4/5] to be compatible with old tests --- deepmd/dpmodel/utils/network.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/deepmd/dpmodel/utils/network.py b/deepmd/dpmodel/utils/network.py index e43ae829f3..feb3355e77 100644 --- a/deepmd/dpmodel/utils/network.py +++ b/deepmd/dpmodel/utils/network.py @@ -215,7 +215,7 @@ def deserialize(cls, data: dict) -> "NativeLayer": """ data = copy.deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) - data.pop("@class") + data.pop("@class", None) variables = data.pop("@variables") assert variables["w"] is not None and len(variables["w"].shape) == 2 num_in, num_out = variables["w"].shape @@ -374,7 +374,7 @@ def deserialize(cls, data: dict) -> "NN": """ data = data.copy() check_version_compatibility(data.pop("@version", 1), 1, 1) - data.pop("@class") + data.pop("@class", None) return cls(data["layers"]) def __getitem__(self, key): @@ -508,7 +508,7 @@ def deserialize(cls, data: dict) -> "EmbeddingNet": """ data = copy.deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) - data.pop("@class") + data.pop("@class", None) layers = data.pop("layers") obj = cls(**data) super(EN, obj).__init__(layers) @@ -608,7 +608,7 @@ def deserialize(cls, data: dict) -> "FittingNet": """ data = copy.deepcopy(data) check_version_compatibility(data.pop("@version", 1), 1, 1) - data.pop("@class") + data.pop("@class", None) layers = data.pop("layers") obj = cls(**data) T_Network.__init__(obj, layers) @@ -730,5 +730,5 @@ def deserialize(cls, data: dict) -> "NetworkCollection": """ data = data.copy() check_version_compatibility(data.pop("@version", 1), 1, 1) - data.pop("@class") + data.pop("@class", None) return cls(**data) From 47bfd4a361cc3e713883e65d1da7b584cbc14643 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 28 Feb 2024 01:38:41 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- deepmd/tf/model/model.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deepmd/tf/model/model.py b/deepmd/tf/model/model.py index f1e1a90fec..889f7ccc4d 100644 --- a/deepmd/tf/model/model.py +++ b/deepmd/tf/model/model.py @@ -60,12 +60,12 @@ from deepmd.tf.utils.type_embed import ( TypeEmbedNet, ) -from deepmd.utils.version import ( - check_version_compatibility, -) from deepmd.utils.plugin import ( make_plugin_registry, ) +from deepmd.utils.version import ( + check_version_compatibility, +) class Model(ABC, make_plugin_registry("model")):