This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
/
train_extractive_reader.py
574 lines (481 loc) · 19.9 KB
/
train_extractive_reader.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
"""
Pipeline to train the reader model on top of the retriever results
"""
import collections
import json
import sys
import hydra
import logging
import numpy as np
import os
import torch
from collections import defaultdict
from omegaconf import DictConfig, OmegaConf
from typing import List
from dpr.data.qa_validation import exact_match_score
from dpr.data.reader_data import (
ReaderSample,
get_best_spans,
SpanPrediction,
ExtractiveReaderDataset,
)
from dpr.models import init_reader_components
from dpr.models.reader import create_reader_input, ReaderBatch, compute_loss
from dpr.options import (
setup_cfg_gpu,
set_seed,
set_cfg_params_from_state,
get_encoder_params_state_from_cfg,
setup_logger,
)
from dpr.utils.data_utils import (
ShardedDataIterator,
)
from dpr.utils.model_utils import (
get_schedule_linear,
load_states_from_checkpoint,
move_to_device,
CheckpointState,
get_model_file,
setup_for_distributed_mode,
get_model_obj,
)
logger = logging.getLogger()
setup_logger(logger)
ReaderQuestionPredictions = collections.namedtuple("ReaderQuestionPredictions", ["id", "predictions", "gold_answers"])
class ReaderTrainer(object):
def __init__(self, cfg: DictConfig):
self.cfg = cfg
self.shard_id = cfg.local_rank if cfg.local_rank != -1 else 0
self.distributed_factor = cfg.distributed_world_size or 1
logger.info("***** Initializing components for training *****")
model_file = get_model_file(self.cfg, self.cfg.checkpoint_file_name)
saved_state = None
if model_file:
logger.info("!! model_file = %s", model_file)
saved_state = load_states_from_checkpoint(model_file)
set_cfg_params_from_state(saved_state.encoder_params, cfg)
tensorizer, reader, optimizer = init_reader_components(cfg.encoder.encoder_model_type, cfg)
reader, optimizer = setup_for_distributed_mode(
reader,
optimizer,
cfg.device,
cfg.n_gpu,
cfg.local_rank,
cfg.fp16,
cfg.fp16_opt_level,
)
self.reader = reader
self.optimizer = optimizer
self.tensorizer = tensorizer
self.start_epoch = 0
self.start_batch = 0
self.scheduler_state = None
self.best_validation_result = None
self.best_cp_name = None
if saved_state:
self._load_saved_state(saved_state)
def get_data_iterator(
self,
path: str,
batch_size: int,
is_train: bool,
shuffle=True,
shuffle_seed: int = 0,
offset: int = 0,
) -> ShardedDataIterator:
run_preprocessing = True if self.distributed_factor == 1 or self.cfg.local_rank in [-1, 0] else False
gold_passages_src = self.cfg.gold_passages_src
if gold_passages_src:
if not is_train:
gold_passages_src = self.cfg.gold_passages_src_dev
assert os.path.exists(gold_passages_src), "Please specify valid gold_passages_src/gold_passages_src_dev"
dataset = ExtractiveReaderDataset(
path,
is_train,
gold_passages_src,
self.tensorizer,
run_preprocessing,
self.cfg.num_workers,
)
dataset.load_data()
iterator = ShardedDataIterator(
dataset,
shard_id=self.shard_id,
num_shards=self.distributed_factor,
batch_size=batch_size,
shuffle=shuffle,
shuffle_seed=shuffle_seed,
offset=offset,
)
iterator.calculate_shards()
# apply deserialization hook
iterator.apply(lambda sample: sample.on_deserialize())
return iterator
def run_train(self):
cfg = self.cfg
train_iterator = self.get_data_iterator(
cfg.train_files,
cfg.train.batch_size,
True,
shuffle=True,
shuffle_seed=cfg.seed,
offset=self.start_batch,
)
# num_train_epochs = cfg.train.num_train_epochs - self.start_epoch
logger.info("Total iterations per epoch=%d", train_iterator.max_iterations)
updates_per_epoch = train_iterator.max_iterations // cfg.train.gradient_accumulation_steps
total_updates = updates_per_epoch * cfg.train.num_train_epochs
logger.info(" Total updates=%d", total_updates)
warmup_steps = cfg.train.warmup_steps
if self.scheduler_state:
logger.info("Loading scheduler state %s", self.scheduler_state)
shift = int(self.scheduler_state["last_epoch"])
logger.info("Steps shift %d", shift)
scheduler = get_schedule_linear(
self.optimizer,
warmup_steps,
total_updates,
)
else:
scheduler = get_schedule_linear(self.optimizer, warmup_steps, total_updates)
eval_step = cfg.train.eval_step
logger.info(" Eval step = %d", eval_step)
logger.info("***** Training *****")
global_step = self.start_epoch * updates_per_epoch + self.start_batch
for epoch in range(self.start_epoch, cfg.train.num_train_epochs):
logger.info("***** Epoch %d *****", epoch)
global_step = self._train_epoch(scheduler, epoch, eval_step, train_iterator, global_step)
if cfg.local_rank in [-1, 0]:
logger.info("Training finished. Best validation checkpoint %s", self.best_cp_name)
return
def validate_and_save(self, epoch: int, iteration: int, scheduler):
cfg = self.cfg
# in distributed DDP mode, save checkpoint for only one process
save_cp = cfg.local_rank in [-1, 0]
reader_validation_score = self.validate()
if save_cp:
cp_name = self._save_checkpoint(scheduler, epoch, iteration)
logger.info("Saved checkpoint to %s", cp_name)
if reader_validation_score < (self.best_validation_result or 0):
self.best_validation_result = reader_validation_score
self.best_cp_name = cp_name
logger.info("New Best validation checkpoint %s", cp_name)
def validate(self):
logger.info("Validation ...")
cfg = self.cfg
self.reader.eval()
data_iterator = self.get_data_iterator(cfg.dev_files, cfg.train.dev_batch_size, False, shuffle=False)
log_result_step = cfg.train.log_batch_step
all_results = []
eval_top_docs = cfg.eval_top_docs
for i, samples_batch in enumerate(data_iterator.iterate_ds_data()):
input = create_reader_input(
self.tensorizer.get_pad_id(),
samples_batch,
cfg.passages_per_question_predict,
cfg.encoder.sequence_length,
cfg.max_n_answers,
is_train=False,
shuffle=False,
sep_token_id=self.tensorizer.tokenizer.sep_token_id, # TODO: tmp
)
input = ReaderBatch(**move_to_device(input._asdict(), cfg.device))
attn_mask = self.tensorizer.get_attn_mask(input.input_ids)
with torch.no_grad():
start_logits, end_logits, relevance_logits = self.reader(
input.input_ids, attn_mask, input.token_type_ids
)
batch_predictions = self._get_best_prediction(
start_logits,
end_logits,
relevance_logits,
samples_batch,
passage_thresholds=eval_top_docs,
)
all_results.extend(batch_predictions)
if (i + 1) % log_result_step == 0:
logger.info("Eval step: %d ", i)
ems = defaultdict(list)
for q_predictions in all_results:
gold_answers = q_predictions.gold_answers
span_predictions = q_predictions.predictions # {top docs threshold -> SpanPrediction()}
for (n, span_prediction) in span_predictions.items():
em_hit = max([exact_match_score(span_prediction.prediction_text, ga) for ga in gold_answers])
ems[n].append(em_hit)
em = 0
for n in sorted(ems.keys()):
em = np.mean(ems[n])
logger.info("n=%d\tEM %.2f" % (n, em * 100))
if cfg.prediction_results_file:
self._save_predictions(cfg.prediction_results_file, all_results)
return em
def _train_epoch(
self,
scheduler,
epoch: int,
eval_step: int,
train_data_iterator: ShardedDataIterator,
global_step: int,
):
cfg = self.cfg
rolling_train_loss = 0.0
epoch_loss = 0
log_result_step = cfg.train.log_batch_step
rolling_loss_step = cfg.train.train_rolling_loss_step
self.reader.train()
epoch_batches = train_data_iterator.max_iterations
for i, samples_batch in enumerate(train_data_iterator.iterate_ds_data(epoch=epoch)):
data_iteration = train_data_iterator.get_iteration()
# enables to resume to exactly same train state
if cfg.fully_resumable:
np.random.seed(cfg.seed + global_step)
torch.manual_seed(cfg.seed + global_step)
if cfg.n_gpu > 0:
torch.cuda.manual_seed_all(cfg.seed + global_step)
input = create_reader_input(
self.tensorizer.get_pad_id(),
samples_batch,
cfg.passages_per_question,
cfg.encoder.sequence_length,
cfg.max_n_answers,
is_train=True,
shuffle=True,
sep_token_id=self.tensorizer.tokenizer.sep_token_id, # TODO: tmp
)
loss = self._calc_loss(input)
epoch_loss += loss.item()
rolling_train_loss += loss.item()
max_grad_norm = cfg.train.max_grad_norm
if cfg.fp16:
from apex import amp
with amp.scale_loss(loss, self.optimizer) as scaled_loss:
scaled_loss.backward()
if max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(amp.master_params(self.optimizer), max_grad_norm)
else:
loss.backward()
if max_grad_norm > 0:
torch.nn.utils.clip_grad_norm_(self.reader.parameters(), max_grad_norm)
if (i + 1) % cfg.train.gradient_accumulation_steps == 0:
self.optimizer.step()
scheduler.step()
self.reader.zero_grad()
global_step += 1
if i % log_result_step == 0:
lr = self.optimizer.param_groups[0]["lr"]
logger.info(
"Epoch: %d: Step: %d/%d, global_step=%d, lr=%f",
epoch,
data_iteration,
epoch_batches,
global_step,
lr,
)
if (i + 1) % rolling_loss_step == 0:
logger.info("Train batch %d", data_iteration)
latest_rolling_train_av_loss = rolling_train_loss / rolling_loss_step
logger.info(
"Avg. loss per last %d batches: %f",
rolling_loss_step,
latest_rolling_train_av_loss,
)
rolling_train_loss = 0.0
if global_step % eval_step == 0:
logger.info(
"Validation: Epoch: %d Step: %d/%d",
epoch,
data_iteration,
epoch_batches,
)
self.validate_and_save(epoch, train_data_iterator.get_iteration(), scheduler)
self.reader.train()
epoch_loss = (epoch_loss / epoch_batches) if epoch_batches > 0 else 0
logger.info("Av Loss per epoch=%f", epoch_loss)
return global_step
def _save_checkpoint(self, scheduler, epoch: int, offset: int) -> str:
cfg = self.cfg
model_to_save = get_model_obj(self.reader)
cp = os.path.join(
cfg.output_dir,
cfg.checkpoint_file_name + "." + str(epoch) + ("." + str(offset) if offset > 0 else ""),
)
meta_params = get_encoder_params_state_from_cfg(cfg)
state = CheckpointState(
model_to_save.state_dict(),
self.optimizer.state_dict(),
scheduler.state_dict(),
offset,
epoch,
meta_params,
)
torch.save(state._asdict(), cp)
return cp
def _load_saved_state(self, saved_state: CheckpointState):
epoch = saved_state.epoch
offset = saved_state.offset
if offset == 0: # epoch has been completed
epoch += 1
logger.info("Loading checkpoint @ batch=%s and epoch=%s", offset, epoch)
self.start_epoch = epoch
self.start_batch = offset
model_to_load = get_model_obj(self.reader)
if saved_state.model_dict:
logger.info("Loading model weights from saved state ...")
model_to_load.load_state_dict(saved_state.model_dict, strict=False)
logger.info("Loading saved optimizer state ...")
if saved_state.optimizer_dict:
self.optimizer.load_state_dict(saved_state.optimizer_dict)
self.scheduler_state = saved_state.scheduler_dict
def _get_best_prediction(
self,
start_logits,
end_logits,
relevance_logits,
samples_batch: List[ReaderSample],
passage_thresholds: List[int] = None,
) -> List[ReaderQuestionPredictions]:
cfg = self.cfg
max_answer_length = cfg.max_answer_length
questions_num, passages_per_question = relevance_logits.size()
_, idxs = torch.sort(
relevance_logits,
dim=1,
descending=True,
)
batch_results = []
for q in range(questions_num):
sample = samples_batch[q]
non_empty_passages_num = len(sample.passages)
nbest = []
for p in range(passages_per_question):
passage_idx = idxs[q, p].item()
if passage_idx >= non_empty_passages_num: # empty passage selected, skip
continue
reader_passage = sample.passages[passage_idx]
sequence_ids = reader_passage.sequence_ids
sequence_len = sequence_ids.size(0)
# assuming question & title information is at the beginning of the sequence
passage_offset = reader_passage.passage_offset
p_start_logits = start_logits[q, passage_idx].tolist()[passage_offset:sequence_len]
p_end_logits = end_logits[q, passage_idx].tolist()[passage_offset:sequence_len]
ctx_ids = sequence_ids.tolist()[passage_offset:]
best_spans = get_best_spans(
self.tensorizer,
p_start_logits,
p_end_logits,
ctx_ids,
max_answer_length,
passage_idx,
relevance_logits[q, passage_idx].item(),
top_spans=10,
)
nbest.extend(best_spans)
if len(nbest) > 0 and not passage_thresholds:
break
if passage_thresholds:
passage_rank_matches = {}
for n in passage_thresholds:
curr_nbest = [pred for pred in nbest if pred.passage_index < n]
passage_rank_matches[n] = curr_nbest[0]
predictions = passage_rank_matches
else:
if len(nbest) == 0:
predictions = {passages_per_question: SpanPrediction("", -1, -1, -1, "")}
else:
predictions = {passages_per_question: nbest[0]}
batch_results.append(ReaderQuestionPredictions(sample.question, predictions, sample.answers))
return batch_results
def _calc_loss(self, input: ReaderBatch) -> torch.Tensor:
cfg = self.cfg
input = ReaderBatch(**move_to_device(input._asdict(), cfg.device))
attn_mask = self.tensorizer.get_attn_mask(input.input_ids)
questions_num, passages_per_question, _ = input.input_ids.size()
if self.reader.training:
loss = self.reader(
input.input_ids,
attn_mask,
input.token_type_ids,
input.start_positions,
input.end_positions,
input.answers_mask,
)
else:
# TODO: remove?
with torch.no_grad():
start_logits, end_logits, rank_logits = self.reader(input.input_ids, attn_mask, input.token_type_ids)
loss = compute_loss(
input.start_positions,
input.end_positions,
input.answers_mask,
start_logits,
end_logits,
rank_logits,
questions_num,
passages_per_question,
)
if cfg.n_gpu > 1:
loss = loss.mean()
if cfg.train.gradient_accumulation_steps > 1:
loss = loss / cfg.train.gradient_accumulation_steps
return loss
def _save_predictions(self, out_file: str, prediction_results: List[ReaderQuestionPredictions]):
logger.info("Saving prediction results to %s", out_file)
with open(out_file, "w", encoding="utf-8") as output:
save_results = []
for r in prediction_results:
save_results.append(
{
"question": r.id,
"gold_answers": r.gold_answers,
"predictions": [
{
"top_k": top_k,
"prediction": {
"text": span_pred.prediction_text,
"score": span_pred.span_score,
"relevance_score": span_pred.relevance_score,
"passage_idx": span_pred.passage_index,
"passage": self.tensorizer.to_string(span_pred.passage_token_ids),
},
}
for top_k, span_pred in r.predictions.items()
],
}
)
output.write(json.dumps(save_results, indent=4) + "\n")
@hydra.main(config_path="conf", config_name="extractive_reader_train_cfg")
def main(cfg: DictConfig):
if cfg.output_dir is not None:
os.makedirs(cfg.output_dir, exist_ok=True)
cfg = setup_cfg_gpu(cfg)
set_seed(cfg)
if cfg.local_rank in [-1, 0]:
logger.info("CFG (after gpu configuration):")
logger.info("%s", OmegaConf.to_yaml(cfg))
trainer = ReaderTrainer(cfg)
if cfg.train_files is not None:
trainer.run_train()
elif cfg.dev_files:
logger.info("No train files are specified. Run validation.")
trainer.validate()
else:
logger.warning("Neither train_file or (model_file & dev_file) parameters are specified. Nothing to do.")
if __name__ == "__main__":
logger.info("Sys.argv: %s", sys.argv)
hydra_formatted_args = []
# convert the cli params added by torch.distributed.launch into Hydra format
for arg in sys.argv:
if arg.startswith("--"):
hydra_formatted_args.append(arg[len("--") :])
else:
hydra_formatted_args.append(arg)
logger.info("Hydra formatted Sys.argv: %s", hydra_formatted_args)
sys.argv = hydra_formatted_args
main()