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

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

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

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

    __a					:    List[str]													=    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   ,							)
    __a					:    List[Any]													=    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  lowerCAmelCase     (self					:   Any   ,							snake_case_					:   str   ,							snake_case_					:   Tuple   ,							snake_case_					:   Optional[int]   ,							snake_case_					:   List[str]   ,							snake_case_					:   Optional[int]   ,							snake_case_					:   Tuple   ,							snake_case_					:   List[str]  ):
    __a					:    Any													=    TFDPRContextEncoder(config=A_  )
    __a					:    Union[str, Any]													=    model(A_   ,							attention_mask=A_   ,							token_type_ids=A_  )
    __a					:    Any													=    model(A_   ,							token_type_ids=A_  )
    __a					:    Dict													=    model(A_  )
    self.parent.assertEqual(result.pooler_output.shape   ,							(self.batch_size, self.projection_dim or self.hidden_size)  )
   def  lowerCAmelCase     (self					:   Union[str, Any]   ,							snake_case_					:   List[Any]   ,							snake_case_					:   Optional[Any]   ,							snake_case_					:   Tuple   ,							snake_case_					:   Union[str, Any]   ,							snake_case_					:   Union[str, Any]   ,							snake_case_					:   Optional[Any]   ,							snake_case_					:   Tuple  ):
    __a					:    str													=    TFDPRQuestionEncoder(config=A_  )
    __a					:    Dict													=    model(A_   ,							attention_mask=A_   ,							token_type_ids=A_  )
    __a					:    Union[str, Any]													=    model(A_   ,							token_type_ids=A_  )
    __a					:    Tuple													=    model(A_  )
    self.parent.assertEqual(result.pooler_output.shape   ,							(self.batch_size, self.projection_dim or self.hidden_size)  )
   def  lowerCAmelCase     (self					:   Any   ,							snake_case_					:   Optional[Any]   ,							snake_case_					:   str   ,							snake_case_					:   str   ,							snake_case_					:   Optional[int]   ,							snake_case_					:   str   ,							snake_case_					:   str   ,							snake_case_					:   List[Any]  ):
    __a					:    Optional[int]													=    TFDPRReader(config=A_  )
    __a					:    Any													=    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  lowerCAmelCase     (self					:   Dict  ):
    __a					:    Any													=    self.prepare_config_and_inputs()
    (
        (
        __a   
    )							,			(
        __a   
    )							,			(
        __a   
    )							,			(
        __a   
    )							,			(
        __a   
    )							,			(
        __a   
    )							,			(
        __a   
    )							,			   
    )					:    str													=    config_and_inputs
    __a					:    int													=    {'''input_ids''': input_ids}
    return config, inputs_dict





@require_tf
class 						UpperCamelCase__    (			_lowercase       ,_lowercase       ,unittest.TestCase       ):
   _SCREAMING_SNAKE_CASE :				Any      			= (
       (
           TFDPRContextEncoder,
           TFDPRQuestionEncoder,
           TFDPRReader,
       )
       if is_tf_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE :				Any      			= {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

   _SCREAMING_SNAKE_CASE :				Tuple      			= False
   _SCREAMING_SNAKE_CASE :				str      			= False
   _SCREAMING_SNAKE_CASE :				int      			= False
   _SCREAMING_SNAKE_CASE :				List[Any]      			= False
   _SCREAMING_SNAKE_CASE :				Union[str, Any]      			= False
   def  lowerCAmelCase     (self					:   Dict  ):
    __a					:    str													=    TFDPRModelTester(self  )
    __a					:    int													=    ConfigTester(self   ,							config_class=A_   ,							hidden_size=3_7  )
   def  lowerCAmelCase     (self					:   int  ):
    self.config_tester.run_common_tests()
   def  lowerCAmelCase     (self					:   List[Any]  ):
    __a					:    Tuple													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_context_encoder(*A_  )
   def  lowerCAmelCase     (self					:   Dict  ):
    __a					:    Dict													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_question_encoder(*A_  )
   def  lowerCAmelCase     (self					:   Optional[Any]  ):
    __a					:    Dict													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_dpr_reader(*A_  )







   @slow
   def  lowerCAmelCase     (self					:   Dict  ):
    for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a					:    Optional[Any]													=    TFDPRContextEncoder.from_pretrained(A_  )
     self.assertIsNotNone(A_  )

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

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

    for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a					:    Union[str, Any]													=    TFDPRReader.from_pretrained(A_  )
     self.assertIsNotNone(A_  )





@require_tf
class 						UpperCamelCase__    (			unittest.TestCase       ):
   @slow
   def  lowerCAmelCase     (self					:   Optional[Any]  ):
    __a					:    Optional[int]													=    TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''  )

    __a					:    Union[str, Any]													=    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]
    __a					:    Optional[Any]													=    model(A_  )[0]  # embedding shape = (1, 768)
    # compare the actual values for a slice.
    __a					:    List[str]													=    tf.constant(
        [
            [
                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,
            ]
        ]  )
    self.assertTrue(numpy.allclose(output[:, :1_0].numpy()   ,							expected_slice.numpy()   ,							atol=1E-4  )  )


