




'''simple docstring'''

import unittest

from transformers import MPNetConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    MPNetForMaskedLM,
						    MPNetForMultipleChoice,
						    MPNetForQuestionAnswering,
						    MPNetForSequenceClassification,
						    MPNetForTokenClassification,
						    MPNetModel,
						)



class 		SCREAMING_SNAKE_CASE__  :





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



						"""simple docstring"""




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





			def 							A (      self						:    int							):



						"""simple docstring"""




						return MPNetConfig.from_pretrained("microsoft/mpnet-base"							)





			def 							A (      self						:    str							):



						"""simple docstring"""




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

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

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

						__snake_case			=		self.get_config()
						return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





			def 							A (      self						:    List[str]							):



						"""simple docstring"""




						return 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      ,		)





			def 							A (      self						:    Tuple      ,		a_						:    int      ,		a_						:    str      ,		a_						:    Optional[int]      ,		a_						:    List[Any]      ,		a_						:    str      ,		a_						:    Optional[Any]							):



						"""simple docstring"""




						__snake_case			=		MPNetModel(config=a_							)
						model.to(a_							)
						model.eval()
						__snake_case			=		model(a_      ,		a_							)
						__snake_case			=		model(a_							)
						self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)							)
						self.parent.assertEqual(result.pooler_output.shape      ,		(self.batch_size, self.hidden_size)							)





			def 							A (      self						:    Any      ,		a_						:    int      ,		a_						:    Tuple      ,		a_						:    str      ,		a_						:    int      ,		a_						:    str      ,		a_						:    List[Any]							):



						"""simple docstring"""




						__snake_case			=		MPNetForQuestionAnswering(config=a_							)
						model.to(a_							)
						model.eval()
						__snake_case			=		model(
						    a_      ,		attention_mask=a_      ,		start_positions=a_      ,		end_positions=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 							A (      self						:    Any      ,		a_						:    Any      ,		a_						:    int      ,		a_						:    Union[str, Any]      ,		a_						:    Dict      ,		a_						:    Optional[Any]      ,		a_						:    Any							):



						"""simple docstring"""




						__snake_case			=		self.num_labels
						__snake_case			=		MPNetForSequenceClassification(a_							)
						model.to(a_							)
						model.eval()
						__snake_case			=		model(a_      ,		attention_mask=a_      ,		labels=a_							)
						self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)							)





			def 							A (      self						:    Optional[Any]      ,		a_						:    Any      ,		a_						:    Union[str, Any]      ,		a_						:    Union[str, Any]      ,		a_						:    Union[str, Any]      ,		a_						:    List[Any]      ,		a_						:    List[Any]							):



						"""simple docstring"""




						__snake_case			=		self.num_choices
						__snake_case			=		MPNetForMultipleChoice(config=a_							)
						model.to(a_							)
						model.eval()
						__snake_case			=		input_ids.unsqueeze(1							).expand(-1      ,		self.num_choices      ,		-1							).contiguous()
						__snake_case			=		input_mask.unsqueeze(1							).expand(-1      ,		self.num_choices      ,		-1							).contiguous()
						__snake_case			=		model(
						    a_      ,		attention_mask=a_      ,		labels=a_      ,		)
						self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_choices)							)





			def 							A (      self						:    Dict      ,		a_						:    List[str]      ,		a_						:    str      ,		a_						:    Union[str, Any]      ,		a_						:    str      ,		a_						:    Optional[int]      ,		a_						:    Optional[Any]							):



						"""simple docstring"""




						__snake_case			=		self.num_labels
						__snake_case			=		MPNetForTokenClassification(config=a_							)
						model.to(a_							)
						model.eval()
						__snake_case			=		model(a_      ,		attention_mask=a_      ,		labels=a_							)
						self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.seq_length, self.num_labels)							)






			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




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







@require_torch
class 		SCREAMING_SNAKE_CASE__  (			_UpperCamelCase				,     _UpperCamelCase				,     unittest.TestCase	):
			__SCREAMING_SNAKE_CASE    =			(
			    (
			        MPNetForMaskedLM,
			        MPNetForMultipleChoice,
			        MPNetForQuestionAnswering,
			        MPNetForSequenceClassification,
			        MPNetForTokenClassification,
			        MPNetModel,
			    )
			    if is_torch_available()
			    else ()
			)
			__SCREAMING_SNAKE_CASE    =			(
			    {
			        """feature-extraction""": MPNetModel,
			        """fill-mask""": MPNetForMaskedLM,
			        """question-answering""": MPNetForQuestionAnswering,
			        """text-classification""": MPNetForSequenceClassification,
			        """token-classification""": MPNetForTokenClassification,
			        """zero-shot""": MPNetForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			__SCREAMING_SNAKE_CASE    =			False
			__SCREAMING_SNAKE_CASE    =			True





			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




						__snake_case			=		MPNetModelTester(self							)
						__snake_case			=		ConfigTester(self      ,		config_class=a_      ,		hidden_size=37							)





			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




						self.config_tester.run_common_tests()





			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




						__snake_case			=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_model(*a_							)





			def 							A (      self						:    Dict							):



						"""simple docstring"""




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





			def 							A (      self						:    List[Any]							):



						"""simple docstring"""




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





			def 							A (      self						:    int							):



						"""simple docstring"""




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






			def 							A (      self						:    Union[str, Any]							):



						"""simple docstring"""




						__snake_case			=		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_mpnet_for_question_answering(*a_							)







@require_torch
class 		SCREAMING_SNAKE_CASE__  (			unittest.TestCase	):





			@slow
			def 							A (      self						:    Optional[Any]							):



						"""simple docstring"""




						__snake_case			=		MPNetModel.from_pretrained("microsoft/mpnet-base"							)
						__snake_case			=		torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]							)
						__snake_case			=		model(a_							)[0]
						__snake_case			=		torch.Size((1, 11, 768)							)
						self.assertEqual(output.shape      ,		a_							)
						__snake_case			=		torch.tensor(
						    [[[-0.0550, 0.1943, -0.0740], [-0.0562, 0.2211, -0.0579], [-0.0437, 0.3337, -0.0641]]]							)
						# compare the actual values for a slice.
						self.assertTrue(torch.allclose(output[:, :3, :3]      ,		a_      ,		atol=1e-4							)							)













"""simple docstring"""
from __future__ import annotations






def       __UpperCamelCase						(		snake_case__ , snake_case__ = None , snake_case__ = None       ):
	if start is None:
		A_		:    Dict						     = 0

	if end is None:
		A_		:    Dict						     = len(snake_case__       ) - 1

	if start >= end:
		return

	A_		:    List[Any]						     = (start + end) // 2

	slowsort(snake_case__ , snake_case__ , snake_case__       )
	slowsort(snake_case__ , mid + 1 , snake_case__       )

	if sequence[end] < sequence[mid]:
		A_     ,  A_		:    Dict						     = sequence[mid], sequence[end]

	slowsort(snake_case__ , snake_case__ , end - 1       )


if __name__ == "__main__":
							from doctest import testmod

							testmod()

