Diffusers documentation
AutoencoderKLCogVideoX
AutoencoderKLCogVideoX
The 3D variational autoencoder (VAE) model with KL loss used in CogVideoX was introduced in CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer by Tsinghua University & ZhipuAI.
可以使用以下代码片段加载该模型。
from diffusers import AutoencoderKLCogVideoX
vae = AutoencoderKLCogVideoX.from_pretrained("THUDM/CogVideoX-2b", subfolder="vae", dtype=torch.float16).to("cuda") # or "mps", "xpu", "cpu"AutoencoderKLCogVideoX
类 diffusers.AutoencoderKLCogVideoX
< source >( in_channels: int = 3out_channels: int = 3down_block_types: tuple = ('CogVideoXDownBlock3D', 'CogVideoXDownBlock3D', 'CogVideoXDownBlock3D', 'CogVideoXDownBlock3D')up_block_types: tuple = ('CogVideoXUpBlock3D', 'CogVideoXUpBlock3D', 'CogVideoXUpBlock3D', 'CogVideoXUpBlock3D')block_out_channels: tuple = (128, 256, 256, 512)latent_channels: int = 16layers_per_block: int = 3act_fn: str = 'silu'norm_eps: float = 1e-06norm_num_groups: int = 32temporal_compression_ratio: float = 4sample_height: int = 480sample_width: int = 720scaling_factor: float = 1.15258426shift_factor: float | None = Nonelatents_mean: tuple[float] | None = Nonelatents_std: tuple[float] | None = Noneforce_upcast: float = Trueuse_quant_conv: bool = Falseuse_post_quant_conv: bool = Falseinvert_scale_latents: bool = False )
参数
- in_channels(int,可选,默认为 3)— 输入图像的通道数。
- out_channels (int, 可选,默认为 3) — 输出中的通道数。
- down_block_types (
tuple[str],可选,默认为("DownEncoderBlock2D",)) — 下采样块类型的元组。 - up_block_types (
tuple[str],可选,默认为("UpDecoderBlock2D",)) — 上采样块类型的元组。 - block_out_channels (
tuple[int],可选,默认为(64,)) — 块输出通道的元组。 - act_fn (
str,可选,默认为"silu") — 要使用的激活函数。 - sample_size (
int,可选,默认为32) — 采样输入大小。 - scaling_factor (
float, 可选,默认为1.15258426) — 使用训练集的第一个批次计算得出的训练后潜在空间的逐分量标准差。在训练扩散模型时,用于将潜在空间缩放至单位方差。在将潜在变量传递给扩散模型之前,使用公式z = z * scaling_factor对其进行缩放。在解码时,使用公式z = 1 / scaling_factor * z将潜在变量缩放回原始尺度。更多详细信息,请参阅 High-Resolution Image Synthesis with Latent Diffusion Models 论文的 4.3.2 节和 D.1 节。 - force_upcast (
bool,可选,默认为True) — 如果启用,将强制 VAE 以 float32 运行,适用于高分辨率图像流水线(例如 SD-XL)。VAE 可以微调/训练至更低的数值范围,而不会损失过多精度,此时可将force_upcast设置为False- 参见:https://huggingface.co/madebyollin/sdxl-vae-fp16-fix
一个带有 KL 损失的 VAE 模型,用于将图像编码为潜在表示,并将潜在表示解码为图像。在 CogVideoX 中使用。
该模型继承自 ModelMixin。请查阅其父类文档,了解为所有模型实现的通用方法(例如下载或保存)。
decode
< source >( z: Tensorreturn_dict: bool = True ) → ~models.vae.DecoderOutput or tuple
解码一批图像。
encode
< source >( x: Tensorreturn_dict: bool = True )
将一批图像编码为潜在表示。
enable_tiling
< source >( tile_sample_min_height: int | None = Nonetile_sample_min_width: int | None = Nonetile_overlap_factor_height: float | None = Nonetile_overlap_factor_width: float | None = None )
参数
- tile_sample_min_height (
int, 可选) — 样本在高度维度上被分割为多个 tile 所需的最小高度。 - tile_sample_min_width (
int, 可选) — 样本在宽度维度上被分割为多个 tile 所需的最小宽度。 - tile_overlap_factor_height (
int, 可选) — 两个相邻垂直瓦片之间的最小重叠量。用于确保在高度维度上不产生瓦片伪影。取值必须在 0 到 1 之间。设置较高的值可能会导致处理更多的瓦片,从而降低解码速度。 - tile_overlap_factor_width (
int, 可选) — 两个相邻水平瓦片之间的最小重叠量。用于确保在宽度维度上不产生瓦片伪影。取值必须在 0 到 1 之间。设置较高的值可能会导致处理更多的瓦片,从而降低解码速度。
启用分块 VAE 解码。启用此选项后,VAE 会将输入张量拆分为多个分块,分步执行解码和编码。这有助于节省大量内存,并支持处理更大的图像。
forward
< source >( sample: Tensorsample_posterior: bool = Falsereturn_dict: bool = Truegenerator: typing.Optional[torch.Generator] = None ) → ~models.vae.DecoderOutput or tuple
参数
- sample (
torch.Tensor) — 输入样本。 - sample_posterior (
bool,可选,默认为False) — 是否从后验分布中采样。 - return_dict (
bool,可选,默认为True) — 是否返回DecoderOutput而不是普通元组。 - generator (
torch.Generator,可选) — 用于使采样过程确定性的torch.Generator。
返回值
~models.vae.DecoderOutput 或 tuple
如果 return_dict 为 True,则返回 ~models.vae.DecoderOutput,否则返回普通的 tuple。
tiled_decode
< source >( z: Tensorreturn_dict: bool = True ) → ~models.vae.DecoderOutput or tuple
使用分块解码器解码一批图像。
tiled_encode
< source >( x: Tensor ) → torch.Tensor
使用分块编码器对一批图像进行编码。
启用此选项后,VAE 会将输入张量拆分为多个图块,分步计算编码。这有助于保持内存占用恒定,而不受图像尺寸影响。由于每个图块使用不同的编码器,分块编码的最终结果与非分块编码不同。为避免分块伪影,图块之间会重叠并混合,以形成平滑的输出。输出中可能仍会出现图块大小的变化,但应明显减弱。
AutoencoderKLOutput
类 diffusers.models.modeling_outputs.AutoencoderKLOutput
< source >( latent_dist: DiagonalGaussianDistribution )
AutoencoderKL 编码方法的输出。
DecoderOutput
class diffusers.models.autoencoders.vae.DecoderOutput
< source >( sample: Tensorcommit_loss: typing.Optional[torch.FloatTensor] = None )
解码方法的输出。