class_docstr: Denoising diffusion probabilistic models (DDPMs) explores the connections between denoising score matching and
  Langevin dynamics sampling.
  
  [`~ConfigMixin`] takes care of storing all config attributes that are passed in the scheduler's `__init__`
  function, such as `num_train_timesteps`. They can be accessed via `scheduler.config.num_train_timesteps`.
  [`SchedulerMixin`] provides general loading and saving functionality via the [`SchedulerMixin.save_pretrained`] and
  [`~SchedulerMixin.from_pretrained`] functions.
  
  For more details, see the original paper: https://arxiv.org/abs/2006.11239
  
  Args:
      num_train_timesteps (`int`): number of diffusion steps used to train the model.
      beta_start (`float`): the starting `beta` value of inference.
      beta_end (`float`): the final `beta` value.
      beta_schedule (`str`):
          the beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
          `linear`, `scaled_linear`, `squaredcos_cap_v2` or `sigmoid`.
      trained_betas (`np.ndarray`, optional):
          option to pass an array of betas directly to the constructor to bypass `beta_start`, `beta_end` etc.
      variance_type (`str`):
          options to clip the variance used when adding noise to the denoised sample. Choose from `fixed_small`,
          `fixed_small_log`, `fixed_large`, `fixed_large_log`, `learned` or `learned_range`.
      clip_sample (`bool`, default `True`):
          option to clip predicted sample for numerical stability.
      clip_sample_range (`float`, default `1.0`):
          the maximum magnitude for sample clipping. Valid only when `clip_sample=True`.
      prediction_type (`str`, default `epsilon`, optional):
          prediction type of the scheduler function, one of `epsilon` (predicting the noise of the diffusion
          process), `sample` (directly predicting the noisy sample`) or `v_prediction` (see section 2.4
          https://imagen.research.google/video/paper.pdf)
      thresholding (`bool`, default `False`):
          whether to use the "dynamic thresholding" method (introduced by Imagen, https://arxiv.org/abs/2205.11487).
          Note that the thresholding method is unsuitable for latent-space diffusion models (such as
          stable-diffusion).
      dynamic_thresholding_ratio (`float`, default `0.995`):
          the ratio for the dynamic thresholding method. Default is `0.995`, the same as Imagen
          (https://arxiv.org/abs/2205.11487). Valid only when `thresholding=True`.
      sample_max_value (`float`, default `1.0`):
          the threshold value for dynamic thresholding. Valid only when `thresholding=True`.
      timestep_spacing (`str`, default `"leading"`):
          The way the timesteps should be scaled. Refer to Table 2. of [Common Diffusion Noise Schedules and Sample
          Steps are Flawed](https://arxiv.org/abs/2305.08891) for more information.
      steps_offset (`int`, default `0`):
          an offset added to the inference steps. You can use a combination of `offset=1` and
          `set_alpha_to_one=False`, to make the last step use step 0 for the previous alpha product, as done in
          stable diffusion.
class_docstr_tok: ["Denoising","diffusion","probabilistic","models","(","DDPMs",")","explores","the","connections","between","denoising","score","matching","and","Langevin","dynamics","sampling",".","[","`","~ConfigMixin","`","]","takes","care","of","storing","all","config","attributes","that","are","passed","in","the","scheduler","'s","`","__init__","`","function",",","such","as","`","num_train_timesteps","`",".","They","can","be","accessed","via","`","scheduler.config.num_train_timesteps","`",".","[","`","SchedulerMixin","`","]","provides","general","loading","and","saving","functionality","via","the","[","`","SchedulerMixin.save_pretrained","`","]","and","[","`","~SchedulerMixin.from_pretrained","`","]","functions",".","For","more","details",",","see","the","original","paper",":","https",":","\/\/arxiv.org\/abs\/2006.11239","Args",":","num_train_timesteps","(","`","int","`",")",":","number","of","diffusion","steps","used","to","train","the","model",".","beta_start","(","`","float","`",")",":","the","starting","`","beta","`","value","of","inference",".","beta_end","(","`","float","`",")",":","the","final","`","beta","`","value",".","beta_schedule","(","`","str","`",")",":","the","beta","schedule",",","a","mapping","from","a","beta","range","to","a","sequence","of","betas","for","stepping","the","model",".","Choose","from","`","linear","`",",","`","scaled_linear","`",",","`","squaredcos_cap_v2","`","or","`","sigmoid","`",".","trained_betas","(","`","np.ndarray","`",",","optional",")",":","option","to","pass","an","array","of","betas","directly","to","the","constructor","to","bypass","`","beta_start","`",",","`","beta_end","`","etc",".","variance_type","(","`","str","`",")",":","options","to","clip","the","variance","used","when","adding","noise","to","the","denoised","sample",".","Choose","from","`","fixed_small","`",",","`","fixed_small_log","`",",","`","fixed_large","`",",","`","fixed_large_log","`",",","`","learned","`","or","`","learned_range","`",".","clip_sample","(","`","bool","`",",","default","`","True","`",")",":","option","to","clip","predicted","sample","for","numerical","stability",".","clip_sample_range","(","`","float","`",",","default","`","1.0","`",")",":","the","maximum","magnitude","for","sample","clipping",".","Valid","only","when","`","clip_sample=True","`",".","prediction_type","(","`","str","`",",","default","`","epsilon","`",",","optional",")",":","prediction","type","of","the","scheduler","function",",","one","of","`","epsilon","`","(","predicting","the","noise","of","the","diffusion","process",")",",","`","sample","`","(","directly","predicting","the","noisy","sample","`",")","or","`","v_prediction","`","(","see","section","2.4","https",":","\/\/imagen.research.google\/video\/paper.pdf",")","thresholding","(","`","bool","`",",","default","`","False","`",")",":","whether","to","use","the","``","dynamic","thresholding","''","method","(","introduced","by","Imagen",",","https",":","\/\/arxiv.org\/abs\/2205.11487",")",".","Note","that","the","thresholding","method","is","unsuitable","for","latent-space","diffusion","models","(","such","as","stable-diffusion",")",".","dynamic_thresholding_ratio","(","`","float","`",",","default","`","0.995","`",")",":","the","ratio","for","the","dynamic","thresholding","method",".","Default","is","`","0.995","`",",","the","same","as","Imagen","(","https",":","\/\/arxiv.org\/abs\/2205.11487",")",".","Valid","only","when","`","thresholding=True","`",".","sample_max_value","(","`","float","`",",","default","`","1.0","`",")",":","the","threshold","value","for","dynamic","thresholding",".","Valid","only","when","`","thresholding=True","`",".","timestep_spacing","(","`","str","`",",","default","`","``","leading","''","`",")",":","The","way","the","timesteps","should","be","scaled",".","Refer","to","Table","2.","of","[","Common","Diffusion","Noise","Schedules","and","Sample","Steps","are","Flawed","]","(","https",":","\/\/arxiv.org\/abs\/2305.08891",")","for","more","information",".","steps_offset","(","`","int","`",",","default","`","0","`",")",":","an","offset","added","to","the","inference","steps",".","You","can","use","a","combination","of","`","offset=1","`","and","`","set_alpha_to_one=False","`",",","to","make","the","last","step","use","step","0","for","the","previous","alpha","product",",","as","done","in","stable","diffusion","."]
docstr: Sets the discrete timesteps used for the diffusion chain (to be run before inference).
  
  Args:
      num_inference_steps (`int`):
          The number of diffusion steps used when generating samples with a pre-trained model. If used,
          `timesteps` must be `None`.
      device (`str` or `torch.device`, *optional*):
          The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
      timesteps (`List[int]`, *optional*):
          Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default
          timestep spacing strategy of equal spacing between timesteps is used. If `timesteps` is passed,
          `num_inference_steps` must be `None`.
