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

Chore: refactor dpmodel model #4296

Merged
merged 21 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
88f5079
chore: refactor dpmodel atomic model
anyangml Nov 1, 2024
de96159
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
ab3bd29
feat: expose dos/polar/dipole model in dpmodel
anyangml Nov 1, 2024
8e85e59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
c5dab77
chore: refactor atomic model fitting assertion
anyangml Nov 1, 2024
58b987e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 1, 2024
a285d63
Merge branch 'devel' into chore/refactor-dpmodel-model
anyangml Nov 15, 2024
e2e9d60
chore: fix typo
anyangml Nov 15, 2024
47114ec
feat: try add dos consistent UT
anyangml Nov 15, 2024
48d8d6c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
81bd024
fix: UT para
anyangml Nov 15, 2024
7490529
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
c040df3
fix: UT res extract
anyangml Nov 15, 2024
8940dac
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2024
4802a9a
fix: add dos consistency UT
anyangml Nov 18, 2024
3355f50
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
5a11ff8
fix: expose more dpmodel interface
anyangml Nov 18, 2024
9071e73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 18, 2024
1dc7fd9
chore: vectorize for loop
anyangml Nov 19, 2024
0afee21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2024
33452bd
Merge branch 'devel' into chore/refactor-dpmodel-model
anyangml Nov 19, 2024
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
16 changes: 16 additions & 0 deletions deepmd/dpmodel/atomic_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
from .base_atomic_model import (
BaseAtomicModel,
)
from .dipole_atomic_model import (
DPDipoleAtomicModel,
)
from .dos_atomic_model import (
DPDOSAtomicModel,
)
from .dp_atomic_model import (
DPAtomicModel,
)
from .energy_atomic_model import (
DPEnergyAtomicModel,
)
from .linear_atomic_model import (
DPZBLLinearEnergyAtomicModel,
LinearEnergyAtomicModel,
Expand All @@ -30,12 +39,19 @@
from .pairtab_atomic_model import (
PairTabAtomicModel,
)
from .polar_atomic_model import (
DPPolarAtomicModel,
)

__all__ = [
"make_base_atomic_model",
"BaseAtomicModel",
"DPAtomicModel",
"DPEnergyAtomicModel",
"PairTabAtomicModel",
"LinearEnergyAtomicModel",
"DPZBLLinearEnergyAtomicModel",
"DPDOSAtomicModel",
"DPPolarAtomicModel",
"DPDipoleAtomicModel",
]
27 changes: 27 additions & 0 deletions deepmd/dpmodel/atomic_model/dipole_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import numpy as np

from deepmd.dpmodel.fitting.dipole_fitting import (
DipoleFitting,
)

from .dp_atomic_model import (
DPAtomicModel,
)


class DPDipoleAtomicModel(DPAtomicModel):
def __init__(self, descriptor, fitting, type_map, **kwargs):
if not isinstance(fitting, DipoleFitting):
raise TypeError(

Check warning on line 16 in deepmd/dpmodel/atomic_model/dipole_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/dipole_atomic_model.py#L15-L16

Added lines #L15 - L16 were not covered by tests
"fitting must be an instance of DipoleFitting for DPDipoleAtomicModel"
)
super().__init__(descriptor, fitting, type_map, **kwargs)

Check warning on line 19 in deepmd/dpmodel/atomic_model/dipole_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/dipole_atomic_model.py#L19

Added line #L19 was not covered by tests

def apply_out_stat(
self,
ret: dict[str, np.ndarray],
atype: np.ndarray,
):
# dipole not applying bias
return ret

Check warning on line 27 in deepmd/dpmodel/atomic_model/dipole_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/dipole_atomic_model.py#L27

Added line #L27 was not covered by tests
17 changes: 17 additions & 0 deletions deepmd/dpmodel/atomic_model/dos_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from deepmd.dpmodel.fitting.dos_fitting import (
DOSFittingNet,
)

from .dp_atomic_model import (
DPAtomicModel,
)


class DPDOSAtomicModel(DPAtomicModel):
def __init__(self, descriptor, fitting, type_map, **kwargs):
if not isinstance(fitting, DOSFittingNet):
raise TypeError(

Check warning on line 14 in deepmd/dpmodel/atomic_model/dos_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/dos_atomic_model.py#L14

Added line #L14 was not covered by tests
"fitting must be an instance of DOSFittingNet for DPDOSAtomicModel"
)
super().__init__(descriptor, fitting, type_map, **kwargs)
20 changes: 20 additions & 0 deletions deepmd/dpmodel/atomic_model/energy_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from deepmd.dpmodel.fitting.ener_fitting import (
EnergyFittingNet,
InvarFitting,
)
anyangml marked this conversation as resolved.
Show resolved Hide resolved

from .dp_atomic_model import (
DPAtomicModel,
)


class DPEnergyAtomicModel(DPAtomicModel):
def __init__(self, descriptor, fitting, type_map, **kwargs):
if not (
isinstance(fitting, EnergyFittingNet) or isinstance(fitting, InvarFitting)
):
raise TypeError(

Check warning on line 17 in deepmd/dpmodel/atomic_model/energy_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/energy_atomic_model.py#L17

Added line #L17 was not covered by tests
"fitting must be an instance of EnergyFittingNet or InvarFitting for DPEnergyAtomicModel"
)
super().__init__(descriptor, fitting, type_map, **kwargs)
63 changes: 63 additions & 0 deletions deepmd/dpmodel/atomic_model/polar_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# SPDX-License-Identifier: LGPL-3.0-or-later

import numpy as np

from deepmd.dpmodel.fitting.polarizability_fitting import (
PolarFitting,
)

from .dp_atomic_model import (
DPAtomicModel,
)


class DPPolarAtomicModel(DPAtomicModel):
def __init__(self, descriptor, fitting, type_map, **kwargs):
if not isinstance(fitting, PolarFitting):
raise TypeError(

Check warning on line 17 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L16-L17

Added lines #L16 - L17 were not covered by tests
"fitting must be an instance of PolarFitting for DPPolarAtomicModel"
)
super().__init__(descriptor, fitting, type_map, **kwargs)

Check warning on line 20 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L20

Added line #L20 was not covered by tests

def apply_out_stat(
self,
ret: dict[str, np.ndarray],
atype: np.ndarray,
):
"""Apply the stat to each atomic output.

Parameters
----------
ret
The returned dict by the forward_atomic method
atype
The atom types. nf x nloc

"""
out_bias, out_std = self._fetch_out_stat(self.bias_keys)

Check warning on line 37 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L37

Added line #L37 was not covered by tests

if self.fitting_net.shift_diag:
nframes, nloc = atype.shape
dtype = out_bias[self.bias_keys[0]].dtype
for kk in self.bias_keys:
ntypes = out_bias[kk].shape[0]
temp = np.zeros(ntypes, dtype=dtype)
Dismissed Show dismissed Hide dismissed
temp = np.mean(

Check warning on line 45 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L39-L45

Added lines #L39 - L45 were not covered by tests
np.diagonal(out_bias[kk].reshape(ntypes, 3, 3), axis1=1, axis2=2),
axis=1,
)
modified_bias = temp[atype]

Check warning on line 49 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L49

Added line #L49 was not covered by tests

# (nframes, nloc, 1)
modified_bias = (

Check warning on line 52 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L52

Added line #L52 was not covered by tests
modified_bias[..., np.newaxis] * (self.fitting_net.scale[atype])
)

eye = np.eye(3, dtype=dtype)
eye = np.tile(eye, (nframes, nloc, 1, 1))

Check warning on line 57 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L56-L57

Added lines #L56 - L57 were not covered by tests
# (nframes, nloc, 3, 3)
modified_bias = modified_bias[..., np.newaxis] * eye

Check warning on line 59 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L59

Added line #L59 was not covered by tests

# nf x nloc x odims, out_bias: ntypes x odims
ret[kk] = ret[kk] + modified_bias
return ret

Check warning on line 63 in deepmd/dpmodel/atomic_model/polar_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/polar_atomic_model.py#L62-L63

Added lines #L62 - L63 were not covered by tests
7 changes: 5 additions & 2 deletions deepmd/dpmodel/atomic_model/property_atomic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class DPPropertyAtomicModel(DPAtomicModel):
def __init__(self, descriptor, fitting, type_map, **kwargs) -> None:
assert isinstance(fitting, PropertyFittingNet)
def __init__(self, descriptor, fitting, type_map, **kwargs):
if not isinstance(fitting, PropertyFittingNet):
raise TypeError(

Check warning on line 14 in deepmd/dpmodel/atomic_model/property_atomic_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/atomic_model/property_atomic_model.py#L12-L14

Added lines #L12 - L14 were not covered by tests
"fitting must be an instance of PropertyFittingNet for DPPropertyAtomicModel"
)
super().__init__(descriptor, fitting, type_map, **kwargs)
31 changes: 31 additions & 0 deletions deepmd/dpmodel/model/dipole_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SPDX-License-Identifier: LGPL-3.0-or-later


from deepmd.dpmodel.atomic_model import (
DPDipoleAtomicModel,
)
from deepmd.dpmodel.model.base_model import (
BaseModel,
)

from .dp_model import (
DPModelCommon,
)
from .make_model import (
make_model,
)

DPDipoleModel_ = make_model(DPDipoleAtomicModel)


@BaseModel.register("dipole")
class DipoleModel(DPModelCommon, DPDipoleModel_):
model_type = "dipole"
anyangml marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
*args,
**kwargs,
):
DPModelCommon.__init__(self)
DPDipoleModel_.__init__(self, *args, **kwargs)

