



"""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    __snake_case:




    def __init__(   self    ,							A_    ,							A_=13    ,							A_=7    ,							A_=True    ,							A_=True    ,							A_=False    ,							A_=True    ,							A_=99    ,							A_=64    ,							A_=2    ,							A_=4    ,							A_=64    ,							A_="gelu"    ,							A_=0.1    ,							A_=0.1    ,							A_=512    ,							A_=16    ,							A_=2    ,							A_=0.02    ,							A_=3    ,							A_=4    ,							A_=None    ,							):





      '''simple docstring'''



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




    def   A   (   self			):





      '''simple docstring'''



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

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

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

      _SCREAMING_SNAKE_CASE        =			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    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			TFMPNetModel(config=a_			)
      _SCREAMING_SNAKE_CASE        =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      _SCREAMING_SNAKE_CASE        =			[input_ids, input_mask]
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      self.parent.assertEqual(result.last_hidden_state.shape    ,							(self.batch_size, self.seq_length, self.hidden_size)			)




    def   A   (   self    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			TFMPNetForMaskedLM(config=a_			)
      _SCREAMING_SNAKE_CASE        =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.vocab_size)			)




    def   A   (   self    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			TFMPNetForQuestionAnswering(config=a_			)
      _SCREAMING_SNAKE_CASE        =			{
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
      }
      _SCREAMING_SNAKE_CASE        =			model(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)			)




    def   A   (   self    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.num_labels
      _SCREAMING_SNAKE_CASE        =			TFMPNetForSequenceClassification(a_			)
      _SCREAMING_SNAKE_CASE        =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_labels)			)




    def   A   (   self    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.num_choices
      _SCREAMING_SNAKE_CASE        =			TFMPNetForMultipleChoice(a_			)
      _SCREAMING_SNAKE_CASE        =			tf.tile(tf.expand_dims(a_    ,							1			)    ,							(1, self.num_choices, 1)			)
      _SCREAMING_SNAKE_CASE        =			tf.tile(tf.expand_dims(a_    ,							1			)    ,							(1, self.num_choices, 1)			)
      _SCREAMING_SNAKE_CASE        =			{
          '''input_ids''': multiple_choice_inputs_ids,
          '''attention_mask''': multiple_choice_input_mask,
      }
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.num_choices)			)




    def   A   (   self    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_    ,							A_			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.num_labels
      _SCREAMING_SNAKE_CASE        =			TFMPNetForTokenClassification(a_			)
      _SCREAMING_SNAKE_CASE        =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      _SCREAMING_SNAKE_CASE        =			model(a_			)
      self.parent.assertEqual(result.logits.shape    ,							(self.batch_size, self.seq_length, self.num_labels)			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.prepare_config_and_inputs()
      (_SCREAMING_SNAKE_CASE)        =			config_and_inputs
      _SCREAMING_SNAKE_CASE        =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
      return config, inputs_dict




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




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			TFMPNetModelTester(self			)
      _SCREAMING_SNAKE_CASE        =			ConfigTester(self    ,							config_class=a_    ,							hidden_size=37			)




    def   A   (   self			):





      '''simple docstring'''



      self.config_tester.run_common_tests()




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_model(*a_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_masked_lm(*a_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_question_answering(*a_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_			)




    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_mpnet_for_token_classification(*a_			)




    @slow
    def   A   (   self			):





      '''simple docstring'''



      for model_name in ["microsoft/mpnet-base"]:
        _SCREAMING_SNAKE_CASE        =			TFMPNetModel.from_pretrained(a_			)
        self.assertIsNotNone(a_			)




@require_tf
class    __snake_case(					unittest.TestCase       ):




    @slow
    def   A   (   self			):





      '''simple docstring'''



      _SCREAMING_SNAKE_CASE        =			TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''			)
      _SCREAMING_SNAKE_CASE        =			tf.constant([[0, 1, 2, 3, 4, 5]]			)
      _SCREAMING_SNAKE_CASE        =			model(a_			)[0]

      _SCREAMING_SNAKE_CASE        =			[1, 6, 768]
      self.assertEqual(output.shape    ,							a_			)

      _SCREAMING_SNAKE_CASE        =			tf.constant(
          [
              [
                  [-0.1_067_172, 0.08_216_473, 0.0_024_543],
                  [-0.03_465_879, 0.8_354_118, -0.03_252_288],
                  [-0.06_569_476, -0.12_424_111, -0.0_494_436],
              ]
          ]			)
      tf.debugging.assert_near(output[:, :3, :3]    ,							a_    ,							atol=1e-4			)




"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


SCREAMING_SNAKE_CASE					:       Tuple							   =							{
    """configuration_realm""": ["""REALM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """RealmConfig"""],
    """tokenization_realm""": ["""RealmTokenizer"""],
}

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

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			SCREAMING_SNAKE_CASE					:       Dict							   =							[
			    """REALM_PRETRAINED_MODEL_ARCHIVE_LIST""",
			    """RealmEmbedder""",
			    """RealmForOpenQA""",
			    """RealmKnowledgeAugEncoder""",
			    """RealmPreTrainedModel""",
			    """RealmReader""",
			    """RealmScorer""",
			    """load_tf_weights_in_realm""",
			]
			SCREAMING_SNAKE_CASE					:       Union[str, Any]							   =							["""RealmRetriever"""]


if TYPE_CHECKING:
			from .configuration_realm import REALM_PRETRAINED_CONFIG_ARCHIVE_MAP, RealmConfig
			from .tokenization_realm import RealmTokenizer

			try:
						if not is_tokenizers_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_realm import RealmTokenizerFast

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_realm import (
						    REALM_PRETRAINED_MODEL_ARCHIVE_LIST,
						    RealmEmbedder,
						    RealmForOpenQA,
						    RealmKnowledgeAugEncoder,
						    RealmPreTrainedModel,
						    RealmReader,
						    RealmScorer,
						    load_tf_weights_in_realm,
						)
						from .retrieval_realm import RealmRetriever


else:
			import sys

			SCREAMING_SNAKE_CASE					:       str							   =							_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





