-
Notifications
You must be signed in to change notification settings - Fork 83
/
runner.py
executable file
·347 lines (287 loc) · 13.3 KB
/
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
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
343
344
345
346
347
# This script is based on https://github.com/salesforce/LAVIS/blob/main/lavis/runners/runner_base.py
import os
import json
import time
import datetime
from pathlib import Path
import logging
import torch
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from tensorboardX import SummaryWriter
from dist_utils import main_process, is_dist_avail_and_initialized, is_main_process, get_rank, get_world_size
from logger import MetricLogger, SmoothedValue
from utils import get_dataloader, prepare_sample
from optims import get_optimizer, LinearWarmupCosineLRScheduler
class Runner:
def __init__(self, cfg, model, datasets, job_id):
self.config = cfg
# log
self.output_dir = Path(self.config.config.run.output_dir) / job_id
self.output_dir.mkdir(parents=True, exist_ok=True)
self.log_writter = SummaryWriter(self.output_dir)
# settings
self.device = torch.device(self.config.config.run.device)
self.use_distributed = self.config.config.run.use_distributed
self.start_epoch = 0
self.max_epoch = self.config.config.run.optims.max_epoch
self.evaluate_only = self.config.config.run.evaluate
self.cuda_enabled = (self.device.type == "cuda")
# test prompt
self.prompt_template = self.config.config.model.get("prompt_template", "")
test_prompt_path = self.config.config.model.get("test_prompt_path", "")
if test_prompt_path:
try:
with open(test_prompt_path, "r") as f:
self.test_prompt_dict = json.load(f)
except:
print("Failed to load test prompt! Try to use utf-8 encoding.")
with open(test_prompt_path, "r", encoding="utf-8") as f:
self.test_prompt_dict = json.load(f)
for k in self.test_prompt_dict.keys():
self.test_prompt_dict[k] = self.prompt_template.format(self.test_prompt_dict[k])
else:
self.test_prompt_dict = None
# model
self._model = model
self._model.to(self.device)
if self.use_distributed:
self.model = DDP(
self._model, device_ids=[self.config.config.run.gpu]
)
else:
self.model = self._model
# dataloaders
self.train_loader = get_dataloader(datasets["train"], self.config.config.run, is_train=True, use_distributed=self.use_distributed)
self.valid_loader = get_dataloader(datasets["valid"], self.config.config.run, is_train=False, use_distributed=self.use_distributed)
self.test_loader = get_dataloader(datasets["test"], self.config.config.run, is_train=False, use_distributed=self.use_distributed)
# scaler
self.use_amp = self.config.config.run.get("amp", False)
if self.use_amp:
self.scaler = torch.cuda.amp.GradScaler()
else:
self.scaler = None
# optimizer & scheduler
self.iters_per_epoch = len(self.train_loader) if self.config.config.run.epoch_based else self.config.config.run.iters_per_epoch
self.optimizer = get_optimizer(self.model, self.config.config.run.optims)
self.scheduler = LinearWarmupCosineLRScheduler(
self.optimizer,
max_epoch=self.max_epoch,
iters_per_epoch=self.iters_per_epoch,
min_lr=self.config.config.run.optims.min_lr,
init_lr=self.config.config.run.optims.init_lr,
warmup_steps=self.config.config.run.optims.warmup_steps,
warmup_start_lr=self.config.config.run.optims.get("warmup_start_lr", -1),
)
self.log_config()
def unwrap_dist_model(self, model):
if self.use_distributed:
return model.module
else:
return model
def train_epoch(self, epoch):
self.model.train()
metric_logger = MetricLogger(delimiter=" ")
metric_logger.add_meter("lr", SmoothedValue(window_size=1, fmt="{value:.6f}"))
metric_logger.add_meter("loss", SmoothedValue(window_size=1, fmt="{value:.4f}"))
logging.info(
"Start training epoch {}, {} iters per inner epoch.".format(
epoch, self.iters_per_epoch
)
)
header = "Train: data epoch: [{}]".format(epoch)
for i in metric_logger.log_every(range(self.iters_per_epoch), self.config.config.run.log_freq, header=header, logger=self.log_writter, start_step=epoch*self.iters_per_epoch):
if i >= self.iters_per_epoch:
break
samples = next(self.train_loader)
samples = prepare_sample(samples, cuda_enabled=self.cuda_enabled)
self.scheduler.step(cur_epoch=epoch, cur_step=i)
with torch.cuda.amp.autocast(enabled=self.use_amp):
loss = self.model(samples)["loss"]
if self.use_amp:
self.scaler.scale(loss).backward()
else:
loss.backward()
if (i + 1) % self.config.config.run.accum_grad_iters == 0:
if self.use_amp:
self.scaler.step(self.optimizer)
self.scaler.update()
else:
self.optimizer.step()
self.optimizer.zero_grad()
metric_logger.update(loss=loss.item())
metric_logger.update(lr=self.optimizer.param_groups[0]["lr"])
metric_logger.synchronize_between_processes()
logging.info("Averaged stats: " + str(metric_logger.global_avg()))
return {
k: "{:.3f}".format(meter.global_avg)
for k, meter in metric_logger.meters.items()
}
@torch.no_grad()
def valid_epoch(self, epoch, split, decode=False, save_json=False):
model = self.unwrap_dist_model(self.model)
model.eval()
dataloader = getattr(self, split + "_loader", None)
assert dataloader is not None, "{}_loader does not exist.".format(split)
metric_logger = MetricLogger(delimiter=" ")
header = "Eval: data epoch: [{}]".format(epoch)
results = []
for samples in metric_logger.log_every(dataloader, self.config.config.run.log_freq, header=header):
samples = prepare_sample(samples, cuda_enabled=self.cuda_enabled)
with torch.cuda.amp.autocast(enabled=self.use_amp):
forward_result = model(samples, verbose=True)
loss = forward_result.get("loss", 0)
correct = forward_result.get("correct", 0)
total = forward_result.get("total", 1)
res = {
"id": samples["id"],
"ground_truth": samples["text"],
"loss": loss.item(),
"acc": (correct / total).item(),
"total": total,
}
if decode:
if model.prompt_dict:
if self.test_prompt_dict is None:
prompts = None
else:
prompts = [self.test_prompt_dict[s] for s in samples["task"]]
if "Q" in samples:
prompts = [p.format(q) if "{}" in p else p for p, q in zip(prompts, samples["Q"])]
else:
prompts = None
text = model.generate(samples, self.config.config.run, prompts=prompts)
res["text"] = text
res["prompt"] = prompts
res["task"] = samples["task"]
results.append(res)
if is_dist_avail_and_initialized():
dist.barrier()
if save_json:
self.save_result(results, self.output_dir, "eval_{}_epoch_{}".format(split, epoch))
res = {
"loss": torch.tensor(0).float().cuda(),
"n_sample": torch.tensor(0).float().cuda(),
"correct": torch.tensor(0).float().cuda(),
"n_token": torch.tensor(0).float().cuda(),
}
for item in results:
item_loss = item["loss"]
item_n_sample = len(item["id"])
item_correct = item["acc"] * item["total"]
item_n_token = item["total"]
res["loss"] += item_loss * item_n_sample
res["n_sample"] += item_n_sample
res["correct"] += item_correct
res["n_token"] += item_n_token
if is_dist_avail_and_initialized():
dist.all_reduce(res["loss"])
dist.all_reduce(res["n_sample"])
dist.all_reduce(res["correct"])
dist.all_reduce(res["n_token"])
ret = {"loss": 0, "agg_metrics": 0}
ret["loss"] = (res["loss"] / res["n_sample"]).item()
ret["agg_metrics"] = (res["correct"] / res["n_token"]).item()
return ret
def save_result(self, result, result_dir, filename):
result_file = os.path.join(
result_dir, "%s_rank%d.json" % (filename, get_rank())
)
final_result_file = os.path.join(result_dir, "%s.json" % filename)
try:
json.dump(result, open(result_file, "w"), ensure_ascii=False)
except Exception as e:
logging.warning(f"Error saving {result_file}. Error: {e}")
json.dump(result, open(result_file, "w", encoding="utf-8"), ensure_ascii=False)
if is_dist_avail_and_initialized():
dist.barrier()
if is_main_process():
logging.info("rank %d starts merging results." % get_rank())
result = []
for rank in range(get_world_size()):
result_file = os.path.join(
result_dir, "%s_rank%d.json" % (filename, rank)
)
try:
res = json.load(open(result_file, "r"))
except Exception as e:
logging.warning(f"Error reading {result_file}. Error: {e}")
res = json.load(open(result_file, "r", encoding="utf-8"))
result += res
try:
json.dump(result, open(final_result_file, "w"), ensure_ascii=False)
except Exception as e:
logging.warning(f"Error saving {final_result_file}. Error: {e}")
json.dump(result, open(final_result_file, "w", encoding="utf-8"), ensure_ascii=False)
print("result file saved to %s" % final_result_file)
def train(self):
start_time = time.time()
best_agg_metric = 0
best_epoch = 0
for cur_epoch in range(self.start_epoch, self.max_epoch):
if self.evaluate_only:
break
# training phase
logging.info("Training Phase")
train_stats = self.train_epoch(cur_epoch)
self.log_stats(train_stats, split_name="train")
# validating phase
logging.info("Validating Phase")
valid_log = self.valid_epoch(cur_epoch, "valid", decode=False, save_json=False)
if valid_log is not None:
if is_main_process():
agg_metrics = valid_log["agg_metrics"]
if agg_metrics > best_agg_metric:
best_agg_metric = agg_metrics
best_epoch = cur_epoch
self.save_checkpoint(cur_epoch, is_best=True)
valid_log.update({"best_epoch": best_epoch})
self.log_stats(valid_log, split_name="valid")
self.save_checkpoint(cur_epoch, is_best=False)
if self.use_distributed:
dist.barrier()
# testing phase
if self.evaluate_only:
test_log = self.valid_epoch("best", "test", decode=True, save_json=True)
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
logging.info("Training time {}".format(total_time_str))
@main_process
def log_config(self):
with open(os.path.join(self.output_dir, "log.txt"), "a") as f:
f.write(json.dumps(self.config.to_dict(), indent=4) + "\n")
@main_process
def log_stats(self, stats, split_name):
if isinstance(stats, dict):
log_stats = {**{f"{split_name}_{k}": v for k, v in stats.items()}}
with open(os.path.join(self.output_dir, "log.txt"), "a") as f:
f.write(json.dumps(log_stats) + "\n")
elif isinstance(stats, list):
pass
@main_process
def save_checkpoint(self, cur_epoch, is_best=False):
"""
Save the checkpoint at the current epoch.
"""
model_no_ddp = self.unwrap_dist_model(self.model)
param_grad_dic = {
k: v.requires_grad for (k, v) in model_no_ddp.named_parameters()
}
state_dict = model_no_ddp.state_dict()
for k in list(state_dict.keys()):
if k in param_grad_dic.keys() and not param_grad_dic[k]:
# delete parameters that do not require gradient
del state_dict[k]
save_obj = {
"model": state_dict,
"optimizer": self.optimizer.state_dict(),
"config": self.config.to_dict(),
"scaler": self.scaler.state_dict() if self.scaler else None,
"epoch": cur_epoch,
}
save_to = os.path.join(
self.output_dir,
"checkpoint_{}.pth".format("best" if is_best else cur_epoch),
)
logging.info("Saving checkpoint at epoch {} to {}.".format(cur_epoch, save_to))
torch.save(save_obj, save_to)