Diffusers documentation

配置

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.43.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

配置

来自 SchedulerMixin 的调度器和来自 ModelMixin 的模型都继承自 ConfigMixin,后者会将传入其各自 __init__ 方法的所有参数存储在一个 JSON 配置文件中。

要使用私有或 受限 模型,请使用 hf auth login 登录。

ConfigMixin

类 diffusers.ConfigMixin

< >

( )

所有配置类的基类。所有配置参数都存储在 self.config 下。同时提供 from_config() 和 save_config() 方法,用于加载、下载和保存继承自 ConfigMixin 的类。

类属性:

  • config_name (str) — 调用 save_config() 时用于存储配置的配置文件名(应由父类覆盖)。
  • ignore_for_config (list[str]) — 不应保存在配置中的属性列表(应由子类覆盖)。
  • has_compatibles (bool) — 该类是否具有兼容类(应由子类重写)。
  • _deprecated_kwargs (list[str]) — 已弃用的关键字参数。请注意,init 函数只有在至少有一个参数被弃用时才应包含 kwargs 参数(应由子类覆盖)。

load_config

< >

( pretrained_model_name_or_path: str | os.PathLikereturn_unused_kwargs = Falsereturn_commit_hash = False**kwargs ) → dict

参数

  • pretrained_model_name_or_path (str 或 os.PathLike, 可选) — 可以是以下之一:
    • 一个字符串,即托管在 Hub 上的预训练模型的 model id(例如 google/ddpm-celebahq-256)。
    • 指向一个目录的路径(例如 ./my_model_directory),其中包含使用 save_config() 保存的模型权重。
  • cache_dir (str | os.PathLike, 可选) — 如果未使用标准缓存,则下载的预训练模型配置将被缓存到的目录路径。
  • force_download (bool,可选,默认为 False) — 是否强制(重新)下载模型权重和配置文件,如果存在缓存版本则覆盖它们。
  • proxies (dict[str, str],可选) — 按协议或端点使用的代理服务器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。每次请求都会使用这些代理。
  • output_loading_info(bool, 可选,默认为 False) — 是否同时返回一个包含缺失键、意外键和错误消息的字典。
  • local_files_only (bool,可选,默认为 False) — 是否仅加载本地的模型权重和配置文件。如果设置为 True,则不会从 Hub 下载模型。
  • token (str 或 bool,可选) — 用于远程文件的 HTTP bearer 授权令牌。如果为 True,则使用由 diffusers-cli login 生成的令牌(存储在 ~/.huggingface 中)。
  • revision (str, 可选, 默认为 "main") — 要使用的特定模型版本。它可以是分支名称、标签名称、提交 id,或 Git 允许的任何标识符。
  • subfolder (str,可选,默认为 "") — 模型文件在 Hub 或本地更大的模型仓库中的子文件夹位置。
  • return_unused_kwargs (bool,可选,默认为 `False) — 是否返回配置中未使用的关键字参数。
  • return_commit_hash (bool, optional, defaults to False) -- Whether the commit_hash` of the loaded configuration are returned.

返回值

dict

存储在 JSON 配置文件中的所有参数的字典。

加载模型或调度器配置。

from_config

< >

( config: diffusers.configuration_utils.FrozenDict | dict[str, typing.Any] = Nonereturn_unused_kwargs = False**kwargs ) → ModelMixin or SchedulerMixin

参数

  • config (dict[str, Any]) — 用于实例化 Python 类的配置字典。请确保仅加载兼容类的配置文件。
  • return_unused_kwargs (bool,可选,默认为 False) — 是否返回未被 Python 类使用的 kwargs。
  • kwargs(其余关键字参数字典,可选) — 可用于更新配置对象(加载后)并初始化 Python 类。 **kwargs 会直接传递给底层调度器/模型的 __init__ 方法,并最终覆盖 config 中同名的参数。

返回值

ModelMixin 或 SchedulerMixin

从配置字典实例化的模型或调度器对象。

从配置字典实例化一个 Python 类。

示例:

>>> from diffusers import DDPMScheduler, DDIMScheduler, PNDMScheduler

>>> # Download scheduler from huggingface.co and cache.
>>> scheduler = DDPMScheduler.from_pretrained("google/ddpm-cifar10-32")

>>> # Instantiate DDIM scheduler class with same config as DDPM
>>> scheduler = DDIMScheduler.from_config(scheduler.config)

>>> # Instantiate PNDM scheduler class with same config as DDPM
>>> scheduler = PNDMScheduler.from_config(scheduler.config)

save_config

< >

( save_directory: str | os.PathLikepush_to_hub: bool = False**kwargs )

参数

  • save_directory (str 或 os.PathLike) — 保存配置 JSON 文件的目录(如果该目录不存在,将会创建)。
  • push_to_hub (bool,可选,默认为 False) — 是否在保存模型后将其推送到 Hugging Face Hub。您可以使用 repo_id 指定要推送到的仓库(默认使用您命名空间中 save_directory 的名称)。
  • kwargs (dict[str, Any],可选) — 传递给 push_to_hub() 方法的额外关键字参数。

将配置对象保存到 save_directory 指定的目录中,以便使用 from_config() 类方法重新加载。

to_json_file

< >

( json_file_path: str | os.PathLike )

参数

  • json_file_path (str 或 os.PathLike) — 用于保存配置实例参数的 JSON 文件路径。

将配置实例的参数保存到 JSON 文件中。

to_json_string

< >

( ) → str

返回值

str

以 JSON 格式包含构成配置实例的所有属性的字符串。

将配置实例序列化为 JSON 字符串。

Update on GitHub