
'''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     :
	"""simple docstring"""





	def __init__(						self				: Optional[int]					,						UpperCamelCase_				: List[str]					,						UpperCamelCase_				: Optional[Any]=13					,						UpperCamelCase_				: Tuple=7					,						UpperCamelCase_				: Any=True					,						UpperCamelCase_				: List[Any]=True					,						UpperCamelCase_				: str=False					,						UpperCamelCase_				: str=True					,						UpperCamelCase_				: Optional[int]=99					,						UpperCamelCase_				: int=64					,						UpperCamelCase_				: Optional[int]=2					,						UpperCamelCase_				: int=4					,						UpperCamelCase_				: Tuple=64					,						UpperCamelCase_				: Any="gelu"					,						UpperCamelCase_				: str=0.1					,						UpperCamelCase_				: Optional[Any]=0.1					,						UpperCamelCase_				: int=512					,						UpperCamelCase_				: int=16					,						UpperCamelCase_				: Union[str, Any]=2					,						UpperCamelCase_				: str=0.0_2					,						UpperCamelCase_				: str=3					,						UpperCamelCase_				: int=4					,						UpperCamelCase_				: Optional[Any]=None					,						):
			lowerCAmelCase_   :       Optional[int]    							=parent
			lowerCAmelCase_   :       Union[str, Any]    							=batch_size
			lowerCAmelCase_   :       List[str]    							=seq_length
			lowerCAmelCase_   :       str    							=is_training
			lowerCAmelCase_   :       Any    							=use_input_mask
			lowerCAmelCase_   :       Optional[int]    							=use_token_type_ids
			lowerCAmelCase_   :       List[str]    							=use_labels
			lowerCAmelCase_   :       Union[str, Any]    							=vocab_size
			lowerCAmelCase_   :       List[Any]    							=hidden_size
			lowerCAmelCase_   :       Optional[int]    							=num_hidden_layers
			lowerCAmelCase_   :       Tuple    							=num_attention_heads
			lowerCAmelCase_   :       List[Any]    							=intermediate_size
			lowerCAmelCase_   :       int    							=hidden_act
			lowerCAmelCase_   :       List[str]    							=hidden_dropout_prob
			lowerCAmelCase_   :       Union[str, Any]    							=attention_probs_dropout_prob
			lowerCAmelCase_   :       str    							=max_position_embeddings
			lowerCAmelCase_   :       Optional[Any]    							=type_vocab_size
			lowerCAmelCase_   :       str    							=type_sequence_label_size
			lowerCAmelCase_   :       Optional[int]    							=initializer_range
			lowerCAmelCase_   :       List[str]    							=num_labels
			lowerCAmelCase_   :       Any    							=num_choices
			lowerCAmelCase_   :       Optional[int]    							=scope




	def 	__A      (						self				: str      ):
			lowerCAmelCase_   :       Optional[int]    							=ids_tensor([self.batch_size, self.seq_length]					,						self.vocab_size      )

			lowerCAmelCase_   :       Optional[int]    							=None
			if self.use_input_mask:
					lowerCAmelCase_   :       Dict    							=random_attention_mask([self.batch_size, self.seq_length]      )

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

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




	def 	__A      (						self				: int					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: List[Any]					,						UpperCamelCase_				: List[str]      ):
			lowerCAmelCase_   :       Tuple    							=TFMPNetForMaskedLM(config=UpperCamelCase_      )
			lowerCAmelCase_   :       Any    							={'''input_ids''': input_ids, '''attention_mask''': input_mask}
			lowerCAmelCase_   :       List[Any]    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.vocab_size)      )




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




	def 	__A      (						self				: Any					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Dict					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: Optional[int]					,						UpperCamelCase_				: Optional[Any]					,						UpperCamelCase_				: int      ):
			lowerCAmelCase_   :       Dict    							=self.num_choices
			lowerCAmelCase_   :       Any    							=TFMPNetForMultipleChoice(UpperCamelCase_      )
			lowerCAmelCase_   :       Any    							=tf.tile(tf.expand_dims(UpperCamelCase_					,						1      )					,						(1, self.num_choices, 1)      )
			lowerCAmelCase_   :       int    							=tf.tile(tf.expand_dims(UpperCamelCase_					,						1      )					,						(1, self.num_choices, 1)      )
			lowerCAmelCase_   :       Any    							={
			    '''input_ids''': multiple_choice_inputs_ids,
			    '''attention_mask''': multiple_choice_input_mask,
			}
			lowerCAmelCase_   :       str    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.num_choices)      )




	def 	__A      (						self				: str					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: Tuple					,						UpperCamelCase_				: Union[str, Any]					,						UpperCamelCase_				: str					,						UpperCamelCase_				: int					,						UpperCamelCase_				: Union[str, Any]      ):
			lowerCAmelCase_   :       Tuple    							=self.num_labels
			lowerCAmelCase_   :       str    							=TFMPNetForTokenClassification(UpperCamelCase_      )
			lowerCAmelCase_   :       Optional[Any]    							={'''input_ids''': input_ids, '''attention_mask''': input_mask}
			lowerCAmelCase_   :       Any    							=model(UpperCamelCase_      )
			self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.seq_length, self.num_labels)      )





	def 	__A      (						self				: Optional[int]      ):
			lowerCAmelCase_   :       Union[str, Any]    							=self.prepare_config_and_inputs()
			((lowerCAmelCase_)							,		(lowerCAmelCase_)							,		(lowerCAmelCase_)							,		(lowerCAmelCase_)							,		(lowerCAmelCase_)							,		(lowerCAmelCase_))   :       Union[str, Any]    							=config_and_inputs
			lowerCAmelCase_   :       Union[str, Any]    							={'''input_ids''': input_ids, '''attention_mask''': input_mask}
			return config, inputs_dict







@require_tf
class 							_snake_case     (  lowerCAmelCase_						,    lowerCAmelCase_						,    unittest.TestCase ):
	"""simple docstring"""





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




	def 	__A      (						self				: List[Any]      ):
			lowerCAmelCase_   :       List[str]    							=TFMPNetModelTester(self      )
			lowerCAmelCase_   :       List[Any]    							=ConfigTester(self					,						config_class=UpperCamelCase_					,						hidden_size=37      )




	def 	__A      (						self				: Tuple      ):
			self.config_tester.run_common_tests()




	def 	__A      (						self				: List[str]      ):
			lowerCAmelCase_   :       List[str]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_model(*UpperCamelCase_      )




	def 	__A      (						self				: Optional[int]      ):
			lowerCAmelCase_   :       int    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_masked_lm(*UpperCamelCase_      )




	def 	__A      (						self				: str      ):
			lowerCAmelCase_   :       Any    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCamelCase_      )




	def 	__A      (						self				: Union[str, Any]      ):
			lowerCAmelCase_   :       Union[str, Any]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCamelCase_      )




	def 	__A      (						self				: Any      ):
			lowerCAmelCase_   :       Optional[Any]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCamelCase_      )




	def 	__A      (						self				: Any      ):
			lowerCAmelCase_   :       List[str]    							=self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCamelCase_      )





	@slow
	def 	__A      (						self				: str      ):
			for model_name in ["microsoft/mpnet-base"]:
					lowerCAmelCase_   :       Dict    							=TFMPNetModel.from_pretrained(UpperCamelCase_      )
					self.assertIsNotNone(UpperCamelCase_      )







@require_tf
class 							_snake_case     (  unittest.TestCase ):
	"""simple docstring"""





	@slow
	def 	__A      (						self				: List[Any]      ):
			lowerCAmelCase_   :       str    							=TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''      )
			lowerCAmelCase_   :       Union[str, Any]    							=tf.constant([[0, 1, 2, 3, 4, 5]]      )
			lowerCAmelCase_   :       Optional[Any]    							=model(UpperCamelCase_      )[0]

			lowerCAmelCase_   :       Dict    							=[1, 6, 768]
			self.assertEqual(output.shape					,						UpperCamelCase_      )

			lowerCAmelCase_   :       str    							=tf.constant(
			    [
			        [
			            [-0.1_0_6_7_1_7_2, 0.0_8_2_1_6_4_7_3, 0.0_0_2_4_5_4_3],
			            [-0.0_3_4_6_5_8_7_9, 0.8_3_5_4_1_1_8, -0.0_3_2_5_2_2_8_8],
			            [-0.0_6_5_6_9_4_7_6, -0.1_2_4_2_4_1_1_1, -0.0_4_9_4_4_3_6],
			        ]
			    ]      )
			tf.debugging.assert_near(output[:, :3, :3]					,						UpperCamelCase_					,						atol=1E-4      )








'''simple docstring'''






from __future__ import annotations







def 		SCREAMING_SNAKE_CASE__					(							_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE  ):
		if len(_SCREAMING_SNAKE_CASE  ) == 0:
				raise ValueError('''find_min() arg is an empty sequence'''  )
		if (
		    left >= len(_SCREAMING_SNAKE_CASE  )
		    or left < -len(_SCREAMING_SNAKE_CASE  )
		    or right >= len(_SCREAMING_SNAKE_CASE  )
		    or right < -len(_SCREAMING_SNAKE_CASE  )
		):
				raise IndexError('''list index out of range'''  )
		if left == right:
				return nums[left]
		lowerCAmelCase_   :       Dict    							=(left + right) >> 1  # the middle
		lowerCAmelCase_   :       Optional[int]    							=find_min(_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE  ,					_SCREAMING_SNAKE_CASE  )  # find min in range[left, mid]
		lowerCAmelCase_   :       Any    							=find_min(_SCREAMING_SNAKE_CASE  ,					mid + 1  ,					_SCREAMING_SNAKE_CASE  )  # find min in range[mid + 1, right]

		return left_min if left_min <= right_min else right_min


if __name__ == "__main__":
							import doctest

							doctest.testmod(verbose=True)






