




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



 '''simple docstring'''


 def __init__(				self					:  List[str]						,							A_					:  Any						,							A_					:  int=13						,							A_					:  List[Any]=7						,							A_					:  List[str]=True						,							A_					:  Union[str, Any]=True						,							A_					:  Optional[Any]=True						,							A_					:  str=True						,							A_					:  Union[str, Any]=99						,							A_					:  List[Any]=32						,							A_					:  Optional[Any]=2						,							A_					:  Optional[int]=4						,							A_					:  List[Any]=37						,							A_					:  Union[str, Any]="gelu"						,							A_					:  int=0.1						,							A_					:  Optional[Any]=0.1						,							A_					:  Union[str, Any]=512						,							A_					:  Union[str, Any]=16						,							A_					:  Any=2						,							A_					:  Optional[int]=0.02						,							A_					:  Optional[int]=3						,							A_					:  Optional[Any]=4						,							A_					:  Any=None						,							A_					:  Optional[int]=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  )   ->	Dict:

       """simple docstring"""






       lowerCamelCase_		     =    ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size  )

       lowerCamelCase_		     =    None
       if self.use_input_mask:
             # follow test_modeling_tf_ctrl.py
             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_		     =    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						,							)
       lowerCamelCase_		     =    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 				a__ (				self					:  List[str]						,							A_					:  Tuple						,							A_					:  Optional[int]						,							A_					:  Union[str, Any]						,							A_					:  Tuple						,							A_					:  Tuple						,							A_					:  Dict						,							A_					:  Any  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    TFDPRContextEncoder(config=A_  )
       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					:  Any						,							A_					:  Optional[int]						,							A_					:  Union[str, Any]						,							A_					:  Optional[int]						,							A_					:  List[Any]						,							A_					:  int						,							A_					:  Optional[int]						,							A_					:  str  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    TFDPRQuestionEncoder(config=A_  )
       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					:  int						,							A_					:  Optional[Any]						,							A_					:  Optional[Any]						,							A_					:  Union[str, Any]						,							A_					:  str						,							A_					:  Dict						,							A_					:  Optional[int]						,							A_					:  int  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    TFDPRReader(config=A_  )
       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					:  Optional[int]  )   ->	Union[str, Any]:

       """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_tf
class   A(						UpperCamelCase							,      UpperCamelCase							,      unittest.TestCase   ):



 '''simple docstring'''


 UpperCamelCase						   =						(
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 UpperCamelCase						   =						{'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

 UpperCamelCase						   =						False
 UpperCamelCase						   =						False
 UpperCamelCase						   =						False
 UpperCamelCase						   =						False
 UpperCamelCase						   =						False

 def 				a__ (				self					:  Dict  )   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    TFDPRModelTester(self  )
       lowerCamelCase_		     =    ConfigTester(self						,							config_class=A_						,							hidden_size=37  )

 def 				a__ (				self					:  Any  )   ->	Tuple:

       """simple docstring"""






       self.config_tester.run_common_tests()

 def 				a__ (				self					:  Union[str, Any]  )   ->	Any:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*A_  )

 def 				a__ (				self					:  Dict  )   ->	Tuple:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*A_  )

 def 				a__ (				self					:  int  )   ->	Optional[Any]:

       """simple docstring"""






       lowerCamelCase_		     =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*A_  )





 @slow
 def 				a__ (				self					:  Union[str, Any]  )   ->	Optional[int]:

       """simple docstring"""






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

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

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

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







@require_tf
class   A(						unittest.TestCase   ):



 '''simple docstring'''


 @slow
 def 				a__ (				self					:  Optional[int]  )   ->	str:

       """simple docstring"""






       lowerCamelCase_		     =    TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'  )

       lowerCamelCase_		     =    tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]  )  # [CLS] hello, is my dog cute? [SEP]
       lowerCamelCase_		     =    model(A_  )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       lowerCamelCase_		     =    tf.constant(
           [
               [
                   0.03236253,
                   0.12753335,
                   0.16818509,
                   0.00279786,
                   0.3896933,
                   0.24264945,
                   0.2178971,
                   -0.02335227,
                   -0.08481959,
                   -0.14324117,
               ]
           ]  )
       self.assertTrue(numpy.allclose(output[:, :10].numpy()						,							expected_slice.numpy()						,							atol=1E-4  )  )









from typing import TYPE_CHECKING

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


lowerCamelCase				:		Union[str, Any]     =   {
    "configuration_convbert": ["CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "ConvBertConfig", "ConvBertOnnxConfig"],
    "tokenization_convbert": ["ConvBertTokenizer"],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Optional[Any]     =   ["ConvBertTokenizerFast"]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Dict     =   [
     "CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "ConvBertForMaskedLM",
     "ConvBertForMultipleChoice",
     "ConvBertForQuestionAnswering",
     "ConvBertForSequenceClassification",
     "ConvBertForTokenClassification",
     "ConvBertLayer",
     "ConvBertModel",
     "ConvBertPreTrainedModel",
     "load_tf_weights_in_convbert",
 ]


try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase				:		Tuple     =   [
     "TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST",
     "TFConvBertForMaskedLM",
     "TFConvBertForMultipleChoice",
     "TFConvBertForQuestionAnswering",
     "TFConvBertForSequenceClassification",
     "TFConvBertForTokenClassification",
     "TFConvBertLayer",
     "TFConvBertModel",
     "TFConvBertPreTrainedModel",
 ]


if TYPE_CHECKING:
 from .configuration_convbert import CONVBERT_PRETRAINED_CONFIG_ARCHIVE_MAP, ConvBertConfig, ConvBertOnnxConfig
 from .tokenization_convbert import ConvBertTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_convbert_fast import ConvBertTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_convbert import (
      CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      ConvBertForMaskedLM,
      ConvBertForMultipleChoice,
      ConvBertForQuestionAnswering,
      ConvBertForSequenceClassification,
      ConvBertForTokenClassification,
      ConvBertLayer,
      ConvBertModel,
      ConvBertPreTrainedModel,
      load_tf_weights_in_convbert,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_convbert import (
      TF_CONVBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFConvBertForMaskedLM,
      TFConvBertForMultipleChoice,
      TFConvBertForQuestionAnswering,
      TFConvBertForSequenceClassification,
      TFConvBertForTokenClassification,
      TFConvBertLayer,
      TFConvBertModel,
      TFConvBertPreTrainedModel,
  )


else:
 import sys

 lowerCamelCase				:		Optional[int]     =   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



