-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
649 lines (593 loc) · 22.9 KB
/
dataset.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
import random
import re
from dataclasses import dataclass, field
from typing import Callable, Dict, List, Optional, Generator
from datasets import interleave_datasets
from datasets.arrow_dataset import Dataset
from datasets.dataset_dict import DatasetDict
from sql_metadata import Parser
from transformers.training_args import TrainingArguments
from .bridge_content_encoder import get_database_matches
from .spider_sql import SpiderSQL
@dataclass
class DataTrainingArguments:
"""
Arguments pertaining to what data we are going to input our model for training and eval.
"""
overwrite_cache: bool = field(
default=False,
metadata={"help": "Overwrite the cached training and evaluation sets"},
)
preprocessing_num_workers: Optional[int] = field(
default=None,
metadata={"help": "The number of processes to use for the preprocessing."},
)
max_source_length: Optional[int] = field(
default=512,
metadata={
"help": "The maximum total input sequence length after tokenization. Sequences longer "
"than this will be truncated, sequences shorter will be padded."
},
)
max_target_length: Optional[int] = field(
default=512,
metadata={
"help": "The maximum total sequence length for target text after tokenization. Sequences longer "
"than this will be truncated, sequences shorter will be padded."
},
)
val_max_target_length: Optional[int] = field(
default=None,
metadata={
"help": "The maximum total sequence length for validation target text after tokenization. Sequences longer "
"than this will be truncated, sequences shorter will be padded. Will default to `max_target_length`."
"This argument is also used to override the ``max_length`` param of ``model.generate``, which is used "
"during ``evaluate`` and ``predict``."
},
)
val_max_time: Optional[int] = field(
default=None,
metadata={
"help": "The maximum allowed time in seconds for generation of one example. This setting can be used to stop "
"generation whenever the full generation exceeds the specified amount of time."
},
)
max_train_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of training examples to this "
"value if set."
},
)
max_val_samples: Optional[int] = field(
default=None,
metadata={
"help": "For debugging purposes or quicker training, truncate the number of validation or test examples to this "
"value if set."
},
)
num_beams: int = field(
default=1,
metadata={
"help": "Number of beams to use for evaluation. This argument will be passed to ``model.generate``, "
"which is used during ``evaluate`` and ``predict``."
},
)
num_beam_groups: int = field(
default=1,
metadata={
"help": "Number of beam groups to use for evaluation. This argument will be passed to ``model.generate``, "
"which is used during ``evaluate`` and ``predict``."
},
)
diversity_penalty: Optional[float] = field(
default=None,
metadata={
"help": "Diversity penalty to use for evaluation. This argument will be passed to ``model.generate``, "
"which is used during ``evaluate`` and ``predict``."
},
)
num_return_sequences: Optional[int] = field(
default=None,
metadata={
"help": "The number of sequences to generate during evaluation. This argument will be passed to "
"``model.generate``, which is used during ``evaluate`` and ``predict``."
},
)
ignore_pad_token_for_loss: bool = field(
default=True,
metadata={
"help": "Whether or not to ignore the tokens corresponding to padded labels in the loss computation or not."
},
)
source_prefix: Optional[str] = field(
default=None,
metadata={
"help": "A prefix to add before every source text (useful for T5 models)."
},
)
schema_serialization_type: str = field(
default="peteshaw",
metadata={
"help": "Choose between ``verbose`` and ``peteshaw`` schema serialization."
},
)
schema_serialization_randomized: bool = field(
default=False,
metadata={"help": "Whether or not to randomize the order of tables."},
)
schema_serialization_with_db_id: bool = field(
default=True,
metadata={
"help": "Whether or not to add the database id to the context. Needed for Picard."
},
)
schema_serialization_with_db_content: bool = field(
default=True,
metadata={
"help": "Whether or not to use the database content to resolve field matches."
},
)
normalize_query: bool = field(
default=True,
metadata={
"help": "Whether to normalize the SQL queries with the process in the 'Decoupling' paper"
},
)
target_with_db_id: bool = field(
default=True,
metadata={
"help": "Whether or not to add the database id to the target. Needed for Picard."
},
)
################################
##### Added by Parker ##########
################################
use_gold_concepts: bool = field(
default=False,
metadata={
"help": "Whether or not to serialize input only with columns/tables/values present in the gold query."
},
)
use_serialization_file: Optional[List[str]] = field(
default=None,
metadata={
"help": "If specified, points to the output of a T5 concept prediction model. Uses predictions as serialization to current text-to-sql model"
},
)
include_explanation: Optional[bool] = field(
default=False,
metadata={
"help": "Boolean defining whether to serialize explanation in SPLASH training"
},
)
include_question: Optional[bool] = field(
default=False,
metadata={
"help": "Boolean defining whether to serialize question in SPLASH training"
},
)
splash_train_with_spider: Optional[bool] = field(
default=False,
metadata={
"help": "Boolean defining whether to interleave Spider train set with Splash train"
},
)
shuffle_splash_feedback: Optional[bool] = field(
default=False,
metadata={
"help": "Test to see if model is actually using feedback, by running evaluation on test set with shuffled feedback"
},
)
shuffle_splash_question: Optional[bool] = field(
default=False,
metadata={
"help": "Test to see if model is actually using question, by running evaluation on test set with shuffled questions"
},
)
task_type: Optional[str] = field(
default="text2sql",
metadata={"help": "One of text2sql, schema_prediction"},
)
spider_eval_on_splash: Optional[bool] = field(
default=False,
metadata={
"help": "Whether we're running a Spider model on SPLASH. Only use question, in that case."
},
)
def __post_init__(self):
if self.val_max_target_length is None:
self.val_max_target_length = self.max_target_length
@dataclass
class DataArguments:
dataset: str = field(
metadata={
"help": "The dataset to be used. Choose between ``spider``, ``cosql``, or ``cosql+spider``, or ``spider_realistic``, or ``spider_syn``, or ``spider_dk``."
},
)
dataset_paths: Dict[str, str] = field(
default_factory=lambda: {
"spider": "./seq2seq/datasets/spider",
"splash": "./seq2seq/datasets/splash",
"cosql": "./seq2seq/datasets/cosql",
"spider_realistic": "./seq2seq/datasets/spider_realistic",
"spider_syn": "./seq2seq/datasets/spider_syn",
"spider_dk": "./seq2seq/datasets/spider_dk",
},
metadata={"help": "Paths of the dataset modules."},
)
spider_dataset_url: str = field(
default="",
metadata={"help": "Path of spider.zip"},
)
splash_dataset_url: str = field(
default="",
metadata={"help": "Path of splash.zip"},
)
metric_config: str = field(
default="both",
metadata={
"help": "Choose between ``exact_match``, ``test_suite``, or ``both``."
},
)
# we are referencing spider_realistic to spider metrics only as both use the main spider dataset as base.
metric_paths: Dict[str, str] = field(
default_factory=lambda: {
"spider": "./seq2seq/metrics/spider",
"splash": "./seq2seq/metrics/splash",
"spider_schema": "./seq2seq/metrics/spider_schema",
"splash_schema": "./seq2seq/metrics/splash_schema",
"spider_realistic": "./seq2seq/metrics/spider",
"cosql": "./seq2seq/metrics/cosql",
"spider_syn": "./seq2seq/metrics/spider",
"spider_dk": "./seq2seq/metrics/spider",
},
metadata={"help": "Paths of the metric modules."},
)
test_suite_db_dir: Optional[str] = field(
default=None, metadata={"help": "Path to the test-suite databases."}
)
data_config_file: Optional[str] = field(
default=None,
metadata={
"help": "Path to data configuration file (specifying the database splits)"
},
)
test_sections: Optional[List[str]] = field(
default=None,
metadata={"help": "Sections from the data config to use for testing"},
)
@dataclass
class TrainSplit(object):
dataset: Dataset
schemas: Dict[str, dict]
@dataclass
class EvalSplit(object):
dataset: Dataset
examples: Dataset
schemas: Dict[str, dict]
@dataclass
class DatasetSplits(object):
train_split: Optional[TrainSplit]
eval_split: Optional[EvalSplit]
test_splits: Optional[Dict[str, EvalSplit]]
schemas: Dict[str, dict]
def _get_schemas(examples: Dataset) -> Dict[str, dict]:
schemas: Dict[str, dict] = dict()
for ex in examples:
if ex["db_id"] not in schemas:
schemas[ex["db_id"]] = {
"db_table_names": ex["db_table_names"],
"db_column_names": ex["db_column_names"],
"db_column_types": ex["db_column_types"],
"db_primary_keys": ex["db_primary_keys"],
"db_foreign_keys": ex["db_foreign_keys"],
}
return schemas
def _prepare_train_split(
dataset: Dataset,
data_training_args: DataTrainingArguments,
add_serialized_schema: Callable[[dict], dict],
pre_process_function: Callable[[dict, Optional[int], Optional[int]], dict],
) -> TrainSplit:
schemas = _get_schemas(examples=dataset)
dataset = dataset.map(
add_serialized_schema,
batched=False,
num_proc=data_training_args.preprocessing_num_workers,
load_from_cache_file=not data_training_args.overwrite_cache,
)
if data_training_args.max_train_samples is not None:
dataset = dataset.select(range(data_training_args.max_train_samples))
column_names = dataset.column_names
dataset = dataset.map(
lambda batch: pre_process_function(
batch=batch,
max_source_length=data_training_args.max_source_length,
max_target_length=data_training_args.max_target_length,
),
batched=True,
num_proc=data_training_args.preprocessing_num_workers,
remove_columns=column_names,
load_from_cache_file=not data_training_args.overwrite_cache,
)
return TrainSplit(dataset=dataset, schemas=schemas)
def _prepare_eval_split(
dataset: Dataset,
data_training_args: DataTrainingArguments,
add_serialized_schema: Callable[[dict], dict],
pre_process_function: Callable[[dict, Optional[int], Optional[int]], dict],
) -> EvalSplit:
if (
data_training_args.max_val_samples is not None
and data_training_args.max_val_samples < len(dataset)
):
eval_examples = dataset.select(range(data_training_args.max_val_samples))
else:
eval_examples = dataset
schemas = _get_schemas(examples=eval_examples)
eval_dataset = eval_examples.map(
add_serialized_schema,
batched=False,
num_proc=data_training_args.preprocessing_num_workers,
load_from_cache_file=not data_training_args.overwrite_cache,
)
column_names = eval_dataset.column_names
eval_dataset = eval_dataset.map(
lambda batch: pre_process_function(
batch=batch,
max_source_length=data_training_args.max_source_length,
max_target_length=data_training_args.val_max_target_length,
),
batched=True,
num_proc=data_training_args.preprocessing_num_workers,
remove_columns=column_names,
load_from_cache_file=not data_training_args.overwrite_cache,
)
return EvalSplit(dataset=eval_dataset, examples=eval_examples, schemas=schemas)
def prepare_splits(
dataset_dict: DatasetDict,
data_args: DataArguments,
training_args: TrainingArguments,
data_training_args: DataTrainingArguments,
add_serialized_schema: Callable[[dict], dict],
pre_process_function: Callable[[dict, Optional[int], Optional[int]], dict],
spider_dataset_dict: DatasetDict = None,
spider_pre_process_function: Callable[
[dict, Optional[int], Optional[int]], dict
] = None,
spider_add_serialized_schema: Callable[[dict], dict] = None,
) -> DatasetSplits:
train_split, eval_split, test_splits = None, None, None
if training_args.do_train:
train_split = _prepare_train_split(
dataset_dict["train"],
data_training_args=data_training_args,
add_serialized_schema=add_serialized_schema,
pre_process_function=pre_process_function,
)
if data_training_args.splash_train_with_spider:
# should have about 17496 training instances now
assert spider_dataset_dict is not None
spider_train_split = _prepare_train_split(
spider_dataset_dict["train"],
data_training_args=data_training_args,
add_serialized_schema=spider_add_serialized_schema,
pre_process_function=spider_pre_process_function,
)
# Spider train split schemas has 140 keys, splash has 111
# interleave train sets, but use spider schemas
interleaved_datasets = interleave_datasets(
[train_split.dataset, spider_train_split.dataset],
probabilities=[0.65, 0.35],
stopping_strategy="all_exhausted",
)
train_split.dataset = interleaved_datasets
train_split.schemas = spider_train_split.schemas
if training_args.do_eval:
# import datasets
# import pandas as pd
# val_subset = datasets.Dataset.from_pandas(pd.DataFrame(data=[i for i in dataset_dict["validation"] if i["db_id"] == "world_1"][:5]))
# dataset_dict["validation"]
eval_split = _prepare_eval_split(
dataset_dict["validation"],
data_training_args=data_training_args,
add_serialized_schema=add_serialized_schema,
pre_process_function=pre_process_function,
)
if training_args.do_predict:
test_splits = {
section: _prepare_eval_split(
dataset_dict[section],
data_training_args=data_training_args,
add_serialized_schema=add_serialized_schema,
pre_process_function=pre_process_function,
)
for section in data_args.test_sections
}
test_split_schemas = {}
for split in test_splits.values():
test_split_schemas.update(split.schemas)
schemas = {
**(train_split.schemas if train_split is not None else {}),
**(eval_split.schemas if eval_split is not None else {}),
**(test_split_schemas if test_splits is not None else {}),
}
return DatasetSplits(
train_split=train_split,
eval_split=eval_split,
test_splits=test_splits,
schemas=schemas,
)
def normalize(sql):
"""
https://github.com/RUCKBReasoning/RESDSQL
preprocessing.py
"""
def white_space_fix(s):
parsed_s = Parser(s)
s = " ".join([token.value for token in parsed_s.tokens])
return s
# convert everything except text between single quotation marks to lower case
def lower(s):
in_quotation = False
out_s = ""
for char in s:
if in_quotation:
out_s += char
else:
out_s += char.lower()
if char == "'":
if in_quotation:
in_quotation = False
else:
in_quotation = True
return out_s
# remove ";"
def remove_semicolon(s):
if s.endswith(";"):
s = s[:-1]
return s
# double quotation -> single quotation
def double2single(s):
return s.replace('"', "'")
def add_asc(s):
pattern = re.compile(
r"order by (?:\w+ \( \S+ \)|\w+\.\w+|\w+)(?: (?:\+|\-|\<|\<\=|\>|\>\=) (?:\w+ \( \S+ \)|\w+\.\w+|\w+))*"
)
if "order by" in s and "asc" not in s and "desc" not in s:
for p_str in pattern.findall(s):
s = s.replace(p_str, p_str + " asc")
return s
def remove_table_alias(s):
tables_aliases = Parser(s).tables_aliases
new_tables_aliases = {}
for i in range(1, 11):
if "t{}".format(i) in tables_aliases.keys():
new_tables_aliases["t{}".format(i)] = tables_aliases["t{}".format(i)]
tables_aliases = new_tables_aliases
for k, v in tables_aliases.items():
s = s.replace("as " + k + " ", "")
s = s.replace(k, v)
return s
processing_func = lambda x: remove_table_alias(
add_asc(lower(white_space_fix(double2single(remove_semicolon(x)))))
)
return processing_func(sql)
def serialize_schema(
question: str,
db_path: str,
db_id: str,
db_column_names: Dict[str, str],
db_table_names: List[str],
schema_serialization_type: str = "peteshaw",
schema_serialization_randomized: bool = False,
schema_serialization_with_db_id: bool = True,
schema_serialization_with_db_content: bool = False,
normalize_query: bool = True,
use_gold_concepts: bool = False,
query: str = None,
) -> str:
if use_gold_concepts and not query:
raise ValueError(
"If use_gold_concepts is True, need to pass gold SQL query as well"
)
if schema_serialization_type == "verbose":
db_id_str = "Database: {db_id}. "
table_sep = ". "
table_str = "Table: {table}. Columns: {columns}"
column_sep = ", "
column_str_with_values = "{column} ({values})"
column_str_without_values = "{column}"
value_sep = ", "
elif schema_serialization_type == "peteshaw":
# see https://github.com/google-research/language/blob/master/language/nqg/tasks/spider/append_schema.py#L42
db_id_str = " | {db_id}"
table_sep = ""
table_str = " | {table} : {columns}"
column_sep = " , "
column_str_with_values = "{column} ( {values} )"
column_str_without_values = "{column}"
value_sep = " , "
else:
raise NotImplementedError
def get_column_str(
table_name: str, column_name: str, gold_values: List[str] = None
) -> str:
column_name_str = column_name.lower() if normalize_query else column_name
if schema_serialization_with_db_content:
if use_gold_concepts:
# Encode the gold values from query
if gold_values:
return column_str_with_values.format(
column=column_name_str, values=value_sep.join(gold_values)
)
else:
return column_str_without_values.format(column=column_name_str)
else:
matches = get_database_matches(
question=question,
table_name=table_name,
column_name=column_name,
db_path=(db_path + "/" + db_id + "/" + db_id + ".sqlite"),
)
if matches:
return column_str_with_values.format(
column=column_name_str, values=value_sep.join(matches)
)
else:
return column_str_without_values.format(column=column_name_str)
else:
return column_str_without_values.format(column=column_name_str)
if use_gold_concepts:
# Filter down schema, only to those concepts included in gold SQL
try:
ssql = SpiderSQL(
data_dir="seq2seq/datasets/spider/spider",
db_path_fmt="database/{db_id}/{db_id}.sqlite"
)
items = ssql.to_gold_concepts(query, db_id=db_id)
db_column_names = items.get("db_column_names")
db_table_names = items.get("db_table_names")
except Exception as e:
print(e)
print(f"ERROR: {question}")
else:
# Just use the full 'db_column_names', 'db_table_names' we passed into this function
pass
tables = [
table_str.format(
table=table_name.lower() if normalize_query else table_name,
columns=column_sep.join(
map(
lambda y: get_column_str(
table_name=table_name, column_name=y[1], gold_values=y[2]
),
filter(
lambda y: y[0] == table_id,
zip(
db_column_names["table_id"],
db_column_names["column_name"],
db_column_names.get(
"values", [None] * len(db_column_names["column_name"])
),
),
),
)
),
)
for table_id, table_name in enumerate(db_table_names)
]
if schema_serialization_randomized:
random.shuffle(tables)
if schema_serialization_with_db_id:
serialized_schema = db_id_str.format(db_id=db_id) + table_sep.join(tables)
else:
serialized_schema = table_sep.join(tables)
# print()
# print("**************************************************************************")
# print(query)
# print(serialized_schema)
# print("**************************************************************************")
# print()
return serialized_schema