forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_dpa3.py
More file actions
375 lines (347 loc) · 10.1 KB
/
test_dpa3.py
File metadata and controls
375 lines (347 loc) · 10.1 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# SPDX-License-Identifier: LGPL-3.0-or-later
import unittest
from typing import (
Any,
)
import numpy as np
from dargs import (
Argument,
)
from deepmd.dpmodel.descriptor.dpa3 import DescrptDPA3 as DescrptDPA3DP
from deepmd.env import (
GLOBAL_NP_FLOAT_PRECISION,
)
from ..common import (
INSTALLED_ARRAY_API_STRICT,
INSTALLED_JAX,
INSTALLED_PD,
INSTALLED_PT,
CommonTest,
parameterized,
)
from .common import (
DescriptorTest,
)
if INSTALLED_PT:
from deepmd.pt.model.descriptor.dpa3 import DescrptDPA3 as DescrptDPA3PT
else:
DescrptDPA3PT = None
if INSTALLED_JAX:
from deepmd.jax.descriptor.dpa3 import DescrptDPA3 as DescrptDPA3JAX
else:
DescrptDPA3JAX = None
if INSTALLED_PD:
from deepmd.pd.model.descriptor.dpa3 import DescrptDPA3 as DescrptDPA3PD
else:
DescrptDPA3PD = None
if INSTALLED_ARRAY_API_STRICT:
from ...array_api_strict.descriptor.dpa3 import DescrptDPA3 as DescrptDPA3Strict
else:
DescrptDPA3Strict = None
# not implemented
DescrptDPA3TF = None
from deepmd.dpmodel.descriptor.dpa3 import (
RepFlowArgs,
)
from deepmd.utils.argcheck import (
descrpt_dpa3_args,
)
@parameterized(
("const",), # update_residual_init
([], [[0, 1]]), # exclude_types
(True, False), # update_angle
(0, 1), # a_compress_rate
(1, 2), # a_compress_e_rate
(True,), # a_compress_use_split
(True, False), # optim_update
(True, False), # edge_init_use_dist
(True, False), # use_dynamic_sel
(0.3, 0.0), # fix_stat_std
(1, 2), # n_multi_edge_message
("float64",), # precision
)
class TestDPA3(CommonTest, DescriptorTest, unittest.TestCase):
@property
def data(self) -> dict:
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
return {
"ntypes": self.ntypes,
# kwargs for repinit
"repflow": RepFlowArgs(
**{
"n_dim": 20,
"e_dim": 10,
"a_dim": 8,
"nlayers": 3,
"e_rcut": 6.0,
"e_rcut_smth": 5.0,
"e_sel": 10,
"a_rcut": 4.0,
"a_rcut_smth": 3.5,
"a_sel": 8,
"a_compress_rate": a_compress_rate,
"a_compress_e_rate": a_compress_e_rate,
"a_compress_use_split": a_compress_use_split,
"optim_update": optim_update,
"edge_init_use_dist": edge_init_use_dist,
"use_dynamic_sel": use_dynamic_sel,
"smooth_edge_update": True,
"fix_stat_std": fix_stat_std,
"n_multi_edge_message": n_multi_edge_message,
"axis_neuron": 4,
"update_angle": update_angle,
"update_style": "res_residual",
"update_residual": 0.1,
"update_residual_init": update_residual_init,
}
),
# kwargs for descriptor
"activation_function": "silu",
"precision": precision,
"exclude_types": exclude_types,
"env_protection": 0.0,
"trainable": True,
}
@property
def skip_pt(self) -> bool:
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
return CommonTest.skip_pt
@property
def skip_pd(self) -> bool:
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
return (
not INSTALLED_PD
or precision == "bfloat16"
or edge_init_use_dist
or use_dynamic_sel
) # not supported yet
@property
def skip_dp(self) -> bool:
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
return CommonTest.skip_dp
@property
def skip_tf(self) -> bool:
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
return True
skip_jax = not INSTALLED_JAX
skip_array_api_strict = not INSTALLED_ARRAY_API_STRICT
tf_class = DescrptDPA3TF
dp_class = DescrptDPA3DP
pt_class = DescrptDPA3PT
pd_class = DescrptDPA3PD
jax_class = DescrptDPA3JAX
array_api_strict_class = DescrptDPA3Strict
args = descrpt_dpa3_args().append(Argument("ntypes", int, optional=False))
def setUp(self) -> None:
CommonTest.setUp(self)
self.ntypes = 2
self.coords = np.array(
[
12.83,
2.56,
2.18,
12.09,
2.87,
2.74,
00.25,
3.32,
1.68,
3.36,
3.00,
1.81,
3.51,
2.51,
2.60,
4.27,
3.22,
1.56,
],
dtype=GLOBAL_NP_FLOAT_PRECISION,
)
self.atype = np.array([0, 1, 1, 0, 1, 1], dtype=np.int32)
self.box = np.array(
[13.0, 0.0, 0.0, 0.0, 13.0, 0.0, 0.0, 0.0, 13.0],
dtype=GLOBAL_NP_FLOAT_PRECISION,
)
self.natoms = np.array([6, 6, 2, 4], dtype=np.int32)
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
def build_tf(self, obj: Any, suffix: str) -> tuple[list, dict]:
return self.build_tf_descriptor(
obj,
self.natoms,
self.coords,
self.atype,
self.box,
suffix,
)
def eval_dp(self, dp_obj: Any) -> Any:
return self.eval_dp_descriptor(
dp_obj,
self.natoms,
self.coords,
self.atype,
self.box,
mixed_types=True,
)
def eval_pt(self, pt_obj: Any) -> Any:
return self.eval_pt_descriptor(
pt_obj,
self.natoms,
self.coords,
self.atype,
self.box,
mixed_types=True,
)
def eval_pd(self, pd_obj: Any) -> Any:
return self.eval_pd_descriptor(
pd_obj,
self.natoms,
self.coords,
self.atype,
self.box,
mixed_types=True,
)
def eval_jax(self, jax_obj: Any) -> Any:
return self.eval_jax_descriptor(
jax_obj,
self.natoms,
self.coords,
self.atype,
self.box,
mixed_types=True,
)
def eval_array_api_strict(self, array_api_strict_obj: Any) -> Any:
return self.eval_array_api_strict_descriptor(
array_api_strict_obj,
self.natoms,
self.coords,
self.atype,
self.box,
mixed_types=True,
)
def extract_ret(self, ret: Any, backend) -> tuple[np.ndarray, ...]:
return (ret[0],)
@property
def rtol(self) -> float:
"""Relative tolerance for comparing the return value."""
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
if precision == "float64":
return 1e-10
elif precision == "float32":
return 1e-4
else:
raise ValueError(f"Unknown precision: {precision}")
@property
def atol(self) -> float:
"""Absolute tolerance for comparing the return value."""
(
update_residual_init,
exclude_types,
update_angle,
a_compress_rate,
a_compress_e_rate,
a_compress_use_split,
optim_update,
edge_init_use_dist,
use_dynamic_sel,
fix_stat_std,
n_multi_edge_message,
precision,
) = self.param
if precision == "float64":
return 1e-6 # need to fix in the future, see issue https://github.com/deepmodeling/deepmd-kit/issues/3786
elif precision == "float32":
return 1e-4
else:
raise ValueError(f"Unknown precision: {precision}")