-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_runner.py
236 lines (216 loc) · 5.85 KB
/
experiment_runner.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
import itertools
import os
import subprocess
import sys
import time
from concurrent.futures import ProcessPoolExecutor
from datetime import date
from multiprocessing import freeze_support, current_process
import torch.cuda
processes_per_gpu = 1
gpu_count = 1
max_batch_size = 1000
run_index = 0
last_index = -1
if len(sys.argv) >= 2:
run_index = int(sys.argv[1])
if len(sys.argv) >= 3:
last_index = int(sys.argv[2])
def run_command(command_idx):
command, env, idx = command_idx
gpu_index = current_process()._identity[0] % gpu_count
if torch.cuda.is_available():
command += f" -device cuda:{gpu_index}"
print(
"Command:",
idx,
"on gpu",
gpu_index,
"on process",
current_process()._identity[0],
)
else:
command += " -device cpu"
print("Command:", idx, "on cpu on process", current_process()._identity[0])
env_str = " ".join(f"{k}={v}" for k, v in env.items())
env.update(os.environ)
today = date.today()
os.makedirs("./logs", exist_ok=True)
try:
start = time.time()
file_name = f"./logs/error_{idx}_{today}.txt"
with open(file_name, "a+") as err:
subprocess.run(
command,
shell=True,
check=True,
stderr=err,
env={**env, "PYTHONOPTIMIZE": "2"},
)
elapsed = time.time() - start
best_score = '0'
with open(file_name, 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith('Best: '):
best_score = line.split(': ')[1].split(',')[0]
with open("./logs/finished_runs.txt", "a+") as fp:
fp.write(f"{idx} -> {today} -> " + str(elapsed) + "s, score: " + best_score + " + " + env_str + " " + command + "\n")
os.remove(file_name)
except subprocess.CalledProcessError:
with open(f"./logs/failed_runs_{today}.txt", "a+") as fp:
fp.write(env_str + " " + command + "\n")
except Exception as e:
import traceback
traceback.print_exc()
def create_run(
dataset,
model,
optimizer,
seed,
epochs,
es_patience,
batch_size,
scheduler_params,
lr,
reduction,
loss_scaling
):
scheduler_name, scheduler_params = scheduler_params
scheduler_params = str(scheduler_params).replace(" ", "")
scheduler_params = str(scheduler_params).replace('"', "'")
scheduler_params = '"' + scheduler_params + '"'
return (
f" -lr {lr}"
f" -bs {batch_size}"
f" -epochs {epochs}"
f" -es_patience {es_patience}"
f" -dataset {dataset}"
f" -reduction {reduction}"
f" -data_path ../data"
f" -scheduler {scheduler_name}"
f" -scheduler_params {scheduler_params}"
f" -model {model}"
f" -seed {seed}"
f" -fill 0.5"
f" --cutout"
f" --autoaug"
f" --tta"
f" --disable_progress_bar"
f" --stderr"
f" --verbose"
) + (
" --half" if torch.cuda.is_available() else ""
) + (
f" -loss_scaling {loss_scaling}" if loss_scaling is not None else ""
)
def generate_runs():
datasets = [
# 'cifar10',
# "cifar10",
"cifar100",
# "FashionMNIST",
]
models = [
"preresnet18_c10"
]
optimizers = [
"sgd"
]
seeds = [
2525
]
epochss = [
200
]
es_patiences = [
20
]
batch_sizes = [
64
]
lrs = [
0.1, 0.075, 0.05, 0.025, 0.01
]
reductions = [
"mean"
]
schedulers = [
("StepLR", {"step_size": 30, "gamma": 0.5}),
]
loss_scalings = [
None,
"uniform-scaling", "normal-scaling"
]
loss_scaling_ranges = [
'0.1', '0.25', '0.5', '0.75'
]
runs = []
envs = []
for (
dataset,
model,
optimizer,
seed,
epochs,
es_patience,
batch_size,
scheduler_params,
lr,
reduction,
loss_scaling
) in itertools.product(
datasets,
models,
optimizers,
seeds,
epochss,
es_patiences,
batch_sizes,
schedulers,
lrs,
reductions,
loss_scalings
):
run = create_run(
dataset=dataset,
model=model,
optimizer=optimizer,
seed=seed,
epochs=epochs,
es_patience=es_patience,
batch_size=batch_size,
scheduler_params=scheduler_params,
lr=lr,
reduction=reduction,
loss_scaling=loss_scaling
)
if loss_scaling is not None:
for loss_scaling_range in loss_scaling_ranges:
runs.append(run)
envs.append({'loss_scaling_range': loss_scaling_range})
else:
runs.append(run)
envs.append({})
return [f"python main.py {i}" for i in runs], envs
if __name__ == "__main__":
freeze_support()
runs, envs = generate_runs()
# # Debug
# for i, env in zip(runs, envs):
# print(env, i)
print(len(runs))
if last_index == -1 or last_index > len(runs):
last_index = len(runs)
with open("./logs/finished_runs.txt", "a+") as fp:
fp.write("New experiment: ")
fp.write("\n")
try:
with ProcessPoolExecutor(max_workers=gpu_count * processes_per_gpu) as executor:
executor.map(
run_command,
[(runs[index], envs[index], index) for index in range(run_index, last_index)],
)
finally:
with open("./logs/finished_runs.txt", "a+") as fp:
fp.write("\n")