

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


_a:			int       							=	getLogger(__name__)


_a:			Optional[int]       							=	4
_a:			int       							=	7

_a:			Tuple       							=	{
    "float16": torch.floataa,
    "float32": torch.floataa,
}



def 					__lowerCAmelCase					(  A			):


	if isinstance(_SCREAMING_SNAKE_CASE				,   torch.Tensor			):
		return image
	elif isinstance(_SCREAMING_SNAKE_CASE				,   PIL.Image.Image			):
		UpperCAmelCase_				 =	[image]

	if isinstance(image[0]				,   PIL.Image.Image			):
		UpperCAmelCase_ ,      UpperCAmelCase_				 =	image[0].size
		UpperCAmelCase_ ,      UpperCAmelCase_				 =	(x - x % 64 for x in (w, h))  # resize to integer multiple of 32

		UpperCAmelCase_				 =	[np.array(i.resize((w, h)			)			)[None, :] for i in image]
		UpperCAmelCase_				 =	np.concatenate(_SCREAMING_SNAKE_CASE				,   axis=0			)
		UpperCAmelCase_				 =	np.array(_SCREAMING_SNAKE_CASE			).astype(np.floataa			) / 255.0
		UpperCAmelCase_				 =	image.transpose(0				,   3				,   1				,   2			)
		UpperCAmelCase_				 =	2.0 * image - 1.0
		UpperCAmelCase_				 =	torch.from_numpy(_SCREAMING_SNAKE_CASE			)
	elif isinstance(image[0]				,   torch.Tensor			):
		UpperCAmelCase_				 =	torch.cat(_SCREAMING_SNAKE_CASE				,   dim=0			)

	return image




