# HeunDiscreteScheduler

Heun スケジューラ（アルゴリズム 1）は、Karras らによる [Elucidating the Design Space of Diffusion-Based Generative Models](https://huggingface.co/papers/2206.00364) 論文に由来します。このスケジューラは [k-diffusion](https://github.com/crowsonkb/k-diffusion) ライブラリから移植され、[Katherine Crowson](https://github.com/crowsonkb/) 氏によって作成されました。

## HeunDiscreteScheduler[[diffusers.HeunDiscreteScheduler]]

#### diffusers.HeunDiscreteScheduler[[diffusers.HeunDiscreteScheduler]]

```python
diffusers.HeunDiscreteScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.00085, beta_end: float = 0.012, beta_schedule: str = 'linear', trained_betas: numpy.ndarray | list[float] | None = None, prediction_type: str = 'epsilon', use_karras_sigmas: bool = False, use_exponential_sigmas: bool = False, use_beta_sigmas: bool = False, clip_sample: bool = False, clip_sample_range: float = 1.0, timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'linspace', steps_offset: int = 0)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L103)

**パラメータ:**

num_train_timesteps (`int`、デフォルトは 1000) : モデルを学習するための拡散ステップ数。

beta_start (`float`、デフォルト値は 0.0001) : 推論の初期 `beta` 値。

beta_end (`float`、デフォルト値は 0.02) : 最終的な `beta` 値。

beta_schedule (`"linear"`、`"scaled_linear"`、`"squaredcos_cap_v2"`、または `"exp"`、デフォルトは `"linear"`) : ベータスケジュールです。ベータの範囲を、モデルをステップするためのベータの列にマッピングします。`linear`、`scaled_linear`、`squaredcos_cap_v2`、または `exp` から選択してください。

trained_betas (`np.ndarray`、*任意*) : `beta_start` と `beta_end` をバイパスして、ベータの配列を直接コンストラクタに渡す。

prediction_type (`"epsilon"`, `"sample"`, or `"v_prediction"`, defaults to `"epsilon"`, *optional*) : Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process), `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper).

clip_sample (`bool`、デフォルトは`True`) : 数値の安定性を確保するために、予測サンプルをクリップします。

clip_sample_range (`float`、デフォルト値は 1.0) : サンプルクリッピングの最大振幅。`clip_sample=True` の場合のみ有効です。

use_karras_sigmas (`bool`, *任意*, デフォルトは `False`) : サンプリングプロセス中のノイズスケジュールにおけるステップサイズに Karras シグマを使用するかどうか。`True` の場合、シグマはノイズレベルの列 {σi} に基づいて決定されます。

use_exponential_sigmas (`bool`、*任意*、デフォルトは`False`) : サンプリングプロセス中のノイズスケジュールにおいて、ステップサイズに指数シグマを使用するかどうか。

use_beta_sigmas (`bool`, *optional*, defaults to `False`) : Whether to use beta sigmas for step sizes in the noise schedule during the sampling process. Refer to [Beta Sampling is All You Need](https://huggingface.co/papers/2407.12173) for more information.

timestep_spacing (`"linspace"`, `"leading"`, or `"trailing"`, defaults to `"linspace"`) : The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.

steps_offset (`int`、デフォルトは 0) : 一部のモデルファミリーで必要とされる、推論ステップに追加されるオフセットです。

離散ベータスケジュール用のHeunステップ付きスケジューラ。

このモデルは [SchedulerMixin](/docs/diffusers/main/ja/api/schedulers/overview#diffusers.SchedulerMixin) と [ConfigMixin](/docs/diffusers/main/ja/api/configuration#diffusers.ConfigMixin) を継承しています。読み込みや保存など、ライブラリがすべてのスケジューラに対して実装している汎用的なメソッドについては、スーパークラスのドキュメントを参照してください。

#### add_noise[[diffusers.HeunDiscreteScheduler.add_noise]]

```python
add_noise(original_samples: Tensor, noise: Tensor, timesteps: Tensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L662)

**パラメータ:**

original_samples (`torch.Tensor`) : ノイズが加えられる元のサンプル。

noise (`torch.Tensor`) : 元のサンプルに追加するノイズのテンソル。

timesteps (`torch.Tensor`) : ノイズを追加するタイムステップ。スケジュールに基づいてノイズレベルを決定します。

**戻り値:** `torch.Tensor`

タイムステップスケジュールに従ってスケーリングされたノイズが加えられたノイズ付きサンプル。

指定されたタイムステップにおけるノイズスケジュールに従って、元のサンプルにノイズを追加します。

#### index_for_timestep[[diffusers.HeunDiscreteScheduler.index_for_timestep]]

```python
index_for_timestep(timestep: typing.Union[float, torch.Tensor], schedule_timesteps: typing.Optional[torch.Tensor] = None)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L198)

**パラメータ:**

timestep (`float` または `torch.Tensor`) : スケジュール内で検索するタイムステップの値。

schedule_timesteps (`torch.Tensor`, *任意*) : 検索対象のタイムステップスケジュール。`None` の場合は `self.timesteps` を使用します。

**戻り値:** `int`

スケジュールにおけるタイムステップのインデックスです。最初のステップでは、スケジュールの途中から開始する場合（画像から画像への生成など）にシグマをスキップすることを避けるため、複数の一致が存在する場合は2番目のインデックスを返します。

与えられたタイムステップのタイムステップスケジュールにおけるインデックスを特定します。

#### scale_model_input[[diffusers.HeunDiscreteScheduler.scale_model_input]]

```python
scale_model_input(sample: Tensor, timestep: typing.Union[float, torch.Tensor])
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L261)

**パラメータ:**

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

timestep (`float` または `torch.Tensor`) : 拡散チェーンにおける現在のタイムステップ。

**戻り値:** `torch.Tensor`

スケーリングされた入力サンプル。

現在のタイムステップに応じて、デノイジングモデルの入力をスケーリングする必要があるスケジューラとの相互運用性を保証します。

#### set_begin_index[[diffusers.HeunDiscreteScheduler.set_begin_index]]

```python
set_begin_index(begin_index: int = 0)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L251)

