forked from zhao-ht/GIMLET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_pipeline.py
280 lines (236 loc) · 11.5 KB
/
basic_pipeline.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
from model import GIMLETConfig,GinConfig,KVPLMConfig,MoMuConfig,MolT5Config, GalacticaConfig, GPT3Config
import numpy as np
import torch
from sklearn.metrics import (r2_score,
roc_auc_score)
import plotly.graph_objects as go
from transformers import (
HfArgumentParser,
)
import re
def load_graph_args(args,left):
assert args.transformer_backbone in ['gimlet', 'gint5','kvplm','molt5','momu', 'galactica', 'gpt3']
if args.transformer_backbone == 'gimlet':
parsernew = HfArgumentParser(GIMLETConfig)
graph_args = parsernew.parse_args(left)
elif args.transformer_backbone == 'gint5':
parsernew = HfArgumentParser(GinConfig)
graph_args = parsernew.parse_args(left)
elif args.transformer_backbone == 'kvplm' :
parsernew = HfArgumentParser(KVPLMConfig)
graph_args = parsernew.parse_args(left)
args.tokenizer_name='allenai/scibert_scivocab_uncased'
elif args.transformer_backbone == 'momu' :
parsernew = HfArgumentParser(MoMuConfig)
graph_args = parsernew.parse_args(left)
args.tokenizer_name = 'allenai/scibert_scivocab_uncased'
elif args.transformer_backbone == 'molt5' :
parsernew = HfArgumentParser(MolT5Config)
graph_args = parsernew.parse_args(left)
assert graph_args.init_checkpoint in ['laituan245/molt5-base','laituan245/molt5-small','laituan245/molt5-large']
args.tokenizer_name = graph_args.init_checkpoint
elif args.transformer_backbone == 'galactica' :
parsernew = HfArgumentParser(GalacticaConfig)
graph_args = parsernew.parse_args(left)
assert graph_args.init_checkpoint in ['facebook/galactica-1.3b','facebook/galactica-125m']
args.tokenizer_name = graph_args.init_checkpoint
elif args.transformer_backbone == 'gpt3' :
parsernew = HfArgumentParser(GPT3Config)
graph_args = parsernew.parse_args(left)
assert graph_args.init_checkpoint in ['text-davinci-003']
args.tokenizer_name = "mrsteyk/gpt3-tokenizer"
if args.transformer_backbone in ['kvplm','momu','galactica','gpt3']:
if graph_args.init_checkpoint is None:
graph_args.init_checkpoint = args.model_name_or_path
if args.model_name_or_path is None:
args.model_name_or_path = graph_args.init_checkpoint
return args,graph_args
def eval_result(model, loader,label_dict,tokenizer,task_type,transformer_backbone,args=None):
if task_type=='cla':
model.eval()
y_true, y_scores = [], []
id_y=label_dict[1][0]
id_n=label_dict[0][0]
id_invalid=label_dict['invalid'][0]
for step, batch in enumerate(loader):
for key in batch.keys():
batch[key] = batch[key].to(model.device)
with torch.no_grad():
labels=batch["labels"]
if labels.shape[1]>1: # Yes <s>
assert all((labels[:,1]==tokenizer.eos_token_id) + (labels[:,1]==id_invalid))
labels=labels[:,0].unsqueeze(1)
del batch["labels"]
if transformer_backbone in ['gimlet']: #Ours
batch["max_length"] = 3 # <PAD> CLASS <EOS>
output = model.generate(
**batch, output_scores=True, return_dict_in_generate=True
# num_beams=beam_size,
# no_repeat_ngram_size=no_repeat_ngram_size,
)
logits=output.scores[0].unsqueeze(1) #logits of CLASS
elif transformer_backbone in ['galactica']: # galactica
batch["max_new_tokens"] = 1 # <PAD> CLASS <EOS>
output = model.generate(
**batch, output_scores=True, return_dict_in_generate=True
# num_beams=beam_size,
# no_repeat_ngram_size=no_repeat_ngram_size,
)
logits=output.scores[0].unsqueeze(1) #logits of CLASS
elif transformer_backbone in ['gpt3']:
prompt = tokenizer.batch_decode(batch["input_ids"])[0] # llm only supports batch_size = 1
output = model.generate(prompt)
logits = output["choices"][0]["logprobs"]["top_logprobs"][0]
else: #kvplm and momu
logits = model(**batch)['logits']
index = labels != id_invalid #mask both text not answer and invalid labels; shape: [batch,answer length]
if not isinstance(logits,dict): # for generative model
assert logits[index].ndim==2 # selected answer shape:[n_valid_sample,n_vocabulary]
pred=(logits[index][:, id_y] - logits[index][:, id_n]).view([-1,1])
true = labels[index].view(pred.shape)
true[true == id_y] = 1
true[true == id_n] = 0
true[true == id_invalid] = -100
else: # for contrastive model and gpt, logits is dict
if transformer_backbone in ['gpt3']:
positive_words = ["Yes"]
negative_words = ["No"]
positive_score = []
for word in positive_words:
if word in logits:
positive_score.append(logits[word])
positive_score = np.array(positive_score).max()
negative_score = []
for word in negative_words:
if word in logits:
negative_score.append(logits[word])
negative_score = np.array(negative_score).max()
pred = torch.tensor([positive_score - negative_score > 0]).unsqueeze(1)
else: #Momu
pred = (logits['pos'].unsqueeze(1)[index] - logits['neg'].unsqueeze(1)[index]).view([-1, 1]) #shape of logits['pos] and logits['pos] are [batch]
true = labels[index].view(pred.shape)
true[true == id_y] = 1
true[true == id_n] = 0
true[true == id_invalid] = -100
assert torch.sum(true == id_invalid) == 0 # For contrastive model, invalid label is previously replaced by id_invalid(-100). Replace it here. Not necessary, because only valid label are selected
y_true.append(true)
y_scores.append(pred)
y_true = torch.cat(y_true, dim=0).cpu().numpy()
y_scores = torch.cat(y_scores, dim=0).cpu().numpy()
roc_list = []
for i in range(y_true.shape[1]):
# AUC is only defined when there is at least one positive data.
if np.sum(y_true[:, i] == 1) > 0 and np.sum(y_true[:, i] == 0) > 0:
is_valid = y_true[:, i] >= 0
roc_list.append(roc_auc_score(y_true[is_valid, i], y_scores[is_valid, i]))
else:
print('{} is invalid'.format(i))
if len(roc_list) < y_true.shape[1]:
print(len(roc_list))
print('Some target is missing!')
print('Missing ratio: %f' % (1 - float(len(roc_list)) / y_true.shape[1]))
if len(roc_list)==0:
return {'score':0},0, y_true, y_scores
else:
return {'score':sum(roc_list) / len(roc_list)}, 0, y_true, y_scores
else: # for regression
model.eval()
y_true, y_scores = [], []
for step, batch in enumerate(loader):
for key in batch.keys():
batch[key] = batch[key].to(model.device)
with torch.no_grad():
labels=batch["labels"]
del batch["labels"]
if "decoder_attention_mask" in batch:
del batch["decoder_attention_mask"]
if transformer_backbone in ['gimlet']: #Ours
batch["max_length"] = labels.shape[1]+1 # additional <pad> in the begining
ids = model.generate(
**batch,
# num_beams=beam_size,
# no_repeat_ngram_size=no_repeat_ngram_size,
)
pred = []
for i in range(ids.shape[0]):
pred.append(tokenizer.decode(ids[i, :]))
elif transformer_backbone in ['galactica']: # galactica
batch["max_new_tokens"] = labels.shape[1]+1 # <PAD> CLASS <EOS>
ids = model.generate(
**batch
# num_beams=beam_size,
# no_repeat_ngram_size=no_repeat_ngram_size,
)
ids=ids[:,batch['input_ids'].shape[1]:]
pred = []
for i in range(ids.shape[0]):
pred.append(tokenizer.decode(ids[i, :]))
else: #kvplm
logits = model(**batch)['logits']
ids=logits.argmax(2)
pred = []
for i in range(ids.shape[0]):
ind_valid = labels[i, :] >= 0
if ind_valid.shape[0] > ids.shape[1]:
ind_valid = ind_valid[0:(ids.shape[1])]
pred.append(tokenizer.decode(ids[i, ind_valid]))
pred_number=[]
for result in pred:
number_list=re.findall(r"-?\d+\.?\d*e??\d*?",result)
try:
decoded_number=eval(number_list[0])
except:
decoded_number=float(np.nan)
pred_number.append(decoded_number)
true=[]
for i in range(labels.shape[0]):
true.append(tokenizer.decode(labels[i, labels[i, :]>0]))
true_number=[]
for result in true:
number_list=re.findall(r"-?\d+\.?\d*e??\d*?",result.replace(" ",""))
true_number.append(eval((number_list[0])) if len(number_list)>0 else float(np.nan))
y_true+=true_number
y_scores+=pred_number
y_true = torch.tensor(y_true)
y_scores = torch.tensor(y_scores)
ind = (~y_scores.isnan())
ratio=ind.float().mean()
y_true=y_true[ind]
y_scores=y_scores[ind]
mrs=(y_true-y_scores).std()
naive_msr=(y_true-y_true.mean()).std()
corrcoef=np.corrcoef(y_true,y_scores)[0,1]
try:
r2=r2_score(y_true,y_scores)
except:
r2=np.nan
if args.plot_regression:
fig = go.Figure()
fig.add_trace(go.Scatter(
x=y_true,
y=y_scores,
mode='markers',
marker=dict(
size=25,
opacity=0.5,
line=dict(width=2,
), symbol="diamond"),
))
fig.update_layout(
title=args.dataset.replace('_',' '),
)
fig.update_layout(title={'font': {'size': 50}})
fig.update_layout(
xaxis_title='True Value',
yaxis_title='Predicted Value',
width=1000,
height=1000,
font=dict(
size=30,
color="Black"
)
)
global fig_number
fig.write_image('cache/'+('{}_{}_fig{}.png'.format(args.dataset,args.model_name_or_path,fig_number)).replace('/','_'))
fig_number+=1
return {'ratio':float(ratio),'RMSE':float(mrs),'corrcoef':float(corrcoef),'R-Square':float(r2),'score':float(mrs)}, 0, y_true, y_scores