# FlowMapEulerDiscreteScheduler

`FlowMapEulerDiscreteScheduler` 是一种专为 flow-map 蒸馏扩散模型设计的欧拉风格采样器。Flow-map 模型学习的是任意区间上的状态转移 $\mathbf{z}_t \to \mathbf{z}_r$，而非一致性模型中固定的 $\mathbf{z}_t \to \mathbf{z}_0$ 映射。该步的两个端点均由调用方提供，这正是实现任意步数采样的关键：单个蒸馏检查点无需重新训练，即可在 1、2、4、8、16……NFE 下进行评估。

该调度器在 [AnyFlow: Any-Step Video Diffusion Model with On-Policy Flow Map Distillation](https://huggingface.co/papers/2605.13724) 中引入，并随 `AnyFlowPipeline` 和 `AnyFlowFARPipeline` 集成提供，但它并非 AnyFlow 专属——任何经过 flow-map 蒸馏的 checkpoint 都可以使用它。

## FlowMapEulerDiscreteScheduler[[diffusers.FlowMapEulerDiscreteScheduler]]

#### diffusers.FlowMapEulerDiscreteScheduler[[diffusers.FlowMapEulerDiscreteScheduler]]

```python
diffusers.FlowMapEulerDiscreteScheduler(num_train_timesteps: int = 1000, shift: float = 1.0)
```

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

**参数：**

num_train_timesteps (*int*，默认为 1000)：用于训练底层 flow-map 模型的扩散步数。

shift (*float*，默认值为 1.0)：应用于推理调度的乘性时间步偏移。`shift=1.0` 表示恒等变换；大于 1.0 的值会将调度推向在后续步骤中进行更多去噪（例如，`shift=5` 与 Wan2.1 的默认设置一致）。

用于流映射蒸馏扩散模型的欧拉风格采样器。

Flow-map 模型学习任意区间的转换 \\(z_t \to z_r\\)，而非一致性模型中固定的 \\(z_t \to z_0\\) 映射，因此单个蒸馏检查点可以在 1、2、4、8、... NFE 下进行评估，而无需重新训练。*step* 方法沿预测速度将样本从 *timestep* 推进到 *r_timestep*。

由 Yuchao Gu、Guian Fang 等人提出的 [AnyFlow: Any-Step Video Diffusion Model with On-Policy Flow Map Distillation](https://huggingface.co/papers/2605.13724) 中引入。

该调度器继承自 [*SchedulerMixin*] 和 [*ConfigMixin*]。有关为所有调度器实现的通用方法（如加载、保存等），请参阅父类文档。

#### apply_shift[[diffusers.FlowMapEulerDiscreteScheduler.apply_shift]]

```python
apply_shift(sigmas: Tensor)
```

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

对 sigma 张量应用已配置的 shift 变换。

#### index_for_timestep[[diffusers.FlowMapEulerDiscreteScheduler.index_for_timestep]]

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

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

返回 `timestep` 在当前调度上的索引，如果不在调度上则返回 `None`。

查找操作基于 `self.timesteps` 进行，并带有较小的浮点容差。该操作用于恢复对应的 sigma，而不假设线性的 `timesteps = sigmas * num_train_timesteps` 关系——这样，自定义调度（例如非线性偏移、手动设置的 timesteps）也能正确解析。

#### scale_model_input[[diffusers.FlowMapEulerDiscreteScheduler.scale_model_input]]

```python
scale_model_input(sample: Tensor, *args, **kwargs)
```

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

无操作恒等缩放。为与其他 Diffusers 调度器保持 API 兼容而提供。

#### scale_noise[[diffusers.FlowMapEulerDiscreteScheduler.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_map_euler_discrete.py#L103)

根据归一化的 `timestep`，将 `sample` 线性插值到 `noise`。

#### set_begin_index[[diffusers.FlowMapEulerDiscreteScheduler.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_map_euler_discrete.py#L94)

设置调度器的起始索引。从调度中途开始的流水线（例如 image-to-image）会在 `set_timesteps` 和第一次 `step` 之间调用此方法，以锚定 rollout。

#### set_timesteps[[diffusers.FlowMapEulerDiscreteScheduler.set_timesteps]]

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

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

**参数：**

num_inference_steps (*int*, *optional*) : 推理步数。如果为 `None`，则必须传入 `sigmas` 或 `timesteps`。

device (*str* 或 *torch.device*，*可选*)：`self.sigmas` / `self.timesteps` 的目标设备。

sigmas (*List[float]*, *可选*) : 长度为 `num_inference_steps` 的自定义 sigma 调度。末尾的 `0` sigma 会自动追加。配置的 `shift` 会在此基础上应用。

timesteps (*List[float]*, *可选*) : 长度为 `num_inference_steps` 的自定义时间步调度，单位与 `self.timesteps` 相同（即按 `num_train_timesteps` 缩放）。内部会将其转换为 sigmas。如果同时传入 `sigmas` 和 `timesteps`，它们的长度必须一致。

构建推理时间步调度。

内部跟踪长度为 `num_inference_steps + 1` 的 `self.sigmas`（默认情况下，对从 `1.0` 到 `0.0` 的 linspace 应用已配置的 shift）；`self.timesteps` 暴露前 `num_inference_steps` 个经 `num_train_timesteps` 缩放的 sigmas——即每个推理步对应一个时间步，与 [FlowMatchEulerDiscreteScheduler](/docs/diffusers/v0.43.0/zh/api/schedulers/flow_match_euler_discrete#diffusers.FlowMatchEulerDiscreteScheduler) 保持一致。最后一个 sigma（`0`）是最后一步的隐式 r 端点，当用户自行提供 `sigmas` / `timesteps` 时会自动追加。

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

```python
step(model_output: FloatTensor, timestep: typing.Union[float, torch.FloatTensor], sample: FloatTensor, r_timestep: typing.Union[float, torch.FloatTensor, NoneType] = None, return_dict: bool = True)
```

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

**参数：**

model_output (*torch.Tensor*)：流映射模型的直接输出（预测的平均速度）。

timestep (*float* 或 *torch.Tensor*)：源时间步 `t`，其单位与 `self.timesteps` 相同。

sample (*torch.Tensor*) : 当前样本 \\(z_t\\)。

r_timestep (*float* 或 *torch.Tensor*，*可选*)：目标时间步 `r`。当为 `None` 时，默认为调度上的下一个时间步；对于任意步采样，请传入显式值。`r_timestep == timestep` 是一个无操作。

return_dict (*bool*，默认为 *True*)：是否返回 [*FlowMapEulerDiscreteSchedulerOutput*]（默认）或普通元组。

**返回：** [*FlowMapEulerDiscreteSchedulerOutput*] 或 *tuple*

当 `return_dict=True` 时，返回一个 [*FlowMapEulerDiscreteSchedulerOutput*]，其 `prev_sample` 为 \\(z_r\\)。否则返回一个 1 元组 `(prev_sample,)`。

使用模型预测的速度，将 `sample` 从 `timestep` 推进到 `r_timestep`。

与标准 Euler 调度器不同，区间的两个端点均可由调用方提供，从而实现任意步数采样：单次模型调用即可从 *t* 步进到任意选定的目标 *r*（包括 *r=0* 用于一次性生成）。当省略 `r_timestep` 时，其默认值为调度上的下一个时间步（与 `FlowMatchEulerDiscreteScheduler` 的语义一致）。

在内部，源 sigma 和目标 sigma 是通过使用 `index_for_timestep` 对 `self.sigmas` 进行索引来恢复的，而不是通过将输入时间步除以 `num_train_timesteps` 来恢复的，因此任何时间步与 sigma 关系为非线性的调度（例如自定义 shift）都能保持正确。对于不在调度内的 `r_timestep`，调度器会回退到 `r_timestep / num_train_timesteps`，从而继续支持调度之外的任意步采样。

