






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







		'''simple docstring'''



		def __init__(							self       ,	_UpperCamelCase       ,	_UpperCamelCase=1_3       ,	_UpperCamelCase=7       ,	_UpperCamelCase=True       ,	_UpperCamelCase=True       ,	_UpperCamelCase=False       ,	_UpperCamelCase=True       ,	_UpperCamelCase=9_9       ,	_UpperCamelCase=6_4       ,	_UpperCamelCase=2       ,	_UpperCamelCase=4       ,	_UpperCamelCase=6_4       ,	_UpperCamelCase="gelu"       ,	_UpperCamelCase=0.1       ,	_UpperCamelCase=0.1       ,	_UpperCamelCase=5_1_2       ,	_UpperCamelCase=1_6       ,	_UpperCamelCase=2       ,	_UpperCamelCase=0.02       ,	_UpperCamelCase=3       ,	_UpperCamelCase=4       ,	_UpperCamelCase=None       ,	)			->							Union[str, Any]:
							UpperCAmelCase_					:   Any   =		parent
							UpperCAmelCase_					:   Any   =		batch_size
							UpperCAmelCase_					:   List[str]   =		seq_length
							UpperCAmelCase_					:   Any   =		is_training
							UpperCAmelCase_					:   str   =		use_input_mask
							UpperCAmelCase_					:   Any   =		use_token_type_ids
							UpperCAmelCase_					:   Any   =		use_labels
							UpperCAmelCase_					:   str   =		vocab_size
							UpperCAmelCase_					:   Dict   =		hidden_size
							UpperCAmelCase_					:   str   =		num_hidden_layers
							UpperCAmelCase_					:   List[Any]   =		num_attention_heads
							UpperCAmelCase_					:   Union[str, Any]   =		intermediate_size
							UpperCAmelCase_					:   str   =		hidden_act
							UpperCAmelCase_					:   Union[str, Any]   =		hidden_dropout_prob
							UpperCAmelCase_					:   Dict   =		attention_probs_dropout_prob
							UpperCAmelCase_					:   List[str]   =		max_position_embeddings
							UpperCAmelCase_					:   Dict   =		type_vocab_size
							UpperCAmelCase_					:   List[Any]   =		type_sequence_label_size
							UpperCAmelCase_					:   Union[str, Any]   =		initializer_range
							UpperCAmelCase_					:   List[str]   =		num_labels
							UpperCAmelCase_					:   Any   =		num_choices
							UpperCAmelCase_					:   Optional[int]   =		scope



		def       __UpperCAmelCase    (							self     )			->							Union[str, Any]:
							UpperCAmelCase_					:   str   =		ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size     )

							UpperCAmelCase_					:   Union[str, Any]   =		None
							if self.use_input_mask:
												UpperCAmelCase_					:   Union[str, Any]   =		random_attention_mask([self.batch_size, self.seq_length]     )

							UpperCAmelCase_					:   List[Any]   =		None
							UpperCAmelCase_					:   str   =		None
							UpperCAmelCase_					:   List[str]   =		None
							if self.use_labels:
												UpperCAmelCase_					:   Any   =		ids_tensor([self.batch_size]       ,	self.type_sequence_label_size     )
												UpperCAmelCase_					:   Dict   =		ids_tensor([self.batch_size, self.seq_length]       ,	self.num_labels     )
												UpperCAmelCase_					:   Optional[Any]   =		ids_tensor([self.batch_size]       ,	self.num_choices     )

							UpperCAmelCase_					:   Optional[Any]   =		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       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							List[str]:
							UpperCAmelCase_					:   List[Any]   =		TFMPNetModel(config=_UpperCamelCase     )
							UpperCAmelCase_					:   Any   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCAmelCase_					:   Optional[int]   =		model(_UpperCamelCase     )
							UpperCAmelCase_					:   Optional[Any]   =		[input_ids, input_mask]
							UpperCAmelCase_					:   int   =		model(_UpperCamelCase     )
							self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.seq_length, self.hidden_size)     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							str:
							UpperCAmelCase_					:   int   =		TFMPNetForMaskedLM(config=_UpperCamelCase     )
							UpperCAmelCase_					:   Tuple   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCAmelCase_					:   List[str]   =		model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.seq_length, self.vocab_size)     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							Dict:
							UpperCAmelCase_					:   List[Any]   =		TFMPNetForQuestionAnswering(config=_UpperCamelCase     )
							UpperCAmelCase_					:   int   =		{
							    '''input_ids''': input_ids,
							    '''attention_mask''': input_mask,
							}
							UpperCAmelCase_					:   int   =		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       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							Dict:
							UpperCAmelCase_					:   Optional[Any]   =		self.num_labels
							UpperCAmelCase_					:   List[str]   =		TFMPNetForSequenceClassification(_UpperCamelCase     )
							UpperCAmelCase_					:   str   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCAmelCase_					:   Dict   =		model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_labels)     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							Optional[Any]:
							UpperCAmelCase_					:   int   =		self.num_choices
							UpperCAmelCase_					:   Dict   =		TFMPNetForMultipleChoice(_UpperCamelCase     )
							UpperCAmelCase_					:   str   =		tf.tile(tf.expand_dims(_UpperCamelCase       ,	1     )       ,	(1, self.num_choices, 1)     )
							UpperCAmelCase_					:   Union[str, Any]   =		tf.tile(tf.expand_dims(_UpperCamelCase       ,	1     )       ,	(1, self.num_choices, 1)     )
							UpperCAmelCase_					:   Tuple   =		{
							    '''input_ids''': multiple_choice_inputs_ids,
							    '''attention_mask''': multiple_choice_input_mask,
							}
							UpperCAmelCase_					:   List[Any]   =		model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_choices)     )



		def       __UpperCAmelCase    (							self       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase       ,	_UpperCamelCase     )			->							List[str]:
							UpperCAmelCase_					:   Dict   =		self.num_labels
							UpperCAmelCase_					:   List[str]   =		TFMPNetForTokenClassification(_UpperCamelCase     )
							UpperCAmelCase_					:   Union[str, Any]   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							UpperCAmelCase_					:   List[Any]   =		model(_UpperCamelCase     )
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.seq_length, self.num_labels)     )



		def       __UpperCAmelCase    (							self     )			->							Union[str, Any]:
							UpperCAmelCase_					:   List[str]   =		self.prepare_config_and_inputs()
							(UpperCAmelCase_)					:   int   =		config_and_inputs
							UpperCAmelCase_					:   int   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict





