forked from YuhangSong/Prospective-Configuration
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
260 lines (213 loc) · 8.67 KB
/
main.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
import os
import copy
import yaml
import argparse
import ray
import utils as u
logger = u.getLogger(__name__)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-c',
'--experiment_config',
required=True,
type=str,
help='Format: experiment/config.',
)
parser.add_argument(
"-l",
"--local-mode",
default=False,
action='store_true',
help="Run in local mode (only one thread is running at a time)."
)
args = parser.parse_args()
# load config
with open('aai-experiments/{}.yaml'.format(args.experiment_config)) as f:
config = yaml.safe_load(f)
# # warning after load config
logger.warning(
"Config yaml loaded. You can safely make changes to the yaml now. \n"
)
# for historical experiments, there were default imports of trainables, importing here
# to avoid breaking old experiments
from base_trainable import BaseTrainable
from dataset_learning_trainable import DatasetLearningTrainable
from supervised_learning_trainable import SupervisedLearningTrainable
from any_energy_trainable import AnyEnergyTrainable
from hand_coded_rules_trainable import HandCodedRulesTrainable
# main_import_code
if config.get('main_import_code', None) is not None:
exec(config['main_import_code'])
config.pop('main_import_code')
# ray_init_kwargs
if config.get("ray_init_kwargs", None) is not None:
ray_init_kwargs = copy.deepcopy(config["ray_init_kwargs"])
config.pop("ray_init_kwargs")
# depreciated warnings
if ray_init_kwargs.get('num_cpus', None) is not None:
if isinstance(ray_init_kwargs.get('num_cpus'), str):
logger.warning((
f"parsing <num_cpus> from str has been depreciated, removing now. "
))
ray_init_kwargs.pop('num_cpus')
else:
ray_init_kwargs = {}
# args.local_mode
if args.local_mode:
ray_init_kwargs.update({"local_mode": True})
# ray.init
ray.init(**ray_init_kwargs)
ray_paradigm = config.get("ray_paradigm", None)
if ray_paradigm is None:
# depreciated warnings
ray_paradigm = "run"
logger.warning(
"ray_paradigm not specified, defaulting to 'run'. It is recommended to specify this. "
)
else:
config.pop("ray_paradigm")
storage_path = os.path.join(os.environ.get('RESULTS_DIR'))
name = args.experiment_config.split('/')[0]
try:
exec(
f"import experiments.{name}.utils as eu"
)
except Exception as e:
logger.warning((
"the recommended workflow is to use eu.Trainable as your <trainable> or <run_or_experiment>, where eu is imported automatically for your from <your_experiment/utils.py>. "
f"But `import experiments.{name}.utils as eu` fails with the following error: \n"
f"{e} "
))
# before_run_code
before_run_code = None
if config.get('before_run_code', None) is not None:
before_run_code = copy.deepcopy(config['before_run_code'])
config.pop('before_run_code')
# after_run_code
after_run_code = None
if config.get('after_run_code', None) is not None:
after_run_code = copy.deepcopy(config['after_run_code'])
config.pop('after_run_code')
# before_run_code
if before_run_code is not None:
exec(before_run_code)
if ray_paradigm == "run":
# depreciated warnings
if config.get('resources_per_trial', None) is not None:
if config['resources_per_trial'].get('gpu', None) is not None:
if isinstance(config['resources_per_trial']['gpu'], str):
logger.warning((
f"parsing <gpu> from str has been depreciated, setting to 0.125 now. "
))
config['resources_per_trial']['gpu'] = 0.125
# config['storage_path']
config['storage_path'] = storage_path
# config['run_or_experiment']
# # depreciation warnings
if config.get('Trainable', None) is not None:
logger.warning((
f"<Trainable> has been deprecated and replaced by <run_or_experiment>. "
))
config['run_or_experiment'] = config['Trainable']
if config.get('run_or_experiment', 'GenTrainable') == "GeneRecTrainable":
logger.warning((
f"<GeneRecTrainable> has been deprecated and replaced by <HandCodedRulesTrainable>. "
))
# # eval run_or_experiment
run_or_experiment = config.get('run_or_experiment', None)
if run_or_experiment is None:
try:
run_or_experiment = eu.Trainable
except Exception as e:
raise RuntimeError(
"run_or_experiment not specified, and using eu.Trainable failed with the followig error: \n"
f"{e} "
)
else:
assert isinstance(run_or_experiment, str), (
f"run_or_experiment should be a string, but got {run_or_experiment} "
)
if 'eu.' not in run_or_experiment:
logger.warning(
"the recommended workflow is to use eu.Trainable as your <run_or_experiment>, where eu is imported automatically for your from <your_experiment/utils.py>. "
f"But you are using a custom <run_or_experiment>: {run_or_experiment}"
)
run_or_experiment = eval(
run_or_experiment
)
config['run_or_experiment'] = run_or_experiment
# config['name']
# # name is the experiment from experiment_config
config['name'] = name
# config['stop']
if config.get('stop', None) is not None:
if isinstance(config['stop'], str):
config['stop'] = eval(config['stop'])
# config['scheduler']
if config.get('scheduler', None) is not None:
config['scheduler'] = eval(
config['scheduler']
)
# config['global_checkpoint_period']
if config.get('global_checkpoint_period', None) is not None:
# # global_checkpoint_period counld be "np.inf", so eval it
config['global_checkpoint_period'] = eval(
config['global_checkpoint_period']
)
# config
if config.get('config', None) is None:
logger.warning((
f"All config passing into the trainable should be moved under a entry named config. "
))
# callbacks
if len(config.get('callbacks', [])) > 0:
for i in range(len(config['callbacks'])):
config['callbacks'][i] = eval(
config['callbacks'][i]
)
analysis = ray.tune.run(
**config,
)
elif ray_paradigm == "fit":
is_resume = config['is_resume']
config.pop('is_resume')
restore_kwargs = config['restore_kwargs']
config.pop('restore_kwargs')
if not is_resume:
trainable = config.get('trainable', None)
if trainable is None:
try:
trainable = eu.Trainable
except Exception as e:
raise RuntimeError(
"trainable not specified, and using eu.Trainable failed with the followig error: \n"
f"{e} "
)
else:
assert isinstance(trainable, str), (
f"trainable should be a string, but got {type(trainable)}."
)
if 'eu.' not in trainable:
logger.warning(
"the recommended workflow is to use eu.Trainable as your <trainable>, where eu is imported automatically for your from <your_experiment/utils.py>. "
f"But you are using a custom <trainable>: {trainable}"
)
trainable = eval(trainable)
config['trainable'] = trainable
config['tune_config'] = eval(config['tune_config'])
config['run_config'] = eval(config['run_config'])
tuner = ray.tune.Tuner(
**config,
)
else:
tuner = ray.tune.Tuner.restore(
path=os.path.join(os.environ.get('RESULTS_DIR'), name),
**restore_kwargs,
)
results = tuner.fit()
else:
raise ValueError(f"ray_paradigm {ray_paradigm} not supported.")
# after_run_code
if after_run_code is not None:
exec(after_run_code)