

'''simple docstring'''

from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
							import tensorflow as tf

							from transformers import TFBlipTextModel
							from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST






class    snake_case     :






				"""simple docstring"""



				def __init__(    self,     _lowercase,     _lowercase=12,     _lowercase=7,     _lowercase=True,     _lowercase=True,     _lowercase=True,     _lowercase=99,     _lowercase=32,     _lowercase=32,     _lowercase=2,     _lowercase=4,     _lowercase=37,     _lowercase=0.1,     _lowercase=0.1,     _lowercase=512,     _lowercase=0.02,     _lowercase=0,     _lowercase=None,     )     ->							Dict:
											SCREAMING_SNAKE_CASE_				=				parent
											SCREAMING_SNAKE_CASE_				=				batch_size
											SCREAMING_SNAKE_CASE_				=				seq_length
											SCREAMING_SNAKE_CASE_				=				is_training
											SCREAMING_SNAKE_CASE_				=				use_input_mask
											SCREAMING_SNAKE_CASE_				=				use_labels
											SCREAMING_SNAKE_CASE_				=				vocab_size
											SCREAMING_SNAKE_CASE_				=				hidden_size
											SCREAMING_SNAKE_CASE_				=				projection_dim
											SCREAMING_SNAKE_CASE_				=				num_hidden_layers
											SCREAMING_SNAKE_CASE_				=				num_attention_heads
											SCREAMING_SNAKE_CASE_				=				intermediate_size
											SCREAMING_SNAKE_CASE_				=				dropout
											SCREAMING_SNAKE_CASE_				=				attention_dropout
											SCREAMING_SNAKE_CASE_				=				max_position_embeddings
											SCREAMING_SNAKE_CASE_				=				initializer_range
											SCREAMING_SNAKE_CASE_				=				scope
											SCREAMING_SNAKE_CASE_				=				bos_token_id



				def   a__					(    self       )     ->							List[str]:
											SCREAMING_SNAKE_CASE_				=				ids_tensor([self.batch_size, self.seq_length],     self.vocab_size       )

											SCREAMING_SNAKE_CASE_				=				None
											if self.use_input_mask:
																		SCREAMING_SNAKE_CASE_				=				random_attention_mask([self.batch_size, self.seq_length]       )

											if input_mask is not None:
																		SCREAMING_SNAKE_CASE_				=				input_mask.numpy()
																		SCREAMING_SNAKE_CASE_				,    SCREAMING_SNAKE_CASE_				=				input_mask.shape
																		SCREAMING_SNAKE_CASE_				=				np.random.randint(1,     seq_length - 1,     size=(batch_size,)       )
																		for batch_idx, start_index in enumerate(_lowercase       ):
																									SCREAMING_SNAKE_CASE_				=				1
																									SCREAMING_SNAKE_CASE_				=				0

											SCREAMING_SNAKE_CASE_				=				self.get_config()

											return config, input_ids, tf.convert_to_tensor(_lowercase       )



				def   a__					(    self       )     ->							Dict:
											return BlipTextConfig(
											    vocab_size=self.vocab_size,     hidden_size=self.hidden_size,     projection_dim=self.projection_dim,     num_hidden_layers=self.num_hidden_layers,     num_attention_heads=self.num_attention_heads,     intermediate_size=self.intermediate_size,     dropout=self.dropout,     attention_dropout=self.attention_dropout,     max_position_embeddings=self.max_position_embeddings,     initializer_range=self.initializer_range,     bos_token_id=self.bos_token_id,     )



				def   a__					(    self,     _lowercase,     _lowercase,     _lowercase       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				TFBlipTextModel(config=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     attention_mask=_lowercase,     training=_lowercase       )
											SCREAMING_SNAKE_CASE_				=				model(_lowercase,     training=_lowercase       )
											self.parent.assertEqual(result.last_hidden_state.shape,     (self.batch_size, self.seq_length, self.hidden_size)       )
											self.parent.assertEqual(result.pooler_output.shape,     (self.batch_size, self.hidden_size)       )





				def   a__					(    self       )     ->							Optional[int]:
											SCREAMING_SNAKE_CASE_				=				self.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE_				,    SCREAMING_SNAKE_CASE_				,    SCREAMING_SNAKE_CASE_				=				config_and_inputs
											SCREAMING_SNAKE_CASE_				=				{'input_ids': input_ids, 'attention_mask': input_mask}
											return config, inputs_dict






@require_tf
class    snake_case     (      _a,						unittest.TestCase		):






				"""simple docstring"""
				_a    	=     (TFBlipTextModel,) if is_tf_available() else ()
				_a    	=     False
				_a    	=     False
				_a    	=     False



				def   a__					(    self       )     ->							Union[str, Any]:
											SCREAMING_SNAKE_CASE_				=				BlipTextModelTester(self       )
											SCREAMING_SNAKE_CASE_				=				ConfigTester(self,     config_class=_lowercase,     hidden_size=37       )



				def   a__					(    self       )     ->							List[str]:
											self.config_tester.run_common_tests()



				def   a__					(    self       )     ->							Any:
											SCREAMING_SNAKE_CASE_				=				self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowercase       )



				def   a__					(    self       )     ->							List[str]:
											pass



				def   a__					(    self       )     ->							Optional[int]:
											pass



				@unittest.skip(reason='Blip does not use inputs_embeds'       )
				def   a__					(    self       )     ->							Tuple:
											pass



				@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'       )
				def   a__					(    self       )     ->							Any:
											pass



				@unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'       )
				def   a__					(    self       )     ->							int:
											pass



				@slow
				def   a__					(    self       )     ->							Union[str, Any]:

											for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE_				=				TFBlipTextModel.from_pretrained(_lowercase       )
																		self.assertIsNotNone(_lowercase       )





				def   a__					(    self,     _lowercase=True       )     ->							List[str]:
											super().test_pt_tf_model_equivalence(allow_missing_keys=_lowercase       )









