# Tiny AutoEncoder

Stable Diffusion 用の Tiny AutoEncoder (TAESD) は、Ollin Boer Bohan 氏によって [madebyollin/taesd](https://github.com/madebyollin/taesd) で紹介されました。これは Stable Diffusion の VAE の小型化・蒸留版であり、[StableDiffusionPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/stable_diffusion/text2img#diffusers.StableDiffusionPipeline) または [StableDiffusionXLPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/stable_diffusion/stable_diffusion_xl#diffusers.StableDiffusionXLPipeline) の latents をほぼ瞬時にデコードできます。

Stable Diffusion v-2.1で使用する場合は:

```python
import torch
from diffusers import DiffusionPipeline, AutoencoderTiny

pipe = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1-base", dtype=torch.float16
)
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesd", dtype=torch.float16)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

prompt = "slice of delicious New York-style berry cheesecake"
image = pipe(prompt, num_inference_steps=25).images[0]
image
```

Stable Diffusion XL 1.0 と一緒に使用するには

```python
import torch
from diffusers import DiffusionPipeline, AutoencoderTiny

pipe = DiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", dtype=torch.float16
)
pipe.vae = AutoencoderTiny.from_pretrained("madebyollin/taesdxl", dtype=torch.float16)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

prompt = "slice of delicious New York-style berry cheesecake"
image = pipe(prompt, num_inference_steps=25).images[0]
image
```

## AutoencoderTiny[[diffusers.AutoencoderTiny]]

#### diffusers.AutoencoderTiny[[diffusers.AutoencoderTiny]]

```python
diffusers.AutoencoderTiny(in_channels: int = 3, out_channels: int = 3, encoder_block_out_channels: tuple[int, ...] = (64, 64, 64, 64), decoder_block_out_channels: tuple[int, ...] = (64, 64, 64, 64), act_fn: str = 'relu', upsample_fn: str = 'nearest', latent_channels: int = 4, upsampling_scaling_factor: int = 2, num_encoder_blocks: tuple[int, ...] = (1, 3, 3, 3), num_decoder_blocks: tuple[int, ...] = (3, 3, 3, 1), latent_magnitude: int = 3, latent_shift: float = 0.5, force_upcast: bool = False, scaling_factor: float = 1.0, shift_factor: float = 0.0)
```

[ソース](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_tiny.py#L40)

**パラメータ:**

in_channels (`int`、*任意*、デフォルトは 3) : 入力画像のチャネル数。

out_channels (`int`、*optional*、デフォルトは 3) : 出力のチャネル数。

encoder_block_out_channels (`tuple[int]`、*optional*、デフォルトは`(64, 64, 64, 64)`) : 各エンコーダーブロックの出力チャネル数を表す整数のタプル。タプルの長さは、エンコーダーブロックの数と等しくなければなりません。

decoder_block_out_channels (`tuple[int]`、*optional*、デフォルトは`(64, 64, 64, 64)`) : 各デコーダーブロックの出力チャネル数を表す整数のタプルです。タプルの長さは、デコーダーブロックの数と等しくなければなりません。

act_fn (`str`、*任意*、デフォルトは`"relu"`) : モデル全体で使用される活性化関数。

latent_channels (`int`、*optional*、デフォルトは 4) : 潜在表現のチャネル数。潜在空間は、入力画像の圧縮された表現として機能します。

upsampling_scaling_factor (`int`、*任意*、デフォルトは 2) : デコーダーにおけるアップサンプリングのスケール係数です。アップサンプリング処理中の出力画像のサイズを決定します。

num_encoder_blocks (`tuple[int]`、*optional*、デフォルトは`(1, 3, 3, 3)`) : エンコーディング処理の各ステージにおけるエンコーダーブロックの数を表す整数のタプル。タプルの長さは、エンコーダーのステージ数と等しくする必要があります。各ステージには異なる数のエンコーダーブロックがあります。

num_decoder_blocks (`tuple[int]`、*optional*、デフォルトは`(3, 3, 3, 1)`) : デコーディングプロセスの各ステージにおけるデコーダーブロックの数を表す整数のタプルです。タプルの長さは、デコーダーのステージ数と等しくする必要があります。各ステージには異なる数のデコーダーブロックがあります。

latent_magnitude (`float`、*任意*、デフォルト値は 3.0) : 潜在表現の大きさ。このパラメータは潜在表現の値をスケーリングし、情報保持の範囲を制御します。

latent_shift (float, *optional*, デフォルト値は 0.5) : 潜在表現に適用されるシフト。このパラメータは潜在空間の中心を制御します。

scaling_factor (`float`, *optional*, デフォルト値は 1.0) : 学習セットの最初のバッチを使用して計算された、学習済み潜在空間の要素ごとの標準偏差です。これは、拡散モデルの学習時に潜在空間を分散 1 にスケーリングするために使用されます。潜在変数は、拡散モデルに渡される前に `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 を参照してください。ただし、この Autoencoder についてはそのようなスケーリング係数が使用されなかったため、デフォルト値は 1.0 となっています。

force_upcast (`bool`、*任意*、デフォルトは`False`) : 有効にすると、SD-XL などの高解像度画像パイプラインにおいて、VAE を float32 で実行するよう強制します。VAE は、精度を大きく失うことなく、より低い範囲にファインチューニング / 学習させることができます。その場合、`force_upcast` を `False` に設定できます（この fp16 対応の [AutoEncoder](https://huggingface.co/madebyollin/sdxl-vae-fp16-fix) を参照）。

画像を潜在変数にエンコードし、潜在表現を画像にデコードするための、小型の蒸留VAEモデルです。

[AutoencoderTiny](/docs/diffusers/v0.43.0/ja/api/models/autoencoder_tiny#diffusers.AutoencoderTiny) は、`TAESD` の元の実装をラップしたものです。

このモデルは [ModelMixin](/docs/diffusers/v0.43.0/ja/api/models/overview#diffusers.ModelMixin) を継承しています。すべてのモデルに実装されている汎用的なメソッド（ダウンロードや保存など）については、スーパークラスのドキュメントを参照してください。

#### forward[[diffusers.AutoencoderTiny.forward]]

```python
forward(sample: Tensor, return_dict: bool = True)
```

[ソース](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_tiny.py#L291)

**パラメータ:**

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

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

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

`return_dict` が True の場合、`DecoderOutput` が返され、それ以外の場合は通常の `tuple` が返されます。

#### scale_latents[[diffusers.AutoencoderTiny.scale_latents]]

```python
scale_latents(x: Tensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_tiny.py#L156)

raw latents -> [0, 1]

#### unscale_latents[[diffusers.AutoencoderTiny.unscale_latents]]

```python
unscale_latents(x: Tensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_tiny.py#L160)

[0, 1] -> 生の潜在変数

## AutoencoderTinyOutput[[diffusers.models.autoencoders.autoencoder_tiny.AutoencoderTinyOutput]]

#### diffusers.models.autoencoders.autoencoder_tiny.AutoencoderTinyOutput[[diffusers.models.autoencoders.autoencoder_tiny.AutoencoderTinyOutput]]

```python
diffusers.models.autoencoders.autoencoder_tiny.AutoencoderTinyOutput(latents: Tensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/models/autoencoders/autoencoder_tiny.py#L28)

**パラメータ:**

latents (`torch.Tensor`) : `Encoder`のエンコード済み出力。

AutoencoderTinyのエンコードメソッドの出力。

