

'''simple docstring'''




import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


_a				: Tuple 							=						logging.get_logger(__name__)  # pylint: disable=invalid-name



@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerDiscrete
class        lowercase_	(		lowerCAmelCase_ ):

  '''simple docstring'''





  __lowerCAmelCase					: List[Any]      =   42
  __lowerCAmelCase					: List[str]      =   None




def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Any       ,   SCREAMING_SNAKE_CASE      :   Tuple=0.999       ,   SCREAMING_SNAKE_CASE      :   Optional[int]="cosine"       ,   ):
  if alpha_transform_type == "cosine":

    def alpha_bar_fn(SCREAMING_SNAKE_CASE      :   List[str]						):
      return math.cos((t + 0.008) / 1.008 * math.pi / 2						) ** 2

  elif alpha_transform_type == "exp":

    def alpha_bar_fn(SCREAMING_SNAKE_CASE      :   Optional[Any]						):
      return math.exp(t * -12.0						)

  else:
    raise ValueError(f'''Unsupported alpha_tranform_type: {alpha_transform_type}'''						)

  UpperCAmelCase													=	[]
  for i in range(_SCREAMING_SNAKE_CASE						):
    UpperCAmelCase													=	i / num_diffusion_timesteps
    UpperCAmelCase													=	(i + 1) / num_diffusion_timesteps
    betas.append(min(1 - alpha_bar_fn(_SCREAMING_SNAKE_CASE						) / alpha_bar_fn(_SCREAMING_SNAKE_CASE						)       ,   _SCREAMING_SNAKE_CASE						)						)
  return torch.tensor(_SCREAMING_SNAKE_CASE       ,   dtype=torch.floataa						)



