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






			def __init__(self     :  int			,SCREAMING_SNAKE_CASE_     :  Dict			,SCREAMING_SNAKE_CASE_     :  List[Any]=13			,SCREAMING_SNAKE_CASE_     :  Dict=7			,SCREAMING_SNAKE_CASE_     :  Optional[int]=True			,SCREAMING_SNAKE_CASE_     :  int=True			,SCREAMING_SNAKE_CASE_     :  int=False			,SCREAMING_SNAKE_CASE_     :  str=True			,SCREAMING_SNAKE_CASE_     :  str=99			,SCREAMING_SNAKE_CASE_     :  List[str]=64			,SCREAMING_SNAKE_CASE_     :  int=2			,SCREAMING_SNAKE_CASE_     :  Optional[int]=4			,SCREAMING_SNAKE_CASE_     :  List[Any]=64			,SCREAMING_SNAKE_CASE_     :  int="gelu"			,SCREAMING_SNAKE_CASE_     :  str=0.1			,SCREAMING_SNAKE_CASE_     :  Tuple=0.1			,SCREAMING_SNAKE_CASE_     :  Optional[Any]=512			,SCREAMING_SNAKE_CASE_     :  int=16			,SCREAMING_SNAKE_CASE_     :  Optional[Any]=2			,SCREAMING_SNAKE_CASE_     :  Optional[int]=0.02			,SCREAMING_SNAKE_CASE_     :  Dict=3			,SCREAMING_SNAKE_CASE_     :  Union[str, Any]=4			,SCREAMING_SNAKE_CASE_     :  Union[str, Any]=None			,)					->  List[Any]:





				"""simple docstring"""


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






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





				"""simple docstring"""


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

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

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

				lowerCAmelCase				       =     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     :  Any			,SCREAMING_SNAKE_CASE_     :  List[str]			,SCREAMING_SNAKE_CASE_     :  Union[str, Any]			,SCREAMING_SNAKE_CASE_     :  List[str]			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Dict			,SCREAMING_SNAKE_CASE_     :  Optional[Any] )					->  int:





				"""simple docstring"""


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






			def 				UpperCAmelCase       (self     :  Optional[int]			,SCREAMING_SNAKE_CASE_     :  int			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Dict			,SCREAMING_SNAKE_CASE_     :  int )					->  Optional[Any]:





				"""simple docstring"""


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






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





				"""simple docstring"""


				lowerCAmelCase				       =     TFMPNetForQuestionAnswering(config=SCREAMING_SNAKE_CASE_ )
				lowerCAmelCase				       =     {
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				}
				lowerCAmelCase				       =     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     :  List[str]			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Optional[Any]			,SCREAMING_SNAKE_CASE_     :  List[Any]			,SCREAMING_SNAKE_CASE_     :  str			,SCREAMING_SNAKE_CASE_     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Tuple )					->  Optional[int]:





				"""simple docstring"""


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






			def 				UpperCAmelCase       (self     :  List[Any]			,SCREAMING_SNAKE_CASE_     :  Union[str, Any]			,SCREAMING_SNAKE_CASE_     :  List[Any]			,SCREAMING_SNAKE_CASE_     :  List[str]			,SCREAMING_SNAKE_CASE_     :  int			,SCREAMING_SNAKE_CASE_     :  Dict			,SCREAMING_SNAKE_CASE_     :  str )					->  List[str]:





				"""simple docstring"""


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






			def 				UpperCAmelCase       (self     :  Tuple			,SCREAMING_SNAKE_CASE_     :  Any			,SCREAMING_SNAKE_CASE_     :  Union[str, Any]			,SCREAMING_SNAKE_CASE_     :  str			,SCREAMING_SNAKE_CASE_     :  Optional[Any]			,SCREAMING_SNAKE_CASE_     :  Optional[int]			,SCREAMING_SNAKE_CASE_     :  Any )					->  List[Any]:





				"""simple docstring"""


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






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





				"""simple docstring"""


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





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






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





				"""simple docstring"""


				lowerCAmelCase				       =     TFMPNetModelTester(self )
				lowerCAmelCase				       =     ConfigTester(self			,config_class=SCREAMING_SNAKE_CASE_			,hidden_size=37 )






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





				"""simple docstring"""


				self.config_tester.run_common_tests()






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





				"""simple docstring"""


				lowerCAmelCase				       =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_mpnet_model(*SCREAMING_SNAKE_CASE_ )






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





				"""simple docstring"""


				lowerCAmelCase				       =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_mpnet_for_masked_lm(*SCREAMING_SNAKE_CASE_ )






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





				"""simple docstring"""


				lowerCAmelCase				       =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_mpnet_for_question_answering(*SCREAMING_SNAKE_CASE_ )






			def 				UpperCAmelCase       (self     :  Any )					->  Any:





				"""simple docstring"""


				lowerCAmelCase				       =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_mpnet_for_sequence_classification(*SCREAMING_SNAKE_CASE_ )






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





				"""simple docstring"""


				lowerCAmelCase				       =     self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_mpnet_for_multiple_choice(*SCREAMING_SNAKE_CASE_ )






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





				"""simple docstring"""


				lowerCAmelCase				       =     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[Any] )					->  Union[str, Any]:





				"""simple docstring"""


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





@require_tf
class  lowercase				(   unittest.TestCase		):






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





				"""simple docstring"""


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

				lowerCAmelCase				       =     [1, 6, 768]
				self.assertEqual(output.shape			,SCREAMING_SNAKE_CASE_ )

				lowerCAmelCase				       =     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 )




"""simple docstring"""






from typing import Optional

from ..utils.logging import get_logger
from .audio_classificiation import AudioClassification
from .automatic_speech_recognition import AutomaticSpeechRecognition
from .base import TaskTemplate
from .image_classification import ImageClassification
from .language_modeling import LanguageModeling
from .question_answering import QuestionAnsweringExtractive
from .summarization import Summarization
from .text_classification import TextClassification


UpperCAmelCase          =							[
    """AutomaticSpeechRecognition""",
    """AudioClassification""",
    """ImageClassification""",
    """LanguageModeling""",
    """QuestionAnsweringExtractive""",
    """Summarization""",
    """TaskTemplate""",
    """TextClassification""",
]

UpperCAmelCase          =							get_logger(__name__)


UpperCAmelCase          =							{
    AutomaticSpeechRecognition.task: AutomaticSpeechRecognition,
    AudioClassification.task: AudioClassification,
    ImageClassification.task: ImageClassification,
    LanguageModeling.task: LanguageModeling,
    QuestionAnsweringExtractive.task: QuestionAnsweringExtractive,
    Summarization.task: Summarization,
    TextClassification.task: TextClassification,
}




def __magic_name__  (							_lowerCamelCase:		dict )				->	Optional[TaskTemplate]:

	'''simple docstring'''




	lowerCAmelCase				       =     task_template_dict.get('''task''' )
	if task_name is None:
		logger.warning(F"""Couldn't find template for task '{task_name}'. Available templates: {list(_lowerCamelCase )}""" )
		return None
	lowerCAmelCase				       =     NAMEaTEMPLATE.get(_lowerCamelCase )
	return template.from_dict(_lowerCamelCase )


