Skip to content

Commit

Permalink
feat: descriptor provide get_rcut_smth and get_env_protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Han Wang committed May 9, 2024
1 parent 6f06adb commit 804b331
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 3 deletions.
8 changes: 8 additions & 0 deletions deepmd/dpmodel/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ def get_rcut(self):
"""Returns cutoff radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_sel(self):
"""Returns cutoff radius."""
return self.sel
Expand All @@ -402,6 +406,10 @@ def mixed_types(self):
"""
return True

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
17 changes: 17 additions & 0 deletions deepmd/dpmodel/descriptor/hybrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import math
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -96,6 +97,11 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return np.max([descrpt.get_rcut() for descrpt in self.descrpt_list]).item()

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
# may not be a good idea...
return np.min([descrpt.get_rcut_smth() for descrpt in self.descrpt_list]).item()

def get_sel(self) -> List[int]:
"""Returns the number of selected atoms for each type."""
if self.mixed_types():
Expand Down Expand Up @@ -127,6 +133,17 @@ def mixed_types(self):
"""
return any(descrpt.mixed_types() for descrpt in self.descrpt_list)

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix. All descriptors should be the same."""
all_protection = [descrpt.get_env_protection() for descrpt in self.descrpt_list]
same_as_0 = [math.isclose(ii, all_protection[0]) for ii in all_protection]
if not all(same_as_0):
raise ValueError(
"Hybrid descriptor requires the environment matrix protection being the same ",
"for all the descriptors.",
)
return all_protection[0]

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
10 changes: 10 additions & 0 deletions deepmd/dpmodel/descriptor/make_base_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
pass

@abstractmethod
def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
pass

@abstractmethod
def get_sel(self) -> List[int]:
"""Returns the number of selected neighboring atoms for each type."""
Expand Down Expand Up @@ -86,6 +91,11 @@ def mixed_types(self) -> bool:
"""
pass

@abstractmethod
def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
pass

@abstractmethod
def share_params(self, base_class, shared_level, resume=False):
"""
Expand Down
8 changes: 8 additions & 0 deletions deepmd/dpmodel/descriptor/se_e2_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ def get_rcut(self):
"""Returns cutoff radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_sel(self):
"""Returns cutoff radius."""
return self.sel
Expand All @@ -249,6 +253,10 @@ def mixed_types(self):
"""
return False

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
8 changes: 8 additions & 0 deletions deepmd/dpmodel/descriptor/se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ def get_rcut(self):
"""Returns cutoff radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_sel(self):
"""Returns cutoff radius."""
return self.sel
Expand All @@ -205,6 +209,10 @@ def mixed_types(self):
"""
return False

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
10 changes: 10 additions & 0 deletions deepmd/pt/model/descriptor/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
pass

@abstractmethod
def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
pass

@abstractmethod
def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
Expand Down Expand Up @@ -88,6 +93,11 @@ def get_dim_emb(self) -> int:
"""Returns the embedding dimension."""
pass

@abstractmethod
def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
pass

