-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
362 lines (303 loc) · 15.3 KB
/
inference.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
"""
Load models in given folder.
Inference the test set using each model.
Save all the decoding results, attention weights(pkl).
"""
import numpy as np
from transformers import BertTokenizer
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
import argparse
import os
import pickle
import random
import re
from sum_dist.configs import MConfigs
from sum_dist.utils.evaluate import RougeCalculator
from sum_dist.utils.data.collate_fn import InitCollate
from sum_dist.models.encoder import TransformerEncoder
from sum_dist.models.decoder import TransformerDecoder
from sum_dist.models.seq2seq import Seq2seqModel
from sum_dist.trainer import Trainer
from sum_dist.utils.parse import str2bool
from sum_dist.utils.data.cnndm import DatasetCNNDM
from sum_dist.utils.data.xsum import DatasetXSUM
from sum_dist.utils.data.mlsum import DatasetMLSUMde, DatasetMLSUMes, DatasetMLSUMru
import sum_dist.utils.logging as logging
logger = logging.get_logger(__name__)
def main():
parser = argparse.ArgumentParser()
# run time settings
parser.add_argument('-dataset', type=str, nargs='?', default='cnndm', choices=['cnndm', 'xsum', 'mlsum_de', 'mlsum_es', 'mlsum_ru', 'wiki_en', 'arxiv'])
parser.add_argument('-exp_name', type=str, nargs='?', default='transformer22/lg/window5_s1-mask-my_loss_masking_pos-span_concat')
parser.add_argument('-run_attn', type=str2bool, nargs='?', const=True, default=True)
parser.add_argument('-filter_len', type=int, nargs='?', default=200)
parser.add_argument('-decoding_max_len', type=int, nargs='?', default=500)
parser.add_argument('-decoding_target_seq_len', type=int, nargs='?', default=100)
parser.add_argument('-decoding_times', type=int, nargs='?', default=2, choices=[1, 2])
parser.add_argument('-num_data_test', nargs='?', default='')
parser.add_argument('-use_high_rouge', type=str2bool, nargs='?', const=True, default=False)
parser.add_argument('-use_high_freq', type=str2bool, nargs='?', const=True, default=False)
parser.add_argument('-similarity_threshold', type=float, nargs='?', default=0.5)
parser.add_argument('-write_source', type=str2bool, nargs='?', const=True, default=True)
# training settings
parser.add_argument('-batch_size', type=int, nargs='?', default=1)
parser.add_argument('-device', type=str, nargs='?', default='cuda:0')
# training paths
parser.add_argument('-load_config_dir', type=str, nargs='?', default=None)
# rouge paths
parser.add_argument('-prediction_file_prefix', type=str, nargs='?', default='prediction')
parser.add_argument('-target_file_prefix', type=str, nargs='?', default='gold')
# dataset paths
parser.add_argument('-cnn_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/cnndm-bert-ann.pkl')
parser.add_argument('-xsum_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/xsum-bert-ann.pkl')
parser.add_argument('-mlsum_de_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/mlsum_de-bert-ann.pkl')
parser.add_argument('-mlsum_es_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/mlsum_es-bert-ann.pkl')
parser.add_argument('-mlsum_ru_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/mlsum_ru-bert-ann.pkl')
parser.add_argument('-arxiv_dataset_dir', type=str, nargs='?', default='./sum_dist/data/arxiv_data/arxiv-dataset/arxiv-dataset')
parser.add_argument('-arxiv_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/arxiv-bert-ann.pkl')
parser.add_argument('-wiki_en_ann_pkl_dir', type=str, nargs='?', default='./sum_dist/data/preprocess/wiki_en-bert-ann.pkl')
# inference (specify if needed)
parser.add_argument('-decoding_method', type=str, nargs='?', default=None)
parser.add_argument('-k', type=int, nargs='?', default=None)
parser.add_argument('-beam_size', type=int, nargs='?', default=None)
args = parser.parse_args()
logger.info(args)
device = torch.device(args.device)
logger.info(f'Using device: {device}')
"""
Set once.
"""
# set dataset
split = 'test'
num_data = args.num_data_test
if args.dataset == 'cnndm':
dataset = DatasetCNNDM(
dataset_pkl_path=None,
ann_pkl_path=args.cnn_ann_pkl_dir,
split=split,
num_data=num_data
)
elif args.dataset == 'xsum':
dataset = DatasetXSUM(
dataset_pkl_path=None,
ann_pkl_path=args.xsum_ann_pkl_dir,
split=split,
num_data=num_data
)
elif args.dataset == 'mlsum_de':
dataset = DatasetMLSUMde(
dataset_pkl_path=None,
ann_pkl_path=args.mlsum_de_ann_pkl_dir,
num_data=num_data,
split=split)
elif args.dataset == 'mlsum_es':
dataset = DatasetMLSUMes(
dataset_pkl_path=None,
ann_pkl_path=args.mlsum_es_ann_pkl_dir,
num_data=num_data,
split=split)
elif args.dataset == 'mlsum_ru':
dataset = DatasetMLSUMru(
dataset_pkl_path=None,
ann_pkl_path=args.mlsum_ru_ann_pkl_dir,
num_data=num_data,
split=split)
logger.info('Loading dataset done.')
# make output dir
if not os.path.exists(f'./sum_dist/output/inference/{args.exp_name}'):
os.makedirs(f'./sum_dist/output/inference/{args.exp_name}')
# write all(unsampled complete) input & target
if args.write_source:
split_filename = 'test'
output_source_filename = f'./sum_dist/output/inference/{args.exp_name}/source_all-{split_filename}.txt'
output_target_filename = f'./sum_dist/output/inference/{args.exp_name}/target_all-{split_filename}.txt'
with open(output_source_filename, 'w') as source_f, \
open(output_target_filename, 'w') as target_f:
for article_ind in range(len(dataset)):
instance = dataset[article_ind]
if len(instance['article']) < 50:
continue
source_f.write(instance['article'][instance['start_idx']:].replace('\n', ' '))
target_f.write(instance['summary'].strip('\n').replace('\n', ' [NEWLINE] '))
if article_ind < len(dataset) - 1:
source_f.write('\n')
target_f.write('\n')
# list all candidate checkpoints
checkpoint_idx_lst = [int(f[f.find('_')+1:f.find('.')]) for f in os.listdir(f'./sum_dist/checkpoint/{args.exp_name}') if os.path.isfile(os.path.join(f'./sum_dist/checkpoint/{args.exp_name}', f))]
checkpoint_idx_lst = sorted(checkpoint_idx_lst)
for checkpoint_idx in tqdm(checkpoint_idx_lst):
"""
Set every time loading new checkpoint.
"""
# set rouge calculator
rouge_calculator = RougeCalculator(
prediction_dir=f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}/prediction',# args.prediction_dest,
gold_dir=f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}/gold', # args.target_dest,
prediction_prefix=args.prediction_file_prefix,
gold_prefix=args.target_file_prefix)
logger.info('Setting ROUGE calculator done.')
config = MConfigs()
tokenizer = None
collate_fn = None
encoder = None
decoder = None
# load config
if f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt' is not None and os.path.exists(f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt'):
checkpoint = torch.load(f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt')
config = checkpoint['config']
logger.info(f'Load checkpoint config from: ./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt')
if args.load_config_dir is not None and os.path.exists(args.load_config_dir):
config = config.load_json(load_dir=args.load_config_dir)
logger.info(f'Load json config from: {args.load_config_dir}')
# TODO: here
config.update({
'decoding_target_seq_len': args.decoding_target_seq_len,
'decoding_times': args.decoding_times,
})
if args.decoding_method:
config.update({"decoding_methods": [args.decoding_method]})
if args.k:
config.update({"k": args.k})
if args.beam_size:
config.update({"beam_size": args.beam_size})
if 'seed' not in config.__dict__.keys():
config.update({'seed': 37})
logger.info(config.__dict__)
# set seed
torch.manual_seed(config.seed)
random.seed(config.seed)
np.random.seed(config.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# set encoder
tokenizer = BertTokenizer.from_pretrained(config.bert_version)
collate_fn = InitCollate(
tokenizer=tokenizer,
encoder_max_seq_len=config.seq_len,
decoder_max_seq_len=args.decoding_max_len,
target_seq_len=args.decoding_target_seq_len,
encoder_sampling_len_levels=config.sampling_length_levels,
cur_encoder_sampling_len_level=config.cur_sampling_length_level,
inference_mode=True)
encoder = TransformerEncoder(
embedding_dim=config.word_embed_size,
num_layer=config.encoder_num_layer,
num_head=config.encoder_num_head,
dim_feedforward=config.encoder_ff_embed_size,
decoder_dropout=config.encoder_dropout,
activation=config.encoder_activation,
num_embeddings=len(tokenizer),
embeddings=None)
# set decoder
decoder = TransformerDecoder(
encoder_embed_size=config.repr_embed_size,
vocab_size=len(tokenizer),
num_layer=config.decoder_num_layer,
num_head=config.decoder_num_head,
dim_feedforward=config.decoder_ff_embed_size,
decoder_dropout=config.decoder_dropout,
pos_dropout=config.decoder_position_dropout,
pos_max_len=config.decoder_position_max_len,
activation=config.decoder_activation)
logger.info('Setting encoder/decoder done.')
# set model & optimizer
encoder_out_embed_size = config.repr_embed_size
if config.span_aggregation_choice == 'cat':
encoder_out_embed_size = config.repr_embed_size*2
model = Seq2seqModel(
word_embed_size=config.word_embed_size,
vocab_size=len(tokenizer),
encoder=encoder,
encoder_out_embed_size=encoder_out_embed_size,
window_size=config.window_size,
slide_step=config.slide_step,
decoder=decoder,
decoder_in_embed_size=config.repr_embed_size,
device=device,
span_aggregation_choice=config.span_aggregation_choice,
masking_ratio=config.masking_ratio_levels[config.cur_masking_ratio_level],
masking_weight=config.masking_weight_levels[config.cur_masking_weight_level],
logger=logger,
).to(device)
# load checkpoint
if f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt' is not None and os.path.exists(f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt'):
checkpoint = torch.load(f'./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt')
cur_step = checkpoint['step']
model.load_state_dict(checkpoint['model'])
logger.info(f'Load model from: ./sum_dist/checkpoint/{args.exp_name}/checkpoint_{checkpoint_idx}.pt')
logger.info('Loading model done.')
# set data loader
data_loader_test = DataLoader(
dataset=dataset,
batch_size=args.batch_size,
collate_fn=collate_fn)
logger.info('Setting data loader done.')
# make output dir
if not os.path.exists(f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}'):
os.makedirs(f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}')
# write truncated source
if args.write_source:
split = 'test'
output_truncated_source_filename = f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}/source_truncated-{split}.txt'
with open(output_truncated_source_filename, 'w') as source_f:
write_newline = False
for batch in data_loader_test:
for instance_ids in batch['input_ids']:
article = tokenizer.decode(instance_ids, skip_special_tokens=True)
if write_newline:
source_f.write('\n')
source_f.write(article)
write_newline = True
del article
# set trainer
trainer = Trainer(
config=config,
model=model,
tokenizer=tokenizer,
rouge_calculator=rouge_calculator,
log_dir=f'./sum_dist/logs/inference/{args.exp_name}',
logger=logger,
device=device)
logger.info('Setting trainer done.')
logger.info('Start inference...')
# inference
logger.info('Run test...')
# calculate decoding result & attn
test_results, test_attn_results, test_encoder_spans_for_attn = trainer.inference(
data_loader=data_loader_test,
batch_size=args.batch_size,
max_len=args.decoding_max_len,
final_decode_len=args.decoding_target_seq_len,
decode_times=config.decoding_times,
return_attn=args.run_attn,
use_high_rouge=args.use_high_rouge,
use_high_freq=args.use_high_freq,
similarity_threshold=args.similarity_threshold,
spacy_model_ver=config.preprocess_spacy_model,
filter_len=args.filter_len)
# save decoding results
output_prediction_filename = f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}/prediction_all-decode{args.decoding_times}-test.txt'
cleaned_test_results = []
with open(output_prediction_filename, 'w') as f:
for pred_ind, pred_article in enumerate(tqdm(test_results)):
processed_article = pred_article.replace('[CLS]', '')
processed_article = pred_article.replace('[PAD]', '')
processed_article = re.sub(' +', ' ', pred_article)
f.write(processed_article)
cleaned_test_results.append(processed_article)
if pred_ind < len(test_results) - 1:
f.write('\n')
if args.run_attn:
# save attn weight pkl
pickle_file_path = f'./sum_dist/output/inference/{args.exp_name}/checkpoint_{checkpoint_idx}/attn_weight-decode{args.decoding_times}-test.pkl'
with open(pickle_file_path, 'wb') as handle:
pickle.dump({
'weights': test_attn_results,
'tokens': test_encoder_spans_for_attn,
}, handle)
logger.info(f'Saving attention weight value in: {pickle_file_path}')
return
if __name__ == '__main__':
main()