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.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

一致性解码器

Consistency decoder 可用于解码 StableDiffusionPipeline 中来自去噪 UNet 的潜变量。该解码器在 DALL-E 3 技术报告 中首次提出。

原始代码库可在 openai/consistencydecoder 中找到。

目前,推理仅支持 2 次迭代。

该 pipeline 的完成离不开来自 此 issue 的 madebyollin 和 mrsteyk 的帮助。

ConsistencyDecoderVAE

class diffusers.ConsistencyDecoderVAE

< >

( scaling_factor: float = 0.18215latent_channels: int = 4sample_size: int = 32encoder_act_fn: str = 'silu'encoder_block_out_channels: tuple = (128, 256, 512, 512)encoder_double_z: bool = Trueencoder_down_block_types: tuple = ('DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D')encoder_in_channels: int = 3encoder_layers_per_block: int = 2encoder_norm_num_groups: int = 32encoder_out_channels: int = 4decoder_add_attention: bool = Falsedecoder_block_out_channels: tuple = (320, 640, 1024, 1024)decoder_down_block_types: tuple = ('ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D')decoder_downsample_padding: int = 1decoder_in_channels: int = 7decoder_layers_per_block: int = 3decoder_norm_eps: float = 1e-05decoder_norm_num_groups: int = 32decoder_num_train_timesteps: int = 1024decoder_out_channels: int = 6decoder_resnet_time_scale_shift: str = 'scale_shift'decoder_time_embedding_type: str = 'learned'decoder_up_block_types: tuple = ('ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D') )

与 DALL-E 3 配合使用的 consistency decoder。

示例:

>>> import torch
>>> from diffusers import StableDiffusionPipeline, ConsistencyDecoderVAE

>>> vae = ConsistencyDecoderVAE.from_pretrained("openai/consistency-decoder", torch_dtype=torch.float16)
>>> pipe = StableDiffusionPipeline.from_pretrained(
...     "stable-diffusion-v1-5/stable-diffusion-v1-5", vae=vae, torch_dtype=torch.float16
... ).to("cuda")

>>> image = pipe("horse", generator=torch.manual_seed(0)).images[0]
>>> image

decode

< >

( z: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = Truenum_inference_steps: int = 2 ) → DecoderOutput | tuple[torch.Tensor]

参数

  • z (torch.Tensor) — 输入潜在向量。
  • generator (torch.Generator | None) — 随机数生成器。默认值为 None。
  • return_dict (bool) — 是否以字典形式返回输出。默认为 True。
  • num_inference_steps (int) — 推理步数。默认值为 2。

返回值

DecoderOutput | tuple[torch.Tensor]

解码后的输出。

使用一致性解码器 VAE 模型解码输入潜在向量 z。

encode

< >

( x: Tensorreturn_dict: bool = True )

参数

  • x (torch.Tensor) — 输入图像批次。
  • return_dict (bool,可选,默认为 True) — 是否返回 ConsistencyDecoderVAEOutput 而不是普通元组。

将一批图像编码为潜在表示。

forward

< >

( sample: Tensorsample_posterior: bool = Falsereturn_dict: bool = Truegenerator: typing.Optional[torch.Generator] = None ) → DecoderOutput or tuple

参数

  • sample (torch.Tensor) — 输入样本。
  • sample_posterior (bool,可选,默认为 False) — 是否从后验分布中采样。
  • return_dict (bool,可选,默认为 True) — 是否返回 DecoderOutput 而不是普通元组。
  • generator (torch.Generator,可选,默认为 None) — 用于采样的生成器。

返回值

DecoderOutput 或 tuple

If return_dict is True, a DecoderOutput is returned, otherwise a plain tuple is returned.

set_default_attn_processor

< >

( )

禁用自定义注意力处理器并设置默认的注意力实现。

tiled_encode

< >

( x: Tensorreturn_dict: bool = True ) → ConsistencyDecoderVAEOutput or tuple

参数

  • x (torch.Tensor) — 输入图像批次。
  • return_dict (bool,可选,默认为 True) — 是否返回 ConsistencyDecoderVAEOutput 而不是普通的元组。

返回值

ConsistencyDecoderVAEOutput 或 tuple

If return_dict is True, a ConsistencyDecoderVAEOutput is returned, otherwise a plain tuple is returned.

使用分块编码器对一批图像进行编码。

启用此选项后,VAE 会将输入张量拆分为多个图块,分步计算编码。这有助于保持内存占用恒定,而不受图像尺寸影响。由于每个图块使用不同的编码器,分块编码的最终结果与非分块编码不同。为避免分块伪影,图块之间会重叠并混合,以形成平滑的输出。输出中可能仍会出现图块大小的变化,但应明显减弱。

Update on GitHub