





"""simple docstring"""






from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin



@dataclass
class 						lowerCAmelCase       (   __snake_case				):
       lowerCAmelCase__    			=	42



class 						lowerCAmelCase       (   __snake_case      ,						__snake_case				):






       @register_to_config
       def __init__( self     ,	a__ = 16     ,	a__ = 88     ,	a__ = None     ,	a__ = None     ,	a__ = 1     ,	a__ = 0.0     ,	a__ = 32     ,	a__ = None     ,	a__ = False     ,	a__ = None     ,	a__ = None     ,	a__ = None     ,	a__ = "geglu"     ,	a__ = None     ,	a__ = False     ,	a__ = False     ,	a__ = False     ,	a__ = "layer_norm"     ,	a__ = True     ,	):
              super().__init__()
              _UpperCAmelCase			     =  use_linear_projection
              _UpperCAmelCase			     =  num_attention_heads
              _UpperCAmelCase			     =  attention_head_dim
              _UpperCAmelCase			     =  num_attention_heads * attention_head_dim

              # 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
              # Define whether input is continuous or discrete depending on configuration
              _UpperCAmelCase			     =  (in_channels is not None) and (patch_size is None)
              _UpperCAmelCase			     =  num_vector_embeds is not None
              _UpperCAmelCase			     =  in_channels is not None and patch_size is not None

              if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
                     _UpperCAmelCase			     =  (
                         f"""The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or"""
                         ' incorrectly set to `\'layer_norm\'`.Make sure to set `norm_type` to `\'ada_norm\'` in the config.'
                         ' Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect'
                         ' results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it'
                         ' would be very nice if you could open a Pull request for the `transformer/config.json` file'
                     )
                     deprecate('norm_type!=num_embeds_ada_norm'     ,	'1.0.0'     ,	a__     ,	standard_warn=a__  )
                     _UpperCAmelCase			     =  'ada_norm'

              if self.is_input_continuous and self.is_input_vectorized:
                     raise ValueError(
                         f"""Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make"""
                         ' sure that either `in_channels` or `num_vector_embeds` is None.'  )
              elif self.is_input_vectorized and self.is_input_patches:
                     raise ValueError(
                         f"""Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make"""
                         ' sure that either `num_vector_embeds` or `num_patches` is None.'  )
              elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
                     raise ValueError(
                         f"""Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:"""
                         f""" {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None."""  )

              # 2. Define input layers
              if self.is_input_continuous:
                     _UpperCAmelCase			     =  in_channels

                     _UpperCAmelCase			     =  torch.nn.GroupNorm(num_groups=a__     ,	num_channels=a__     ,	eps=1E-6     ,	affine=a__  )
                     if use_linear_projection:
                            _UpperCAmelCase			     =  nn.Linear(a__     ,	a__  )
                     else:
                            _UpperCAmelCase			     =  nn.Convad(a__     ,	a__     ,	kernel_size=1     ,	stride=1     ,	padding=0  )
              elif self.is_input_vectorized:
                     assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
                     assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

                     _UpperCAmelCase			     =  sample_size
                     _UpperCAmelCase			     =  sample_size
                     _UpperCAmelCase			     =  num_vector_embeds
                     _UpperCAmelCase			     =  self.height * self.width

                     _UpperCAmelCase			     =  ImagePositionalEmbeddings(
                         num_embed=a__     ,	embed_dim=a__     ,	height=self.height     ,	width=self.width  )
              elif self.is_input_patches:
                     assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

                     _UpperCAmelCase			     =  sample_size
                     _UpperCAmelCase			     =  sample_size

                     _UpperCAmelCase			     =  patch_size
                     _UpperCAmelCase			     =  PatchEmbed(
                         height=a__     ,	width=a__     ,	patch_size=a__     ,	in_channels=a__     ,	embed_dim=a__     ,	)

              # 3. Define transformers blocks
              _UpperCAmelCase			     =  nn.ModuleList(
                  [
                      BasicTransformerBlock(
                          a__     ,	a__     ,	a__     ,	dropout=a__     ,	cross_attention_dim=a__     ,	activation_fn=a__     ,	num_embeds_ada_norm=a__     ,	attention_bias=a__     ,	only_cross_attention=a__     ,	upcast_attention=a__     ,	norm_type=a__     ,	norm_elementwise_affine=a__     ,	)
                      for d in range(a__  )
                  ]  )

              # 4. Define output layers
              _UpperCAmelCase			     =  in_channels if out_channels is None else out_channels
              if self.is_input_continuous:
                     # TODO: should use out_channels for continuous projections
                     if use_linear_projection:
                            _UpperCAmelCase			     =  nn.Linear(a__     ,	a__  )
                     else:
                            _UpperCAmelCase			     =  nn.Convad(a__     ,	a__     ,	kernel_size=1     ,	stride=1     ,	padding=0  )
              elif self.is_input_vectorized:
                     _UpperCAmelCase			     =  nn.LayerNorm(a__  )
                     _UpperCAmelCase			     =  nn.Linear(a__     ,	self.num_vector_embeds - 1  )
              elif self.is_input_patches:
                     _UpperCAmelCase			     =  nn.LayerNorm(a__     ,	elementwise_affine=a__     ,	eps=1E-6  )
                     _UpperCAmelCase			     =  nn.Linear(a__     ,	2 * inner_dim  )
                     _UpperCAmelCase			     =  nn.Linear(a__     ,	patch_size * patch_size * self.out_channels  )






       def 				__A			( self     ,	a__     ,	a__ = None     ,	a__ = None     ,	a__ = None     ,	a__ = None     ,	a__ = None     ,	a__ = None     ,	a__ = True     ,	):
              # ensure attention_mask is a bias, and give it a singleton query_tokens dimension.
              #   we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward.
              #   we can tell by counting dims; if ndim == 2: it's a mask rather than a bias.
              # expects mask of shape:
              #   [batch, key_tokens]
              # adds singleton query_tokens dimension:
              #   [batch,                    1, key_tokens]
              # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
              #   [batch,  heads, query_tokens, key_tokens] (e.g. torch sdp attn)
              #   [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
              if attention_mask is not None and attention_mask.ndim == 2:
                     # assume that mask is expressed as:
                     #   (1 = keep,      0 = discard)
                     # convert mask into a bias that can be added to attention scores:
                     #       (keep = +0,     discard = -10000.0)
                     _UpperCAmelCase			     =  (1 - attention_mask.to(hidden_states.dtype  )) * -1_00_00.0
                     _UpperCAmelCase			     =  attention_mask.unsqueeze(1  )

              # convert encoder_attention_mask to a bias the same way we do for attention_mask
              if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
                     _UpperCAmelCase			     =  (1 - encoder_attention_mask.to(hidden_states.dtype  )) * -1_00_00.0
                     _UpperCAmelCase			     =  encoder_attention_mask.unsqueeze(1  )

              # 1. Input
              if self.is_input_continuous:
                     _UpperCAmelCase     ,				_UpperCAmelCase     ,				_UpperCAmelCase     ,				_UpperCAmelCase			     =  hidden_states.shape
                     _UpperCAmelCase			     =  hidden_states

                     _UpperCAmelCase			     =  self.norm(a__  )
                     if not self.use_linear_projection:
                            _UpperCAmelCase			     =  self.proj_in(a__  )
                            _UpperCAmelCase			     =  hidden_states.shape[1]
                            _UpperCAmelCase			     =  hidden_states.permute(0     ,	2     ,	3     ,	1  ).reshape(a__     ,	height * width     ,	a__  )
                     else:
                            _UpperCAmelCase			     =  hidden_states.shape[1]
                            _UpperCAmelCase			     =  hidden_states.permute(0     ,	2     ,	3     ,	1  ).reshape(a__     ,	height * width     ,	a__  )
                            _UpperCAmelCase			     =  self.proj_in(a__  )
              elif self.is_input_vectorized:
                     _UpperCAmelCase			     =  self.latent_image_embedding(a__  )
              elif self.is_input_patches:
                     _UpperCAmelCase			     =  self.pos_embed(a__  )

              # 2. Blocks
              for block in self.transformer_blocks:
                     _UpperCAmelCase			     =  block(
                         a__     ,	attention_mask=a__     ,	encoder_hidden_states=a__     ,	encoder_attention_mask=a__     ,	timestep=a__     ,	cross_attention_kwargs=a__     ,	class_labels=a__     ,	)

              # 3. Output
              if self.is_input_continuous:
                     if not self.use_linear_projection:
                            _UpperCAmelCase			     =  hidden_states.reshape(a__     ,	a__     ,	a__     ,	a__  ).permute(0     ,	3     ,	1     ,	2  ).contiguous()
                            _UpperCAmelCase			     =  self.proj_out(a__  )
                     else:
                            _UpperCAmelCase			     =  self.proj_out(a__  )
                            _UpperCAmelCase			     =  hidden_states.reshape(a__     ,	a__     ,	a__     ,	a__  ).permute(0     ,	3     ,	1     ,	2  ).contiguous()

                     _UpperCAmelCase			     =  hidden_states + residual
              elif self.is_input_vectorized:
                     _UpperCAmelCase			     =  self.norm_out(a__  )
                     _UpperCAmelCase			     =  self.out(a__  )
                     # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
                     _UpperCAmelCase			     =  logits.permute(0     ,	2     ,	1  )

                     # log(p(x_0))
                     _UpperCAmelCase			     =  F.log_softmax(logits.double()     ,	dim=1  ).float()
              elif self.is_input_patches:
                     # TODO: cleanup!
                     _UpperCAmelCase			     =  self.transformer_blocks[0].norma.emb(
                         a__     ,	a__     ,	hidden_dtype=hidden_states.dtype  )
                     _UpperCAmelCase     ,				_UpperCAmelCase			     =  self.proj_out_a(F.silu(a__  )  ).chunk(2     ,	dim=1  )
                     _UpperCAmelCase			     =  self.norm_out(a__  ) * (1 + scale[:, None]) + shift[:, None]
                     _UpperCAmelCase			     =  self.proj_out_a(a__  )

                     # unpatchify
                     _UpperCAmelCase			     =  _UpperCAmelCase			     =  int(hidden_states.shape[1] ** 0.5  )
                     _UpperCAmelCase			     =  hidden_states.reshape(
                         shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)  )
                     _UpperCAmelCase			     =  torch.einsum('nhwpqc->nchpwq'     ,	a__  )
                     _UpperCAmelCase			     =  hidden_states.reshape(
                         shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)  )

              if not return_dict:
                     return (output,)

              return TransformeraDModelOutput(sample=a__  )









