




import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       VQModel
 lowercase_							       =       "sample"



 @property
 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						str=(32, 32)		):
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     3

       SCREAMING_SNAKE_CASE_      		=     floats_tensor((batch_size, num_channels) + sizes		).to(_lowerCAmelCase		)

       return {"sample": image}



 @property
 def  lowerCAmelCase_    (				self    :						str		):
       return (3, 32, 32)



 @property
 def  lowerCAmelCase_    (				self    :						List[str]		):
       return (3, 32, 32)



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     {
           'block_out_channels': [32, 64],
           'in_channels': 3,
           'out_channels': 3,
           'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
           'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
           'latent_channels': 3,
       }
       SCREAMING_SNAKE_CASE_      		=     self.dummy_input
       return init_dict, inputs_dict



 def  lowerCAmelCase_    (				self    :						Tuple		):
       pass



 def  lowerCAmelCase_    (				self    :						List[str]		):
       pass



 def  lowerCAmelCase_    (				self    :						List[Any]		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     VQModel.from_pretrained('fusing/vqgan-dummy'							,							output_loading_info=_lowerCAmelCase		)
       self.assertIsNotNone(_lowerCAmelCase		)
       self.assertEqual(len(loading_info['missing_keys']		)							,							0		)

       model.to(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     model(**self.dummy_input		)

       assert image is not None, "Make sure output is not None"




 def  lowerCAmelCase_    (				self    :						List[Any]		):
       SCREAMING_SNAKE_CASE_      		=     VQModel.from_pretrained('fusing/vqgan-dummy'		)
       model.to(_lowerCAmelCase		).eval()

       torch.manual_seed(0		)
       if torch.cuda.is_available():
             torch.cuda.manual_seed_all(0		)

       SCREAMING_SNAKE_CASE_      		=     torch.randn(1							,							model.config.in_channels							,							model.config.sample_size							,							model.config.sample_size		)
       SCREAMING_SNAKE_CASE_      		=     image.to(_lowerCAmelCase		)
       with torch.no_grad():
             SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase		).sample

       SCREAMING_SNAKE_CASE_      		=     output[0, -1, -3:, -3:].flatten().cpu()
       # fmt: off
       SCREAMING_SNAKE_CASE_      		=     torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143]		)
       # fmt: on
       self.assertTrue(torch.allclose(_lowerCAmelCase							,							_lowerCAmelCase							,							atol=1E-3		)		)

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin





class     lowerCamelCase      (       __lowerCamelCase ,		__lowerCamelCase					):







	@register_to_config
	def __init__(     self      :Tuple   ,     *,
	    lowercase      :int = 4   ,     lowercase      :int = 7_6_8   ,     lowercase      :int   ,     lowercase      :Any   ,     )		->      Optional[Any]:







				"""simple docstring"""






				super().__init__()

				SCREAMING_SNAKE_CASE     						= nn.Parameter(torch.zeros(lowercase			)			)

				# parameters for additional clip time embeddings
				SCREAMING_SNAKE_CASE     						= nn.Linear(lowercase   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= nn.Linear(lowercase   ,     lowercase			)

				# parameters for encoder hidden states
				SCREAMING_SNAKE_CASE     						= clip_extra_context_tokens
				SCREAMING_SNAKE_CASE     						= nn.Linear(
				    lowercase   ,     self.clip_extra_context_tokens * cross_attention_dim			)
				SCREAMING_SNAKE_CASE     						= nn.Linear(lowercase   ,     lowercase			)
				SCREAMING_SNAKE_CASE     						= nn.LayerNorm(lowercase			)







	def  snake_case__     (     self      :Union[str, Any]   ,     *, lowercase      :Optional[int]   ,     lowercase      :Union[str, Any]   ,     lowercase      :Union[str, Any]   ,     lowercase      :Optional[Any]			)		->      Union[str, Any]:







				"""simple docstring"""






				if do_classifier_free_guidance:
							# Add the classifier free guidance embeddings to the image embeddings
							SCREAMING_SNAKE_CASE     						= image_embeddings.shape[0]
							SCREAMING_SNAKE_CASE     						= self.learned_classifier_free_guidance_embeddings.unsqueeze(0			)
							SCREAMING_SNAKE_CASE     						= classifier_free_guidance_embeddings.expand(
							    lowercase   ,     -1			)
							SCREAMING_SNAKE_CASE     						= torch.cat([classifier_free_guidance_embeddings, image_embeddings]   ,     dim=0			)

				# The image embeddings batch size and the text embeddings batch size are equal
				assert image_embeddings.shape[0] == prompt_embeds.shape[0]

				SCREAMING_SNAKE_CASE     						= prompt_embeds.shape[0]

				# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
				# adding CLIP embeddings to the existing timestep embedding, ...
				SCREAMING_SNAKE_CASE     						= self.embedding_proj(lowercase			)
				SCREAMING_SNAKE_CASE     						= self.clip_image_embeddings_project_to_time_embeddings(lowercase			)
				SCREAMING_SNAKE_CASE     						= time_projected_image_embeddings + time_projected_prompt_embeds

				# ... and by projecting CLIP embeddings into four
				# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
				SCREAMING_SNAKE_CASE     						= self.clip_extra_context_tokens_proj(lowercase			)
				SCREAMING_SNAKE_CASE     						= clip_extra_context_tokens.reshape(lowercase   ,     -1   ,     self.clip_extra_context_tokens			)
				SCREAMING_SNAKE_CASE     						= clip_extra_context_tokens.permute(0   ,     2   ,     1			)

				SCREAMING_SNAKE_CASE     						= self.encoder_hidden_states_proj(lowercase			)
				SCREAMING_SNAKE_CASE     						= self.text_encoder_hidden_states_norm(lowercase			)
				SCREAMING_SNAKE_CASE     						= torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]   ,     dim=1			)

				return text_encoder_hidden_states, additive_clip_time_embeddings