-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: refactor dpmodel model (#4296)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced several new atomic models: DPDipoleAtomicModel, DPDOSAtomicModel, DPEnergyAtomicModel, and DPPolarAtomicModel. - Enhanced the public API to include these new models for easier access. - Added new model classes: DipoleModel, PolarModel, and DOSModel, extending the functionality of the framework. - Implemented unit tests for Density of States (DOS) models, ensuring functionality across different computational backends. - **Bug Fixes** - Improved error handling in atomic model constructors to provide clearer messages for type requirements. - **Documentation** - Added a license identifier to the dos_model.py file. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Anyang Peng <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
447ea94
commit a4e30cc
Showing
20 changed files
with
561 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
"fitting must be an instance of DipoleFitting for DPDipoleAtomicModel" | ||
) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
"fitting must be an instance of DOSFittingNet for DPDOSAtomicModel" | ||
) | ||
super().__init__(descriptor, fitting, type_map, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
|
||
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( | ||
"fitting must be an instance of EnergyFittingNet or InvarFitting for DPEnergyAtomicModel" | ||
) | ||
super().__init__(descriptor, fitting, type_map, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
"fitting must be an instance of PolarFitting for DPPolarAtomicModel" | ||
) | ||
super().__init__(descriptor, fitting, type_map, **kwargs) | ||
|
||
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) | ||
temp = np.mean( | ||
np.diagonal(out_bias[kk].reshape(ntypes, 3, 3), axis1=1, axis2=2), | ||
axis=1, | ||
) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
**kwargs, | ||
): | ||
DPModelCommon.__init__(self) | ||
DPDipoleModel_.__init__(self, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.