code: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import math
  import warnings
  from dataclasses import dataclass
  from typing import List, Optional, Tuple, Union
  
  import numpy as np
  import torch
  from scipy import integrate
  
  from ..configuration_utils import ConfigMixin, register_to_config
  from ..utils import BaseOutput
  from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin
  
  
  
  @dataclass
  # Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->LMSDiscrete
  class 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    42
  		__lowerCAmelCase 				=    None
  
  
  
  
  
  
  def    __a (     UpperCAmelCase       ,			UpperCAmelCase=0.999       ,			UpperCAmelCase="cosine"       ,			)      ->List[str]:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	if alpha_transform_type == "cosine":
  
  		def alpha_bar_fn(UpperCAmelCase    ):
  			return math.cos((t + 0.008) / 1.008 * math.pi / 2    ) ** 2
  
  	elif alpha_transform_type == "exp":
  
  		def alpha_bar_fn(UpperCAmelCase    ):
  			return math.exp(t * -12.0    )
  
  	else:
  		raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}"""    )
  
  	A							  =      []
  	for i in range(UpperCAmelCase    ):
  		A							  =      i / num_diffusion_timesteps
  		A							  =      (i + 1) / num_diffusion_timesteps
  		betas.append(min(1 - alpha_bar_fn(UpperCAmelCase    ) / alpha_bar_fn(UpperCAmelCase    )       ,			UpperCAmelCase    )    )
  	return torch.tensor(UpperCAmelCase       ,			dtype=torch.floataa    )
  
  
  
  class 							__UpperCAmelCase  (       A__		,					A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    [e.name for e in KarrasDiffusionSchedulers]
  		__lowerCAmelCase 				=    1
  
  
  
  
  
  
  
  		@register_to_config
  		def __init__(self						:      List[Any]      ,      _lowerCAmelCase						:      int = 1000      ,      _lowerCAmelCase						:      float = 0.0_001      ,      _lowerCAmelCase						:      float = 0.02      ,      _lowerCAmelCase						:      str = "linear"      ,      _lowerCAmelCase						:      Optional[Union[np.ndarray, List[float]]] = None      ,      _lowerCAmelCase						:      Optional[bool] = False      ,      _lowerCAmelCase						:      str = "epsilon"      ,      _lowerCAmelCase						:      str = "linspace"      ,      _lowerCAmelCase						:      int = 0      ,      ):
  			if trained_betas is not None:
  				A							  =      torch.tensor(_lowerCAmelCase      ,      dtype=torch.floataa      )
  			elif beta_schedule == "linear":
  				A							  =      torch.linspace(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ,      dtype=torch.floataa      )
  			elif beta_schedule == "scaled_linear":
  				# this schedule is very specific to the latent diffusion model.
  				A							  =      (
  				    torch.linspace(beta_start**0.5      ,      beta_end**0.5      ,      _lowerCAmelCase      ,      dtype=torch.floataa      ) ** 2
  				)
  			elif beta_schedule == "squaredcos_cap_v2":
  				# Glide cosine schedule
  				A							  =      betas_for_alpha_bar(_lowerCAmelCase      )
  			else:
  				raise NotImplementedError(F"""{beta_schedule} does is not implemented for {self.__class__}"""      )
  
  			A							  =      1.0 - self.betas
  			A							  =      torch.cumprod(self.alphas      ,      dim=0      )
  
  			A							  =      np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5      )
  			A							  =      np.concatenate([sigmas[::-1], [0.0]]      ).astype(np.floataa      )
  			A							  =      torch.from_numpy(_lowerCAmelCase      )
  
  			# setable values
  			A							  =      None
  			A							  =      use_karras_sigmas
  			self.set_timesteps(_lowerCAmelCase      ,      _lowerCAmelCase      )
  			A							  =      []
  			A							  =      False
  
  
  
  
  
  
  
  		@property
  		def        A     (self						:      Tuple      ):
  			# standard deviation of the initial noise distribution
  			if self.config.timestep_spacing in ["linspace", "trailing"]:
  				return self.sigmas.max()
  
  			return (self.sigmas.max() ** 2 + 1) ** 0.5
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      _lowerCAmelCase						:      Union[float, torch.FloatTensor]      ):
  			if isinstance(_lowerCAmelCase      ,      torch.Tensor      ):
  				A							  =      timestep.to(self.timesteps.device      )
  			A							  =      (self.timesteps == timestep).nonzero().item()
  			A							  =      self.sigmas[step_index]
  			A							  =      sample / ((sigma**2 + 1) ** 0.5)
  			A							  =      True
  			return sample
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      List[Any]      ):
  
  			def lms_derivative(_lowerCAmelCase						:      Optional[Any]      ):
  				A							  =      1.0
  				for k in range(_lowerCAmelCase      ):
  					if current_order == k:
  						continue
  					prod *= (tau - self.sigmas[t - k]) / (self.sigmas[t - current_order] - self.sigmas[t - k])
  				return prod
  
  			A							  =      integrate.quad(_lowerCAmelCase      ,      self.sigmas[t]      ,      self.sigmas[t + 1]      ,      epsrel=1e-4      )[0]
  
  			return integrated_coeff
  
  
  
  
  
  
  
  		def        A     (self						:      int      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      Union[str, torch.device] = None      ):
  			A							  =      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":
  				A							  =      np.linspace(0      ,      self.config.num_train_timesteps - 1      ,      _lowerCAmelCase      ,      dtype=_lowerCAmelCase      )[
  				    ::-1
  				].copy()
  			elif self.config.timestep_spacing == "leading":
  				A							  =      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
  				A							  =      (np.arange(0      ,      _lowerCAmelCase      ) * step_ratio).round()[::-1].copy().astype(_lowerCAmelCase      )
  				timesteps += self.config.steps_offset
  			elif self.config.timestep_spacing == "trailing":
  				A							  =      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
  				A							  =      (np.arange(self.config.num_train_timesteps      ,      0      ,      -step_ratio      )).round().copy().astype(_lowerCAmelCase      )
  				timesteps -= 1
  			else:
  				raise ValueError(
  				    F"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""      )
  
  			A							  =      np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5      )
  			A							  =      np.log(_lowerCAmelCase      )
  			A							  =      np.interp(_lowerCAmelCase      ,      np.arange(0      ,      len(_lowerCAmelCase      )      )      ,      _lowerCAmelCase      )
  
  			if self.use_karras_sigmas:
  				A							  =      self._convert_to_karras(in_sigmas=_lowerCAmelCase      )
  				A							  =      np.array([self._sigma_to_t(_lowerCAmelCase      ,      _lowerCAmelCase      ) for sigma in sigmas]      )
  
  			A							  =      np.concatenate([sigmas, [0.0]]      ).astype(np.floataa      )
  
  			A							  =      torch.from_numpy(_lowerCAmelCase      ).to(device=_lowerCAmelCase      )
  			if str(_lowerCAmelCase      ).startswith("""mps"""      ):
  				# mps does not support float64
  				A							  =      torch.from_numpy(_lowerCAmelCase      ).to(_lowerCAmelCase      ,      dtype=torch.floataa      )
  			else:
  				A							  =      torch.from_numpy(_lowerCAmelCase      ).to(device=_lowerCAmelCase      )
  
  			A							  =      []
  
  
  
  
  
  
  
  		def        A     (self						:      List[str]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      Union[str, Any]      ):
  			# get log sigma
  			A							  =      np.log(_lowerCAmelCase      )
  
  			# get distribution
  			A							  =      log_sigma - log_sigmas[:, np.newaxis]
  
  			# get sigmas range
  			A							  =      np.cumsum((dists >= 0)      ,      axis=0      ).argmax(axis=0      ).clip(max=log_sigmas.shape[0] - 2      )
  			A							  =      low_idx + 1
  
  			A							  =      log_sigmas[low_idx]
  			A							  =      log_sigmas[high_idx]
  
  			# interpolate sigmas
  			A							  =      (low - log_sigma) / (low - high)
  			A							  =      np.clip(_lowerCAmelCase      ,      0      ,      1      )
  
  			# transform interpolation to time range
  			A							  =      (1 - w) * low_idx + w * high_idx
  			A							  =      t.reshape(sigma.shape      )
  			return t
  
  
  
  
  
  
  
  		def        A     (self						:      Union[str, Any]      ,      _lowerCAmelCase						:      torch.FloatTensor      ):
  			A							  =      in_sigmas[-1].item()
  			A							  =      in_sigmas[0].item()
  
  			A							  =      7.0  # 7.0 is the value used in the paper
  			A							  =      np.linspace(0      ,      1      ,      self.num_inference_steps      )
  			A							  =      sigma_min ** (1 / rho)
  			A							  =      sigma_max ** (1 / rho)
  			A							  =      (max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
  			return sigmas
  
  
  
  
  
  
  
  		def        A     (self						:      Tuple      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      _lowerCAmelCase						:      Union[float, torch.FloatTensor]      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      _lowerCAmelCase						:      int = 4      ,      _lowerCAmelCase						:      bool = True      ,      ):
  			if not self.is_scale_input_called:
  				warnings.warn(
  				    """The `scale_model_input` function should be called before `step` to ensure correct denoising. """
  				    """See `StableDiffusionPipeline` for a usage example."""      )
  
  			if isinstance(_lowerCAmelCase      ,      torch.Tensor      ):
  				A							  =      timestep.to(self.timesteps.device      )
  			A							  =      (self.timesteps == timestep).nonzero().item()
  			A							  =      self.sigmas[step_index]
  
  			# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
  			if self.config.prediction_type == "epsilon":
  				A							  =      sample - sigma * model_output
  			elif self.config.prediction_type == "v_prediction":
  				# * c_out + input * c_skip
  				A							  =      model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
  			elif self.config.prediction_type == "sample":
  				A							  =      model_output
  			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
  			A							  =      (sample - pred_original_sample) / sigma
  			self.derivatives.append(_lowerCAmelCase      )
  			if len(self.derivatives      ) > order:
  				self.derivatives.pop(0      )
  
  			# 3. Compute linear multistep coefficients
  			A							  =      min(step_index + 1      ,      _lowerCAmelCase      )
  			A							  =      [self.get_lms_coefficient(_lowerCAmelCase      ,      _lowerCAmelCase      ,      _lowerCAmelCase      ) for curr_order in range(_lowerCAmelCase      )]
  
  			# 4. Compute previous sample based on the derivatives path
  			A							  =      sample + sum(
  			    coeff * derivative for coeff, derivative in zip(_lowerCAmelCase      ,      reversed(self.derivatives      )      )      )
  
  			if not return_dict:
  				return (prev_sample,)
  
  			return LMSDiscreteSchedulerOutput(prev_sample=_lowerCAmelCase      ,      pred_original_sample=_lowerCAmelCase      )
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      _lowerCAmelCase						:      torch.FloatTensor      ,      ):
  			# Make sure sigmas and timesteps have the same device and dtype as original_samples
  			A							  =      self.sigmas.to(device=original_samples.device      ,      dtype=original_samples.dtype      )
  			if original_samples.device.type == "mps" and torch.is_floating_point(_lowerCAmelCase      ):
  				# mps does not support float64
  				A							  =      self.timesteps.to(original_samples.device      ,      dtype=torch.floataa      )
  				A							  =      timesteps.to(original_samples.device      ,      dtype=torch.floataa      )
  			else:
  				A							  =      self.timesteps.to(original_samples.device      )
  				A							  =      timesteps.to(original_samples.device      )
  
  			A							  =      [(schedule_timesteps == t).nonzero().item() for t in timesteps]
  
  			A							  =      sigmas[step_indices].flatten()
  			while len(sigma.shape      ) < len(original_samples.shape      ):
  				A							  =      sigma.unsqueeze(-1      )
  
  			A							  =      original_samples + noise * sigma
  			return noisy_samples
  
  
  
  
  
  
  
  		def __len__(self						:      Optional[Any]      ):
  			return self.config.num_train_timesteps
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import tempfile
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DiffusionPipeline
  from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
  			A							  =      10
  
  			if only_cross_attention:
  				A							  =      12
  			else:
  				# when only cross attention is not set, the cross attention dim must be the same as the query dim
  				A							  =      query_dim
  
  			return {
  			    "query_dim": query_dim,
  			    "cross_attention_dim": cross_attention_dim,
  			    "heads": 2,
  			    "dim_head": 4,
  			    "added_kv_proj_dim": 6,
  			    "norm_num_groups": 1,
  			    "only_cross_attention": only_cross_attention,
  			    "processor": AttnAddedKVProcessor(),
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      2
  
  			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
  			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
  			A							  =      None
  
  			return {
  			    "hidden_states": hidden_states,
  			    "encoder_hidden_states": encoder_hidden_states,
  			    "attention_mask": attention_mask,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# self and cross attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is not None      )
  			self.assertTrue(attn.to_v is not None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			# only self attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is None      )
  			self.assertTrue(attn.to_v is None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			# the initial conversion succeeds
  			A							  =      DiffusionPipeline.from_pretrained(
  			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			with tempfile.TemporaryDirectory() as tmpdir:
  				# save the converted model
  				pipe.save_pretrained(_lowerCAmelCase      )
  
  				# can also load the converted weights
  				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  
  