-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
412 lines (302 loc) · 12.9 KB
/
infer.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
from copy import deepcopy
import os
from os.path import join
import shutil
from typing import Dict, List
from datetime import datetime
from easydict import EasyDict as edict
from tqdm import tqdm
import torch
from models import *
from utils import *
from inference.data_types import *
from inference.pipeline_base import *
from inference.inference_models import *
from inference.pipeline_torchscript import *
from utils.evaluate_tools import *
infer_input_names = ['input_token_ids', 'entity_indices', 'question_indices', 'column_indices', 'value_indices']
# 读取文件的每一行, 返回列表
def get_lines(filename):
with open(filename, encoding='utf-8') as f:
lines = []
for s in f.readlines():
s = s.strip()
if s:
lines.append(s)
return lines
def load_json_file(filename):
"""
:param filename: 文件名
:return: 数据对象,json/list
"""
with open(filename, encoding='utf-8') as fp:
data = json.load(fp)
return data
def load_jsonl_file(filename):
"""
:param filename: 文件名
:return: 数据对象,json/list
"""
data = []
for line in get_lines(filename):
js = json.loads(line)
data.append(js)
return data
def save_json_file(filename, data, indent=2):
"""
:param filename: 输出文件名
:param data: 数据对象,json/list
:return:
"""
with open(filename, 'w', encoding='utf-8') as fp:
if indent:
json.dump(data, fp, indent=indent, ensure_ascii=False)
else:
json.dump(data, fp, ensure_ascii=False)
print('save file %s successful!' % filename)
def convert_to_numpy(inputs):
if isinstance(inputs, torch.Tensor):
return inputs.cpu().numpy()
if isinstance(inputs, list):
return [convert_to_numpy(x) for x in inputs]
if isinstance(inputs, dict):
return { key : convert_to_numpy(val) for key, val in inputs.items() }
raise NotImplementedError(inputs)
def convert_to_request(example: Text2SQLExample) -> NLBindingRequest:
return NLBindingRequest(
language=example.language,
question_tokens=[NLToken.from_json(x.to_json()) for x in example.question.tokens],
columns=[NLColumn.from_json(x.to_json()) for x in example.schema.columns],
matched_values=[NLMatchedValue(column=x.column, name=x.name, tokens=[NLToken.from_json(xx.to_json()) for xx in x.tokens], start=x.start, end=x.end) for x in example.matched_values]
)
def load_data_inputs(path: str, tokenizer: PreTrainedTokenizer, sampling_size: int = -1, infer_input_names: List[str] = infer_input_names) -> List[Dict]:
examples: List[Text2SQLExample] = load_json_objects(Text2SQLExample, path)
examples = [ex.ignore_unmatched_values() for ex in examples if ex.value_resolved]
if sampling_size > 0:
print()
print('sampling {} examples ...'.format(sampling_size))
examples = examples[:sampling_size] # random.sample(examples, sampling_size)
data_iter = load_data_loader(
examples=examples,
tokenizer=tokenizer,
batch_size=1,
is_training=False,
max_enc_length=tokenizer.model_max_length,
n_processes=1)
data_inputs, data_requests, cp_labels, data_examples = [], [], [], []
for batch_inputs in data_iter:
data_inputs += [[batch_inputs[key][0] for key in infer_input_names ]]
data_requests += [convert_to_request(batch_inputs['example'][0])]
data_examples += [batch_inputs['example'][0]]
cp_label = batch_inputs['concept_labels'][0].cpu().tolist()
cp_labels += [cp_label]
return data_inputs, data_requests, cp_labels, data_examples
def get_binding_tag_str(binding_list):
# Col Val O Key Unk Amb
merge = []
for i, js in enumerate(binding_list):
token = js['token']
term_type = js['term_type']
if term_type == QuestionLabel.Null.name:
term_type = 'O'
term_type = term_type[:3]
if term_type not in ['O', 'Key']:
one = [token, term_type]
else:
term_type = 'O'
one = [token, 'O']
if i == 0:
merge.append(one)
continue
if term_type == merge[-1][1]:
merge[-1][0] += " " + token
else:
merge.append(one)
str_list = []
for token, term_type in merge:
if term_type == 'O':
text = token
else:
text = "[{} | {}]".format(token, term_type)
str_list.append(text)
bind_str = ' '.join(str_list)
return bind_str
def get_binding_result_str(binding_list, tag=False):
# Col Val O Key Unk Amb
merge = []
find = False
if tag:
str_list = []
for i, js in enumerate(binding_list):
token = js['token']
term_type = js['term_type']
if term_type == QuestionLabel.Null.name:
term_type = 'O'
term_type = term_type[:3]
if term_type not in ['O', 'Key']:
str_list += [f"[{token} | {term_type}]"]
else:
str_list += [token]
bind_str = ' '.join(str_list)
return bind_str
for i, js in enumerate(binding_list):
token = js['token']
term_type = js['term_type']
if term_type == QuestionLabel.Null.name:
term_type = 'O'
term_type = term_type[:3]
term_value = js['term_value']
confidence = js['confidence']
one = [token, term_type, term_value, confidence]
if i == 0:
merge.append(one)
continue
if term_type in ['Col', 'Val'] and merge[-1][1:-1] == one[1:-1]:
merge[-1][0] += " " + token
elif term_type in ['O', 'Unk', 'Amb'] and merge[-1][1] == one[1]:
merge[-1][0] += " " + token
else:
merge.append(one)
str_list = []
for token, term_type, term_value, confidence in merge:
if term_type in ['Col', 'Val']:
# text = "[{} | {} | {}]".format(token, term_value, confidence)
text = "[{} | {}]".format(token, term_value.strip())
elif term_type in ['Unk', 'Amb']:
text = "[{} | {}]".format(token, term_type)
else:
text = token
str_list.append(text)
bind_str = ' '.join(str_list)
return bind_str
def get_concept_list(result: NLBindingResult):
concept_list = []
for i, x in enumerate(result.term_results):
if x.term_score > 0.5:
score_str = float(str(x.term_score)[:4])
concept_list.append([x.term_type.name, x.term_value, score_str])
return concept_list
def dump_nl_binding_predictions(requests: List[NLBindingRequest], pipeline: NLBindingInferencePipeline, saved_path: str, data_examples: List[Text2SQLExample]):
time_costs = []
# wramup
result = pipeline.infer(requests[0])
in_list = []
out_list = []
# saved_dir = os.path.dirname(saved_path)
in_file = saved_path.replace('predict', 'input')
i = 0
for j, request in enumerate(tqdm(requests)):
print('\n--------------------------------------------------------')
example = data_examples[j]
try:
result = pipeline.infer(request)
except:
continue
time_costs.append(result.inference_ms)
input_js = example.to_json()
print('UUID: {}\n'.format(example.uuid))
print('Question: {}\n'.format(example.question.text))
print('Dataset: {}; Schema: {}\n'.format(example.dataset, example.schema.to_string()))
print('SQL: {}\n'.format(str(example.sql)))
output_js = deepcopy(input_js)
del output_js['language']
concept_list = get_concept_list(result)
output_js['concepts'] = concept_list
print('Gold VS SL VS Grounding:')
gold_binding_result = example.get_binding_result_json_list()
output_js['gold_binding_result'] = gold_binding_result
result.print_binding_result(gold_binding_result)
sl_binding_js_list = result.export_sequence_labeling_json()
output_js['sl_binding_result'] = sl_binding_js_list
grounding_binding_js_list = result.export_binding_json()
output_js['sl_grounding_binding_result'] = grounding_binding_js_list
in_list.append(input_js)
out_list.append(output_js)
i += 1
save_json_file(in_file, in_list)
save_json_file(saved_path, out_list)
return out_list
def print_bad_case(data, examples):
gbinds = [x.gold_binding_result for x in data]
pbinds = [x.sl_grounding_binding_result for x in data]
p_sl_binds = [x.sl_binding_result for x in data]
correct_all = 0
total_all = 0
print('\n------------------------\n')
print('bad case:')
for idx, (g, p) in enumerate(zip(gbinds, pbinds)):
correct = get_sentence_correct(g, p)
correct_all += int(correct)
total_all += 1
if not correct:
example = examples[idx]
print('UUID: {}'.format(example.uuid))
print('Question: {}'.format(example.question.text))
print('Dataset: {}; Schema: {}'.format(example.dataset, example.schema.to_string()))
print('SQL: {}'.format(str(example.sql)))
print()
gold_bind_str = get_binding_result_str(g)
print(f"Gold: {gold_bind_str}")
pred_bind_str = get_binding_result_str(p)
print(f"Pred: {pred_bind_str}")
sl_binds = p_sl_binds[idx]
pred_bind_tag_str = get_binding_tag_str(sl_binds)
print(f"Tag : {pred_bind_tag_str}")
print('\n------------------------\n')
col_acc = (correct_all * 100) / total_all
acc_str = f"sentence accuracy: {str(col_acc)[:4]}% {correct_all}/{total_all}"
print(acc_str)
def print_evaluate_performance(data):
gbinds = [x.gold_binding_result for x in data]
pbinds = [x.sl_grounding_binding_result for x in data]
p_sl_binds = [x.sl_binding_result for x in data]
gconcepts = [get_gold_concepts(x.gold_binding_result) for x in data]
pconcepts = [x.concepts if 'concepts' in x else [] for x in data]
col_acc, acc_str = get_sentence_accuracy(gbinds, pbinds)
print(acc_str)
col_acc, acc_str = get_tag_accuracy(gbinds, pbinds, tag='Column')
print(acc_str)
col_acc, acc_str = get_tag_accuracy(gbinds, pbinds, tag='Value')
print(acc_str)
col_acc, acc_str = get_tag_accuracy(gbinds, p_sl_binds, tag='Unknown')
print(acc_str)
col_acc, acc_str = get_concept_accuracy(gconcepts, pconcepts, tags=['Column'])
print(acc_str)
col_acc, acc_str = get_concept_accuracy(gconcepts, pconcepts, tags=['Value'])
print(acc_str)
def infer_torchscript(args):
model_dir = os.path.dirname(args.checkpoint)
# Load model
model = GroundingInferenceModel.from_trained(args.checkpoint)
# Load data inputs
if args.sampling:
data_inputs, data_requests, _, data_examples = load_data_inputs(args.data_path, tokenizer=model.tokenizer, sampling_size=20)
else:
data_inputs, data_requests, _, data_examples = load_data_inputs(args.data_path, tokenizer=model.tokenizer)
dummy_input = data_inputs[0]
print('sample data_inputs[0] shape:')
for input_name, input_value in zip(infer_input_names, dummy_input):
print(input_name, " = ", input_value.size())
print()
out_dir = args.out_dir
if not os.path.exists(out_dir):
os.makedirs(out_dir)
basename = os.path.basename(args.data_path)
predict_save_path = join(out_dir, basename + '.predict.json')
pipeline = NLBindingTorchScriptPipeline(model_dir=model_dir, model=model, \
greedy_linking=True, threshold=0.2, use_gpu=torch.cuda.is_available() and args.gpu)
out_list = dump_nl_binding_predictions(data_requests, pipeline, predict_save_path, data_examples)
data = [edict(i) for i in out_list]
print_evaluate_performance(data)
print_bad_case(data, data_examples)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-ckpt', '--checkpoint', help='checkpoint', type=str, required=True)
parser.add_argument('-do_val', '--do_validation', action='store_true')
parser.add_argument('-data_path', '--data_path', default='data/wikisql_label/dev.preproc.json')
parser.add_argument('-out_dir', '--out_dir', default='./output')
parser.add_argument('-sampling', '--sampling', action='store_true')
parser.add_argument('-gpu', '--gpu', action='store_true')
args = parser.parse_args()
infer_torchscript(args)