class_docstr: DPMSolverSDEScheduler implements the stochastic sampler from the [Elucidating the Design Space of Diffusion-Based
  Generative Models](https://huggingface.co/papers/2206.00364) paper.
  
  This model inherits from [`SchedulerMixin`] and [`ConfigMixin`]. Check the superclass documentation for the generic
  methods the library implements for all schedulers such as loading and saving.
  
  Args:
      num_train_timesteps (`int`, defaults to 1000):
          The number of diffusion steps to train the model.
      beta_start (`float`, defaults to 0.00085):
          The starting `beta` value of inference.
      beta_end (`float`, defaults to 0.012):
          The final `beta` value.
      beta_schedule (`str`, defaults to `"linear"`):
          The beta schedule, a mapping from a beta range to a sequence of betas for stepping the model. Choose from
          `linear` or `scaled_linear`.
      trained_betas (`np.ndarray`, *optional*):
          Pass an array of betas directly to the constructor to bypass `beta_start` and `beta_end`.
      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://imagen.research.google/video/paper.pdf) paper).
      use_karras_sigmas (`bool`, *optional*, defaults to `False`):
          Whether to use Karras sigmas for step sizes in the noise schedule during the sampling process. If `True`,
          the sigmas are determined according to a sequence of noise levels {σi}.
      noise_sampler_seed (`int`, *optional*, defaults to `None`):
          The random seed to use for the noise sampler. If `None`, a random seed is generated.
      timestep_spacing (`str`, defaults to `"linspace"`):
          The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and
          Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.
      steps_offset (`int`, defaults to 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 like in Stable
          Diffusion.
class_docstr_tok: ["DPMSolverSDEScheduler","implements","the","stochastic","sampler","from","the","[","Elucidating","the","Design","Space","of","Diffusion-Based","Generative","Models","]","(","https",":","\/\/huggingface.co\/papers\/2206.00364",")","paper",".","This","model","inherits","from","[","`","SchedulerMixin","`","]","and","[","`","ConfigMixin","`","]",".","Check","the","superclass","documentation","for","the","generic","methods","the","library","implements","for","all","schedulers","such","as","loading","and","saving",".","Args",":","num_train_timesteps","(","`","int","`",",","defaults","to","1000",")",":","The","number","of","diffusion","steps","to","train","the","model",".","beta_start","(","`","float","`",",","defaults","to","0.00085",")",":","The","starting","`","beta","`","value","of","inference",".","beta_end","(","`","float","`",",","defaults","to","0.012",")",":","The","final","`","beta","`","value",".","beta_schedule","(","`","str","`",",","defaults","to","`","``","linear","''","`",")",":","The","beta","schedule",",","a","mapping","from","a","beta","range","to","a","sequence","of","betas","for","stepping","the","model",".","Choose","from","`","linear","`","or","`","scaled_linear","`",".","trained_betas","(","`","np.ndarray","`",",","*","optional","*",")",":","Pass","an","array","of","betas","directly","to","the","constructor","to","bypass","`","beta_start","`","and","`","beta_end","`",".","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",":","\/\/imagen.research.google\/video\/paper.pdf",")","paper",")",".","use_karras_sigmas","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","to","use","Karras","sigmas","for","step","sizes","in","the","noise","schedule","during","the","sampling","process",".","If","`","True","`",",","the","sigmas","are","determined","according","to","a","sequence","of","noise","levels","{","\u03c3i","}",".","noise_sampler_seed","(","`","int","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","The","random","seed","to","use","for","the","noise","sampler",".","If","`","None","`",",","a","random","seed","is","generated",".","timestep_spacing","(","`","str","`",",","defaults","to","`","``","linspace","''","`",")",":","The","way","the","timesteps","should","be","scaled",".","Refer","to","Table","2","of","the","[","Common","Diffusion","Noise","Schedules","and","Sample","Steps","are","Flawed","]","(","https",":","\/\/huggingface.co\/papers\/2305.08891",")","for","more","information",".","steps_offset","(","`","int","`",",","defaults","to","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","like","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.
      device (`str` or `torch.device`, *optional*):
          The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
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",".","device","(","`","str","`","or","`","torch.device","`",",","*","optional","*",")",":","The","device","to","which","the","timesteps","should","be","moved","to",".","If","`","None","`",",","the","timesteps","are","not","moved","."]
code: def set_timesteps(
      self,
      num_inference_steps: int,
      device: Union[str, torch.device] = None,
      num_train_timesteps: Optional[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.
          device (`str` or `torch.device`, *optional*):
              The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
      """
      self.num_inference_steps = num_inference_steps
  
      num_train_timesteps = (
          num_train_timesteps or self.config.num_train_timesteps
      )
  
      # "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,
              num_train_timesteps - 1,
              num_inference_steps,
              dtype=float,
          )[::-1].copy()
      elif self.config.timestep_spacing == "leading":
          step_ratio = 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(float)
          )
          timesteps += self.config.steps_offset
      elif self.config.timestep_spacing == "trailing":
          step_ratio = 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(num_train_timesteps, 0, -step_ratio))
              .round()
              .copy()
              .astype(float)
          )
          timesteps -= 1
      else:
          raise ValueError(
              f"{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."
          )
  
      sigmas = np.array(
          ((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5
      )
      log_sigmas = np.log(sigmas)
      sigmas = np.interp(timesteps, np.arange(0, len(sigmas)), sigmas)
  
      if self.use_karras_sigmas:
          sigmas = self._convert_to_karras(in_sigmas=sigmas)
          timesteps = np.array(
              [self._sigma_to_t(sigma, log_sigmas) for sigma in sigmas]
          )
  
      second_order_timesteps = self._second_order_timesteps(
          sigmas, log_sigmas
      )
  
      sigmas = np.concatenate([sigmas, [0.0]]).astype(np.float32)
      sigmas = torch.from_numpy(sigmas).to(device=device)
      self.sigmas = torch.cat(
          [sigmas[:1], sigmas[1:-1].repeat_interleave(2), sigmas[-1:]]
      )
  
      timesteps = torch.from_numpy(timesteps)
      second_order_timesteps = torch.from_numpy(second_order_timesteps)
      timesteps = torch.cat(
          [timesteps[:1], timesteps[1:].repeat_interleave(2)]
      )
      timesteps[1::2] = second_order_timesteps
  
      if str(device).startswith("mps"):
          # mps does not support float64
          self.timesteps = timesteps.to(device, dtype=torch.float32)
      else:
          self.timesteps = timesteps.to(device=device)
  
      # empty first order variables
      self.sample = None
      self.mid_point_sigma = None
  
      self._step_index = None
      self.noise_sampler = None
  
      # for exp beta schedules, such as the one for `pipeline_shap_e.py`
      # we need an index counter
      self._index_counter = defaultdict(int)
  
imports: import math
  from collections import defaultdict
  from typing import List, Optional, Tuple, Union
  import numpy
  import torch
  import torchsde
  from ..configuration_utils import ConfigMixin, register_to_config
  from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin, SchedulerOutput
label_desc: Use image node_id 7 for calling the DPMSolverSDEScheduler obj's underlying member method code with example usage: obj.set_timesteps(num_inference_steps, device, num_train_timesteps) without return types