forked from deepmodeling/deepmd-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_dp_show.py
195 lines (182 loc) · 7.19 KB
/
test_dp_show.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
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
# SPDX-License-Identifier: LGPL-3.0-or-later
import json
import os
import shutil
import unittest
from copy import (
deepcopy,
)
from pathlib import (
Path,
)
from deepmd.pt.entrypoints.main import (
get_trainer,
)
from deepmd.pt.utils.multi_task import (
preprocess_shared_params,
)
from .model.test_permutation import (
model_se_e2_a,
)
class TestSingleTaskModel(unittest.TestCase):
def setUp(self):
input_json = str(Path(__file__).parent / "water/se_atten.json")
with open(input_json) as f:
self.config = json.load(f)
self.config["training"]["numb_steps"] = 1
self.config["training"]["save_freq"] = 1
data_file = [str(Path(__file__).parent / "water/data/single")]
self.config["training"]["training_data"]["systems"] = data_file
self.config["training"]["validation_data"]["systems"] = data_file
self.config["model"] = deepcopy(model_se_e2_a)
self.config["model"]["type_map"] = ["O", "H", "Au"]
trainer = get_trainer(deepcopy(self.config))
trainer.run()
os.system("dp --pt freeze")
def test_checkpoint(self):
INPUT = "model.pt"
ATTRIBUTES = "type-map descriptor fitting-net"
os.system(f"dp --pt show {INPUT} {ATTRIBUTES} 2> output.txt")
with open("output.txt") as f:
results = f.readlines()
assert "This is a singletask model" in results[-4]
assert "The type_map is ['O', 'H', 'Au']" in results[-3]
assert (
"{'type': 'se_e2_a'" and "'sel': [46, 92, 4]" and "'rcut': 4.0"
) in results[-2]
assert (
"The fitting_net parameter is {'neuron': [24, 24, 24], 'resnet_dt': True, 'seed': 1}"
in results[-1]
)
def test_frozen_model(self):
INPUT = "frozen_model.pth"
ATTRIBUTES = "type-map descriptor fitting-net"
os.system(f"dp --pt show {INPUT} {ATTRIBUTES} 2> output.txt")
with open("output.txt") as f:
results = f.readlines()
assert "This is a singletask model" in results[-4]
assert "The type_map is ['O', 'H', 'Au']" in results[-3]
assert (
"{'type': 'se_e2_a'" and "'sel': [46, 92, 4]" and "'rcut': 4.0"
) in results[-2]
assert (
"The fitting_net parameter is {'neuron': [24, 24, 24], 'resnet_dt': True, 'seed': 1}"
in results[-1]
)
def test_checkpoint_error(self):
INPUT = "model.pt"
ATTRIBUTES = "model-branch type-map descriptor fitting-net"
os.system(f"dp --pt show {INPUT} {ATTRIBUTES} 2> output.txt")
with open("output.txt") as f:
results = f.readlines()
assert (
"RuntimeError: The 'model-branch' option requires a multitask model. The provided model does not meet this criterion."
in results[-1]
)
def tearDown(self):
for f in os.listdir("."):
if f.startswith("model") and f.endswith("pt"):
os.remove(f)
if f in ["lcurve.out", "frozen_model.pth", "output.txt", "checkpoint"]:
os.remove(f)
if f in ["stat_files"]:
shutil.rmtree(f)
class TestMultiTaskModel(unittest.TestCase):
def setUp(self):
input_json = str(Path(__file__).parent / "water/multitask.json")
with open(input_json) as f:
self.config = json.load(f)
self.config["model"]["shared_dict"]["my_descriptor"] = model_se_e2_a[
"descriptor"
]
data_file = [str(Path(__file__).parent / "water/data/data_0")]
self.stat_files = "se_e2_a"
os.makedirs(self.stat_files, exist_ok=True)
self.config["training"]["data_dict"]["model_1"]["training_data"]["systems"] = (
data_file
)
self.config["training"]["data_dict"]["model_1"]["validation_data"][
"systems"
] = data_file
self.config["training"]["data_dict"]["model_1"]["stat_file"] = (
f"{self.stat_files}/model_1"
)
self.config["training"]["data_dict"]["model_2"]["training_data"]["systems"] = (
data_file
)
self.config["training"]["data_dict"]["model_2"]["validation_data"][
"systems"
] = data_file
self.config["training"]["data_dict"]["model_2"]["stat_file"] = (
f"{self.stat_files}/model_2"
)
self.config["model"]["model_dict"]["model_1"]["fitting_net"] = {
"neuron": [1, 2, 3],
"seed": 678,
}
self.config["model"]["model_dict"]["model_2"]["fitting_net"] = {
"neuron": [9, 8, 7],
"seed": 1111,
}
self.config["training"]["numb_steps"] = 1
self.config["training"]["save_freq"] = 1
self.origin_config = deepcopy(self.config)
self.config["model"], self.shared_links = preprocess_shared_params(
self.config["model"]
)
trainer = get_trainer(deepcopy(self.config), shared_links=self.shared_links)
trainer.run()
os.system("dp --pt freeze --head model_1")
def test_checkpoint(self):
INPUT = "model.ckpt.pt"
ATTRIBUTES = "model-branch type-map descriptor fitting-net"
os.system(f"dp --pt show {INPUT} {ATTRIBUTES} 2> output.txt")
with open("output.txt") as f:
results = f.readlines()
assert "This is a multitask model" in results[-8]
assert "Available model branches are ['model_1', 'model_2']" in results[-7]
assert "The type_map of branch model_1 is ['O', 'H', 'B']" in results[-6]
assert "The type_map of branch model_2 is ['O', 'H', 'B']" in results[-5]
assert (
"model_1"
and "'type': 'se_e2_a'"
and "'sel': [46, 92, 4]"
and "'rcut_smth': 0.5"
) in results[-4]
assert (
"model_2"
and "'type': 'se_e2_a'"
and "'sel': [46, 92, 4]"
and "'rcut_smth': 0.5"
) in results[-3]
assert (
"The fitting_net parameter of branch model_1 is {'neuron': [1, 2, 3], 'seed': 678}"
in results[-2]
)
assert (
"The fitting_net parameter of branch model_2 is {'neuron': [9, 8, 7], 'seed': 1111}"
in results[-1]
)
def test_frozen_model(self):
INPUT = "frozen_model.pth"
ATTRIBUTES = "type-map descriptor fitting-net"
os.system(f"dp --pt show {INPUT} {ATTRIBUTES} 2> output.txt")
with open("output.txt") as f:
results = f.readlines()
assert "This is a singletask model" in results[-4]
assert "The type_map is ['O', 'H', 'B']" in results[-3]
assert (
"'type': 'se_e2_a'" and "'sel': [46, 92, 4]" and "'rcut_smth': 0.5"
) in results[-2]
assert (
"The fitting_net parameter is {'neuron': [1, 2, 3], 'seed': 678}"
in results[-1]
)
def tearDown(self):
for f in os.listdir("."):
if f.startswith("model") and f.endswith("pt"):
os.remove(f)
if f in ["lcurve.out", "frozen_model.pth", "checkpoint", "output.txt"]:
os.remove(f)
if f in ["stat_files", self.stat_files]:
shutil.rmtree(f)