# CogVideoXDDIMScheduler

`CogVideoXDDIMScheduler` 基于 [Denoising Diffusion Implicit Models](https://huggingface.co/papers/2010.02502)，专为 CogVideoX 模型设计。

## CogVideoXDDIMScheduler[[diffusers.CogVideoXDDIMScheduler]]

#### diffusers.CogVideoXDDIMScheduler[[diffusers.CogVideoXDDIMScheduler]]

```python
diffusers.CogVideoXDDIMScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.00085, beta_end: float = 0.012, beta_schedule: typing.Literal['linear', 'scaled_linear', 'squaredcos_cap_v2'] = 'scaled_linear', trained_betas: numpy.ndarray | list[float] | None = None, clip_sample: bool = True, set_alpha_to_one: bool = True, steps_offset: int = 0, prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction'] = 'epsilon', clip_sample_range: float = 1.0, sample_max_value: float = 1.0, timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'leading', rescale_betas_zero_snr: bool = False, snr_shift_scale: float = 3.0)
```

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

**参数：**

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

beta_start (`float`，默认值为 0.00085)：推理的初始 `beta` 值。

beta_end (`float`，默认值为 0.0120)：`beta` 的最终值。

beta_schedule (`str`，默认为 `"scaled_linear"`)：beta 调度，即将 beta 范围映射为用于模型步进的一系列 beta 值。可选 `linear`、`scaled_linear` 或 `squaredcos_cap_v2`。

trained_betas (`np.ndarray`，*可选*)：将 beta 数组直接传递给构造函数，以绕过 `beta_start` 和 `beta_end`。

clip_sample (`bool`，默认为 `True`)：对预测样本进行裁剪，以确保数值稳定性。

clip_sample_range (`float`，默认为 1.0)：样本裁剪的最大幅度。仅在 `clip_sample=True` 时有效。

set_alpha_to_one (`bool`，默认为 `True`)：每个扩散步骤都会使用该步骤及前一步的 alpha 乘积值。对于最后一步，不存在前一个 alpha。当此选项为 `True` 时，前一个 alpha 乘积固定为 `1`；否则，使用第 0 步的 alpha 值。

steps_offset (`int`，默认为 0)：根据某些模型系列的要求，添加到推理步数中的偏移量。

prediction_type (`str`, 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).

thresholding (`bool`，默认为 `False`)：是否使用“动态阈值”方法。此方法不适用于 Stable Diffusion 等潜在空间扩散模型。

dynamic_thresholding_ratio (`float`，默认值为 0.995)：动态阈值化方法的比例。仅在 `thresholding=True` 时有效。

sample_max_value (`float`，默认值为 1.0)：动态阈值化的阈值。仅在 `thresholding=True` 时有效。

timestep_spacing (`str`，默认为 `"leading"`)：时间步的缩放方式。更多信息请参阅 [通用扩散噪声调度和采样步骤存在缺陷](https://huggingface.co/papers/2305.08891) 中的表 2。

rescale_betas_zero_snr (`bool`，默认为 `False`)：是否将 betas 重新缩放，使其具有零终端 SNR。这使模型能够生成非常明亮和非常暗的样本，而不是将其限制在中等亮度的样本上。与 [`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506) 大致相关。

snr_shift_scale (`float`，默认值为 3.0)：SNR 的偏移比例。

`DDIMScheduler` 在去噪扩散概率模型（DDPM）所引入的去噪过程基础上，扩展了非马尔可夫引导。

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

#### add_noise[[diffusers.CogVideoXDDIMScheduler.add_noise]]

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

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

**参数：**

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

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

timesteps (`torch.IntTensor`)：指示每个样本噪声水平的时间步。

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

带噪声的样本。

根据每个时间步的噪声幅度，向原始样本添加噪声（即前向扩散过程）。

#### get_velocity[[diffusers.CogVideoXDDIMScheduler.get_velocity]]

```python
get_velocity(sample: Tensor, noise: Tensor, timesteps: IntTensor)
```

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

**参数：**

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

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

timesteps (`torch.IntTensor`)：用于速度计算的 timesteps。

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

计算得到的速度。

根据速度公式，从样本和噪声计算速度预测。

#### scale_model_input[[diffusers.CogVideoXDDIMScheduler.scale_model_input]]

```python
scale_model_input(sample: Tensor, timestep: int = None)
```

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

**参数：**

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

timestep (`int`, *可选*) : 扩散链中的当前时间步。

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

经过缩放的输入样本。

确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器保持可互换性。

#### set_timesteps[[diffusers.CogVideoXDDIMScheduler.set_timesteps]]

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

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

**参数：**

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

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

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

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

```python
step(model_output: Tensor, timestep: int, sample: Tensor, eta: float = 0.0, use_clipped_model_output: bool = False, generator: typing.Optional[torch.Generator] = None, variance_noise: typing.Optional[torch.Tensor] = None, return_dict: bool = True)
```

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

**参数：**

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

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

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

eta (`float`) : 在扩散步骤中添加噪声的噪声权重。

use_clipped_model_output (`bool`，默认为 `False`) : 如果为 `True`，则从经过裁剪的预测原始样本计算“校正后的”`model_output`。这是必要的，因为当 `self.config.clip_sample` 为 `True` 时，预测的原始样本会被裁剪到 [-1, 1] 范围内。如果未发生裁剪，则“校正后的”`model_output` 将与作为输入提供的那个一致，`use_clipped_model_output` 将不起作用。

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

variance_noise (`torch.Tensor`) : 直接提供方差本身的噪声，作为使用 `generator` 生成噪声的替代方案。适用于 `CycleDiffusion` 等方法。

return_dict (`bool`，*可选*，默认为 `True`)：是否返回 [DDIMSchedulerOutput](/docs/diffusers/v0.43.0/zh/api/schedulers/ddim#diffusers.schedulers.scheduling_ddim.DDIMSchedulerOutput) 或 `tuple`。

**返回：** [DDIMSchedulerOutput](/docs/diffusers/v0.43.0/zh/api/schedulers/ddim#diffusers.schedulers.scheduling_ddim.DDIMSchedulerOutput) 或 `tuple`

If return_dict is `True`, [DDIMSchedulerOutput](/docs/diffusers/v0.43.0/zh/api/schedulers/ddim#diffusers.schedulers.scheduling_ddim.DDIMSchedulerOutput) is returned, otherwise a tuple is returned where the first element is the sample tensor.

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

