


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






      '''simple docstring'''







      def __init__(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE=13   ,			__SCREAMING_SNAKE_CASE=7   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=False   ,			__SCREAMING_SNAKE_CASE=True   ,			__SCREAMING_SNAKE_CASE=99   ,			__SCREAMING_SNAKE_CASE=64   ,			__SCREAMING_SNAKE_CASE=2   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=64   ,			__SCREAMING_SNAKE_CASE="gelu"   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=0.1   ,			__SCREAMING_SNAKE_CASE=5_12   ,			__SCREAMING_SNAKE_CASE=16   ,			__SCREAMING_SNAKE_CASE=2   ,			__SCREAMING_SNAKE_CASE=0.02   ,			__SCREAMING_SNAKE_CASE=3   ,			__SCREAMING_SNAKE_CASE=4   ,			__SCREAMING_SNAKE_CASE=None   ,			)					->						Tuple:







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=parent
         UpperCamelCase__ :			Optional[int] 						=batch_size
         UpperCamelCase__ :			Optional[Any] 						=seq_length
         UpperCamelCase__ :			int 						=is_training
         UpperCamelCase__ :			int 						=use_input_mask
         UpperCamelCase__ :			str 						=use_token_type_ids
         UpperCamelCase__ :			Any 						=use_labels
         UpperCamelCase__ :			Dict 						=vocab_size
         UpperCamelCase__ :			List[str] 						=hidden_size
         UpperCamelCase__ :			Optional[Any] 						=num_hidden_layers
         UpperCamelCase__ :			str 						=num_attention_heads
         UpperCamelCase__ :			str 						=intermediate_size
         UpperCamelCase__ :			str 						=hidden_act
         UpperCamelCase__ :			Optional[int] 						=hidden_dropout_prob
         UpperCamelCase__ :			List[str] 						=attention_probs_dropout_prob
         UpperCamelCase__ :			Optional[Any] 						=max_position_embeddings
         UpperCamelCase__ :			str 						=type_vocab_size
         UpperCamelCase__ :			Tuple 						=type_sequence_label_size
         UpperCamelCase__ :			Dict 						=initializer_range
         UpperCamelCase__ :			Union[str, Any] 						=num_labels
         UpperCamelCase__ :			Any 						=num_choices
         UpperCamelCase__ :			Dict 						=scope




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			str 						=ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size)

         UpperCamelCase__ :			Any 						=None
         if self.use_input_mask:
            UpperCamelCase__ :			Optional[Any] 						=random_attention_mask([self.batch_size, self.seq_length])

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

         UpperCamelCase__ :			List[str] 						=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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[Any]:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=TFMPNetModel(config=__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[str] 						={"input_ids": input_ids, "attention_mask": input_mask}
         UpperCamelCase__ :			Optional[int] 						=model(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[str] 						=[input_ids, input_mask]
         UpperCamelCase__ :			Optional[Any] 						=model(__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.last_hidden_state.shape   ,			(self.batch_size, self.seq_length, self.hidden_size))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=TFMPNetForMaskedLM(config=__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			int 						={"input_ids": input_ids, "attention_mask": input_mask}
         UpperCamelCase__ :			List[str] 						=model(__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.vocab_size))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=TFMPNetForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Dict 						={
             "input_ids": input_ids,
             "attention_mask": input_mask,
         }
         UpperCamelCase__ :			Dict 						=model(__SCREAMING_SNAKE_CASE)
         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    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.num_labels
         UpperCamelCase__ :			Tuple 						=TFMPNetForSequenceClassification(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Union[str, Any] 						={"input_ids": input_ids, "attention_mask": input_mask}
         UpperCamelCase__ :			Dict 						=model(__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Union[str, Any] 						=self.num_choices
         UpperCamelCase__ :			Dict 						=TFMPNetForMultipleChoice(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			List[Any] 						=tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE   ,			1)   ,			(1, self.num_choices, 1))
         UpperCamelCase__ :			Any 						=tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE   ,			1)   ,			(1, self.num_choices, 1))
         UpperCamelCase__ :			List[str] 						={
             "input_ids": multiple_choice_inputs_ids,
             "attention_mask": multiple_choice_input_mask,
         }
         UpperCamelCase__ :			List[Any] 						=model(__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_choices))




      def    UpperCAmelCase							(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						Optional[int]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.num_labels
         UpperCamelCase__ :			Dict 						=TFMPNetForTokenClassification(__SCREAMING_SNAKE_CASE)
         UpperCamelCase__ :			Any 						={"input_ids": input_ids, "attention_mask": input_mask}
         UpperCamelCase__ :			str 						=model(__SCREAMING_SNAKE_CASE)
         self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.num_labels))




      def    UpperCAmelCase							(		self)					->						Optional[Any]:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.prepare_config_and_inputs()
         ((UpperCamelCase__)						,	(UpperCamelCase__)						,	(UpperCamelCase__)						,	(UpperCamelCase__)						,	(UpperCamelCase__)						,	(UpperCamelCase__)) :			Dict 						=config_and_inputs
         UpperCamelCase__ :			List[Any] 						={"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict




@require_tf
class 							lowercase__(						snake_case__			,							snake_case__			,							unittest.TestCase			):






      '''simple docstring'''







      snake_case__       =					(
          (
              TFMPNetForMaskedLM,
              TFMPNetForMultipleChoice,
              TFMPNetForQuestionAnswering,
              TFMPNetForSequenceClassification,
              TFMPNetForTokenClassification,
              TFMPNetModel,
          )
          if is_tf_available()
          else ()
      )
      snake_case__       =					(
          {
              '''feature-extraction''': TFMPNetModel,
              '''fill-mask''': TFMPNetForMaskedLM,
              '''question-answering''': TFMPNetForQuestionAnswering,
              '''text-classification''': TFMPNetForSequenceClassification,
              '''token-classification''': TFMPNetForTokenClassification,
              '''zero-shot''': TFMPNetForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      snake_case__       =					False
      snake_case__       =					False




      def    UpperCAmelCase							(		self)					->						Any:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=TFMPNetModelTester(self)
         UpperCamelCase__ :			int 						=ConfigTester(self   ,			config_class=__SCREAMING_SNAKE_CASE   ,			hidden_size=37)




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







         """simple docstring"""







         self.config_tester.run_common_tests()




      def    UpperCAmelCase							(		self)					->						List[str]:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_model(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Any 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_masked_lm(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			Tuple 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_question_answering(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_sequence_classification(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						str:







         """simple docstring"""







         UpperCamelCase__ :			Dict 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_multiple_choice(*__SCREAMING_SNAKE_CASE)




      def    UpperCAmelCase							(		self)					->						Dict:







         """simple docstring"""







         UpperCamelCase__ :			List[str] 						=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_token_classification(*__SCREAMING_SNAKE_CASE)




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







         """simple docstring"""







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




@require_tf
class 							lowercase__(						unittest.TestCase			):






      '''simple docstring'''







      @slow
      def    UpperCAmelCase							(		self)					->						List[Any]:







         """simple docstring"""







         UpperCamelCase__ :			int 						=TFMPNetModel.from_pretrained("microsoft/mpnet-base")
         UpperCamelCase__ :			Optional[int] 						=tf.constant([[0, 1, 2, 3, 4, 5]])
         UpperCamelCase__ :			List[Any] 						=model(__SCREAMING_SNAKE_CASE)[0]

         UpperCamelCase__ :			Any 						=[1, 6, 7_68]
         self.assertEqual(output.shape   ,			__SCREAMING_SNAKE_CASE)

         UpperCamelCase__ :			Union[str, Any] 						=tf.constant(
             [
                 [
                     [-0.1_06_71_72, 0.08_21_64_73, 0.0_02_45_43],
                     [-0.03_46_58_79, 0.8_35_41_18, -0.03_25_22_88],
                     [-0.06_56_94_76, -0.12_42_41_11, -0.0_49_44_36],
                 ]
             ])
         tf.debugging.assert_near(output[:, :3, :3]   ,			__SCREAMING_SNAKE_CASE   ,			atol=1E-4)





from collections import Counter
from functools import total_ordering

from data_structures.heap.heap import Heap



@total_ordering
class 							lowercase__:






      '''simple docstring'''







      def __init__(		self   ,			__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE)					->						None:







         """simple docstring"""







         UpperCamelCase__ :			Optional[int] 						=word
         UpperCamelCase__ :			Dict 						=count




      def __eq__(		self   ,			__SCREAMING_SNAKE_CASE)					->						bool:







         """simple docstring"""







         if not isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
            return NotImplemented
         return self.count == other.count




      def __lt__(		self   ,			__SCREAMING_SNAKE_CASE)					->						bool:







         """simple docstring"""







         if not isinstance(__SCREAMING_SNAKE_CASE   ,			__SCREAMING_SNAKE_CASE):
            return NotImplemented
         return self.count < other.count




def 	_lowerCamelCase  (		A_  :	list[str]							,  A_  :	int      )			->			list[str]:

   '''simple docstring'''






   UpperCamelCase__ :			Heap[WordCount] 						=Heap()
   UpperCamelCase__ :			Any 						=Counter(A_      )
   heap.build_max_heap(
       [WordCount(A_							,  A_      ) for word, count in count_by_word.items()]      )
   return [heap.extract_max().word for _ in range(min(A_							,  len(A_      )      )      )]


if __name__ == "__main__":
     import doctest

     doctest.testmod()

