-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_se_e2_a.py
169 lines (158 loc) · 5.33 KB
/
test_se_e2_a.py
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
158
159
160
161
162
163
164
165
166
167
168
169
import torch, copy
import unittest
import itertools
import numpy as np
try :
from deepmd_utils.model_format import (
DescrptSeA as DPDescrptSeA,
PRECISION_DICT as DP_PRECISION_DICT,
)
support_se_e2_a = True
except ModuleNotFoundError:
support_se_e2_a = False
except ImportError:
support_se_e2_a = False
from deepmd_pt.model.descriptor.se_a import (
DescrptSeA
)
from deepmd_pt.utils.env import (
PRECISION_DICT,
DEFAULT_PRECISION,
)
from deepmd_pt.utils import env
from .test_mlp import get_tols
dtype = env.GLOBAL_PT_FLOAT_PRECISION
class TestCaseSingleFrameWithNlist():
def setUp(self):
# nloc == 3, nall == 4
self.nloc = 3
self.nall = 4
self.nf, self.nt = 1, 2
self.coord_ext = np.array(
[
[0, 0, 0],
[0, 1, 0],
[0, 0, 1],
[0, -2, 0],
],
dtype=np.float64,
).reshape([1, self.nall * 3])
self.atype_ext = np.array([0, 0, 1, 0], dtype=int).reshape([1, self.nall])
# sel = [5, 2]
self.sel = [5, 2]
self.nlist = np.array(
[
[1, 3, -1, -1, -1, 2, -1],
[0, -1, -1, -1, -1, 2, -1],
[0, 1, -1, -1, -1, 0, -1],
],
dtype=int,
).reshape([1, self.nloc, sum(self.sel)])
self.rcut = 0.4
self.rcut_smth = 2.2
# to be merged with the tf test case
@unittest.skipIf(not support_se_e2_a, "EnvMat not supported")
class TestDescrptSeA(unittest.TestCase, TestCaseSingleFrameWithNlist):
def setUp(self):
TestCaseSingleFrameWithNlist.setUp(self)
def test_consistency(
self,
):
rng = np.random.default_rng()
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 in itertools.product(
[False, True],
["float64", "float32"],
):
dtype = PRECISION_DICT[prec]
rtol, atol = get_tols(prec)
err_msg = f"idt={idt} prec={prec}"
# sea new impl
dd0 = DescrptSeA(
self.rcut, self.rcut_smth, self.sel,
precision=prec,
resnet_dt=idt,
old_impl=False,
).to(env.DEVICE)
dd0.sea.mean = torch.tensor(davg, dtype=dtype, device=env.DEVICE)
dd0.sea.dstd = 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 = DescrptSeA.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 = DPDescrptSeA.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,
)
# old impl
if idt == False and prec == "float64":
dd3 = DescrptSeA(
self.rcut, self.rcut_smth, self.sel,
precision=prec,
resnet_dt=idt,
old_impl=True,
).to(env.DEVICE)
dd0_state_dict = dd0.sea.state_dict()
dd3_state_dict = dd3.sea.state_dict()
for i in dd3_state_dict:
dd3_state_dict[i] = dd0_state_dict[i.replace('.deep_layers.', '.layers.')
.replace('filter_layers_old.', 'filter_layers.networks.')].detach().clone()
if '.bias' in i:
dd3_state_dict[i] = dd3_state_dict[i].unsqueeze(0)
dd3.sea.load_state_dict(dd3_state_dict)
rd3, _,_,_,_ = dd3(
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(), rd3.detach().cpu().numpy(),
rtol=rtol, atol=atol, err_msg=err_msg,
)
def test_jit(
self,
):
rng = np.random.default_rng()
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 in itertools.product(
[False, True],
["float64", "float32"],
):
dtype = PRECISION_DICT[prec]
rtol, atol = get_tols(prec)
err_msg = f"idt={idt} prec={prec}"
# sea new impl
dd0 = DescrptSeA(
self.rcut, self.rcut_smth, self.sel,
precision=prec,
resnet_dt=idt,
old_impl=False,
)
dd0.sea.mean = torch.tensor(davg, dtype=dtype, device=env.DEVICE)
dd0.sea.dstd = torch.tensor(dstd, dtype=dtype, device=env.DEVICE)
dd1 = DescrptSeA.deserialize(dd0.serialize())
model = torch.jit.script(dd0)
model = torch.jit.script(dd1)