# AutoencoderKLCogVideoX

The 3D variational autoencoder (VAE) model with KL loss used in [CogVideoX](https://github.com/THUDM/CogVideo) was introduced in [CogVideoX: Text-to-Video Diffusion Models with An Expert Transformer](https://huggingface.co/papers/2408.06072) by Tsinghua University & ZhipuAI.

可以使用以下代码片段加载该模型。

```python
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]]

#### diffusers.AutoencoderKLCogVideoX[[diffusers.AutoencoderKLCogVideoX]]

```python
diffusers.AutoencoderKLCogVideoX(in_channels: int = 3, out_channels: int = 3, down_block_types: tuple[str] = ('CogVideoXDownBlock3D', 'CogVideoXDownBlock3D', 'CogVideoXDownBlock3D', 'CogVideoXDownBlock3D'), up_block_types: tuple[str] = ('CogVideoXUpBlock3D', 'CogVideoXUpBlock3D', 'CogVideoXUpBlock3D', 'CogVideoXUpBlock3D'), block_out_channels: tuple[int] = (128, 256, 256, 512), latent_channels: int = 16, layers_per_block: int = 3, act_fn: str = 'silu', norm_eps: float = 1e-06, norm_num_groups: int = 32, temporal_compression_ratio: float = 4, sample_height: int = 480, sample_width: int = 720, scaling_factor: float = 1.15258426, shift_factor: float | None = None, latents_mean: tuple[float] | None = None, latents_std: tuple[float] | None = None, force_upcast: float = True, use_quant_conv: bool = False, use_post_quant_conv: bool = False, invert_scale_latents: bool = False)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L958)

**参数：**

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](https://huggingface.co/papers/2112.10752) 论文的 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](https://github.com/THUDM/CogVideo) 中使用。

该模型继承自 [ModelMixin](/docs/diffusers/main/zh/api/models/overview#diffusers.ModelMixin)。请查阅其父类文档，了解为所有模型实现的通用方法（例如下载或保存）。

#### decode[[diffusers.AutoencoderKLCogVideoX.decode]]

```python
decode(z: Tensor, return_dict: bool = True)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1209)

**参数：**

z (`torch.Tensor`) : 输入的潜在向量批次。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `~models.vae.DecoderOutput` 而不是普通元组。

**返回：** `~models.vae.DecoderOutput` 或 `tuple`

如果 return_dict 为 True，则返回 `~models.vae.DecoderOutput`，否则返回普通的 `tuple`。

解码一批图像。

#### encode[[diffusers.AutoencoderKLCogVideoX.encode]]

```python
encode(x: Tensor, return_dict: bool = True)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1153)

**参数：**

x (`torch.Tensor`) : 输入图像批次。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `~models.autoencoder_kl.AutoencoderKLOutput` 而不是普通元组。

**返回值：**

编码视频的潜在表示。如果 `return_dict` 为 True，则返回 `~models.autoencoder_kl.AutoencoderKLOutput`，否则返回普通的 `tuple`。

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

#### enable_tiling[[diffusers.AutoencoderKLCogVideoX.enable_tiling]]

```python
enable_tiling(tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_overlap_factor_height: float | None = None, tile_overlap_factor_width: float | None = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1091)

**参数：**

tile_sample_min_height (`int`, *可选*) : 样本在高度维度上被分割为多个图块所需的最小高度。

tile_sample_min_width (`int`, *可选*) : 将样本沿宽度维度分割为多个图块所需的最小宽度。

tile_overlap_factor_height (`int`, *可选*)：两个连续垂直切片之间的最小重叠量。这用于确保在高度维度上不会产生切片伪影。取值必须在 0 到 1 之间。设置较高的值可能会导致处理更多的切片，从而降低解码速度。

tile_overlap_factor_width (`int`, *可选*)：两个相邻水平切片之间的最小重叠量。这用于确保在宽度维度上不会产生切片伪影。取值必须在 0 到 1 之间。设置较高的值可能会导致处理更多的切片，从而降低解码速度。

启用分块 VAE 解码。启用此选项后，VAE 会将输入张量拆分为多个块，分步执行解码和编码。这有助于节省大量内存，并支持处理更大的图像。

#### forward[[diffusers.AutoencoderKLCogVideoX.forward]]

```python
forward(sample: Tensor, sample_posterior: bool = False, return_dict: bool = True, generator: typing.Optional[torch.Generator] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1407)

**参数：**

sample (`torch.Tensor`) : 输入样本。

sample_posterior (`bool`，*可选*，默认为 `False`)：是否从后验分布中采样。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `DecoderOutput` 而不是普通元组。

generator (`torch.Generator`，*可选*)：一个 [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html)，用于使采样过程具有确定性。

**返回：** `~models.vae.DecoderOutput` 或 `tuple`

如果 `return_dict` 为 True，则返回 `~models.vae.DecoderOutput`，否则返回普通的 `tuple`。

#### tiled_decode[[diffusers.AutoencoderKLCogVideoX.tiled_decode]]

```python
tiled_decode(z: Tensor, return_dict: bool = True)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1324)

**参数：**

z (`torch.Tensor`) : 输入的潜在向量批次。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 `~models.vae.DecoderOutput` 而不是普通元组。

**返回：** `~models.vae.DecoderOutput` 或 `tuple`

如果 return_dict 为 True，则返回 `~models.vae.DecoderOutput`，否则返回普通的 `tuple`。

使用分块解码器解码一批图像。

#### tiled_encode[[diffusers.AutoencoderKLCogVideoX.tiled_encode]]

```python
tiled_encode(x: Tensor)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/autoencoder_kl_cogvideox.py#L1250)

**参数：**

x (`torch.Tensor`) : 输入的视频批次。

**返回：** `torch.Tensor`

编码视频的潜在表示。

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

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

## AutoencoderKLOutput[[diffusers.models.modeling_outputs.AutoencoderKLOutput]]

#### diffusers.models.modeling_outputs.AutoencoderKLOutput[[diffusers.models.modeling_outputs.AutoencoderKLOutput]]

```python
diffusers.models.modeling_outputs.AutoencoderKLOutput(latent_dist: DiagonalGaussianDistribution)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/modeling_outputs.py#L7)

**参数：**

latent_dist (`DiagonalGaussianDistribution`)：`Encoder` 的编码输出，表示为 `DiagonalGaussianDistribution` 的均值和 logvar。`DiagonalGaussianDistribution` 允许从该分布中采样潜在变量。

AutoencoderKL 编码方法的输出。

## DecoderOutput[[diffusers.models.autoencoders.vae.DecoderOutput]]

#### diffusers.models.autoencoders.vae.DecoderOutput[[diffusers.models.autoencoders.vae.DecoderOutput]]

```python
diffusers.models.autoencoders.vae.DecoderOutput(sample: Tensor, commit_loss: typing.Optional[torch.FloatTensor] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/vae.py#L49)

**参数：**

sample (`torch.Tensor`，形状为 `(batch_size, num_channels, height, width)`)：模型最后一层解码后的输出样本。

解码方法的输出。