class   __UpperCamelCase			(    lowerCAmelCase__  ):




	def __init__(						self		:  Tuple						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  List[Any]						,		lowerCAmelCase		:  List[str]						,		lowerCAmelCase		:  Dict						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  Any						,		lowerCAmelCase		:  Any = 350						,		):


		'''simple docstring'''







		super().__init__(
		    vae=_A						,		text_encoder=_A						,		tokenizer=_A						,		unet=_A						,		low_res_scheduler=_A						,		scheduler=_A						,		safety_checker=_A						,		feature_extractor=_A						,		watermarker=_A						,		max_noise_level=_A						,		)




	def __call__(						self		:  Dict						,		lowerCAmelCase		:  Any						,		lowerCAmelCase		:  Union[str, Any]						,		lowerCAmelCase		:  Dict = 75						,		lowerCAmelCase		:  Dict = 9.0						,		lowerCAmelCase		:  Optional[int] = 20						,		lowerCAmelCase		:  Any = None						,		lowerCAmelCase		:  Any = 1						,		lowerCAmelCase		:  List[str] = 0.0						,		lowerCAmelCase		:  Optional[Any] = None						,		lowerCAmelCase		:  Dict = None						,		lowerCAmelCase		:  Optional[Any] = None						,		lowerCAmelCase		:  int = None						,		lowerCAmelCase		:  str = "pil"						,		lowerCAmelCase		:  Optional[Any] = True						,		lowerCAmelCase		:  List[str] = None						,		lowerCAmelCase		:  int = 1						,		):


		'''simple docstring'''







		self.check_inputs(_A						,		_A						,		_A						,		_A							)

		# 2. Define call parameters
		if prompt is not None and isinstance(_A						,		_A							):
			UpperCAmelCase_				 =	1
		elif prompt is not None and isinstance(_A						,		_A							):
			UpperCAmelCase_				 =	len(_A							)
		else:
			UpperCAmelCase_				 =	prompt_embeds.shape[0]

		UpperCAmelCase_				 =	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.
		UpperCAmelCase_				 =	guidance_scale > 1.0

		# 3. Encode input prompt
		UpperCAmelCase_				 =	self._encode_prompt(
		    _A						,		_A						,		_A						,		_A						,		_A						,		prompt_embeds=_A						,		negative_prompt_embeds=_A						,		)

		UpperCAmelCase_				 =	ORT_TO_PT_TYPE[str(text_embeddings.dtype							)]

		# 4. Preprocess image
		UpperCAmelCase_				 =	preprocess(_A							)
		UpperCAmelCase_				 =	image.cpu()

		# 5. set timesteps
		self.scheduler.set_timesteps(_A						,		device=_A							)
		UpperCAmelCase_				 =	self.scheduler.timesteps

		# 5. Add noise to image
		UpperCAmelCase_				 =	torch.tensor([noise_level]						,		dtype=torch.long						,		device=_A							)
		UpperCAmelCase_				 =	torch.randn(image.shape						,		generator=_A						,		device=_A						,		dtype=_A							)
		UpperCAmelCase_				 =	self.low_res_scheduler.add_noise(_A						,		_A						,		_A							)

		UpperCAmelCase_				 =	2 if do_classifier_free_guidance else 1
		UpperCAmelCase_				 =	np.concatenate([image] * batch_multiplier * num_images_per_prompt							)
		UpperCAmelCase_				 =	np.concatenate([noise_level] * image.shape[0]							)

		# 6. Prepare latent variables
		UpperCAmelCase_ ,      UpperCAmelCase_				 =	image.shape[2:]
		UpperCAmelCase_				 =	self.prepare_latents(
		    batch_size * num_images_per_prompt						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A						,		_A						,		)

		# 7. Check that sizes of image and latents match
		UpperCAmelCase_				 =	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
		UpperCAmelCase_				 =	self.prepare_extra_step_kwargs(_A						,		_A							)

		UpperCAmelCase_				 =	next(
		    (input.type for input in self.unet.model.get_inputs() if input.name == "timestep")						,		"tensor(float)"							)
		UpperCAmelCase_				 =	ORT_TO_NP_TYPE[timestep_dtype]

		# 9. Denoising loop
		UpperCAmelCase_				 =	len(_A							) - num_inference_steps * self.scheduler.order
		with self.progress_bar(total=_A							) as progress_bar:
			for i, t in enumerate(_A							):
				# expand the latents if we are doing classifier free guidance
				UpperCAmelCase_				 =	np.concatenate([latents] * 2							) if do_classifier_free_guidance else latents

				# concat latents, mask, masked_image_latents in the channel dimension
				UpperCAmelCase_				 =	self.scheduler.scale_model_input(_A						,		_A							)
				UpperCAmelCase_				 =	np.concatenate([latent_model_input, image]						,		axis=1							)

				# timestep to tensor
				UpperCAmelCase_				 =	np.array([t]						,		dtype=_A							)

				# predict the noise residual
				UpperCAmelCase_				 =	self.unet(
				    sample=_A						,		timestep=_A						,		encoder_hidden_states=_A						,		class_labels=noise_level.astype(np.intaa							)						,		)[0]

				# perform guidance
				if do_classifier_free_guidance:
					UpperCAmelCase_ ,      UpperCAmelCase_				 =	np.split(_A						,		2							)
				UpperCAmelCase_				 =	noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

				# compute the previous noisy sample x_t -> x_t-1
				UpperCAmelCase_				 =	self.scheduler.step(
				    torch.from_numpy(_A							)						,		_A						,		_A						,		**_A							).prev_sample

				# call the callback, if provided
				if i == len(_A							) - 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(_A						,		_A						,		_A							)

        # 10. Post-processing
		UpperCAmelCase_				 =	self.decode_latents(latents.float()							)

		# 11. Convert to PIL
		if output_type == "pil":
			UpperCAmelCase_				 =	self.numpy_to_pil(_A							)

		if not return_dict:
			return (image,)

		return ImagePipelineOutput(images=_A							)




	def 					__A    (						self		:  Dict						,		lowerCAmelCase		:  Optional[Any]							):


		'''simple docstring'''







		UpperCAmelCase_				 =	1 / 0.08_333 * latents
		UpperCAmelCase_				 =	self.vae(latent_sample=_A							)[0]
		UpperCAmelCase_				 =	np.clip(image / 2 + 0.5						,		0						,		1							)
		UpperCAmelCase_				 =	image.transpose((0, 2, 3, 1)							)
		return image




	def 					__A    (						self		:  List[str]						,		lowerCAmelCase		:  int						,		lowerCAmelCase		:  int						,		lowerCAmelCase		:  List[Any]						,		lowerCAmelCase		:  List[str]						,		lowerCAmelCase		:  Any						,		lowerCAmelCase		:  Optional[Any] = None						,		lowerCAmelCase		:  Union[str, Any] = None						,		):


		'''simple docstring'''







		if prompt is not None and isinstance(_A						,		_A							):
			UpperCAmelCase_				 =	1
		elif prompt is not None and isinstance(_A						,		_A							):
			UpperCAmelCase_				 =	len(_A							)
		else:
			UpperCAmelCase_				 =	prompt_embeds.shape[0]

		if prompt_embeds is None:
			UpperCAmelCase_				 =	self.tokenizer(
			    _A						,		padding="max_length"						,		max_length=self.tokenizer.model_max_length						,		truncation=_A						,		return_tensors="pt"						,		)
			UpperCAmelCase_				 =	text_inputs.input_ids
			UpperCAmelCase_				 =	self.tokenizer(_A						,		padding="longest"						,		return_tensors="pt"							).input_ids

			if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
			    _A						,		_A							):
				UpperCAmelCase_				 =	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
			UpperCAmelCase_				 =	self.text_encoder(
			    input_ids=text_input_ids.int().to(_A							)						,		)
			UpperCAmelCase_				 =	prompt_embeds[0]

		UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_				 =	prompt_embeds.shape
		# duplicate text embeddings for each generation per prompt, using mps friendly method
		UpperCAmelCase_				 =	prompt_embeds.repeat(1						,		_A							)
		UpperCAmelCase_				 =	prompt_embeds.reshape(bs_embed * num_images_per_prompt						,		_A						,		-1							)

		# get unconditional embeddings for classifier free guidance
		if do_classifier_free_guidance and negative_prompt_embeds is None:
			UpperCAmelCase_				 =	42
			if negative_prompt is None:
				UpperCAmelCase_				 =	[""] * batch_size
			elif type(_A							) is not type(_A							):
				raise TypeError(
				    F"`negative_prompt` should be the same type to `prompt`, but got {type(_A							)} !="
				    F" {type(_A							)}."							)
			elif isinstance(_A						,		_A							):
				UpperCAmelCase_				 =	[negative_prompt]
			elif batch_size != len(_A							):
				raise ValueError(
				    F"`negative_prompt`: {negative_prompt} has batch size {len(_A							)}, but `prompt`:"
				    F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
				    " the batch size of `prompt`."							)
			else:
				UpperCAmelCase_				 =	negative_prompt

			UpperCAmelCase_				 =	text_input_ids.shape[-1]
			UpperCAmelCase_				 =	self.tokenizer(
			    _A						,		padding="max_length"						,		max_length=_A						,		truncation=_A						,		return_tensors="pt"						,		)

			# if hasattr(uncond_input, "attention_mask"):
			#     attention_mask = uncond_input.attention_mask.to(device)
			# else:
			#     attention_mask = None

			UpperCAmelCase_				 =	self.text_encoder(
			    input_ids=uncond_input.input_ids.int().to(_A							)						,		)
			UpperCAmelCase_				 =	uncond_embeddings[0]

		if do_classifier_free_guidance:
			UpperCAmelCase_				 =	uncond_embeddings.shape[1]
			# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
			UpperCAmelCase_				 =	uncond_embeddings.repeat(1						,		_A							)
			UpperCAmelCase_				 =	uncond_embeddings.reshape(batch_size * num_images_per_prompt						,		_A						,		-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
			UpperCAmelCase_				 =	np.concatenate([uncond_embeddings, prompt_embeds]							)

		return prompt_embeds


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 					__snake_case     (			lowerCAmelCase__				):
  __lowerCAmelCase   :		torch.FloatTensor






class 					__snake_case     (			lowerCAmelCase__   ,       lowerCAmelCase__				):

  @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__()
      SCREAMING_SNAKE_CASE_													=  use_linear_projection
      SCREAMING_SNAKE_CASE_													=  num_attention_heads
      SCREAMING_SNAKE_CASE_													=  attention_head_dim
      SCREAMING_SNAKE_CASE_													=  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
      SCREAMING_SNAKE_CASE_													=  (in_channels is not None) and (patch_size is None)
      SCREAMING_SNAKE_CASE_													=  num_vector_embeds is not None
      SCREAMING_SNAKE_CASE_													=  in_channels is not None and patch_size is not None

      if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
          SCREAMING_SNAKE_CASE_													=  (
              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)
          SCREAMING_SNAKE_CASE_													=  '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:
          SCREAMING_SNAKE_CASE_													=  in_channels

          SCREAMING_SNAKE_CASE_													=  torch.nn.GroupNorm(num_groups=_A  ,						num_channels=_A  ,						eps=1E-6  ,						affine=_A)
          if use_linear_projection:
              SCREAMING_SNAKE_CASE_													=  nn.Linear(_A  ,						_A)
          else:
              SCREAMING_SNAKE_CASE_													=  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"

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

          SCREAMING_SNAKE_CASE_													=  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"

          SCREAMING_SNAKE_CASE_													=  sample_size
          SCREAMING_SNAKE_CASE_													=  sample_size

          SCREAMING_SNAKE_CASE_													=  patch_size
          SCREAMING_SNAKE_CASE_													=  PatchEmbed(
              height=_A  ,						width=_A  ,						patch_size=_A  ,						in_channels=_A  ,						embed_dim=_A  ,						)

      # 3. Define transformers blocks
      SCREAMING_SNAKE_CASE_													=  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
      SCREAMING_SNAKE_CASE_													=  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:
              SCREAMING_SNAKE_CASE_													=  nn.Linear(_A  ,						_A)
          else:
              SCREAMING_SNAKE_CASE_													=  nn.Convad(_A  ,						_A  ,						kernel_size=1  ,						stride=1  ,						padding=0)
      elif self.is_input_vectorized:
          SCREAMING_SNAKE_CASE_													=  nn.LayerNorm(_A)
          SCREAMING_SNAKE_CASE_													=  nn.Linear(_A  ,						self.num_vector_embeds - 1)
      elif self.is_input_patches:
          SCREAMING_SNAKE_CASE_													=  nn.LayerNorm(_A  ,						elementwise_affine=_A  ,						eps=1E-6)
          SCREAMING_SNAKE_CASE_													=  nn.Linear(_A  ,						2 * inner_dim)
          SCREAMING_SNAKE_CASE_													=  nn.Linear(_A  ,						patch_size * patch_size * self.out_channels)


  def   lowerCAmelCase__   (    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)
          SCREAMING_SNAKE_CASE_													=  (1 - attention_mask.to(hidden_states.dtype)) * -1_0_0_0_0.0
          SCREAMING_SNAKE_CASE_													=  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:
          SCREAMING_SNAKE_CASE_													=  (1 - encoder_attention_mask.to(hidden_states.dtype)) * -1_0_0_0_0.0
          SCREAMING_SNAKE_CASE_													=  encoder_attention_mask.unsqueeze(1)

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

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

      # 2. Blocks
      for block in self.transformer_blocks:
          SCREAMING_SNAKE_CASE_													=  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:
              SCREAMING_SNAKE_CASE_													=  hidden_states.reshape(_A  ,						_A  ,						_A  ,						_A).permute(0  ,						3  ,						1  ,						2).contiguous()
              SCREAMING_SNAKE_CASE_													=  self.proj_out(_A)
          else:
              SCREAMING_SNAKE_CASE_													=  self.proj_out(_A)
              SCREAMING_SNAKE_CASE_													=  hidden_states.reshape(_A  ,						_A  ,						_A  ,						_A).permute(0  ,						3  ,						1  ,						2).contiguous()

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

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

          # unpatchify
          SCREAMING_SNAKE_CASE_													=  SCREAMING_SNAKE_CASE_													=  int(hidden_states.shape[1] ** 0.5)
          SCREAMING_SNAKE_CASE_													=  hidden_states.reshape(
              shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels))
          SCREAMING_SNAKE_CASE_													=  torch.einsum('nhwpqc->nchpwq'  ,						_A)
          SCREAMING_SNAKE_CASE_													=  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)


