save the pretrained model, its configuration and other related files to a directory, so that it can be re-loaded Args: model (Model): Model whose params are to be saved. target_folder (Union[str, os.PathLike]): Directory to which to save. Will be created if it doesn't e
(model,
target_folder: Union[str, os.PathLike],
save_checkpoint_name: str = None,
save_function: Callable = None,
**kwargs)
| 582 | |
| 583 | |
| 584 | def save_pretrained(model, |
| 585 | target_folder: Union[str, os.PathLike], |
| 586 | save_checkpoint_name: str = None, |
| 587 | save_function: Callable = None, |
| 588 | **kwargs): |
| 589 | """save the pretrained model, its configuration and other related files to a directory, so that it can be re-loaded |
| 590 | |
| 591 | Args: |
| 592 | model (Model): Model whose params are to be saved. |
| 593 | |
| 594 | target_folder (Union[str, os.PathLike]): |
| 595 | Directory to which to save. Will be created if it doesn't exist. |
| 596 | |
| 597 | save_checkpoint_name (str): |
| 598 | The checkpoint name to be saved in the target_folder |
| 599 | |
| 600 | save_function (Callable): |
| 601 | The function to use to save the state dictionary. |
| 602 | """ |
| 603 | |
| 604 | if save_function is None or not isinstance(save_function, Callable): |
| 605 | raise Exception('A valid save function must be passed in') |
| 606 | |
| 607 | if target_folder is None or os.path.isfile(target_folder): |
| 608 | raise ValueError( |
| 609 | f'Provided path ({target_folder}) should be a directory, not a file' |
| 610 | ) |
| 611 | |
| 612 | if save_checkpoint_name is None: |
| 613 | raise Exception( |
| 614 | 'At least pass in one checkpoint name for saving method') |
| 615 | |
| 616 | # Single ckpt path, sharded ckpt logic will be added later |
| 617 | output_ckpt_path = os.path.join(target_folder, save_checkpoint_name) |
| 618 | |
| 619 | # Save the files to be copied to the save directory, ignore the original ckpts and configuration |
| 620 | origin_file_to_be_ignored = [save_checkpoint_name] |
| 621 | ignore_file_set = set(origin_file_to_be_ignored) |
| 622 | ignore_file_set.add(ModelFile.CONFIGURATION) |
| 623 | ignore_file_set.add('*.safetensors') |
| 624 | ignore_file_set.add('.*') |
| 625 | if hasattr(model, |
| 626 | 'model_dir') and model.model_dir is not None and is_master(): |
| 627 | if sys.version_info.minor >= 8: |
| 628 | copytree_func = copytree |
| 629 | else: # == 7 |
| 630 | copytree_func = copytree_py37 |
| 631 | copytree_func( |
| 632 | model.model_dir, |
| 633 | target_folder, |
| 634 | ignore=ignore_patterns(*ignore_file_set), |
| 635 | dirs_exist_ok=True) |
| 636 | |
| 637 | # Save the ckpt to the save directory |
| 638 | try: |
| 639 | save_function(model, output_ckpt_path, **kwargs) |
| 640 | except Exception as e: |
| 641 | raise Exception( |
no test coverage detected
searching dependent graphs…