


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






		'''simple docstring'''

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





			'''simple docstring'''

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



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Any         =							ids_tensor([self.batch_size, self.seq_length]							,			self.vocab_size   )

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

			__snake_case      :		int         =							None
			__snake_case      :		List[str]         =							None
			__snake_case      :		List[str]         =							None
			if self.use_labels:
				__snake_case      :		Tuple         =							ids_tensor([self.batch_size]							,			self.type_sequence_label_size   )
				__snake_case      :		Optional[int]         =							ids_tensor([self.batch_size, self.seq_length]							,			self.num_labels   )
				__snake_case      :		List[str]         =							ids_tensor([self.batch_size]							,			self.num_choices   )

			__snake_case      :		Dict         =							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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

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



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		Optional[Any]         =							TFMPNetForMaskedLM(config=a_   )
			__snake_case      :		List[Any]         =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
			__snake_case      :		int         =							model(a_   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.seq_length, self.vocab_size)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		List[str]         =							TFMPNetForQuestionAnswering(config=a_   )
			__snake_case      :		Optional[Any]         =							{
			    '''input_ids''': input_ids,
			    '''attention_mask''': input_mask,
			}
			__snake_case      :		Optional[int]         =							model(a_   )
			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 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		List[Any]         =							self.num_labels
			__snake_case      :		Optional[int]         =							TFMPNetForSequenceClassification(a_   )
			__snake_case      :		Dict         =							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
			__snake_case      :		Optional[Any]         =							model(a_   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_labels)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		Tuple         =							self.num_choices
			__snake_case      :		List[str]         =							TFMPNetForMultipleChoice(a_   )
			__snake_case      :		List[str]         =							tf.tile(tf.expand_dims(a_							,			1   )							,			(1, self.num_choices, 1)   )
			__snake_case      :		Tuple         =							tf.tile(tf.expand_dims(a_							,			1   )							,			(1, self.num_choices, 1)   )
			__snake_case      :		Tuple         =							{
			    '''input_ids''': multiple_choice_inputs_ids,
			    '''attention_mask''': multiple_choice_input_mask,
			}
			__snake_case      :		str         =							model(a_   )
			self.parent.assertEqual(result.logits.shape							,			(self.batch_size, self.num_choices)   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_							,			a_							,			a_   ):





			'''simple docstring'''

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



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

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



@require_tf
class      _UpperCAmelCase					(				__snake_case,							__snake_case,							unittest.TestCase							):






		'''simple docstring'''

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



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							TFMPNetModelTester(self   )
			__snake_case      :		int         =							ConfigTester(self							,			config_class=a_							,			hidden_size=37   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			self.config_tester.run_common_tests()



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_model(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_masked_lm(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Union[str, Any]         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_question_answering(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Dict         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_sequence_classification(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Dict         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_multiple_choice(*a_   )



		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Dict         =							self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_mpnet_for_token_classification(*a_   )



		@slow
		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

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



@require_tf
class      _UpperCAmelCase					(				unittest.TestCase							):






		'''simple docstring'''

		@slow
		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			__snake_case      :		Optional[int]         =							TFMPNetModel.from_pretrained('''microsoft/mpnet-base'''   )
			__snake_case      :		Tuple         =							tf.constant([[0, 1, 2, 3, 4, 5]]   )
			__snake_case      :		List[str]         =							model(a_   )[0]

			__snake_case      :		List[str]         =							[1, 6, 7_68]
			self.assertEqual(output.shape							,			a_   )

			__snake_case      :		Optional[int]         =							tf.constant(
			    [
			        [
			            [-0.106_7172, 0.0821_6473, 0.002_4543],
			            [-0.0346_5879, 0.835_4118, -0.0325_2288],
			            [-0.0656_9476, -0.1242_4111, -0.049_4436],
			        ]
			    ]   )
			tf.debugging.assert_near(output[:, :3, :3]							,			a_							,			atol=1E-4   )










'''simple docstring'''







from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	bool						) ->							torch.Tensor:
							# The first operation in a checkpoint can't be in-place, but it's
							# nice to have in-place addition during inference. Thus...
							if not inplace:
														lowerCAmelCase__  =   ma + ma
							else:
														ma += ma

							return ma
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	List[int]						) ->							torch.Tensor:
							lowerCAmelCase__  =   -1 * len(UpperCAmelCase_						)
							lowerCAmelCase__  =   list(range(len(tensor.shape[:zero_index]						)						)						)
							return tensor.permute(first_inds + [zero_index + i for i in inds]						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int						) ->							torch.Tensor:
							return t.reshape(t.shape[:-no_dims] + (-1,)						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int					, UpperCAmelCase_      :	float = 1E-4						) ->							torch.Tensor:
							lowerCAmelCase__  =   mask.expand(*value.shape						)
							return torch.sum(mask * value					, dim=UpperCAmelCase_						) / (eps + torch.sum(UpperCAmelCase_					, dim=UpperCAmelCase_						))
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.types.Number = 2.31_25					, UpperCAmelCase_      :	torch.types.Number = 21.68_75					, UpperCAmelCase_      :	int = 64						) ->							torch.Tensor:
							lowerCAmelCase__  =   torch.linspace(UpperCAmelCase_					, UpperCAmelCase_					, no_bins - 1					, device=pts.device						)
							lowerCAmelCase__  =   torch.sqrt(torch.sum((pts.unsqueeze(-2						) - pts.unsqueeze(-3						)) ** 2					, dim=-1						)						)
							return torch.bucketize(UpperCAmelCase_					, UpperCAmelCase_						)
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[list], Any]					, UpperCAmelCase_      :	List[dict]						) ->							dict:
							lowerCAmelCase__  =   dicts[0]
							lowerCAmelCase__  =   {}
							for k, v in first.items():
														lowerCAmelCase__  =   [d[k] for d in dicts]
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_multimap(UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   fn(UpperCAmelCase_						)

							return new_dict
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor						) ->							torch.Tensor:
							lowerCAmelCase__  =   v_bins.view(((1,) * len(x.shape						)) + (len(UpperCAmelCase_						),)						)
							lowerCAmelCase__  =   x[..., None] - reshaped_bins
							lowerCAmelCase__  =   torch.argmin(torch.abs(UpperCAmelCase_						)					, dim=-1						)
							return nn.functional.one_hot(UpperCAmelCase_					, num_classes=len(UpperCAmelCase_						)						).float()
