# Consistency Decoder

Consistency decoder は、[StableDiffusionPipeline](/docs/diffusers/main/ja/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline) のデノイジング UNet から潜在変数をデコードするために使用できます。このデコーダーは、[DALL-E 3 技術レポート](https://openai.com/dall-e-3) で紹介されました。

元のコードベースは[openai/consistencydecoder](https://github.com/openai/consistencydecoder)で確認できます。

> [!WARNING]
> 現時点では、推論は2回の反復のみサポートされています。

このパイプラインは、[このイシュー](https://github.com/openai/consistencydecoder/issues/1)からの[madebyollin](https://github.com/madebyollin)と[mrsteyk](https://github.com/mrsteyk)の助けがなければ貢献されていませんでした。

## 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'))
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L51)

DALL-E 3 と併せて使用される consistency decoder。

例:
```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)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L220)

**パラメータ:**

z (torch.Tensor) : 入力潜在ベクトル。

generator (torch.Generator | None) : 乱数ジェネレーター。デフォルトは None です。

return_dict (bool) : 出力を辞書として返すかどうか。デフォルトは True です。

num_inference_steps (int) : 推論ステップ数。デフォルト値は2です。

**戻り値:** DecoderOutput | tuple[torch.Tensor]

デコードされた出力。

consistency decoder VAE モデルを使用して、入力潜在ベクトル `z` をデコードします。

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

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

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L185)

**パラメータ:**

x (`torch.Tensor`) : 画像の入力バッチ。

return_dict (`bool`、*任意*、デフォルトは`True`) : プレーンなタプルではなく`ConsistencyDecoderVAEOutput`を返すかどうか。

**戻り値:**

エンコードされた画像の潜在表現。`return_dict`がTrueの場合、`ConsistencyDecoderVAEOutput`が返され、それ以外の場合は通常の`tuple`が返されます。

バッチの画像を潜在変数にエンコードします。

#### forward[[diffusers.ConsistencyDecoderVAE.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/consistency_decoder_vae.py#L336)

**パラメータ:**

sample (`torch.Tensor`) : 入力サンプル。

sample_posterior (`bool`、*任意*、デフォルトは`False`) : 事後分布からサンプリングするかどうか。

return_dict (`bool`、*任意*、デフォルトは`True`) : プレーンなタプルではなく`DecoderOutput`を返すかどうか。

generator (`torch.Generator`、*任意*、デフォルトは`None`) : サンプリングに使用するジェネレーター。

**戻り値:** `DecoderOutput` または `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()
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L170)

カスタムアテンションプロセッサを無効にし、デフォルトのアテンション実装を設定します。

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

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

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/autoencoders/consistency_decoder_vae.py#L281)

**パラメータ:**

x (`torch.Tensor`) : 画像の入力バッチ。

return_dict (`bool`、*任意*、デフォルトは`True`) : プレーンなタプルではなく`ConsistencyDecoderVAEOutput`を返すかどうか。

**戻り値:** `ConsistencyDecoderVAEOutput` または `tuple`

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

タイル化されたエンコーダーを使用して、画像のバッチをエンコードします。

このオプションを有効にすると、VAE は入力テンソルをタイルに分割し、エンコーディングを複数ステップで計算します。これにより、画像サイズに関係なくメモリ使用量を一定に保つことができます。タイル化されたエンコーディングの結果は、タイル化されていない場合と異なります。これは、各タイルが異なるエンコーダーを使用するためです。タイル化によるアーティファクトを回避するために、タイルはオーバーラップし、滑らかな出力を形成するためにブレンドされます。出力にタイルサイズの变化が見られる場合もありますが、以前よりもはるかに目立たなくなります。

