Huggingface dataset, where the dataset is loaded using the huggingface datasets.load_dataset() function.
| 240 | |
| 241 | |
| 242 | class HuggingfaceDataset(object): |
| 243 | """ Huggingface dataset, where the dataset is loaded using the huggingface |
| 244 | datasets.load_dataset() function. |
| 245 | """ |
| 246 | |
| 247 | @staticmethod |
| 248 | def get_default_config(updates=None): |
| 249 | config = ConfigDict() |
| 250 | config.path = 'c4' |
| 251 | config.name = 'en' |
| 252 | config.split = 'train' |
| 253 | config.streaming = False |
| 254 | config.seq_length = 1024 |
| 255 | config.batch_size = 8 |
| 256 | config.always_start_with_bos = False |
| 257 | |
| 258 | if updates is not None: |
| 259 | config.update(ConfigDict(updates).copy_and_resolve_references()) |
| 260 | return config |
| 261 | |
| 262 | def __init__(self, config, tokenizer, text_processor): |
| 263 | self.config = self.get_default_config(config) |
| 264 | name = self.config.name if self.config.name != '' else None |
| 265 | split = self.config.split if self.config.split != '' else None |
| 266 | self._tokenizer = tokenizer |
| 267 | self._text_processor = text_processor |
| 268 | self._dataset = load_dataset( |
| 269 | self.config.path, name, split=split, streaming=self.config.streaming |
| 270 | ) |
| 271 | |
| 272 | def __iter__(self): |
| 273 | chunk_size = self.config.batch_size * self.config.seq_length |
| 274 | total_tokens = 0 |
| 275 | while True: |
| 276 | token_buffer = [] |
| 277 | loss_mask_buffer = [] |
| 278 | for index, example in enumerate(self._dataset): |
| 279 | tokens, loss_masks = self.text_processor(example) |
| 280 | token_buffer.extend(tokens) |
| 281 | loss_mask_buffer.extend(loss_masks) |
| 282 | while len(token_buffer) > chunk_size + 1: |
| 283 | total_tokens += chunk_size |
| 284 | metrics = { |
| 285 | 'dataset_example_index': index, |
| 286 | 'dataset_total_tokens': total_tokens, |
| 287 | } |
| 288 | batch = { |
| 289 | 'input_tokens': np.array(token_buffer[:chunk_size], dtype=np.int32).reshape( |
| 290 | self.config.batch_size, -1 |
| 291 | ), |
| 292 | 'target_tokens': np.array(token_buffer[1:chunk_size + 1], dtype=np.int32).reshape( |
| 293 | self.config.batch_size, -1 |
| 294 | ), |
| 295 | 'loss_masks': np.array(loss_mask_buffer[1:chunk_size + 1], dtype=np.float32).reshape( |
| 296 | self.config.batch_size, -1 |
| 297 | ), |
| 298 | } |
| 299 | if self.config.always_start_with_bos: |