Check warning on line 31 in deepmd/dpmodel/model/dipole_model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/dipole_model.py#L30-L31

Added lines #L30 - L31 were not covered by tests
30 changes: 30 additions & 0 deletions deepmd/dpmodel/model/dos_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: LGPL-3.0-or-later

from deepmd.dpmodel.atomic_model import (
DPDOSAtomicModel,
)
from deepmd.dpmodel.model.base_model import (
BaseModel,
)

from .dp_model import (
DPModelCommon,
)
from .make_model import (
make_model,
)

DPDOSModel_ = make_model(DPDOSAtomicModel)


@BaseModel.register("dos")
class DOSModel(DPModelCommon, DPDOSModel_):
model_type = "dos"

def __init__(
self,
*args,
**kwargs,
):
DPModelCommon.__init__(self)
DPDOSModel_.__init__(self, *args, **kwargs)
anyangml marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 3 additions & 3 deletions deepmd/dpmodel/model/ener_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from deepmd.dpmodel.atomic_model.dp_atomic_model import (
DPAtomicModel,
from deepmd.dpmodel.atomic_model import (
DPEnergyAtomicModel,
)
from deepmd.dpmodel.model.base_model import (
BaseModel,
Expand All @@ -13,7 +13,7 @@
make_model,
)

DPEnergyModel_ = make_model(DPAtomicModel)
DPEnergyModel_ = make_model(DPEnergyAtomicModel)


@BaseModel.register("ener")
Expand Down
80 changes: 62 additions & 18 deletions deepmd/dpmodel/model/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import copy

from deepmd.dpmodel.atomic_model.dp_atomic_model import (
DPAtomicModel,
)
Expand All @@ -8,18 +10,33 @@
from deepmd.dpmodel.descriptor.base_descriptor import (
BaseDescriptor,
)
from deepmd.dpmodel.fitting.base_fitting import (
BaseFitting,
)
from deepmd.dpmodel.fitting.ener_fitting import (
EnergyFittingNet,
)
from deepmd.dpmodel.model.base_model import (
BaseModel,
)
from deepmd.dpmodel.model.dipole_model import (
DipoleModel,
)
from deepmd.dpmodel.model.dos_model import (
DOSModel,
)
from deepmd.dpmodel.model.dp_zbl_model import (
DPZBLModel,
)
from deepmd.dpmodel.model.ener_model import (
EnergyModel,
)
from deepmd.dpmodel.model.polar_model import (
PolarModel,
)
from deepmd.dpmodel.model.property_model import (
PropertyModel,
)
from deepmd.dpmodel.model.spin_model import (
SpinModel,
)
Expand All @@ -28,6 +45,29 @@
)


