

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






						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.0_2       ,     _lowercase=3       ,     _lowercase=4       ,     _lowercase=None       ,     _lowercase=0       ,     )      ->				str:
									a_		:		Any				    =				parent
									a_		:		int				    =				batch_size
									a_		:		int				    =				seq_length
									a_		:		Union[str, Any]				    =				is_training
									a_		:		Tuple				    =				use_input_mask
									a_		:		List[Any]				    =				use_token_type_ids
									a_		:		List[Any]				    =				use_labels
									a_		:		int				    =				vocab_size
									a_		:		Dict				    =				hidden_size
									a_		:		List[str]				    =				num_hidden_layers
									a_		:		int				    =				num_attention_heads
									a_		:		Optional[Any]				    =				intermediate_size
									a_		:		List[str]				    =				hidden_act
									a_		:		Dict				    =				hidden_dropout_prob
									a_		:		Union[str, Any]				    =				attention_probs_dropout_prob
									a_		:		Dict				    =				max_position_embeddings
									a_		:		List[Any]				    =				type_vocab_size
									a_		:		Union[str, Any]				    =				type_sequence_label_size
									a_		:		Any				    =				initializer_range
									a_		:		Dict				    =				num_labels
									a_		:		Any				    =				num_choices
									a_		:		List[Any]				    =				scope
									a_		:		Optional[Any]				    =				projection_dim





						def 	UpperCamelCase__			(  self  )      ->				Optional[int]:
									a_		:		List[str]				    =				ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size  )

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

									a_		:		Tuple				    =				None
									if self.use_token_type_ids:
												a_		:		List[str]				    =				ids_tensor([self.batch_size, self.seq_length]       ,     self.type_vocab_size  )

									a_		:		Dict				    =				None
									a_		:		int				    =				None
									a_		:		Any				    =				None
									if self.use_labels:
												a_		:		Any				    =				ids_tensor([self.batch_size]       ,     self.type_sequence_label_size  )
												a_		:		Dict				    =				ids_tensor([self.batch_size, self.seq_length]       ,     self.num_labels  )
												a_		:		List[str]				    =				ids_tensor([self.batch_size]       ,     self.num_choices  )

									a_		:		Dict				    =				self.get_config()

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





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





						def 	UpperCamelCase__			(  self       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase  )      ->				Dict:
									a_		:		Union[str, Any]				    =				DPRQuestionEncoder(config=_lowercase  )
									model.to(_lowercase  )
									model.eval()
									a_		:		Dict				    =				model(_lowercase       ,     attention_mask=_lowercase       ,     token_type_ids=_lowercase  )
									a_		:		Union[str, Any]				    =				model(_lowercase       ,     token_type_ids=_lowercase  )
									a_		:		Optional[Any]				    =				model(_lowercase  )
									self.parent.assertEqual(result.pooler_output.shape       ,     (self.batch_size, self.projection_dim or self.hidden_size)  )





						def 	UpperCamelCase__			(  self       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase       ,     _lowercase  )      ->				Dict:
									a_		:		Any				    =				DPRReader(config=_lowercase  )
									model.to(_lowercase  )
									model.eval()
									a_		:		List[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 	UpperCamelCase__			(  self  )      ->				Union[str, Any]:
									a_		:		Tuple				    =				self.prepare_config_and_inputs()
									(
									    a_			
									)		:		int				    =				config_and_inputs
									a_		:		Union[str, Any]				    =				{"""input_ids""": input_ids}
									return config, inputs_dict







@require_torch
class    A__(a_,       a_,       unittest.TestCase    ):
						"""simple docstring"""






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

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





						def 	UpperCamelCase__			(  self  )      ->				Dict:
									a_		:		List[str]				    =				DPRModelTester(self  )
									a_		:		Union[str, Any]				    =				ConfigTester(self       ,     config_class=_lowercase       ,     hidden_size=37  )





						def 	UpperCamelCase__			(  self  )      ->				str:
									self.config_tester.run_common_tests()





						def 	UpperCamelCase__			(  self  )      ->				str:
									a_		:		Tuple				    =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_context_encoder(*_lowercase  )





						def 	UpperCamelCase__			(  self  )      ->				str:
									a_		:		int				    =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_question_encoder(*_lowercase  )





						def 	UpperCamelCase__			(  self  )      ->				int:
									a_		:		Dict				    =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_reader(*_lowercase  )





						def 	UpperCamelCase__			(  self  )      ->				int:
									a_		:		int				    =				self.model_tester.prepare_config_and_inputs()[0]

									a_		:		Any				    =				DPRQuestionEncoder(config=_lowercase  )
									model.to(_lowercase  )
									model.eval()

									with tempfile.TemporaryDirectory() as tmp_dirname:
												model.save_pretrained(_lowercase  )
												a_		:		Optional[Any]				    =				DPRQuestionEncoder.from_pretrained(_lowercase       ,     projection_dim=512  )

									self.assertIsNotNone(_lowercase  )





						@slow
						def 	UpperCamelCase__			(  self  )      ->				int:
									for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_		:		List[str]				    =				DPRContextEncoder.from_pretrained(_lowercase  )
												self.assertIsNotNone(_lowercase  )

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

									for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_		:		Tuple				    =				DPRQuestionEncoder.from_pretrained(_lowercase  )
												self.assertIsNotNone(_lowercase  )

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







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






						@slow
						def 	UpperCamelCase__			(  self  )      ->				Tuple:
									a_		:		List[str]				    =				DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""       ,     return_dict=_lowercase  )
									model.to(_lowercase  )

									a_		:		Union[str, 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]
									a_		:		Optional[Any]				    =				model(_lowercase  )[0]  # embedding shape = (1, 768)
									# compare the actual values for a slice.
									a_		:		List[Any]				    =				torch.tensor(
									    [
									        [
									            0.0_3_2_3_6_2_5_3,
									            0.1_2_7_5_3_3_3_5,
									            0.1_6_8_1_8_5_0_9,
									            0.0_0_2_7_9_7_8_6,
									            0.3_8_9_6_9_3_3,
									            0.2_4_2_6_4_9_4_5,
									            0.2_1_7_8_9_7_1,
									            -0.0_2_3_3_5_2_2_7,
									            -0.0_8_4_8_1_9_5_9,
									            -0.1_4_3_2_4_1_1_7,
									        ]
									    ]       ,     dtype=torch.float       ,     device=_lowercase       ,     )
									self.assertTrue(torch.allclose(output[:, :10]       ,     _lowercase       ,     atol=1e-4  )  )





						@slow
						def 	UpperCamelCase__			(  self  )      ->				Any:
									a_		:		Union[str, Any]				    =				DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""  )
									a_		:		Union[str, Any]				    =				DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""  )
									model.to(_lowercase  )

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

									a_		:		Tuple				    =				model(**_lowercase  )

									# compare the actual values for a slice.
									a_		:		str				    =				torch.tensor(
									    [[-10.3_005, -10.7_765, -11.4_872, -11.6_841, -11.9_312, -10.3_002, -9.8_5_4_4, -11.7_378, -12.0_821, -10.2_975]]       ,     dtype=torch.float       ,     device=_lowercase       ,     )

									a_		:		Optional[int]				    =				torch.tensor(
									    [[-11.0_684, -11.7_041, -11.5_397, -10.3_465, -10.8_791, -6.8_4_4_3, -11.9_959, -11.0_364, -10.0_096, -6.8_4_0_5]]       ,     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  )  )









def 			_UpperCAmelCase							(		a__):

			'''simple docstring'''



			return 1 if input_a == 0 else 0


def 			_UpperCAmelCase							(		):

			'''simple docstring'''



			assert not_gate(0) == 1
			assert not_gate(1) == 0


if __name__ == "__main__":
	print(not_gate(0))
	print(not_gate(1))


