




import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
							from transformers.models.dpr.modeling_dpr import (
							    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
							    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
							    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
							)


class 					__SCREAMING_SNAKE_CASE							:

					def __init__(    self			,	__lowercase			,	__lowercase=13			,	__lowercase=7			,	__lowercase=False			,	__lowercase=True			,	__lowercase=True			,	__lowercase=True			,	__lowercase=99			,	__lowercase=32			,	__lowercase=5			,	__lowercase=4			,	__lowercase=37			,	__lowercase="gelu"			,	__lowercase=0.1			,	__lowercase=0.1			,	__lowercase=512			,	__lowercase=16			,	__lowercase=2			,	__lowercase=0.02			,	__lowercase=3			,	__lowercase=4			,	__lowercase=None			,	__lowercase=0			,	):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[int]							       =							parent
						lowerCamelCase_							:		Dict							       =							batch_size
						lowerCamelCase_							:		Any							       =							seq_length
						lowerCamelCase_							:		Dict							       =							is_training
						lowerCamelCase_							:		Tuple							       =							use_input_mask
						lowerCamelCase_							:		str							       =							use_token_type_ids
						lowerCamelCase_							:		List[Any]							       =							use_labels
						lowerCamelCase_							:		Union[str, Any]							       =							vocab_size
						lowerCamelCase_							:		int							       =							hidden_size
						lowerCamelCase_							:		Dict							       =							num_hidden_layers
						lowerCamelCase_							:		List[str]							       =							num_attention_heads
						lowerCamelCase_							:		Tuple							       =							intermediate_size
						lowerCamelCase_							:		int							       =							hidden_act
						lowerCamelCase_							:		List[Any]							       =							hidden_dropout_prob
						lowerCamelCase_							:		int							       =							attention_probs_dropout_prob
						lowerCamelCase_							:		int							       =							max_position_embeddings
						lowerCamelCase_							:		Union[str, Any]							       =							type_vocab_size
						lowerCamelCase_							:		Optional[Any]							       =							type_sequence_label_size
						lowerCamelCase_							:		Dict							       =							initializer_range
						lowerCamelCase_							:		int							       =							num_labels
						lowerCamelCase_							:		str							       =							num_choices
						lowerCamelCase_							:		int							       =							scope
						lowerCamelCase_							:		Any							       =							projection_dim

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size )

						lowerCamelCase_							:		List[Any]							       =							None
						if self.use_input_mask:
							lowerCamelCase_							:		List[Any]							       =							random_attention_mask([self.batch_size, self.seq_length] )

						lowerCamelCase_							:		List[str]							       =							None
						if self.use_token_type_ids:
							lowerCamelCase_							:		Optional[Any]							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size )

						lowerCamelCase_							:		Dict							       =							None
						lowerCamelCase_							:		Tuple							       =							None
						lowerCamelCase_							:		Dict							       =							None
						if self.use_labels:
							lowerCamelCase_							:		str							       =							ids_tensor([self.batch_size]			,	self.type_sequence_label_size )
							lowerCamelCase_							:		Optional[int]							       =							ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels )
							lowerCamelCase_							:		int							       =							ids_tensor([self.batch_size]			,	self.num_choices )

						lowerCamelCase_							:		List[str]							       =							self.get_config()

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

					def      lowercase_							(    self ):







						'''simple docstring'''




						return DPRConfig(
						    projection_dim=self.projection_dim			,	vocab_size=self.vocab_size			,	hidden_size=self.hidden_size			,	num_hidden_layers=self.num_hidden_layers			,	num_attention_heads=self.num_attention_heads			,	intermediate_size=self.intermediate_size			,	hidden_act=self.hidden_act			,	hidden_dropout_prob=self.hidden_dropout_prob			,	attention_probs_dropout_prob=self.attention_probs_dropout_prob			,	max_position_embeddings=self.max_position_embeddings			,	type_vocab_size=self.type_vocab_size			,	initializer_range=self.initializer_range			,	)

					def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							DPRContextEncoder(config=__lowercase )
						model.to(__lowercase )
						model.eval()
						lowerCamelCase_							:		Union[str, Any]							       =							model(__lowercase			,	attention_mask=__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		Optional[int]							       =							model(__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		List[str]							       =							model(__lowercase )
						self.parent.assertEqual(result.pooler_output.shape			,	(self.batch_size, self.projection_dim or self.hidden_size) )

					def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							DPRQuestionEncoder(config=__lowercase )
						model.to(__lowercase )
						model.eval()
						lowerCamelCase_							:		Optional[Any]							       =							model(__lowercase			,	attention_mask=__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		List[Any]							       =							model(__lowercase			,	token_type_ids=__lowercase )
						lowerCamelCase_							:		Optional[Any]							       =							model(__lowercase )
						self.parent.assertEqual(result.pooler_output.shape			,	(self.batch_size, self.projection_dim or self.hidden_size) )

					def      lowercase_							(    self			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase			,	__lowercase ):







						'''simple docstring'''




						lowerCamelCase_							:		Any							       =							DPRReader(config=__lowercase )
						model.to(__lowercase )
						model.eval()
						lowerCamelCase_							:		str							       =							model(
						    __lowercase			,	attention_mask=__lowercase			,	)

						self.parent.assertEqual(result.start_logits.shape			,	(self.batch_size, self.seq_length) )
						self.parent.assertEqual(result.end_logits.shape			,	(self.batch_size, self.seq_length) )
						self.parent.assertEqual(result.relevance_logits.shape			,	(self.batch_size,) )





					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Union[str, Any]							       =							self.prepare_config_and_inputs()
						(
						    lowerCamelCase_      
						)							:		Tuple							       =							config_and_inputs
						lowerCamelCase_							:		Dict							       =							{'input_ids': input_ids}
						return config, inputs_dict


@require_torch
class 					__SCREAMING_SNAKE_CASE							(		lowerCamelCase       ,					lowerCamelCase       ,					unittest.TestCase  ):
					_lowerCamelCase		  =			(
					    (
					        DPRContextEncoder,
					        DPRQuestionEncoder,
					        DPRReader,
					    )
					    if is_torch_available()
					    else ()
					)
					_lowerCamelCase		  =			{"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

					_lowerCamelCase		  =			False
					_lowerCamelCase		  =			False  # why?
					_lowerCamelCase		  =			False
					_lowerCamelCase		  =			False

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							DPRModelTester(self )
						lowerCamelCase_							:		List[Any]							       =							ConfigTester(self			,	config_class=__lowercase			,	hidden_size=37 )

					def      lowercase_							(    self ):







						'''simple docstring'''




						self.config_tester.run_common_tests()

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[Any]							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		str							       =							self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*__lowercase )

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							       =							self.model_tester.prepare_config_and_inputs()[0]

						lowerCamelCase_							:		Dict							       =							DPRQuestionEncoder(config=__lowercase )
						model.to(__lowercase )
						model.eval()

						with tempfile.TemporaryDirectory() as tmp_dirname:
							model.save_pretrained(__lowercase )
							lowerCamelCase_							:		Union[str, Any]							       =							DPRQuestionEncoder.from_pretrained(__lowercase			,	projection_dim=512 )

						self.assertIsNotNone(__lowercase )





					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Tuple							       =							DPRContextEncoder.from_pretrained(__lowercase )
							self.assertIsNotNone(__lowercase )

						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Optional[int]							       =							DPRContextEncoder.from_pretrained(__lowercase )
							self.assertIsNotNone(__lowercase )

						for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Optional[Any]							       =							DPRQuestionEncoder.from_pretrained(__lowercase )
							self.assertIsNotNone(__lowercase )

						for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCamelCase_							:		Tuple							       =							DPRReader.from_pretrained(__lowercase )
							self.assertIsNotNone(__lowercase )


@require_torch
class 					__SCREAMING_SNAKE_CASE							(		unittest.TestCase  ):

					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							       =							DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'			,	return_dict=__lowercase )
						model.to(__lowercase )

						lowerCamelCase_							:		Any							       =							torch.tensor(
						    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]			,	dtype=torch.long			,	device=__lowercase )  # [CLS] hello, is my dog cute? [SEP]
						lowerCamelCase_							:		List[Any]							       =							model(__lowercase )[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						lowerCamelCase_							:		Any							       =							torch.tensor(
						    [
						        [
						            0.03_23_62_53,
						            0.12_75_33_35,
						            0.16_81_85_09,
						            0.00_27_97_86,
						            0.3_89_69_33,
						            0.24_26_49_45,
						            0.2_17_89_71,
						            -0.02_33_52_27,
						            -0.08_48_19_59,
						            -0.14_32_41_17,
						        ]
						    ]			,	dtype=torch.float			,	device=__lowercase			,	)
						self.assertTrue(torch.allclose(output[:, :10]			,	__lowercase			,	atol=1E-4 ) )





					@slow
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[Any]							       =							DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base' )
						lowerCamelCase_							:		List[str]							       =							DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base' )
						model.to(__lowercase )

						lowerCamelCase_							:		Any							       =							tokenizer(
						    questions='What is love ?'			,	titles='Haddaway'			,	texts='What Is Love is a song recorded by the artist Haddaway'			,	padding=__lowercase			,	return_tensors='pt'			,	)
						encoded_inputs.to(__lowercase )

						lowerCamelCase_							:		Dict							       =							model(**__lowercase )

						# compare the actual values for a slice.
						lowerCamelCase_							:		Dict							       =							torch.tensor(
						    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]			,	dtype=torch.float			,	device=__lowercase			,	)

						lowerCamelCase_							:		List[str]							       =							torch.tensor(
						    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]			,	dtype=torch.float			,	device=__lowercase			,	)
						self.assertTrue(torch.allclose(outputs.start_logits[:, :10]			,	__lowercase			,	atol=1E-4 ) )
						self.assertTrue(torch.allclose(outputs.end_logits[:, :10]			,	__lowercase			,	atol=1E-4 ) )






import random
import unittest

import numpy as np
import torch
from PIL import Image
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorEmbaEmbPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class     __SCREAMING_SNAKE_CASE							(		lowerCamelCase       ,					unittest.TestCase  ):
					_lowerCamelCase		  =			KandinskyVaaPriorEmbaEmbPipeline
					_lowerCamelCase		  =			["""prompt""", """image"""]
					_lowerCamelCase		  =			["""prompt""", """image"""]
					_lowerCamelCase		  =			[
					    """num_images_per_prompt""",
					    """strength""",
					    """generator""",
					    """num_inference_steps""",
					    """latents""",
					    """negative_prompt""",
					    """guidance_scale""",
					    """output_type""",
					    """return_dict""",
					]
					_lowerCamelCase		  =			False

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						return 32

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						return 32

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						return self.time_input_dim

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						return self.time_input_dim * 4

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						return 100

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							       =							CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' )
						return tokenizer

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						torch.manual_seed(0 )
						lowerCamelCase_							:		Dict							       =							CLIPTextConfig(
						    bos_token_id=0			,	eos_token_id=2			,	hidden_size=self.text_embedder_hidden_size			,	projection_dim=self.text_embedder_hidden_size			,	intermediate_size=37			,	layer_norm_eps=1E-05			,	num_attention_heads=4			,	num_hidden_layers=5			,	pad_token_id=1			,	vocab_size=1_000			,	)
						return CLIPTextModelWithProjection(__lowercase )

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						torch.manual_seed(0 )

						lowerCamelCase_							:		int							       =							{
						    'num_attention_heads': 2,
						    'attention_head_dim': 12,
						    'embedding_dim': self.text_embedder_hidden_size,
						    'num_layers': 1,
						}

						lowerCamelCase_							:		str							       =							PriorTransformer(**__lowercase )
						# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
						lowerCamelCase_							:		Dict							       =							nn.Parameter(torch.ones(model.clip_std.shape ) )
						return model

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						torch.manual_seed(0 )
						lowerCamelCase_							:		str							       =							CLIPVisionConfig(
						    hidden_size=self.text_embedder_hidden_size			,	image_size=224			,	projection_dim=self.text_embedder_hidden_size			,	intermediate_size=37			,	num_attention_heads=4			,	num_channels=3			,	num_hidden_layers=5			,	patch_size=14			,	)

						lowerCamelCase_							:		List[str]							       =							CLIPVisionModelWithProjection(__lowercase )
						return model

					@property
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							CLIPImageProcessor(
						    crop_size=224			,	do_center_crop=__lowercase			,	do_normalize=__lowercase			,	do_resize=__lowercase			,	image_mean=[0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73]			,	image_std=[0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11]			,	resample=3			,	size=224			,	)

						return image_processor

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		List[Any]							       =							self.dummy_prior
						lowerCamelCase_							:		str							       =							self.dummy_image_encoder
						lowerCamelCase_							:		Tuple							       =							self.dummy_text_encoder
						lowerCamelCase_							:		str							       =							self.dummy_tokenizer
						lowerCamelCase_							:		int							       =							self.dummy_image_processor

						lowerCamelCase_							:		Dict							       =							UnCLIPScheduler(
						    variance_type='fixed_small_log'			,	prediction_type='sample'			,	num_train_timesteps=1_000			,	clip_sample=__lowercase			,	clip_sample_range=10.0			,	)

						lowerCamelCase_							:		Dict							       =							{
						    'prior': prior,
						    'image_encoder': image_encoder,
						    'text_encoder': text_encoder,
						    'tokenizer': tokenizer,
						    'scheduler': scheduler,
						    'image_processor': image_processor,
						}

						return components

					def      lowercase_							(    self			,	__lowercase			,	__lowercase=0 ):







						'''simple docstring'''




						if str(__lowercase ).startswith('mps' ):
							lowerCamelCase_							:		Union[str, Any]							       =							torch.manual_seed(__lowercase )
						else:
							lowerCamelCase_							:		Optional[Any]							       =							torch.Generator(device=__lowercase ).manual_seed(__lowercase )

						lowerCamelCase_							:		Optional[int]							       =							floats_tensor((1, 3, 64, 64)			,	rng=random.Random(__lowercase ) ).to(__lowercase )
						lowerCamelCase_							:		Any							       =							image.cpu().permute(0			,	2			,	3			,	1 )[0]
						lowerCamelCase_							:		List[Any]							       =							Image.fromarray(np.uinta(__lowercase ) ).convert('RGB' ).resize((256, 256) )

						lowerCamelCase_							:		Tuple							       =							{
						    'prompt': 'horse',
						    'image': init_image,
						    'strength': 0.5,
						    'generator': generator,
						    'guidance_scale': 4.0,
						    'num_inference_steps': 2,
						    'output_type': 'np',
						}
						return inputs

					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		str							       =							'cpu'

						lowerCamelCase_							:		Union[str, Any]							       =							self.get_dummy_components()

						lowerCamelCase_							:		str							       =							self.pipeline_class(**__lowercase )
						lowerCamelCase_							:		Optional[Any]							       =							pipe.to(__lowercase )

						pipe.set_progress_bar_config(disable=__lowercase )

						lowerCamelCase_							:		List[str]							       =							pipe(**self.get_dummy_inputs(__lowercase ) )
						lowerCamelCase_							:		Union[str, Any]							       =							output.image_embeds

						lowerCamelCase_							:		List[str]							       =							pipe(
						    **self.get_dummy_inputs(__lowercase )			,	return_dict=__lowercase			,	)[0]

						lowerCamelCase_							:		List[Any]							       =							image[0, -10:]
						lowerCamelCase_							:		List[Any]							       =							image_from_tuple[0, -10:]

						assert image.shape == (1, 32)

						lowerCamelCase_							:		Any							       =							np.array(
						    [
						        0.1_07_12_84,
						        1.3_33_02_71,
						        0.61_26_02_23,
						        -0.6_69_10_65,
						        -0.3_84_68_52,
						        -1.0_30_36_61,
						        0.22_71_61_11,
						        0.03_34_89_01,
						        0.30_04_06_75,
						        -0.24_80_50_29,
						    ] )

						assert np.abs(image_slice.flatten() - expected_slice ).max() < 1E-2
						assert np.abs(image_from_tuple_slice.flatten() - expected_slice ).max() < 1E-2

					@skip_mps
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		int							       =							torch_device == 'cpu'
						lowerCamelCase_							:		Union[str, Any]							       =							True
						lowerCamelCase_							:		str							       =							False

						self._test_inference_batch_single_identical(
						    test_max_difference=__lowercase			,	relax_max_difference=__lowercase			,	test_mean_pixel_difference=__lowercase			,	)





					@skip_mps
					def      lowercase_							(    self ):







						'''simple docstring'''




						lowerCamelCase_							:		Optional[Any]							       =							torch_device == 'cpu'
						lowerCamelCase_							:		Optional[Any]							       =							False

						self._test_attention_slicing_forward_pass(
						    test_max_difference=__lowercase			,	test_mean_pixel_difference=__lowercase			,	)
