-
Notifications
You must be signed in to change notification settings - Fork 2
/
predictor.py
381 lines (329 loc) · 14.1 KB
/
predictor.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
import json
import logging
from arguments import ArgumentsForTest
from torch.utils.data import DataLoader
import numpy as np
import random
import os
from transformers import (
AutoModelForSeq2SeqLM,
AutoModelForCausalLM,
AutoTokenizer,
set_seed,
TrainingArguments,
HfArgumentParser,
)
import yaml
from utils import *
import tqdm
logging.basicConfig(level = logging.INFO)
os.environ["TOKENIZERS_PARALLELISM"] = "false"
import copy
import torch
import tqdm
import os
import numpy as np
def getinstruction(target_labels, mapping, gpt=False):
if gpt:
insturction = 'Target entity types: '
insturction = insturction + ', '.join([mapping[i] for i in target_labels])
insturction = insturction + '.\n'
else:
insturction = ''
for label in target_labels:
insturction = insturction + ' <extra_id_7> ' + mapping[label]
return insturction
def getpredformat(formattext,text):
return formattext.format(text=text)
def getoutputids(tokenizer, formats,context,targetlabels,mapping):
outputids = []
index = 0
for entity in context['entity']:
type = entity['type']
if type in targetlabels:
text = entity['text'] + ' is ' + mapping[type] + '.'
if index == 0:
outputid = tokenizer.encode(text, add_special_tokens=False)
else:
outputid = tokenizer.encode(' '+text, add_special_tokens=False)
outputids += outputid
index += 1
outputids = formats['entity']['prefix'] + outputids + formats['entity']['end']
return outputids
logger = logging.getLogger(__name__)
class DataCollator:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def __call__(self, features):
newfeatures = []
for feature in features:
feature = {
'input_ids': feature['input_ids'],
'attention_mask': [1] * len(feature['input_ids'])
}
newfeatures.append(feature)
features = self.tokenizer.pad(
newfeatures,
padding=True,
)
inputs = {k:torch.tensor(v, dtype=torch.int64) for k,v in features.items()}
return inputs
def gettestset(datapath ,tokenizer, args, formats):
if type(datapath) is list:
trainset = datapath[0]
testset = datapath[1]
targets = datapath[2]
else:
targets = {}
targetlabels = []
with open(datapath + '/record.schema') as f:
for line in f:
targetlabels.append(json.loads(line))
for label in targetlabels[0]:
targets[label] = 'spot'
with open(datapath + '/train.json') as f:
trainset = [json.loads(line) for line in f]
with open(datapath + '/test.json') as f:
testset = [json.loads(line) for line in f]
random.shuffle(trainset)
type2indexs = {i:[] for i in targets}
labelindex = {i:0 for i in targets}
for i, instance in enumerate(trainset):
types = []
if 'entity' not in instance:
instance['entity'] = instance['entity_offsets']
for entity in instance['entity']:
if entity['type'] in targets:
types.append(entity['type'])
for etype in types:
type2indexs[etype].append(i)
newtrainset = []
while len(newtrainset) != len(trainset):
for label in targets:
if labelindex[label] >= len(type2indexs[label]):
continue
index = labelindex[label]
instanceindex = type2indexs[label][index]
if instanceindex not in newtrainset:
newtrainset.append(instanceindex)
labelindex[label] += 1
trainset = [trainset[i] for i in newtrainset]
nums = len(trainset)
if nums < args.context_num and args.context_num > 0:
args.context_num = nums
instances = []
mapping = {}
typeindex = 1
for label in targets:
if 'anonymization' in args.enhance:
mapping[label] = '<type' + str(typeindex) + '>'
else:
mapping[label] = label
typeindex += 1
targetlabels = targets
gpt = False
if args.modeltype != 'metaner':
gpt = True
insturction = getinstruction(targets, mapping, gpt)
insturction = tokenizer.encode(insturction, add_special_tokens=False)
for context in trainset:
outputids = []
if 'entity' not in context:
context['entity'] = context['entity_offsets']
context['entity'] = sorted(context['entity'],key=lambda k:k['offset'][0])
index = 0
outputids = getoutputids(tokenizer, formats, context,targetlabels,mapping)
text = getpredformat(formats['entity']['inputformat'],' '.join(context['tokens']))
textids = tokenizer.encode(text, add_special_tokens=False)
context['input_ids'] = textids + outputids
trainset = trainset + trainset
max_length = 512
if args.modeltype == 'opt':
max_length = 2048 - 100
elif args.modeltype == 'gpt':
max_length = 1024 - 100
testindex = 0
for instance in tqdm.tqdm(testset):
if 'entity' not in instance:
instance['entity'] = instance['entity_offsets']
text = getpredformat(formats['entity']['inputformat'],' '.join(instance['tokens']))
textids = tokenizer.encode(text, add_special_tokens=False)
if args.modeltype != 'metaner':
textids = textids + formats['entity']['prefix']
if args.modeltype == 't5' and args.context_num > 0:
textids = textids + tokenizer.convert_tokens_to_ids(['<extra_id_0>'])
limit = max_length - len(textids)
usednum = 0
contextnum = 0
fullinputids = copy.deepcopy(insturction)
if args.context_num <= 0:
newinstance = copy.deepcopy(instance)
newinstance['index'] = testindex
newinstance['input_ids'] = fullinputids + textids
newinstance['targetlabel'] = targets
if 'anonymization' in args.enhance:
newinstance['mapping'] = mapping
newtargets = {}
for label in targets:
newtargets[mapping[label]] = targets[label]
newinstance['targetlabel'] = newtargets
instances.append(newinstance)
else:
for context in trainset:
inputids = copy.deepcopy(context['input_ids'])
if len(inputids) + len(fullinputids) > limit or contextnum == args.context_num:
newinstance = copy.deepcopy(instance)
newinstance['index'] = testindex
newinstance['input_ids'] = fullinputids + textids
newinstance['targetlabel'] = targets
if 'anonymization' in args.enhance:
newinstance['mapping'] = mapping
newtargets = {}
for label in targets:
newtargets[mapping[label]] = targets[label]
newinstance['targetlabel'] = newtargets
instances.append(newinstance)
if usednum > nums:
break
fullinputids = copy.deepcopy(insturction)
contextnum = 0
fullinputids += inputids
contextnum += 1
usednum += 1
testindex += 1
return instances
def predict(model, dataset, data_collator, training_args, args, tokenizer, endid, tag, prefixid = None, textmid = None, enhance = 'None'):
training_args = copy.deepcopy(training_args)
training_args.output_dir = training_args.output_dir + '/' + str(tag)
if model is None:
if args.modeltype == 'metaner':
model = AutoModelForSeq2SeqLM.from_pretrained(
training_args.output_dir,
)
elif args.modeltype == 'gpt':
model = AutoModelForCausalLM.from_pretrained(
training_args.output_dir,
pad_token_id=tokenizer.eos_token_id, torch_dtype=torch.float16
)
os.makedirs(training_args.output_dir, exist_ok=True)
print(len(dataset))
data_loader = DataLoader(dataset, batch_size=training_args.per_device_eval_batch_size, collate_fn=data_collator)
max_length = 512
if 'opt' in args.modeltype :
max_length = 2048
elif args.modeltype == 'gpt':
max_length = 1024
model.eval()
if args.modeltype != 'optbig':
model = model.cuda()
decoder_start_token_id = None
if args.modeltype == 'metaner':
decoder_start_token_id = prefixid
elif args.modeltype == 't5':
endid = tokenizer.eos_token_id
index = 0
with torch.no_grad():
with open(training_args.output_dir + '/' + args.predictfile + '.json', 'w') as f:
for batch in tqdm.tqdm(data_loader):
outputs = model.generate(inputs=batch['input_ids'].cuda(),attention_mask=batch['attention_mask'].cuda(),max_length=max_length,num_beams=1,eos_token_id=endid, decoder_start_token_id=decoder_start_token_id,return_dict_in_generate=True,output_scores =True)
preds = []
newdataset = []
batch = outputs.scores[0].size(0)
for i in range(batch):
newdataset.append(dataset[index])
index += 1
for i in range(len(outputs.scores)):
score = outputs.scores[i]
score, generated = torch.max(score,dim=-1)
generated = generated.cpu().numpy()
generated = np.expand_dims(generated,1)
preds.append(generated)
preds = np.concatenate(preds,axis=1)
if args.modeltype == 't5' and args.context_num > 0:
preds = preds[:,1:]
preds, golds, targetlabels, generations = tokenid2result(preds,endid,newdataset,tokenizer)
for i in range(len(preds)):
instance = newdataset[i]
f.write(json.dumps({'index':instance['index'], 'generation': generations[i],'gold': golds[i], 'pred': preds[i], 'targetlabels': targetlabels[i]})+'\n')
return preds
def main():
parser = HfArgumentParser((
ArgumentsForTest,
TrainingArguments
))
args, training_args = parser.parse_args_into_dataclasses()
set_seed(args.randomseed)
logger.info("Options:")
logger.info(args)
logger.info(training_args)
if '.yaml' in args.formatsconfig:
formatsconfig = yaml.load(open(args.formatsconfig),Loader=yaml.FullLoader)
args.formatsconfig = formatsconfig
logger.info("context config:")
logger.info(formatsconfig)
formats = args.formatsconfig
args.modeltype = formats['universal']['modeltype']
args.enhance = formats['universal']['enhance']
if args.modeltype == 't5' or args.modeltype == 'metaner':
tokenizer = AutoTokenizer.from_pretrained(args.plm)
elif args.modeltype == 'gpt':
tokenizer = AutoTokenizer.from_pretrained(args.plm,padding_side = "left",add_prefix_space=True)
tokenizer.pad_token = tokenizer.eos_token
elif 'opt' in args.modeltype:
tokenizer = AutoTokenizer.from_pretrained(args.plm,padding_side = "left", use_fast=False)
for task in formats:
for tag in formats[task]:
if 'format' not in tag:
formats[task][tag] = tokenizer.encode(formats[task][tag], add_special_tokens=False)
if args.debugfile != 'None':
seeds = os.listdir(args.testset)
for seed in seeds:
datasetpath = args.testset + '/' + seed + '/' + str(args.shot_num) + 'shot'
testset = gettestset(datasetpath ,tokenizer, args, formats)
index = 0
f = open(args.debugfile+'_'+str(seed),'w')
for instance in tqdm.tqdm(testset):
text = tokenizer.decode(instance['input_ids'])
f.write(json.dumps({'index': instance['index'], 'text': text, 'entity': instance['entity']}))
# f.write(tokenizer.decode(instance['input_ids'])+'\n')
data_collator = DataCollator(tokenizer=tokenizer)
if args.context_num >= 0:
if args.modeltype == 't5' or args.modeltype == 'metaner':
logger.info("seq2seq model")
model = AutoModelForSeq2SeqLM.from_pretrained(
args.plm,decoder_start_token_id = formats['universal']['prefix'][0],
)
elif args.modeltype == 'gpt':
logger.info("gpt model")
model = AutoModelForCausalLM.from_pretrained(
args.plm,
pad_token_id=tokenizer.eos_token_id, torch_dtype=torch.float16
)
elif args.modeltype == 'optbig':
logger.info("opt big model")
import nvgpu
def max_memo():
gpus = nvgpu.available_gpus()
max_memo = {}
for gpu in gpus:
gpu = int(gpu)
max_memo[gpu] = "68000MiB"
return max_memo
max_memo = max_memo()
model = AutoModelForCausalLM.from_pretrained(args.plm, torch_dtype=torch.float16, device_map="auto", max_memory=max_memo)
elif args.modeltype == 'opt':
logger.info("opt model")
model = AutoModelForCausalLM.from_pretrained(
args.plm, torch_dtype=torch.float16
)
else:
model = None
seeds = os.listdir(args.testset)
for seed in seeds:
# set_seed(args.randomseed)
datasetpath = args.testset + '/' + seed + '/' + str(args.shot_num) + 'shot'
testset = gettestset(datasetpath ,tokenizer, args, formats)
endid = formats['universal']['end'][0] if args.modeltype == 't5' or args.modeltype == 'metaner' else formats['universal']['end'][-1]
predict(model, testset, data_collator, training_args, args, tokenizer, endid, seed, prefixid = formats['universal']['prefix'][0], textmid=[formats['entity']['context_left'],formats['entity']['context_mid']],enhance=args.enhance)
if __name__ == "__main__":
main()