|
| 1 | +import os |
| 2 | +import random |
| 3 | +from copy import deepcopy |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +import torch.utils.data.dataset |
| 7 | +from datasets import Dataset, load_dataset, concatenate_datasets |
| 8 | +from transformers import DataCollatorForWholeWordMask |
| 9 | + |
| 10 | +from .utils import tensorize_batch |
| 11 | + |
| 12 | + |
| 13 | +class DatasetForPretraining(torch.utils.data.Dataset): |
| 14 | + def __init__(self, data_dir): |
| 15 | + if os.path.isdir(data_dir): |
| 16 | + datasets = [] |
| 17 | + for file in os.listdir(data_dir): |
| 18 | + print(f"Loading {file}") |
| 19 | + file = os.path.join(data_dir, file) |
| 20 | + datasets.append(self.load_dataset(file)) |
| 21 | + self.dataset = concatenate_datasets(datasets) |
| 22 | + else: |
| 23 | + print(f"Loading {data_dir}") |
| 24 | + self.dataset = self.load_dataset(data_dir) |
| 25 | + |
| 26 | + def load_dataset(self, file): |
| 27 | + if file.endswith('.jsonl') or file.endswith('.json'): |
| 28 | + return load_dataset('json', data_files=file)['train'] |
| 29 | + elif os.path.isdir(file): |
| 30 | + return Dataset.load_from_disk(file) |
| 31 | + else: |
| 32 | + raise NotImplementedError(f"Not support this file format:{file}") |
| 33 | + |
| 34 | + def __getitem__(self, item): |
| 35 | + return self.dataset[item]['text'] |
| 36 | + |
| 37 | + def __len__(self): |
| 38 | + return len(self.dataset) |
| 39 | + |
| 40 | + |
| 41 | +@dataclass |
| 42 | +class RetroMAECollator(DataCollatorForWholeWordMask): |
| 43 | + max_seq_length: int = 512 |
| 44 | + encoder_mlm_probability: float = 0.15 |
| 45 | + decoder_mlm_probability: float = 0.15 |
| 46 | + |
| 47 | + def __call__(self, examples): |
| 48 | + input_ids_batch = [] |
| 49 | + attention_mask_batch = [] |
| 50 | + encoder_mlm_mask_batch = [] |
| 51 | + decoder_labels_batch = [] |
| 52 | + decoder_matrix_attention_mask_batch = [] |
| 53 | + |
| 54 | + for e in examples: |
| 55 | + |
| 56 | + e_trunc = self.tokenizer.encode(e, max_length=self.max_seq_length, truncation=True) |
| 57 | + tokens = [self.tokenizer._convert_id_to_token(tid) for tid in e_trunc] |
| 58 | + |
| 59 | + self.mlm_probability = self.encoder_mlm_probability |
| 60 | + text_encoder_mlm_mask = self._whole_word_mask(tokens) |
| 61 | + |
| 62 | + self.mlm_probability = self.decoder_mlm_probability |
| 63 | + mask_set = [] |
| 64 | + for _ in range(min(len(tokens), 128)): |
| 65 | + mask_set.append(self._whole_word_mask(tokens)) |
| 66 | + |
| 67 | + text_matrix_attention_mask = [] |
| 68 | + for i in range(len(tokens)): |
| 69 | + idx = random.randint(0, min(len(tokens), 128) - 1) |
| 70 | + text_decoder_mlm_mask = deepcopy(mask_set[idx]) |
| 71 | + text_decoder_mlm_mask[i] = 1 |
| 72 | + text_matrix_attention_mask.append(text_decoder_mlm_mask) |
| 73 | + |
| 74 | + input_ids_batch.append(torch.tensor(e_trunc)) |
| 75 | + attention_mask_batch.append(torch.tensor([1] * len(e_trunc))) |
| 76 | + e_trunc[0] = -100 |
| 77 | + e_trunc[-1] = -100 |
| 78 | + decoder_labels_batch.append(torch.tensor(e_trunc)) |
| 79 | + |
| 80 | + encoder_mlm_mask_batch.append(torch.tensor(text_encoder_mlm_mask)) |
| 81 | + decoder_matrix_attention_mask_batch.append(1 - torch.tensor(text_matrix_attention_mask)) |
| 82 | + |
| 83 | + input_ids_batch = tensorize_batch(input_ids_batch, self.tokenizer.pad_token_id) |
| 84 | + attention_mask_batch = tensorize_batch(attention_mask_batch, 0) |
| 85 | + origin_input_ids_batch = input_ids_batch.clone() |
| 86 | + encoder_mlm_mask_batch = tensorize_batch(encoder_mlm_mask_batch, 0) |
| 87 | + encoder_input_ids_batch, encoder_labels_batch = self.torch_mask_tokens(input_ids_batch, encoder_mlm_mask_batch) |
| 88 | + decoder_labels_batch = tensorize_batch(decoder_labels_batch, -100) |
| 89 | + matrix_attention_mask_batch = tensorize_batch(decoder_matrix_attention_mask_batch, 0) |
| 90 | + |
| 91 | + batch = { |
| 92 | + "encoder_input_ids": encoder_input_ids_batch, |
| 93 | + "encoder_attention_mask": attention_mask_batch, |
| 94 | + "encoder_labels": encoder_labels_batch, |
| 95 | + "decoder_input_ids": origin_input_ids_batch, |
| 96 | + "decoder_attention_mask": matrix_attention_mask_batch, # [B,L,L] |
| 97 | + "decoder_labels": decoder_labels_batch, |
| 98 | + } |
| 99 | + |
| 100 | + return batch |
0 commit comments