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: add se_atten_v2to PyTorch and DP #3840

Merged
merged 46 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
2e8396e
Add se_attn_v2 in pt
Chengqian-Zhang May 30, 2024
87c3561
Add se_attn_v2 in dp
Chengqian-Zhang May 30, 2024
9b238ad
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
95696ee
Delete redirect to dpa1
Chengqian-Zhang May 30, 2024
e55a891
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang May 30, 2024
08bf55a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
999adc8
Change se_attn_v2 impl in dp and pt
Chengqian-Zhang May 30, 2024
0e27dc8
Fix conflict
Chengqian-Zhang May 30, 2024
c08f61d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
59269f2
Add UT
Chengqian-Zhang May 30, 2024
30cc6af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 30, 2024
46783e3
Fix bug
Chengqian-Zhang May 30, 2024
fdc67f8
Merge branch 'devel' into 3831
Chengqian-Zhang May 31, 2024
c46fb27
Merge branch 'devel' into 3831
Chengqian-Zhang Jun 3, 2024
9ae4dc7
Add UT for consistency
Chengqian-Zhang Jun 3, 2024
7e4826e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
d6a6e9b
change sentence
Chengqian-Zhang Jun 3, 2024
9680443
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
d4696a1
Delete tf UT
Chengqian-Zhang Jun 3, 2024
d5c7ace
Solve conflict
Chengqian-Zhang Jun 3, 2024
67ad350
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
8494c44
Change Doc of se_atten_v2
Chengqian-Zhang Jun 3, 2024
0218a01
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 3, 2024
2e990cd
Delete tf se_atten_v2
Chengqian-Zhang Jun 3, 2024
890df1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
98d6daf
change attn-->atten
Chengqian-Zhang Jun 3, 2024
3d66922
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 3, 2024
a14fed2
Add serialize in se_attn_v2 tf
Chengqian-Zhang Jun 3, 2024
c642823
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
10c8909
fix pre-commit
Chengqian-Zhang Jun 3, 2024
6b9a93e
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 3, 2024
6177522
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 3, 2024
6813f7f
Solve conversation
Chengqian-Zhang Jun 4, 2024
45f9755
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 4, 2024
d2fd8a2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 4, 2024
9d4d0e3
Solve alert
Chengqian-Zhang Jun 4, 2024
d25993f
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 4, 2024
001d21c
Solve alert
Chengqian-Zhang Jun 4, 2024
8ee1125
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 4, 2024
b0380f9
Update deepmd/dpmodel/descriptor/se_atten_v2.py
Chengqian-Zhang Jun 4, 2024
6f2473f
Update deepmd/pt/model/descriptor/se_atten_v2.py
Chengqian-Zhang Jun 4, 2024
ef94c04
Update deepmd/pt/model/descriptor/se_atten_v2.py
Chengqian-Zhang Jun 4, 2024
b39fcce
Update deepmd/utils/argcheck.py
Chengqian-Zhang Jun 4, 2024
c982d0e
Merge branch 'devel' into 3831
Chengqian-Zhang Jun 5, 2024
c3892f4
Delete doc_stripped_type_embedding
Chengqian-Zhang Jun 5, 2024
38fc569
Merge branch '3831' of github.com:Chengqian-Zhang/deepmd-kit into 3831
Chengqian-Zhang Jun 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 60 additions & 6 deletions deepmd/dpmodel/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
return x / np.linalg.norm(x, axis=axis, keepdims=True)


