# AutoencoderKLAllegro

[Allegro](https://github.com/rhymes-ai/Allegro) 中使用的带 KL 损失的 3D 变分自编码器（VAE）模型，由 RhymesAI 在 [Allegro: Open the Black Box of Commercial-Level Video Generation Model](https://huggingface.co/papers/2410.15458) 中提出。

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

```python
from diffusers import AutoencoderKLAllegro

vae = AutoencoderKLAllegro.from_pretrained("rhymes-ai/Allegro", subfolder="vae", dtype=torch.float32).to("cuda")  # or "mps", "xpu", "cpu"
```

## AutoencoderKLAllegro[[diffusers.AutoencoderKLAllegro]]

#### diffusers.AutoencoderKLAllegro[[diffusers.AutoencoderKLAllegro]]

```python
diffusers.AutoencoderKLAllegro(in_channels: int = 3, out_channels: int = 3, down_block_types: tuple[str, ...] = ('AllegroDownBlock3D', 'AllegroDownBlock3D', 'AllegroDownBlock3D', 'AllegroDownBlock3D'), up_block_types: tuple[str, ...] = ('AllegroUpBlock3D', 'AllegroUpBlock3D', 'AllegroUpBlock3D', 'AllegroUpBlock3D'), block_out_channels: tuple[int, ...] = (128, 256, 512, 512), temporal_downsample_blocks: tuple[bool, ...] = (True, True, False, False), temporal_upsample_blocks: tuple[bool, ...] = (False, True, True, False), latent_channels: int = 4, layers_per_block: int = 2, act_fn: str = 'silu', norm_num_groups: int = 32, temporal_compression_ratio: float = 4, sample_size: int = 320, scaling_factor: float = 0.13, force_upcast: bool = True)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_kl_allegro.py#L676)

**参数：**

in_channels (int，默认为 `3`)：输入图像的通道数。

out_channels (int，默认为 `3`)：输出中的通道数。

down_block_types (`tuple[str, ...]`，默认为 `("AllegroDownBlock3D", "AllegroDownBlock3D", "AllegroDownBlock3D", "AllegroDownBlock3D")`)：字符串元组，指定要使用的下采样块类型。

up_block_types (`tuple[str, ...]`，默认为 `("AllegroUpBlock3D", "AllegroUpBlock3D", "AllegroUpBlock3D", "AllegroUpBlock3D")`)：字符串元组，指定要使用的上采样块类型。

block_out_channels (`tuple[int, ...]`，默认为 `(128, 256, 512, 512)`)：一个整数元组，表示每个块中的输出通道数。

temporal_downsample_blocks (`tuple[bool, ...]`，默认为 `(True, True, False, False)`)：布尔值元组，用于指定在哪些块中启用时间下采样。

latent_channels (`int`，默认为 `4`)：潜在表示中的通道数。

layers_per_block (`int`，默认为 `2`)：每个下采样/上采样块中 ResNet、注意力或时间卷积层的数量。

act_fn (`str`，默认为 `"silu"`)：要使用的激活函数。

norm_num_groups (`int`，默认为 `32`)：归一化层中使用的组数。

temporal_compression_ratio (`int`，默认为 `4`)：样本时间维度被压缩的比例。

sample_size (`int`，默认为 `320`)：默认的潜在尺寸。

scaling_factor (`float`，默认为 `0.13235`)：使用训练集的第一个批次计算得出的训练后潜在空间的逐分量标准差。此值用于在训练扩散模型时将潜在空间缩放至单位方差。在将潜在变量传入扩散模型之前，会使用公式 `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

一个用于将视频编码为潜在表示并将潜在表示解码为视频的 VAE 模型，包含 KL 损失。用于 [Allegro](https://github.com/rhymes-ai/Allegro)。

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

#### decode[[diffusers.AutoencoderKLAllegro.decode]]

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

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_kl_allegro.py#L843)

**参数：**

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

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

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

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

解码一批视频。

#### encode[[diffusers.AutoencoderKLAllegro.encode]]

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

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_kl_allegro.py#L806)

**参数：**

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

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

**返回值：**

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

将一批视频编码为潜在表示。

#### forward[[diffusers.AutoencoderKLAllegro.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/v0.43.0/src/diffusers/models/autoencoders/autoencoder_kl_allegro.py#L1041)

**参数：**

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

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

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

generator (`torch.Generator`, *可选*) : PyTorch 随机数生成器。

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

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

## 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/v0.43.0/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/v0.43.0/src/diffusers/models/autoencoders/vae.py#L49)

**参数：**

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

解码方法的输出。

