# EDMEulerScheduler

Karras 等人论文 [Elucidating the Design Space of Diffusion-Based Generative Models](https://huggingface.co/papers/2206.00364) 中提出的 Euler 调度器（算法 2）的 Karras 公式。这是一种快速调度器，通常能在 20-30 步内生成良好的输出。该调度器基于 [Katherine Crowson](https://github.com/crowsonkb/) 的原始 [k-diffusion](https://github.com/crowsonkb/k-diffusion/blob/481677d114f6ea445aa009cf5bd7a9cdee909e47/k_diffusion/sampling.py#L51) 实现。

## EDMEulerScheduler[[diffusers.EDMEulerScheduler]]

#### diffusers.EDMEulerScheduler[[diffusers.EDMEulerScheduler]]

```python
diffusers.EDMEulerScheduler(sigma_min: float = 0.002, sigma_max: float = 80.0, sigma_data: float = 0.5, sigma_schedule: typing.Literal['karras', 'exponential'] = 'karras', num_train_timesteps: int = 1000, prediction_type: typing.Literal['epsilon', 'v_prediction'] = 'epsilon', rho: float = 7.0, final_sigmas_type: typing.Literal['zero', 'sigma_min'] = 'zero')
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L49)

**参数：**

sigma_min (`float`，*可选*，默认值为 `0.002`)：sigma 调度中的最小噪声幅度。EDM 论文 [1] 中将其设置为 0.002；合理的取值范围为 [0, 10]。

sigma_max (`float`，*可选*，默认值为 `80.0`)：sigma 调度中的最大噪声幅度。EDM 论文 [1] 中将其设置为 80.0；合理的取值范围为 [0.2, 80.0]。

sigma_data (`float`，*可选*，默认值为 `0.5`)：数据分布的标准差。在 EDM 论文 [1] 中，该值被设置为 0.5。

sigma_schedule (`Literal["karras", "exponential"]`，*可选*，默认为 `"karras"`)：用于计算 `sigmas` 的 Sigma 调度。默认情况下，我们使用 EDM 论文（https://huggingface.co/papers/2206.00364）中提出的调度。该模型中引入了 `"exponential"` 调度：https://huggingface.co/stabilityai/cosxl。

num_train_timesteps (`int`，*可选*，默认为 `1000`)：用于训练模型的扩散步数。

prediction_type (`Literal["epsilon", "v_prediction"]`，*可选*，默认为 `"epsilon"`)：调度器函数的预测类型。`"epsilon"` 预测扩散过程的噪声，而 `"v_prediction"`（参见 [Imagen Video](https://huggingface.co/papers/2210.02303) 论文的第 2.4 节）。

rho (`float`, *可选*，默认为 `7.0`) : 用于计算 Karras sigma 调度的 rho 参数，在 EDM 论文 [1] 中设置为 7.0。

final_sigmas_type (`Literal["zero", "sigma_min"]`，*可选*，默认为 `"zero"`)：采样过程中噪声调度的最终 `sigma` 值。如果为 `"sigma_min"`，则最终 sigma 与训练调度中的最后一个 sigma 相同。如果为 `"zero"`，则最终 sigma 设为 0。

实现了 Karras 等人 2022 年 [1] 中提出的 EDM 形式下的 Euler 调度器。

[1] Karras, Tero, et al. "Elucidating the Design Space of Diffusion-Based Generative Models." https://huggingface.co/papers/2206.00364

该模型继承自 [SchedulerMixin](/docs/diffusers/main/zh/api/schedulers/overview#diffusers.SchedulerMixin) 和 [ConfigMixin](/docs/diffusers/main/zh/api/configuration#diffusers.ConfigMixin)。有关该库为所有调度器实现的通用方法（例如加载和保存），请参阅父类文档。

#### add_noise[[diffusers.EDMEulerScheduler.add_noise]]

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

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L536)

**参数：**

original_samples (`torch.Tensor`) : 将添加噪声的原始样本。

noise (`torch.Tensor`) : 要添加到原始样本中的噪声张量。

timesteps (`torch.Tensor`) : 添加噪声的时间步，用于从调度器中确定噪声水平。

**返回：** `torch.Tensor`

根据时间步调度对添加的噪声进行缩放后的含噪样本。

根据指定时间步的噪声调度，向原始样本添加噪声。

#### index_for_timestep[[diffusers.EDMEulerScheduler.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_edm_euler.py#L381)

**参数：**

timestep (`float` 或 `torch.Tensor`)：要在调度器中查找的时间步值。

schedule_timesteps (`torch.Tensor`, *可选*) : 要搜索的时间步调度。如果为 `None`，则使用 `self.timesteps`。

**返回值：** `int`

时间步在调度器中的索引。对于第一步，如果存在多个匹配项，则返回第二个索引，以避免在从调度器中间开始（例如，用于图像到图像任务）时跳过某个 sigma。

查找给定时间步在时间步调度中的索引。

#### precondition_inputs[[diffusers.EDMEulerScheduler.precondition_inputs]]

```python
precondition_inputs(sample: Tensor, sigma: typing.Union[float, torch.Tensor])
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L177)

**参数：**

sample (`torch.Tensor`) : 需要预处理的输入样本张量。

sigma (`float` 或 `torch.Tensor`)：当前的 sigma（噪声水平）值。

**返回：** `torch.Tensor`

缩放后的输入样本。

根据 EDM 公式对输入样本进行缩放，以对其进行预处理。

