



'''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 __snake_case:


						'''simple docstring'''



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


						def     __snake_case   ( self				)     ->     Optional[int]:
							return MPNetConfig.from_pretrained("""microsoft/mpnet-base"""				)


						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     ids_tensor([self.batch_size, self.seq_length]     ,	self.vocab_size				)

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

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

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


						def     __snake_case   ( self				)     ->     str:
							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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Union[str, Any]:
							lowerCAmelCase   =     MPNetModel(config=A_				)
							model.to(A_				)
							model.eval()
							lowerCAmelCase   =     model(A_     ,	A_				)
							lowerCAmelCase   =     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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     List[Any]:
							lowerCAmelCase   =     MPNetForQuestionAnswering(config=A_				)
							model.to(A_				)
							model.eval()
							lowerCAmelCase   =     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     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Optional[int]:
							lowerCAmelCase   =     self.num_labels
							lowerCAmelCase   =     MPNetForSequenceClassification(A_				)
							model.to(A_				)
							model.eval()
							lowerCAmelCase   =     model(A_     ,	attention_mask=A_     ,	labels=A_				)
							self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.num_labels)				)


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


						def     __snake_case   ( self     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_     ,	A_				)     ->     Tuple:
							lowerCAmelCase   =     self.num_labels
							lowerCAmelCase   =     MPNetForTokenClassification(config=A_				)
							model.to(A_				)
							model.eval()
							lowerCAmelCase   =     model(A_     ,	attention_mask=A_     ,	labels=A_				)
							self.parent.assertEqual(result.logits.shape     ,	(self.batch_size, self.seq_length, self.num_labels)				)




						def     __snake_case   ( self				)     ->     Union[str, Any]:
							lowerCAmelCase   =     self.prepare_config_and_inputs()
							((lowerCAmelCase),       (lowerCAmelCase),       (lowerCAmelCase),       (lowerCAmelCase),       (lowerCAmelCase),       (lowerCAmelCase))   =     config_and_inputs
							lowerCAmelCase   =     {"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict







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


						'''simple docstring'''



						UpperCAmelCase							:   List[str]									=		(
						    (
						        MPNetForMaskedLM,
						        MPNetForMultipleChoice,
						        MPNetForQuestionAnswering,
						        MPNetForSequenceClassification,
						        MPNetForTokenClassification,
						        MPNetModel,
						    )
						    if is_torch_available()
						    else ()
						)
						UpperCAmelCase							:   List[str]									=		(
						    {
						        "feature-extraction": MPNetModel,
						        "fill-mask": MPNetForMaskedLM,
						        "question-answering": MPNetForQuestionAnswering,
						        "text-classification": MPNetForSequenceClassification,
						        "token-classification": MPNetForTokenClassification,
						        "zero-shot": MPNetForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						UpperCAmelCase							:   Any									=		False
						UpperCAmelCase							:   Union[str, Any]									=		True


						def     __snake_case   ( self				)     ->     int:
							lowerCAmelCase   =     MPNetModelTester(self				)
							lowerCAmelCase   =     ConfigTester(self     ,	config_class=A_     ,	hidden_size=37				)


						def     __snake_case   ( self				)     ->     int:
							self.config_tester.run_common_tests()


						def     __snake_case   ( self				)     ->     int:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_model(*A_				)


						def     __snake_case   ( self				)     ->     str:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_sequence_classification(*A_				)


						def     __snake_case   ( self				)     ->     Tuple:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_multiple_choice(*A_				)


						def     __snake_case   ( self				)     ->     Optional[Any]:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_token_classification(*A_				)




						def     __snake_case   ( self				)     ->     int:
							lowerCAmelCase   =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_mpnet_for_question_answering(*A_				)







@require_torch
class __snake_case(				unittest.TestCase ):


						'''simple docstring'''



						@slow
						def     __snake_case   ( self				)     ->     Optional[Any]:
							lowerCAmelCase   =     MPNetModel.from_pretrained("""microsoft/mpnet-base"""				)
							lowerCAmelCase   =     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]				)
							lowerCAmelCase   =     model(A_				)[0]
							lowerCAmelCase   =     torch.Size((1, 11, 768)				)
							self.assertEqual(output.shape     ,	A_				)
							lowerCAmelCase   =     torch.tensor(
							    [[[-0.0_5_5_0, 0.1_9_4_3, -0.0_7_4_0], [-0.0_5_6_2, 0.2_2_1_1, -0.0_5_7_9], [-0.0_4_3_7, 0.3_3_3_7, -0.0_6_4_1]]]				)
							# compare the actual values for a slice.
							self.assertTrue(torch.allclose(output[:, :3, :3]     ,	A_     ,	atol=1e-4				)				)




'''simple docstring'''

from torch import nn






def 		_snake_case		( _SCREAMING_SNAKE_CASE					:						Optional[int]							)      ->						Union[str, Any]:







	"""simple docstring"""
	if act_fn in ["swish", "silu"]:
		return nn.SiLU()
	elif act_fn == "mish":
		return nn.Mish()
	elif act_fn == "gelu":
		return nn.GELU()
	else:
		raise ValueError(f'Unsupported activation function: {act_fn}'							)