# AutoencoderKLHunyuanImageRefiner

在 [HunyuanImage2.1](https://github.com/Tencent-Hunyuan/HunyuanImage-2.1) 的 refiner 流水线中使用的带 KL 损失的 3D 变分自编码器（VAE）模型。

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

```python
from diffusers import AutoencoderKLHunyuanImageRefiner

vae = AutoencoderKLHunyuanImageRefiner.from_pretrained("hunyuanvideo-community/HunyuanImage-2.1-Refiner-Diffusers", subfolder="vae", dtype=torch.bfloat16)
```

## AutoencoderKLHunyuanImageRefiner[[diffusers.AutoencoderKLHunyuanImageRefiner]]

#### diffusers.AutoencoderKLHunyuanImageRefiner[[diffusers.AutoencoderKLHunyuanImageRefiner]]

```python
diffusers.AutoencoderKLHunyuanImageRefiner(in_channels: int = 3, out_channels: int = 3, latent_channels: int = 32, block_out_channels: tuple[int, ...] = (128, 256, 512, 1024, 1024), layers_per_block: int = 2, spatial_compression_ratio: int = 16, temporal_compression_ratio: int = 4, downsample_match_channel: bool = True, upsample_match_channel: bool = True, scaling_factor: float = 1.03682)
```

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

一个带有 KL 损失的 VAE 模型，用于将视频编码为潜在表示，并将潜在表示解码为视频。用于 HunyuanImage-2.1 Refiner。

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

#### decode[[diffusers.AutoencoderKLHunyuanImageRefiner.decode]]

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

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

**参数：**

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

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

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

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

解码一批图像。

#### enable_tiling[[diffusers.AutoencoderKLHunyuanImageRefiner.enable_tiling]]

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

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

**参数：**

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

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

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

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

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

#### encode[[diffusers.AutoencoderKLHunyuanImageRefiner.encode]]

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

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

**参数：**

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

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

**返回值：**

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

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

#### forward[[diffusers.AutoencoderKLHunyuanImageRefiner.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_hunyuanimage_refiner.py#L893)

**参数：**

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

```python
tiled_decode(z: Tensor)
```

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

**参数：**

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

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

解码后的图像。

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

#### tiled_encode[[diffusers.AutoencoderKLHunyuanImageRefiner.tiled_encode]]

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

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

**参数：**

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

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

编码视频的潜在表示。

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

## 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)`)：模型最后一层解码后的输出样本。

解码方法的输出。