"""simple docstring"""


from typing import TYPE_CHECKING

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


_lowercase				 =					{
    '''configuration_electra''': ['''ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ElectraConfig''', '''ElectraOnnxConfig'''],
    '''tokenization_electra''': ['''ElectraTokenizer'''],
}

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

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase				 =					[
		    '''ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ElectraForCausalLM''',
		    '''ElectraForMaskedLM''',
		    '''ElectraForMultipleChoice''',
		    '''ElectraForPreTraining''',
		    '''ElectraForQuestionAnswering''',
		    '''ElectraForSequenceClassification''',
		    '''ElectraForTokenClassification''',
		    '''ElectraModel''',
		    '''ElectraPreTrainedModel''',
		    '''load_tf_weights_in_electra''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase				 =					[
		    '''TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFElectraForMaskedLM''',
		    '''TFElectraForMultipleChoice''',
		    '''TFElectraForPreTraining''',
		    '''TFElectraForQuestionAnswering''',
		    '''TFElectraForSequenceClassification''',
		    '''TFElectraForTokenClassification''',
		    '''TFElectraModel''',
		    '''TFElectraPreTrainedModel''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase				 =					[
		    '''FlaxElectraForCausalLM''',
		    '''FlaxElectraForMaskedLM''',
		    '''FlaxElectraForMultipleChoice''',
		    '''FlaxElectraForPreTraining''',
		    '''FlaxElectraForQuestionAnswering''',
		    '''FlaxElectraForSequenceClassification''',
		    '''FlaxElectraForTokenClassification''',
		    '''FlaxElectraModel''',
		    '''FlaxElectraPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_electra import ELECTRA_PRETRAINED_CONFIG_ARCHIVE_MAP, ElectraConfig, ElectraOnnxConfig
		from .tokenization_electra import ElectraTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_electra_fast import ElectraTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_electra import (
				    ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ElectraForCausalLM,
				    ElectraForMaskedLM,
				    ElectraForMultipleChoice,
				    ElectraForPreTraining,
				    ElectraForQuestionAnswering,
				    ElectraForSequenceClassification,
				    ElectraForTokenClassification,
				    ElectraModel,
				    ElectraPreTrainedModel,
				    load_tf_weights_in_electra,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_electra import (
				    TF_ELECTRA_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFElectraForMaskedLM,
				    TFElectraForMultipleChoice,
				    TFElectraForPreTraining,
				    TFElectraForQuestionAnswering,
				    TFElectraForSequenceClassification,
				    TFElectraForTokenClassification,
				    TFElectraModel,
				    TFElectraPreTrainedModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_electra import (
				    FlaxElectraForCausalLM,
				    FlaxElectraForMaskedLM,
				    FlaxElectraForMultipleChoice,
				    FlaxElectraForPreTraining,
				    FlaxElectraForQuestionAnswering,
				    FlaxElectraForSequenceClassification,
				    FlaxElectraForTokenClassification,
				    FlaxElectraModel,
				    FlaxElectraPreTrainedModel,
				)

else:
		import sys

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