| import json |
| import torch |
| import gc |
|
|
| from datasets import Dataset |
| from torch.utils.data import Dataset |
| from schema_item_filter import SchemaItemClassifierInference, filter_schema, filter_schema_purple |
| from utils.db_utils import get_db_schema_sequence, get_matched_content_sequence |
|
|
| def prepare_text2sql_prefix_sequence(data): |
| prompt = f"""Convert the question to SQL query. |
| {data["schema_sequence"]} |
| {data["content_sequence"]} |
| question: {data["text"]}""" |
| return prompt |
|
|
| def prepare_inputs_and_labels(prefix_seq, target_seq, tokenizer, max_tokens): |
| prefix_ids = [tokenizer.bos_token_id] + tokenizer(prefix_seq , truncation = False)["input_ids"] |
| target_ids = tokenizer(target_seq, truncation = False)["input_ids"] + [tokenizer.eos_token_id] |
|
|
| seq_length = len(prefix_ids) + len(target_ids) |
| if seq_length <= max_tokens: |
| pad_length = max_tokens - seq_length |
| input_ids = prefix_ids + target_ids + [tokenizer.pad_token_id] * pad_length |
| |
| attention_mask = [1] * seq_length + [0] * pad_length |
| |
| labels = [-100] * len(prefix_ids) + target_ids + [-100] * pad_length |
| else: |
| print("the current input sequence exceeds the max_tokens, we will truncate it.") |
| input_ids = prefix_ids + target_ids |
| |
| input_ids = [tokenizer.bos_token_id] + input_ids[-(max_tokens-1):] |
| attention_mask = [1] * max_tokens |
| |
| labels = [-100] * len(prefix_ids) + target_ids |
| |
| labels = labels[-max_tokens:] |
| |
| return { |
| "input_ids": torch.tensor(input_ids, dtype = torch.int64), |
| "attention_mask": torch.tensor(attention_mask, dtype = torch.int64), |
| "labels": torch.tensor(labels, dtype = torch.int64) |
| } |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def prepare_inputs(prefix_seq, tokenizer, max_prefix_length): |
| messages = [{ |
| 'role': 'user', |
| 'content': prefix_seq |
| }] |
| prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
|
|
| input_ids = tokenizer(prompt , truncation = False)["input_ids"] |
|
|
| if len(input_ids) > max_prefix_length: |
| print("the current input sequence exceeds the max_tokens, we will truncate it.") |
| input_ids = input_ids[-(max_prefix_length-1):] |
| |
| attention_mask = [1] * len(input_ids) |
| |
| return { |
| "input_ids": torch.tensor(input_ids, dtype = torch.int64), |
| "attention_mask": torch.tensor(attention_mask, dtype = torch.int64) |
| } |
|
|
| class SFTSQLGenerationDataset(Dataset): |
| def __init__(self, text2sql_data_dir, tokenizer, max_tokens, mode, table_num, column_num, threshold, sic_path, do_filter_schema=True): |
| super().__init__() |
| dataset = json.load(open(text2sql_data_dir)) |
|
|
| print("apply filtering strategies...") |
| if do_filter_schema: |
| if mode == "train": |
| dataset = filter_schema(dataset, "train", None, table_num, column_num, threshold=threshold) |
| elif mode == "eval": |
| sic = SchemaItemClassifierInference(sic_path) |
| dataset = filter_schema(dataset, "eval", sic, table_num, column_num, threshold=threshold) |
| |
| del sic |
| torch.cuda.empty_cache() |
|
|
| |
| for data in dataset: |
| data["schema_sequence"] = get_db_schema_sequence(data["schema"]) |
| |
|
|
| self.mode = mode |
| self.dataset = dataset |
| self.tokenizer = tokenizer |
| self.max_tokens = max_tokens |
|
|
| def __getitem__(self, index): |
| data = self.dataset[index] |
| prefix_seq = prepare_text2sql_prefix_sequence(data) |
| if index < 2: |
| print(prefix_seq) |
|
|
| if self.mode == "train": |
| target_seq = data["sql"] |
| return prepare_inputs_and_labels(prefix_seq, target_seq, self.tokenizer, self.max_tokens) |
| elif self.mode == "eval": |
| return prepare_inputs(prefix_seq, self.tokenizer, self.max_tokens) |
|
|
| def __len__(self): |
| return len(self.dataset) |