-
Notifications
You must be signed in to change notification settings - Fork 608
Expand file tree
/
Copy pathtest_dpa1.py
More file actions
157 lines (144 loc) · 5.05 KB
/
test_dpa1.py
File metadata and controls
157 lines (144 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# SPDX-License-Identifier: LGPL-3.0-or-later
import itertools
import unittest
import numpy as np
import torch
from deepmd.dpmodel.descriptor.dpa1 import DescrptDPA1 as DPDescrptDPA1
from deepmd.pt.model.descriptor.dpa1 import (
DescrptDPA1,
)
from deepmd.pt.utils import (
env,
)
from deepmd.pt.utils.env import (
PRECISION_DICT,
)
from ...seed import (
GLOBAL_SEED,
)
from .test_env_mat import (
TestCaseSingleFrameWithNlist,
)
from .test_mlp import (
get_tols,
)
dtype = env.GLOBAL_PT_FLOAT_PRECISION
class TestDescrptSeAtten(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self) -> None:
TestCaseSingleFrameWithNlist.setUp(self)
def test_consistency(
self,
) -> None:
rng = np.random.default_rng(100)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
dstd = 0.1 + np.abs(dstd)
for idt, sm, to, tm, prec, ect in itertools.product(
[False, True], # resnet_dt
[False, True], # smooth_type_embedding
[False, True], # type_one_side
["concat", "strip"], # tebd_input_mode
[
"float64",
], # precision
[False, True], # use_econf_tebd
):
dtype = PRECISION_DICT[prec]
rtol, atol = get_tols(prec)
err_msg = f"idt={idt} prec={prec}"
# dpa1 new impl
dd0 = DescrptDPA1(
self.rcut,
self.rcut_smth,
self.sel_mix,
self.nt,
attn_layer=2,
precision=prec,
resnet_dt=idt,
smooth_type_embedding=sm,
type_one_side=to,
tebd_input_mode=tm,
use_econf_tebd=ect,
type_map=["O", "H"] if ect else None,
seed=GLOBAL_SEED,
).to(env.DEVICE)
dd0.se_atten.mean = torch.tensor(davg, dtype=dtype, device=env.DEVICE)
dd0.se_atten.stddev = torch.tensor(dstd, dtype=dtype, device=env.DEVICE)
rd0, _, _, _, _ = dd0(
torch.tensor(self.coord_ext, dtype=dtype, device=env.DEVICE),
torch.tensor(self.atype_ext, dtype=int, device=env.DEVICE),
torch.tensor(self.nlist, dtype=int, device=env.DEVICE),
)
# serialization
dd1 = DescrptDPA1.deserialize(dd0.serialize())
rd1, _, _, _, _ = dd1(
torch.tensor(self.coord_ext, dtype=dtype, device=env.DEVICE),
torch.tensor(self.atype_ext, dtype=int, device=env.DEVICE),
torch.tensor(self.nlist, dtype=int, device=env.DEVICE),
)
np.testing.assert_allclose(
rd0.detach().cpu().numpy(),
rd1.detach().cpu().numpy(),
rtol=rtol,
atol=atol,
err_msg=err_msg,
)
# dp impl
dd2 = DPDescrptDPA1.deserialize(dd0.serialize())
rd2, _, _, _, _ = dd2.call(
self.coord_ext,
self.atype_ext,
self.nlist,
)
np.testing.assert_allclose(
rd0.detach().cpu().numpy(),
rd2,
rtol=rtol,
atol=atol,
err_msg=err_msg,
)
def test_export(
self,
) -> None:
rng = np.random.default_rng(GLOBAL_SEED)
nf, nloc, nnei = self.nlist.shape
davg = rng.normal(size=(self.nt, nnei, 4))
dstd = rng.normal(size=(self.nt, nnei, 4))
dstd = 0.1 + np.abs(dstd)
for idt, prec, sm, to, tm, ect in itertools.product(
[
False,
], # resnet_dt
[
"float64",
], # precision
[False, True], # smooth_type_embedding
[
False,
], # type_one_side
["concat", "strip"], # tebd_input_mode
[False, True], # use_econf_tebd
):
dtype = PRECISION_DICT[prec]
# dpa1 new impl
dd0 = DescrptDPA1(
self.rcut,
self.rcut_smth,
self.sel,
self.nt,
precision=prec,
resnet_dt=idt,
smooth_type_embedding=sm,
type_one_side=to,
tebd_input_mode=tm,
use_econf_tebd=ect,
type_map=["O", "H"] if ect else None,
seed=GLOBAL_SEED,
)
dd0.se_atten.mean = torch.tensor(davg, dtype=dtype, device=env.DEVICE)
dd0.se_atten.dstd = torch.tensor(dstd, dtype=dtype, device=env.DEVICE)
coord_ext = torch.tensor(self.coord_ext, dtype=dtype, device=env.DEVICE)
atype_ext = torch.tensor(self.atype_ext, dtype=int, device=env.DEVICE)
nlist = torch.tensor(self.nlist, dtype=int, device=env.DEVICE)
_ = torch.export.export(dd0, (coord_ext, atype_ext, nlist))