code: 
  from logging import getLogger
  from typing import Any, Callable, List, Optional, Union
  
  import numpy as np
  import PIL
  import torch
  
  from ...schedulers import DDPMScheduler
  from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
  from ..pipeline_utils import ImagePipelineOutput
  from . import StableDiffusionUpscalePipeline
  
  
  __lowerCAmelCase				      =					getLogger(__name__)
  
  
  __lowerCAmelCase				      =					4
  __lowerCAmelCase				      =					7
  
  __lowerCAmelCase				      =					{
      'float16': torch.floataa,
      'float32': torch.floataa,
  }
  
  
  def       a			(   a  )    ->Tuple:
  
  
  
  
  			'''simple docstring'''
  
  
  			if isinstance(a		,	torch.Tensor  ):
  						return image
  			elif isinstance(a		,	PIL.Image.Image  ):
  						SCREAMING_SNAKE_CASE    	=   [image]
  
  			if isinstance(image[0]		,	PIL.Image.Image  ):
  						SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   image[0].size
  						SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   (x - x % 64 for x in (w, h))  # resize to integer multiple of 32
  
  						SCREAMING_SNAKE_CASE    	=   [np.array(i.resize((w, h)  )  )[None, :] for i in image]
  						SCREAMING_SNAKE_CASE    	=   np.concatenate(a		,	axis=0  )
  						SCREAMING_SNAKE_CASE    	=   np.array(a  ).astype(np.floataa  ) / 255.0
  						SCREAMING_SNAKE_CASE    	=   image.transpose(0		,	3		,	1		,	2  )
  						SCREAMING_SNAKE_CASE    	=   2.0 * image - 1.0
  						SCREAMING_SNAKE_CASE    	=   torch.from_numpy(a  )
  			elif isinstance(image[0]		,	torch.Tensor  ):
  						SCREAMING_SNAKE_CASE    	=   torch.cat(a		,	dim=0  )
  
  			return image
  
  
  
  
  
  class 						lowerCamelCase			(					lowerCAmelCase__				):
  
  
  
  
  
  
  
  	def __init__(    self			:int      ,    lowercase			:OnnxRuntimeModel      ,    lowercase			:OnnxRuntimeModel      ,    lowercase			:Any      ,    lowercase			:OnnxRuntimeModel      ,    lowercase			:DDPMScheduler      ,    lowercase			:Any      ,    lowercase			:int = 3_5_0      ,    )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__(
  				    vae=lowerCAmelCase_      ,    text_encoder=lowerCAmelCase_      ,    tokenizer=lowerCAmelCase_      ,    unet=lowerCAmelCase_      ,    low_res_scheduler=lowerCAmelCase_      ,    scheduler=lowerCAmelCase_      ,    safety_checker=lowerCAmelCase_      ,    feature_extractor=lowerCAmelCase_      ,    watermarker=lowerCAmelCase_      ,    max_noise_level=lowerCAmelCase_      ,    )
  
  
  
  
  
  
  
  	def __call__(    self			:List[str]      ,    lowercase			:Union[str, List[str]]      ,    lowercase			:Union[torch.FloatTensor, PIL.Image.Image, List[PIL.Image.Image]]      ,    lowercase			:int = 7_5      ,    lowercase			:float = 9.0      ,    lowercase			:int = 2_0      ,    lowercase			:Optional[Union[str, List[str]]] = None      ,    lowercase			:Optional[int] = 1      ,    lowercase			:float = 0.0      ,    lowercase			:Optional[Union[torch.Generator, List[torch.Generator]]] = None      ,    lowercase			:Optional[torch.FloatTensor] = None      ,    lowercase			:Optional[np.ndarray] = None      ,    lowercase			:Optional[np.ndarray] = None      ,    lowercase			:Optional[str] = "pil"      ,    lowercase			:bool = True      ,    lowercase			:Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,    lowercase			:Optional[int] = 1      ,    )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				self.check_inputs(lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_ )
  
  				# 2. Define call parameters
  				if prompt is not None and isinstance(lowerCAmelCase_      ,    lowerCAmelCase_ ):
  							SCREAMING_SNAKE_CASE    	=   1
  				elif prompt is not None and isinstance(lowerCAmelCase_      ,    lowerCAmelCase_ ):
  							SCREAMING_SNAKE_CASE    	=   len(lowerCAmelCase_ )
  				else:
  							SCREAMING_SNAKE_CASE    	=   prompt_embeds.shape[0]
  
  				SCREAMING_SNAKE_CASE    	=   self._execution_device
  				# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
  				# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
  				# corresponds to doing no classifier free guidance.
  				SCREAMING_SNAKE_CASE    	=   guidance_scale > 1.0
  
  				# 3. Encode input prompt
  				SCREAMING_SNAKE_CASE    	=   self._encode_prompt(
  				    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    prompt_embeds=lowerCAmelCase_      ,    negative_prompt_embeds=lowerCAmelCase_      ,    )
  
  				SCREAMING_SNAKE_CASE    	=   ORT_TO_PT_TYPE[str(text_embeddings.dtype )]
  
  				# 4. Preprocess image
  				SCREAMING_SNAKE_CASE    	=   preprocess(lowerCAmelCase_ )
  				SCREAMING_SNAKE_CASE    	=   image.cpu()
  
  				# 5. set timesteps
  				self.scheduler.set_timesteps(lowerCAmelCase_      ,    device=lowerCAmelCase_ )
  				SCREAMING_SNAKE_CASE    	=   self.scheduler.timesteps
  
  				# 5. Add noise to image
  				SCREAMING_SNAKE_CASE    	=   torch.tensor([noise_level]      ,    dtype=torch.long      ,    device=lowerCAmelCase_ )
  				SCREAMING_SNAKE_CASE    	=   torch.randn(image.shape      ,    generator=lowerCAmelCase_      ,    device=lowerCAmelCase_      ,    dtype=lowerCAmelCase_ )
  				SCREAMING_SNAKE_CASE    	=   self.low_res_scheduler.add_noise(lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_ )
  
  				SCREAMING_SNAKE_CASE    	=   2 if do_classifier_free_guidance else 1
  				SCREAMING_SNAKE_CASE    	=   np.concatenate([image] * batch_multiplier * num_images_per_prompt )
  				SCREAMING_SNAKE_CASE    	=   np.concatenate([noise_level] * image.shape[0] )
  
  				# 6. Prepare latent variables
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   image.shape[2:]
  				SCREAMING_SNAKE_CASE    	=   self.prepare_latents(
  				    batch_size * num_images_per_prompt      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    )
  
  				# 7. Check that sizes of image and latents match
  				SCREAMING_SNAKE_CASE    	=   image.shape[1]
  				if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
  							raise ValueError(
  							    '''Incorrect configuration settings! The config of `pipeline.unet` expects'''
  							    f""" {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +"""
  							    f""" `num_channels_image`: {num_channels_image} """
  							    f""" = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of"""
  							    ''' `pipeline.unet` or your `image` input.''' )
  
  				# 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
  				SCREAMING_SNAKE_CASE    	=   self.prepare_extra_step_kwargs(lowerCAmelCase_      ,    lowerCAmelCase_ )
  
  				SCREAMING_SNAKE_CASE    	=   next(
  				    (input.type for input in self.unet.model.get_inputs() if input.name == '''timestep''')      ,    '''tensor(float)''' )
  				SCREAMING_SNAKE_CASE    	=   ORT_TO_NP_TYPE[timestep_dtype]
  
  				# 9. Denoising loop
  				SCREAMING_SNAKE_CASE    	=   len(lowerCAmelCase_ ) - num_inference_steps * self.scheduler.order
  				with self.progress_bar(total=lowerCAmelCase_ ) as progress_bar:
  							for i, t in enumerate(lowerCAmelCase_ ):
  										# expand the latents if we are doing classifier free guidance
  										SCREAMING_SNAKE_CASE    	=   np.concatenate([latents] * 2 ) if do_classifier_free_guidance else latents
  
  										# concat latents, mask, masked_image_latents in the channel dimension
  										SCREAMING_SNAKE_CASE    	=   self.scheduler.scale_model_input(lowerCAmelCase_      ,    lowerCAmelCase_ )
  										SCREAMING_SNAKE_CASE    	=   np.concatenate([latent_model_input, image]      ,    axis=1 )
  
  										# timestep to tensor
  										SCREAMING_SNAKE_CASE    	=   np.array([t]      ,    dtype=lowerCAmelCase_ )
  
  										# predict the noise residual
  										SCREAMING_SNAKE_CASE    	=   self.unet(
  										    sample=lowerCAmelCase_      ,    timestep=lowerCAmelCase_      ,    encoder_hidden_states=lowerCAmelCase_      ,    class_labels=noise_level.astype(np.intaa )      ,    )[0]
  
  										# perform guidance
  										if do_classifier_free_guidance:
  													SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   np.split(lowerCAmelCase_      ,    2 )
  										SCREAMING_SNAKE_CASE    	=   noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
  
  										# compute the previous noisy sample x_t -> x_t-1
  										SCREAMING_SNAKE_CASE    	=   self.scheduler.step(
  										    torch.from_numpy(lowerCAmelCase_ )      ,    lowerCAmelCase_      ,    lowerCAmelCase_      ,    **lowerCAmelCase_ ).prev_sample
  
  										# call the callback, if provided
  										if i == len(lowerCAmelCase_ ) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
  													progress_bar.update()
  													if callback is not None and i % callback_steps == 0:
  																callback(lowerCAmelCase_      ,    lowerCAmelCase_      ,    lowerCAmelCase_ )
  
          # 10. Post-processing
  				SCREAMING_SNAKE_CASE    	=   self.decode_latents(latents.float() )
  
  				# 11. Convert to PIL
  				if output_type == "pil":
  							SCREAMING_SNAKE_CASE    	=   self.numpy_to_pil(lowerCAmelCase_ )
  
  				if not return_dict:
  							return (image,)
  
  				return ImagePipelineOutput(images=lowerCAmelCase_ )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict      ,    lowercase			:Optional[int] )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   1 / 0.0_83_33 * latents
  				SCREAMING_SNAKE_CASE    	=   self.vae(latent_sample=lowerCAmelCase_ )[0]
  				SCREAMING_SNAKE_CASE    	=   np.clip(image / 2 + 0.5      ,    0      ,    1 )
  				SCREAMING_SNAKE_CASE    	=   image.transpose((0, 2, 3, 1) )
  				return image
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Dict      ,    lowercase			:Union[str, List[str]]      ,    lowercase			:int      ,    lowercase			:Optional[int]      ,    lowercase			:bool      ,    lowercase			:Optional[str]      ,    lowercase			:Optional[torch.FloatTensor] = None      ,    lowercase			:Optional[torch.FloatTensor] = None      ,    )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if prompt is not None and isinstance(lowerCAmelCase_      ,    lowerCAmelCase_ ):
  							SCREAMING_SNAKE_CASE    	=   1
  				elif prompt is not None and isinstance(lowerCAmelCase_      ,    lowerCAmelCase_ ):
  							SCREAMING_SNAKE_CASE    	=   len(lowerCAmelCase_ )
  				else:
  							SCREAMING_SNAKE_CASE    	=   prompt_embeds.shape[0]
  
  				if prompt_embeds is None:
  							SCREAMING_SNAKE_CASE    	=   self.tokenizer(
  							    lowerCAmelCase_      ,    padding='''max_length'''      ,    max_length=self.tokenizer.model_max_length      ,    truncation=lowerCAmelCase_      ,    return_tensors='''pt'''      ,    )
  							SCREAMING_SNAKE_CASE    	=   text_inputs.input_ids
  							SCREAMING_SNAKE_CASE    	=   self.tokenizer(lowerCAmelCase_      ,    padding='''longest'''      ,    return_tensors='''pt''' ).input_ids
  
  							if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
  							    lowerCAmelCase_      ,    lowerCAmelCase_ ):
  										SCREAMING_SNAKE_CASE    	=   self.tokenizer.batch_decode(
  										    untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1] )
  										logger.warning(
  										    '''The following part of your input was truncated because CLIP can only handle sequences up to'''
  										    f""" {self.tokenizer.model_max_length} tokens: {removed_text}""" )
  
  							# no positional arguments to text_encoder
  							SCREAMING_SNAKE_CASE    	=   self.text_encoder(
  							    input_ids=text_input_ids.int().to(lowerCAmelCase_ )      ,    )
  							SCREAMING_SNAKE_CASE    	=   prompt_embeds[0]
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   prompt_embeds.shape
  				# duplicate text embeddings for each generation per prompt, using mps friendly method
  				SCREAMING_SNAKE_CASE    	=   prompt_embeds.repeat(1      ,    lowerCAmelCase_ )
  				SCREAMING_SNAKE_CASE    	=   prompt_embeds.reshape(bs_embed * num_images_per_prompt      ,    lowerCAmelCase_      ,    -1 )
  
  				# get unconditional embeddings for classifier free guidance
  				if do_classifier_free_guidance and negative_prompt_embeds is None:
  							SCREAMING_SNAKE_CASE    	=   4_2
  							if negative_prompt is None:
  										SCREAMING_SNAKE_CASE    	=   [''''''] * batch_size
  							elif type(lowerCAmelCase_ ) is not type(lowerCAmelCase_ ):
  										raise TypeError(
  										    f"""`negative_prompt` should be the same type to `prompt`, but got {type(lowerCAmelCase_ )} !="""
  										    f""" {type(lowerCAmelCase_ )}.""" )
  							elif isinstance(lowerCAmelCase_      ,    lowerCAmelCase_ ):
  										SCREAMING_SNAKE_CASE    	=   [negative_prompt]
  							elif batch_size != len(lowerCAmelCase_ ):
  										raise ValueError(
  										    f"""`negative_prompt`: {negative_prompt} has batch size {len(lowerCAmelCase_ )}, but `prompt`:"""
  										    f""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
  										    ''' the batch size of `prompt`.''' )
  							else:
  										SCREAMING_SNAKE_CASE    	=   negative_prompt
  
  							SCREAMING_SNAKE_CASE    	=   text_input_ids.shape[-1]
  							SCREAMING_SNAKE_CASE    	=   self.tokenizer(
  							    lowerCAmelCase_      ,    padding='''max_length'''      ,    max_length=lowerCAmelCase_      ,    truncation=lowerCAmelCase_      ,    return_tensors='''pt'''      ,    )
  
  							# if hasattr(uncond_input, "attention_mask"):
  							#     attention_mask = uncond_input.attention_mask.to(device)
  							# else:
  							#     attention_mask = None
  
  							SCREAMING_SNAKE_CASE    	=   self.text_encoder(
  							    input_ids=uncond_input.input_ids.int().to(lowerCAmelCase_ )      ,    )
  							SCREAMING_SNAKE_CASE    	=   uncond_embeddings[0]
  
  				if do_classifier_free_guidance:
  							SCREAMING_SNAKE_CASE    	=   uncond_embeddings.shape[1]
  							# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
  							SCREAMING_SNAKE_CASE    	=   uncond_embeddings.repeat(1      ,    lowerCAmelCase_ )
  							SCREAMING_SNAKE_CASE    	=   uncond_embeddings.reshape(batch_size * num_images_per_prompt      ,    lowerCAmelCase_      ,    -1 )
  
  							# For classifier free guidance, we need to do two forward passes.
  							# Here we concatenate the unconditional and text embeddings into a single batch
  							# to avoid doing two forward passes
  							SCREAMING_SNAKE_CASE    	=   np.concatenate([uncond_embeddings, prompt_embeds] )
  
  				return prompt_embeds
