A class for processing a Bloom text dataset
| 19 | from linear_moe.tokenizer import get_tokenizer |
| 20 | |
| 21 | class BloomRawDataset(torch.utils.data.Dataset): |
| 22 | """A class for processing a Bloom text dataset""" |
| 23 | def __init__(self, datapaths, max_seq_length): |
| 24 | """ |
| 25 | Initializes the dataset. |
| 26 | Args: |
| 27 | path(str): The path of the dataset file. |
| 28 | tokenizer(object): The tokenizer object. |
| 29 | max_seq_length(int): The maximum length of sequences. |
| 30 | """ |
| 31 | self.tokenizer = get_tokenizer() |
| 32 | self.max_seq_length = max_seq_length |
| 33 | self.prompt = '' |
| 34 | self.samples = [] |
| 35 | for datapath in datapaths: |
| 36 | self.samples.extend( |
| 37 | self.process_samples_from_single_path(datapath)) |
| 38 | print(' >> total number of samples: {}'.format(len(self.samples))) |
| 39 | |
| 40 | def __len__(self): |
| 41 | return len(self.samples) |
| 42 | |
| 43 | def __getitem__(self, idx): |
| 44 | raw_sample = self.samples[idx] |
| 45 | return self.gpt_convert_example_to_feature(raw_sample, self.tokenizer, |
| 46 | self.max_seq_length) |
| 47 | |
| 48 | def truncate(self, tokenizer, array, max_length): |
| 49 | """ |
| 50 | Truncates an array to a maximum length or pads it with zeros if its length is less than `max_length`. |
| 51 | Args: |
| 52 | tokenizer: The tokenizer used to encode the input. |
| 53 | array: The numpy array to truncate or pad. |
| 54 | max_length: The maximum length of the array. |
| 55 | Returns: |
| 56 | A numpy array of length `max_length` containing the contents of `array`, truncated if necessary or padded with zeros. |
| 57 | """ |
| 58 | |
| 59 | if len(array) < max_length: |
| 60 | return np.pad(array, (0, max_length - len(array)), |
| 61 | constant_values=tokenizer.eod) |
| 62 | else: |
| 63 | return array[:max_length] |
| 64 | |
| 65 | def process_samples_from_single_path(self, filename): |
| 66 | """ |
| 67 | Process a single file containing prompt-answer pairs and return a list of samples. |
| 68 | """ |
| 69 | |
| 70 | print(' > Processing {} ...'.format(filename)) |
| 71 | samples = [] |
| 72 | total = 0 |
| 73 | with open(filename, encoding='utf-8-sig') as f: |
| 74 | for example in f: |
| 75 | text = json.loads(example)['text'] |
| 76 | sample = { |
| 77 | 'prompt': |
| 78 | text + '</s>' if not text.endswith('</s>') else text, |
no outgoing calls
no test coverage detected