docstr_tok: ["Sets","the","discrete","timesteps","used","for","the","diffusion","chain","(","to","be","run","before","inference",")",".","Args",":","num_inference_steps","(","`","int","`",")",":","The","number","of","diffusion","steps","used","when","generating","samples","with","a","pre-trained","model",".","If","used",",","`","timesteps","`","must","be","`","None","`",".","device","(","`","str","`","or","`","torch.device","`",",","*","optional","*",")",":","The","device","to","which","the","timesteps","should","be","moved","to",".","If","`","None","`",",","the","timesteps","are","not","moved",".","timesteps","(","`","List","[","int","]","`",",","*","optional","*",")",":","Custom","timesteps","used","to","support","arbitrary","spacing","between","timesteps",".","If","`","None","`",",","then","the","default","timestep","spacing","strategy","of","equal","spacing","between","timesteps","is","used",".","If","`","timesteps","`","is","passed",",","`","num_inference_steps","`","must","be","`","None","`","."]
code: def set_timesteps(
      self,
      num_inference_steps: Optional[int] = None,
      device: Union[str, torch.device] = None,
      timesteps: Optional[List[int]] = None,
  ):
      """
      Sets the discrete timesteps used for the diffusion chain (to be run before inference).
  
      Args:
          num_inference_steps (`int`):
              The number of diffusion steps used when generating samples with a pre-trained model. If used,
              `timesteps` must be `None`.
          device (`str` or `torch.device`, *optional*):
              The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
          timesteps (`List[int]`, *optional*):
              Custom timesteps used to support arbitrary spacing between timesteps. If `None`, then the default
              timestep spacing strategy of equal spacing between timesteps is used. If `timesteps` is passed,
              `num_inference_steps` must be `None`.
  
      """
      if num_inference_steps is not None and timesteps is not None:
          raise ValueError(
              "Can only pass one of `num_inference_steps` or `custom_timesteps`."
          )
  
      if timesteps is not None:
          for i in range(1, len(timesteps)):
              if timesteps[i] >= timesteps[i - 1]:
                  raise ValueError(
                      "`custom_timesteps` must be in descending order."
                  )
  
          if timesteps[0] >= self.config.num_train_timesteps:
              raise ValueError(
                  f"`timesteps` must start before `self.config.train_timesteps`:"
                  f" {self.config.num_train_timesteps}."
              )
  
          timesteps = np.array(timesteps, dtype=np.int64)
          self.custom_timesteps = True
      else:
          if num_inference_steps > self.config.num_train_timesteps:
              raise ValueError(
                  f"`num_inference_steps`: {num_inference_steps} cannot be larger than `self.config.train_timesteps`:"
                  f" {self.config.num_train_timesteps} as the unet model trained with this scheduler can only handle"
                  f" maximal {self.config.num_train_timesteps} timesteps."
              )
  
          self.num_inference_steps = num_inference_steps
          self.custom_timesteps = False
  
          # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
          if self.config.timestep_spacing == "linspace":
              timesteps = (
                  np.linspace(
                      0,
                      self.config.num_train_timesteps - 1,
                      num_inference_steps,
                  )
                  .round()[::-1]
                  .copy()
                  .astype(np.int64)
              )
          elif self.config.timestep_spacing == "leading":
              step_ratio = (
                  self.config.num_train_timesteps
                  // self.num_inference_steps
              )
              # creates integer timesteps by multiplying by ratio
              # casting to int to avoid issues when num_inference_step is power of 3
              timesteps = (
                  (np.arange(0, num_inference_steps) * step_ratio)
                  .round()[::-1]
                  .copy()
                  .astype(np.int64)
              )
              timesteps += self.config.steps_offset
          elif self.config.timestep_spacing == "trailing":
              step_ratio = (
                  self.config.num_train_timesteps
                  / self.num_inference_steps
              )
              # creates integer timesteps by multiplying by ratio
              # casting to int to avoid issues when num_inference_step is power of 3
              timesteps = np.round(
                  np.arange(
                      self.config.num_train_timesteps, 0, -step_ratio
                  )
              ).astype(np.int64)
              timesteps -= 1
          else:
              raise ValueError(
                  f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."
              )
  
      self.timesteps = torch.from_numpy(timesteps).to(device)
  
imports: import math
  from dataclasses import dataclass
  from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  from ..configuration_utils import ConfigMixin, register_to_config
  from ..utils import BaseOutput
  from ..utils.torch_utils import randn_tensor
  from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin
label_desc: Use image node_id 3 for calling the DDPMParallelScheduler obj's underlying member method code with example usage: obj.set_timesteps(num_inference_steps, device, timesteps) without return types