#### precondition_noise[[diffusers.EDMEulerScheduler.precondition_noise]]

```python
precondition_noise(sigma: typing.Union[float, torch.Tensor])
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L195)

**参数：**

sigma (`float` 或 `torch.Tensor`)：用于预处理的 sigma（噪声水平）值。

**返回：** `torch.Tensor`

计算得到的预条件噪声值，即 `0.25 * log(sigma)`。

通过对噪声水平应用对数变换来进行预处理。

#### precondition_outputs[[diffusers.EDMEulerScheduler.precondition_outputs]]

```python
precondition_outputs(sample: Tensor, model_output: Tensor, sigma: typing.Union[float, torch.Tensor])
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L214)

**参数：**

sample (`torch.Tensor`) : 输入样本张量。

model_output (`torch.Tensor`)：学习到的扩散模型的直接输出。

sigma (`float` 或 `torch.Tensor`)：当前的 sigma（噪声水平）值。

**返回：** `torch.Tensor`

通过结合跳跃连接和输出缩放计算得到的去噪样本。

根据 EDM 公式对模型输出进行预处理。

#### scale_model_input[[diffusers.EDMEulerScheduler.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_edm_euler.py#L249)

**参数：**

sample (`torch.Tensor`) : 输入样本张量。

timestep (`float` 或 `torch.Tensor`)：扩散链中的当前时间步。

**返回：** `torch.Tensor`

经过缩放的输入样本。

将去噪模型的输入进行缩放，以匹配 Euler 算法。确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器互换使用。

#### set_begin_index[[diffusers.EDMEulerScheduler.set_begin_index]]

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

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L167)

**参数：**

begin_index (`int`，默认为 `0`)：调度器的起始索引。

设置调度器的起始索引。此函数应在推理之前从流水线中运行。

#### set_timesteps[[diffusers.EDMEulerScheduler.set_timesteps]]

```python
set_timesteps(num_inference_steps: int = None, device: typing.Union[str, torch.device] = None, sigmas: typing.Union[list[float], torch.Tensor, NoneType] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L273)

**参数：**

num_inference_steps (`int`, *可选*)：使用预训练模型生成样本时所用的扩散步数。

device (`str` 或 `torch.device`，*可选*)：时间步应移动到的设备。如果为 `None`，则不移动时间步。

sigmas (`torch.Tensor | list[float]`, *可选*) : 用于去噪过程的自定义 sigmas。如果未定义，则使用传入 `num_inference_steps` 时的默认行为。

设置扩散链所使用的离散时间步（需在推理之前运行）。

#### 步骤[[diffusers.EDMEulerScheduler.step]]

```python
step(model_output: Tensor, timestep: typing.Union[float, torch.Tensor], sample: Tensor, s_churn: float = 0.0, s_tmin: float = 0.0, s_tmax: float = inf, s_noise: float = 1.0, generator: typing.Optional[torch.Generator] = None, return_dict: bool = True, pred_original_sample: typing.Optional[torch.Tensor] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L427)

**参数：**

model_output (`torch.Tensor`)：学习到的扩散模型的直接输出。

timestep (`float` 或 `torch.Tensor`)：扩散链中当前的离散时间步。

sample (`torch.Tensor`)：扩散过程生成的当前样本实例。

s_churn (`float`, *可选*，默认为 `0.0`)：每个步骤中添加的随机性程度。值越高，添加的噪声越多。

s_tmin (`float`, *可选*，默认为 `0.0`)：低于此最小 sigma 阈值时不添加噪声。

s_tmax (`float`，*可选*，默认为 `float("inf")`)：超过此最大 sigma 阈值后不再添加噪声。

s_noise (`float`, *可选*, 默认值为 `1.0`) : 添加到样本中的噪声的缩放因子。

generator (`torch.Generator`, *可选*) : 用于可复现性的随机数生成器。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 [EDMEulerSchedulerOutput](/docs/diffusers/main/zh/api/schedulers/edm_euler#diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput) 或元组。

pred_original_sample (`torch.Tensor`, *可选*) : 来自上一步的预测去噪样本。如果提供，则跳过重新计算。

**返回：** [EDMEulerSchedulerOutput](/docs/diffusers/main/zh/api/schedulers/edm_euler#diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput) 或 `tuple`

如果 `return_dict` 为 `True`，则返回 [EDMEulerSchedulerOutput](/docs/diffusers/main/zh/api/schedulers/edm_euler#diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput)；否则返回一个元组，其中第一个元素是前一个样本张量，第二个元素是预测的原始样本张量。

通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出（通常是预测的噪声）进行传播。

## EDMEulerSchedulerOutput[[diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput]]

#### diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput[[diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput]]

```python
diffusers.schedulers.scheduling_edm_euler.EDMEulerSchedulerOutput(prev_sample: Tensor, pred_original_sample: typing.Optional[torch.Tensor] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_edm_euler.py#L32)

**参数：**

prev_sample （对于图像，形状为 `(batch_size, num_channels, height, width)` 的 `torch.Tensor`）：计算得到的前一时间步的样本 `(x_{t-1})`。在去噪循环中，应将 `prev_sample` 用作下一个模型输入。

pred_original_sample （对于图像，形状为 `(batch_size, num_channels, height, width)` 的 `torch.Tensor`）：基于当前时间步的模型输出预测的去噪样本 `(x_{0})`。`pred_original_sample` 可用于预览进度或进行引导。

调度器 `step` 函数输出的输出类。

