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

feat: descriptor provide get_rcut_smth and get_env_protection #3761

Merged
merged 10 commits into from
May 11, 2024
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,12 @@ 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...
# Note: Using the minimum rcut_smth might not be appropriate in all scenarios. Consider using a different approach or provide detailed documentation on why the minimum value is chosen.
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 +134,16 @@ 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 same environment matrix protection for all descriptors. Found differing values."
)
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 @@
"""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

Check warning on line 57 in deepmd/dpmodel/descriptor/make_base_descriptor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/make_base_descriptor.py#L57

Added line #L57 was not covered by tests

@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 @@
"""
pass

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

Check warning on line 97 in deepmd/dpmodel/descriptor/make_base_descriptor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/make_base_descriptor.py#L97

Added line #L97 was not covered by tests

@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 @@
"""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

Check warning on line 64 in deepmd/pt/model/descriptor/descriptor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/descriptor.py#L64

Added line #L64 was not covered by tests

@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 @@
"""Returns the embedding dimension."""
pass

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

Check warning on line 99 in deepmd/pt/model/descriptor/descriptor.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/descriptor.py#L99

Added line #L99 was not covered by tests

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 @@
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 @@
"""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

Check warning on line 279 in deepmd/pt/model/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa2.py#L279

Added line #L279 was not covered by tests

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 @@
"""
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()

Check warning on line 319 in deepmd/pt/model/descriptor/dpa2.py

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa2.py#L319

Added line #L319 was not covered by tests

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,12 @@ 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...
# Note: Using the minimum rcut_smth might not be appropriate in all scenarios. Consider using a different approach or provide detailed documentation on why the minimum value is chosen.
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 +139,16 @@ 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 same environment matrix protection for all descriptors. Found differing values."
)
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