style_context: 
  import math
  from typing import Optional
  
  import numpy as np
  import torch
  from torch import nn
  
  from .activations import get_activation
  
  
  def       a			(   a		,	a		,	a = False		,	a = 1		,	a = 1		,	a = 1_0000		,	)    ->Dict:
  
  
  
  
  			'''simple docstring'''
  
  
  			assert len(timesteps.shape  ) == 1, "Timesteps should be a 1d-array"
  
  			SCREAMING_SNAKE_CASE    	=   embedding_dim // 2
  			SCREAMING_SNAKE_CASE    	=   -math.log(a  ) * torch.arange(
  			    start=0		,	end=a		,	dtype=torch.floataa		,	device=timesteps.device  )
  			SCREAMING_SNAKE_CASE    	=   exponent / (half_dim - downscale_freq_shift)
  
  			SCREAMING_SNAKE_CASE    	=   torch.exp(a  )
  			SCREAMING_SNAKE_CASE    	=   timesteps[:, None].float() * emb[None, :]
  
  			# scale embeddings
  			SCREAMING_SNAKE_CASE    	=   scale * emb
  
  			# concat sine and cosine embeddings
  			SCREAMING_SNAKE_CASE    	=   torch.cat([torch.sin(a  ), torch.cos(a  )]		,	dim=-1  )
  
  			# flip sine and cosine embeddings
  			if flip_sin_to_cos:
  						SCREAMING_SNAKE_CASE    	=   torch.cat([emb[:, half_dim:], emb[:, :half_dim]]		,	dim=-1  )
  
  			# zero pad
  			if embedding_dim % 2 == 1:
  						SCREAMING_SNAKE_CASE    	=   torch.nn.functional.pad(a		,	(0, 1, 0, 0)  )
  			return emb
  
  
  def       a			(   a		,	a		,	a=False		,	a=0  )    ->Union[str, Any]:
  
  
  
  
  			'''simple docstring'''
  
  
  			SCREAMING_SNAKE_CASE    	=   np.arange(a		,	dtype=np.floataa  )
  			SCREAMING_SNAKE_CASE    	=   np.arange(a		,	dtype=np.floataa  )
  			SCREAMING_SNAKE_CASE    	=   np.meshgrid(a		,	a  )  # here w goes first
  			SCREAMING_SNAKE_CASE    	=   np.stack(a		,	axis=0  )
  
  			SCREAMING_SNAKE_CASE    	=   grid.reshape([2, 1, grid_size, grid_size]  )
  			SCREAMING_SNAKE_CASE    	=   get_ad_sincos_pos_embed_from_grid(a		,	a  )
  			if cls_token and extra_tokens > 0:
  						SCREAMING_SNAKE_CASE    	=   np.concatenate([np.zeros([extra_tokens, embed_dim]  ), pos_embed]		,	axis=0  )
  			return pos_embed
  
  
  def       a			(   a		,	a  )    ->Optional[int]:
  
  
  
  
  			'''simple docstring'''
  
  
  			if embed_dim % 2 != 0:
  						raise ValueError('''embed_dim must be divisible by 2'''  )
  
  			# use half of dimensions to encode grid_h
  			SCREAMING_SNAKE_CASE    	=   get_ad_sincos_pos_embed_from_grid(embed_dim // 2		,	grid[0]  )  # (H*W, D/2)
  			SCREAMING_SNAKE_CASE    	=   get_ad_sincos_pos_embed_from_grid(embed_dim // 2		,	grid[1]  )  # (H*W, D/2)
  
  			SCREAMING_SNAKE_CASE    	=   np.concatenate([emb_h, emb_w]		,	axis=1  )  # (H*W, D)
  			return emb
  
  
  def       a			(   a		,	a  )    ->List[Any]:
  
  
  
  
  			'''simple docstring'''
  
  
  			if embed_dim % 2 != 0:
  						raise ValueError('''embed_dim must be divisible by 2'''  )
  
  			SCREAMING_SNAKE_CASE    	=   np.arange(embed_dim // 2		,	dtype=np.floataa  )
  			omega /= embed_dim / 2.0
  			SCREAMING_SNAKE_CASE    	=   1.0 / 1_0000**omega  # (D/2,)
  
  			SCREAMING_SNAKE_CASE    	=   pos.reshape(-1  )  # (M,)
  			SCREAMING_SNAKE_CASE    	=   np.einsum('''m,d->md'''		,	a		,	a  )  # (M, D/2), outer product
  
  			SCREAMING_SNAKE_CASE    	=   np.sin(a  )  # (M, D/2)
  			SCREAMING_SNAKE_CASE    	=   np.cos(a  )  # (M, D/2)
  
  			SCREAMING_SNAKE_CASE    	=   np.concatenate([emb_sin, emb_cos]		,	axis=1  )  # (M, D)
  			return emb
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:str      ,    lowercase			:int=2_2_4      ,    lowercase			:Dict=2_2_4      ,    lowercase			:List[Any]=1_6      ,    lowercase			:str=3      ,    lowercase			:List[str]=7_6_8      ,    lowercase			:Tuple=False      ,    lowercase			:Optional[int]=True      ,    lowercase			:str=True      ,    )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   (height // patch_size) * (width // patch_size)
  				SCREAMING_SNAKE_CASE    	=   flatten
  				SCREAMING_SNAKE_CASE    	=   layer_norm
  
  				SCREAMING_SNAKE_CASE    	=   nn.Convad(
  				    lowercase      ,    lowercase      ,    kernel_size=(patch_size, patch_size)      ,    stride=lowercase      ,    bias=lowercase )
  				if layer_norm:
  							SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase      ,    elementwise_affine=lowercase      ,    eps=1e-6 )
  				else:
  							SCREAMING_SNAKE_CASE    	=   None
  
  				SCREAMING_SNAKE_CASE    	=   get_ad_sincos_pos_embed(lowercase      ,    int(num_patches**0.5 ) )
  				self.register_buffer('''pos_embed'''      ,    torch.from_numpy(lowercase ).float().unsqueeze(0 )      ,    persistent=lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[int]      ,    lowercase			:str )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.proj(lowercase )
  				if self.flatten:
  							SCREAMING_SNAKE_CASE    	=   latent.flatten(2 ).transpose(1      ,    2 )  # BCHW -> BNC
  				if self.layer_norm:
  							SCREAMING_SNAKE_CASE    	=   self.norm(lowercase )
  				return latent + self.pos_embed
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Union[str, Any]      ,    lowercase			:int      ,    lowercase			:int      ,    lowercase			:str = "silu"      ,    lowercase			:int = None      ,    lowercase			:Optional[str] = None      ,    lowercase			:int=None      ,    )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  
  				if cond_proj_dim is not None:
  							SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase      ,    bias=lowercase )
  				else:
  							SCREAMING_SNAKE_CASE    	=   None
  
  				SCREAMING_SNAKE_CASE    	=   get_activation(lowercase )
  
  				if out_dim is not None:
  							SCREAMING_SNAKE_CASE    	=   out_dim
  				else:
  							SCREAMING_SNAKE_CASE    	=   time_embed_dim
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  
  				if post_act_fn is None:
  							SCREAMING_SNAKE_CASE    	=   None
  				else:
  							SCREAMING_SNAKE_CASE    	=   get_activation(lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[int]      ,    lowercase			:Tuple      ,    lowercase			:Optional[Any]=None )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if condition is not None:
  							SCREAMING_SNAKE_CASE    	=   sample + self.cond_proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.linear_a(lowercase )
  
  				if self.act is not None:
  							SCREAMING_SNAKE_CASE    	=   self.act(lowercase )
  
  				SCREAMING_SNAKE_CASE    	=   self.linear_a(lowercase )
  
  				if self.post_act is not None:
  							SCREAMING_SNAKE_CASE    	=   self.post_act(lowercase )
  				return sample
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:int      ,    lowercase			:int      ,    lowercase			:bool      ,    lowercase			:float )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   num_channels
  				SCREAMING_SNAKE_CASE    	=   flip_sin_to_cos
  				SCREAMING_SNAKE_CASE    	=   downscale_freq_shift
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str      ,    lowercase			:str )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   get_timestep_embedding(
  				    lowercase      ,    self.num_channels      ,    flip_sin_to_cos=self.flip_sin_to_cos      ,    downscale_freq_shift=self.downscale_freq_shift      ,    )
  				return t_emb
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:List[str]      ,    lowercase			:int = 2_5_6      ,    lowercase			:float = 1.0      ,    lowercase			:int=True      ,    lowercase			:Any=True      ,    lowercase			:List[Any]=False )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   nn.Parameter(torch.randn(lowercase ) * scale      ,    requires_grad=lowercase )
  				SCREAMING_SNAKE_CASE    	=   log
  				SCREAMING_SNAKE_CASE    	=   flip_sin_to_cos
  
  				if set_W_to_weight:
  							# to delete later
  							SCREAMING_SNAKE_CASE    	=   nn.Parameter(torch.randn(lowercase ) * scale      ,    requires_grad=lowercase )
  
  							SCREAMING_SNAKE_CASE    	=   self.W
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Optional[Any]      ,    lowercase			:Optional[Any] )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if self.log:
  							SCREAMING_SNAKE_CASE    	=   torch.log(lowercase )
  
  				SCREAMING_SNAKE_CASE    	=   x[:, None] * self.weight[None, :] * 2 * np.pi
  
  				if self.flip_sin_to_cos:
  							SCREAMING_SNAKE_CASE    	=   torch.cat([torch.cos(lowercase ), torch.sin(lowercase )]      ,    dim=-1 )
  				else:
  							SCREAMING_SNAKE_CASE    	=   torch.cat([torch.sin(lowercase ), torch.cos(lowercase )]      ,    dim=-1 )
  				return out
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Optional[int]      ,    lowercase			:int      ,    lowercase			:int      ,    lowercase			:int      ,    lowercase			:int      ,    )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   height
  				SCREAMING_SNAKE_CASE    	=   width
  				SCREAMING_SNAKE_CASE    	=   num_embed
  				SCREAMING_SNAKE_CASE    	=   embed_dim
  
  				SCREAMING_SNAKE_CASE    	=   nn.Embedding(self.num_embed      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.Embedding(self.height      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.Embedding(self.width      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int      ,    lowercase			:Tuple )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.emb(lowercase )
  
  				SCREAMING_SNAKE_CASE    	=   self.height_emb(torch.arange(self.height      ,    device=index.device ).view(1      ,    self.height ) )
  
  				# 1 x H x D -> 1 x H x 1 x D
  				SCREAMING_SNAKE_CASE    	=   height_emb.unsqueeze(2 )
  
  				SCREAMING_SNAKE_CASE    	=   self.width_emb(torch.arange(self.width      ,    device=index.device ).view(1      ,    self.width ) )
  
  				# 1 x W x D -> 1 x 1 x W x D
  				SCREAMING_SNAKE_CASE    	=   width_emb.unsqueeze(1 )
  
  				SCREAMING_SNAKE_CASE    	=   height_emb + width_emb
  
  				# 1 x H x W x D -> 1 x L xD
  				SCREAMING_SNAKE_CASE    	=   pos_emb.view(1      ,    self.height * self.width      ,    -1 )
  
  				SCREAMING_SNAKE_CASE    	=   emb + pos_emb[:, : emb.shape[1], :]
  
  				return emb
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Optional[Any]      ,    lowercase			:Tuple      ,    lowercase			:int      ,    lowercase			:Union[str, Any] )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   dropout_prob > 0
  				SCREAMING_SNAKE_CASE    	=   nn.Embedding(num_classes + use_cfg_embedding      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   num_classes
  				SCREAMING_SNAKE_CASE    	=   dropout_prob
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int      ,    lowercase			:Tuple      ,    lowercase			:List[str]=None )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				if force_drop_ids is None:
  							SCREAMING_SNAKE_CASE    	=   torch.rand(labels.shape[0]      ,    device=labels.device ) < self.dropout_prob
  				else:
  							SCREAMING_SNAKE_CASE    	=   torch.tensor(force_drop_ids == 1 )
  				SCREAMING_SNAKE_CASE    	=   torch.where(lowercase      ,    self.num_classes      ,    lowercase )
  				return labels
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[Any]      ,    lowercase			:torch.LongTensor      ,    lowercase			:Tuple=None )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.dropout_prob > 0
  				if (self.training and use_dropout) or (force_drop_ids is not None):
  							SCREAMING_SNAKE_CASE    	=   self.token_drop(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.embedding_table(lowercase )
  				return embeddings
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Any      ,    lowercase			:int = 1_0_2_4      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 1_0      ,    )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   num_image_text_embeds
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    self.num_image_text_embeds * cross_attention_dim )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Tuple      ,    lowercase			:torch.FloatTensor      ,    lowercase			:torch.FloatTensor )		->			Optional[int]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   text_embeds.shape[0]
  
  				# image
  				SCREAMING_SNAKE_CASE    	=   self.image_embeds(lowercase )
  				SCREAMING_SNAKE_CASE    	=   image_text_embeds.reshape(lowercase      ,    self.num_image_text_embeds      ,    -1 )
  
  				# text
  				SCREAMING_SNAKE_CASE    	=   self.text_proj(lowercase )
  
  				return torch.cat([image_text_embeds, text_embeds]      ,    dim=1 )
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Optional[int]      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 3_2      ,    )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   num_image_text_embeds
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    self.num_image_text_embeds * cross_attention_dim )
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:List[str]      ,    lowercase			:torch.FloatTensor )		->			Tuple:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   image_embeds.shape[0]
  
  				# image
  				SCREAMING_SNAKE_CASE    	=   self.image_embeds(lowercase )
  				SCREAMING_SNAKE_CASE    	=   image_embeds.reshape(lowercase      ,    self.num_image_text_embeds      ,    -1 )
  				SCREAMING_SNAKE_CASE    	=   self.norm(lowercase )
  				return image_embeds
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:List[str]      ,    lowercase			:int      ,    lowercase			:Union[str, Any]      ,    lowercase			:Optional[Any]=0.1 )		->			int:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  
  				SCREAMING_SNAKE_CASE    	=   Timesteps(num_channels=2_5_6      ,    flip_sin_to_cos=lowercase      ,    downscale_freq_shift=1 )
  				SCREAMING_SNAKE_CASE    	=   TimestepEmbedding(in_channels=2_5_6      ,    time_embed_dim=lowercase )
  				SCREAMING_SNAKE_CASE    	=   LabelEmbedding(lowercase      ,    lowercase      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int      ,    lowercase			:Optional[int]      ,    lowercase			:int      ,    lowercase			:Optional[int]=None )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.time_proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.timestep_embedder(timesteps_proj.to(dtype=lowercase ) )  # (N, D)
  
  				SCREAMING_SNAKE_CASE    	=   self.class_embedder(lowercase )  # (N, D)
  
  				SCREAMING_SNAKE_CASE    	=   timesteps_emb + class_labels  # (N, D)
  
  				return conditioning
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:str      ,    lowercase			:int      ,    lowercase			:int      ,    lowercase			:int = 6_4 )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  				SCREAMING_SNAKE_CASE    	=   AttentionPooling(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int      ,    lowercase			:Optional[int] )		->			Dict:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.norma(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.pool(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.norma(lowercase )
  				return hidden_states
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:Tuple      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 1_5_3_6 )		->			Union[str, Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:str      ,    lowercase			:torch.FloatTensor      ,    lowercase			:torch.FloatTensor )		->			List[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.text_proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.text_norm(lowercase )
  
  				# image
  				SCREAMING_SNAKE_CASE    	=   self.image_proj(lowercase )
  
  				return time_image_embeds + time_text_embeds
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:int      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 1_5_3_6 )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Any      ,    lowercase			:torch.FloatTensor )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.image_proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.image_norm(lowercase )
  				return time_image_embeds
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:List[str]      ,    lowercase			:int = 7_6_8      ,    lowercase			:int = 1_5_3_6 )		->			str:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.LayerNorm(lowercase )
  				SCREAMING_SNAKE_CASE    	=   nn.Sequential(
  				    nn.Convad(3      ,    1_6      ,    3      ,    padding=1 )      ,    nn.SiLU()      ,    nn.Convad(1_6      ,    1_6      ,    3      ,    padding=1 )      ,    nn.SiLU()      ,    nn.Convad(1_6      ,    3_2      ,    3      ,    padding=1      ,    stride=2 )      ,    nn.SiLU()      ,    nn.Convad(3_2      ,    3_2      ,    3      ,    padding=1 )      ,    nn.SiLU()      ,    nn.Convad(3_2      ,    9_6      ,    3      ,    padding=1      ,    stride=2 )      ,    nn.SiLU()      ,    nn.Convad(9_6      ,    9_6      ,    3      ,    padding=1 )      ,    nn.SiLU()      ,    nn.Convad(9_6      ,    2_5_6      ,    3      ,    padding=1      ,    stride=2 )      ,    nn.SiLU()      ,    nn.Convad(2_5_6      ,    4      ,    3      ,    padding=1 )      ,    )
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:Any      ,    lowercase			:torch.FloatTensor      ,    lowercase			:torch.FloatTensor )		->			Any:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE    	=   self.image_proj(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.image_norm(lowercase )
  				SCREAMING_SNAKE_CASE    	=   self.input_hint_block(lowercase )
  				return time_image_embeds, hint
  
  
  
  
  
  class 						lowerCamelCase			(					nn.Module				):
  
  
  
  
  
  
  
  	def __init__(    self			:List[Any]      ,    lowercase			:Any      ,    lowercase			:Union[str, Any]      ,    lowercase			:int=None )		->			Optional[Any]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				super().__init__()
  				SCREAMING_SNAKE_CASE    	=   dtype
  				SCREAMING_SNAKE_CASE    	=   nn.Parameter(torch.randn(1      ,    lowercase ) / embed_dim**0.5 )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase      ,    dtype=self.dtype )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase      ,    dtype=self.dtype )
  				SCREAMING_SNAKE_CASE    	=   nn.Linear(lowercase      ,    lowercase      ,    dtype=self.dtype )
  				SCREAMING_SNAKE_CASE    	=   num_heads
  				SCREAMING_SNAKE_CASE    	=   embed_dim // self.num_heads
  
  
  
  
  
  
  
  	def    snake_case__    (    self			:int      ,    lowercase			:Optional[int] )		->			List[str]:
  
  
  
  
  
  
  
  				"""simple docstring"""
  
  
  
  
  
  
  				SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE	,       SCREAMING_SNAKE_CASE    	=   x.size()
  
  				def shape(lowercase			:List[Any] ):
  							# (bs, length, width) --> (bs, length, n_heads, dim_per_head)
  							SCREAMING_SNAKE_CASE    	=   x.view(lowercase      ,    -1      ,    self.num_heads      ,    self.dim_per_head )
  							# (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
  							SCREAMING_SNAKE_CASE    	=   x.transpose(1      ,    2 )
  							# (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
  							SCREAMING_SNAKE_CASE    	=   x.reshape(bs * self.num_heads      ,    -1      ,    self.dim_per_head )
  							# (bs*n_heads, length, dim_per_head) --> (bs*n_heads, dim_per_head, length)
  							SCREAMING_SNAKE_CASE    	=   x.transpose(1      ,    2 )
  							return x
  
  				SCREAMING_SNAKE_CASE    	=   x.mean(dim=1      ,    keepdim=lowercase ) + self.positional_embedding.to(x.dtype )
  				SCREAMING_SNAKE_CASE    	=   torch.cat([class_token, x]      ,    dim=1 )  # (bs, length+1, width)
  
  				# (bs*n_heads, class_token_length, dim_per_head)
  				SCREAMING_SNAKE_CASE    	=   shape(self.q_proj(lowercase ) )
  				# (bs*n_heads, length+class_token_length, dim_per_head)
  				SCREAMING_SNAKE_CASE    	=   shape(self.k_proj(lowercase ) )
  				SCREAMING_SNAKE_CASE    	=   shape(self.v_proj(lowercase ) )
  
  				# (bs*n_heads, class_token_length, length+class_token_length):
  				SCREAMING_SNAKE_CASE    	=   1 / math.sqrt(math.sqrt(self.dim_per_head ) )
  				SCREAMING_SNAKE_CASE    	=   torch.einsum('''bct,bcs->bts'''      ,    q * scale      ,    k * scale )  # More stable with f16 than dividing afterwards
  				SCREAMING_SNAKE_CASE    	=   torch.softmax(weight.float()      ,    dim=-1 ).type(weight.dtype )
  
  				# (bs*n_heads, dim_per_head, class_token_length)
  				SCREAMING_SNAKE_CASE    	=   torch.einsum('''bts,bcs->bct'''      ,    lowercase      ,    lowercase )
  
  				# (bs, length+1, width)
  				SCREAMING_SNAKE_CASE    	=   a.reshape(lowercase      ,    -1      ,    1 ).transpose(1      ,    2 )
  
  				return a[:, 0, :]  # cls_token