Save a model and its configuration file to a directory so that it can be reloaded using the [`~models.ModelMixin.from_pretrained`] class method. Arguments: save_directory (`str` or `os.PathLike`): Directory to save a model and its configuration f
(
self,
save_directory: str | os.PathLike,
is_main_process: bool = True,
save_function: Callable | None = None,
safe_serialization: bool = True,
variant: str | None = None,
max_shard_size: int | str = "10GB",
push_to_hub: bool = False,
use_flashpack: bool = False,
**kwargs,
)
| 667 | processor._attention_backend = None |
| 668 | |
| 669 | def save_pretrained( |
| 670 | self, |
| 671 | save_directory: str | os.PathLike, |
| 672 | is_main_process: bool = True, |
| 673 | save_function: Callable | None = None, |
| 674 | safe_serialization: bool = True, |
| 675 | variant: str | None = None, |
| 676 | max_shard_size: int | str = "10GB", |
| 677 | push_to_hub: bool = False, |
| 678 | use_flashpack: bool = False, |
| 679 | **kwargs, |
| 680 | ): |
| 681 | """ |
| 682 | Save a model and its configuration file to a directory so that it can be reloaded using the |
| 683 | [`~models.ModelMixin.from_pretrained`] class method. |
| 684 | |
| 685 | Arguments: |
| 686 | save_directory (`str` or `os.PathLike`): |
| 687 | Directory to save a model and its configuration file to. Will be created if it doesn't exist. |
| 688 | is_main_process (`bool`, *optional*, defaults to `True`): |
| 689 | Whether the process calling this is the main process or not. Useful during distributed training and you |
| 690 | need to call this function on all processes. In this case, set `is_main_process=True` only on the main |
| 691 | process to avoid race conditions. |
| 692 | save_function (`Callable`): |
| 693 | The function to use to save the state dictionary. Useful during distributed training when you need to |
| 694 | replace `torch.save` with another method. Can be configured with the environment variable |
| 695 | `DIFFUSERS_SAVE_MODE`. |
| 696 | safe_serialization (`bool`, *optional*, defaults to `True`): |
| 697 | Whether to save the model using `safetensors` or the traditional PyTorch way with `pickle`. |
| 698 | variant (`str`, *optional*): |
| 699 | If specified, weights are saved in the format `pytorch_model.<variant>.bin`. |
| 700 | max_shard_size (`int` or `str`, defaults to `"10GB"`): |
| 701 | The maximum size for a checkpoint before being sharded. Checkpoints shard will then be each of size |
| 702 | lower than this size. If expressed as a string, needs to be digits followed by a unit (like `"5GB"`). |
| 703 | If expressed as an integer, the unit is bytes. Note that this limit will be decreased after a certain |
| 704 | period of time (starting from Oct 2024) to allow users to upgrade to the latest version of `diffusers`. |
| 705 | This is to establish a common default size for this argument across different libraries in the Hugging |
| 706 | Face ecosystem (`transformers`, and `accelerate`, for example). |
| 707 | push_to_hub (`bool`, *optional*, defaults to `False`): |
| 708 | Whether or not to push your model to the Hugging Face Hub after saving it. You can specify the |
| 709 | repository you want to push to with `repo_id` (will default to the name of `save_directory` in your |
| 710 | namespace). |
| 711 | kwargs (`dict[str, Any]`, *optional*): |
| 712 | Additional keyword arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method. |
| 713 | """ |
| 714 | if os.path.isfile(save_directory): |
| 715 | logger.error(f"Provided path ({save_directory}) should be a directory, not a file") |
| 716 | return |
| 717 | |
| 718 | hf_quantizer = getattr(self, "hf_quantizer", None) |
| 719 | if hf_quantizer is not None: |
| 720 | quantization_serializable = ( |
| 721 | hf_quantizer is not None |
| 722 | and isinstance(hf_quantizer, DiffusersQuantizer) |
| 723 | and hf_quantizer.is_serializable |
| 724 | ) |
| 725 | if not quantization_serializable: |
| 726 | raise ValueError( |
nothing calls this directly
no test coverage detected