-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR provides DOS fitting net in Pytorch. Future TODO: - [ ] Loss implementation - [ ] Training/Fine-tuning test - [ ] Jit test - [ ] Doc --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: anyangml <[email protected]>
- Loading branch information
1 parent
d61b152
commit 2caf92c
Showing
27 changed files
with
1,019 additions
and
196 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,93 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
import copy | ||
from typing import ( | ||
TYPE_CHECKING, | ||
List, | ||
Optional, | ||
Union, | ||
) | ||
|
||
import numpy as np | ||
|
||
from deepmd.dpmodel.common import ( | ||
DEFAULT_PRECISION, | ||
) | ||
from deepmd.dpmodel.fitting.invar_fitting import ( | ||
InvarFitting, | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from deepmd.dpmodel.fitting.general_fitting import ( | ||
GeneralFitting, | ||
) | ||
|
||
from deepmd.utils.version import ( | ||
check_version_compatibility, | ||
) | ||
|
||
|
||
@InvarFitting.register("dos") | ||
class DOSFittingNet(InvarFitting): | ||
def __init__( | ||
self, | ||
ntypes: int, | ||
dim_descrpt: int, | ||
numb_dos: int = 300, | ||
neuron: List[int] = [120, 120, 120], | ||
resnet_dt: bool = True, | ||
numb_fparam: int = 0, | ||
numb_aparam: int = 0, | ||
bias_dos: Optional[np.ndarray] = None, | ||
rcond: Optional[float] = None, | ||
trainable: Union[bool, List[bool]] = True, | ||
activation_function: str = "tanh", | ||
precision: str = DEFAULT_PRECISION, | ||
mixed_types: bool = False, | ||
exclude_types: List[int] = [], | ||
# not used | ||
seed: Optional[int] = None, | ||
): | ||
if bias_dos is not None: | ||
self.bias_dos = bias_dos | ||
else: | ||
self.bias_dos = np.zeros((ntypes, numb_dos), dtype=DEFAULT_PRECISION) | ||
super().__init__( | ||
var_name="dos", | ||
ntypes=ntypes, | ||
dim_descrpt=dim_descrpt, | ||
dim_out=numb_dos, | ||
neuron=neuron, | ||
resnet_dt=resnet_dt, | ||
bias_atom=bias_dos, | ||
numb_fparam=numb_fparam, | ||
numb_aparam=numb_aparam, | ||
rcond=rcond, | ||
trainable=trainable, | ||
activation_function=activation_function, | ||
precision=precision, | ||
mixed_types=mixed_types, | ||
exclude_types=exclude_types, | ||
) | ||
|
||
@classmethod | ||
def deserialize(cls, data: dict) -> "GeneralFitting": | ||
data = copy.deepcopy(data) | ||
check_version_compatibility(data.pop("@version", 1), 1, 1) | ||
data["numb_dos"] = data.pop("dim_out") | ||
data.pop("tot_ener_zero", None) | ||
data.pop("var_name", None) | ||
data.pop("layer_name", None) | ||
data.pop("use_aparam_as_mask", None) | ||
data.pop("spin", None) | ||
data.pop("atom_ener", None) | ||
return super().deserialize(data) | ||
|
||
def serialize(self) -> dict: | ||
"""Serialize the fitting to dict.""" | ||
dd = { | ||
**super().serialize(), | ||
"type": "dos", | ||
} | ||
dd["@variables"]["bias_atom_e"] = self.bias_atom_e | ||
|
||
return dd |
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
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
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,80 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Dict, | ||
Optional, | ||
) | ||
|
||
import torch | ||
|
||
from .dp_model import ( | ||
DPModel, | ||
) | ||
|
||
|
||
class DOSModel(DPModel): | ||
model_type = "dos" | ||
|
||
def __init__( | ||
self, | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__(*args, **kwargs) | ||
|
||
def forward( | ||
self, | ||
coord, | ||
atype, | ||
box: Optional[torch.Tensor] = None, | ||
fparam: Optional[torch.Tensor] = None, | ||
aparam: Optional[torch.Tensor] = None, | ||
do_atomic_virial: bool = False, | ||
) -> Dict[str, torch.Tensor]: | ||
model_ret = self.forward_common( | ||
coord, | ||
atype, | ||
box, | ||
fparam=fparam, | ||
aparam=aparam, | ||
do_atomic_virial=do_atomic_virial, | ||
) | ||
if self.get_fitting_net() is not None: | ||
model_predict = {} | ||
model_predict["atom_dos"] = model_ret["dos"] | ||
model_predict["dos"] = model_ret["dos_redu"] | ||
|
||
if "mask" in model_ret: | ||
model_predict["mask"] = model_ret["mask"] | ||
else: | ||
model_predict = model_ret | ||
model_predict["updated_coord"] += coord | ||
return model_predict | ||
|
||
@torch.jit.export | ||
def forward_lower( | ||
self, | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping: Optional[torch.Tensor] = None, | ||
fparam: Optional[torch.Tensor] = None, | ||
aparam: Optional[torch.Tensor] = None, | ||
do_atomic_virial: bool = False, | ||
): | ||
model_ret = self.forward_common_lower( | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping, | ||
fparam=fparam, | ||
aparam=aparam, | ||
do_atomic_virial=do_atomic_virial, | ||
) | ||
if self.get_fitting_net() is not None: | ||
model_predict = {} | ||
model_predict["atom_dos"] = model_ret["dos"] | ||
model_predict["dos"] = model_ret["dos_redu"] | ||
|
||
else: | ||
model_predict = model_ret | ||
return model_predict |
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.