# Bộ giải mã nhất quán

Consistency decoder có thể được sử dụng để giải mã các latents từ denoising UNet trong [StableDiffusionPipeline](/docs/diffusers/v0.43.0/vi/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline). Bộ giải mã này được giới thiệu trong [báo cáo kỹ thuật DALL-E 3](https://openai.com/dall-e-3).

Mã nguồn gốc có thể được tìm thấy tại [openai/consistencydecoder](https://github.com/openai/consistencydecoder).

> [!WARNING]
> Hiện tại, suy luận chỉ được hỗ trợ cho 2 lần lặp.

Pipeline này không thể được đóng góp mà không có sự giúp đỡ của [madebyollin](https://github.com/madebyollin) và [mrsteyk](https://github.com/mrsteyk) từ [vấn đề này](https://github.com/openai/consistencydecoder/issues/1).

## ConsistencyDecoderVAE[[diffusers.ConsistencyDecoderVAE]]

#### diffusers.ConsistencyDecoderVAE[[diffusers.ConsistencyDecoderVAE]]

```python
diffusers.ConsistencyDecoderVAE(scaling_factor: float = 0.18215, latent_channels: int = 4, sample_size: int = 32, encoder_act_fn: str = 'silu', encoder_block_out_channels: tuple[int, ...] = (128, 256, 512, 512), encoder_double_z: bool = True, encoder_down_block_types: tuple[str, ...] = ('DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D'), encoder_in_channels: int = 3, encoder_layers_per_block: int = 2, encoder_norm_num_groups: int = 32, encoder_out_channels: int = 4, decoder_add_attention: bool = False, decoder_block_out_channels: tuple[int, ...] = (320, 640, 1024, 1024), decoder_down_block_types: tuple[str, ...] = ('ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D', 'ResnetDownsampleBlock2D'), decoder_downsample_padding: int = 1, decoder_in_channels: int = 7, decoder_layers_per_block: int = 3, decoder_norm_eps: float = 1e-05, decoder_norm_num_groups: int = 32, decoder_num_train_timesteps: int = 1024, decoder_out_channels: int = 6, decoder_resnet_time_scale_shift: str = 'scale_shift', decoder_time_embedding_type: str = 'learned', decoder_up_block_types: tuple[str, ...] = ('ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D', 'ResnetUpsampleBlock2D'))
```

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L51)

Bộ giải mã tính nhất quán được sử dụng với DALL-E 3.

Ví dụ:
```py
>>> import torch
>>> from diffusers import StableDiffusionPipeline, ConsistencyDecoderVAE

>>> vae = ConsistencyDecoderVAE.from_pretrained("openai/consistency-decoder", torch_dtype=torch.float16)
>>> pipe = StableDiffusionPipeline.from_pretrained(
...     "stable-diffusion-v1-5/stable-diffusion-v1-5", vae=vae, torch_dtype=torch.float16
... ).to("cuda")

>>> image = pipe("horse", generator=torch.manual_seed(0)).images[0]
>>> image
```

#### decode[[diffusers.ConsistencyDecoderVAE.decode]]

```python
decode(z: Tensor, generator: typing.Optional[torch.Generator] = None, return_dict: bool = True, num_inference_steps: int = 2)
```

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L220)

**Tham số:**

z (torch.Tensor) : Vector ẩn đầu vào.

generator (torch.Generator | None) : Bộ tạo số ngẫu nhiên. Mặc định là None.

return_dict (bool) : Xác định có trả về kết quả dưới dạng từ điển hay không. Mặc định là True.

num_inference_steps (int) : Số bước suy luận. Mặc định là 2.

**Trả về:** DecoderOutput | tuple[torch.Tensor]

Đầu ra đã được giải mã.

Giải mã vector ẩn đầu vào `z` bằng mô hình VAE consistency decoder.

#### encode[[diffusers.ConsistencyDecoderVAE.encode]]

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

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L185)

**Tham số:**

x (`torch.Tensor`) : Lô đầu vào của các hình ảnh.

return_dict (`bool`, *tùy chọn*, mặc định là `True`) : Có nên trả về một `ConsistencyDecoderVAEOutput` thay vì một tuple thông thường hay không.

**Trả về:**

Các biểu diễn ẩn của các hình ảnh đã được mã hóa. Nếu `return_dict` là True, một `ConsistencyDecoderVAEOutput` sẽ được trả về, ngược lại một `tuple` thông thường sẽ được trả về.

Mã hóa một lô ảnh thành các biến ẩn.

#### forward[[diffusers.ConsistencyDecoderVAE.forward]]

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

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L336)

**Tham số:**

sample (`torch.Tensor`) : Mẫu đầu vào.

sample_posterior (`bool`, *tùy chọn*, mặc định là `False`) : Có lấy mẫu từ phân phối hậu nghiệm hay không.

return_dict (`bool`, *tùy chọn*, mặc định là `True`) : Có nên trả về một `DecoderOutput` thay vì một tuple thông thường hay không.

generator (`torch.Generator`, *tùy chọn*, mặc định là `None`) : Bộ tạo (Generator) được sử dụng để lấy mẫu.

**Trả về:** `DecoderOutput` hoặc `tuple`

If return_dict is True, a `DecoderOutput` is returned, otherwise a plain `tuple` is returned.

#### set_default_attn_processor[[diffusers.ConsistencyDecoderVAE.set_default_attn_processor]]

```python
set_default_attn_processor()
```

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L170)

Tắt các bộ xử lý chú ý tùy chỉnh và đặt cài đặt chú ý mặc định.

#### tiled_encode[[diffusers.ConsistencyDecoderVAE.tiled_encode]]

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

[Nguồn](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L281)

**Tham số:**

x (`torch.Tensor`) : Lô đầu vào của các hình ảnh.

return_dict (`bool`, *tùy chọn*, mặc định là `True`) : Có nên trả về một `ConsistencyDecoderVAEOutput` thay vì một tuple thông thường hay không.

**Trả về:** `ConsistencyDecoderVAEOutput` hoặc `tuple`

Nếu return_dict là True, một `ConsistencyDecoderVAEOutput` sẽ được trả về, ngược lại một `tuple` thông thường sẽ được trả về.

Mã hóa một lô ảnh bằng trình mã hóa dạng gạch.

Khi tùy chọn này được bật, VAE sẽ chia tensor đầu vào thành các ô (tiles) để tính toán mã hóa qua nhiều bước. Điều này hữu ích để giữ cho mức sử dụng bộ nhớ luôn ổn định bất kể kích thước ảnh. Kết quả cuối cùng của mã hóa theo ô sẽ khác so với mã hóa không theo ô vì mỗi ô sử dụng một bộ mã hóa khác nhau. Để tránh các hiện tượng méo do chia ô, các ô sẽ chồng lấn lên nhau và được trộn lẫn để tạo ra đầu vào mượt mà. Bạn vẫn có thể nhìn thấy các thay đổi có kích thước bằng ô trong đầu ra, nhưng chúng sẽ ít gây chú ý hơn nhiều.