def 			_lowerCAmelCase(      UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	torch.Tensor					, UpperCAmelCase_      :	int = 0					, UpperCAmelCase_      :	int = 0						) ->							torch.Tensor:
							lowerCAmelCase__  =   []
							for i, s in enumerate(data.shape[:no_batch_dims]						):
														lowerCAmelCase__  =   torch.arange(UpperCAmelCase_						)
														lowerCAmelCase__  =   r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape						) - i - 1)))						)
														ranges.append(UpperCAmelCase_						)

							lowerCAmelCase__  =   [slice(UpperCAmelCase_						) for _ in range(len(data.shape						) - no_batch_dims						)]
							lowerCAmelCase__  =   inds
							ranges.extend(UpperCAmelCase_						)
							# Matt note: Editing this to get around the behaviour of using a list as an array index changing
							# in recent Numpy versions
							return data[tuple(UpperCAmelCase_						)]


_UpperCamelCase  						=			TypeVar("""T""")
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	Dict[Any, Union[dict, list, tuple, T]]					, UpperCAmelCase_      :	Type[T]						) ->							Dict[Any, Union[dict, list, tuple, Any]]:
							lowerCAmelCase__  =   {}
							for k, v in dic.items():
														if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
																					lowerCAmelCase__  =   dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
														else:
																					lowerCAmelCase__  =   tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)

							return new_dict
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	T					, UpperCAmelCase_      :	Type[T]						) ->							Any:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	dict					, UpperCAmelCase_      :	Type[T]						) ->							dict:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	list					, UpperCAmelCase_      :	Type[T]						) ->							list:
							...
@overload
def 			_lowerCAmelCase(      UpperCAmelCase_      :	Callable[[T], Any]					, UpperCAmelCase_      :	tuple					, UpperCAmelCase_      :	Type[T]						) ->							tuple:
							...
def 			_lowerCAmelCase(      UpperCAmelCase_      :	str					, UpperCAmelCase_      :	Dict					, UpperCAmelCase_      :	Union[str, Any]						) ->							List[str]:
							if isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return dict_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return [tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree]
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return tuple(tree_map(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_						) for x in tree						)
							elif isinstance(UpperCAmelCase_					, UpperCAmelCase_						):
														return fn(UpperCAmelCase_						)
							else:
														print(type(UpperCAmelCase_						)						)
														raise ValueError("""Not supported"""						)






_UpperCamelCase  						=			partial(tree_map, leaf_type=torch.Tensor)





