-
Notifications
You must be signed in to change notification settings - Fork 26
/
run_piqa.py
869 lines (744 loc) · 43 KB
/
run_piqa.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import collections
import logging
import json
import os
import random
from time import time
import h5py
from torch.optim import Adam
from tqdm import tqdm as tqdm_
import numpy as np
import torch
from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
from torch.utils.data.distributed import DistributedSampler
import tokenization
from bert import BertConfig
from optimization import BERTAdam
from phrase import BertPhraseModel
from pre import convert_examples_to_features, read_squad_examples, convert_documents_to_features, \
convert_questions_to_features, SquadExample, inject_noise_to_neg_features_list, sample_similar_questions
from post import write_predictions, write_hdf5, get_question_results as get_question_results_, \
convert_question_features_to_dataloader, write_question_results
from serve import serve
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger(__name__)
RawResult = collections.namedtuple("RawResult", ["unique_id", "all_logits", "filter_start_logits", "filter_end_logits"])
ContextResult = collections.namedtuple("ContextResult",
['unique_id', 'start', 'end', 'span_logits',
'filter_start_logits', 'filter_end_logits',
'sparse'])
def tqdm(*args, mininterval=5.0, **kwargs):
return tqdm_(*args, mininterval=mininterval, **kwargs)
def main():
parser = argparse.ArgumentParser()
## Required parameters
parser.add_argument('--mode', type=str, default='train')
parser.add_argument('--pause', type=int, default=0)
parser.add_argument('--iteration', type=str, default='1')
# Data paths
parser.add_argument('--data_dir', default='data/', type=str)
parser.add_argument("--train_file", default='train-v1.1.json', type=str,
help="SQuAD json for training. E.g., train-v1.1.json")
parser.add_argument("--predict_file", default='dev-v1.1.json', type=str,
help="SQuAD json for predictions. E.g., dev-v1.1.json or test-v1.1.json")
parser.add_argument('--gt_file', default='dev-v1.1.json', type=str, help='ground truth file needed for evaluation.')
# Metadata paths
parser.add_argument('--metadata_dir', default='metadata/', type=str)
parser.add_argument("--vocab_file", default='vocab.txt', type=str,
help="The vocabulary file that the BERT model was trained on.")
parser.add_argument("--bert_model_option", default='large_uncased', type=str,
help="model architecture option. [large_uncased] or [base_uncased]")
parser.add_argument("--bert_config_file", default='bert_config.json', type=str,
help="The config json file corresponding to the pre-trained BERT model. "
"This specifies the model architecture.")
parser.add_argument("--init_checkpoint", default='pytorch_model.bin', type=str,
help="Initial checkpoint (usually from a pre-trained BERT model).")
# Output and load paths
parser.add_argument("--output_dir", default='out/', type=str,
help="The output directory where the model checkpoints will be written.")
parser.add_argument("--dump_file", default='phrase.hdf5', type=str, help="dump output file.")
parser.add_argument("--question_emb_file", default='question.hdf5', type=str, help="question output file.")
parser.add_argument("--train_question_emb_file", default='train_question.hdf5', type=str,
help="question output file.")
parser.add_argument('--save_dir', default='save/', type=str)
parser.add_argument('--load_dir', default='save/', type=str)
# Local paths (if we want to run cmd)
parser.add_argument('--eval_script', default='evaluate-v1.1.py', type=str)
# Do's
parser.add_argument("--do_load", default=False, action='store_true', help='Do load. If eval, do load automatically')
parser.add_argument("--do_train", default=False, action='store_true', help="Whether to run training.")
parser.add_argument("--do_train_neg", default=False, action='store_true', help="Whether to run neg training.")
parser.add_argument("--do_train_filter", default=False, action='store_true', help='Train filter or not.')
parser.add_argument("--do_predict", default=False, action='store_true', help="Whether to run eval on the dev set.")
parser.add_argument('--do_eval', default=False, action='store_true')
parser.add_argument('--do_dump_question', default=False, action='store_true')
parser.add_argument('--do_dump', default=False, action='store_true')
parser.add_argument('--do_serve', default=False, action='store_true')
# Model options: if you change these, you need to train again
parser.add_argument("--do_case", default=False, action='store_true',
help="Whether to lower case the input text. Should be True for uncased "
"models and False for cased models.")
parser.add_argument('--phrase_size', default=961, type=int)
parser.add_argument('--metric', default='ip', type=str, help='ip | l2')
parser.add_argument("--use_sparse", default=False, action='store_true')
# GPU and memory related options
parser.add_argument("--max_seq_length", default=384, type=int,
help="The maximum total input sequence length after WordPiece tokenization. Sequences "
"longer than this will be truncated, and sequences shorter than this will be padded.")
parser.add_argument("--doc_stride", default=128, type=int,
help="When splitting up a long document into chunks, how much stride to take between chunks.")
parser.add_argument("--max_query_length", default=64, type=int,
help="The maximum number of tokens for the question. Questions longer than this will "
"be truncated to this length.")
parser.add_argument("--train_batch_size", default=12, type=int, help="Total batch size for training.")
parser.add_argument("--train_neg_batch_size", default=9, type=int, help="Total batch size for training.")
parser.add_argument("--predict_batch_size", default=16, type=int, help="Total batch size for predictions.")
parser.add_argument('--gradient_accumulation_steps',
type=int,
default=1,
help="Number of updates steps to accumulate before performing a backward/update pass.")
parser.add_argument('--optimize_on_cpu',
default=False,
action='store_true',
help="Whether to perform optimization and keep the optimizer averages on CPU")
parser.add_argument("--no_cuda",
default=False,
action='store_true',
help="Whether not to use CUDA when available")
parser.add_argument("--local_rank",
type=int,
default=-1,
help="local_rank for distributed training on gpus")
parser.add_argument('--fp16',
default=False,
action='store_true',
help="Whether to use 16-bit float precision instead of 32-bit")
# Training options: only effective during training
parser.add_argument("--learning_rate", default=3e-5, type=float, help="The initial learning rate for Adam.")
parser.add_argument("--num_train_epochs", default=2.0, type=float,
help="Total number of training epochs to perform.")
parser.add_argument("--num_train_neg_epochs", default=2.0, type=float,
help="Total number of neg training epochs to perform.")
parser.add_argument("--num_train_filter_epochs", default=1.0, type=float,
help="Total number of training epochs for filter to perform.")
parser.add_argument("--warmup_proportion", default=0.1, type=float,
help="Proportion of training to perform linear learning rate warmup for. E.g., 0.1 = 10% "
"of training.")
parser.add_argument("--save_checkpoints_steps", default=1000, type=int,
help="How often to save the model checkpoint.")
parser.add_argument("--iterations_per_loop", default=1000, type=int,
help="How many steps to make in each estimator call.")
# Prediction options: only effective during prediction
parser.add_argument("--n_best_size", default=20, type=int,
help="The total number of n-best predictions to generate in the nbest_predictions.json "
"output file.")
parser.add_argument("--max_answer_length", default=30, type=int,
help="The maximum length of an answer that can be generated. This is needed because the start "
"and end predictions are not conditioned on one another.")
# Index Options
parser.add_argument('--dtype', default='float32', type=str)
parser.add_argument('--filter_threshold', default=-1e9, type=float)
parser.add_argument('--compression_offset', default=-2, type=float)
parser.add_argument('--compression_scale', default=20, type=float)
parser.add_argument('--split_by_para', default=False, action='store_true')
# Serve Options
parser.add_argument('--port', default=9009, type=int)
# Others
parser.add_argument('--parallel', default=False, action='store_true')
parser.add_argument("--verbose_logging", default=False, action='store_true',
help="If true, all of the warnings related to data processing will be printed. "
"A number of warnings are expected for a normal SQuAD evaluation.")
parser.add_argument('--seed',
type=int,
default=42,
help="random seed for initialization")
parser.add_argument('--draft', default=False, action='store_true')
parser.add_argument('--draft_num_examples', type=int, default=12)
parser.add_argument('--train_word_emb', default=False, action='store_true')
args = parser.parse_args()
# Filesystem routines
class Processor(object):
def __init__(self, save_path):
self._save = None
self._load = None
self._save_path = save_path
def bind(self, save, load):
self._save = save
self._load = load
def save(self, checkpoint=None, save_fn=None, **kwargs):
path = os.path.join(self._save_path, str(checkpoint))
if save_fn is None:
self._save(path, **kwargs)
else:
save_fn(path, **kwargs)
def load(self, checkpoint, load_fn=None, session=None, **kwargs):
path = os.path.join(session, str(checkpoint), 'model.pt')
if load_fn is None:
self._load(path, **kwargs)
else:
load_fn(path, **kwargs)
processor = Processor(args.save_dir)
if not args.do_train:
args.do_load = True
# Configure paths
args.train_file = os.path.join(args.data_dir, args.train_file)
args.predict_file = os.path.join(args.data_dir, args.predict_file)
args.gt_file = os.path.join(args.data_dir, args.gt_file)
args.bert_config_file = os.path.join(args.metadata_dir, args.bert_config_file.replace(".json", "") +
"_" + args.bert_model_option + ".json")
args.init_checkpoint = os.path.join(args.metadata_dir, args.init_checkpoint.replace(".bin", "") +
"_" + args.bert_model_option + ".bin")
args.vocab_file = os.path.join(args.metadata_dir, args.vocab_file)
args.dump_file = os.path.join(args.output_dir, args.dump_file)
args.question_emb_file = os.path.join(args.output_dir, args.question_emb_file)
args.train_question_emb_file = os.path.join(args.output_dir, args.train_question_emb_file)
# Multi-GPU stuff
if args.local_rank == -1 or args.no_cuda:
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count()
else:
device = torch.device("cuda", args.local_rank)
n_gpu = 1
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
torch.distributed.init_process_group(backend='nccl')
logger.info("device %s n_gpu %d distributed training %r", device, n_gpu, bool(args.local_rank != -1))
if args.gradient_accumulation_steps < 1:
raise ValueError("Invalid gradient_accumulation_steps parameter: {}, should be >= 1".format(
args.gradient_accumulation_steps))
args.train_batch_size = int(args.train_batch_size / args.gradient_accumulation_steps)
args.train_neg_batch_size = int(args.train_neg_batch_size / args.gradient_accumulation_steps)
# Seed for reproducibility
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if n_gpu > 0:
torch.cuda.manual_seed_all(args.seed)
bert_config = BertConfig.from_json_file(args.bert_config_file)
if args.max_seq_length > bert_config.max_position_embeddings:
raise ValueError(
"Cannot use sequence length %d because the BERT model "
"was only trained up to sequence length %d" %
(args.max_seq_length, bert_config.max_position_embeddings))
if os.path.exists(args.output_dir) and os.listdir(args.output_dir):
# raise ValueError("Output directory () already exists and is not empty.")
pass
else:
os.makedirs(args.output_dir, exist_ok=True)
tokenizer = tokenization.FullTokenizer(vocab_file=args.vocab_file, do_lower_case=not args.do_case)
model = BertPhraseModel(
bert_config,
phrase_size=args.phrase_size,
metric=args.metric,
use_sparse=args.use_sparse
)
print('Number of model parameters:', sum(p.numel() for p in model.parameters()))
if not args.do_load and args.init_checkpoint is not None:
state_dict = torch.load(args.init_checkpoint, map_location='cpu')
# If below: for Korean BERT compatibility
if next(iter(state_dict)).startswith('bert.'):
state_dict = {key[len('bert.'):]: val for key, val in state_dict.items()}
state_dict = {key: val for key, val in state_dict.items() if key in model.encoder.bert_model.state_dict()}
model.encoder.bert.load_state_dict(state_dict)
if args.fp16:
model.half()
if not args.optimize_on_cpu:
model.to(device)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank)
elif args.parallel or n_gpu > 1:
model = torch.nn.DataParallel(model)
if args.do_load:
bind_model(processor, model)
processor.load(args.iteration, session=args.load_dir)
def is_param(name):
if not args.train_word_emb:
if name.endswith("encoder.bert.embeddings.word_embeddings.weight"):
print(f'freezeing {name}')
return False
return True
if args.do_train:
train_examples = read_squad_examples(
input_file=args.train_file, is_training=True, draft=args.draft, draft_num_examples=args.draft_num_examples)
num_train_steps = int(
len(train_examples) / args.train_batch_size / args.gradient_accumulation_steps * args.num_train_epochs)
no_decay = ['bias', 'gamma', 'beta']
optimizer_parameters = [
{'params': [p for n, p in model.named_parameters() if (n not in no_decay) and is_param(n)],
'weight_decay_rate': 0.01},
{'params': [p for n, p in model.named_parameters() if (n in no_decay) and is_param(n)],
'weight_decay_rate': 0.0}
]
optimizer = BERTAdam(optimizer_parameters,
lr=args.learning_rate,
warmup=args.warmup_proportion,
t_total=num_train_steps)
bind_model(processor, model, optimizer)
global_step = 0
train_features, train_features_ = convert_examples_to_features(
examples=train_examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride,
max_query_length=args.max_query_length,
is_training=True)
logger.info("***** Running training *****")
logger.info(" Num orig examples = %d", len(train_examples))
logger.info(" Num split examples = %d", len(train_features))
logger.info(" Batch size = %d", args.train_batch_size)
logger.info(" Num steps = %d", num_train_steps)
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
all_start_positions = torch.tensor([f.start_position for f in train_features], dtype=torch.long)
all_end_positions = torch.tensor([f.end_position for f in train_features], dtype=torch.long)
all_input_ids_ = torch.tensor([f.input_ids for f in train_features_], dtype=torch.long)
all_input_mask_ = torch.tensor([f.input_mask for f in train_features_], dtype=torch.long)
if args.fp16:
(all_input_ids, all_input_mask,
all_start_positions,
all_end_positions) = tuple(t.half() for t in (all_input_ids, all_input_mask,
all_start_positions, all_end_positions))
all_input_ids_, all_input_mask_ = tuple(t.half() for t in (all_input_ids_, all_input_mask_))
train_data = TensorDataset(all_input_ids, all_input_mask,
all_input_ids_, all_input_mask_,
all_start_positions, all_end_positions)
if args.local_rank == -1:
train_sampler = RandomSampler(train_data)
else:
train_sampler = DistributedSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)
model.train()
for epoch in range(int(args.num_train_epochs)):
for step, batch in enumerate(tqdm(train_dataloader, desc="Epoch %d" % (epoch + 1))):
batch = tuple(t.to(device) for t in batch)
(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions) = batch
loss, _ = model(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions)
if n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
loss.backward()
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.optimize_on_cpu:
model.to('cpu')
optimizer.step() # We have accumulated enought gradients
model.zero_grad()
if args.optimize_on_cpu:
model.to(device)
global_step += 1
processor.save(epoch + 1)
if args.do_dump_question:
question_examples = read_squad_examples(
question_only=True,
input_file=args.train_file, is_training=False, draft=args.draft,
draft_num_examples=args.draft_num_examples)
query_eval_features = convert_questions_to_features(
examples=question_examples,
tokenizer=tokenizer,
max_query_length=args.max_query_length)
question_dataloader = convert_question_features_to_dataloader(query_eval_features, args.fp16, args.local_rank,
args.predict_batch_size)
model.eval()
logger.info("Start embedding")
question_results = get_question_results_(question_examples, query_eval_features, question_dataloader, device,
model)
print('Writing %s' % args.train_question_emb_file)
write_question_results(question_results, query_eval_features, args.train_question_emb_file)
if args.do_train_neg:
train_examples = read_squad_examples(
input_file=args.train_file, is_training=True, draft=args.draft, draft_num_examples=args.draft_num_examples)
num_train_steps = int(
len(train_examples) / args.train_neg_batch_size / args.gradient_accumulation_steps *
args.num_train_neg_epochs)
no_decay = ['bias', 'gamma', 'beta']
optimizer_parameters = [
{'params': [p for n, p in model.named_parameters() if (n not in no_decay) and is_param(n)],
'weight_decay_rate': 0.01},
{'params': [p for n, p in model.named_parameters() if (n in no_decay) and is_param(n)],
'weight_decay_rate': 0.0}
]
optimizer = BERTAdam(optimizer_parameters,
lr=args.learning_rate,
warmup=args.warmup_proportion,
t_total=num_train_steps)
bind_model(processor, model, optimizer)
global_step = 0
train_features, train_features_ = convert_examples_to_features(
examples=train_examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride,
max_query_length=args.max_query_length,
is_training=True)
neg_train_features = sample_similar_questions(train_examples, train_features, args.train_question_emb_file,
cuda=not args.no_cuda)
# neg_train_features = random.sample(train_features, len(train_features))
neg_train_features = inject_noise_to_neg_features_list(neg_train_features,
noise_prob=0.2,
clamp=True, clamp_prob=0.1,
replace=True, replace_prob=0.1, unk_prob=0.1,
shuffle=True, shuffle_prob=0.1)
logger.info("***** Running training *****")
logger.info(" Num orig examples = %d", len(train_examples))
logger.info(" Num split examples = %d", len(train_features))
logger.info(" Batch size = %d", args.train_neg_batch_size)
logger.info(" Num steps = %d", num_train_steps)
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
all_start_positions = torch.tensor([f.start_position for f in train_features], dtype=torch.long)
all_end_positions = torch.tensor([f.end_position for f in train_features], dtype=torch.long)
all_input_ids_ = torch.tensor([f.input_ids for f in train_features_], dtype=torch.long)
all_input_mask_ = torch.tensor([f.input_mask for f in train_features_], dtype=torch.long)
all_neg_input_ids = torch.tensor([f.input_ids for f in neg_train_features], dtype=torch.long)
all_neg_input_mask = torch.tensor([f.input_mask for f in neg_train_features], dtype=torch.long)
if args.fp16:
(all_input_ids, all_input_mask,
all_start_positions,
all_end_positions) = tuple(t.half() for t in (all_input_ids, all_input_mask,
all_start_positions, all_end_positions))
all_input_ids_, all_input_mask_ = tuple(t.half() for t in (all_input_ids_, all_input_mask_))
all_neg_input_ids, all_neg_input_mask = tuple(t.half() for t in (all_neg_input_ids, all_neg_input_mask))
train_data = TensorDataset(all_input_ids, all_input_mask,
all_input_ids_, all_input_mask_,
all_start_positions, all_end_positions,
all_neg_input_ids, all_neg_input_mask)
if args.local_rank == -1:
train_sampler = RandomSampler(train_data)
else:
train_sampler = DistributedSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_neg_batch_size)
model.train()
for epoch in range(int(args.num_train_epochs)):
for step, batch in enumerate(tqdm(train_dataloader, desc="Epoch %d" % (epoch + 1))):
batch = tuple(t.to(device) for t in batch)
(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions,
neg_input_ids, neg_input_mask) = batch
loss, _ = model(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions,
neg_input_ids, neg_input_mask)
if n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
loss.backward()
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.optimize_on_cpu:
model.to('cpu')
optimizer.step() # We have accumulated enought gradients
model.zero_grad()
if args.optimize_on_cpu:
model.to(device)
global_step += 1
processor.save(epoch + 1)
if args.do_train_filter:
train_examples = read_squad_examples(
input_file=args.train_file, is_training=True, draft=args.draft, draft_num_examples=args.draft_num_examples)
num_train_steps = int(
len(
train_examples) / args.train_batch_size / args.gradient_accumulation_steps *
args.num_train_filter_epochs)
if args.parallel or n_gpu > 1:
optimizer = Adam(model.module.filter.parameters())
else:
optimizer = Adam(model.filter.parameters())
bind_model(processor, model, optimizer)
global_step = 0
train_features, train_features_ = convert_examples_to_features(
examples=train_examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride,
max_query_length=args.max_query_length,
is_training=True)
logger.info("***** Running filter training *****")
logger.info(" Num orig examples = %d", len(train_examples))
logger.info(" Num split examples = %d", len(train_features))
logger.info(" Batch size = %d", args.train_batch_size)
logger.info(" Num steps = %d", num_train_steps)
all_input_ids = torch.tensor([f.input_ids for f in train_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in train_features], dtype=torch.long)
all_start_positions = torch.tensor([f.start_position for f in train_features], dtype=torch.long)
all_end_positions = torch.tensor([f.end_position for f in train_features], dtype=torch.long)
all_input_ids_ = torch.tensor([f.input_ids for f in train_features_], dtype=torch.long)
all_input_mask_ = torch.tensor([f.input_mask for f in train_features_], dtype=torch.long)
if args.fp16:
(all_input_ids, all_input_mask,
all_start_positions,
all_end_positions) = tuple(t.half() for t in (all_input_ids, all_input_mask,
all_start_positions, all_end_positions))
all_input_ids_, all_input_mask_ = tuple(t.half() for t in (all_input_ids_, all_input_mask_))
train_data = TensorDataset(all_input_ids, all_input_mask,
all_input_ids_, all_input_mask_,
all_start_positions, all_end_positions)
if args.local_rank == -1:
train_sampler = RandomSampler(train_data)
else:
train_sampler = DistributedSampler(train_data)
train_dataloader = DataLoader(train_data, sampler=train_sampler, batch_size=args.train_batch_size)
model.train()
for epoch in range(int(args.num_train_filter_epochs)):
for step, batch in enumerate(tqdm(train_dataloader, desc="Epoch %d" % (epoch + 1))):
batch = tuple(t.to(device) for t in batch)
(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions) = batch
_, loss = model(input_ids, input_mask,
input_ids_, input_mask_,
start_positions, end_positions)
if n_gpu > 1:
loss = loss.mean() # mean() to average on multi-gpu.
if args.gradient_accumulation_steps > 1:
loss = loss / args.gradient_accumulation_steps
loss.backward()
if (step + 1) % args.gradient_accumulation_steps == 0:
if args.optimize_on_cpu:
model.to('cpu')
optimizer.step() # We have accumulated enought gradients
model.zero_grad()
if args.optimize_on_cpu:
model.to(device)
global_step += 1
processor.save(epoch + 1)
if args.do_predict:
eval_examples = read_squad_examples(
input_file=args.predict_file, is_training=False, draft=args.draft,
draft_num_examples=args.draft_num_examples)
eval_features, query_eval_features = convert_examples_to_features(
examples=eval_examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride,
max_query_length=args.max_query_length,
is_training=False)
logger.info("***** Running predictions *****")
logger.info(" Num orig examples = %d", len(eval_examples))
logger.info(" Num split examples = %d", len(eval_features))
logger.info(" Batch size = %d", args.predict_batch_size)
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
all_input_ids_ = torch.tensor([f.input_ids for f in query_eval_features], dtype=torch.long)
all_input_mask_ = torch.tensor([f.input_mask for f in query_eval_features], dtype=torch.long)
all_example_index = torch.arange(all_input_ids.size(0), dtype=torch.long)
if args.fp16:
(all_input_ids, all_input_mask, all_example_index) = tuple(t.half() for t in (all_input_ids, all_input_mask,
all_example_index))
all_input_ids_, all_input_mask_ = tuple(t.half() for t in (all_input_ids_, all_input_mask_))
eval_data = TensorDataset(all_input_ids, all_input_mask,
all_input_ids_, all_input_mask_,
all_example_index)
if args.local_rank == -1:
eval_sampler = SequentialSampler(eval_data)
else:
eval_sampler = DistributedSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=args.predict_batch_size)
model.eval()
logger.info("Start evaluating")
def get_results():
for (input_ids, input_mask, input_ids_, input_mask_, example_indices) in eval_dataloader:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
input_ids_ = input_ids_.to(device)
input_mask_ = input_mask_.to(device)
with torch.no_grad():
batch_all_logits, bs, be = model(input_ids, input_mask, input_ids_, input_mask_)
for i, example_index in enumerate(example_indices):
all_logits = batch_all_logits[i].detach().cpu().numpy()
filter_start_logits = bs[i].detach().cpu().numpy()
filter_end_logits = be[i].detach().cpu().numpy()
eval_feature = eval_features[example_index.item()]
unique_id = int(eval_feature.unique_id)
yield RawResult(unique_id=unique_id,
all_logits=all_logits,
filter_start_logits=filter_start_logits,
filter_end_logits=filter_end_logits)
output_prediction_file = os.path.join(args.output_dir, "predictions.json")
write_predictions(eval_examples, eval_features, get_results(),
args.max_answer_length,
not args.do_case, output_prediction_file, args.verbose_logging,
args.filter_threshold)
if args.do_eval:
command = "python %s %s %s" % (args.eval_script, args.gt_file, output_prediction_file)
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if args.do_dump_question:
question_examples = read_squad_examples(
question_only=True,
input_file=args.predict_file, is_training=False, draft=args.draft,
draft_num_examples=args.draft_num_examples)
query_eval_features = convert_questions_to_features(
examples=question_examples,
tokenizer=tokenizer,
max_query_length=args.max_query_length)
question_dataloader = convert_question_features_to_dataloader(query_eval_features, args.fp16, args.local_rank,
args.predict_batch_size)
model.eval()
logger.info("Start embedding")
question_results = get_question_results_(question_examples, query_eval_features, question_dataloader, device,
model)
print('Writing %s' % args.question_emb_file)
write_question_results(question_results, query_eval_features, args.question_emb_file)
if args.do_dump:
if ':' not in args.predict_file:
predict_files = [args.predict_file]
offsets = [0]
else:
dirname = os.path.dirname(args.predict_file)
basename = os.path.basename(args.predict_file)
start, end = list(map(int, basename.split(':')))
# skip files if possible
if os.path.exists(args.dump_file):
with h5py.File(args.dump_file, 'r') as f:
dids = list(map(int, f.keys()))
start = int(max(dids) / 1000)
print('%s exists; starting from %d' % (args.dump_file, start))
names = [str(i).zfill(4) for i in range(start, end)]
predict_files = [os.path.join(dirname, name) for name in names]
offsets = [int(each) * 1000 for each in names]
for offset, predict_file in zip(offsets, predict_files):
context_examples = read_squad_examples(
context_only=True,
input_file=predict_file, is_training=False, draft=args.draft,
draft_num_examples=args.draft_num_examples)
for example in context_examples:
example.doc_idx += offset
context_features = convert_documents_to_features(
examples=context_examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride)
logger.info("***** Running dumping on %s *****" % predict_file)
logger.info(" Num orig examples = %d", len(context_examples))
logger.info(" Num split examples = %d", len(context_features))
logger.info(" Batch size = %d", args.predict_batch_size)
all_input_ids = torch.tensor([f.input_ids for f in context_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in context_features], dtype=torch.long)
all_example_index = torch.arange(all_input_ids.size(0), dtype=torch.long)
if args.fp16:
all_input_ids, all_input_mask, all_example_index = tuple(
t.half() for t in (all_input_ids, all_input_mask, all_example_index))
context_data = TensorDataset(all_input_ids, all_input_mask, all_example_index)
if args.local_rank == -1:
context_sampler = SequentialSampler(context_data)
else:
context_sampler = DistributedSampler(context_data)
context_dataloader = DataLoader(context_data, sampler=context_sampler,
batch_size=args.predict_batch_size)
model.eval()
logger.info("Start dumping")
def get_context_results():
for (input_ids, input_mask, example_indices) in context_dataloader:
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
with torch.no_grad():
batch_start, batch_end, batch_span_logits, bs, be, batch_sparse = model(input_ids,
input_mask)
for i, example_index in enumerate(example_indices):
start = batch_start[i].detach().cpu().numpy().astype(args.dtype)
end = batch_end[i].detach().cpu().numpy().astype(args.dtype)
sparse = None
if batch_sparse is not None:
sparse = batch_sparse[i].detach().cpu().numpy().astype(args.dtype)
span_logits = batch_span_logits[i].detach().cpu().numpy().astype(args.dtype)
filter_start_logits = bs[i].detach().cpu().numpy().astype(args.dtype)
filter_end_logits = be[i].detach().cpu().numpy().astype(args.dtype)
context_feature = context_features[example_index.item()]
unique_id = int(context_feature.unique_id)
yield ContextResult(unique_id=unique_id,
start=start,
end=end,
span_logits=span_logits,
filter_start_logits=filter_start_logits,
filter_end_logits=filter_end_logits,
sparse=sparse)
t0 = time()
write_hdf5(context_examples, context_features, get_context_results(),
args.max_answer_length, not args.do_case, args.dump_file, args.filter_threshold,
args.verbose_logging,
offset=args.compression_offset, scale=args.compression_scale,
split_by_para=args.split_by_para,
use_sparse=args.use_sparse)
print('%s: %.1f mins' % (predict_file, (time() - t0) / 60))
if args.do_serve:
def get(text):
question_examples = [SquadExample(qas_id='serve', question_text=text)]
query_eval_features = convert_questions_to_features(
examples=question_examples,
tokenizer=tokenizer,
max_query_length=16)
question_dataloader = convert_question_features_to_dataloader(query_eval_features, args.fp16,
args.local_rank,
args.predict_batch_size)
model.eval()
question_results = get_question_results_(question_examples, query_eval_features, question_dataloader,
device, model)
question_result = next(iter(question_results))
out = question_result.start.tolist(), question_result.end.tolist(), question_result.span_logit.tolist()
return out
serve(get, args.port)
def bind_model(processor, model, optimizer=None):
def save(filename, save_model=True, saver=None, **kwargs):
if not os.path.exists(filename):
os.makedirs(filename)
if save_model:
state = {'model': model.state_dict(), 'optimizer': optimizer.state_dict()}
model_path = os.path.join(filename, 'model.pt')
dummy_path = os.path.join(filename, 'dummy')
torch.save(state, model_path)
with open(dummy_path, 'w') as fp:
json.dump([], fp)
print('Model saved at %s' % model_path)
if saver is not None:
saver(filename)
def load(filename, load_model=True, loader=None, **kwargs):
if load_model:
# print('%s: %s' % (filename, os.listdir(filename)))
model_path = os.path.join(filename, 'model.pt')
if not os.path.exists(model_path): # for compatibility
model_path = filename
state = torch.load(model_path, map_location='cpu')
try:
model.load_state_dict(state['model'])
if optimizer is not None:
optimizer.load_state_dict(state['optimizer'])
except:
# Backward compatibility
model.load_state_dict(load_backward(state), strict=False)
print('Model loaded from %s' % model_path)
if loader is not None:
loader(filename)
processor.bind(save=save, load=load)
def load_backward(state):
new_state = collections.OrderedDict()
for key, val in state.items():
multi = False
if key.startswith('module.'):
multi = True
key = key[len('module.'):]
if key == 'true_help':
continue
if key.startswith('bert_q.'):
continue
if key.startswith('linear.'):
continue
if key.startswith('bert.'):
key = 'encoder.' + key
if multi:
key = 'module.' + key
new_state[key] = val
return new_state
if __name__ == "__main__":
main()