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



   def __init__(    self					,      lowerCAmelCase__					,      lowerCAmelCase__=13					,      lowerCAmelCase__=7					,      lowerCAmelCase__=False					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=True					,      lowerCAmelCase__=99					,      lowerCAmelCase__=32					,      lowerCAmelCase__=5					,      lowerCAmelCase__=4					,      lowerCAmelCase__=37					,      lowerCAmelCase__="gelu"					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=0.1					,      lowerCAmelCase__=512					,      lowerCAmelCase__=16					,      lowerCAmelCase__=2					,      lowerCAmelCase__=0.02					,      lowerCAmelCase__=3					,      lowerCAmelCase__=4					,      lowerCAmelCase__=None					,      lowerCAmelCase__=0					,      )				->     str:
        """simple docstring"""





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



   def  _UpperCamelCase		(    self     )				->     Union[str, Any]:
        """simple docstring"""





        _UpperCamelCase  :Any       =ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     )

        _UpperCamelCase  :Optional[int]       =None
        if self.use_input_mask:
             _UpperCamelCase  :Optional[Any]       =random_attention_mask([self.batch_size, self.seq_length]     )

        _UpperCamelCase  :int       =None
        if self.use_token_type_ids:
             _UpperCamelCase  :Optional[Any]       =ids_tensor([self.batch_size, self.seq_length]					,      self.type_vocab_size     )

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

        _UpperCamelCase  :Any       =self.get_config()

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



   def  _UpperCamelCase		(    self     )				->     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  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =DPRContextEncoder(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :Optional[Any]       =model(lowerCamelCase__					,      attention_mask=lowerCamelCase__					,      token_type_ids=lowerCamelCase__     )
        _UpperCamelCase  :Dict       =model(lowerCamelCase__					,      token_type_ids=lowerCamelCase__     )
        _UpperCamelCase  :Optional[Any]       =model(lowerCamelCase__     )
        self.parent.assertEqual(result.pooler_output.shape					,      (self.batch_size, self.projection_dim or self.hidden_size)     )



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )				->     str:
        """simple docstring"""





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



   def  _UpperCamelCase		(    self					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__					,      lowerCAmelCase__     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase  :Dict       =DPRReader(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()
        _UpperCamelCase  :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     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :List[str]       =self.prepare_config_and_inputs()
        (
            _UpperCamelCase							
        )  :List[str]       =config_and_inputs
        _UpperCamelCase  :List[Any]       ={'input_ids': input_ids}
        return config, inputs_dict




@require_torch
class    lowerCamelCase__  (   __snake_case       ,				__snake_case       ,				unittest.TestCase     ):
   __UpperCAmelCase           =						(
       (
           DPRContextEncoder,
           DPRQuestionEncoder,
           DPRReader,
       )
       if is_torch_available()
       else ()
   )
   __UpperCAmelCase           =						{"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False  # why?
   __UpperCAmelCase           =						False
   __UpperCAmelCase           =						False



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase  :int       =DPRModelTester(self     )
        _UpperCamelCase  :Optional[Any]       =ConfigTester(self					,      config_class=lowerCamelCase__					,      hidden_size=37     )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        self.config_tester.run_common_tests()



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        _UpperCamelCase  :Any       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_context_encoder(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        _UpperCamelCase  :Optional[int]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_question_encoder(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        _UpperCamelCase  :List[Any]       =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_reader(*lowerCamelCase__     )



   def  _UpperCamelCase		(    self     )				->     Any:
        """simple docstring"""





        _UpperCamelCase  :int       =self.model_tester.prepare_config_and_inputs()[0]

        _UpperCamelCase  :Union[str, Any]       =DPRQuestionEncoder(config=lowerCamelCase__     )
        model.to(lowerCamelCase__     )
        model.eval()

        with tempfile.TemporaryDirectory() as tmp_dirname:
             model.save_pretrained(lowerCamelCase__     )
             _UpperCamelCase  :List[str]       =DPRQuestionEncoder.from_pretrained(lowerCamelCase__					,      projection_dim=512     )

        self.assertIsNotNone(lowerCamelCase__     )




   @slow
   def  _UpperCamelCase		(    self     )				->     Optional[Any]:
        """simple docstring"""





        for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCamelCase  :Any       =DPRContextEncoder.from_pretrained(lowerCamelCase__     )
             self.assertIsNotNone(lowerCamelCase__     )

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

        for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCamelCase  :List[Any]       =DPRQuestionEncoder.from_pretrained(lowerCamelCase__     )
             self.assertIsNotNone(lowerCamelCase__     )

        for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _UpperCamelCase  :List[Any]       =DPRReader.from_pretrained(lowerCamelCase__     )
             self.assertIsNotNone(lowerCamelCase__     )




@require_torch
class    lowerCamelCase__  (   unittest.TestCase     ):



   @slow
   def  _UpperCamelCase		(    self     )				->     Dict:
        """simple docstring"""





        _UpperCamelCase  :Tuple       =DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""					,      return_dict=lowerCamelCase__     )
        model.to(lowerCamelCase__     )

        _UpperCamelCase  :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=lowerCamelCase__     )  # [CLS] hello, is my dog cute? [SEP]
        _UpperCamelCase  :Tuple       =model(lowerCamelCase__     )[0]  # embedding shape = (1, 768)
        # compare the actual values for a slice.
        _UpperCamelCase  :Optional[Any]       =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=lowerCamelCase__					,      )
        self.assertTrue(torch.allclose(output[:, :10]					,      lowerCamelCase__					,      atol=1e-4     )     )




   @slow
   def  _UpperCamelCase		(    self     )				->     List[str]:
        """simple docstring"""





        _UpperCamelCase  :str       =DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""     )
        _UpperCamelCase  :List[str]       =DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""     )
        model.to(lowerCamelCase__     )

        _UpperCamelCase  :Union[str, Any]       =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__     )

        _UpperCamelCase  :List[str]       =model(**lowerCamelCase__     )

        # compare the actual values for a slice.
        _UpperCamelCase  :Any       =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=lowerCamelCase__					,      )

        _UpperCamelCase  :int       =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=lowerCamelCase__					,      )
        self.assertTrue(torch.allclose(outputs.start_logits[:, :10]					,      lowerCamelCase__					,      atol=1e-4     )     )
        self.assertTrue(torch.allclose(outputs.end_logits[:, :10]					,      lowerCamelCase__					,      atol=1e-4     )     )







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A_			:Union[str, Any]	    =		{'''configuration_nat''': ['''NAT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''NatConfig''']}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_			:Optional[Any]	    =		[
							    '''NAT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''NatForImageClassification''',
							    '''NatModel''',
							    '''NatPreTrainedModel''',
							    '''NatBackbone''',
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							A_			:Tuple	    =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