@require_tf
class 						lowerCamelCase   (_snake_case				,						_snake_case				,						unittest.TestCase    ):







		'''simple docstring'''



		_snake_case					:					str 		=						(
		    (
		        TFMPNetForMaskedLM,
		        TFMPNetForMultipleChoice,
		        TFMPNetForQuestionAnswering,
		        TFMPNetForSequenceClassification,
		        TFMPNetForTokenClassification,
		        TFMPNetModel,
		    )
		    if is_tf_available()
		    else ()
		)
		_snake_case					:					Dict 		=						(
		    {
		        '''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					:					Union[str, Any] 		=						False
		_snake_case					:					List[Any] 		=						False



		def       __UpperCAmelCase    (							self     )			->							Tuple:
							UpperCAmelCase_					:   List[str]   =		TFMPNetModelTester(self     )
							UpperCAmelCase_					:   Optional[Any]   =		ConfigTester(self       ,	config_class=_UpperCamelCase       ,	hidden_size=3_7     )



		def       __UpperCAmelCase    (							self     )			->							Union[str, Any]:
							self.config_tester.run_common_tests()



		def       __UpperCAmelCase    (							self     )			->							Optional[int]:
							UpperCAmelCase_					:   int   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_model(*_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							str:
							UpperCAmelCase_					:   int   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_masked_lm(*_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							Dict:
							UpperCAmelCase_					:   Optional[Any]   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_question_answering(*_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							str:
							UpperCAmelCase_					:   Optional[int]   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_sequence_classification(*_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							Any:
							UpperCAmelCase_					:   Optional[int]   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_multiple_choice(*_UpperCamelCase     )



		def       __UpperCAmelCase    (							self     )			->							List[Any]:
							UpperCAmelCase_					:   Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_token_classification(*_UpperCamelCase     )



		@slow
		def       __UpperCAmelCase    (							self     )			->							int:
							for model_name in ["microsoft/mpnet-base"]:
												UpperCAmelCase_					:   List[Any]   =		TFMPNetModel.from_pretrained(_UpperCamelCase     )
												self.assertIsNotNone(_UpperCamelCase     )





@require_tf
class 						lowerCamelCase   (unittest.TestCase    ):







		'''simple docstring'''



		@slow
		def       __UpperCAmelCase    (							self     )			->							Dict:
							UpperCAmelCase_					:   Any   =		TFMPNetModel.from_pretrained('microsoft/mpnet-base'     )
							UpperCAmelCase_					:   str   =		tf.constant([[0, 1, 2, 3, 4, 5]]     )
							UpperCAmelCase_					:   int   =		model(_UpperCamelCase     )[0]

							UpperCAmelCase_					:   Optional[int]   =		[1, 6, 7_6_8]
							self.assertEqual(output.shape       ,	_UpperCamelCase     )

							UpperCAmelCase_					:   List[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]       ,	_UpperCamelCase       ,	atol=1E-4     )














from typing import TYPE_CHECKING

from ...utils import _LazyModule


__UpperCAmelCase										=		{'tokenization_phobert': ['PhobertTokenizer']}


if TYPE_CHECKING:
		from .tokenization_phobert import PhobertTokenizer

else:
		import sys

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






