






"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import MPNetConfig, 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 tensorflow as tf

      from transformers.models.mpnet.modeling_tf_mpnet import (
          TFMPNetForMaskedLM,
          TFMPNetForMultipleChoice,
          TFMPNetForQuestionAnswering,
          TFMPNetForSequenceClassification,
          TFMPNetForTokenClassification,
          TFMPNetModel,
      )







class 					_UpperCamelCase		:






    def __init__(self						,      lowerCamelCase__						,      lowerCamelCase__=1_3						,      lowerCamelCase__=7						,      lowerCamelCase__=True						,      lowerCamelCase__=True						,      lowerCamelCase__=False						,      lowerCamelCase__=True						,      lowerCamelCase__=9_9						,      lowerCamelCase__=6_4						,      lowerCamelCase__=2						,      lowerCamelCase__=4						,      lowerCamelCase__=6_4						,      lowerCamelCase__="gelu"						,      lowerCamelCase__=0.1						,      lowerCamelCase__=0.1						,      lowerCamelCase__=5_1_2						,      lowerCamelCase__=1_6						,      lowerCamelCase__=2						,      lowerCamelCase__=0.0_2						,      lowerCamelCase__=3						,      lowerCamelCase__=4						,      lowerCamelCase__=None						,      ):


      """simple docstring"""
      A__											=							parent
      A__											=							batch_size
      A__											=							seq_length
      A__											=							is_training
      A__											=							use_input_mask
      A__											=							use_token_type_ids
      A__											=							use_labels
      A__											=							vocab_size
      A__											=							hidden_size
      A__											=							num_hidden_layers
      A__											=							num_attention_heads
      A__											=							intermediate_size
      A__											=							hidden_act
      A__											=							hidden_dropout_prob
      A__											=							attention_probs_dropout_prob
      A__											=							max_position_embeddings
      A__											=							type_vocab_size
      A__											=							type_sequence_label_size
      A__											=							initializer_range
      A__											=							num_labels
      A__											=							num_choices
      A__											=							scope






    def  A    (self   ):


      """simple docstring"""
      A__											=							ids_tensor([self.batch_size, self.seq_length]						,      self.vocab_size   )

      A__											=							None
      if self.use_input_mask:
        A__											=							random_attention_mask([self.batch_size, self.seq_length]   )

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

      A__											=							MPNetConfig(
          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						,      initializer_range=self.initializer_range						,      )
      return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							TFMPNetModel(config=lowerCamelCase__   )
      A__											=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
      A__											=							model(lowerCamelCase__   )
      A__											=							[input_ids, input_mask]
      A__											=							model(lowerCamelCase__   )
      self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)   )






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							TFMPNetForMaskedLM(config=lowerCamelCase__   )
      A__											=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
      A__											=							model(lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.vocab_size)   )






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							TFMPNetForQuestionAnswering(config=lowerCamelCase__   )
      A__											=							{
          """input_ids""": input_ids,
          """attention_mask""": input_mask,
      }
      A__											=							model(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)   )






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							self.num_labels
      A__											=							TFMPNetForSequenceClassification(lowerCamelCase__   )
      A__											=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
      A__											=							model(lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_labels)   )






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							self.num_choices
      A__											=							TFMPNetForMultipleChoice(lowerCamelCase__   )
      A__											=							tf.tile(tf.expand_dims(lowerCamelCase__						,      1   )						,      (1, self.num_choices, 1)   )
      A__											=							tf.tile(tf.expand_dims(lowerCamelCase__						,      1   )						,      (1, self.num_choices, 1)   )
      A__											=							{
          """input_ids""": multiple_choice_inputs_ids,
          """attention_mask""": multiple_choice_input_mask,
      }
      A__											=							model(lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_choices)   )






    def  A    (self						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__						,      lowerCamelCase__   ):


      """simple docstring"""
      A__											=							self.num_labels
      A__											=							TFMPNetForTokenClassification(lowerCamelCase__   )
      A__											=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
      A__											=							model(lowerCamelCase__   )
      self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.num_labels)   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.prepare_config_and_inputs()
      ((A__)						,(A__)						,(A__)						,(A__)						,(A__)						,(A__))											=							config_and_inputs
      A__											=							{"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict







@require_tf
class 					_UpperCamelCase		( __snake_case						, __snake_case						, unittest.TestCase):
    __lowerCamelCase         =  (
        (
            TFMPNetForMaskedLM,
            TFMPNetForMultipleChoice,
            TFMPNetForQuestionAnswering,
            TFMPNetForSequenceClassification,
            TFMPNetForTokenClassification,
            TFMPNetModel,
        )
        if is_tf_available()
        else ()
    )
    __lowerCamelCase         =  (
        {
            "feature-extraction": TFMPNetModel,
            "fill-mask": TFMPNetForMaskedLM,
            "question-answering": TFMPNetForQuestionAnswering,
            "text-classification": TFMPNetForSequenceClassification,
            "token-classification": TFMPNetForTokenClassification,
            "zero-shot": TFMPNetForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    __lowerCamelCase         =  False
    __lowerCamelCase         =  False






    def  A    (self   ):


      """simple docstring"""
      A__											=							TFMPNetModelTester(self   )
      A__											=							ConfigTester(self						,      config_class=lowerCamelCase__						,      hidden_size=3_7   )






    def  A    (self   ):


      """simple docstring"""
      self.config_tester.run_common_tests()






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*lowerCamelCase__   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_masked_lm(*lowerCamelCase__   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCamelCase__   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCamelCase__   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCamelCase__   )






    def  A    (self   ):


      """simple docstring"""
      A__											=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCamelCase__   )






    @slow
    def  A    (self   ):


      """simple docstring"""
      for model_name in ["microsoft/mpnet-base"]:
        A__											=							TFMPNetModel.from_pretrained(lowerCamelCase__   )
        self.assertIsNotNone(lowerCamelCase__   )







@require_tf
class 					_UpperCamelCase		( unittest.TestCase):






    @slow
    def  A    (self   ):


      """simple docstring"""
      A__											=							TFMPNetModel.from_pretrained("""microsoft/mpnet-base"""   )
      A__											=							tf.constant([[0, 1, 2, 3, 4, 5]]   )
      A__											=							model(lowerCamelCase__   )[0]

      A__											=							[1, 6, 7_6_8]
      self.assertEqual(output.shape						,      lowerCamelCase__   )

      A__											=							tf.constant(
          [
              [
                  [-0.1_0_6_7_1_7_2, 0.0_8_2_1_6_4_7_3, 0.0_0_2_4_5_4_3],
                  [-0.0_3_4_6_5_8_7_9, 0.8_3_5_4_1_1_8, -0.0_3_2_5_2_2_8_8],
                  [-0.0_6_5_6_9_4_7_6, -0.1_2_4_2_4_1_1_1, -0.0_4_9_4_4_3_6],
              ]
          ]   )
      tf.debugging.assert_near(output[:, :3, :3]						,      lowerCamelCase__						,      atol=1E-4   )












"""simple docstring"""




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase__			=   logging.get_logger(__name__)

lowerCamelCase__			=   {
    "google/electra-small-generator": "https://huggingface.co/google/electra-small-generator/resolve/main/config.json",
    "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/config.json",
    "google/electra-large-generator": "https://huggingface.co/google/electra-large-generator/resolve/main/config.json",
    "google/electra-small-discriminator": (
        "https://huggingface.co/google/electra-small-discriminator/resolve/main/config.json"
    ),
    "google/electra-base-discriminator": (
        "https://huggingface.co/google/electra-base-discriminator/resolve/main/config.json"
    ),
    "google/electra-large-discriminator": (
        "https://huggingface.co/google/electra-large-discriminator/resolve/main/config.json"
    ),
}







class 					_UpperCamelCase		( __snake_case):
    __lowerCamelCase         =  "electra"






    def __init__(self						,      lowerCamelCase__=3_0_5_2_2						,      lowerCamelCase__=1_2_8						,      lowerCamelCase__=2_5_6						,      lowerCamelCase__=1_2						,      lowerCamelCase__=4						,      lowerCamelCase__=1_0_2_4						,      lowerCamelCase__="gelu"						,      lowerCamelCase__=0.1						,      lowerCamelCase__=0.1						,      lowerCamelCase__=5_1_2						,      lowerCamelCase__=2						,      lowerCamelCase__=0.0_2						,      lowerCamelCase__=1E-12						,      lowerCamelCase__="first"						,      lowerCamelCase__=True						,      lowerCamelCase__="gelu"						,      lowerCamelCase__=0.1						,      lowerCamelCase__=0						,      lowerCamelCase__="absolute"						,      lowerCamelCase__=True						,      lowerCamelCase__=None						,      **lowerCamelCase__						,      ):


      """simple docstring"""
      super().__init__(pad_token_id=lowerCamelCase__						,      **lowerCamelCase__   )

      A__											=							vocab_size
      A__											=							embedding_size
      A__											=							hidden_size
      A__											=							num_hidden_layers
      A__											=							num_attention_heads
      A__											=							intermediate_size
      A__											=							hidden_act
      A__											=							hidden_dropout_prob
      A__											=							attention_probs_dropout_prob
      A__											=							max_position_embeddings
      A__											=							type_vocab_size
      A__											=							initializer_range
      A__											=							layer_norm_eps

      A__											=							summary_type
      A__											=							summary_use_proj
      A__											=							summary_activation
      A__											=							summary_last_dropout
      A__											=							position_embedding_type
      A__											=							use_cache
      A__											=							classifier_dropout







class 					_UpperCamelCase		( __snake_case):






    @property
    def  A    (self   ):


      """simple docstring"""
      if self.task == "multiple-choice":
        A__											=							{0: """batch""", 1: """choice""", 2: """sequence"""}
      else:
        A__											=							{0: """batch""", 1: """sequence"""}
      return OrderedDict(
          [
              ("""input_ids""", dynamic_axis),
              ("""attention_mask""", dynamic_axis),
              ("""token_type_ids""", dynamic_axis),
          ]   )




