




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        snake_case__				:
				"""simple docstring"""



				def __init__( self							:	str   , __lowerCamelCase							:	Union[str, Any]   , __lowerCamelCase							:	int=13   , __lowerCamelCase							:	Tuple=7   , __lowerCamelCase							:	Union[str, Any]=False   , __lowerCamelCase							:	List[Any]=True   , __lowerCamelCase							:	List[Any]=True   , __lowerCamelCase							:	List[str]=True   , __lowerCamelCase							:	Tuple=99   , __lowerCamelCase							:	Optional[Any]=32   , __lowerCamelCase							:	Optional[int]=5   , __lowerCamelCase							:	List[str]=4   , __lowerCamelCase							:	List[Any]=37   , __lowerCamelCase							:	Tuple="gelu"   , __lowerCamelCase							:	Optional[int]=0.1   , __lowerCamelCase							:	int=0.1   , __lowerCamelCase							:	int=5_12   , __lowerCamelCase							:	int=16   , __lowerCamelCase							:	Optional[Any]=2   , __lowerCamelCase							:	Union[str, Any]=0.02   , __lowerCamelCase							:	int=3   , __lowerCamelCase							:	Dict=4   , __lowerCamelCase							:	Tuple=None   , __lowerCamelCase							:	Dict=0   , )   ->	Any:
							a       =     parent
							a       =     batch_size
							a       =     seq_length
							a       =     is_training
							a       =     use_input_mask
							a       =     use_token_type_ids
							a       =     use_labels
							a       =     vocab_size
							a       =     hidden_size
							a       =     num_hidden_layers
							a       =     num_attention_heads
							a       =     intermediate_size
							a       =     hidden_act
							a       =     hidden_dropout_prob
							a       =     attention_probs_dropout_prob
							a       =     max_position_embeddings
							a       =     type_vocab_size
							a       =     type_sequence_label_size
							a       =     initializer_range
							a       =     num_labels
							a       =     num_choices
							a       =     scope
							a       =     projection_dim





				def 				__UpperCAmelCase				( self							:	Optional[Any]				)   ->	Optional[int]:
							a       =     ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size				)

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

							a       =     None
							if self.use_token_type_ids:
										a       =     ids_tensor([self.batch_size, self.seq_length]   , self.type_vocab_size				)

							a       =     None
							a       =     None
							a       =     None
							if self.use_labels:
										a       =     ids_tensor([self.batch_size]   , self.type_sequence_label_size				)
										a       =     ids_tensor([self.batch_size, self.seq_length]   , self.num_labels				)
										a       =     ids_tensor([self.batch_size]   , self.num_choices				)

							a       =     self.get_config()

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





				def 				__UpperCAmelCase				( self							:	Optional[int]				)   ->	Optional[Any]:
							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 				__UpperCAmelCase				( self							:	List[str]   , __lowerCamelCase							:	List[Any]   , __lowerCamelCase							:	Optional[int]   , __lowerCamelCase							:	List[Any]   , __lowerCamelCase							:	Optional[int]   , __lowerCamelCase							:	Optional[Any]   , __lowerCamelCase							:	str   , __lowerCamelCase							:	str				)   ->	Tuple:
							a       =     DPRContextEncoder(config=__UpperCAmelCase				)
							model.to(__UpperCAmelCase				)
							model.eval()
							a       =     model(__UpperCAmelCase   , attention_mask=__UpperCAmelCase   , token_type_ids=__UpperCAmelCase				)
							a       =     model(__UpperCAmelCase   , token_type_ids=__UpperCAmelCase				)
							a       =     model(__UpperCAmelCase				)
							self.parent.assertEqual(result.pooler_output.shape   , (self.batch_size, self.projection_dim or self.hidden_size)				)





				def 				__UpperCAmelCase				( self							:	Optional[int]   , __lowerCamelCase							:	List[str]   , __lowerCamelCase							:	int   , __lowerCamelCase							:	Union[str, Any]   , __lowerCamelCase							:	int   , __lowerCamelCase							:	Optional[Any]   , __lowerCamelCase							:	str   , __lowerCamelCase							:	Optional[int]				)   ->	int:
							a       =     DPRQuestionEncoder(config=__UpperCAmelCase				)
							model.to(__UpperCAmelCase				)
							model.eval()
							a       =     model(__UpperCAmelCase   , attention_mask=__UpperCAmelCase   , token_type_ids=__UpperCAmelCase				)
							a       =     model(__UpperCAmelCase   , token_type_ids=__UpperCAmelCase				)
							a       =     model(__UpperCAmelCase				)
							self.parent.assertEqual(result.pooler_output.shape   , (self.batch_size, self.projection_dim or self.hidden_size)				)





				def 				__UpperCAmelCase				( self							:	Any   , __lowerCamelCase							:	int   , __lowerCamelCase							:	int   , __lowerCamelCase							:	Tuple   , __lowerCamelCase							:	int   , __lowerCamelCase							:	int   , __lowerCamelCase							:	Tuple   , __lowerCamelCase							:	Optional[int]				)   ->	Tuple:
							a       =     DPRReader(config=__UpperCAmelCase				)
							model.to(__UpperCAmelCase				)
							model.eval()
							a       =     model(
							    __UpperCAmelCase   , attention_mask=__UpperCAmelCase   , )

							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 				__UpperCAmelCase				( self							:	Union[str, Any]				)   ->	Tuple:
							a       =     self.prepare_config_and_inputs()
							(
							    (
							    a				
							)       ,						(
							    a				
							)       ,						(
							    a				
							)       ,						(
							    a				
							)       ,						(
							    a				
							)       ,						(
							    a				
							)       ,						(
							    a				
							)       ,										
							)       =     config_and_inputs
							a       =     {"input_ids": input_ids}
							return config, inputs_dict




