'''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     lowercase_		:

  '''simple docstring'''





  def __init__(       self				,	a_				,	a_=1_3				,	a_=7				,	a_=False				,	a_=True				,	a_=True				,	a_=True				,	a_=9_9				,	a_=3_2				,	a_=5				,	a_=4				,	a_=3_7				,	a_="gelu"				,	a_=0.1				,	a_=0.1				,	a_=5_1_2				,	a_=1_6				,	a_=2				,	a_=0.02				,	a_=3				,	a_=4				,	a_=None				,	a_=0				,	)							->			str:


    """simple docstring"""
    UpperCAmelCase  							=		parent
    UpperCAmelCase  							=		batch_size
    UpperCAmelCase  							=		seq_length
    UpperCAmelCase  							=		is_training
    UpperCAmelCase  							=		use_input_mask
    UpperCAmelCase  							=		use_token_type_ids
    UpperCAmelCase  							=		use_labels
    UpperCAmelCase  							=		vocab_size
    UpperCAmelCase  							=		hidden_size
    UpperCAmelCase  							=		num_hidden_layers
    UpperCAmelCase  							=		num_attention_heads
    UpperCAmelCase  							=		intermediate_size
    UpperCAmelCase  							=		hidden_act
    UpperCAmelCase  							=		hidden_dropout_prob
    UpperCAmelCase  							=		attention_probs_dropout_prob
    UpperCAmelCase  							=		max_position_embeddings
    UpperCAmelCase  							=		type_vocab_size
    UpperCAmelCase  							=		type_sequence_label_size
    UpperCAmelCase  							=		initializer_range
    UpperCAmelCase  							=		num_labels
    UpperCAmelCase  							=		num_choices
    UpperCAmelCase  							=		scope
    UpperCAmelCase  							=		projection_dim
  def   snake_case_     (       self       )							->			List[str]:


    """simple docstring"""
    UpperCAmelCase  							=		ids_tensor([self.batch_size, self.seq_length]				,	self.vocab_size       )

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

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

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

    UpperCAmelCase  							=		self.get_config()

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
  def   snake_case_     (       self       )							->			Union[str, 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   snake_case_     (       self				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_       )							->			Any:


    """simple docstring"""
    UpperCAmelCase  							=		DPRContextEncoder(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE				,	attention_mask=__SCREAMING_SNAKE_CASE				,	token_type_ids=__SCREAMING_SNAKE_CASE       )
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE				,	token_type_ids=__SCREAMING_SNAKE_CASE       )
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.pooler_output.shape				,	(self.batch_size, self.projection_dim or self.hidden_size)       )
  def   snake_case_     (       self				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_       )							->			Any:


    """simple docstring"""
    UpperCAmelCase  							=		DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE				,	attention_mask=__SCREAMING_SNAKE_CASE				,	token_type_ids=__SCREAMING_SNAKE_CASE       )
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE				,	token_type_ids=__SCREAMING_SNAKE_CASE       )
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.pooler_output.shape				,	(self.batch_size, self.projection_dim or self.hidden_size)       )
  def   snake_case_     (       self				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_       )							->			Tuple:


    """simple docstring"""
    UpperCAmelCase  							=		DPRReader(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    UpperCAmelCase  							=		model(
        __SCREAMING_SNAKE_CASE				,	attention_mask=__SCREAMING_SNAKE_CASE				,	)

    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   snake_case_     (       self       )							->			str:


    """simple docstring"""
    UpperCAmelCase  							=		self.prepare_config_and_inputs()
    (
        (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							, (
        UpperCAmelCase      
    )							,       
    )  							=		config_and_inputs
    UpperCAmelCase  							=		{'input_ids': input_ids}
    return config, inputs_dict




@require_torch
class     lowercase_		(						__lowerCAmelCase	,    __lowerCAmelCase	,    unittest.TestCase	):

  '''simple docstring'''





  __lowerCAmelCase   :	Optional[int]      					=				(
      (
          DPRContextEncoder,
          DPRQuestionEncoder,
          DPRReader,
      )
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase   :	Optional[Any]      					=				{'''feature-extraction''': DPRQuestionEncoder} if is_torch_available() else {}

  __lowerCAmelCase   :	Optional[Any]      					=				False
  __lowerCAmelCase   :	List[str]      					=				False  # why?
  __lowerCAmelCase   :	Optional[Any]      					=				False
  __lowerCAmelCase   :	Dict      					=				False
  def   snake_case_     (       self       )							->			Tuple:


    """simple docstring"""
    UpperCAmelCase  							=		DPRModelTester(self       )
    UpperCAmelCase  							=		ConfigTester(self				,	config_class=__SCREAMING_SNAKE_CASE				,	hidden_size=3_7       )
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    self.config_tester.run_common_tests()
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase  							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_context_encoder(*__SCREAMING_SNAKE_CASE       )
  def   snake_case_     (       self       )							->			str:


    """simple docstring"""
    UpperCAmelCase  							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_question_encoder(*__SCREAMING_SNAKE_CASE       )
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase  							=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_reader(*__SCREAMING_SNAKE_CASE       )
  def   snake_case_     (       self       )							->			Any:


    """simple docstring"""
    UpperCAmelCase  							=		self.model_tester.prepare_config_and_inputs()[0]

    UpperCAmelCase  							=		DPRQuestionEncoder(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()

    with tempfile.TemporaryDirectory() as tmp_dirname:
      model.save_pretrained(__SCREAMING_SNAKE_CASE       )
      UpperCAmelCase  							=		DPRQuestionEncoder.from_pretrained(__SCREAMING_SNAKE_CASE				,	projection_dim=5_1_2       )

    self.assertIsNotNone(__SCREAMING_SNAKE_CASE       )





  @slow
  def   snake_case_     (       self       )							->			Tuple:


    """simple docstring"""




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

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

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

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




@require_torch
class     lowercase_		(						unittest.TestCase	):

  '''simple docstring'''





  @slow
  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    UpperCAmelCase  							=		DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'				,	return_dict=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )

    UpperCAmelCase  							=		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=__SCREAMING_SNAKE_CASE       )  # [CLS] hello, is my dog cute? [SEP]
    UpperCAmelCase  							=		model(__SCREAMING_SNAKE_CASE       )[0]  # embedding shape = (1, 768)
    # compare the actual values for a slice.
    UpperCAmelCase  							=		torch.tensor(
        [
            [
                0.0323_6253,
                0.1275_3335,
                0.1681_8509,
                0.0027_9786,
                0.389_6933,
                0.2426_4945,
                0.217_8971,
                -0.0233_5227,
                -0.0848_1959,
                -0.1432_4117,
            ]
        ]				,	dtype=torch.float				,	device=__SCREAMING_SNAKE_CASE				,	)
    self.assertTrue(torch.allclose(output[:, :1_0]				,	__SCREAMING_SNAKE_CASE				,	atol=1E-4       )       )





  @slow
  def   snake_case_     (       self       )							->			Any:


    """simple docstring"""
    UpperCAmelCase  							=		DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'       )
    UpperCAmelCase  							=		DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'       )
    model.to(__SCREAMING_SNAKE_CASE       )

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

    UpperCAmelCase  							=		model(**__SCREAMING_SNAKE_CASE       )

    # compare the actual values for a slice.
    UpperCAmelCase  							=		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=__SCREAMING_SNAKE_CASE				,	)

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





'''simple docstring'''




def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				str  ):
  UpperCAmelCase  							=		str(SCREAMING_SNAKE_CASE  ).strip()
  if not bin_string:
    raise ValueError('Empty string was passed to the function'  )
  UpperCAmelCase  							=		bin_string[0] == '-'
  if is_negative:
    UpperCAmelCase  							=		bin_string[1:]
  if not all(char in '01' for char in bin_string  ):
    raise ValueError('Non-binary value was passed to the function'  )
  UpperCAmelCase  							=		0
  for char in bin_string:
    UpperCAmelCase  							=		2 * decimal_number + int(SCREAMING_SNAKE_CASE  )
  return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
  from doctest import testmod

  testmod()