import gc
import inspect
import unittest

import torch
from parameterized import parameterized

from diffusers import PriorTransformer
from diffusers.utils import floats_tensor, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()






class      UpperCamelCase(	_a  ,				unittest.TestCase ):
					snake_case_		:					Any    					=						PriorTransformer
					snake_case_		:					List[str]    					=						"""hidden_states"""


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						str:




											'''simple docstring'''

											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		floats_tensor((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		floats_tensor((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]=0    )      ->						Any:




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		4
											__snake_case				=		8
											__snake_case				=		7

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						str:




											'''simple docstring'''

											return (4, 8)


					@property
					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]    )      ->						Any:




											'''simple docstring'''

											return (4, 8)


					def 				SCREAMING_SNAKE_CASE_	(    self						:		str    )      ->						Dict:




											'''simple docstring'''

											__snake_case				=		{
											    "num_attention_heads": 2,
											    "attention_head_dim": 4,
											    "num_layers": 2,
											    "embedding_dim": 8,
											    "num_embeddings": 7,
											    "additional_embeddings": 4,
											}
											__snake_case				=		self.dummy_input
											return init_dict, inputs_dict


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Any    )      ->						Dict:




											'''simple docstring'''

											__snake_case		,       __snake_case				=		PriorTransformer.from_pretrained(
											    "hf-internal-testing/prior-dummy"  ,     output_loading_info=SCREAMING_SNAKE_CASE    )
											self.assertIsNotNone(SCREAMING_SNAKE_CASE    )
											self.assertEqual(len(loading_info["missing_keys"]    )  ,     0    )

											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		model(**self.dummy_input    )[0]

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


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Dict    )      ->						Optional[Any]:




											'''simple docstring'''

											__snake_case		,       __snake_case				=		self.prepare_init_args_and_inputs_for_common()

											__snake_case				=		self.model_class(**SCREAMING_SNAKE_CASE    )
											__snake_case				=		inspect.signature(model.forward    )
											# signature.parameters is an OrderedDict => so arg_names order is deterministic
											__snake_case				=		[*signature.parameters.keys()]

											__snake_case				=		["hidden_states", "timestep"]
											self.assertListEqual(arg_names[:2]  ,     SCREAMING_SNAKE_CASE    )






					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("hf-internal-testing/prior-dummy"    )
											__snake_case				=		model.to(SCREAMING_SNAKE_CASE    )

											if hasattr(SCREAMING_SNAKE_CASE  ,     "set_default_attn_processor"    ):
																	model.set_default_attn_processor()

											__snake_case				=		self.get_dummy_seed_input()

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											__snake_case				=		output[0, :5].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )

											# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
											# the expected output slices are not the same for CPU and GPU.
											__snake_case				=		torch.tensor([-1.3436, -0.2870, 0.7538, 0.4368, -0.0239]    )
											self.assertTrue(torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     rtol=1e-2    )    )






@slow
class      UpperCamelCase(	unittest.TestCase ):


					def 				SCREAMING_SNAKE_CASE_	(    self						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		int=1  ,     SCREAMING_SNAKE_CASE						:		str=7_6_8  ,     SCREAMING_SNAKE_CASE						:		Dict=7_7  ,     SCREAMING_SNAKE_CASE						:		int=0    )      ->						List[str]:




											'''simple docstring'''

											torch.manual_seed(SCREAMING_SNAKE_CASE    )
											__snake_case				=		batch_size
											__snake_case				=		embedding_dim
											__snake_case				=		num_embeddings

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											__snake_case				=		torch.randn((batch_size, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.randn((batch_size, num_embeddings, embedding_dim)    ).to(SCREAMING_SNAKE_CASE    )

											return {
											    "hidden_states": hidden_states,
											    "timestep": 2,
											    "proj_embedding": proj_embedding,
											    "encoder_hidden_states": encoder_hidden_states,
											}


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						Union[str, Any]:




											'''simple docstring'''

											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()






					@parameterized.expand(
					    [
					        # fmt: off
					        [1_3, [-0.5861,  0.1283, -0.0931,  0.0882,  0.4476,  0.1329, -0.0498,  0.0640]],
					        [3_7, [-0.4913,  0.0110, -0.0483,  0.0541,  0.4954, -0.0170,  0.0354,  0.1651]],
					        # fmt: on
					    ]    )
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		Tuple    )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		PriorTransformer.from_pretrained("kandinsky-community/kandinsky-2-1-prior"  ,     subfolder="prior"    )
											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		self.get_dummy_seed_input(seed=SCREAMING_SNAKE_CASE    )

											with torch.no_grad():
																	__snake_case				=		model(**SCREAMING_SNAKE_CASE    )[0]

											assert list(sample.shape    ) == [1, 7_6_8]

											__snake_case				=		sample[0, :8].flatten().cpu()
											print(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.tensor(SCREAMING_SNAKE_CASE    )

											assert torch_all_close(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     atol=1e-3    )





