-
Notifications
You must be signed in to change notification settings - Fork 211
/
zero-shot-cls.py
478 lines (386 loc) · 18.2 KB
/
zero-shot-cls.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
import os
import numpy as np
import torch
import torch.nn.functional as F
import tqdm
from arguments import get_args
from data_utils.tokenization_gpt2 import GPT2Tokenizer
import mpu
import json
from data.samplers import DistributedBatchSampler, RandomSampler
from torch.utils.data import TensorDataset
from generate_samples import *
def get_batch(context_tokens, args):
tokens = context_tokens
tokens = tokens.view(args.batch_size, -1).contiguous()
# Get the masks and postition ids.
attention_mask, loss_mask, position_ids = get_masks_and_position_ids(
tokens,
args.eod_token,
args.reset_position_ids,
args.reset_attention_mask)
return tokens, attention_mask, position_ids
def top_k_logits(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
# This function has been mostly taken from huggingface conversational ai code at
# https://medium.com/huggingface/how-to-build-a-state-of-the-art-conversational-ai-with-transfer-learning-2d818ac26313
if top_k > 0:
# Remove all tokens with a probability less than the last token of the top-k
indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
logits[indices_to_remove] = filter_value
if top_p > 0.0:
#convert to 1D
logits=logits.view(logits.size()[1]).contiguous()
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
# Remove tokens with cumulative probability above the threshold
sorted_indices_to_remove = cumulative_probs > top_p
# Shift the indices to the right to keep also the first token above the threshold
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove]
logits[indices_to_remove] = filter_value
#going back to 2D
logits=logits.view(1, -1).contiguous()
return logits
def prepare_tokenizer(args):
tokenizer_args = {
'tokenizer_type': args.tokenizer_type,
'corpus': None,
'model_path': args.tokenizer_path,
'vocab_size': args.vocab_size,
'model_type': args.tokenizer_model_type,
'cache_dir': args.cache_dir}
tokenizer = make_tokenizer(**tokenizer_args)
args.tokenizer_num_tokens = tokenizer.num_tokens
args.tokenizer_num_type_tokens = tokenizer.num_type_tokens
args.eod_token = tokenizer.get_command('eos').Id
after = tokenizer.num_tokens
while after % mpu.get_model_parallel_world_size() != 0:
after += 1
args.vocab_size = after
print("prepare tokenizer done", flush=True)
return tokenizer
def load_ocnli_data(data_path, data_type, tokenizer):
args = get_args()
filename = os.path.join(data_path, data_type+'.json')
objs = []
with open(filename) as fin:
for line in fin:
objs.append(json.loads(line.strip()))
pad_id = tokenizer.encoder['<pad>']
args.eod_token = tokenizer.encoder['<eod>']
all_tokens_1 = []
all_masks_1 = []
all_tokens_2 = []
all_masks_2 = []
all_tokens_3 = []
all_masks_3 = []
all_labels = []
for obj in objs:
if obj['label'] == '-':
continue
prompt = "{}?对,".format(obj['sentence1'])
prompt_tokens = tokenizer.encode(prompt)
prompt_len = len(prompt_tokens)
tokens = prompt_tokens + tokenizer.encode(obj['sentence2'])
second_mask = [0] * (args.seq_length-1)
for idx in range(prompt_len-1, len(tokens)-1):
second_mask[idx] = 1
all_masks_1.append(second_mask)
token_length = len(tokens)
assert token_length < args.seq_length
tokens.extend([pad_id] * (args.seq_length - token_length))
all_tokens_1.append(tokens)
prompt = "{}?错,".format(obj['sentence1'])
prompt_tokens = tokenizer.encode(prompt)
prompt_len = len(prompt_tokens)
tokens = prompt_tokens + tokenizer.encode(obj['sentence2'])
second_mask = [0] * (args.seq_length-1)
for idx in range(prompt_len-1, len(tokens)-1):
second_mask[idx] = 1
all_masks_2.append(second_mask)
token_length = len(tokens)
assert token_length < args.seq_length
tokens.extend([pad_id] * (args.seq_length - token_length))
all_tokens_2.append(tokens)
prompt = "{}?也许,".format(obj['sentence1'])
prompt_tokens = tokenizer.encode(prompt)
prompt_len = len(prompt_tokens)
tokens = prompt_tokens + tokenizer.encode(obj['sentence2'])
second_mask = [0] * (args.seq_length-1)
for idx in range(prompt_len-1, len(tokens)-1):
second_mask[idx] = 1
all_masks_3.append(second_mask)
token_length = len(tokens)
assert token_length < args.seq_length
tokens.extend([pad_id] * (args.seq_length - token_length))
all_tokens_3.append(tokens)
if obj['label'] == 'entailment':
all_labels.append([0])
elif obj['label'] == 'contradiction':
all_labels.append([1])
else:
all_labels.append([2])
all_tokens_1 = torch.tensor(all_tokens_1, dtype=torch.long)
all_masks_1 = torch.tensor(all_masks_1, dtype=torch.float)
all_tokens_2 = torch.tensor(all_tokens_2, dtype=torch.long)
all_masks_2 = torch.tensor(all_masks_2, dtype=torch.float)
all_tokens_3 = torch.tensor(all_tokens_3, dtype=torch.long)
all_masks_3 = torch.tensor(all_masks_3, dtype=torch.float)
all_labels = torch.tensor(all_labels, dtype=torch.long)
dataset = TensorDataset(all_tokens_1, all_masks_1, all_tokens_2, all_masks_2, all_tokens_3, all_masks_3, all_labels)
# Data parallel arguments.
world_size = mpu.get_data_parallel_world_size()
rank = mpu.get_data_parallel_rank()
global_batch_size = args.batch_size * world_size
num_workers = args.num_workers
# Use a random sampler with distributed batch sampler.
if data_type == 'train':
sampler = RandomSampler(dataset)
else:
sampler = torch.utils.data.SequentialSampler(dataset)
batch_sampler = DistributedBatchSampler(sampler=sampler,
batch_size=global_batch_size,
drop_last=True,
rank=rank,
world_size=world_size)
# Torch dataloader.
return torch.utils.data.DataLoader(dataset,
batch_sampler=batch_sampler,
num_workers=num_workers,
pin_memory=True)
def load_iflytek_data(data_path, data_type, tokenizer, sampled_labels=False):
args = get_args()
filename = os.path.join(data_path, data_type+'.json')
objs = []
with open(filename) as fin:
for line in fin:
objs.append(json.loads(line.strip()))
pad_id = tokenizer.encoder['<pad>']
args.eod_token = tokenizer.encoder['<eod>']
labels = []
label_map = {}
with open(os.path.join(data_path, 'labels.json')) as fin:
for i, line in enumerate(fin):
obj = json.loads(line.strip())
labels.append(obj['label_des'])
label_map[obj['label_des']] = i
all_tokens = []
all_masks = []
all_labels = []
for _, obj in enumerate(objs):
sentence = obj['sentence']
tokenized_sentence = tokenizer.encode(sentence)[:args.seq_length-20]
if sampled_labels:
cur_labels = random.sample(labels, 3)
while obj['label_des'] in cur_labels:
cur_labels = random.sample(labels, 3)
cur_labels.append(obj['label_des'])
cur_label = cur_labels.index(obj['label_des'])
assert cur_label != -1
else:
cur_labels = labels
cur_label = label_map[obj['label_des']]
all_labels.append(cur_label)
for _, label in enumerate(cur_labels):
prompt = "这是关于{}的应用程序:".format(label)
prompt_tokens = tokenizer.encode(prompt)
prompt_len = len(prompt_tokens)
tokens = prompt_tokens + tokenized_sentence
second_mask = [0] * (args.seq_length-1)
for idx in range(prompt_len-1, len(tokens)-1):
second_mask[idx] = 1
all_masks.append(second_mask)
token_length = len(tokens)
assert token_length < args.seq_length
tokens.extend([pad_id] * (args.seq_length - token_length))
all_tokens.append(tokens)
all_tokens = torch.tensor(all_tokens, dtype=torch.long)
all_masks = torch.tensor(all_masks, dtype=torch.float)
dataset = TensorDataset(all_tokens, all_masks)
# Data parallel arguments.
world_size = mpu.get_data_parallel_world_size()
rank = mpu.get_data_parallel_rank()
global_batch_size = args.batch_size * world_size
num_workers = args.num_workers
sampler = torch.utils.data.SequentialSampler(dataset)
batch_sampler = DistributedBatchSampler(sampler=sampler,
batch_size=global_batch_size,
drop_last=True,
rank=rank,
world_size=world_size)
# Torch dataloader.
return torch.utils.data.DataLoader(dataset,
batch_sampler=batch_sampler,
num_workers=num_workers,
pin_memory=True), all_labels
def load_tnews_data(data_path, data_type, tokenizer, sampled_labels=False):
args = get_args()
filename = os.path.join(data_path, data_type+'.json')
objs = []
with open(filename) as fin:
for line in fin:
objs.append(json.loads(line.strip()))
pad_id = tokenizer.encoder['<pad>']
args.eod_token = tokenizer.encoder['<eod>']
labels = []
label_map = {}
label_reverse = {}
with open(os.path.join(data_path, 'labels.json')) as fin:
for i, line in enumerate(fin):
obj = json.loads(line.strip())
labels.append(obj['label_desc'])
label_map[obj['label_desc']] = i
label_reverse[obj['label']] = obj['label_desc']
all_tokens = []
all_masks = []
all_labels = []
for _, obj in enumerate(objs):
sentence = obj['sentence']
tokenized_sentence = tokenizer.encode(sentence)[:args.seq_length-20]
obj['label_desc'] = label_reverse[obj['label']]
if sampled_labels:
cur_labels = random.sample(labels, 3)
while obj['label_desc'] in cur_labels:
cur_labels = random.sample(labels, 3)
cur_labels.append(obj['label_desc'])
cur_label = cur_labels.index(obj['label_desc'])
assert cur_label != -1
else:
cur_labels = labels
cur_label = label_map[obj['label_desc']]
all_labels.append(cur_label)
for _, label in enumerate(cur_labels):
prompt = "这是关于{}的文章:".format(label)
prompt_tokens = tokenizer.encode(prompt)
prompt_len = len(prompt_tokens)
tokens = prompt_tokens + tokenized_sentence
second_mask = [0] * (args.seq_length-1)
for idx in range(prompt_len-1, len(tokens)-1):
second_mask[idx] = 1
all_masks.append(second_mask)
token_length = len(tokens)
assert token_length < args.seq_length
tokens.extend([pad_id] * (args.seq_length - token_length))
all_tokens.append(tokens)
all_tokens = torch.tensor(all_tokens, dtype=torch.long)
all_masks = torch.tensor(all_masks, dtype=torch.float)
dataset = TensorDataset(all_tokens, all_masks)
# Data parallel arguments.
world_size = mpu.get_data_parallel_world_size()
rank = mpu.get_data_parallel_rank()
global_batch_size = args.batch_size * world_size
num_workers = args.num_workers
sampler = torch.utils.data.SequentialSampler(dataset)
batch_sampler = DistributedBatchSampler(sampler=sampler,
batch_size=global_batch_size,
drop_last=True,
rank=rank,
world_size=world_size)
# Torch dataloader.
return torch.utils.data.DataLoader(dataset,
batch_sampler=batch_sampler,
num_workers=num_workers,
pin_memory=True), all_labels
def evaluate_ocnli(model, dev_dataloader, device, args):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for batch in tqdm.tqdm(dev_dataloader):
tokens_1, masks_1, tokens_2, masks_2, tokens_3, masks_3, labels = [x.to(device) for x in batch]
tokens, attention_mask, position_ids = get_batch(tokens_1, args)
output, _ = model(tokens, position_ids, attention_mask)
losses = mpu.vocab_parallel_cross_entropy(output[:, :-1, :].contiguous().float(), tokens[:, 1:])
output_1 = torch.sum(losses * masks_1, 1) / torch.sum(masks_1, -1)
tensor_list = [torch.zeros_like(output_1) for _ in range(mpu.get_data_parallel_world_size())]
torch.distributed.all_gather(tensor_list, output_1, mpu.get_data_parallel_group())
output_1 = torch.stack(tensor_list, 0).view(-1).cpu().detach().numpy()
# --------------
tokens, attention_mask, position_ids = get_batch(tokens_2, args)
output, _ = model(tokens, position_ids, attention_mask)
losses = mpu.vocab_parallel_cross_entropy(output[:, :-1, :].contiguous().float(), tokens[:, 1:])
output_2 = torch.sum(losses * masks_2, 1) / torch.sum(masks_2, -1)
tensor_list = [torch.zeros_like(output_2) for _ in range(mpu.get_data_parallel_world_size())]
torch.distributed.all_gather(tensor_list, output_2, mpu.get_data_parallel_group())
output_2 = torch.stack(tensor_list, 0).view(-1).cpu().detach().numpy()
# ---------------
tokens, attention_mask, position_ids = get_batch(tokens_3, args)
output, _ = model(tokens, position_ids, attention_mask)
losses = mpu.vocab_parallel_cross_entropy(output[:, :-1, :].contiguous().float(), tokens[:, 1:])
output_3 = torch.sum(losses * masks_3, 1) / torch.sum(masks_3, -1)
tensor_list = [torch.zeros_like(output_3) for _ in range(mpu.get_data_parallel_world_size())]
torch.distributed.all_gather(tensor_list, output_3, mpu.get_data_parallel_group())
output_3 = torch.stack(tensor_list, 0).view(-1).cpu().detach().numpy()
# --------------
tensor_list_labels = [torch.zeros_like(labels) for _ in range(mpu.get_data_parallel_world_size())]
torch.distributed.all_gather(tensor_list_labels, labels, mpu.get_data_parallel_group())
if torch.distributed.get_rank() == 0:
labels = torch.stack(tensor_list_labels, 0)
labels = labels.view(-1).cpu().detach().numpy()
res = [np.argmin(np.array(x)) for x in zip(output_1, output_2, output_3)]
res = [x==y for x, y in zip(res, labels)]
correct += sum(res)
total += len(res)
if torch.distributed.get_rank() == 0:
print("EVAL", correct, total)
def evaluate(model, dev_dataloader, all_labels, device, args):
model.eval()
if torch.distributed.get_rank() == 0:
res = []
with torch.no_grad():
for batch in tqdm.tqdm(dev_dataloader):
tokens, masks = [x.to(device) for x in batch]
tokens, attention_mask, position_ids = get_batch(tokens, args)
output, _ = model(tokens, position_ids, attention_mask)
losses = mpu.vocab_parallel_cross_entropy(output[:, :-1, :].contiguous().float(), tokens[:, 1:])
output = torch.sum(losses * masks, 1) / torch.sum(masks, -1)
tensor_list = [torch.zeros_like(output) for _ in range(mpu.get_data_parallel_world_size())]
torch.distributed.all_gather(tensor_list, output, mpu.get_data_parallel_group())
output = torch.stack(tensor_list, 0).view(-1).cpu().detach().numpy()
if torch.distributed.get_rank() == 0:
for v in output:
res.append(v)
if torch.distributed.get_rank() == 0:
cnt = 0
label_size = max(all_labels) + 1
num_inst = len(res) // label_size
for x in range(num_inst):
label = all_labels[x]
cur_res = res[x*label_size:(x+1)*label_size]
pos = np.argmin(cur_res)
if pos == label:
cnt += 1
print("EVAL", cnt, num_inst)
def main():
"""Main training program."""
# Disable CuDNN.
torch.backends.cudnn.enabled = False
# Arguments.
args = get_args()
# Pytorch distributed.
initialize_distributed(args)
# Random seeds for reproducability.
set_random_seed(args.seed)
#get the tokenizer
tokenizer = GPT2Tokenizer(os.path.join(args.tokenizer_path, 'vocab.json'), os.path.join(args.tokenizer_path, 'chinese_vocab.model'))
# load data
assert args.eval_data_path is not None
device = torch.cuda.current_device()
args.eod_token = tokenizer.encoder['<eod>']
# Model
args.parallel_output = True
model = setup_model(args)
if args.task == "ocnli":
dev_dataloader = load_ocnli_data(args.eval_data_path, 'dev', tokenizer)
evaluate_ocnli(model, dev_dataloader, device, args)
elif args.task == "iflytek":
dev_dataloader, all_labels = load_iflytek_data(args.eval_data_path, 'dev', tokenizer, True)
evaluate(model, dev_dataloader, all_labels, device, args)
elif args.task == "tnews":
dev_dataloader, all_labels = load_tnews_data(args.eval_data_path, 'dev', tokenizer, True)
evaluate(model, dev_dataloader, all_labels, device, args)
else:
print("Unknown task!")
if __name__ == "__main__":
main()