-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support exclude atypes in atomic model (#3357)
With this pr, we do not need to implement the exclude types for each descriptor and fitting. --------- Co-authored-by: Han Wang <wang_han@iapcm.ac.cn>
- Loading branch information
1 parent
d09af56
commit 342b0c3
Showing
23 changed files
with
470 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,89 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
from typing import ( | ||
Dict, | ||
List, | ||
Optional, | ||
Tuple, | ||
) | ||
|
||
import numpy as np | ||
|
||
from deepmd.dpmodel.utils import ( | ||
AtomExcludeMask, | ||
PairExcludeMask, | ||
) | ||
|
||
from .make_base_atomic_model import ( | ||
make_base_atomic_model, | ||
) | ||
|
||
BaseAtomicModel = make_base_atomic_model(np.ndarray) | ||
BaseAtomicModel_ = make_base_atomic_model(np.ndarray) | ||
|
||
|
||
class BaseAtomicModel(BaseAtomicModel_): | ||
def __init__( | ||
self, | ||
atom_exclude_types: List[int] = [], | ||
pair_exclude_types: List[Tuple[int, int]] = [], | ||
): | ||
super().__init__() | ||
self.reinit_atom_exclude(atom_exclude_types) | ||
self.reinit_pair_exclude(pair_exclude_types) | ||
|
||
def reinit_atom_exclude( | ||
self, | ||
exclude_types: List[int] = [], | ||
): | ||
self.atom_exclude_types = exclude_types | ||
if exclude_types == []: | ||
self.atom_excl = None | ||
else: | ||
self.atom_excl = AtomExcludeMask(self.get_ntypes(), self.atom_exclude_types) | ||
|
||
def reinit_pair_exclude( | ||
self, | ||
exclude_types: List[Tuple[int, int]] = [], | ||
): | ||
self.pair_exclude_types = exclude_types | ||
if exclude_types == []: | ||
self.pair_excl = None | ||
else: | ||
self.pair_excl = PairExcludeMask(self.get_ntypes(), self.pair_exclude_types) | ||
|
||
def forward_common_atomic( | ||
self, | ||
extended_coord: np.ndarray, | ||
extended_atype: np.ndarray, | ||
nlist: np.ndarray, | ||
mapping: Optional[np.ndarray] = None, | ||
fparam: Optional[np.ndarray] = None, | ||
aparam: Optional[np.ndarray] = None, | ||
) -> Dict[str, np.ndarray]: | ||
_, nloc, _ = nlist.shape | ||
atype = extended_atype[:, :nloc] | ||
if self.pair_excl is not None: | ||
pair_mask = self.pair_excl.build_type_exclude_mask(nlist, extended_atype) | ||
# exclude neighbors in the nlist | ||
nlist = np.where(pair_mask == 1, nlist, -1) | ||
|
||
ret_dict = self.forward_atomic( | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping=mapping, | ||
fparam=fparam, | ||
aparam=aparam, | ||
) | ||
|
||
if self.atom_excl is not None: | ||
atom_mask = self.atom_excl.build_type_exclude_mask(atype) | ||
for kk in ret_dict.keys(): | ||
ret_dict[kk] = ret_dict[kk] * atom_mask[:, :, None] | ||
|
||
return ret_dict | ||
|
||
def serialize(self) -> dict: | ||
return { | ||
"atom_exclude_types": self.atom_exclude_types, | ||
"pair_exclude_types": self.pair_exclude_types, | ||
} |
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
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 |
---|---|---|
@@ -1,20 +1,99 @@ | ||
# SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
||
|
||
from typing import ( | ||
Dict, | ||
List, | ||
Optional, | ||
Tuple, | ||
) | ||
|
||
import torch | ||
|
||
from deepmd.dpmodel.atomic_model import ( | ||
make_base_atomic_model, | ||
) | ||
from deepmd.pt.utils import ( | ||
AtomExcludeMask, | ||
PairExcludeMask, | ||
) | ||
|
||
BaseAtomicModel_ = make_base_atomic_model(torch.Tensor) | ||
|
||
|
||
class BaseAtomicModel(BaseAtomicModel_): | ||
def __init__( | ||
self, | ||
atom_exclude_types: List[int] = [], | ||
pair_exclude_types: List[Tuple[int, int]] = [], | ||
): | ||
super().__init__() | ||
self.reinit_atom_exclude(atom_exclude_types) | ||
self.reinit_pair_exclude(pair_exclude_types) | ||
|
||
def reinit_atom_exclude( | ||
self, | ||
exclude_types: List[int] = [], | ||
): | ||
self.atom_exclude_types = exclude_types | ||
if exclude_types == []: | ||
self.atom_excl = None | ||
else: | ||
self.atom_excl = AtomExcludeMask(self.get_ntypes(), self.atom_exclude_types) | ||
|
||
def reinit_pair_exclude( | ||
self, | ||
exclude_types: List[Tuple[int, int]] = [], | ||
): | ||
self.pair_exclude_types = exclude_types | ||
if exclude_types == []: | ||
self.pair_excl = None | ||
else: | ||
self.pair_excl = PairExcludeMask(self.get_ntypes(), self.pair_exclude_types) | ||
|
||
# export public methods that are not abstract | ||
get_nsel = torch.jit.export(BaseAtomicModel_.get_nsel) | ||
get_nnei = torch.jit.export(BaseAtomicModel_.get_nnei) | ||
|
||
@torch.jit.export | ||
def get_model_def_script(self) -> str: | ||
return self.model_def_script | ||
|
||
def forward_common_atomic( | ||
self, | ||
extended_coord: torch.Tensor, | ||
extended_atype: torch.Tensor, | ||
nlist: torch.Tensor, | ||
mapping: Optional[torch.Tensor] = None, | ||
fparam: Optional[torch.Tensor] = None, | ||
aparam: Optional[torch.Tensor] = None, | ||
) -> Dict[str, torch.Tensor]: | ||
_, nloc, _ = nlist.shape | ||
atype = extended_atype[:, :nloc] | ||
|
||
if self.pair_excl is not None: | ||
pair_mask = self.pair_excl(nlist, extended_atype) | ||
# exclude neighbors in the nlist | ||
nlist = torch.where(pair_mask == 1, nlist, -1) | ||
|
||
ret_dict = self.forward_atomic( | ||
extended_coord, | ||
extended_atype, | ||
nlist, | ||
mapping=mapping, | ||
fparam=fparam, | ||
aparam=aparam, | ||
) | ||
|
||
if self.atom_excl is not None: | ||
atom_mask = self.atom_excl(atype) | ||
for kk in ret_dict.keys(): | ||
ret_dict[kk] = ret_dict[kk] * atom_mask[:, :, None] | ||
|
||
return ret_dict | ||
|
||
def serialize(self) -> dict: | ||
return { | ||
"atom_exclude_types": self.atom_exclude_types, | ||
"pair_exclude_types": self.pair_exclude_types, | ||
} |
Oops, something went wrong.