def _get_standard_model_components(data, ntypes):
# descriptor
data["descriptor"]["ntypes"] = ntypes
data["descriptor"]["type_map"] = copy.deepcopy(data["type_map"])
descriptor = BaseDescriptor(**data["descriptor"])
# fitting
fitting_net = data.get("fitting_net", {})
fitting_net["type"] = fitting_net.get("type", "ener")
fitting_net["ntypes"] = descriptor.get_ntypes()
fitting_net["type_map"] = copy.deepcopy(data["type_map"])
fitting_net["mixed_types"] = descriptor.mixed_types()
if fitting_net["type"] in ["dipole", "polar"]:
fitting_net["embedding_width"] = descriptor.get_dim_emb()

Check warning on line 60 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L60

Added line #L60 was not covered by tests
fitting_net["dim_descrpt"] = descriptor.get_dim_out()
grad_force = "direct" not in fitting_net["type"]
if not grad_force:
fitting_net["out_dim"] = descriptor.get_dim_emb()
if "ener" in fitting_net["type"]:
fitting_net["return_energy"] = True

Check warning on line 66 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L64-L66

Added lines #L64 - L66 were not covered by tests
fitting = BaseFitting(**fitting_net)
return descriptor, fitting, fitting_net["type"]
anyangml marked this conversation as resolved.
Show resolved Hide resolved


