# AutoencoderKLLTX2Video

[LTX-2](https://huggingface.co/Lightricks/LTX-2) 中使用的带 KL 损失的 3D 变分自编码器（VAE）模型由 Lightricks 提出。

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

```python
from diffusers import AutoencoderKLLTX2Video

vae = AutoencoderKLLTX2Video.from_pretrained("Lightricks/LTX-2", subfolder="vae", dtype=torch.float32).to("cuda")  # or "mps", "xpu", "cpu"
```

## AutoencoderKLLTX2Video[[diffusers.AutoencoderKLLTX2Video]]

#### diffusers.AutoencoderKLLTX2Video[[diffusers.AutoencoderKLLTX2Video]]

```python
diffusers.AutoencoderKLLTX2Video(in_channels: int = 3, out_channels: int = 3, latent_channels: int = 128, block_out_channels: tuple = (256, 512, 1024, 2048), down_block_types: tuple = ('LTX2VideoDownBlock3D', 'LTX2VideoDownBlock3D', 'LTX2VideoDownBlock3D', 'LTX2VideoDownBlock3D'), decoder_block_out_channels: tuple = (256, 512, 1024), layers_per_block: tuple = (4, 6, 6, 2, 2), decoder_layers_per_block: tuple = (5, 5, 5, 5), spatio_temporal_scaling: bool | tuple[bool, ...] = (True, True, True, True), decoder_spatio_temporal_scaling: bool | tuple[bool, ...] = (True, True, True), decoder_inject_noise: bool | tuple[bool, ...] = (False, False, False, False), downsample_type: tuple = ('spatial', 'temporal', 'spatiotemporal', 'spatiotemporal'), upsample_type: tuple = ('spatiotemporal', 'spatiotemporal', 'spatiotemporal'), upsample_residual: bool | tuple[bool, ...] = (True, True, True), upsample_factor: tuple = (2, 2, 2), timestep_conditioning: bool = False, patch_size: int = 4, patch_size_t: int = 1, resnet_norm_eps: float = 1e-06, scaling_factor: float = 1.0, encoder_causal: bool = True, decoder_causal: bool = True, encoder_spatial_padding_mode: str = 'zeros', decoder_spatial_padding_mode: str = 'reflect', spatial_compression_ratio: int = None, temporal_compression_ratio: int = None)
```

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

**参数：**

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

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

latent_channels (`int`，默认为 `128`)：潜在通道的数量。

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

spatio_temporal_scaling (`tuple[bool, ...], defaults to `(True, True, True, False)` : 是否应在块中包含时空下采样。

layers_per_block (`tuple[int, ...]`，默认为 `(4, 3, 3, 3, 4)`)：每个块中的层数。

patch_size (`int`，默认为 `4`)：空间补丁的大小。

patch_size_t (`int`，默认为 `1`)：时间补丁的大小。

resnet_norm_eps (`float`，默认值为 `1e-6`)：ResNet 归一化层的 epsilon 值。

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

encoder_causal (`bool`，默认为 `True`)：编码器是否应以因果方式运行（即未来帧仅依赖于过去帧）。

decoder_causal (`bool`，默认为 `False`)：解码器是否应以因果方式运行（即未来帧仅依赖于过去帧）。

一种带有 KL 损失的 VAE 模型，用于将图像编码为潜在表示，并将潜在表示解码为图像。在 [LTX-2](https://huggingface.co/Lightricks/LTX-2) 中使用。

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

#### decode[[diffusers.AutoencoderKLLTX2Video.decode]]

```python
decode(z: Tensor, temb: typing.Optional[torch.Tensor] = None, causal: bool | None = None, return_dict: bool = True)
```

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

**参数：**

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

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

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

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

解码一批图像。

#### encode[[diffusers.AutoencoderKLLTX2Video.encode]]

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

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

**参数：**

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

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

**返回值：**

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

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

#### enable_tiling[[diffusers.AutoencoderKLLTX2Video.enable_tiling]]

```python
enable_tiling(tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_sample_min_num_frames: int | None = None, tile_sample_stride_height: float | None = None, tile_sample_stride_width: float | None = None, tile_sample_stride_num_frames: float | None = None)
```

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

**参数：**

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

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

tile_sample_stride_height (`int`, *可选*) : 两个连续垂直瓦片之间的最小重叠量。这旨在确保在高度维度上不会产生瓦片拼接伪影。

tile_sample_stride_width (`int`, *可选*) : 两个相邻水平瓦片之间的步长。这用于确保在宽度维度上不会产生瓦片伪影。

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

#### forward[[diffusers.AutoencoderKLLTX2Video.forward]]

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

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

**参数：**

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

temb (`torch.Tensor`, *可选*) : 用于对解码器进行条件化的可选时间步嵌入张量。

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

encoder_causal (`bool`，*可选*)：编码器是否应使用因果卷积。如果为 `None`，则回退到模型默认值。

decoder_causal (`bool`，*可选*)：解码器是否应使用因果卷积。如果为 `None`，则回退到模型默认值。

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.AutoencoderKLLTX2Video.tiled_decode]]

```python
tiled_decode(z: Tensor, temb: typing.Optional[torch.Tensor], causal: bool | None = None, return_dict: bool = True)
```

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

**参数：**

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

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

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

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

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

#### tiled_encode[[diffusers.AutoencoderKLLTX2Video.tiled_encode]]

```python
tiled_encode(x: Tensor, causal: bool | None = None)
```

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

**参数：**

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

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

编码视频的潜在表示。

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

