
from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import numpy
   import tensorflow as tf

   from transformers import (
       TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
       BertConfig,
       DPRConfig,
       TFDPRContextEncoder,
       TFDPRQuestionEncoder,
       TFDPRReader,
   )







class 					lowerCamelCase		:





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





     def 		UpperCAmelCase(self				:   str       )						->						str:
         snake_case								= ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size       )

         snake_case								= None
         if self.use_input_mask:
             # follow test_modeling_tf_ctrl.py
             snake_case								= random_attention_mask([self.batch_size, self.seq_length]       )

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

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

         snake_case								= BertConfig(
             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    ,   is_decoder=_a    ,   initializer_range=self.initializer_range    ,   )
         snake_case								= DPRConfig(projection_dim=self.projection_dim    ,   **config.to_dict()       )

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





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





     def 		UpperCAmelCase(self				:   str    ,   _A				:   str    ,   _A				:   List[str]    ,   _A				:   Any    ,   _A				:   List[str]    ,   _A				:   Any    ,   _A				:   List[str]    ,   _A				:   Tuple       )						->						Tuple:
         snake_case								= TFDPRQuestionEncoder(config=_a       )
         snake_case								= model(_a    ,   attention_mask=_a    ,   token_type_ids=_a       )
         snake_case								= model(_a    ,   token_type_ids=_a       )
         snake_case								= model(_a       )
         self.parent.assertEqual(result.pooler_output.shape    ,   (self.batch_size, self.projection_dim or self.hidden_size)       )





     def 		UpperCAmelCase(self				:   List[str]    ,   _A				:   List[Any]    ,   _A				:   Optional[int]    ,   _A				:   Union[str, Any]    ,   _A				:   List[Any]    ,   _A				:   List[Any]    ,   _A				:   List[Any]    ,   _A				:   List[str]       )						->						Union[str, Any]:
         snake_case								= TFDPRReader(config=_a       )
         snake_case								= 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 		UpperCAmelCase(self				:   Optional[int]       )						->						Optional[Any]:
         snake_case								= self.prepare_config_and_inputs()
         (
             snake_case   
         )								= config_and_inputs
         snake_case								= {"input_ids": input_ids}
         return config, inputs_dict







@require_tf
class 					lowerCamelCase		(  A_							,			A_							,			unittest.TestCase  ):
     UpperCAmelCase__    :							int									=		(
         (
             TFDPRContextEncoder,
             TFDPRQuestionEncoder,
             TFDPRReader,
         )
         if is_tf_available()
         else ()
     )
     UpperCAmelCase__    :							str									=		{"feature-extraction": TFDPRQuestionEncoder} if is_tf_available() else {}

     UpperCAmelCase__    :							List[Any]									=		False
     UpperCAmelCase__    :							str									=		False
     UpperCAmelCase__    :							Tuple									=		False
     UpperCAmelCase__    :							Dict									=		False
     UpperCAmelCase__    :							str									=		False





     def 		UpperCAmelCase(self				:   str       )						->						List[str]:
         snake_case								= TFDPRModelTester(self       )
         snake_case								= ConfigTester(self    ,   config_class=_a    ,   hidden_size=3_7       )





     def 		UpperCAmelCase(self				:   Optional[int]       )						->						int:
         self.config_tester.run_common_tests()





     def 		UpperCAmelCase(self				:   Any       )						->						Optional[int]:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_context_encoder(*_a       )





     def 		UpperCAmelCase(self				:   Tuple       )						->						List[str]:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_question_encoder(*_a       )





     def 		UpperCAmelCase(self				:   Optional[Any]       )						->						List[Any]:
         snake_case								= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_dpr_reader(*_a       )





     @slow
     def 		UpperCAmelCase(self				:   int       )						->						int:
         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case								= TFDPRContextEncoder.from_pretrained(_a       )
             self.assertIsNotNone(_a       )

         for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case								= TFDPRContextEncoder.from_pretrained(_a       )
             self.assertIsNotNone(_a       )

         for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case								= TFDPRQuestionEncoder.from_pretrained(_a       )
             self.assertIsNotNone(_a       )

         for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case								= TFDPRReader.from_pretrained(_a       )
             self.assertIsNotNone(_a       )







@require_tf
class 					lowerCamelCase		(  unittest.TestCase  ):





     @slow
     def 		UpperCAmelCase(self				:   List[Any]       )						->						Union[str, Any]:
         snake_case								= TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"       )

         snake_case								= tf.constant(
             [[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]]       )  # [CLS] hello, is my dog cute? [SEP]
         snake_case								= model(_a       )[0]  # embedding shape = (1, 768)
         # compare the actual values for a slice.
         snake_case								= tf.constant(
             [
                 [
                     0.03_23_62_53,
                     0.12_75_33_35,
                     0.16_81_85_09,
                     0.00_27_97_86,
                     0.3_89_69_33,
                     0.24_26_49_45,
                     0.2_17_89_71,
                     -0.02_33_52_27,
                     -0.08_48_19_59,
                     -0.14_32_41_17,
                 ]
             ]       )
         self.assertTrue(numpy.allclose(output[:, :1_0].numpy()    ,   expected_slice.numpy()    ,   atol=1E-4       )       )



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A   =			{
    "configuration_lilt": ["LILT_PRETRAINED_CONFIG_ARCHIVE_MAP", "LiltConfig"],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _A   =			[
       "LILT_PRETRAINED_MODEL_ARCHIVE_LIST",
       "LiltForQuestionAnswering",
       "LiltForSequenceClassification",
       "LiltForTokenClassification",
       "LiltModel",
       "LiltPreTrainedModel",
   ]

if TYPE_CHECKING:
   from .configuration_lilt import LILT_PRETRAINED_CONFIG_ARCHIVE_MAP, LiltConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_lilt import (
          LILT_PRETRAINED_MODEL_ARCHIVE_LIST,
          LiltForQuestionAnswering,
          LiltForSequenceClassification,
          LiltForTokenClassification,
          LiltModel,
          LiltPreTrainedModel,
      )

else:
   import sys

   _A   =			_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

