-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbbt.py
744 lines (699 loc) · 33.5 KB
/
bbt.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
import os
import copy
import time
import random
import torch
# import fitlog
import argparse
import numpy as np
import cma
from fastNLP import cache_results, Tester, DataSet
from transformers import (
RobertaConfig,
RobertaTokenizer,
BertConfig,
BertTokenizer,
ElectraConfig,
ElectraTokenizer,
BartConfig,
BartTokenizer,
T5Config,
T5Tokenizer,
GPT2Config,
GPT2Tokenizer,
BartConfig as CPTConfig,
)
from models.modeling_roberta import RobertaForMaskedLM
from models.modeling_bart import BartForConditionalGeneration
from models.modeling_t5 import T5ForConditionalGeneration
from models.modeling_gpt2 import GPT2LMHeadModel
from models.modeling_bert import BertForMaskedLM
from models.modeling_electra import ElectraForMaskedLM
from models.modeling_cpt import CPTForMaskedLM
from utils import hinge_loss
from sklearn.metrics import f1_score
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", default='roberta-large',
choices=['roberta-base', 'roberta-large',
'bert-base-uncased', 'bert-large-uncased',
'google/electra-base-generator', 'google/electra-large-generator',
'facebook/bart-base', 'facebook/bart-large',
't5-small', 't5-base', 't5-large', 't5-3b',
'gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl',
'fnlp/cpt-large'], type=str)
parser.add_argument("--task_name", default='sst2', type=str)
parser.add_argument("--n_prompt_tokens", default=50, type=int)
parser.add_argument("--intrinsic_dim", default=500, type=int)
parser.add_argument("--k_shot", default=16, type=int)
parser.add_argument("--batch_size", default=32, type=int)
parser.add_argument("--budget", default=8000, type=int)
parser.add_argument("--popsize", default=20, type=int)
parser.add_argument("--bound", default=0, type=int)
parser.add_argument("--sigma", default=1, type=float)
parser.add_argument("--alpha", default=1, type=float)
parser.add_argument("--print_every", default=50, type=int)
parser.add_argument("--eval_every", default=100, type=int)
parser.add_argument("--device", default='cuda:0', type=str)
parser.add_argument("--alg", default='CMA', type=str)
parser.add_argument("--random_proj", default='normal', type=str)
parser.add_argument("--seed", default=42, type=int)
parser.add_argument("--loss_type", default='ce', type=str)
parser.add_argument("--cat_or_add", default='add', type=str)
parser.add_argument("--parallel", action='store_true', help='Whether to allow parallel evaluation')
parser.add_argument(
"--inference_framework",
default='pt',
type=str,
help='''Which inference framework to use.
Currently supports `pt` and `ort`, standing for pytorch and Microsoft onnxruntime respectively'''
)
parser.add_argument(
"--onnx_model_path",
default=None,
type=str,
help='Path to your onnx model.'
)
args = parser.parse_args()
# below are free hyper-params
model_name = args.model_name
if model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
from dataloaders.dataloader_t5 import SST2Loader, AGNewsLoader, YelpPLoader, DBPediaLoader, RTELoader, MRPCLoader, SNLILoader
from metrics.metrics_t5 import SST2Metric, AGNewsMetric, YelpPMetric, DBPediaMetric, RTEMetric, MRPCMetric, SNLIMetric
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
from dataloaders.dataloader_gpt import SST2Loader, AGNewsLoader, YelpPLoader, DBPediaLoader, RTELoader, MRPCLoader, SNLILoader
from metrics.metrics_gpt import SST2Metric, AGNewsMetric, YelpPMetric, DBPediaMetric, RTEMetric, MRPCMetric, SNLIMetric
elif model_name in ['fnlp/cpt-large']:
from dataloaders.dataloader_cpt import ChnSentLoader, AmazonLoader, THUCNewsLoader, BQLoader, CMNLILoader, CCPMLoader, TNewsLoader, OCNLILoader, LCQMCLoader, C3Loader
from metrics.metrics_cpt import ChnSentMetric, AmazonMetric, THUCNewsMetric, BQMetric, CMNLIMetric, CCPMMetric, TNewsMetric, OCNLIMetric, LCQMCMetric, C3Metric
else:
from dataloaders.dataloader import SST2Loader, AGNewsLoader, YelpPLoader, DBPediaLoader, RTELoader, MRPCLoader, SNLILoader
from metrics.metrics import SST2Metric, AGNewsMetric, YelpPMetric, DBPediaMetric, RTEMetric, MRPCMetric, SNLIMetric
task_name = args.task_name
n_prompt_tokens = args.n_prompt_tokens
intrinsic_dim = args.intrinsic_dim
k_shot = args.k_shot
batch_size = args.batch_size
budget = args.budget
bound = args.bound
sigma = args.sigma
alpha = args.alpha
if args.popsize > 0:
popsize = args.popsize
else:
popsize = 4 + 3 * np.log(intrinsic_dim)
device = args.device
alg = args.alg
random_proj = args.random_proj
seed = args.seed
loss_type = args.loss_type
print_every = args.print_every
eval_every = args.eval_every
# if task_name in ['mrpc', 'snli', 'qnli', 'rte']:
# args.cat_or_add = 'cat'
cat_or_add = args.cat_or_add
parallel = args.parallel
inference_framework = args.inference_framework
onnx_model_path = args.onnx_model_path
if inference_framework not in ['pt', 'ort']:
raise ValueError(f'inference_framework only supports "pt", "ort", got `{inference_framework}` instead.')
if inference_framework == 'ort':
assert onnx_model_path is not None, 'Path to onnx model is required, got None instead.'
assert os.path.exists(onnx_model_path), f'In valid onnx model path `{onnx_model_path}`'
# fixed hyper-params
if cat_or_add == 'add':
init_prompt_path = None
else:
init_prompt_path = './nli_base_prompt.pt'
if task_name in ['sst2', 'yelpp', 'rte', 'mrpc', 'chnsent', 'lcqmc', 'bq']:
num_labels = 2
elif task_name in ['snli', 'cmnli', 'ocnli']:
num_labels = 3
elif task_name in ['agnews', 'ccpm', 'c3']:
num_labels = 4
elif task_name in ['amazon']:
num_labels = 5
elif task_name in ['thucnews']:
num_labels = 10
elif task_name in ['dbpedia', 'tnews']:
num_labels = 14
else:
raise ValueError
# save_path = '{}_results/{}_results/D_{}_d_{}_data_{}_{}_range_{}_loss_{}_budget_{}_seed_{}_{}_{}_{}_{}'.format(
# model_name.replace('/', '-'),
# task_name,
# n_prompt_tokens * 1024,
# intrinsic_dim,
# k_shot * num_labels,
# alg,
# bound,
# loss_type,
# budget,
# seed,
# cat_or_add,
# random_proj,
# 'parallel' if parallel else 'serial',
# inference_framework
# )
# print('Results will be saved in {}'.format(save_path))
#
# if os.path.exists(save_path):
# print('Experiment already run.')
# exit()
#
# args.save_path = save_path
args.bbt_version = 'bbt'
# log_dir = './logs'
# fitlog.set_log_dir(log_dir)
# fitlog.commit(__file__, fit_msg=save_path)
# fitlog.add_hyper(args)
# fitlog.add_hyper_in_file(__file__)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
class LMForwardAPI:
def __init__(self, model_name='roberta-large', n_prompt_tokens=50, task_name='sst2',
loss_type='hinge', init_prompt_path=None):
if model_name in ['roberta-base', 'roberta-large']:
self.config = RobertaConfig.from_pretrained(model_name)
self.tokenizer = RobertaTokenizer.from_pretrained(model_name)
self.model = RobertaForMaskedLM.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
inference_framework=inference_framework,
onnx_model_path=onnx_model_path,
)
self.model.lm_head.bias = torch.nn.parameter.Parameter(torch.zeros(self.config.vocab_size))
elif model_name in ['bert-base-uncased', 'bert-large-uncased']:
self.config = BertConfig.from_pretrained(model_name)
self.tokenizer = BertTokenizer.from_pretrained(model_name)
self.model = BertForMaskedLM.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
elif model_name in ['google/electra-base-generator', 'google/electra-large-generator']:
self.config = ElectraConfig.from_pretrained(model_name)
self.tokenizer = ElectraTokenizer.from_pretrained(model_name)
self.model = ElectraForMaskedLM.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
elif model_name in ['facebook/bart-base', 'facebook/bart-large']:
self.config = BartConfig.from_pretrained(model_name)
self.tokenizer = BartTokenizer.from_pretrained(model_name)
self.model = BartForConditionalGeneration.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
elif model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
self.config = T5Config.from_pretrained(model_name)
self.tokenizer = T5Tokenizer.from_pretrained(model_name)
self.model = T5ForConditionalGeneration.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
self.config = GPT2Config.from_pretrained(model_name)
self.tokenizer = GPT2Tokenizer.from_pretrained(model_name)
self.model = GPT2LMHeadModel.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
elif model_name in ['fnlp/cpt-large']:
self.config = CPTConfig.from_pretrained(model_name)
self.tokenizer = BertTokenizer.from_pretrained(model_name)
self.model = CPTForMaskedLM.from_pretrained(
model_name,
config=self.config,
n_prompt_tokens=n_prompt_tokens,
)
else:
raise NotImplementedError
if inference_framework == 'ort':
self.model.roberta = None
if cat_or_add == 'cat':
self.model.set_concat_prompt(True)
if init_prompt_path is not None:
print('Initialize prompt embedding from {}'.format(init_prompt_path))
self.init_prompt = torch.load(init_prompt_path).weight.cpu().reshape(-1)
else:
print('Initial prompt embedding not found. Initialize to zero embedding.')
self.init_prompt = torch.zeros(n_prompt_tokens * self.config.hidden_size)
print('Shape of initial prompt embedding: {}'.format(self.init_prompt.shape))
else:
# self.model.set_concat_prompt(False)
self.init_prompt = None
self.model.to(device)
self.model.eval()
self.linear = torch.nn.Linear(intrinsic_dim, n_prompt_tokens * self.config.hidden_size, bias=False)
if random_proj == 'normal':
# calculate std for normal distribution
if model_name in ['roberta-base', 'roberta-large']:
embedding = self.model.roberta.get_input_embeddings().weight.clone().cpu()
elif model_name in ['bert-base-uncased', 'bert-large-uncased']:
embedding = self.model.bert.get_input_embeddings().weight.clone().cpu()
elif model_name in ['google/electra-base-generator', 'google/electra-large-generator']:
embedding = self.model.electra.get_input_embeddings().weight.clone().cpu()
elif model_name in ['facebook/bart-base', 'facebook/bart-large', 'fnlp/cpt-large']:
embedding = self.model.model.get_input_embeddings().weight.clone().cpu()
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
embedding = self.model.transformer.get_input_embeddings().weight.clone().cpu()
else: # T5
embedding = self.model.get_input_embeddings().weight.clone().cpu()
# embedding = embedding[1000: 2000]
mu_hat = np.mean(embedding.reshape(-1).detach().cpu().numpy())
std_hat = np.std(embedding.reshape(-1).detach().cpu().numpy())
mu = 0.0
std = alpha * std_hat / (np.sqrt(intrinsic_dim) * sigma)
# temp = intrinsic_dim - std_hat * std_hat
# mu = mu_hat / temp
# std = std_hat / np.sqrt(temp)
print('[Embedding] mu: {} | std: {} [RandProj] mu: {} | std: {}'.format(mu_hat, std_hat, mu, std))
for p in self.linear.parameters():
torch.nn.init.normal_(p, mu, std)
self.best_train_perf = 0.0
self.best_dev_perf = 0.0
self.best_prompt = None
self.num_call = 0
# self.save_path = save_path
self.print_every = print_every
self.eval_every = eval_every
self.loss_type = loss_type
# if save_path is not None:
# os.makedirs(save_path, exist_ok=True)
if task_name == 'sst2':
self.metric = SST2Metric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'SST2Metric'
elif task_name == 'agnews':
self.metric = AGNewsMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'AGNewsMetric'
elif task_name == 'yelpp':
self.metric = YelpPMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'YelpPMetric'
elif task_name == 'dbpedia':
self.metric = DBPediaMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'DBPediaMetric'
elif task_name == 'rte':
self.metric = RTEMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'RTEMetric'
elif task_name == 'mrpc':
self.metric = MRPCMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'f1'
self.metric_name = 'MRPCMetric'
elif task_name == 'snli':
self.metric = SNLIMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'SNLIMetric'
elif task_name == 'chnsent':
self.metric = ChnSentMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'ChnSentMetric'
elif task_name == 'thucnews':
self.metric = THUCNewsMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'THUCNewsMetric'
elif task_name == 'lcqmc':
self.metric = LCQMCMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'LCQMCMetric'
elif task_name == 'cmnli':
self.metric = CMNLIMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'CMNLIMetric'
elif task_name == 'ocnli':
self.metric = OCNLIMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'OCNLIMetric'
elif task_name == 'amazon':
self.metric = AmazonMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'AmazonMetric'
elif task_name == 'bq':
self.metric = BQMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'BQMetric'
elif task_name == 'ccpm':
self.metric = CCPMMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'CCPMMetric'
elif task_name == 'tnews':
self.metric = TNewsMetric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'TNewsMetric'
elif task_name == 'c3':
self.metric = C3Metric(target='labels', pred='logits', tokenizer=tokenizer)
self.metric_key = 'acc'
self.metric_name = 'C3Metric'
else:
raise NotImplementedError
self.margin = self.metric.margin
self.ce_loss = torch.nn.CrossEntropyLoss(reduction='mean')
def calc_metric(self, logits, target):
label_map = self.metric.label_map
converted_target = target.clone()
for key, val in label_map.items():
converted_target[target == key] = val
interest_index = list(label_map.keys())
logits = logits[:, interest_index]
pred = logits.argmax(dim=-1)
if self.metric_key == 'acc':
perf = (pred == converted_target).sum() / len(target)
elif self.metric_key == 'f1':
perf = f1_score(converted_target.detach().cpu().numpy().tolist(),
pred.detach().cpu().numpy().tolist())
else:
raise KeyError(f'[Metric] Only support [acc, f1], got {self.metric_key} instead.')
if self.loss_type == 'hinge':
loss = hinge_loss(logits, converted_target, margin=self.margin, reduction='sum').item() / len(target)
elif self.loss_type == 'ce':
loss = self.ce_loss(logits, converted_target).item()
elif self.loss_type == 'perf':
loss = -1 * perf
else:
raise KeyError(f'[Loss] Only support [hinge, ce, perf], got {self.loss_type} instead.')
return loss, perf
def eval(self, prompt_embedding=None, test_data=None):
self.num_call += 1
if prompt_embedding is None:
prompt_embedding = self.best_prompt
if test_data is None:
bsz = len(dev_data['input_ids']) # batch size of dev data is the orignal batch size of training data
else:
bsz = batch_size # for test data
tmp_prompt = copy.deepcopy(prompt_embedding) # list or numpy.ndarray
if isinstance(prompt_embedding, list): # multiple queries
pe_list = []
for pe in prompt_embedding:
z = torch.tensor(pe).type(torch.float32) # z
z = self.linear(z) # Az
if self.init_prompt is not None:
z = z + self.init_prompt # Az + p_0
pe_list.append(z.reshape(n_prompt_tokens, -1).repeat(bsz, 1, 1))
prompt_embedding = torch.cat(pe_list) # num_workers*bsz x prompt_len x dim
assert len(prompt_embedding) == len(train_data['input_ids'])
elif isinstance(prompt_embedding, np.ndarray): # single query or None
prompt_embedding = torch.tensor(prompt_embedding).type(torch.float32) # z
prompt_embedding = self.linear(prompt_embedding) # Az
if self.init_prompt is not None:
prompt_embedding = prompt_embedding + self.init_prompt # Az + p_0
prompt_embedding = prompt_embedding.reshape(n_prompt_tokens, -1).repeat(bsz, 1, 1)
else:
raise ValueError(
f'[Prompt Embedding] Only support [list, numpy.ndarray], got `{type(prompt_embedding)}` instead.'
)
self.model.set_prompt_embedding(prompt_embedding)
if isinstance(test_data, DataSet):
if prompt_embedding.shape[0] > bsz:
raise ValueError('Provide a single prompt embedding for testing.')
test_tester = Tester(data=test_data, model=self.model, metrics=self.metric, batch_size=batch_size,
num_workers=1, device=device, use_tqdm=True)
results = test_tester.test()
test_acc = results[self.metric_name][self.metric_key]
# fitlog.add_best_metric(test_acc, name='test_acc')
return test_acc
else:
for k, v in train_data.items():
train_data[k] = v.to(device)
with torch.no_grad():
if model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
logits = self.model(
input_ids=train_data['input_ids'],
attention_mask=train_data['attention_mask'],
decoder_input_ids=train_data['decoder_input_ids'],
decoder_attention_mask=train_data['decoder_attention_mask'],
)['logits']
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
logits = self.model(
input_ids=train_data['input_ids'],
attention_mask=train_data['attention_mask'],
)['logits']
else:
logits = self.model(
input_ids=train_data['input_ids'],
attention_mask=train_data['attention_mask'],
mask_pos=train_data['mask_pos'],
)['logits']
if parallel: # we have multiple queries
all_losses, all_perfs = [], []
for i in range(len(logits) // bsz):
tmp_logits = logits[i * bsz:i * bsz + bsz]
tmp_target = train_data['labels'][i * bsz:i * bsz + bsz]
tmp_loss, tmp_perf = self.calc_metric(tmp_logits, tmp_target)
all_losses.append(tmp_loss)
all_perfs.append(tmp_perf)
loss = min(all_losses)
best_sol = all_losses.index(loss) # argmin
perf = all_perfs[best_sol] # corresponding performance
tmp_prompt = tmp_prompt[best_sol] # numpy.ndarray
prompt_embedding = pe_list[best_sol] # to be prepended to the input
else: # single query
loss, perf = self.calc_metric(logits, train_data['labels'])
# fitlog.add_loss(loss, name=self.loss_type, step=self.num_call)
# fitlog.add_metric(perf, name='train_acc', step=self.num_call)
if perf > self.best_train_perf:
self.best_train_perf = perf
# fitlog.add_best_metric(self.best_train_perf, name='train_acc')
# if self.save_path is not None:
# with open(os.path.join(self.save_path, 'train_acc.txt'), 'a') as fout:
# fout.write('{}\t{}\n'.format(self.num_call, perf))
if self.num_call % self.print_every == 0:
print(
'[# API Calls {}] loss: {}. Current perf: {}. Best perf so far: {}'.format(
self.num_call,
round(float(loss), 4),
round(float(perf), 4),
round(float(self.best_train_perf), 4)))
if self.num_call % self.eval_every == 0:
print('********* Evaluated on dev set *********')
if parallel: # if we have multiple queries, use the one that achieves minimal loss
self.model.set_prompt_embedding(prompt_embedding)
for k, v in dev_data.items():
dev_data[k] = v.to(device)
with torch.no_grad():
if model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
logits = self.model(
input_ids=dev_data['input_ids'],
attention_mask=dev_data['attention_mask'],
decoder_input_ids=dev_data['decoder_input_ids'],
decoder_attention_mask=dev_data['decoder_attention_mask'],
)['logits']
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
logits = self.model(
input_ids=dev_data['input_ids'],
attention_mask=dev_data['attention_mask'],
)['logits']
else:
logits = self.model(
input_ids=dev_data['input_ids'],
attention_mask=dev_data['attention_mask'],
mask_pos=dev_data['mask_pos'],
)['logits']
dev_loss, dev_perf = self.calc_metric(logits, dev_data['labels'])
# fitlog.add_metric(dev_perf, name='dev_acc', step=self.num_call)
if dev_perf > self.best_dev_perf:
self.best_dev_perf = dev_perf
# fitlog.add_best_metric(self.best_dev_perf, name='dev_acc')
self.best_prompt = copy.deepcopy(tmp_prompt)
# if self.save_path is not None:
# with open(os.path.join(self.save_path, 'dev_acc.txt'), 'a') as fout:
# fout.write('{}\t{}\n'.format(self.num_call, dev_loss))
print('Dev loss: {}. Dev perf: {}. Best dev perf: {}'.format(
round(float(dev_loss), 4),
round(float(dev_perf), 4),
round(float(self.best_dev_perf), 4)))
print('********* Done *********')
if parallel:
return all_losses
else:
return loss
if model_name in ['roberta-base', 'roberta-large']:
tokenizer = RobertaTokenizer.from_pretrained(model_name)
elif model_name in ['bert-base-uncased', 'bert-large-uncased', 'fnlp/cpt-large']:
tokenizer = BertTokenizer.from_pretrained(model_name)
elif model_name in ['google/electra-base-generator', 'google/electra-large-generator']:
tokenizer = ElectraTokenizer.from_pretrained(model_name)
elif model_name in ['facebook/bart-base', 'facebook/bart-large']:
tokenizer = BartTokenizer.from_pretrained(model_name)
elif model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
tokenizer = T5Tokenizer.from_pretrained(model_name)
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
else:
raise NotImplementedError
cache_fn = f"caches/data_{model_name.replace('/', '-')}_{task_name}_{n_prompt_tokens}_{seed}.pt"
if model_name not in ['fnlp/cpt-large']:
DataLoader = {
'sst2': SST2Loader,
'agnews': AGNewsLoader,
'yelpp': YelpPLoader,
'dbpedia': DBPediaLoader,
'rte': RTELoader,
'mrpc': MRPCLoader,
'snli': SNLILoader,
}
else:
DataLoader = {
'chnsent': ChnSentLoader,
'thucnews': THUCNewsLoader,
'lcqmc': LCQMCLoader,
'cmnli': CMNLILoader,
'ocnli': OCNLILoader,
'amazon': AmazonLoader,
'bq': BQLoader,
'ccpm': CCPMLoader,
'tnews': TNewsLoader,
'c3': C3Loader,
}
@cache_results(cache_fn, _refresh=False)
def get_data(task_name, tokenizer):
if task_name in ['agnews', 'yelpp', 'dbpedia', 'snli']:
splits = ['train', 'test']
else: # for datasets without test set, we use dev set
splits = ['train', 'validation']
if args.cat_or_add == 'cat':
data_bundle = DataLoader[task_name](tokenizer=tokenizer, n_prompt_tokens=0).my_load(splits)
else:
data_bundle = DataLoader[task_name](tokenizer=tokenizer, n_prompt_tokens=n_prompt_tokens).my_load(splits)
return data_bundle
def construct_true_few_shot_data(train_data, k_shot):
train_label_count = {}
dev_label_count = {}
new_train_data = DataSet()
new_dev_data = DataSet()
all_indices = [_ for _ in range(len(train_data))]
np.random.shuffle(all_indices)
for index in all_indices:
label = train_data[index]['labels']
if label < 0:
continue
if label not in train_label_count:
train_label_count[label] = 0
if label not in dev_label_count:
dev_label_count[label] = 0
if train_label_count[label] < k_shot:
new_train_data.append(train_data[index])
train_label_count[label] += 1
elif dev_label_count[label] < k_shot:
new_dev_data.append(train_data[index])
dev_label_count[label] += 1
if model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
new_train_data.set_input("input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask")
new_dev_data.set_input("input_ids", "attention_mask", "decoder_input_ids", "decoder_attention_mask")
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
new_train_data.set_input("input_ids", "attention_mask")
new_dev_data.set_input("input_ids", "attention_mask")
else:
new_train_data.set_input("input_ids", "attention_mask", "mask_pos")
new_dev_data.set_input("input_ids", "attention_mask", "mask_pos")
new_train_data.set_target("labels")
new_dev_data.set_target("labels")
return new_train_data, new_dev_data
data_bundle = get_data(task_name=task_name, tokenizer=tokenizer)
if task_name in ['agnews', 'yelpp', 'dbpedia', 'snli']:
train_data, test_data = data_bundle.get_dataset('train'), data_bundle.get_dataset('test')
else:
train_data, test_data = data_bundle.get_dataset('train'), data_bundle.get_dataset('validation')
train_data, dev_data = construct_true_few_shot_data(train_data, k_shot)
for ds in [train_data, dev_data, test_data]:
ds.set_pad_val('input_ids', tokenizer.pad_token_id if tokenizer.pad_token_id is not None else 0)
ds.set_pad_val('attention_mask', 0)
print('# of train data: {}'.format(len(train_data)))
print('Example:')
print(train_data[0])
print('\n# of dev data: {}'.format(len(dev_data)))
print('Example:')
print(dev_data[0])
print('\n# of test data: {}'.format(len(test_data)))
print('Example:')
print(test_data[0])
if model_name in ['t5-small', 't5-base', 't5-large', 't5-3b']:
train_data = {
'input_ids': torch.tensor(train_data['input_ids'].get(list(range(len(train_data))))),
'attention_mask': torch.tensor(train_data['attention_mask'].get(list(range(len(train_data))))),
'decoder_input_ids': torch.tensor(train_data['decoder_input_ids'].get(list(range(len(train_data))))),
'decoder_attention_mask': torch.tensor(train_data['decoder_attention_mask'].get(list(range(len(train_data))))),
'labels': torch.tensor(train_data['labels'].get(list(range(len(train_data))))),
}
dev_data = {
'input_ids': torch.tensor(dev_data['input_ids'].get(list(range(len(dev_data))))),
'attention_mask': torch.tensor(dev_data['attention_mask'].get(list(range(len(dev_data))))),
'decoder_input_ids': torch.tensor(dev_data['decoder_input_ids'].get(list(range(len(dev_data))))),
'decoder_attention_mask': torch.tensor(dev_data['decoder_attention_mask'].get(list(range(len(dev_data))))),
'labels': torch.tensor(dev_data['labels'].get(list(range(len(dev_data))))),
}
elif model_name in ['gpt2', 'gpt2-medium', 'gpt2-large', 'gpt2-xl']:
train_data = {
'input_ids': torch.tensor(train_data['input_ids'].get(list(range(len(train_data))))),
'attention_mask': torch.tensor(train_data['attention_mask'].get(list(range(len(train_data))))),
'labels': torch.tensor(train_data['labels'].get(list(range(len(train_data))))),
}
dev_data = {
'input_ids': torch.tensor(dev_data['input_ids'].get(list(range(len(dev_data))))),
'attention_mask': torch.tensor(dev_data['attention_mask'].get(list(range(len(dev_data))))),
'labels': torch.tensor(dev_data['labels'].get(list(range(len(dev_data))))),
}
else:
train_data = {
'input_ids': torch.tensor(train_data['input_ids'].get(list(range(len(train_data))))),
'attention_mask': torch.tensor(train_data['attention_mask'].get(list(range(len(train_data))))),
'mask_pos': torch.tensor(train_data['mask_pos'].get(list(range(len(train_data))))),
'labels': torch.tensor(train_data['labels'].get(list(range(len(train_data))))),
}
dev_data = {
'input_ids': torch.tensor(dev_data['input_ids'].get(list(range(len(dev_data))))),
'attention_mask': torch.tensor(dev_data['attention_mask'].get(list(range(len(dev_data))))),
'mask_pos': torch.tensor(dev_data['mask_pos'].get(list(range(len(dev_data))))),
'labels': torch.tensor(dev_data['labels'].get(list(range(len(dev_data))))),
}
model_forward_api = LMForwardAPI(
model_name=model_name,
n_prompt_tokens=n_prompt_tokens,
task_name=task_name,
# save_path=save_path,
loss_type=loss_type,
init_prompt_path=init_prompt_path
)
cma_opts = {
'seed': seed,
'popsize': popsize,
'maxiter': budget if parallel else budget // popsize,
'verbose': -1,
}
if bound > 0:
cma_opts['bounds'] = [-1 * bound, 1 * bound]
es = cma.CMAEvolutionStrategy(intrinsic_dim * [0], sigma, inopts=cma_opts)
print('Population Size: {}'.format(es.popsize))
print('{} Evaluation.'.format('Parallel' if parallel else 'Serial'))
if parallel:
# expand training data to a larger batch for parallel evaluation
train_data['input_ids'] = train_data['input_ids'].repeat(es.popsize, 1)
train_data['attention_mask'] = train_data['attention_mask'].repeat(es.popsize, 1)
train_data['mask_pos'] = train_data['mask_pos'].repeat(es.popsize)
train_data['labels'] = train_data['labels'].repeat(es.popsize)
# opt = cma.CMAOptions()
start_time = time.time()
while not es.stop():
solutions = es.ask()
if parallel:
fitnesses = model_forward_api.eval(solutions)
else:
fitnesses = [model_forward_api.eval(x) for x in solutions]
es.tell(solutions, fitnesses)
# es.logger.add() # write data to disc to be plotted
# es.disp()
end_time = time.time()
print('Done. Elapsed time: {} (mins)'.format((end_time - start_time) / 60))
print('Evaluate on test data...')
test_acc = model_forward_api.eval(test_data=test_data)
print('Test acc: {}'.format(round(test_acc, 4)))
# fitlog.finish()