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 4 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",
]
24 changes: 24 additions & 0 deletions deepmd/dpmodel/atomic_model/dipole_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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):
assert isinstance(fitting, DipoleFitting)
super().__init__(descriptor, fitting, type_map, **kwargs)

def apply_out_stat(
self,
ret: dict[str, np.ndarray],
atype: np.ndarray,
):
# dipole not applying bias
return ret
14 changes: 14 additions & 0 deletions deepmd/dpmodel/atomic_model/dos_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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):
assert isinstance(fitting, DOSFittingNet)
super().__init__(descriptor, fitting, type_map, **kwargs)
17 changes: 17 additions & 0 deletions deepmd/dpmodel/atomic_model/energy_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.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):
assert isinstance(fitting, EnergyFittingNet) or isinstance(
fitting, InvarFitting
)
super().__init__(descriptor, fitting, type_map, **kwargs)
58 changes: 58 additions & 0 deletions deepmd/dpmodel/atomic_model/polar_atomic_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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):
assert isinstance(fitting, PolarFitting)
super().__init__(descriptor, fitting, type_map, **kwargs)
anyangml marked this conversation as resolved.
Show resolved Hide resolved

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)

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
for i in range(ntypes):
temp[i] = np.mean(np.diagonal(out_bias[kk][i].reshape(3, 3)))
anyangml marked this conversation as resolved.
Show resolved Hide resolved
anyangml marked this conversation as resolved.
Show resolved Hide resolved
modified_bias = temp[atype]

# (nframes, nloc, 1)
modified_bias = (
modified_bias[..., np.newaxis] * (self.fitting_net.scale[atype])
)

eye = np.eye(3, dtype=dtype)
eye = np.tile(eye, (nframes, nloc, 1, 1))
# (nframes, nloc, 3, 3)
modified_bias = modified_bias[..., np.newaxis] * eye

# nf x nloc x odims, out_bias: ntypes x odims
ret[kk] = ret[kk] + modified_bias
return ret
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.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)
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.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
30 changes: 30 additions & 0 deletions deepmd/dpmodel/model/polar_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 (
DPPolarAtomicModel,
)
from deepmd.dpmodel.model.model import (
BaseModel,
)

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

DPPolarModel_ = make_model(DPPolarAtomicModel)


@BaseModel.register("polar")
class PolarModel(DPModelCommon, DPPolarModel_):
model_type = "polar"
anyangml marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
*args,
**kwargs,
):
DPModelCommon.__init__(self)
DPPolarModel_.__init__(self, *args, **kwargs)
6 changes: 3 additions & 3 deletions deepmd/pt/model/model/dipole_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
make_model,
)

DPDOSModel_ = make_model(DPDipoleAtomicModel)
DPDipoleModel_ = make_model(DPDipoleAtomicModel)


@BaseModel.register("dipole")
class DipoleModel(DPModelCommon, DPDOSModel_):
class DipoleModel(DPModelCommon, DPDipoleModel_):
model_type = "dipole"

def __init__(
Expand All @@ -35,7 +35,7 @@ def __init__(
**kwargs,
):
DPModelCommon.__init__(self)
DPDOSModel_.__init__(self, *args, **kwargs)
DPDipoleModel_.__init__(self, *args, **kwargs)

def translated_output_def(self):
out_def_data = self.model_output_def().get_data()
Expand Down
6 changes: 3 additions & 3 deletions deepmd/pt/model/model/polar_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
make_model,
)

DPDOSModel_ = make_model(DPPolarAtomicModel)
DPPolarModel_ = make_model(DPPolarAtomicModel)


@BaseModel.register("polar")
class PolarModel(DPModelCommon, DPDOSModel_):
class PolarModel(DPModelCommon, DPPolarModel_):
model_type = "polar"

def __init__(
Expand All @@ -35,7 +35,7 @@ def __init__(
**kwargs,
):
DPModelCommon.__init__(self)
DPDOSModel_.__init__(self, *args, **kwargs)
DPPolarModel_.__init__(self, *args, **kwargs)

def translated_output_def(self):
out_def_data = self.model_output_def().get_data()
Expand Down
Loading