Get an iterator over batches of data. Stops after n_epochs or n_examples, whichever comes first. Args: names: Names of datasets to use. tokenizer: Tokenizer to use. split: Which split to use. batch_size: Batch size. shuffle: Whether to shuffle the data af
(names: List[str],
tokenizer,
split: str = 'train',
batch_size: int = 1,
shuffle: bool = True,
max_length: int = 512,
max_prompt_length: int = 128,
sft_mode: bool = False,
n_epochs: Optional[int] = None,
n_examples: Optional[int] = None,
seed:int = 0,
silent: bool = False,
cache_dir: Optional[str] = None)
| 278 | |
| 279 | |
| 280 | def get_batch_iterator(names: List[str], |
| 281 | tokenizer, |
| 282 | split: str = 'train', |
| 283 | batch_size: int = 1, |
| 284 | shuffle: bool = True, |
| 285 | max_length: int = 512, |
| 286 | max_prompt_length: int = 128, |
| 287 | sft_mode: bool = False, |
| 288 | n_epochs: Optional[int] = None, |
| 289 | n_examples: Optional[int] = None, |
| 290 | seed:int = 0, |
| 291 | silent: bool = False, |
| 292 | cache_dir: Optional[str] = None) -> Iterator[Dict]: |
| 293 | """Get an iterator over batches of data. Stops after n_epochs or n_examples, whichever comes first. |
| 294 | |
| 295 | Args: |
| 296 | names: Names of datasets to use. |
| 297 | tokenizer: Tokenizer to use. |
| 298 | split: Which split to use. |
| 299 | batch_size: Batch size. |
| 300 | shuffle: Whether to shuffle the data after each epoch. |
| 301 | max_length: Maximum length of the combined prompt + response. |
| 302 | max_prompt_length: Maximum length of the prompt. |
| 303 | sft_mode: Whether to use SFT mode (i.e., return sft_target instead of chosen/rejected). In sft mode, we just return chosen_input_ids, but they contain the sft_target. |
| 304 | n_epochs: Number of epochs to run for. This or n_examples must be specified. |
| 305 | n_examples: Number of examples to run for. This or n_epochs must be specified. |
| 306 | seed: Random seed. |
| 307 | silent: Whether to silence the progress bar(s). |
| 308 | cache_dir: Directory to cache the datasets in. |
| 309 | """ |
| 310 | assert n_epochs is not None or n_examples is not None, "Must specify either n_epochs or n_examples" |
| 311 | if silent: |
| 312 | datasets.logging.disable_progress_bar() |
| 313 | datasets.logging.set_verbosity_error() |
| 314 | |
| 315 | with TemporarilySeededRandom(seed): |
| 316 | permutation_seeds = iter(np.random.randint(0, 2**32, size=1000000)) |
| 317 | flat_data = [] |
| 318 | for name in names: |
| 319 | truncation_mode = 'keep_end' if name == 'hh' else 'keep_start' |
| 320 | for prompt, data in get_dataset(name, split, silent=silent, cache_dir=cache_dir).items(): |
| 321 | flat_data.append((prompt, data['responses'], data['pairs'], data['sft_target'], truncation_mode)) |
| 322 | |
| 323 | collate_fn = get_collate_fn(tokenizer) |
| 324 | |
| 325 | epoch_idx = 0 |
| 326 | example_idx = 0 |
| 327 | done = False |
| 328 | while True: |
| 329 | if n_epochs is not None and epoch_idx >= n_epochs: |
| 330 | if not silent: |
| 331 | print(f'Finished generating {n_epochs} epochs on {split} split') |
| 332 | break |
| 333 | if shuffle: |
| 334 | with TemporarilySeededRandom(next(permutation_seeds)): |
| 335 | random.shuffle(flat_data) |
| 336 | |
| 337 | batch = [] |
no test coverage detected