@BaseDescriptor.register("se_atten")
@BaseDescriptor.register("se_atten_v2")
Chengqian-Zhang marked this conversation as resolved.
Show resolved Hide resolved
@BaseDescriptor.register("dpa1")
class DescrptDPA1(NativeOP, BaseDescriptor):
r"""Attention-based descriptor which is proposed in the pretrainable DPA-1[1] model.
Expand Down Expand Up @@ -226,7 +226,6 @@
neuron: List[int] = [25, 50, 100],
axis_neuron: int = 8,
tebd_dim: int = 8,
tebd_input_mode: str = "concat",
resnet_dt: bool = False,
trainable: bool = True,
type_one_side: bool = False,
Expand All @@ -244,7 +243,6 @@
temperature: Optional[float] = None,
trainable_ln: bool = True,
ln_eps: Optional[float] = 1e-5,
smooth_type_embedding: bool = True,
concat_output_tebd: bool = True,
spin: Optional[Any] = None,
stripped_type_embedding: Optional[bool] = None,
Expand All @@ -268,15 +266,14 @@
if ln_eps is None:
ln_eps = 1e-5

self.se_atten = DescrptBlockSeAtten(
self.se_atten = DescrptBlockSeAttenV2(

Check warning on line 269 in deepmd/dpmodel/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L269

Added line #L269 was not covered by tests
rcut,
rcut_smth,
sel,
ntypes,
neuron=neuron,
axis_neuron=axis_neuron,
tebd_dim=tebd_dim,
tebd_input_mode=tebd_input_mode,
set_davg_zero=set_davg_zero,
attn=attn,
attn_layer=attn_layer,
Expand All @@ -288,7 +285,6 @@
scaling_factor=scaling_factor,
normalize=normalize,
temperature=temperature,
smooth=smooth_type_embedding,
type_one_side=type_one_side,
exclude_types=exclude_types,
env_protection=env_protection,
Expand Down Expand Up @@ -871,6 +867,64 @@
)


@DescriptorBlock.register("se_atten_v2")
class DescrptBlockSeAttenV2(DescrptBlockSeAtten):
def __init__(
self,
rcut: float,
rcut_smth: float,
sel: Union[List[int], int],
ntypes: int,
neuron: List[int] = [25, 50, 100],
axis_neuron: int = 8,
tebd_dim: int = 8,
resnet_dt: bool = False,
type_one_side: bool = False,
attn: int = 128,
attn_layer: int = 2,
attn_dotr: bool = True,
attn_mask: bool = False,
exclude_types: List[Tuple[int, int]] = [],
env_protection: float = 0.0,
set_davg_zero: bool = False,
activation_function: str = "tanh",
precision: str = DEFAULT_PRECISION,
scaling_factor=1.0,
normalize: bool = True,
temperature: Optional[float] = None,
trainable_ln: bool = True,
ln_eps: Optional[float] = 1e-5,
) -> None:
DescrptBlockSeAtten.__init__(

Check warning on line 898 in deepmd/dpmodel/descriptor/dpa1.py

View check run for this annotation

Codecov / codecov/patch

deepmd/dpmodel/descriptor/dpa1.py#L898

Added line #L898 was not covered by tests
self,
rcut,
rcut_smth,
sel,
ntypes,
neuron=neuron,
axis_neuron=axis_neuron,
tebd_dim=tebd_dim,
tebd_input_mode="strip",
resnet_dt=resnet_dt,
type_one_side=type_one_side,
attn=attn,
attn_layer=attn_layer,
attn_dotr=attn_dotr,
attn_mask=attn_mask,
exclude_types=exclude_types,
env_protection=env_protection,
set_davg_zero=set_davg_zero,
activation_function=activation_function,
precision=precision,
scaling_factor=scaling_factor,
normalize=normalize,
temperature=temperature,
trainable_ln=trainable_ln,
ln_eps=ln_eps,
smooth=True,
)


class NeighborGatedAttention(NativeOP):
def __init__(
self,
Expand Down
12 changes: 5 additions & 7 deletions deepmd/pt/model/descriptor/dpa1.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
BaseDescriptor,
)
from .se_atten import (
DescrptBlockSeAtten,
NeighborGatedAttention,
)
from .se_atten_v2 import (

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L43

Added line #L43 was not covered by tests
DescrptBlockSeAttenV2,
)


@BaseDescriptor.register("dpa1")
@BaseDescriptor.register("se_atten")
@BaseDescriptor.register("se_atten_v2")

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L49

Added line #L49 was not covered by tests
class DescrptDPA1(BaseDescriptor, torch.nn.Module):
r"""Attention-based descriptor which is proposed in the pretrainable DPA-1[1] model.

Expand Down Expand Up @@ -206,7 +208,6 @@
neuron: list = [25, 50, 100],
axis_neuron: int = 16,
tebd_dim: int = 8,
tebd_input_mode: str = "concat",
set_davg_zero: bool = True,
attn: int = 128,
attn_layer: int = 2,
Expand All @@ -224,7 +225,6 @@
trainable: bool = True,
trainable_ln: bool = True,
ln_eps: Optional[float] = 1e-5,
smooth_type_embedding: bool = True,
type_one_side: bool = False,
stripped_type_embedding: Optional[bool] = None,
seed: Optional[int] = None,
Expand All @@ -251,15 +251,14 @@
ln_eps = 1e-5

