-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patheval_lm-trime.py
341 lines (279 loc) · 11.4 KB
/
eval_lm-trime.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
#!/usr/bin/env python3 -u
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
"""
Evaluate the perplexity of a trained language model.
"""
import logging
import math
import os
import json
import torch
import numpy as np
from fairseq import checkpoint_utils, options, progress_bar, tasks, utils
from fairseq.data import LMContextWindowDataset
from fairseq.meters import StopwatchMeter, TimeMeter
from fairseq.sequence_scorer import SequenceScorer
from fairseq.knnlm import KNN_Dstore
from collections import Counter
logging.basicConfig(
format='%(asctime)s | %(levelname)s | %(name)s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO,
)
logger = logging.getLogger('fairseq_cli.eval_lm')
class WordStat(object):
def __init__(self, word, is_bpe):
self.word = word
self.is_bpe = is_bpe
self.log_prob = 0
self.next_word_prob = 0
self.count = 0
self.missing_next_words = 0
def add(self, log_prob, next_word_prob):
""" increments counters for the sum of log probs of current word and next
word (given context ending at current word). Since the next word might be at the end of the example,
or it might be not counted because it is not an ending subword unit,
also keeps track of how many of those we have seen """
if next_word_prob is not None:
self.next_word_prob += next_word_prob
else:
self.missing_next_words += 1
self.log_prob += log_prob
self.count += 1
def __str__(self):
return '{}\t{}\t{}\t{}\t{}\t{}'.format(self.word, self.count, self.log_prob, self.is_bpe,
self.next_word_prob, self.count - self.missing_next_words)
def bucket_tokens(cnt, tot, dictionary, num_buckets):
tid_to_bid = dict()
if num_buckets == 10:
accum = 0
cur_bid = 0
num_types = 0
next_bar = tot / num_buckets
for i in range(len(dictionary)):
tid_to_bid[i] = cur_bid
accum += cnt[i]
if cnt[i] != 0:
num_types += 1
if accum >= next_bar:
cur_bid += 1
next_bar += tot / num_buckets
num_types = 0
elif num_buckets == 4:
for i in range(len(dictionary)):
if cnt[i] > 10000:
tid_to_bid[i] = 0
elif cnt[i] > 1000:
tid_to_bid[i] = 1
elif cnt[i] > 100:
tid_to_bid[i] = 2
else:
tid_to_bid[i] = 3
elif num_buckets == 5:
for i in range(len(dictionary)):
if cnt[i] > 10000:
tid_to_bid[i] = 0
elif cnt[i] > 1000:
tid_to_bid[i] = 1
elif cnt[i] > 100:
tid_to_bid[i] = 2
elif cnt[i] > 10:
tid_to_bid[i] = 3
else:
tid_to_bid[i] = 4
return tid_to_bid
def main(parsed_args):
assert parsed_args.path is not None, '--path required for evaluation!'
utils.import_user_module(parsed_args)
logger.info(parsed_args)
use_cuda = torch.cuda.is_available() and not parsed_args.cpu
task = tasks.setup_task(parsed_args)
# Load ensemble
logger.info('loading model(s) from {}'.format(parsed_args.path))
models, args = checkpoint_utils.load_model_ensemble(
parsed_args.path.split(os.pathsep),
arg_overrides=eval(parsed_args.model_overrides),
task=task,
)
for arg in vars(parsed_args).keys():
if arg not in {
'self_target', 'future_target', 'past_target', 'tokens_per_sample',
'output_size_dictionary', 'add_bos_token',
}:
setattr(args, arg, getattr(parsed_args, arg))
args.keep_order = False
# reduce tokens per sample by the required context window size
print('Tokens per sample:', args.tokens_per_sample)
print('Context window:', args.context_window)
args.tokens_per_sample -= args.context_window
task = tasks.setup_task(args)
# Load dataset splits
task.load_dataset(args.gen_subset)
dataset = task.dataset(args.gen_subset)
if args.context_window > 0:
print('Building LMContextWindowDataset w/ tokens_per_sample = %d'%args.tokens_per_sample)
dataset = LMContextWindowDataset(
dataset=dataset,
tokens_per_sample=args.tokens_per_sample,
context_window=args.context_window,
pad_idx=task.source_dictionary.pad(),
)
max_length = 0
min_length = 512000
tot = 0
cnt = Counter()
for sample in dataset:
max_length = max(sample['source'].shape[0], max_length)
min_length = min(sample['source'].shape[0], min_length)
for t in sample['source']:
cnt[int(t.item())] += 1
tot += sample['source'].shape[0]
logger.info('{} {} {} examples'.format(args.data, args.gen_subset, len(dataset)))
logger.info('Per example, max: {}, min: {}, tot: {}'.format(max_length, min_length, tot))
num_buckets = 5
tid_to_bid = bucket_tokens(cnt, tot, task.dictionary, num_buckets)
# Optimize ensemble for generation and set the source and dest dicts on the model (required by scorer)
for model in models:
model.make_generation_fast_()
if args.fp16:
model.half()
if use_cuda:
model.cuda()
assert len(models) > 0
logger.info('num. model params: {}'.format(sum(p.numel() for p in models[0].parameters())))
logger.info('Args: max_tokens = {}, max_sentences = {}, num_shards = {}, shard_id = {}'.format(args.max_tokens, args.max_sentences, args.num_shards, args.shard_id))
gen_timer = StopwatchMeter()
scorer = SequenceScorer(task.target_dictionary, args.softmax_batch, args=args)
score_sum = 0.
count = 0
if args.remove_bpe is not None:
if args.remove_bpe == 'sentencepiece':
raise NotImplementedError
else:
bpe_cont = args.remove_bpe.rstrip()
bpe_toks = {
i
for i in range(len(task.source_dictionary))
if task.source_dictionary[i].endswith(bpe_cont)
}
bpe_len = len(bpe_cont)
else:
bpe_toks = None
bpe_len = 0
word_stats = dict()
if args.knnlm and args.save_knnlm_dstore:
raise ValueError("Cannot use knnlm while trying to build the datastore!")
if args.use_interp or args.use_external:
knn_dstore = KNN_Dstore(args)
logger.info('Finished reading KNN Dstore')
else:
knn_dstore = None
def eval_temp(temp):
print('TEMP = ', temp)
args.softmax_temp = temp
if args.knnlm:
knn_dstore.temp = temp
score_sum = 0.
count = 0
token_score = []
itr = task.get_batch_iterator(
dataset=dataset,
max_tokens=args.max_tokens or 36000,
max_sentences=args.max_sentences,
max_positions=utils.resolve_max_positions(*[
model.max_positions() for model in models
]),
ignore_invalid_inputs=True,
num_shards=args.num_shards,
shard_id=args.shard_id,
num_workers=args.num_workers,
).next_epoch_itr(shuffle=False)
with progress_bar.build_progress_bar(args, itr) as t:
wps_meter = TimeMeter()
dstore_idx = 0
for ex_i, sample in enumerate(t):
if dstore_idx >= args.dstore_size:
logger.info('Stored %d indexes, early stop now!'%(dstore_idx))
break
if 'net_input' not in sample:
continue
sample = utils.move_to_cuda(sample) if use_cuda else sample
gen_timer.start()
if args.mode == 'lm':
hypos = scorer.generate(models, sample)
elif args.mode in ['gn', 'li', 'gnli']:
assert (not args.mode == 'gnli') or args.use_external
hypos = scorer.trime_generate(models, sample, dstore=knn_dstore, mode=args.mode,
use_local=args.use_local, use_long=args.use_long,
use_external=args.use_external, use_interp=args.use_interp)
else:
raise NotImplementedError
gen_timer.stop(sample['ntokens'])
for i, hypos_i in enumerate(hypos):
hypo = hypos_i[0]
sample_id = sample['id'][i]
tokens = hypo['tokens']
tgt_len = tokens.numel()
pos_scores = hypo['positional_scores'].float()
if args.add_bos_token:
assert hypo['tokens'][0].item() == task.target_dictionary.bos()
tokens = tokens[1:]
pos_scores = pos_scores[1:]
skipped_toks = 0
if bpe_toks is not None:
for i in range(tgt_len - 1):
if tokens[i].item() in bpe_toks:
skipped_toks += 1
pos_scores[i + 1] += pos_scores[i]
pos_scores[i] = 0
score_sum += pos_scores.sum().cpu()
count += pos_scores.numel() - skipped_toks
for tk, ps in zip(tokens, pos_scores):
token_score.append((tk.item(), ps.item()))
wps_meter.update(sample['ntokens'])
t.log({'wps': round(wps_meter.avg)})
avg_nll_loss = -score_sum / count / math.log(2) # convert to base 2
logger.info('Evaluated {} tokens in {:.1f}s ({:.2f} tokens/s)'.format(
gen_timer.n, gen_timer.sum, 1. / gen_timer.avg
))
logger.info('Loss (base 2): {:.4f}, Perplexity: {:.2f}'.format(
avg_nll_loss, 2**avg_nll_loss
))
print('Evaluated {} tokens in {:.1f}s ({:.2f} tokens/s)'.format(
gen_timer.n, gen_timer.sum, 1. / gen_timer.avg
))
print('Loss (base 2): {:.4f}, Perplexity: {:.2f}'.format(
avg_nll_loss, 2**avg_nll_loss
))
print(avg_nll_loss, 2**avg_nll_loss)
ppl = (2**avg_nll_loss).cpu().item()
b_nll = {x: 0.0 for x in range(num_buckets)}
b_tot = {x: 0 for x in range(num_buckets)}
for tk, ps in token_score:
bid = tid_to_bid[tk]
b_nll[bid] += ps
b_tot[bid] += 1
# output ppl of each freq buckets
for i in range(num_buckets):
avg_nll_loss = -b_nll[i] / b_tot[i] / math.log(2)
_ppl = 2**avg_nll_loss
# print(i, b_tot[i], _ppl)
return ppl
best_v = eval_temp(args.softmax_temp)
best_t = args.softmax_temp
if args.eval_file is not None:
eval_res = {'perplexity': best_v, 'temp': best_t, 'loss': math.log2(best_v)}
with open(args.eval_file, 'w') as f:
json.dump(eval_res, f)
if args.output_word_stats:
for ws in sorted(word_stats.values(), key=lambda x: x.count, reverse=True):
logger.info(ws)
def cli_main():
parser = options.get_eval_lm_parser()
args = options.parse_args_and_arch(parser)
main(args)
if __name__ == '__main__':
cli_main()