


"""simple docstring"""







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_			(				UpperCamelCase    ,						unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						List[Any]					      =    PriorTransformer
	snake_case__       :						Union[str, Any]					      =    "hidden_states"




	@property
	def 						UpperCAmelCase_   (				self    :       Dict     ) ->			Union[str, Any]:
						__SCREAMING_SNAKE_CASE		     =       4
						__SCREAMING_SNAKE_CASE		     =       8
						__SCREAMING_SNAKE_CASE		     =       7

						__SCREAMING_SNAKE_CASE		     =       floats_tensor((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )

						__SCREAMING_SNAKE_CASE		     =       floats_tensor((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       floats_tensor((batch_size, num_embeddings, embedding_dim)     ).to(UpperCAmelCase__     )

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




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       Union[str, Any]=0     ) ->			List[Any]:
						torch.manual_seed(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       4
						__SCREAMING_SNAKE_CASE		     =       8
						__SCREAMING_SNAKE_CASE		     =       7

						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )

						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, num_embeddings, embedding_dim)     ).to(UpperCAmelCase__     )

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




	@property
	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Union[str, Any]:
						return (4, 8)




	@property
	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			str:
						return (4, 8)




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Union[str, Any]:
						__SCREAMING_SNAKE_CASE		     =       {
						    "num_attention_heads": 2,
						    "attention_head_dim": 4,
						    "num_layers": 2,
						    "embedding_dim": 8,
						    "num_embeddings": 7,
						    "additional_embeddings": 4,
						}
						__SCREAMING_SNAKE_CASE		     =       self.dummy_input
						return init_dict, inputs_dict




	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			Optional[int]:
						__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE		     =       PriorTransformer.from_pretrained(
						    "hf-internal-testing/prior-dummy"    ,    output_loading_info=UpperCAmelCase__     )
						self.assertIsNotNone(UpperCAmelCase__     )
						self.assertEqual(len(loading_info["missing_keys"]     )    ,    0     )

						model.to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       model(**self.dummy_input     )[0]

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




	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			List[Any]:
						__SCREAMING_SNAKE_CASE     ,						__SCREAMING_SNAKE_CASE		     =       self.prepare_init_args_and_inputs_for_common()

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

						__SCREAMING_SNAKE_CASE		     =       ["hidden_states", "timestep"]
						self.assertListEqual(arg_names[:2]    ,    UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Any     ) ->			int:
						__SCREAMING_SNAKE_CASE		     =       PriorTransformer.from_pretrained("hf-internal-testing/prior-dummy"     )
						__SCREAMING_SNAKE_CASE		     =       model.to(UpperCAmelCase__     )

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

						__SCREAMING_SNAKE_CASE		     =       self.get_dummy_seed_input()

						with torch.no_grad():
											__SCREAMING_SNAKE_CASE		     =       model(**UpperCAmelCase__     )[0]

						__SCREAMING_SNAKE_CASE		     =       output[0, :5].flatten().cpu()
						print(UpperCAmelCase__     )

						# 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.
						__SCREAMING_SNAKE_CASE		     =       torch.tensor([-1.3_436, -0.2_870, 0.7_538, 0.4_368, -0.0_239]     )
						self.assertTrue(torch_all_close(UpperCAmelCase__    ,    UpperCAmelCase__    ,    rtol=1E-2     )     )




@slow
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	def 						UpperCAmelCase_   (				self    :       Optional[int]    ,    UpperCAmelCase__    :       List[Any]=1    ,    UpperCAmelCase__    :       Tuple=7_6_8    ,    UpperCAmelCase__    :       List[str]=7_7    ,    UpperCAmelCase__    :       Optional[int]=0     ) ->			List[str]:
						torch.manual_seed(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       batch_size
						__SCREAMING_SNAKE_CASE		     =       embedding_dim
						__SCREAMING_SNAKE_CASE		     =       num_embeddings

						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )

						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, embedding_dim)     ).to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       torch.randn((batch_size, num_embeddings, embedding_dim)     ).to(UpperCAmelCase__     )

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




	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			Tuple:
						# clean up the VRAM after each test
						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()




	@parameterized.expand(
	    [
	        # fmt: off
	        [1_3, [-0.5_861,  0.1_283, -0.0_931,  0.0_882,  0.4_476,  0.1_329, -0.0_498,  0.0_640]],
	        [3_7, [-0.4_913,  0.0_110, -0.0_483,  0.0_541,  0.4_954, -0.0_170,  0.0_354,  0.1_651]],
	        # fmt: on
	    ]     )
	def 						UpperCAmelCase_   (				self    :       Optional[Any]    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Union[str, Any]     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       PriorTransformer.from_pretrained("kandinsky-community/kandinsky-2-1-prior"    ,    subfolder="prior"     )
						model.to(UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       self.get_dummy_seed_input(seed=UpperCAmelCase__     )

						with torch.no_grad():
											__SCREAMING_SNAKE_CASE		     =       model(**UpperCAmelCase__     )[0]

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

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

						assert torch_all_close(UpperCAmelCase__    ,    UpperCAmelCase__    ,    atol=1E-3     )




"""simple docstring"""







import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
		from PIL import Image
else:




		class 				UpperCamelCase_			:

			"""simple docstring"""





			@staticmethod
			def 						UpperCAmelCase_   (				*UpperCAmelCase__    :       List[Any]    ,    **UpperCAmelCase__    :       Optional[Any]     ) ->			Optional[int]:
								pass




@is_pipeline_test
@require_torch
@require_vision
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						List[Any]					      =    MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       Tuple    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Optional[int]     ) ->			str:
						__SCREAMING_SNAKE_CASE		     =       pipeline("visual-question-answering"    ,    model="hf-internal-testing/tiny-vilt-random-vqa"     )
						__SCREAMING_SNAKE_CASE		     =       [
						    {
						        "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     ),
						        "question": "How many cats are there?",
						    },
						    {
						        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
						        "question": "How many cats are there?",
						    },
						]
						return vqa_pipeline, examples




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Any     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(UpperCAmelCase__    ,    top_k=1     )
						self.assertEqual(
						    UpperCAmelCase__    ,    [
						        [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}],
						        [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}],
						    ]    ,    )




	@require_torch
	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			int:
						__SCREAMING_SNAKE_CASE		     =       pipeline("visual-question-answering"    ,    model="hf-internal-testing/tiny-vilt-random-vqa"     )
						__SCREAMING_SNAKE_CASE		     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
						__SCREAMING_SNAKE_CASE		     =       "How many cats are there?"

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(image=UpperCAmelCase__    ,    question="How many cats are there?"    ,    top_k=2     )
						self.assertEqual(
						    UpperCAmelCase__    ,    [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}, {"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}]     )

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline({"image": image, "question": question}    ,    top_k=2     )
						self.assertEqual(
						    UpperCAmelCase__    ,    [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}, {"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}]     )




	@slow
	@require_torch
	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       pipeline("visual-question-answering"    ,    model="dandelin/vilt-b32-finetuned-vqa"     )
						__SCREAMING_SNAKE_CASE		     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
						__SCREAMING_SNAKE_CASE		     =       "How many cats are there?"

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(image=UpperCAmelCase__    ,    question=UpperCAmelCase__    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline({"image": image, "question": question}    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]     )

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(
						    [{"image": image, "question": question}, {"image": image, "question": question}]    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    decimals=4     )    ,    [[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2    ,    )




	@require_tf
	@unittest.skip("Visual question answering not implemented in TF"     )
	def 						UpperCAmelCase_   (				self    :       str     ) ->			Optional[Any]:
						pass
