-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathax_opt_ray.py
executable file
·342 lines (319 loc) · 11.1 KB
/
ax_opt_ray.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
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
#!/usr/bin/env python3
# fmt: off
##SBATCH --time=16:00:00
#SBATCH --time=4-00:00:00
#SBATCH --nodes=1
#SBATCH --ntasks=64
#SBATCH --mail-type=all
#SBATCH -p gpu
#SBATCH -J parallel_hyperopt
#SBATCH --qos=long
#SBATCH -o run.log
# black always format pure comments as of now
# add some codes here to keep SLURM derivatives valid
import os
import sys
# SLURM copies the script to a tmp folder
# so to find the local package `training` we need add cwd to path
# per https://stackoverflow.com/a/39574373/7066315
sys.path.append(os.getcwd())
# fmt: on
"""
Hyperparameter tuning for HIPNN using AX and Ray.
"""
import contextlib
import gc
import shutil
import numpy as np
import ray
import torch
from ax.core import Trial as AXTrial
from ax.service.ax_client import AxClient
from ax.service.utils.instantiation import ObjectiveProperties
from ray import air, tune
from ray.air import session
from ray.tune.experiment.trial import Trial
from ray.tune.logger import JsonLoggerCallback, LoggerCallback
from ray.tune.search import ConcurrencyLimiter
from ray.tune.search.ax import AxSearch
from training import main, read_args
# to make sure ray loads correct the local package
ray.init(runtime_env={"working_dir": "."})
def evaluate(parameter: dict, checkpoint_dir=None):
"""
Evaluates a trial for MBO HIPNN.
Args:
parameter (dict): Python dictionary for trial values of HIPNN hyperparameters.
checkpoint_dir (str, optional): To enable checkpoints for ray. Defaults to None.
Returns:
dict : Loss metrics to be minimized.
"""
gc.collect()
torch.cuda.empty_cache()
# initialize and override parameters
# targets = ["energy"]
targets = ["energy", "dipole"]
weights = []
for i in targets:
weights.append(parameter.get(i) or 1.0)
del parameter[i]
params = read_args(
noprogress=True,
db_to_gpu=True,
training_targets=targets,
target_weights=weights,
init_batch_size=32,
custom_kernel=True,
raise_batch_patience=50,
termination_patience=200,
max_batch_size=2048,
max_epochs=8001,
n_states=2,
bypass_cli_args=True,
init_learning_rate=5e-4,
**parameter,
)
# train model
with contextlib.redirect_stdout(open(params.log_filename, "w")):
out = main(params)
e_Loss = out["metric"]["valid"]["ENERGY-Loss"]
d_Loss = out["metric"]["valid"]["DIPOLE-Loss"]
session.report(
{
"Loss": out["Loss"],
# "Metric": out["Loss"],
"Metric": e_Loss + d_Loss / 0.8,
"ENERGY-Loss": e_Loss,
"DIPOLE-Loss": d_Loss,
}
)
class AxLogger(LoggerCallback):
def __init__(self, ax_client: AxClient, json_name: str, csv_name: str):
"""
A logger callback to save the progress to json file after every trial ends.
Similar to running `ax_client.save_to_json_file` every iteration in sequential
searches.
Args:
ax_client (AxClient): ax client to save
json_name (str): name for the json file. Append a path if you want to save the \
json file to somewhere other than cwd.
csv_name (str): name for the csv file. Append a path if you want to save the \
csv file to somewhere other than cwd.
"""
self.ax_client = ax_client
self.json = json_name
self.csv = csv_name
def log_trial_end(
self, trial: Trial, id: int, metric: float, runtime: int, failed: bool = False
):
self.ax_client.save_to_json_file(filepath=self.json)
shutil.copy(self.json, f"{trial.local_dir}/{self.json}")
try:
data_frame = self.ax_client.get_trials_data_frame().sort_values("Metric")
data_frame.to_csv(self.csv, header=True)
except KeyError:
pass
shutil.copy(self.csv, f"{trial.local_dir}/{self.csv}")
if failed:
status = "failed"
else:
status = "finished"
print(
f"AX trial {id} {status}. Final loss: {metric}. Time taken"
f" {runtime} seconds. Location directory: {trial.logdir}."
)
def on_trial_error(self, iteration: int, trials: list[Trial], trial: Trial, **info):
id = int(trial.experiment_tag.split("_")[0]) - 1
ax_trial = self.ax_client.get_trial(id)
ax_trial.mark_abandoned(reason="Error encountered")
self.log_trial_end(
trial, id + 1, "not available", self.calculate_runtime(ax_trial), True
)
def on_trial_complete(
self, iteration: int, trials: list["Trial"], trial: Trial, **info
):
# trial.trial_id is the random id generated by ray, not ax
# the default experiment_tag starts with ax' trial index
# but this workaround is totally fragile, as users can
# customize the tag or folder name
id = int(trial.experiment_tag.split("_")[0]) - 1
ax_trial = self.ax_client.get_trial(id)
failed = False
try:
loss = ax_trial.objective_mean
except ValueError:
failed = True
loss = "not available"
else:
if np.isnan(loss) or np.isinf(loss):
failed = True
loss = "not available"
if failed:
ax_trial.mark_failed()
self.log_trial_end(
trial, id + 1, loss, self.calculate_runtime(ax_trial), failed
)
@classmethod
def calculate_runtime(cls, trial: AXTrial):
delta = trial.time_completed - trial.time_run_started
return int(delta.total_seconds())
# initialize the client and experiment.
if __name__ == "__main__":
os.chdir("/users/lix/scratch/prosq")
# TODO: better way to handle restarting of searches
restart = True
if restart:
ax_client = AxClient.load_from_json_file(filepath="hyperopt_ray.json")
# update existing experiment
# `immutable_search_space_and_opt_config` has to be False
# ax_client.set_search_space(
# [
# {
# "name": "n_interactions",
# "type": "fixed",
# "value_type": "int",
# "value": 1,
# },
# {
# "name": "n_atom_layers",
# "type": "choice",
# "values": [2, 3, 4, 5, 6],
# },
# ]
# )
else:
ax_client = AxClient(
verbose_logging=False,
enforce_sequential_optimization=False,
)
ax_client.create_experiment(
name="prosq_opt",
parameters=[
{
"name": "lower_cutoff",
"type": "fixed",
"value_type": "float",
"value": 0.6334700267070265,
},
{
"name": "upper_cutoff",
"type": "fixed",
"value_type": "float",
"value": 7.170363852174164,
},
{
"name": "cutoff_distance",
"type": "fixed",
"value_type": "float",
"value": 7.426888330783524,
},
{
"name": "n_interactions",
"type": "fixed",
"value_type": "int",
"value": 3,
},
{
"name": "n_atom_layers",
"type": "fixed",
"value": 5,
},
{
"name": "n_sensitivities",
"type": "range",
"value_type": "int",
"bounds": [20, 40],
},
{
"name": "n_features",
"type": "range",
"value_type": "int",
"bounds": [20, 60],
},
# {
# "name": "lower_cutoff",
# "type": "range",
# "value_type": "float",
# "bounds": [0.5, 0.95],
# },
# {
# "name": "upper_cutoff",
# "type": "range",
# "value_type": "float",
# "bounds": [1.5, 10.0],
# },
# {
# "name": "cutoff_distance",
# "type": "range",
# "value_type": "float",
# "bounds": [1.75, 15.0],
# },
# {
# "name": "n_sensitivities",
# "type": "range",
# "value_type": "int",
# "bounds": [20, 40],
# },
# {
# "name": "n_features",
# "type": "range",
# "value_type": "int",
# "bounds": [20, 60],
# },
# {
# "name": "n_interactions",
# "type": "choice",
# "value_type": "int",
# "values": [1, 2, 3],
# },
# {
# "name": "n_atom_layers",
# "type": "choice",
# "values": [2, 3, 4, 5],
# },
{
"name": "dipole",
"type": "fixed",
"value_type": "float",
"value": 1.0,
},
{
"name": "energy",
"type": "range",
"value_type": "float",
"bounds": [0.1, 2],
# "log_scale": True,
},
],
objectives={
"Metric": ObjectiveProperties(minimize=True, threshold=2),
# "Loss": ObjectiveProperties(minimize=True),
# "DIPOLE-Loss": ObjectiveProperties(minimize=True, threshold=0.5),
# "ENERGY-Loss": ObjectiveProperties(minimize=True, threshold=1.0),
},
overwrite_existing_experiment=True,
is_test=False,
# slightly more overhead
# but make it possible to adjust the experiment setups
immutable_search_space_and_opt_config=False,
# parameter_constraints=[
# "lower_cutoff <= upper_cutoff",
# "upper_cutoff <= cutoff_distance",
# ],
)
# run the optimization Loop.
algo = AxSearch(ax_client=ax_client)
algo = ConcurrencyLimiter(algo, max_concurrent=4)
ax_logger = AxLogger(ax_client, "hyperopt_ray.json", "hyperopt.csv")
tuner = tune.Tuner(
tune.with_resources(evaluate, resources={"gpu": 1}),
tune_config=tune.TuneConfig(search_alg=algo, num_samples=10),
run_config=air.RunConfig(
local_dir="./all_in_one",
verbose=0,
callbacks=[ax_logger, JsonLoggerCallback()],
log_to_file=True,
),
)
tuner.fit()
print("Script done.")