# AutoencoderKLPH3Image

Pruna AI 的 [P-H3-Image](https://huggingface.co/PrunaAI/P-H3-Image) 图像 VAE。它使用了 MiniMax-H3 的 [视频 VAE](./autoencoderkl_minimax_h3) 的编码器和潜在空间，并采用 ViT 解码器将单个潜在帧解码为图像。

它遵循 [AutoencoderKLMiniMaxH3](/docs/diffusers/main/zh/api/models/autoencoderkl_minimax_h3#diffusers.AutoencoderKLMiniMaxH3) 的约定：

- 图像以单帧视频的形式传入，形状为 `(batch_size, channels, 1, height, width)`，空间压缩率为 16 倍。
- 潜变量按通道使用 `latents_mean` / `latents_std` 进行归一化，而不是使用 `scaling_factor`。
- 像素是经过 ImageNet 归一化的 RGB 值，取值范围为 `[0, 1]`：`encode` 期望输入为 `(pixel - imagenet_mean) / imagenet_std`，而 `decode` 返回的也是该空间中的值。
- 空间平铺默认开启，使用 256 像素的平铺块，且至少包含 64 像素的重叠区域。

```python
import torch
from diffusers import AutoencoderKLPH3Image

vae = AutoencoderKLPH3Image.from_pretrained("PrunaAI/P-H3-Image", subfolder="vae", dtype=torch.bfloat16).to(
    "cuda"
)  # or "mps", "xpu", "cpu"
```

无论模型以何种 `dtype` 加载，编码器和解码器的输出投影层均保持为 float32。

## AutoencoderKLPH3Image[[diffusers.AutoencoderKLPH3Image]]

#### diffusers.AutoencoderKLPH3Image[[diffusers.AutoencoderKLPH3Image]]

```python
diffusers.AutoencoderKLPH3Image(in_channels: int = 3, out_channels: int = 3, latent_channels: int = 24, block_out_channels: tuple[int, ...] = (128, 256, 256, 512, 512, 1024), layers_per_block: int = 2, spatial_downsample_factors: tuple[int, ...] = (2, 2, 2, 2, 1, 1), temporal_downsample_factors: tuple[int, ...] = (1, 2, 2, 1, 1, 1), norm_num_groups: int = 32, norm_eps: float = 1e-06, spatial_padding_mode: str = 'reflect', decoder_num_layers: int = 36, decoder_num_attention_heads: int = 32, decoder_attention_head_dim: int = 64, decoder_num_register_tokens: int = 4, decoder_ffn_mult: int = 4, decoder_rope_theta: float = 100.0, decoder_rope_dim_ratio: float = 0.75, decoder_norm_eps: float = 1e-05, latents_mean: tuple[float, ...] = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), latents_std: tuple[float, ...] = (1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0))
```

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

[P-H3-Image](https://huggingface.co/PrunaAI/P-H3-Image) 的图像 VAE：MiniMax-H3 的 VAE 编码器与潜在空间，以及用于解码单个潜在帧的 `PH3ImageViTDecoder`。

图像以单帧视频的形式传入，形状为 `(batch_size, channels, 1, height, width)`。与 [AutoencoderKLMiniMaxH3](/docs/diffusers/main/zh/api/models/autoencoderkl_minimax_h3#diffusers.AutoencoderKLMiniMaxH3) 相同，像素为在 `[0, 1]` 范围内经过 ImageNet 归一化的 RGB 值（均值 `(0.485, 0.456, 0.406)`，标准差 `(0.229, 0.224, 0.225)`），并且默认启用空间分块。

#### encode[[diffusers.AutoencoderKLPH3Image.encode]]

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

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

**参数：**

x (`torch.Tensor`) : 作为单帧视频输入的图像批次，形状为 `(batch_size, in_channels, 1, height, width)`。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 [AutoencoderKLOutput](/docs/diffusers/main/zh/api/models/autoencoderkl_qwenimage#diffusers.models.modeling_outputs.AutoencoderKLOutput) 而非普通元组。

**返回值：**

编码图像的潜在分布。请注意，潜在变量随后会使用 `latents_mean` / `latents_std` 进行归一化。

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

#### decode[[diffusers.AutoencoderKLPH3Image.decode]]

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

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

**参数：**

z (`torch.Tensor`) : 输入的潜在变量批次，形状为 `(batch_size, latent_channels, 1, height, width)`。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 [DecoderOutput](/docs/diffusers/main/zh/api/models/autoencoderkl_qwenimage#diffusers.models.autoencoders.vae.DecoderOutput) 而不是普通元组。

**返回：** [DecoderOutput](/docs/diffusers/main/zh/api/models/autoencoderkl_qwenimage#diffusers.models.autoencoders.vae.DecoderOutput) 或 `tuple`

解码后的图像，形状为 `(batch_size, out_channels, 1, height, width)`。

解码一批潜在图像。

#### enable_tiling[[diffusers.AutoencoderKLPH3Image.enable_tiling]]

```python
enable_tiling(tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_sample_min_overlap_height: int | None = None, tile_sample_min_overlap_width: int | None = None)
```

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

**参数：**

tile_sample_min_height (`int`, *可选*) : 像素空间中的瓦片高度。高度超过此值的帧将沿高度维度进行分割。

tile_sample_min_width (`int`，*可选*)：像素空间中的瓦片宽度。宽度超过此值的帧将沿宽度维度进行分割。

tile_sample_min_overlap_height (`int`，*可选*)：两个连续垂直瓦片之间的最小重叠量，以像素为单位。

tile_sample_min_overlap_width (`int`, *可选*)：两个相邻水平切片之间的最小重叠像素数。

启用分块 VAE 编码/解码。启用此选项后，VAE 会将帧分割为多个分块，分别对每个分块进行编码或解码，并将重叠区域线性混合以重新拼接。这可以降低内存需求，从而支持处理更大的帧。

#### forward[[diffusers.AutoencoderKLPH3Image.forward]]

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

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

**参数：**

sample (`torch.Tensor`) : 作为单帧视频输入的图像批次，形状为 `(batch_size, in_channels, 1, height, width)`。

sample_posterior (`bool`，*可选*，默认为 `False`)：是否对后验分布进行采样，而不是取其众数。

generator (`torch.Generator`，*可选*)：当 `sample_posterior=True` 时使用的生成器。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 [DecoderOutput](/docs/diffusers/main/zh/api/models/autoencoderkl_qwenimage#diffusers.models.autoencoders.vae.DecoderOutput) 而不是普通元组。

**返回：** [DecoderOutput](/docs/diffusers/main/zh/api/models/autoencoderkl_qwenimage#diffusers.models.autoencoders.vae.DecoderOutput) 或 `tuple`

往返处理后的图像，形状为 `(batch_size, out_channels, 1, height, width)`。

对一批图像进行编码，然后解码。

