Diffusers¶
关于本页
这是 BentoML 中 🤗 Diffusers 的 API 参考。有关如何在 BentoML 中使用 Hugging Face Diffusers 的更多信息,请参阅Diffusers 指南。
- bentoml.diffusers.import_model(name: Tag | str, model_name_or_path: str | os.PathLike[str], *, proxies: dict[str, str] | None = None, revision: str = 'main', variant: str | None = None, pipeline_class: str | type[diffusers.DiffusionPipeline] | None = None, sync_with_hub_version: bool = False, signatures: dict[str, ModelSignatureDict | ModelSignature] | None = None, labels: dict[str, str] | None = None, custom_objects: dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: dict[str, t.Any] | None = None) bentoml.Model ¶
将 Diffusion 模型从 artifact URI 导入到 BentoML 模型库。
- 参数:
name – 要在 BentoML 模型库中给模型指定的名称。这必须是有效的
Tag
名称。model_name_or_path –
可以是以下之一: - 一个字符串,即托管在模型仓库中的预训练 pipeline 的 repo id,位于
https://hugging-face.cn/ 有效的 repo ids 必须位于用户或组织名称下,例如 CompVis/ldm-text2im-large-256。
保存使用 [~DiffusionPipeline.save_pretrained] 的 pipeline 权重的 目录 路径,例如 ./my_pipeline_directory/。
proxies (Dict[str, str],可选) – 按协议或端点使用的代理服务器字典,例如 {‘http’: ‘foo.bar:3128’, ‘http://hostname’: ‘foo.bar:4012’}。每个请求都会使用这些代理。
revision (str,可选,默认为 “main”) – 要使用的特定模型版本。它可以是分支名称、标签名称或提交 ID,因为我们在 huggingface.co 上使用基于 Git 的系统存储模型和其他 artifacts,因此 revision 可以是 Git 允许的任何标识符。
variant (str,可选) – 要导入的模型的变体。例如,“DeepFloyd/IF-I-XL-v1.0” 有 “fp16” 和 “fp32” 变体。这可以节省下载带宽和本地磁盘空间。
sync_with_hub_version (bool,默认为 False) – 如果 sync_with_hub_version 为 true,则导入的模型
signatures – 要使用的 predict 方法的签名。如果未提供,签名默认为 {“__call__”: {“batchable”: False}}。有关更多详细信息,请参阅 ModelSignature。
labels – 与模型关联的默认管理标签集。例如:
{"training-set": "data-v1"}
。custom_objects – 与模型一起保存的自定义对象。例如
{"my-normalizer": normalizer}
。自定义对象使用 cloudpickle 序列化。metadata –
与模型关联的元数据。例如
{"param_a": .2}
。元数据旨在用于模型管理 UI 中的显示,因此元数据字典中的所有值必须是 Python 基本类型,例如
str
或int
。
- 返回值:
一个
Model
实例,引用本地 BentoML 模型库中已保存的模型。
示例
import bentoml bentoml.diffusers.import_model( 'my_sd15_model', "runwayml/stable-diffusion-v1-5", signatures={ "__call__": {"batchable": False}, } )
- bentoml.diffusers.save_model(name: Tag | str, pipeline: diffusers.DiffusionPipeline, *, signatures: dict[str, ModelSignatureDict | ModelSignature] | None = None, labels: dict[str, str] | None = None, custom_objects: dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: dict[str, t.Any] | None = None) bentoml.Model ¶
将 DiffusionPipeline 保存到 BentoML 模型库。
- 参数:
name – 要在 BentoML 模型库中给模型指定的名称。这必须是有效的
Tag
名称。pipeline – 要保存的 Diffusers pipeline 实例
signatures – 要使用的 predict 方法的签名。如果未提供,签名默认为 {“__call__”: {“batchable”: False}}。有关更多详细信息,请参阅 ModelSignature。
labels – 与模型关联的默认管理标签集。例如:
{"training-set": "data-v1"}
。custom_objects – 与模型一起保存的自定义对象。例如
{"my-normalizer": normalizer}
。自定义对象使用 cloudpickle 序列化。metadata –
与模型关联的元数据。例如
{"param_a": .2}
。元数据旨在用于模型管理 UI 中的显示,因此元数据字典中的所有值必须是 Python 基本类型,例如
str
或int
。
- 返回值:
一个
Model
实例,引用本地 BentoML 模型库中已保存的模型。
- bentoml.diffusers.load_model(bento_model: str | Tag | bentoml.Model, device_id: str | torch.device | None = None, pipeline_class: str | type[diffusers.pipelines.DiffusionPipeline] = diffusers.DiffusionPipeline, device_map: str | dict[str, int | str | torch.device] | None = None, custom_pipeline: str | None = None, scheduler_class: type[diffusers.SchedulerMixin] | None = None, torch_dtype: str | torch.dtype | None = None, low_cpu_mem_usage: bool | None = None, enable_xformers: bool = False, enable_attention_slicing: int | str | None = None, enable_model_cpu_offload: bool | None = None, enable_sequential_cpu_offload: bool | None = None, enable_torch_compile: bool | None = None, variant: str | None = None, lora_weights: LoraOptionType | list[LoraOptionType] | None = None, textual_inversions: TextualInversionOptionType | list[TextualInversionOptionType] | None = None, load_pretrained_extra_kwargs: dict[str, t.Any] | None = None) diffusers.DiffusionPipeline ¶
从本地 BentoML 模型库加载 Diffusion 模型,并将其转换为具有给定 tag 的 diffusers Pipeline。
- 参数:
bento_model – 要从库中获取的模型的 tag,或是用于加载模型的 BentoML
~bentoml.Model
实例。device_id (
str
,可选,默认为None
) – 可选的设备,用于放置给定模型。请参阅 device 属性。pipeline_class (
type[diffusers.DiffusionPipeline]
,可选) – 用于加载已保存 Diffusion 模型的 DiffusionPipeline 类,默认为diffusers.DiffusionPipeline
。有关更多 pipeline 类型,请参阅 Pipeline 概览device_map (
None | str | Dict[str, Union[int, str, torch.device]]
,可选) – 指定每个子模块应放置在哪里的映射。有关更多信息,请参阅 device_mapcustom_pipeline (
None | str
,可选) – 在 GitHub 上托管的自定义 pipeline 的标识符。有关社区维护的自定义 pipeline 列表,请参阅 https://github.com/huggingface/diffusers/tree/main/examples/communityscheduler_class (
type[diffusers.SchedulerMixin]
,可选) – DiffusionPipeline 将使用的 Scheduler 类torch_dtype (
str | torch.dtype
,可选) – 覆盖默认的 torch.dtype 并在此 dtype 下加载模型。low_cpu_mem_usage (
bool
,可选) – 通过不初始化权重而仅加载预训练权重来加快模型加载速度。如果 torch 版本 >= 1.9.0 则默认为 True,否则为 Falseenable_xformers (
bool
,可选) – 如果可用,则使用 xformers 优化。有关更多信息,请参阅 https://github.com/facebookresearch/xformersvariant (
str
,可选) – 如果指定,则从 variant 文件名加载权重,例如 pytorch_model.<variant>.bin。lora_weights (
LoraOptionType | list[LoraOptionType]
可选) – 要加载的 LoRA 权重。LoraOptionType
可以是字符串或字典。当它是字符串时,表示权重文件的路径。当它是字典时,包含一个 key"model_name"
,指向 huggingface repository 或本地目录;一个 keyweight_name
,指向权重文件;以及其他将传递给 pipeline 的load_lora_weights
方法的 key。textual_inversions (
TextualInversionOptionType | list[TextualInversionOptionType]
可选) – 要加载的 Textual Inversions。TextualInversionOptionType
可以是字符串或字典。当它是字符串时,表示权重文件的路径。当它是字典时,包含一个 key"model_name"
,指向 huggingface repository 或本地目录;一个 keyweight_name
,指向权重文件;以及其他将传递给 pipeline 的load_lora_weights
方法的 key。load_pretrained_extra_kwargs – (
dict[str, t.Any]
,可选):传递给 Pipeline 类的from_pretrained
方法的额外 kwargs
- 返回值:
从 BentoML 模型库加载为 diffusers pipeline 的 Diffusion 模型。
示例
import bentoml pipeline = bentoml.diffusers.load_model('my_diffusers_model:latest') pipeline(prompt)