def compute_input_stats(
self,
merged: Union[Callable[[], List[dict]], List[dict]],
Expand Down
8 changes: 8 additions & 0 deletions deepmd/pt/model/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.se_atten.get_rcut()

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.se_atten.get_rcut_smth()

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return self.se_atten.get_nsel()
Expand Down Expand Up @@ -315,6 +319,10 @@ def mixed_types(self) -> bool:
"""
return self.se_atten.mixed_types()

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.se_atten.get_env_protection()

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
10 changes: 10 additions & 0 deletions deepmd/pt/model/descriptor/dpa2.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ def __init__(
self.concat_output_tebd = concat_output_tebd
self.tebd_dim = tebd_dim
self.rcut = self.repinit.get_rcut()
self.rcut_smth = self.repinit.get_rcut_smth()
self.ntypes = ntypes
self.sel = self.repinit.sel
# set trainable
Expand All @@ -273,6 +274,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return sum(self.sel)
Expand Down Expand Up @@ -308,6 +313,11 @@ def mixed_types(self) -> bool:
"""
return True

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
# the env_protection of repinit is the same as that of the repformer
return self.repinit.get_env_protection()

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
17 changes: 17 additions & 0 deletions deepmd/pt/model/descriptor/hybrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import math
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -101,6 +102,11 @@ def get_rcut(self) -> float:
# do not use numpy here - jit is not happy
return max([descrpt.get_rcut() for descrpt in self.descrpt_list])

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
# may not be a good idea...
return min([descrpt.get_rcut_smth() for descrpt in self.descrpt_list])

def get_sel(self) -> List[int]:
"""Returns the number of selected atoms for each type."""
if self.mixed_types():
Expand Down Expand Up @@ -132,6 +138,17 @@ def mixed_types(self):
"""
return any(descrpt.mixed_types() for descrpt in self.descrpt_list)

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix. All descriptors should be the same."""
all_protection = [descrpt.get_env_protection() for descrpt in self.descrpt_list]
same_as_0 = [math.isclose(ii, all_protection[0]) for ii in all_protection]
if not all(same_as_0):
raise ValueError(
"Hybrid descriptor requires the environment matrix protection being the same ",
"for all the descriptors.",
)
return all_protection[0]

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
8 changes: 8 additions & 0 deletions deepmd/pt/model/descriptor/repformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return sum(self.sel)
Expand Down Expand Up @@ -226,6 +230,10 @@ def mixed_types(self) -> bool:
"""
return True

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

@property
def dim_out(self):
"""Returns the output dimension of this descriptor."""
Expand Down
16 changes: 16 additions & 0 deletions deepmd/pt/model/descriptor/se_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.sea.get_rcut()

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.sea.get_rcut_smth()

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return self.sea.get_nsel()
Expand All @@ -132,6 +136,10 @@ def mixed_types(self):
"""
return self.sea.mixed_types()

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.sea.get_env_protection()

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down Expand Up @@ -400,6 +408,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return sum(self.sel)
Expand Down Expand Up @@ -436,6 +448,10 @@ def mixed_types(self) -> bool:
"""
return False

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

@property
def dim_out(self):
"""Returns the output dimension of this descriptor."""
Expand Down
8 changes: 8 additions & 0 deletions deepmd/pt/model/descriptor/se_atten.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return sum(self.sel)
Expand Down Expand Up @@ -338,6 +342,10 @@ def mixed_types(self) -> bool:
"""
return True

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

@property
def dim_out(self):
"""Returns the output dimension of this descriptor."""
Expand Down
8 changes: 8 additions & 0 deletions deepmd/pt/model/descriptor/se_r.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def get_rcut(self) -> float:
"""Returns the cut-off radius."""
return self.rcut

def get_rcut_smth(self) -> float:
"""Returns the radius where the neighbor information starts to smoothly decay to 0."""
return self.rcut_smth

def get_nsel(self) -> int:
"""Returns the number of selected atoms in the cut-off radius."""
return sum(self.sel)
Expand Down Expand Up @@ -160,6 +164,10 @@ def mixed_types(self) -> bool:
"""
return False

def get_env_protection(self) -> float:
"""Returns the protection of building environment matrix."""
return self.env_protection

def share_params(self, base_class, shared_level, resume=False):
"""
Share the parameters of self to the base_class with shared_level during multitask training.
Expand Down
4 changes: 2 additions & 2 deletions deepmd/pt/utils/env_mat_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def iter(
one_stddev,
self.descriptor.get_rcut(),
# TODO: export rcut_smth from DescriptorBlock
self.descriptor.rcut_smth,
self.descriptor.get_rcut_smth(),
radial_only,
protection=self.descriptor.env_protection,
protection=self.descriptor.get_env_protection(),
)
# apply excluded_types
exclude_mask = self.descriptor.emask(nlist, extended_atype)
Expand Down
Loading

0 comments on commit 804b331

Please sign in to comment.