"""simple docstring"""






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 SchedulerMixin


__lowerCAmelCase				      =   logging.get_logger(__name__)  # pylint: disable=invalid-name
@dataclass
class 	_lowerCAmelCase    (      __snake_case       ):
							__lowerCAmelCase					:    torch.FloatTensor




class 	_lowerCAmelCase    (      __snake_case       ,       __snake_case       ):
							__lowerCAmelCase					:    int     =  1

							@register_to_config
							def __init__( self       :    Union[str, Any] ,		a       :    int = 40 ,		a       :    float = 0.002 ,		a       :    float = 80.0 ,		a       :    float = 0.5 ,		a       :    float = 1.0 ,		a       :    float = 7.0 ,		a       :    bool = True ,		)   ->    List[Any]:

									"""simple docstring"""
									# standard deviation of the initial noise distribution
									lowercase										=							sigma_max

									lowercase										=							np.linspace(0 ,		1 ,		a    )
									lowercase										=							self._convert_to_karras(a    )
									lowercase										=							self.sigma_to_t(a    )

									# setable values
									lowercase										=							None
									lowercase										=							torch.from_numpy(a    )
									lowercase										=							torch.from_numpy(a    )
									lowercase										=							False
									lowercase										=							False

							def 					_lowerCAmelCase  ( self       :    Tuple ,		a       :    Optional[int] ,		a       :    str=None    )   ->    Any:

									"""simple docstring"""
									if schedule_timesteps is None:
											lowercase										=							self.timesteps

									lowercase										=							(schedule_timesteps == timestep).nonzero()
									return indices.item()

							def 					_lowerCAmelCase  ( self       :    Tuple ,		a       :    torch.FloatTensor ,		a       :    Union[float, torch.FloatTensor]    )   ->    torch.FloatTensor:

									"""simple docstring"""
									# Get sigma corresponding to timestep
									if isinstance(a ,		torch.Tensor    ):
											lowercase										=							timestep.to(self.timesteps.device    )
									lowercase										=							self.index_for_timestep(a    )
									lowercase										=							self.sigmas[step_idx]

									lowercase										=							sample / ((sigma**2 + self.config.sigma_data**2) ** 0.5)

									lowercase										=							True
									return sample

							def 					_lowerCAmelCase  ( self       :    Optional[Any] ,		a       :    Union[float, np.ndarray]    )   ->    str:

									"""simple docstring"""
									if not isinstance(a ,		np.ndarray    ):
											lowercase										=							np.array(a ,		dtype=np.floataa    )

									lowercase										=							1000 * 0.25 * np.log(sigmas + 1E-44    )

									return timesteps

							def 					_lowerCAmelCase  ( self       :    Optional[Any] ,		a       :    Optional[int] = None ,		a       :    Union[str, torch.device] = None ,		a       :    Optional[List[int]] = None ,		)   ->    Dict:

									"""simple docstring"""
									if num_inference_steps is None and timesteps is None:
											raise ValueError('''Exactly one of `num_inference_steps` or `timesteps` must be supplied.'''    )

									if num_inference_steps is not None and timesteps is not None:
											raise ValueError('''Can only pass one of `num_inference_steps` or `timesteps`.'''    )

									# Follow DDPMScheduler custom timesteps logic
									if timesteps is not None:
											for i in range(1 ,		len(a    )    ):
													if timesteps[i] >= timesteps[i - 1]:
															raise ValueError('''`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}."""    )

											lowercase										=							np.array(a ,		dtype=np.intaa    )
											lowercase										=							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."""    )

											lowercase										=							num_inference_steps

											lowercase										=							self.config.num_train_timesteps // self.num_inference_steps
											lowercase										=							(np.arange(0 ,		a    ) * step_ratio).round()[::-1].copy().astype(np.intaa    )
											lowercase										=							False

									# Map timesteps to Karras sigmas directly for multistep sampling
									# See https://github.com/openai/consistency_models/blob/main/cm/karras_diffusion.py#L675
									lowercase										=							self.config.num_train_timesteps
									lowercase										=							timesteps[::-1].copy()
									lowercase										=							ramp / (num_train_timesteps - 1)
									lowercase										=							self._convert_to_karras(a    )
									lowercase										=							self.sigma_to_t(a    )

									lowercase										=							np.concatenate([sigmas, [self.sigma_min]]    ).astype(np.floataa    )
									lowercase										=							torch.from_numpy(a    ).to(device=a    )

									if str(a    ).startswith('''mps'''    ):
											# mps does not support float64
											lowercase										=							torch.from_numpy(a    ).to(a ,		dtype=torch.floataa    )
									else:
											lowercase										=							torch.from_numpy(a    ).to(device=a    )

							def 					_lowerCAmelCase  ( self       :    Tuple ,		a       :    int    )   ->    List[Any]:

									"""simple docstring"""
									lowercase										=							self.config.sigma_min
									lowercase										=							self.config.sigma_max

									lowercase										=							self.config.rho
									lowercase										=							sigma_min ** (1 / rho)
									lowercase										=							sigma_max ** (1 / rho)
									lowercase										=							(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
									return sigmas

							def 					_lowerCAmelCase  ( self       :    Any ,		a       :    str    )   ->    int:

									"""simple docstring"""
									lowercase										=							self.config.sigma_data

									lowercase										=							sigma_data**2 / (sigma**2 + sigma_data**2)
									lowercase										=							sigma * sigma_data / (sigma**2 + sigma_data**2) ** 0.5
									return c_skip, c_out

							def 					_lowerCAmelCase  ( self       :    Optional[Any] ,		a       :    Any    )   ->    Optional[int]:

									"""simple docstring"""
									lowercase										=							self.config.sigma_min
									lowercase										=							self.config.sigma_data

									lowercase										=							sigma_data**2 / ((sigma - sigma_min) ** 2 + sigma_data**2)
									lowercase										=							(sigma - sigma_min) * sigma_data / (sigma**2 + sigma_data**2) ** 0.5
									return c_skip, c_out

							def 					_lowerCAmelCase  ( self       :    Dict ,		a       :    torch.FloatTensor ,		a       :    Union[float, torch.FloatTensor] ,		a       :    torch.FloatTensor ,		a       :    Optional[torch.Generator] = None ,		a       :    bool = True ,		)   ->    Union[CMStochasticIterativeSchedulerOutput, 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'''
											        f""" `{self.__class__}.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    ):
											lowercase										=							timestep.to(self.timesteps.device    )

									lowercase										=							self.config.sigma_min
									lowercase										=							self.config.sigma_max

									lowercase										=							self.index_for_timestep(a    )

									# sigma_next corresponds to next_t in original implementation
									lowercase										=							self.sigmas[step_index]
									if step_index + 1 < self.config.num_train_timesteps:
											lowercase										=							self.sigmas[step_index + 1]
									else:
											# Set sigma_next to sigma_min
											lowercase										=							self.sigmas[-1]

									# Get scalings for boundary conditions
									lowercase					,  lowercase										=							self.get_scalings_for_boundary_condition(a    )

									# 1. Denoise model output using boundary conditions
									lowercase										=							c_out * model_output + c_skip * sample
									if self.config.clip_denoised:
											lowercase										=							denoised.clamp(-1 ,		1    )

									# 2. Sample z ~ N(0, s_noise^2 * I)
									# Noise is not used for onestep sampling.
									if len(self.timesteps    ) > 1:
											lowercase										=							randn_tensor(
											    model_output.shape ,		dtype=model_output.dtype ,		device=model_output.device ,		generator=a    )
									else:
											lowercase										=							torch.zeros_like(a    )
									lowercase										=							noise * self.config.s_noise

									lowercase										=							sigma_next.clamp(min=a ,		max=a    )

									# 3. Return noisy sample
									# tau = sigma_hat, eps = sigma_min
									lowercase										=							denoised + z * (sigma_hat**2 - sigma_min**2) ** 0.5

									if not return_dict:
											return (prev_sample,)

									return CMStochasticIterativeSchedulerOutput(prev_sample=a    )

							def 					_lowerCAmelCase  ( self       :    Any ,		a       :    torch.FloatTensor ,		a       :    torch.FloatTensor ,		a       :    torch.FloatTensor ,		)   ->    torch.FloatTensor:

									"""simple docstring"""
									# Make sure sigmas and timesteps have the same device and dtype as original_samples
									lowercase										=							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
											lowercase										=							self.timesteps.to(original_samples.device ,		dtype=torch.floataa    )
											lowercase										=							timesteps.to(original_samples.device ,		dtype=torch.floataa    )
									else:
											lowercase										=							self.timesteps.to(original_samples.device    )
											lowercase										=							timesteps.to(original_samples.device    )

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

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

									lowercase										=							original_samples + noise * sigma
									return noisy_samples

							def __len__( self       :    Union[str, Any]    )   ->    Any:

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