del type, spin, attn_mask
self.se_atten = DescrptBlockSeAtten(
self.se_atten = DescrptBlockSeAttenV2(

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/dpa1.py#L254

Added line #L254 was not covered by tests
rcut,
rcut_smth,
sel,
ntypes,
neuron=neuron,
axis_neuron=axis_neuron,
tebd_dim=tebd_dim,
tebd_input_mode=tebd_input_mode,
set_davg_zero=set_davg_zero,
attn=attn,
attn_layer=attn_layer,
Expand All @@ -271,7 +270,6 @@
scaling_factor=scaling_factor,
normalize=normalize,
temperature=temperature,
smooth=smooth_type_embedding,
type_one_side=type_one_side,
exclude_types=exclude_types,
env_protection=env_protection,
Expand Down
139 changes: 139 additions & 0 deletions deepmd/pt/model/descriptor/se_atten_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from typing import (

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_atten_v2.py#L2

Added line #L2 was not covered by tests
List,
Optional,
Tuple,
Union,
)

from deepmd.pt.model.descriptor.descriptor import (

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_atten_v2.py#L9

Added line #L9 was not covered by tests
DescriptorBlock,
)
from deepmd.pt.model.descriptor.se_atten import (

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_atten_v2.py#L12

Added line #L12 was not covered by tests
DescrptBlockSeAtten,
)


@DescriptorBlock.register("se_atten_v2")
class DescrptBlockSeAttenV2(DescrptBlockSeAtten):
def __init__(

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_atten_v2.py#L17-L19

Added lines #L17 - L19 were not covered by tests
self,
rcut: float,
rcut_smth: float,
sel: Union[List[int], int],
ntypes: int,
neuron: list = [25, 50, 100],
axis_neuron: int = 16,
tebd_dim: int = 8,
set_davg_zero: bool = True,
attn: int = 128,
attn_layer: int = 2,
attn_dotr: bool = True,
attn_mask: bool = False,
activation_function="tanh",
precision: str = "float64",
resnet_dt: bool = False,
scaling_factor=1.0,
normalize=True,
temperature=None,
type_one_side: bool = False,
exclude_types: List[Tuple[int, int]] = [],
env_protection: float = 0.0,
trainable_ln: bool = True,
ln_eps: Optional[float] = 1e-5,
seed: Optional[int] = None,
type: Optional[str] = None,
old_impl: bool = False,
) -> None:
r"""Construct an embedding net of type `se_atten`.

Parameters
----------
rcut : float
The cut-off radius :math:`r_c`
rcut_smth : float
From where the environment matrix should be smoothed :math:`r_s`
sel : list[int], int
list[int]: sel[i] specifies the maxmum number of type i atoms in the cut-off radius
int: the total maxmum number of atoms in the cut-off radius
ntypes : int
Number of element types
neuron : list[int]
Number of neurons in each hidden layers of the embedding net :math:`\mathcal{N}`
axis_neuron : int
Number of the axis neuron :math:`M_2` (number of columns of the sub-matrix of the embedding matrix)
tebd_dim : int
Dimension of the type embedding
resnet_dt : bool
Time-step `dt` in the resnet construction:
y = x + dt * \phi (Wx + b)
trainable_ln : bool
Whether to use trainable shift and scale weights in layer normalization.
ln_eps : float, Optional
The epsilon value for layer normalization.
type_one_side : bool
If 'False', type embeddings of both neighbor and central atoms are considered.
If 'True', only type embeddings of neighbor atoms are considered.
Default is 'False'.
attn : int
Hidden dimension of the attention vectors
attn_layer : int
Number of attention layers
attn_dotr : bool
If dot the angular gate to the attention weights
attn_mask : bool
(Only support False to keep consistent with other backend references.)
(Not used in this version.)
If mask the diagonal of attention weights
exclude_types : List[List[int]]
The excluded pairs of types which have no interaction with each other.
For example, `[[0, 1]]` means no interaction between type 0 and type 1.
env_protection : float
Protection parameter to prevent division by zero errors during environment matrix calculations.
set_davg_zero : bool
Set the shift of embedding net input to zero.
activation_function : str
The activation function in the embedding net. Supported options are |ACTIVATION_FN|
precision : str
The precision of the embedding net parameters. Supported options are |PRECISION|
scaling_factor : float
The scaling factor of normalization in calculations of attention weights.
If `temperature` is None, the scaling of attention weights is (N_dim * scaling_factor)**0.5
normalize : bool
Whether to normalize the hidden vectors in attention weights calculation.
temperature : float
If not None, the scaling of attention weights is `temperature` itself.
seed : int, Optional
Random seed for parameter initialization.
"""
DescrptBlockSeAtten.__init__(

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

View check run for this annotation

Codecov / codecov/patch

deepmd/pt/model/descriptor/se_atten_v2.py#L109

Added line #L109 was not covered by tests
self,
rcut,
rcut_smth,
sel,
ntypes,
neuron=neuron,
axis_neuron=axis_neuron,
tebd_dim=tebd_dim,
tebd_input_mode="strip",
set_davg_zero=set_davg_zero,
attn=attn,
attn_layer=attn_layer,
attn_dotr=attn_dotr,
attn_mask=attn_mask,
activation_function=activation_function,
precision=precision,
resnet_dt=resnet_dt,
scaling_factor=scaling_factor,
normalize=normalize,
temperature=temperature,
smooth=True,
type_one_side=type_one_side,
exclude_types=exclude_types,
env_protection=env_protection,
trainable_ln=trainable_ln,
ln_eps=ln_eps,
seed=seed,
type=type,
old_impl=old_impl,
)
Loading