






"""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       ,						snake_case__       ,						snake_case__=13       ,						snake_case__=7       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=False       ,						snake_case__=True       ,						snake_case__=99       ,						snake_case__=64       ,						snake_case__=2       ,						snake_case__=4       ,						snake_case__=64       ,						snake_case__="gelu"       ,						snake_case__=0.1       ,						snake_case__=0.1       ,						snake_case__=5_12       ,						snake_case__=16       ,						snake_case__=2       ,						snake_case__=0.02       ,						snake_case__=3       ,						snake_case__=4       ,						snake_case__=None       ,						)	->	Optional[Any]:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





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

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

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

         UpperCAmelCase					=	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       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Optional[int]:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	List[Any]:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	TFMPNetForQuestionAnswering(config=__UpperCAmelCase )
         UpperCAmelCase					=	{
             """input_ids""": input_ids,
             """attention_mask""": input_mask,
         }
         UpperCAmelCase					=	model(__UpperCAmelCase )
         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       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.num_labels
         UpperCAmelCase					=	TFMPNetForSequenceClassification(__UpperCAmelCase )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask}
         UpperCAmelCase					=	model(__UpperCAmelCase )
         self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels) )



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Tuple:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__       ,						snake_case__ )	->	Tuple:



         """simple docstring"""





         UpperCAmelCase					=	self.num_labels
         UpperCAmelCase					=	TFMPNetForTokenClassification(__UpperCAmelCase )
         UpperCAmelCase					=	{"""input_ids""": input_ids, """attention_mask""": input_mask}
         UpperCAmelCase					=	model(__UpperCAmelCase )
         self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.num_labels) )



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



         """simple docstring"""





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





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



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	TFMPNetModelTester(self )
         UpperCAmelCase					=	ConfigTester(self       ,						config_class=__UpperCAmelCase       ,						hidden_size=37 )



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



         """simple docstring"""





         self.config_tester.run_common_tests()



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



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_model(*__UpperCAmelCase )



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



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_masked_lm(*__UpperCAmelCase )



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_question_answering(*__UpperCAmelCase )



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



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_sequence_classification(*__UpperCAmelCase )



  def 						UpperCamelCase_ (					self )	->	int:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_multiple_choice(*__UpperCAmelCase )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_mpnet_for_token_classification(*__UpperCAmelCase )



  @slow
  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





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





@require_tf
class      UpperCamelCase_       (				unittest.TestCase     ):



  @slow
  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





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

         UpperCAmelCase					=	[1, 6, 7_68]
         self.assertEqual(output.shape       ,						__UpperCAmelCase )

         UpperCAmelCase					=	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]       ,						__UpperCAmelCase       ,						atol=1e-4 )


"""simple docstring"""


from typing import TYPE_CHECKING

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


__A       						=       {'configuration_bert_generation': ['BertGenerationConfig']}

try:
			if not is_sentencepiece_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A       						=       ['BertGenerationTokenizer']

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__A       						=       [
			    'BertGenerationDecoder',
			    'BertGenerationEncoder',
			    'BertGenerationPreTrainedModel',
			    'load_tf_weights_in_bert_generation',
			]


if TYPE_CHECKING:
			from .configuration_bert_generation import BertGenerationConfig

			try:
						if not is_sentencepiece_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .tokenization_bert_generation import BertGenerationTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_bert_generation import (
						    BertGenerationDecoder,
						    BertGenerationEncoder,
						    BertGenerationPreTrainedModel,
						    load_tf_weights_in_bert_generation,
						)

else:
			import sys

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