def get_standard_model(data: dict) -> EnergyModel:
"""Get a EnergyModel from a dictionary.

Expand All @@ -40,29 +80,33 @@
raise ValueError(
"In the DP backend, type_embedding is not at the model level, but within the descriptor. See type embedding documentation for details."
)
data["descriptor"]["type_map"] = data["type_map"]
data["descriptor"]["ntypes"] = len(data["type_map"])
fitting_type = data["fitting_net"].pop("type")
data["fitting_net"]["type_map"] = data["type_map"]
descriptor = BaseDescriptor(
**data["descriptor"],
)
if fitting_type == "ener":
fitting = EnergyFittingNet(
ntypes=descriptor.get_ntypes(),
dim_descrpt=descriptor.get_dim_out(),
mixed_types=descriptor.mixed_types(),
**data["fitting_net"],
)
data = copy.deepcopy(data)
ntypes = len(data["type_map"])
descriptor, fitting, fitting_net_type = _get_standard_model_components(data, ntypes)
atom_exclude_types = data.get("atom_exclude_types", [])
pair_exclude_types = data.get("pair_exclude_types", [])

if fitting_net_type == "dipole":
modelcls = DipoleModel

Check warning on line 90 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L90

Added line #L90 was not covered by tests
elif fitting_net_type == "polar":
modelcls = PolarModel

Check warning on line 92 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L92

Added line #L92 was not covered by tests
elif fitting_net_type == "dos":
modelcls = DOSModel
elif fitting_net_type in ["ener", "direct_force_ener"]:
modelcls = EnergyModel
elif fitting_net_type == "property":
modelcls = PropertyModel

Check warning on line 98 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L97-L98

Added lines #L97 - L98 were not covered by tests
else:
raise ValueError(f"Unknown fitting type {fitting_type}")
return EnergyModel(
raise RuntimeError(f"Unknown fitting type: {fitting_net_type}")

Check warning on line 100 in deepmd/dpmodel/model/model.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/model/model.py#L100

Added line #L100 was not covered by tests

model = modelcls(
descriptor=descriptor,
fitting=fitting,
type_map=data["type_map"],
atom_exclude_types=data.get("atom_exclude_types", []),
pair_exclude_types=data.get("pair_exclude_types", []),
atom_exclude_types=atom_exclude_types,
pair_exclude_types=pair_exclude_types,
)
return model


def get_zbl_model(data: dict) -> DPZBLModel:
Expand Down
Loading