



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





		"""simple docstring"""






		def __init__(       self				,     _lowercase				,     _lowercase=1_3				,     _lowercase=7				,     _lowercase=True				,     _lowercase=True				,     _lowercase=False				,     _lowercase=True				,     _lowercase=9_9				,     _lowercase=6_4				,     _lowercase=5				,     _lowercase=4				,     _lowercase=6_4				,     _lowercase="gelu"				,     _lowercase=0.1				,     _lowercase=0.1				,     _lowercase=5_1_2				,     _lowercase=1_6				,     _lowercase=2				,     _lowercase=0.02				,     _lowercase=3				,     _lowercase=4				,     _lowercase=None				,     )			->    List[str]:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

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





		def  UpperCAmelCase__				(       self )			->    Any:







								'''simple docstring'''

								snake_case_ :			List[str]            =      ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size )

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

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

								snake_case_ :			List[str]            =      self.get_config()
								return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''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  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			Any            =      MPNetModel(config=__lowerCamelCase )
								model.to(__lowerCamelCase )
								model.eval()
								snake_case_ :			Optional[Any]            =      model(__lowerCamelCase				,     __lowerCamelCase )
								snake_case_ :			Optional[int]            =      model(__lowerCamelCase )
								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  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    List[str]:







								'''simple docstring'''

								snake_case_ :			Dict            =      MPNetForQuestionAnswering(config=__lowerCamelCase )
								model.to(__lowerCamelCase )
								model.eval()
								snake_case_ :			Any            =      model(
								    __lowerCamelCase				,     attention_mask=__lowerCamelCase				,     start_positions=__lowerCamelCase				,     end_positions=__lowerCamelCase				,     )
								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				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Any:







								'''simple docstring'''

								snake_case_ :			Optional[Any]            =      self.num_labels
								snake_case_ :			Any            =      MPNetForSequenceClassification(__lowerCamelCase )
								model.to(__lowerCamelCase )
								model.eval()
								snake_case_ :			List[str]            =      model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels) )





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







								'''simple docstring'''

								snake_case_ :			Tuple            =      self.num_choices
								snake_case_ :			str            =      MPNetForMultipleChoice(config=__lowerCamelCase )
								model.to(__lowerCamelCase )
								model.eval()
								snake_case_ :			List[Any]            =      input_ids.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
								snake_case_ :			Optional[int]            =      input_mask.unsqueeze(1 ).expand(-1				,     self.num_choices				,     -1 ).contiguous()
								snake_case_ :			str            =      model(
								    __lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase				,     )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_choices) )





		def  UpperCAmelCase__				(       self				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase				,     _lowercase )			->    Optional[Any]:







								'''simple docstring'''

								snake_case_ :			Any            =      self.num_labels
								snake_case_ :			Dict            =      MPNetForTokenClassification(config=__lowerCamelCase )
								model.to(__lowerCamelCase )
								model.eval()
								snake_case_ :			Any            =      model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase )
								self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels) )





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Tuple            =      self.prepare_config_and_inputs()
								((snake_case_)      ,		(snake_case_)      ,		(snake_case_)      ,		(snake_case_)      ,		(snake_case_)      ,		(snake_case_)) :			List[str]            =      config_and_inputs
								snake_case_ :			List[str]            =      {"""input_ids""": input_ids, """attention_mask""": input_mask}
								return config, inputs_dict




@require_torch
class  _lowerCAmelCase     (					UpperCamelCase__	,					UpperCamelCase__	,					unittest.TestCase		):





		"""simple docstring"""






		_lowerCamelCase      	=							(
		    (
		        MPNetForMaskedLM,
		        MPNetForMultipleChoice,
		        MPNetForQuestionAnswering,
		        MPNetForSequenceClassification,
		        MPNetForTokenClassification,
		        MPNetModel,
		    )
		    if is_torch_available()
		    else ()
		)
		_lowerCamelCase      	=							(
		    {
		        '''feature-extraction''': MPNetModel,
		        '''fill-mask''': MPNetForMaskedLM,
		        '''question-answering''': MPNetForQuestionAnswering,
		        '''text-classification''': MPNetForSequenceClassification,
		        '''token-classification''': MPNetForTokenClassification,
		        '''zero-shot''': MPNetForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_lowerCamelCase      	=							False
		_lowerCamelCase      	=							True





		def  UpperCAmelCase__				(       self )			->    List[Any]:







								'''simple docstring'''

								snake_case_ :			int            =      MPNetModelTester(self )
								snake_case_ :			Optional[int]            =      ConfigTester(self				,     config_class=__lowerCamelCase				,     hidden_size=3_7 )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								self.config_tester.run_common_tests()





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Any            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_model(*__lowerCamelCase )





		def  UpperCAmelCase__				(       self )			->    Dict:







								'''simple docstring'''

								snake_case_ :			Optional[int]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCamelCase )





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







								'''simple docstring'''

								snake_case_ :			Optional[Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCamelCase )





		def  UpperCAmelCase__				(       self )			->    str:







								'''simple docstring'''

								snake_case_ :			int            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCamelCase )





		def  UpperCAmelCase__				(       self )			->    Tuple:







								'''simple docstring'''

								snake_case_ :			List[Any]            =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCamelCase )




@require_torch
class  _lowerCAmelCase     (					unittest.TestCase		):





		"""simple docstring"""






		@slow
		def  UpperCAmelCase__				(       self )			->    Optional[int]:







								'''simple docstring'''

								snake_case_ :			Union[str, Any]            =      MPNetModel.from_pretrained("""microsoft/mpnet-base""" )
								snake_case_ :			int            =      torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]] )
								snake_case_ :			str            =      model(__lowerCamelCase )[0]
								snake_case_ :			Any            =      torch.Size((1, 1_1, 7_6_8) )
								self.assertEqual(output.shape				,     __lowerCamelCase )
								snake_case_ :			int            =      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]				,     __lowerCamelCase				,     atol=1E-4 ) )








a__: str							   =						'0.21.0'

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
       from .utils import rich





