Diffusers documentation
一致性解码器
一致性解码器
Consistency decoder 可用于解码 StableDiffusionPipeline 中来自去噪 UNet 的潜变量。该解码器在 DALL-E 3 技术报告 中首次提出。
原始代码库可在 openai/consistencydecoder 中找到。
目前,推理仅支持 2 次迭代。
该 pipeline 的完成离不开来自 此 issue 的 madebyollin 和 mrsteyk 的帮助。
ConsistencyDecoderVAE
class diffusers.ConsistencyDecoderVAE
< source >( 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]
>>> imagedecode
< source >( z: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = Truenum_inference_steps: int = 2 ) → DecoderOutput | tuple[torch.Tensor]
使用一致性解码器 VAE 模型解码输入潜在向量 z。
encode
< source >( x: Tensorreturn_dict: bool = True )
将一批图像编码为潜在表示。
forward
< source >( 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.
禁用自定义注意力处理器并设置默认的注意力实现。
tiled_encode
< source >( x: Tensorreturn_dict: bool = True ) → ConsistencyDecoderVAEOutput or tuple
使用分块编码器对一批图像进行编码。
启用此选项后,VAE 会将输入张量拆分为多个图块,分步计算编码。这有助于保持内存占用恒定,而不受图像尺寸影响。由于每个图块使用不同的编码器,分块编码的最终结果与非分块编码不同。为避免分块伪影,图块之间会重叠并混合,以形成平滑的输出。输出中可能仍会出现图块大小的变化,但应明显减弱。