

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


     def __init__(							self    :	Any    ,						UpperCAmelCase    :	Optional[int]    ,						UpperCAmelCase    :	Dict=1_3    ,						UpperCAmelCase    :	Dict=7    ,						UpperCAmelCase    :	str=False    ,						UpperCAmelCase    :	List[Any]=True    ,						UpperCAmelCase    :	Any=True    ,						UpperCAmelCase    :	Optional[int]=True    ,						UpperCAmelCase    :	Any=9_9    ,						UpperCAmelCase    :	str=3_2    ,						UpperCAmelCase    :	int=5    ,						UpperCAmelCase    :	Dict=4    ,						UpperCAmelCase    :	Tuple=3_7    ,						UpperCAmelCase    :	Any="gelu"    ,						UpperCAmelCase    :	Optional[Any]=0.1    ,						UpperCAmelCase    :	str=0.1    ,						UpperCAmelCase    :	Tuple=5_1_2    ,						UpperCAmelCase    :	Dict=1_6    ,						UpperCAmelCase    :	Union[str, Any]=2    ,						UpperCAmelCase    :	Optional[int]=0.02    ,						UpperCAmelCase    :	Optional[int]=3    ,						UpperCAmelCase    :	Optional[Any]=4    ,						UpperCAmelCase    :	Union[str, Any]=None    ,						UpperCAmelCase    :	int=0    ,						)							->			Tuple:
      __lowerCAmelCase:     Union[str, Any]     			=   parent
      __lowerCAmelCase:     List[Any]     			=   batch_size
      __lowerCAmelCase:     Optional[Any]     			=   seq_length
      __lowerCAmelCase:     Union[str, Any]     			=   is_training
      __lowerCAmelCase:     Any     			=   use_input_mask
      __lowerCAmelCase:     Optional[Any]     			=   use_token_type_ids
      __lowerCAmelCase:     str     			=   use_labels
      __lowerCAmelCase:     Optional[int]     			=   vocab_size
      __lowerCAmelCase:     Tuple     			=   hidden_size
      __lowerCAmelCase:     int     			=   num_hidden_layers
      __lowerCAmelCase:     List[str]     			=   num_attention_heads
      __lowerCAmelCase:     Dict     			=   intermediate_size
      __lowerCAmelCase:     str     			=   hidden_act
      __lowerCAmelCase:     Dict     			=   hidden_dropout_prob
      __lowerCAmelCase:     Union[str, Any]     			=   attention_probs_dropout_prob
      __lowerCAmelCase:     Union[str, Any]     			=   max_position_embeddings
      __lowerCAmelCase:     Optional[int]     			=   type_vocab_size
      __lowerCAmelCase:     Tuple     			=   type_sequence_label_size
      __lowerCAmelCase:     List[Any]     			=   initializer_range
      __lowerCAmelCase:     List[str]     			=   num_labels
      __lowerCAmelCase:     str     			=   num_choices
      __lowerCAmelCase:     Optional[Any]     			=   scope
      __lowerCAmelCase:     Union[str, Any]     			=   projection_dim


     def 						UpperCAmelCase						(							self    :	Union[str, Any]      )							->			str:
      __lowerCAmelCase:     int     			=   ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size      )

      __lowerCAmelCase:     Union[str, Any]     			=   None
      if self.use_input_mask:
       __lowerCAmelCase:     Any     			=   random_attention_mask([self.batch_size, self.seq_length]      )

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

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

      __lowerCAmelCase:     str     			=   self.get_config()

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


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


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


     def 						UpperCAmelCase						(							self    :	str    ,						UpperCAmelCase    :	int    ,						UpperCAmelCase    :	List[Any]    ,						UpperCAmelCase    :	Any    ,						UpperCAmelCase    :	Optional[int]    ,						UpperCAmelCase    :	Any    ,						UpperCAmelCase    :	List[str]    ,						UpperCAmelCase    :	List[Any]      )							->			int:
      __lowerCAmelCase:     Optional[int]     			=   DPRReader(config=UpperCAmelCase      )
      model.to(UpperCAmelCase      )
      model.eval()
      __lowerCAmelCase:     Tuple     			=   model(
          UpperCAmelCase    ,						attention_mask=UpperCAmelCase    ,						)

      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[int]      )							->			Union[str, Any]:
      __lowerCAmelCase:     Any     			=   self.prepare_config_and_inputs()
      (
          (
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							(
          __lowerCAmelCase
      )			,							
      ):     int     			=   config_and_inputs
      __lowerCAmelCase:     List[Any]     			=   {'input_ids': input_ids}
      return config, inputs_dict







@require_torch
class 							A_				(	snake_case__							,			snake_case__							,			unittest.TestCase      ):
     _lowercase    :  Union[str, Any]          =			(
         (
             DPRContextEncoder,
             DPRQuestionEncoder,
             DPRReader,
         )
         if is_torch_available()
         else ()
     )
     _lowercase    :  Union[str, Any]          =			{'feature-extraction': DPRQuestionEncoder} if is_torch_available() else {}

     _lowercase    :  Optional[int]          =			False
     _lowercase    :  List[Any]          =			False  # why?
     _lowercase    :  Union[str, Any]          =			False
     _lowercase    :  List[Any]          =			False


     def 						UpperCAmelCase						(							self    :	List[Any]      )							->			Union[str, Any]:
      __lowerCAmelCase:     Dict     			=   DPRModelTester(self      )
      __lowerCAmelCase:     Optional[int]     			=   ConfigTester(self    ,						config_class=UpperCAmelCase    ,						hidden_size=3_7      )


     def 						UpperCAmelCase						(							self    :	Union[str, Any]      )							->			Dict:
      self.config_tester.run_common_tests()


     def 						UpperCAmelCase						(							self    :	Any      )							->			Any:
      __lowerCAmelCase:     List[str]     			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_context_encoder(*UpperCAmelCase      )


     def 						UpperCAmelCase						(							self    :	List[str]      )							->			Optional[int]:
      __lowerCAmelCase:     Optional[int]     			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_question_encoder(*UpperCAmelCase      )


     def 						UpperCAmelCase						(							self    :	str      )							->			Optional[Any]:
      __lowerCAmelCase:     List[str]     			=   self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_reader(*UpperCAmelCase      )


     def 						UpperCAmelCase						(							self    :	Any      )							->			int:
      __lowerCAmelCase:     Optional[int]     			=   self.model_tester.prepare_config_and_inputs()[0]

      __lowerCAmelCase:     List[str]     			=   DPRQuestionEncoder(config=UpperCAmelCase      )
      model.to(UpperCAmelCase      )
      model.eval()

      with tempfile.TemporaryDirectory() as tmp_dirname:
       model.save_pretrained(UpperCAmelCase      )
       __lowerCAmelCase:     Optional[Any]     			=   DPRQuestionEncoder.from_pretrained(UpperCAmelCase    ,						projection_dim=5_1_2      )

      self.assertIsNotNone(UpperCAmelCase      )


     @slow
     def 						UpperCAmelCase						(							self    :	List[str]      )							->			Any:
      for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCAmelCase:     List[str]     			=   DPRContextEncoder.from_pretrained(UpperCAmelCase      )
       self.assertIsNotNone(UpperCAmelCase      )

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

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

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







@require_torch
class 							A_				(	unittest.TestCase      ):


     @slow
     def 						UpperCAmelCase						(							self    :	Dict      )							->			Optional[Any]:
      __lowerCAmelCase:     Union[str, Any]     			=   DPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'    ,						return_dict=UpperCAmelCase      )
      model.to(UpperCAmelCase      )

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


     @slow
     def 						UpperCAmelCase						(							self    :	Tuple      )							->			List[str]:
      __lowerCAmelCase:     List[Any]     			=   DPRReaderTokenizer.from_pretrained('facebook/dpr-reader-single-nq-base'      )
      __lowerCAmelCase:     int     			=   DPRReader.from_pretrained('facebook/dpr-reader-single-nq-base'      )
      model.to(UpperCAmelCase      )

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

      __lowerCAmelCase:     Union[str, Any]     			=   model(**UpperCAmelCase      )

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

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








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_keras_nlp_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_a	 =     {
    '''configuration_gpt2''': ['''GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''GPT2Config''', '''GPT2OnnxConfig'''],
    '''tokenization_gpt2''': ['''GPT2Tokenizer'''],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _a	 =     ['''GPT2TokenizerFast''']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _a	 =     [
      '''GPT2_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''GPT2DoubleHeadsModel''',
      '''GPT2ForQuestionAnswering''',
      '''GPT2ForSequenceClassification''',
      '''GPT2ForTokenClassification''',
      '''GPT2LMHeadModel''',
      '''GPT2Model''',
      '''GPT2PreTrainedModel''',
      '''load_tf_weights_in_gpt2''',
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _a	 =     [
      '''TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST''',
      '''TFGPT2DoubleHeadsModel''',
      '''TFGPT2ForSequenceClassification''',
      '''TFGPT2LMHeadModel''',
      '''TFGPT2MainLayer''',
      '''TFGPT2Model''',
      '''TFGPT2PreTrainedModel''',
  ]

try:
  if not is_keras_nlp_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _a	 =     ['''TFGPT2Tokenizer''']

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _a	 =     ['''FlaxGPT2LMHeadModel''', '''FlaxGPT2Model''', '''FlaxGPT2PreTrainedModel''']

if TYPE_CHECKING:
  from .configuration_gpta import GPT2_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTaConfig, GPTaOnnxConfig
  from .tokenization_gpta import GPTaTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_gpta_fast import GPTaTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_gpta import (
        GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
        GPTaDoubleHeadsModel,
        GPTaForQuestionAnswering,
        GPTaForSequenceClassification,
        GPTaForTokenClassification,
        GPTaLMHeadModel,
        GPTaModel,
        GPTaPreTrainedModel,
        load_tf_weights_in_gpta,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_gpta import (
        TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFGPTaDoubleHeadsModel,
        TFGPTaForSequenceClassification,
        TFGPTaLMHeadModel,
        TFGPTaMainLayer,
        TFGPTaModel,
        TFGPTaPreTrainedModel,
    )

  try:
    if not is_keras_nlp_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_gpta_tf import TFGPTaTokenizer

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel, FlaxGPTaPreTrainedModel

else:
  import sys

  _a	 =     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





