# FlowMatchEulerDiscreteScheduler

`FlowMatchEulerDiscreteScheduler` 基于 [Stable Diffusion 3](https://huggingface.co/papers/2403.03206) 中引入的 flow-matching 采样方法。

## FlowMatchEulerDiscreteScheduler[[diffusers.FlowMatchEulerDiscreteScheduler]]

#### diffusers.FlowMatchEulerDiscreteScheduler[[diffusers.FlowMatchEulerDiscreteScheduler]]

```python
diffusers.FlowMatchEulerDiscreteScheduler(num_train_timesteps: int = 1000, shift: float = 1.0, use_dynamic_shifting: bool = False, base_shift: float | None = 0.5, max_shift: float | None = 1.15, base_image_seq_len: int = 256, max_image_seq_len: int = 4096, invert_sigmas: bool = False, shift_terminal: float = None, use_karras_sigmas: bool = False, use_exponential_sigmas: bool = False, use_beta_sigmas: bool = False, time_shift_type: typing.Literal['exponential', 'linear'] = 'exponential', stochastic_sampling: bool = False)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L48)

**参数：**

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

shift (`float`，默认值为 1.0)：时间步调度的偏移值。

use_dynamic_shifting (`bool`，默认为 False)：是否根据图像分辨率实时应用时间步偏移。

base_shift (`float`，默认值为 0.5)：用于稳定图像生成的值。增大 `base_shift` 会减少变化，使图像与期望输出更加一致。

max_shift (`float`，默认值为 1.15)：允许对潜在向量进行的数值变更。增大 `max_shift` 会鼓励产生更多变化，图像可能会更加夸张或风格化。

base_image_seq_len (`int`，默认为 256)：基础图像序列长度。

max_image_seq_len (`int`，默认为 4096)：最大图像序列长度。

invert_sigmas (`bool`，默认为 False)：是否反转 sigmas。

shift_terminal (`float`，默认为 None)：移位时间步调度的结束值。

use_karras_sigmas (`bool`，默认为 False)：是否在采样期间的噪声调度中使用 Karras sigmas 作为步长。

use_exponential_sigmas (`bool`，默认为 False)：在采样过程中，噪声调度的步长是否使用指数 sigma。

use_beta_sigmas (`bool`，默认为 False)：在采样期间的噪声调度中，是否使用 beta sigmas 作为步长。

time_shift_type (`str`，默认为 "exponential") : 要应用的动态分辨率相关时间步偏移类型。可以是 "exponential" 或 "linear"。

stochastic_sampling (`bool`，默认为 False)：是否使用随机采样。

Euler 调度器。

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

#### index_for_timestep[[diffusers.FlowMatchEulerDiscreteScheduler.index_for_timestep]]

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

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L393)

**参数：**

timestep (`float` 或 `torch.FloatTensor`)：用于查找索引的时间步。

schedule_timesteps (`torch.FloatTensor`, *可选*) : 用于验证的调度时间步。如果为 `None`，则使用调度器的时间步。

**返回值：** `int`

时间步的索引。

获取给定时间步的索引。

#### scale_noise[[diffusers.FlowMatchEulerDiscreteScheduler.scale_noise]]

```python
scale_noise(sample: FloatTensor, timestep: typing.Union[float, torch.FloatTensor], noise: typing.Optional[torch.FloatTensor] = None)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L188)

**参数：**

sample (`torch.FloatTensor`)：输入样本。

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

noise (`torch.FloatTensor`) : 噪声张量。

**返回：** `torch.FloatTensor`

经过缩放的输入样本。

流匹配中的前向过程

#### set_begin_index[[diffusers.FlowMatchEulerDiscreteScheduler.set_begin_index]]

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

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L168)

**参数：**

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

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

#### set_shift[[diffusers.FlowMatchEulerDiscreteScheduler.set_shift]]

```python
set_shift(shift: float)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L178)

**参数：**

shift (`float`) : 要设置的偏移值。

设置调度器的 shift 值。

#### set_timesteps[[diffusers.FlowMatchEulerDiscreteScheduler.set_timesteps]]

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

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L283)

**参数：**

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

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

sigmas (`list[float]`, *可选*) : 用于每个扩散步骤的自定义 sigmas 值。如果为 `None`，则 sigmas 会自动计算。

mu (`float`, *可选*) : 确定在执行分辨率相关的 timestep 移位时，对 sigmas 应用的移位量。

timesteps (`list[float]`, *可选*) : 用于每个扩散步的自定义时间步值。如果为 `None`，则时间步会自动计算。

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

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

```python
step(model_output: FloatTensor, timestep: typing.Union[float, torch.FloatTensor], sample: FloatTensor, 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, per_token_timesteps: typing.Optional[torch.Tensor] = None, return_dict: bool = True)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L432)

**参数：**

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

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

sample (`torch.FloatTensor`)：扩散过程创建的当前样本实例。

s_churn (`float`) --

s_tmin  (`float`) --

s_tmax (`float`) --

s_noise (`float`，默认为 1.0)：添加到样本中的噪声的缩放因子。

generator (`torch.Generator`, *可选*) : 随机数生成器。

per_token_timesteps (`torch.Tensor`, *可选*) : 样本中每个 token 的时间步。

return_dict (`bool`，默认为 `True`)：是否返回 `FlowMatchEulerDiscreteSchedulerOutput` 或元组。

**返回：** `FlowMatchEulerDiscreteSchedulerOutput` 或 `tuple`

If return_dict is `True`, `FlowMatchEulerDiscreteSchedulerOutput` is returned, otherwise a tuple is returned where the first element is the sample tensor.

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

#### stretch_shift_to_terminal[[diffusers.FlowMatchEulerDiscreteScheduler.stretch_shift_to_terminal]]

```python
stretch_shift_to_terminal(t: Tensor)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L262)

**参数：**

t (`torch.Tensor`) : 一个待拉伸和偏移的时间步张量。

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

一个经过调整的 timesteps 张量，其最终值等于 `self.config.shift_terminal`。

拉伸并平移时间步调度，以确保其在配置的 `shift_terminal` 配置值处终止。

参考：https://github.com/Lightricks/LTX-Video/blob/a01a171f8fe3d99dce2728d60a73fecf4d4238ae/ltx_video/schedulers/rf.py#L51

#### time_shift[[diffusers.FlowMatchEulerDiscreteScheduler.time_shift]]

```python
time_shift(mu: float, sigma: float, t: Tensor)
```

[源代码](https://github.com/huggingface/diffusers/blob/v0.43.0/src/diffusers/schedulers/scheduling_flow_match_euler_discrete.py#L241)

**参数：**

mu (`float`) : 时间偏移的 mu 参数。

sigma (`float`) : 时间偏移的 sigma 参数。

t (`torch.Tensor`) : 输入的时间步。

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

时间偏移的时间步。

对 sigmas 应用时间偏移。

