Skip to content

Commit

Permalink
Merge branch 'devel' into train_rf
Browse files Browse the repository at this point in the history
  • Loading branch information
iProzd authored Feb 28, 2024
2 parents 3812866 + fd17e2e commit 08e18fe
Show file tree
Hide file tree
Showing 23 changed files with 229 additions and 4 deletions.
5 changes: 5 additions & 0 deletions deepmd/dpmodel/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from deepmd.dpmodel.output_def import (
FittingOutputDef,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_atomic_model import (
BaseAtomicModel,
Expand Down Expand Up @@ -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(),
Expand All @@ -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"])
Expand Down
7 changes: 7 additions & 0 deletions deepmd/dpmodel/atomic_model/linear_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
get_multiple_nlist_key,
nlist_distinguish_types,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from ..output_def import (
FittingOutputDef,
Expand Down Expand Up @@ -185,13 +188,15 @@ 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],
}

@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"]
Expand Down Expand Up @@ -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,
Expand All @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions deepmd/dpmodel/atomic_model/pairtab_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
from deepmd.utils.pair_tab import (
PairTab,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_atomic_model import (
BaseAtomicModel,
Expand Down Expand Up @@ -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,
Expand All @@ -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"]
Expand Down
5 changes: 5 additions & 0 deletions deepmd/dpmodel/descriptor/se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from deepmd.utils.path import (
DPPath,
)
from deepmd.utils.version import (
check_version_compatibility,
)

try:
from deepmd._version import version as __version__
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions deepmd/dpmodel/descriptor/se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from deepmd.utils.path import (
DPPath,
)
from deepmd.utils.version import (
check_version_compatibility,
)

try:
from deepmd._version import version as __version__
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions deepmd/dpmodel/fitting/general_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
FittingNet,
NetworkCollection,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_fitting import (
BaseFitting,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
30 changes: 29 additions & 1 deletion deepmd/dpmodel/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import h5py
import numpy as np

from deepmd.utils.version import (
check_version_compatibility,
)

try:
from deepmd._version import version as __version__
except ImportError:
Expand Down Expand Up @@ -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,
Expand All @@ -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", None)
variables = data.pop("@variables")
assert variables["w"] is not None and len(variables["w"].shape) == 2
num_in, num_out = variables["w"].shape
Expand Down Expand Up @@ -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":
Expand All @@ -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", None)
return cls(data["layers"])

def __getitem__(self, key):
Expand Down Expand Up @@ -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,
Expand All @@ -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", None)
layers = data.pop("layers")
obj = cls(**data)
super(EN, obj).__init__(layers)
Expand Down Expand Up @@ -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(),
Expand All @@ -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", None)
layers = data.pop("layers")
obj = cls(**data)
T_Network.__init__(obj, layers)
Expand Down Expand Up @@ -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,
Expand All @@ -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", None)
return cls(**data)
5 changes: 5 additions & 0 deletions deepmd/pt/model/atomic_model/dp_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
from deepmd.utils.path import (
DPPath,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_atomic_model import (
BaseAtomicModel,
Expand Down Expand Up @@ -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(),
Expand All @@ -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"])
Expand Down
10 changes: 10 additions & 0 deletions deepmd/pt/model/atomic_model/linear_atomic_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import copy
import sys
from abc import (
abstractmethod,
Expand All @@ -24,6 +25,9 @@
get_multiple_nlist_key,
nlist_distinguish_types,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_atomic_model import (
BaseAtomicModel,
Expand Down Expand Up @@ -206,13 +210,16 @@ 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],
}

@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)
Expand Down Expand Up @@ -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,
Expand All @@ -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"]
Expand Down
7 changes: 7 additions & 0 deletions deepmd/pt/model/atomic_model/pairtab_atomic_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import copy
from typing import (
Dict,
List,
Expand All @@ -15,6 +16,9 @@
from deepmd.utils.pair_tab import (
PairTab,
)
from deepmd.utils.version import (
check_version_compatibility,
)

from .base_atomic_model import (
BaseAtomicModel,
Expand Down Expand Up @@ -124,13 +128,16 @@ def serialize(self) -> dict:
return {
"@class": "Model",
"type": "pairtab",
"@version": 1,
"tab": self.tab.serialize(),
"rcut": self.rcut,
"sel": self.sel,
}

@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"])
Expand Down
Loading

0 comments on commit 08e18fe

Please sign in to comment.