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: "Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
  prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
  s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
  pixels from saturation at each step. We find that dynamic thresholding results in significantly better
  photorealism as well as better image-text alignment, especially when using very large guidance weights."
  
  https://arxiv.org/abs/2205.11487
docstr_tok: ["``","Dynamic","thresholding",":","At","each","sampling","step","we","set","s","to","a","certain","percentile","absolute","pixel","value","in","xt0","(","the","prediction","of","x_0","at","timestep","t",")",",","and","if","s",">","1",",","then","we","threshold","xt0","to","the","range","[","-s",",","s","]","and","then","divide","by","s.","Dynamic","thresholding","pushes","saturated","pixels","(","those","near","-1","and","1",")","inwards",",","thereby","actively","preventing","pixels","from","saturation","at","each","step",".","We","find","that","dynamic","thresholding","results","in","significantly","better","photorealism","as","well","as","better","image-text","alignment",",","especially","when","using","very","large","guidance","weights",".","''","https",":","\/\/arxiv.org\/abs\/2205.11487"]
code: def _threshold_sample(
      self, sample: torch.FloatTensor
  ) -> torch.FloatTensor:
      """
      "Dynamic thresholding: At each sampling step we set s to a certain percentile absolute pixel value in xt0 (the
      prediction of x_0 at timestep t), and if s > 1, then we threshold xt0 to the range [-s, s] and then divide by
      s. Dynamic thresholding pushes saturated pixels (those near -1 and 1) inwards, thereby actively preventing
      pixels from saturation at each step. We find that dynamic thresholding results in significantly better
      photorealism as well as better image-text alignment, especially when using very large guidance weights."
  
      https://arxiv.org/abs/2205.11487
      """
      dtype = sample.dtype
      batch_size, channels, *remaining_dims = sample.shape
  
      if dtype not in (torch.float32, torch.float64):
          sample = (
              sample.float()
          )  # upcast for quantile calculation, and clamp not implemented for cpu half
  
      # Flatten sample for doing quantile calculation along each image
      sample = sample.reshape(
          batch_size, channels * np.prod(remaining_dims)
      )
  
      abs_sample = (
          sample.abs()
      )  # "a certain percentile absolute pixel value"
  
      s = torch.quantile(
          abs_sample, self.config.dynamic_thresholding_ratio, dim=1
      )
      s = torch.clamp(
          s, min=1, max=self.config.sample_max_value
      )  # When clamped to min=1, equivalent to standard clipping to [-1, 1]
      s = s.unsqueeze(
          1
      )  # (batch_size, 1) because clamp will broadcast along dim=0
      sample = (
          torch.clamp(sample, -s, s) / s
      )  # "we threshold xt0 to the range [-s, s] and then divide by s"
  
      sample = sample.reshape(batch_size, channels, *remaining_dims)
      sample = sample.to(dtype)
  
      return sample
  
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 5 for calling the DDPMParallelScheduler obj's underlying member method code with example usage: obj._threshold_sample(sample) and returns: sample