**パラメータ:**

begin_index (`int`、デフォルトは`0`) : スケジューラの開始インデックス。

スケジューラの開始インデックスを設定します。この関数は、推論の前にパイプラインから実行する必要があります。

#### set_timesteps[[diffusers.HeunDiscreteScheduler.set_timesteps]]

```python
set_timesteps(num_inference_steps: int | None = None, device: typing.Union[str, torch.device] = None, num_train_timesteps: int | None = None, timesteps: list[int] | None = None)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L287)

**パラメータ:**

num_inference_steps (`int`、*任意*、デフォルトは`None`) : 事前学習済みモデルでサンプルを生成する際に使用される拡散ステップ数。

device (`str`、`torch.device`、*optional*、デフォルトは `None`) : timesteps を移動先とするデバイスです。`None` の場合、timesteps は移動されません。

num_train_timesteps (`int`、*任意*、デフォルトは`None`) : モデルを学習する際に使用する拡散ステップ数です。`None`の場合、デフォルトの`num_train_timesteps`属性が使用されます。

timesteps (`list[int]`, *任意*) : 任意の間隔でタイムステップを扱うために使用するカスタムタイムステップ。`None` の場合、`timestep_spacing` 属性に基づいてタイムステップが生成されます。`timesteps` が渡された場合、`num_inference_steps` は `None` でなければならず、`timestep_spacing` 属性は無視されます。

拡散チェーンで使用される離散タイムステップを設定します（推論の実行前に実行する必要があります）。

#### ステップ[[diffusers.HeunDiscreteScheduler.step]]

```python
step(model_output: typing.Union[torch.Tensor, numpy.ndarray], timestep: typing.Union[float, torch.Tensor], sample: typing.Union[torch.Tensor, numpy.ndarray], return_dict: bool = True)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_heun_discrete.py#L558)

**パラメータ:**

model_output (`torch.Tensor`) : 学習済み拡散モデルからの直接の出力です。

timestep (`float`) : 拡散チェーンにおける現在の離散タイムステップ。

sample (`torch.Tensor`) : 拡散プロセスによって生成されたサンプルの現在のインスタンス。

return_dict (`bool`) : `HeunDiscreteSchedulerOutput`を返すか、タプルを返すかを指定します。

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

`True` の場合、`HeunDiscreteSchedulerOutput` が返され、それ以外の場合は、最初の要素がサンプルテンソルであるタプルが返されます。

前回のタイムステップからのサンプルを、SDEを逆方向に適用することで予測します。この関数は、学習済みモデルの出力（最もよく使われるのは予測ノイズ）から拡散過程を伝播させます。

## SchedulerOutput[[diffusers.schedulers.scheduling_utils.SchedulerOutput]]

#### diffusers.schedulers.scheduling_utils.SchedulerOutput[[diffusers.schedulers.scheduling_utils.SchedulerOutput]]

```python
diffusers.schedulers.scheduling_utils.SchedulerOutput(prev_sample: Tensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_utils.py#L66)

**パラメータ:**

prev_sample (`torch.Tensor`、画像の場合は `(batch_size, num_channels, height, width)` の形状) : 前のタイムステップの計算済みサンプル `(x_{t-1})`。`prev_sample` は、デノイジングループの次のモデル入力として使用する必要があります。

スケジューラーの`step`関数の出力の基底クラス。