class        lowercase_	(		lowerCAmelCase_ ,       lowerCAmelCase_ ):

  '''simple docstring'''





  __lowerCAmelCase					: Optional[int]      =   [e.name for e in KarrasDiffusionSchedulers]
  __lowerCAmelCase					: int      =   1
  @register_to_config
  def __init__(     self   , a_ = 1_0_0_0   , a_ = 0.0001   , a_ = 0.02   , a_ = "linear"   , a_ = None   , a_ = "epsilon"   , a_ = "linear"   , a_ = False   , a_ = "linspace"   , a_ = 0   , )							->				Optional[Any]:


    """simple docstring"""





    if trained_betas is not None:
      UpperCAmelCase													=	torch.tensor(__a   , dtype=torch.floataa     )
    elif beta_schedule == "linear":
      UpperCAmelCase													=	torch.linspace(__a   , __a   , __a   , dtype=torch.floataa     )
    elif beta_schedule == "scaled_linear":
      # this schedule is very specific to the latent diffusion model.
      UpperCAmelCase													=	(
          torch.linspace(beta_start**0.5   , beta_end**0.5   , __a   , dtype=torch.floataa     ) ** 2
      )
    elif beta_schedule == "squaredcos_cap_v2":
      # Glide cosine schedule
      UpperCAmelCase													=	betas_for_alpha_bar(__a     )
    else:
      raise NotImplementedError(F'''{beta_schedule} does is not implemented for {self.__class__}'''     )

    UpperCAmelCase													=	1.0 - self.betas
    UpperCAmelCase													=	torch.cumprod(self.alphas   , dim=0     )

    UpperCAmelCase													=	np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5     )
    UpperCAmelCase													=	np.concatenate([sigmas[::-1], [0.0]]     ).astype(np.floataa     )
    UpperCAmelCase													=	torch.from_numpy(__a     )

    # setable values
    UpperCAmelCase													=	None
    UpperCAmelCase													=	np.linspace(0   , num_train_timesteps - 1   , __a   , dtype=__a     )[::-1].copy()
    UpperCAmelCase													=	torch.from_numpy(__a     )
    UpperCAmelCase													=	False
    UpperCAmelCase													=	use_karras_sigmas
  @property
  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""





    if self.config.timestep_spacing in ["linspace", "trailing"]:
      return self.sigmas.max()

    return (self.sigmas.max() ** 2 + 1) ** 0.5
  def 						snake_case_			(     self   , a_   , a_     )							->				List[str]:


    """simple docstring"""





    if isinstance(__a   , torch.Tensor     ):
      UpperCAmelCase													=	timestep.to(self.timesteps.device     )
    UpperCAmelCase													=	(self.timesteps == timestep).nonzero().item()
    UpperCAmelCase													=	self.sigmas[step_index]

    UpperCAmelCase													=	sample / ((sigma**2 + 1) ** 0.5)

    UpperCAmelCase													=	True
    return sample
  def 						snake_case_			(     self   , a_   , a_ = None     )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	num_inference_steps

    # "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
    if self.config.timestep_spacing == "linspace":
      UpperCAmelCase													=	np.linspace(0   , self.config.num_train_timesteps - 1   , __a   , dtype=__a     )[
          ::-1
      ].copy()
    elif self.config.timestep_spacing == "leading":
      UpperCAmelCase													=	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
      UpperCAmelCase													=	(np.arange(0   , __a     ) * step_ratio).round()[::-1].copy().astype(__a     )
      timesteps += self.config.steps_offset
    elif self.config.timestep_spacing == "trailing":
      UpperCAmelCase													=	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
      UpperCAmelCase													=	(np.arange(self.config.num_train_timesteps   , 0   , -step_ratio     )).round().copy().astype(__a     )
      timesteps -= 1
    else:
      raise ValueError(
          F'''{self.config.timestep_spacing} is not supported. Please make sure to choose one of \'linspace\', \'leading\' or \'trailing\'.'''     )

    UpperCAmelCase													=	np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5     )
    UpperCAmelCase													=	np.log(__a     )

    if self.config.interpolation_type == "linear":
      UpperCAmelCase													=	np.interp(__a   , np.arange(0   , len(__a     )     )   , __a     )
    elif self.config.interpolation_type == "log_linear":
      UpperCAmelCase													=	torch.linspace(np.log(sigmas[-1]     )   , np.log(sigmas[0]     )   , num_inference_steps + 1     ).exp()
    else:
      raise ValueError(
          F'''{self.config.interpolation_type} is not implemented. Please specify interpolation_type to either'''
          ' \'linear\' or \'log_linear\''     )

    if self.use_karras_sigmas:
      UpperCAmelCase													=	self._convert_to_karras(in_sigmas=__a   , num_inference_steps=self.num_inference_steps     )
      UpperCAmelCase													=	np.array([self._sigma_to_t(__a   , __a     ) for sigma in sigmas]     )

    UpperCAmelCase													=	np.concatenate([sigmas, [0.0]]     ).astype(np.floataa     )
    UpperCAmelCase													=	torch.from_numpy(__a     ).to(device=__a     )
    if str(__a     ).startswith('mps'     ):
      # mps does not support float64
      UpperCAmelCase													=	torch.from_numpy(__a     ).to(__a   , dtype=torch.floataa     )
    else:
      UpperCAmelCase													=	torch.from_numpy(__a     ).to(device=__a     )
  def 						snake_case_			(     self   , a_   , a_     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	np.log(__a     )

    # get distribution
    UpperCAmelCase													=	log_sigma - log_sigmas[:, np.newaxis]

    # get sigmas range
    UpperCAmelCase													=	np.cumsum((dists >= 0)   , axis=0     ).argmax(axis=0     ).clip(max=log_sigmas.shape[0] - 2     )
    UpperCAmelCase													=	low_idx + 1

    UpperCAmelCase													=	log_sigmas[low_idx]
    UpperCAmelCase													=	log_sigmas[high_idx]

    # interpolate sigmas
    UpperCAmelCase													=	(low - log_sigma) / (low - high)
    UpperCAmelCase													=	np.clip(__a   , 0   , 1     )

    # transform interpolation to time range
    UpperCAmelCase													=	(1 - w) * low_idx + w * high_idx
    UpperCAmelCase													=	t.reshape(sigma.shape     )
    return t
  def 						snake_case_			(     self   , a_   , a_     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	in_sigmas[-1].item()
    UpperCAmelCase													=	in_sigmas[0].item()

    UpperCAmelCase													=	7.0  # 7.0 is the value used in the paper
    UpperCAmelCase													=	np.linspace(0   , 1   , __a     )
    UpperCAmelCase													=	sigma_min ** (1 / rho)
    UpperCAmelCase													=	sigma_max ** (1 / rho)
    UpperCAmelCase													=	(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
    return sigmas
  def 						snake_case_			(     self   , a_   , a_   , a_   , a_ = 0.0   , a_ = 0.0   , a_ = float('inf'     )   , a_ = 1.0   , a_ = None   , a_ = True   , )							->				Tuple:


    """simple docstring"""





    if (
        isinstance(__a   , __a     )
        or isinstance(__a   , torch.IntTensor     )
        or isinstance(__a   , torch.LongTensor     )
    ):
      raise ValueError(
          (
              'Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to'
              ' `EulerDiscreteScheduler.step()` is not supported. Make sure to pass'
              ' one of the `scheduler.timesteps` as a timestep.'
          )   , )

    if not self.is_scale_input_called:
      logger.warning(
          'The `scale_model_input` function should be called before `step` to ensure correct denoising. '
          'See `StableDiffusionPipeline` for a usage example.'     )

    if isinstance(__a   , torch.Tensor     ):
      UpperCAmelCase													=	timestep.to(self.timesteps.device     )

    UpperCAmelCase													=	(self.timesteps == timestep).nonzero().item()
    UpperCAmelCase													=	self.sigmas[step_index]

    UpperCAmelCase													=	min(s_churn / (len(self.sigmas     ) - 1)   , 2**0.5 - 1     ) if s_tmin <= sigma <= s_tmax else 0.0

    UpperCAmelCase													=	randn_tensor(
        model_output.shape   , dtype=model_output.dtype   , device=model_output.device   , generator=__a     )

    UpperCAmelCase													=	noise * s_noise
    UpperCAmelCase													=	sigma * (gamma + 1)

    if gamma > 0:
      UpperCAmelCase													=	sample + eps * (sigma_hat**2 - sigma**2) ** 0.5

    # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
    # NOTE: "original_sample" should not be an expected prediction_type but is left in for
    # backwards compatibility
    if self.config.prediction_type == "original_sample" or self.config.prediction_type == "sample":
      UpperCAmelCase													=	model_output
    elif self.config.prediction_type == "epsilon":
      UpperCAmelCase													=	sample - sigma_hat * model_output
    elif self.config.prediction_type == "v_prediction":
      # * c_out + input * c_skip
      UpperCAmelCase													=	model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
    else:
      raise ValueError(
          F'''prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`'''     )

    # 2. Convert to an ODE derivative
    UpperCAmelCase													=	(sample - pred_original_sample) / sigma_hat

    UpperCAmelCase													=	self.sigmas[step_index + 1] - sigma_hat

    UpperCAmelCase													=	sample + derivative * dt

    if not return_dict:
      return (prev_sample,)

    return EulerDiscreteSchedulerOutput(prev_sample=__a   , pred_original_sample=__a     )
  def 						snake_case_			(     self   , a_   , a_   , a_   , )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.sigmas.to(device=original_samples.device   , dtype=original_samples.dtype     )
    if original_samples.device.type == "mps" and torch.is_floating_point(__a     ):
      # mps does not support float64
      UpperCAmelCase													=	self.timesteps.to(original_samples.device   , dtype=torch.floataa     )
      UpperCAmelCase													=	timesteps.to(original_samples.device   , dtype=torch.floataa     )
    else:
      UpperCAmelCase													=	self.timesteps.to(original_samples.device     )
      UpperCAmelCase													=	timesteps.to(original_samples.device     )

    UpperCAmelCase													=	[(schedule_timesteps == t).nonzero().item() for t in timesteps]

    UpperCAmelCase													=	sigmas[step_indices].flatten()
    while len(sigma.shape     ) < len(original_samples.shape     ):
      UpperCAmelCase													=	sigma.unsqueeze(-1     )

    UpperCAmelCase													=	original_samples + noise * sigma
    return noisy_samples





  def __len__(     self     )							->				Any:


    """simple docstring"""
    return self.config.num_train_timesteps





'''simple docstring'''




from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
  import torch

_a				: List[Any] 							=						logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
  from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):


  def       lowerCamelCase__		(						cls      :   Dict						):
    return cls


def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Union[Tuple, List]       ,   SCREAMING_SNAKE_CASE      :   Optional[Union[List["torch.Generator"], "torch.Generator"]] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.device"] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.dtype"] = None       ,   SCREAMING_SNAKE_CASE      :   Optional["torch.layout"] = None       ,   ):
  UpperCAmelCase													=	device
  UpperCAmelCase													=	shape[0]

  UpperCAmelCase													=	layout or torch.strided
  UpperCAmelCase													=	device or torch.device('cpu'						)

  if generator is not None:
    UpperCAmelCase													=	generator.device.type if not isinstance(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) else generator[0].device.type
    if gen_device_type != device.type and gen_device_type == "cpu":
      UpperCAmelCase													=	'cpu'
      if device != "mps":
        logger.info(
            f'''The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'''
            f''' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'''
            f''' slighly speed up this function by passing a generator that was created on the {device} device.'''						)
    elif gen_device_type != device.type and gen_device_type == "cuda":
      raise ValueError(f'''Cannot generate a {device} tensor from a generator of type {gen_device_type}.'''						)

  if isinstance(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						):
    UpperCAmelCase													=	(1,) + shape[1:]
    UpperCAmelCase													=	[
        torch.randn(SCREAMING_SNAKE_CASE       ,   generator=generator[i]       ,   device=SCREAMING_SNAKE_CASE       ,   dtype=SCREAMING_SNAKE_CASE       ,   layout=SCREAMING_SNAKE_CASE						)
        for i in range(SCREAMING_SNAKE_CASE						)
    ]
    UpperCAmelCase													=	torch.cat(SCREAMING_SNAKE_CASE       ,   dim=0						).to(SCREAMING_SNAKE_CASE						)
  else:
    UpperCAmelCase													=	torch.randn(SCREAMING_SNAKE_CASE       ,   generator=SCREAMING_SNAKE_CASE       ,   device=SCREAMING_SNAKE_CASE       ,   dtype=SCREAMING_SNAKE_CASE       ,   layout=SCREAMING_SNAKE_CASE						).to(SCREAMING_SNAKE_CASE						)

  return latents



def       lowerCamelCase__		(						SCREAMING_SNAKE_CASE      :   Tuple						):
  if is_torch_version('<'       ,   '2.0.0'						) or not hasattr(SCREAMING_SNAKE_CASE       ,   '_dynamo'						):
    return False
  return isinstance(SCREAMING_SNAKE_CASE       ,   torch._dynamo.eval_frame.OptimizedModule						)


