一致性解码器
一致性解码器可以用于解码 StableDiffusionPipeline 中去噪 UNet 的潜在表示。此解码器在 DALL-E 3 技术报告中被引入。
原始代码库可以在 openai/consistencydecoder 找到。
目前仅支持 2 次迭代的推理。
如果没有来自 this issue 的 madebyollin 和 mrsteyk 的帮助,这个 pipeline 就无法贡献出来。
ConsistencyDecoderVAE
class diffusers.ConsistencyDecoderVAE
< 源代码 >( scaling_factor: float = 0.18215 latent_channels: int = 4 sample_size: int = 32 encoder_act_fn: str = 'silu' encoder_block_out_channels: Tuple = (128, 256, 512, 512) encoder_double_z: bool = True encoder_down_block_types: Tuple = ('DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D') encoder_in_channels: int = 3 encoder_layers_per_block: int = 2 encoder_norm_num_groups: int = 32 encoder_out_channels: int = 4 decoder_add_attention: bool = False decoder_block_out_channels: Tuple = (320, 640, 1024, 1024) decoder_down_block_types: Tuple = ('ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D') decoder_downsample_padding: int = 1 decoder_in_channels: int = 7 decoder_layers_per_block: int = 3 decoder_norm_eps: float = 1e-05 decoder_norm_num_groups: int = 32 decoder_num_train_timesteps: int = 1024 decoder_out_channels: int = 6 decoder_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 的一致性解码器。
示例
>>> import torch
>>> from diffusers import StableDiffusionPipeline, ConsistencyDecoderVAE
>>> vae = ConsistencyDecoderVAE.from_pretrained("openai/consistency-decoder", torch_dtype=torch.float16)
>>> pipe = StableDiffusionPipeline.from_pretrained(
... "runwayml/stable-diffusion-v1-5", vae=vae, torch_dtype=torch.float16
... ).to("cuda")
>>> image = pipe("horse", generator=torch.manual_seed(0)).images[0]
>>> image
禁用切片 VAE 解码。如果之前启用了 enable_slicing
,此方法将恢复为一步计算解码。
禁用平铺 VAE 解码。如果之前启用了 enable_tiling
,此方法将恢复为一步计算解码。
启用切片 VAE 解码。当启用此选项时,VAE 会将输入张量分割成切片,分几步计算解码。这有助于节省一些内存并允许更大的批次大小。
启用平铺 VAE 解码。当启用此选项时,VAE 会将输入张量分割成图块,分几步计算解码和编码。这对于节省大量内存和允许处理更大的图像非常有用。
前向
< source >( sample: Tensor sample_posterior: bool = False return_dict: bool = True generator: Optional = None ) → DecoderOutput
或 tuple
参数
- sample (
torch.Tensor
) — 输入样本。 - sample_posterior (
bool
, optional, 默认为False
) — 是否从后验分布中采样。 - return_dict (
bool
, optional, 默认为True
) — 是否返回DecoderOutput
而不是普通的元组。 - generator (
torch.Generator
, optional, 默认为None
) — 用于采样的生成器。
返回值
DecoderOutput
或 tuple
如果 return_dict 为 True,则返回 DecoderOutput
,否则返回普通 tuple
。
set_attn_processor
< source >( processor: Union )
设置用于计算注意力的注意力处理器。
禁用自定义注意力处理器并设置默认的注意力实现。
tiled_encode
< source >( x: Tensor return_dict: bool = True ) → ConsistencyDecoderVAEOutput
或 tuple
使用平铺编码器编码一批图像。
启用此选项后,VAE 会将输入张量拆分为瓦片,以分步计算编码。这对于保持内存使用恒定,而与图像大小无关非常有用。平铺编码的最终结果与非平铺编码不同,因为每个瓦片都使用不同的编码器。为了避免平铺伪影,瓦片会重叠并混合在一起以形成平滑的输出。您可能仍然会在输出中看到瓦片大小的变化,但它们应该不那么明显。