forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_property.py
More file actions
333 lines (307 loc) · 9.07 KB
/
test_property.py
File metadata and controls
333 lines (307 loc) · 9.07 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
# SPDX-License-Identifier: LGPL-3.0-or-later
import unittest
from typing import (
Any,
)
import numpy as np
from deepmd.dpmodel.common import (
to_numpy_array,
)
from deepmd.dpmodel.fitting.property_fitting import (
PropertyFittingNet as PropertyFittingDP,
)
from deepmd.env import (
GLOBAL_NP_FLOAT_PRECISION,
)
from deepmd.utils.argcheck import (
fitting_property,
)
from ..common import (
INSTALLED_ARRAY_API_STRICT,
INSTALLED_JAX,
INSTALLED_PT,
INSTALLED_PT_EXPT,
CommonTest,
parameterized,
)
from .common import (
FittingTest,
)
if INSTALLED_PT:
import torch
from deepmd.pt.model.task.property import PropertyFittingNet as PropertyFittingPT
from deepmd.pt.utils.env import DEVICE as PT_DEVICE
else:
PropertyFittingPT = object
if INSTALLED_PT_EXPT:
from deepmd.pt_expt.fitting.property_fitting import (
PropertyFittingNet as PropertyFittingPTExpt,
)
from deepmd.pt_expt.utils.env import DEVICE as PT_EXPT_DEVICE
else:
PropertyFittingPTExpt = None
if INSTALLED_JAX:
from deepmd.jax.env import (
jnp,
)
from deepmd.jax.fitting.fitting import PropertyFittingNet as PropertyFittingJAX
else:
PropertyFittingJAX = object
if INSTALLED_ARRAY_API_STRICT:
import array_api_strict
from ...array_api_strict.fitting.fitting import (
PropertyFittingNet as PropertyFittingStrict,
)
else:
PropertyFittingStrict = object
PropertyFittingTF = object
@parameterized(
(True, False), # resnet_dt
("float64", "float32"), # precision
(True, False), # mixed_types
(0, 1), # numb_fparam
(0, 1), # numb_aparam
(1, 3), # task_dim
(True, False), # intensive
)
class TestProperty(CommonTest, FittingTest, unittest.TestCase):
@property
def data(self) -> dict:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return {
"neuron": [5, 5, 5],
"resnet_dt": resnet_dt,
"precision": precision,
"numb_fparam": numb_fparam,
"numb_aparam": numb_aparam,
"seed": 20240217,
"task_dim": task_dim,
"intensive": intensive,
"property_name": "foo",
"activation_function": "relu",
}
@property
def skip_pt(self) -> bool:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return CommonTest.skip_pt
@property
def skip_tf(self) -> bool:
return True
skip_jax = not INSTALLED_JAX
skip_array_api_strict = not INSTALLED_ARRAY_API_STRICT
@property
def skip_pt_expt(self) -> bool:
return CommonTest.skip_pt_expt
tf_class = PropertyFittingTF
dp_class = PropertyFittingDP
pt_class = PropertyFittingPT
pt_expt_class = PropertyFittingPTExpt
jax_class = PropertyFittingJAX
array_api_strict_class = PropertyFittingStrict
args = fitting_property()
def setUp(self) -> None:
CommonTest.setUp(self)
self.ntypes = 2
self.natoms = np.array([6, 6, 2, 4], dtype=np.int32)
self.inputs = np.ones((1, 6, 20), dtype=GLOBAL_NP_FLOAT_PRECISION)
self.atype = np.array([0, 1, 1, 0, 1, 1], dtype=np.int32)
# inconsistent if not sorted
self.atype.sort()
self.fparam = -np.ones((1,), dtype=GLOBAL_NP_FLOAT_PRECISION)
self.aparam = np.zeros_like(
self.atype, dtype=GLOBAL_NP_FLOAT_PRECISION
).reshape(-1, 1)
@property
def additional_data(self) -> dict:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return {
"ntypes": self.ntypes,
"dim_descrpt": self.inputs.shape[-1],
"mixed_types": mixed_types,
}
def build_tf(self, obj: Any, suffix: str) -> tuple[list, dict]:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return self.build_tf_fitting(
obj,
self.inputs.ravel(),
self.natoms,
self.atype,
self.fparam if numb_fparam else None,
self.aparam if numb_aparam else None,
suffix,
)
def eval_pt(self, pt_obj: Any) -> Any:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return (
pt_obj(
torch.from_numpy(self.inputs).to(device=PT_DEVICE),
torch.from_numpy(self.atype.reshape(1, -1)).to(device=PT_DEVICE),
fparam=torch.from_numpy(self.fparam).to(device=PT_DEVICE)
if numb_fparam
else None,
aparam=torch.from_numpy(self.aparam).to(device=PT_DEVICE)
if numb_aparam
else None,
)[pt_obj.var_name]
.detach()
.cpu()
.numpy()
)
def eval_pt_expt(self, pt_expt_obj: Any) -> Any:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return (
pt_expt_obj(
torch.from_numpy(self.inputs).to(device=PT_EXPT_DEVICE),
torch.from_numpy(self.atype.reshape(1, -1)).to(device=PT_EXPT_DEVICE),
fparam=torch.from_numpy(self.fparam).to(device=PT_EXPT_DEVICE)
if numb_fparam
else None,
aparam=torch.from_numpy(self.aparam).to(device=PT_EXPT_DEVICE)
if numb_aparam
else None,
)[pt_expt_obj.var_name]
.detach()
.cpu()
.numpy()
)
def eval_dp(self, dp_obj: Any) -> Any:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return dp_obj(
self.inputs,
self.atype.reshape(1, -1),
fparam=self.fparam if numb_fparam else None,
aparam=self.aparam if numb_aparam else None,
)[dp_obj.var_name]
def eval_jax(self, jax_obj: Any) -> Any:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return np.asarray(
jax_obj(
jnp.asarray(self.inputs),
jnp.asarray(self.atype.reshape(1, -1)),
fparam=jnp.asarray(self.fparam) if numb_fparam else None,
aparam=jnp.asarray(self.aparam) if numb_aparam else None,
)[jax_obj.var_name]
)
def eval_array_api_strict(self, array_api_strict_obj: Any) -> Any:
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
return to_numpy_array(
array_api_strict_obj(
array_api_strict.asarray(self.inputs),
array_api_strict.asarray(self.atype.reshape(1, -1)),
fparam=array_api_strict.asarray(self.fparam) if numb_fparam else None,
aparam=array_api_strict.asarray(self.aparam) if numb_aparam else None,
)[array_api_strict_obj.var_name]
)
def extract_ret(self, ret: Any, backend) -> tuple[np.ndarray, ...]:
if backend == self.RefBackend.TF:
# shape is not same
ret = ret[0].reshape(-1, self.natoms[0], 1)
return (ret,)
@property
def rtol(self) -> float:
"""Relative tolerance for comparing the return value."""
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = 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."""
(
resnet_dt,
precision,
mixed_types,
numb_fparam,
numb_aparam,
task_dim,
intensive,
) = self.param
if precision == "float64":
return 1e-10
elif precision == "float32":
return 1e-4
else:
raise ValueError(f"Unknown precision: {precision}")