



'''simple docstring'''




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 						_UpperCamelCase  :


			'''simple docstring'''

			def __init__(							self					:			Dict							,					lowerCAmelCase__					:			str							,					lowerCAmelCase__					:			Optional[Any]=1_3							,					lowerCAmelCase__					:			Any=7							,					lowerCAmelCase__					:			Dict=False							,					lowerCAmelCase__					:			Dict=True							,					lowerCAmelCase__					:			Optional[Any]=True							,					lowerCAmelCase__					:			List[str]=True							,					lowerCAmelCase__					:			Optional[int]=9_9							,					lowerCAmelCase__					:			Optional[int]=3_2							,					lowerCAmelCase__					:			Optional[Any]=5							,					lowerCAmelCase__					:			str=4							,					lowerCAmelCase__					:			int=3_7							,					lowerCAmelCase__					:			Dict="gelu"							,					lowerCAmelCase__					:			Tuple=0.1							,					lowerCAmelCase__					:			Optional[int]=0.1							,					lowerCAmelCase__					:			Union[str, Any]=5_1_2							,					lowerCAmelCase__					:			List[Any]=1_6							,					lowerCAmelCase__					:			List[Any]=2							,					lowerCAmelCase__					:			Any=0.02							,					lowerCAmelCase__					:			int=3							,					lowerCAmelCase__					:			Optional[Any]=4							,					lowerCAmelCase__					:			str=None							,					lowerCAmelCase__					:			int=0							,					):





					"""simple docstring"""






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

			def UpperCamelCase__ (							self					:			int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size						)

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

					__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		None
					if self.use_token_type_ids:
							__SCREAMING_SNAKE_CASE	:		str   	=		ids_tensor([self.batch_size, self.seq_length]							,					self.type_vocab_size						)

					__SCREAMING_SNAKE_CASE	:		List[str]   	=		None
					__SCREAMING_SNAKE_CASE	:		Any   	=		None
					__SCREAMING_SNAKE_CASE	:		str   	=		None
					if self.use_labels:
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		ids_tensor([self.batch_size]							,					self.type_sequence_label_size						)
							__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		ids_tensor([self.batch_size, self.seq_length]							,					self.num_labels						)
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		ids_tensor([self.batch_size]							,					self.num_choices						)

					__SCREAMING_SNAKE_CASE	:		Any   	=		self.get_config()

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

			def UpperCamelCase__ (							self					:			Optional[int]						):





					"""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 UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			Dict							,					lowerCAmelCase__					:			List[Any]							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			Dict							,					lowerCAmelCase__					:			List[str]							,					lowerCAmelCase__					:			str						):





					"""simple docstring"""






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

			def UpperCamelCase__ (							self					:			Any							,					lowerCAmelCase__					:			int							,					lowerCAmelCase__					:			Tuple							,					lowerCAmelCase__					:			Optional[Any]							,					lowerCAmelCase__					:			Union[str, Any]							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			List[Any]							,					lowerCAmelCase__					:			str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		int   	=		DPRQuestionEncoder(config=lowerCAmelCase__						)
					model.to(lowerCAmelCase__						)
					model.eval()
					__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		model(lowerCAmelCase__							,					attention_mask=lowerCAmelCase__							,					token_type_ids=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		int   	=		model(lowerCAmelCase__							,					token_type_ids=lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		model(lowerCAmelCase__						)
					self.parent.assertEqual(result.pooler_output.shape							,					(self.batch_size, self.projection_dim or self.hidden_size)						)

			def UpperCamelCase__ (							self					:			str							,					lowerCAmelCase__					:			Tuple							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			Tuple							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			Union[str, Any]							,					lowerCAmelCase__					:			str							,					lowerCAmelCase__					:			Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		DPRReader(config=lowerCAmelCase__						)
					model.to(lowerCAmelCase__						)
					model.eval()
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		model(
					    lowerCAmelCase__							,					attention_mask=lowerCAmelCase__							,					)

					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					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.prepare_config_and_inputs()
					(
					    (
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						(
					    __SCREAMING_SNAKE_CASE
					)	,						
					)	:		List[str]   	=		config_and_inputs
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		{"""input_ids""": input_ids}
					return config, inputs_dict






@require_torch
class 						_UpperCamelCase  (							lowerCamelCase__   , lowerCamelCase__   , unittest.TestCase   ):


			'''simple docstring'''

			_A				:		Union[str, Any]         =		(
			    (
			        DPRContextEncoder,
			        DPRQuestionEncoder,
			        DPRReader,
			    )
			    if is_torch_available()
			    else ()
			)
			_A				:		str         =		{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

			_A				:		str         =		False
			_A				:		Tuple         =		False  # why?
			_A				:		Optional[int]         =		False
			_A				:		str         =		False

			def UpperCamelCase__ (							self					:			List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		DPRModelTester(self						)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		ConfigTester(self							,					config_class=lowerCAmelCase__							,					hidden_size=3_7						)

			def UpperCamelCase__ (							self					:			Tuple						):





					"""simple docstring"""






					self.config_tester.run_common_tests()

			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_context_encoder(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_question_encoder(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_reader(*lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		self.model_tester.prepare_config_and_inputs()[0]

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		DPRQuestionEncoder(config=lowerCAmelCase__						)
					model.to(lowerCAmelCase__						)
					model.eval()

					with tempfile.TemporaryDirectory() as tmp_dirname:
							model.save_pretrained(lowerCAmelCase__						)
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		DPRQuestionEncoder.from_pretrained(lowerCAmelCase__							,					projection_dim=5_1_2						)

					self.assertIsNotNone(lowerCAmelCase__						)



			@slow
			def UpperCamelCase__ (							self					:			Union[str, Any]						):





					"""simple docstring"""






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

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

					for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE	:		int   	=		DPRQuestionEncoder.from_pretrained(lowerCAmelCase__						)
							self.assertIsNotNone(lowerCAmelCase__						)

					for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		DPRReader.from_pretrained(lowerCAmelCase__						)
							self.assertIsNotNone(lowerCAmelCase__						)






@require_torch
class 						_UpperCamelCase  (							unittest.TestCase   ):


			'''simple docstring'''

			@slow
			def UpperCamelCase__ (							self					:			str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Any   	=		DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""							,					return_dict=lowerCAmelCase__						)
					model.to(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE	:		Any   	=		torch.tensor(
					    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]							,					dtype=torch.long							,					device=lowerCAmelCase__						)  # [CLS] hello, is my dog cute? [SEP]
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		model(lowerCAmelCase__						)[0]  # embedding shape = (1, 768)
					# compare the actual values for a slice.
					__SCREAMING_SNAKE_CASE	:		int   	=		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=lowerCAmelCase__							,					)
					self.assertTrue(torch.allclose(output[:, :1_0]							,					lowerCAmelCase__							,					atol=1E-4						)						)



			@slow
			def UpperCamelCase__ (							self					:			str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		str   	=		DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
					__SCREAMING_SNAKE_CASE	:		Dict   	=		DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
					model.to(lowerCAmelCase__						)

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

					__SCREAMING_SNAKE_CASE	:		int   	=		model(**lowerCAmelCase__						)

					# compare the actual values for a slice.
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		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=lowerCAmelCase__							,					)

					__SCREAMING_SNAKE_CASE	:		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=lowerCAmelCase__							,					)
					self.assertTrue(torch.allclose(outputs.start_logits[:, :1_0]							,					lowerCAmelCase__							,					atol=1E-4						)						)
					self.assertTrue(torch.allclose(outputs.end_logits[:, :1_0]							,					lowerCAmelCase__							,					atol=1E-4						)						)




'''simple docstring'''




import torch
import torch.nn.functional as F

from diffusers import VQDiffusionScheduler

from .test_schedulers import SchedulerCommonTest






class 						_UpperCamelCase  (							lowerCamelCase__   ):


			'''simple docstring'''

			_A				:		int         =		(VQDiffusionScheduler,)

			def UpperCamelCase__ (							self					:			List[str]							,					**lowerCAmelCase__					:			Dict						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		int   	=		{
					    """num_vec_classes""": 4_0_9_7,
					    """num_train_timesteps""": 1_0_0,
					}

					config.update(**lowerCAmelCase__						)
					return config

			def UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		Dict   	=		4
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		8
					__SCREAMING_SNAKE_CASE	:		int   	=		8

					__SCREAMING_SNAKE_CASE	:		Dict   	=		torch.randint(0							,					lowerCAmelCase__							,					(batch_size, height * width)						)

					return sample

			@property
			def UpperCamelCase__ (							self					:			str						):





					"""simple docstring"""






					assert False

			def UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			str						):





					"""simple docstring"""






					def model(lowerCAmelCase__					:			Optional[Any]							,					lowerCAmelCase__					:			Any							,					*lowerCAmelCase__					:			Optional[int]						):
							__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		sample.shape
							__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		torch.rand((batch_size, num_vec_classes - 1, num_latent_pixels)						)
							__SCREAMING_SNAKE_CASE	:		str   	=		F.log_softmax(logits.double()							,					dim=1						).float()
							return return_value

					return model

			def UpperCamelCase__ (							self					:			Optional[int]						):





					"""simple docstring"""






					for timesteps in [2, 5, 1_0_0, 1_0_0_0]:
							self.check_over_configs(num_train_timesteps=lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			int						):





					"""simple docstring"""






					for num_vec_classes in [5, 1_0_0, 1_0_0_0, 4_0_0_0]:
							self.check_over_configs(num_vec_classes=lowerCAmelCase__						)

			def UpperCamelCase__ (							self					:			Any						):





					"""simple docstring"""






					for t in [0, 5_0, 9_9]:
							self.check_over_forward(time_step=lowerCAmelCase__						)



			def UpperCamelCase__ (							self					:			List[Any]						):





					"""simple docstring"""






					pass