@require_torch
class        snake_case__				(_UpperCamelCase       ,			_UpperCamelCase       ,			unittest.TestCase  ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_			:				Tuple									=   (
				    (
				        DPRContextEncoder,
				        DPRQuestionEncoder,
				        DPRReader,
				    )
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE_			:				Any									=   {"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

				SCREAMING_SNAKE_CASE_			:				int									=   False
				SCREAMING_SNAKE_CASE_			:				Any									=   False  # why?
				SCREAMING_SNAKE_CASE_			:				Union[str, Any]									=   False
				SCREAMING_SNAKE_CASE_			:				int									=   False





				def 				__UpperCAmelCase				( self							:	Dict				)   ->	Dict:
							a       =     DPRModelTester(self				)
							a       =     ConfigTester(self   , config_class=__UpperCAmelCase   , hidden_size=37				)





				def 				__UpperCAmelCase				( self							:	str				)   ->	Optional[Any]:
							self.config_tester.run_common_tests()





				def 				__UpperCAmelCase				( self							:	List[str]				)   ->	Union[str, Any]:
							a       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_context_encoder(*__UpperCAmelCase				)





				def 				__UpperCAmelCase				( self							:	int				)   ->	List[Any]:
							a       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_question_encoder(*__UpperCAmelCase				)





				def 				__UpperCAmelCase				( self							:	List[Any]				)   ->	Optional[Any]:
							a       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_reader(*__UpperCAmelCase				)





				def 				__UpperCAmelCase				( self							:	List[Any]				)   ->	List[str]:
							a       =     self.model_tester.prepare_config_and_inputs()[0]

							a       =     DPRQuestionEncoder(config=__UpperCAmelCase				)
							model.to(__UpperCAmelCase				)
							model.eval()

							with tempfile.TemporaryDirectory() as tmp_dirname:
										model.save_pretrained(__UpperCAmelCase				)
										a       =     DPRQuestionEncoder.from_pretrained(__UpperCAmelCase   , projection_dim=5_12				)

							self.assertIsNotNone(__UpperCAmelCase				)





				@slow
				def 				__UpperCAmelCase				( self							:	Tuple				)   ->	Dict:
							for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a       =     DPRContextEncoder.from_pretrained(__UpperCAmelCase				)
										self.assertIsNotNone(__UpperCAmelCase				)

							for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a       =     DPRContextEncoder.from_pretrained(__UpperCAmelCase				)
										self.assertIsNotNone(__UpperCAmelCase				)

							for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a       =     DPRQuestionEncoder.from_pretrained(__UpperCAmelCase				)
										self.assertIsNotNone(__UpperCAmelCase				)

							for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										a       =     DPRReader.from_pretrained(__UpperCAmelCase				)
										self.assertIsNotNone(__UpperCAmelCase				)




@require_torch
class        snake_case__				(unittest.TestCase  ):
				"""simple docstring"""



				@slow
				def 				__UpperCAmelCase				( self							:	Tuple				)   ->	Union[str, Any]:
							a       =     DPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"   , return_dict=__UpperCAmelCase				)
							model.to(__UpperCAmelCase				)

							a       =     torch.tensor(
							    [[1_01, 75_92, 10_10, 20_03, 20_26, 38_99, 1_01_40, 10_29, 1_02]]   , dtype=torch.long   , device=__UpperCAmelCase				)  # [CLS] hello, is my dog cute? [SEP]
							a       =     model(__UpperCAmelCase				)[0]  # embedding shape = (1, 768)
							# compare the actual values for a slice.
							a       =     torch.tensor(
							    [
							        [
							            0.03_236_253,
							            0.12_753_335,
							            0.16_818_509,
							            0.00_279_786,
							            0.3_896_933,
							            0.24_264_945,
							            0.2_178_971,
							            -0.02_335_227,
							            -0.08_481_959,
							            -0.14_324_117,
							        ]
							    ]   , dtype=torch.float   , device=__UpperCAmelCase   , )
							self.assertTrue(torch.allclose(output[:, :10]   , __UpperCAmelCase   , atol=1e-4				)				)





				@slow
				def 				__UpperCAmelCase				( self							:	str				)   ->	Tuple:
							a       =     DPRReaderTokenizer.from_pretrained("facebook/dpr-reader-single-nq-base"				)
							a       =     DPRReader.from_pretrained("facebook/dpr-reader-single-nq-base"				)
							model.to(__UpperCAmelCase				)

							a       =     tokenizer(
							    questions="What is love ?"   , titles="Haddaway"   , texts="What Is Love is a song recorded by the artist Haddaway"   , padding=__UpperCAmelCase   , return_tensors="pt"   , )
							encoded_inputs.to(__UpperCAmelCase				)

							a       =     model(**__UpperCAmelCase				)

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

							a       =     torch.tensor(
							    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.8_443, -11.99_59, -11.03_64, -10.00_96, -6.8_405]]   , dtype=torch.float   , device=__UpperCAmelCase   , )
							self.assertTrue(torch.allclose(outputs.start_logits[:, :10]   , __UpperCAmelCase   , atol=1e-4				)				)
							self.assertTrue(torch.allclose(outputs.end_logits[:, :10]   , __UpperCAmelCase   , atol=1e-4				)				)








"""simple docstring"""


import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor







class 						lowerCamelCase__   (   unittest.TestCase							):





			'''simple docstring'''



			@property
			def    _lowerCAmelCase						(				self    )					->	List[str]:
				_lowerCAmelCase								=1
				_lowerCAmelCase								=3
				_lowerCAmelCase								=8
				_lowerCAmelCase								=8

				_lowerCAmelCase								=torch.rand((batch_size, num_channels, height, width)    )

				return sample



			def    _lowerCAmelCase						(				self	,	__UpperCAmelCase    )					->	Any:
				if isinstance(image[0]	,	PIL.Image.Image    ):
					return np.stack([np.array(__UpperCAmelCase    ) for i in image]	,	axis=0    )
				elif isinstance(__UpperCAmelCase	,	torch.Tensor    ):
					return image.cpu().numpy().transpose(0	,	2	,	3	,	1    )
				return image



			def    _lowerCAmelCase						(				self    )					->	Tuple:
				_lowerCAmelCase								=VaeImageProcessor(do_resize=__UpperCAmelCase	,	do_normalize=__UpperCAmelCase    )

				_lowerCAmelCase								=self.dummy_sample
				_lowerCAmelCase								=self.to_np(__UpperCAmelCase    )

				for output_type in ["pt", "np", "pil"]:
					_lowerCAmelCase								=image_processor.postprocess(
					    image_processor.preprocess(__UpperCAmelCase    )	,	output_type=__UpperCAmelCase	,	)
					_lowerCAmelCase								=self.to_np(__UpperCAmelCase    )
					_lowerCAmelCase								=(input_np * 2_55).round() if output_type == """pil""" else input_np
					assert (
					    np.abs(in_np - out_np    ).max() < 1e-6
					), f'''decoded output does not match input for output_type {output_type}'''



			def    _lowerCAmelCase						(				self    )					->	Union[str, Any]:
				_lowerCAmelCase								=VaeImageProcessor(do_resize=__UpperCAmelCase	,	do_normalize=__UpperCAmelCase    )
				_lowerCAmelCase								=self.dummy_sample.cpu().numpy().transpose(0	,	2	,	3	,	1    )

				for output_type in ["pt", "np", "pil"]:
					_lowerCAmelCase								=image_processor.postprocess(image_processor.preprocess(__UpperCAmelCase    )	,	output_type=__UpperCAmelCase    )

					_lowerCAmelCase								=self.to_np(__UpperCAmelCase    )
					_lowerCAmelCase								=(input_np * 2_55).round() if output_type == """pil""" else input_np
					assert (
					    np.abs(in_np - out_np    ).max() < 1e-6
					), f'''decoded output does not match input for output_type {output_type}'''



			def    _lowerCAmelCase						(				self    )					->	List[str]:
				_lowerCAmelCase								=VaeImageProcessor(do_resize=__UpperCAmelCase	,	do_normalize=__UpperCAmelCase    )

				_lowerCAmelCase								=self.dummy_sample.cpu().numpy().transpose(0	,	2	,	3	,	1    )
				_lowerCAmelCase								=image_processor.numpy_to_pil(__UpperCAmelCase    )

				for output_type in ["pt", "np", "pil"]:
					_lowerCAmelCase								=image_processor.postprocess(image_processor.preprocess(__UpperCAmelCase    )	,	output_type=__UpperCAmelCase    )
					for i, o in zip(__UpperCAmelCase	,	__UpperCAmelCase    ):
						_lowerCAmelCase								=np.array(__UpperCAmelCase    )
						_lowerCAmelCase								=self.to_np(__UpperCAmelCase    ) if output_type == """pil""" else (self.to_np(__UpperCAmelCase    ) * 2_55).round()
						assert (
						    np.abs(in_np - out_np    ).max() < 1e-6
						), f'''decoded output does not match input for output_type {output_type}'''



			def    _lowerCAmelCase						(				self    )					->	Tuple:
				_lowerCAmelCase								=VaeImageProcessor(do_resize=__UpperCAmelCase	,	do_normalize=__UpperCAmelCase    )

				_lowerCAmelCase								=self.dummy_sample
				_lowerCAmelCase								=input_pt_ad.squeeze(0    )

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)
				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				_lowerCAmelCase								=self.to_np(self.dummy_sample    )
				_lowerCAmelCase								=input_np_ad.squeeze(0    )

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)
				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				assert np.abs(out_pt_ad - out_pt_ad    ).max() < 1e-6
				assert np.abs(out_np_ad - out_np_ad    ).max() < 1e-6



			def    _lowerCAmelCase						(				self    )					->	Optional[Any]:
				_lowerCAmelCase								=VaeImageProcessor(do_resize=__UpperCAmelCase	,	do_normalize=__UpperCAmelCase    )

				_lowerCAmelCase								=self.dummy_sample
				_lowerCAmelCase								=list(__UpperCAmelCase    )

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				_lowerCAmelCase								=self.to_np(self.dummy_sample    )
				list(__UpperCAmelCase    )

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				_lowerCAmelCase								=image_processor.postprocess(
				    image_processor.preprocess(__UpperCAmelCase    )	,	output_type="""np"""	,	)

				assert np.abs(out_pt_ad - out_pt_list    ).max() < 1e-6
				assert np.abs(out_np_ad - out_np_list    ).max() < 1e-6



