code: 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 					__lowercase					:
  
  
  
  		"""simple docstring"""
  
  		def __init__(   self		,			A		,			A=13		,			A=7		,			A=False		,			A=True		,			A=True		,			A=True		,			A=99		,			A=32		,			A=5		,			A=4		,			A=37		,			A="gelu"		,			A=0.1		,			A=0.1		,			A=5_12		,			A=16		,			A=2		,			A=0.02		,			A=3		,			A=4		,			A=None		,			A=0		,			)     ->   List[Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     parent
  					lowerCamelCase								=     batch_size
  					lowerCamelCase								=     seq_length
  					lowerCamelCase								=     is_training
  					lowerCamelCase								=     use_input_mask
  					lowerCamelCase								=     use_token_type_ids
  					lowerCamelCase								=     use_labels
  					lowerCamelCase								=     vocab_size
  					lowerCamelCase								=     hidden_size
  					lowerCamelCase								=     num_hidden_layers
  					lowerCamelCase								=     num_attention_heads
  					lowerCamelCase								=     intermediate_size
  					lowerCamelCase								=     hidden_act
  					lowerCamelCase								=     hidden_dropout_prob
  					lowerCamelCase								=     attention_probs_dropout_prob
  					lowerCamelCase								=     max_position_embeddings
  					lowerCamelCase								=     type_vocab_size
  					lowerCamelCase								=     type_sequence_label_size
  					lowerCamelCase								=     initializer_range
  					lowerCamelCase								=     num_labels
  					lowerCamelCase								=     num_choices
  					lowerCamelCase								=     scope
  					lowerCamelCase								=     projection_dim
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   str:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size  )
  
  					lowerCamelCase								=     None
  					if self.use_input_mask:
  								lowerCamelCase								=     random_attention_mask([self.batch_size, self.seq_length]  )
  
  					lowerCamelCase								=     None
  					if self.use_token_type_ids:
  								lowerCamelCase								=     ids_tensor([self.batch_size, self.seq_length]		,			self.type_vocab_size  )
  
  					lowerCamelCase								=     None
  					lowerCamelCase								=     None
  					lowerCamelCase								=     None
  					if self.use_labels:
  								lowerCamelCase								=     ids_tensor([self.batch_size]		,			self.type_sequence_label_size  )
  								lowerCamelCase								=     ids_tensor([self.batch_size, self.seq_length]		,			self.num_labels  )
  								lowerCamelCase								=     ids_tensor([self.batch_size]		,			self.num_choices  )
  
  					lowerCamelCase								=     self.get_config()
  
  					return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Optional[Any]:
  
  
  
  
  
  
  
  					'''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 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A  )     ->   List[str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRContextEncoder(config=A  )
  					model.to(A  )
  					model.eval()
  					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A  )
  					lowerCamelCase								=     model(A		,			token_type_ids=A  )
  					lowerCamelCase								=     model(A  )
  					self.parent.assertEqual(result.pooler_output.shape		,			(self.batch_size, self.projection_dim or self.hidden_size)  )
  
  
  
  
  
  
  
  		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A  )     ->   Union[str, Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRQuestionEncoder(config=A  )
  					model.to(A  )
  					model.eval()
  					lowerCamelCase								=     model(A		,			attention_mask=A		,			token_type_ids=A  )
  					lowerCamelCase								=     model(A		,			token_type_ids=A  )
  					lowerCamelCase								=     model(A  )
  					self.parent.assertEqual(result.pooler_output.shape		,			(self.batch_size, self.projection_dim or self.hidden_size)  )
  
  
  
  
  
  
  
  		def 				__A      (   self		,			A		,			A		,			A		,			A		,			A		,			A		,			A  )     ->   Tuple:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRReader(config=A  )
  					model.to(A  )
  					model.eval()
  					lowerCamelCase								=     model(
  					    A		,			attention_mask=A		,			)
  
  					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 				__A      (   self  )     ->   Dict:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     self.prepare_config_and_inputs()
  					(
  					    (
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,		(
  					    lowerCamelCase		
  					)		,				
  					)								=     config_and_inputs
  					lowerCamelCase								=     {"""input_ids""": input_ids}
  					return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class 					__lowercase					(       a_ ,       a_ ,       unittest.TestCase     ):
  
  
  
  		"""simple docstring"""
  
  		UpperCamelCase    :				List[str] 					=		(
  		    (
  		        DPRContextEncoder,
  		        DPRQuestionEncoder,
  		        DPRReader,
  		    )
  		    if is_torch_available()
  		    else ()
  		)
  		UpperCamelCase    :				Dict 					=		{"feature-extraction": DPRQuestionEncoder} if is_torch_available() else {}
  
  		UpperCamelCase    :				List[str] 					=		False
  		UpperCamelCase    :				Tuple 					=		False  # why?
  		UpperCamelCase    :				Optional[int] 					=		False
  		UpperCamelCase    :				Optional[int] 					=		False
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Optional[int]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRModelTester(self  )
  					lowerCamelCase								=     ConfigTester(self		,			config_class=A		,			hidden_size=37  )
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Tuple:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					self.config_tester.run_common_tests()
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Optional[Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
  					self.model_tester.create_and_check_context_encoder(*A  )
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Optional[int]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
  					self.model_tester.create_and_check_question_encoder(*A  )
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   Optional[Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()
  					self.model_tester.create_and_check_reader(*A  )
  
  
  
  
  
  
  
  		def 				__A      (   self  )     ->   List[str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     self.model_tester.prepare_config_and_inputs()[0]
  
  					lowerCamelCase								=     DPRQuestionEncoder(config=A  )
  					model.to(A  )
  					model.eval()
  
  					with tempfile.TemporaryDirectory() as tmp_dirname:
  								model.save_pretrained(A  )
  								lowerCamelCase								=     DPRQuestionEncoder.from_pretrained(A		,			projection_dim=5_12  )
  
  					self.assertIsNotNone(A  )
  
  
  
  
  
  
  
  		@slow
  		def 				__A      (   self  )     ->   List[str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  								lowerCamelCase								=     DPRContextEncoder.from_pretrained(A  )
  								self.assertIsNotNone(A  )
  
  					for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  								lowerCamelCase								=     DPRContextEncoder.from_pretrained(A  )
  								self.assertIsNotNone(A  )
  
  					for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  								lowerCamelCase								=     DPRQuestionEncoder.from_pretrained(A  )
  								self.assertIsNotNone(A  )
  
  					for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
  								lowerCamelCase								=     DPRReader.from_pretrained(A  )
  								self.assertIsNotNone(A  )
  
  
  
  
  
  
  
  @require_torch
  class 					__lowercase					(       unittest.TestCase     ):
  
  
  
  		"""simple docstring"""
  
  		@slow
  		def 				__A      (   self  )     ->   Optional[Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""		,			return_dict=A  )
  					model.to(A  )
  
  					lowerCamelCase								=     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=A  )  # [CLS] hello, is my dog cute? [SEP]
  					lowerCamelCase								=     model(A  )[0]  # embedding shape = (1, 768)
  					# compare the actual values for a slice.
  					lowerCamelCase								=     torch.tensor(
  					    [
  					        [
  					            0.03236253,
  					            0.12753335,
  					            0.16818509,
  					            0.00279786,
  					            0.3896933,
  					            0.24264945,
  					            0.2178971,
  					            -0.02335227,
  					            -0.08481959,
  					            -0.14324117,
  					        ]
  					    ]		,			dtype=torch.float		,			device=A		,			)
  					self.assertTrue(torch.allclose(output[:, :10]		,			A		,			atol=1e-4  )  )
  
  
  
  
  
  
  
  		@slow
  		def 				__A      (   self  )     ->   Tuple:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""  )
  					lowerCamelCase								=     DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""  )
  					model.to(A  )
  
  					lowerCamelCase								=     tokenizer(
  					    questions="""What is love ?"""		,			titles="""Haddaway"""		,			texts="""What Is Love is a song recorded by the artist Haddaway"""		,			padding=A		,			return_tensors="""pt"""		,			)
  					encoded_inputs.to(A  )
  
  					lowerCamelCase								=     model(**A  )
  
  					# compare the actual values for a slice.
  					lowerCamelCase								=     torch.tensor(
  					    [[-10.3005, -10.7765, -11.4872, -11.6841, -11.9312, -10.3002, -9.8544, -11.7378, -12.0821, -10.2975]]		,			dtype=torch.float		,			device=A		,			)
  
  					lowerCamelCase								=     torch.tensor(
  					    [[-11.0684, -11.7041, -11.5397, -10.3465, -10.8791, -6.8443, -11.9959, -11.0364, -10.0096, -6.8405]]		,			dtype=torch.float		,			device=A		,			)
  					self.assertTrue(torch.allclose(outputs.start_logits[:, :10]		,			A		,			atol=1e-4  )  )
  					self.assertTrue(torch.allclose(outputs.end_logits[:, :10]		,			A		,			atol=1e-4  )  )
  
  
style_context: 
  
  
  import abc
  import copy
  import dataclasses
  from dataclasses import dataclass
  from typing import ClassVar, Dict, Type, TypeVar
  
  from ..features import Features
  
  
  UpperCAmelCase      :						List[Any]    =		TypeVar("T", bound="TaskTemplate")
  
  
  
  
  
  
  
  @dataclass(frozen=a_     )
  class 					__lowercase					(       abc.ABC     ):
  
  
  
  		"""simple docstring"""
  
  		UpperCamelCase    :				str
  		UpperCamelCase    :				ClassVar[Features]
  		UpperCamelCase    :				ClassVar[Features]
  
  
  
  
  
  
  
  		def 				__A      (   self		,			A  )     ->   T:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					return copy.deepcopy(self  )
  
  
  
  
  
  
  
  		@property
  		def 				__A      (   self  )     ->   Features:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					return Features(**self.input_schema		,			**self.label_schema  )
  
  
  
  
  
  
  
  		@property
  		@abc.abstractmethod
  		def 				__A      (   self  )     ->   Dict[str, str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					raise NotImplementedError
  
  
  
  
  
  
  
  		@classmethod
  		def 				__A      (   cls		,			A  )     ->   T:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  
  
  
  
  					lowerCamelCase								=     {f.name for f in dataclasses.fields(cls  )}
  					return cls(**{k: v for k, v in template_dict.items() if k in field_names}  )
  
  