





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							:
 def __init__(   self						:   Optional[int]    ,  UpperCAmelCase						:   Union[str, Any]    ,  UpperCAmelCase						:   Optional[Any]=13    ,  UpperCAmelCase						:   Optional[Any]=7    ,  UpperCAmelCase						:   Any=True    ,  UpperCAmelCase						:   List[str]=True    ,  UpperCAmelCase						:   Tuple=False    ,  UpperCAmelCase						:   Optional[Any]=True    ,  UpperCAmelCase						:   int=99    ,  UpperCAmelCase						:   Optional[Any]=64    ,  UpperCAmelCase						:   Optional[int]=5    ,  UpperCAmelCase						:   List[str]=4    ,  UpperCAmelCase						:   Union[str, Any]=64    ,  UpperCAmelCase						:   Tuple="gelu"    ,  UpperCAmelCase						:   Union[str, Any]=0.1    ,  UpperCAmelCase						:   Dict=0.1    ,  UpperCAmelCase						:   Optional[int]=512    ,  UpperCAmelCase						:   Optional[int]=16    ,  UpperCAmelCase						:   List[str]=2    ,  UpperCAmelCase						:   Union[str, Any]=0.0_2    ,  UpperCAmelCase						:   List[str]=3    ,  UpperCAmelCase						:   Optional[int]=4    ,  UpperCAmelCase						:   Optional[Any]=None    ,  )    ->			int:
     lowerCamelCase__					:   List[str]      				=	parent
     lowerCamelCase__					:   Any      				=	batch_size
     lowerCamelCase__					:   Optional[int]      				=	seq_length
     lowerCamelCase__					:   Union[str, Any]      				=	is_training
     lowerCamelCase__					:   Tuple      				=	use_input_mask
     lowerCamelCase__					:   Tuple      				=	use_token_type_ids
     lowerCamelCase__					:   int      				=	use_labels
     lowerCamelCase__					:   List[Any]      				=	vocab_size
     lowerCamelCase__					:   Optional[Any]      				=	hidden_size
     lowerCamelCase__					:   Any      				=	num_hidden_layers
     lowerCamelCase__					:   str      				=	num_attention_heads
     lowerCamelCase__					:   Any      				=	intermediate_size
     lowerCamelCase__					:   Optional[int]      				=	hidden_act
     lowerCamelCase__					:   str      				=	hidden_dropout_prob
     lowerCamelCase__					:   Tuple      				=	attention_probs_dropout_prob
     lowerCamelCase__					:   Union[str, Any]      				=	max_position_embeddings
     lowerCamelCase__					:   List[Any]      				=	type_vocab_size
     lowerCamelCase__					:   int      				=	type_sequence_label_size
     lowerCamelCase__					:   List[Any]      				=	initializer_range
     lowerCamelCase__					:   Dict      				=	num_labels
     lowerCamelCase__					:   str      				=	num_choices
     lowerCamelCase__					:   List[Any]      				=	scope
 def   A_			(   self						:   Any			)    ->			List[Any]:
     return MPNetConfig.from_pretrained('microsoft/mpnet-base'			)
 def   A_			(   self						:   List[Any]			)    ->			Any:
     lowerCamelCase__					:   Optional[int]      				=	ids_tensor([self.batch_size, self.seq_length]    ,  self.vocab_size			)

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

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

     lowerCamelCase__					:   List[str]      				=	self.get_config()
     return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def   A_			(   self						:   Dict			)    ->			Tuple:
     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						:   Union[str, Any]    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   str    ,  UpperCAmelCase						:   str    ,  UpperCAmelCase						:   Tuple			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Union[str, Any]      				=	MPNetModel(config=UpperCAmelCase			)
     model.to(UpperCAmelCase			)
     model.eval()
     lowerCamelCase__					:   str      				=	model(UpperCAmelCase    ,  UpperCAmelCase			)
     lowerCamelCase__					:   Dict      				=	model(UpperCAmelCase			)
     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						:   List[Any]    ,  UpperCAmelCase						:   Any    ,  UpperCAmelCase						:   Dict    ,  UpperCAmelCase						:   List[Any]    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   str    ,  UpperCAmelCase						:   List[str]			)    ->			Optional[int]:
     lowerCamelCase__					:   int      				=	MPNetForQuestionAnswering(config=UpperCAmelCase			)
     model.to(UpperCAmelCase			)
     model.eval()
     lowerCamelCase__					:   Union[str, Any]      				=	model(
         UpperCAmelCase    ,  attention_mask=UpperCAmelCase    ,  start_positions=UpperCAmelCase    ,  end_positions=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						:   Union[str, Any]    ,  UpperCAmelCase						:   Any    ,  UpperCAmelCase						:   Optional[int]    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   str    ,  UpperCAmelCase						:   Optional[Any]    ,  UpperCAmelCase						:   Optional[int]			)    ->			Any:
     lowerCamelCase__					:   Tuple      				=	self.num_labels
     lowerCamelCase__					:   Any      				=	MPNetForSequenceClassification(UpperCAmelCase			)
     model.to(UpperCAmelCase			)
     model.eval()
     lowerCamelCase__					:   Tuple      				=	model(UpperCAmelCase    ,  attention_mask=UpperCAmelCase    ,  labels=UpperCAmelCase			)
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_labels)			)
 def   A_			(   self						:   Dict    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   Tuple    ,  UpperCAmelCase						:   Optional[Any]    ,  UpperCAmelCase						:   List[str]    ,  UpperCAmelCase						:   List[Any]    ,  UpperCAmelCase						:   Optional[Any]			)    ->			Optional[int]:
     lowerCamelCase__					:   Tuple      				=	self.num_choices
     lowerCamelCase__					:   Dict      				=	MPNetForMultipleChoice(config=UpperCAmelCase			)
     model.to(UpperCAmelCase			)
     model.eval()
     lowerCamelCase__					:   int      				=	input_ids.unsqueeze(1			).expand(-1    ,  self.num_choices    ,  -1			).contiguous()
     lowerCamelCase__					:   Optional[int]      				=	input_mask.unsqueeze(1			).expand(-1    ,  self.num_choices    ,  -1			).contiguous()
     lowerCamelCase__					:   Any      				=	model(
         UpperCAmelCase    ,  attention_mask=UpperCAmelCase    ,  labels=UpperCAmelCase    ,  )
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.num_choices)			)
 def   A_			(   self						:   str    ,  UpperCAmelCase						:   List[Any]    ,  UpperCAmelCase						:   Dict    ,  UpperCAmelCase						:   str    ,  UpperCAmelCase						:   int    ,  UpperCAmelCase						:   int    ,  UpperCAmelCase						:   Any			)    ->			Any:
     lowerCamelCase__					:   Union[str, Any]      				=	self.num_labels
     lowerCamelCase__					:   str      				=	MPNetForTokenClassification(config=UpperCAmelCase			)
     model.to(UpperCAmelCase			)
     model.eval()
     lowerCamelCase__					:   Tuple      				=	model(UpperCAmelCase    ,  attention_mask=UpperCAmelCase    ,  labels=UpperCAmelCase			)
     self.parent.assertEqual(result.logits.shape    ,  (self.batch_size, self.seq_length, self.num_labels)			)







 def   A_			(   self						:   List[Any]			)    ->			int:
     lowerCamelCase__					:   Dict      				=	self.prepare_config_and_inputs()
     ((lowerCamelCase__)				,							(lowerCamelCase__)				,							(lowerCamelCase__)				,							(lowerCamelCase__)				,							(lowerCamelCase__)				,							(lowerCamelCase__))					:   Optional[int]      				=	config_and_inputs
     lowerCamelCase__					:   int      				=	{'input_ids': input_ids, 'attention_mask': input_mask}
     return config, inputs_dict


@require_torch
class      lowerCAmelCase							(      __UpperCamelCase,						__UpperCamelCase,						unittest.TestCase    ):
 UpperCAmelCase__									=	(
     (
         MPNetForMaskedLM,
         MPNetForMultipleChoice,
         MPNetForQuestionAnswering,
         MPNetForSequenceClassification,
         MPNetForTokenClassification,
         MPNetModel,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase__									=	(
     {
         """feature-extraction""": MPNetModel,
         """fill-mask""": MPNetForMaskedLM,
         """question-answering""": MPNetForQuestionAnswering,
         """text-classification""": MPNetForSequenceClassification,
         """token-classification""": MPNetForTokenClassification,
         """zero-shot""": MPNetForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 UpperCAmelCase__									=	False
 UpperCAmelCase__									=	True
 def   A_			(   self						:   Union[str, Any]			)    ->			Dict:
     lowerCamelCase__					:   List[Any]      				=	MPNetModelTester(self			)
     lowerCamelCase__					:   Any      				=	ConfigTester(self    ,  config_class=UpperCAmelCase    ,  hidden_size=37			)
 def   A_			(   self						:   Any			)    ->			str:
     self.config_tester.run_common_tests()
 def   A_			(   self						:   Optional[int]			)    ->			Optional[Any]:
     lowerCamelCase__					:   List[Any]      				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_model(*UpperCAmelCase			)
 def   A_			(   self						:   str			)    ->			List[Any]:
     lowerCamelCase__					:   Dict      				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_sequence_classification(*UpperCAmelCase			)
 def   A_			(   self						:   Optional[int]			)    ->			Dict:
     lowerCamelCase__					:   List[str]      				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_multiple_choice(*UpperCAmelCase			)
 def   A_			(   self						:   str			)    ->			Optional[int]:
     lowerCamelCase__					:   List[Any]      				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_token_classification(*UpperCAmelCase			)







 def   A_			(   self						:   Any			)    ->			Tuple:
     lowerCamelCase__					:   List[str]      				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_mpnet_for_question_answering(*UpperCAmelCase			)


@require_torch
class      lowerCAmelCase							(      unittest.TestCase    ):
 @slow
 def   A_			(   self						:   Dict			)    ->			Tuple:
     lowerCamelCase__					:   int      				=	MPNetModel.from_pretrained('microsoft/mpnet-base'			)
     lowerCamelCase__					:   Dict      				=	torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]			)
     lowerCamelCase__					:   Dict      				=	model(UpperCAmelCase			)[0]
     lowerCamelCase__					:   Optional[int]      				=	torch.Size((1, 11, 768)			)
     self.assertEqual(output.shape    ,  UpperCAmelCase			)
     lowerCamelCase__					:   int      				=	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]    ,  UpperCAmelCase    ,  atol=1e-4			)			)












import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
    import torch

    from transformers.activations import gelu_new, gelu_python, get_activation

@require_torch
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   Tuple			)    ->			int:
     lowerCamelCase__					:   Optional[int]      				=	torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]			)
     lowerCamelCase__					:   Dict      				=	get_activation('gelu'			)
     self.assertTrue(torch.allclose(gelu_python(UpperCAmelCase			)    ,  torch_builtin(UpperCAmelCase			)			)			)
     self.assertFalse(torch.allclose(gelu_python(UpperCAmelCase			)    ,  gelu_new(UpperCAmelCase			)			)			)
 def   A_			(   self						:   Dict			)    ->			str:
     lowerCamelCase__					:   Optional[int]      				=	torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]			)
     lowerCamelCase__					:   str      				=	get_activation('gelu'			)
     lowerCamelCase__					:   Tuple      				=	get_activation('gelu_10'			)

     lowerCamelCase__					:   Tuple      				=	torch_builtin(UpperCAmelCase			)
     lowerCamelCase__					:   List[str]      				=	geluaa(UpperCAmelCase			)

     lowerCamelCase__					:   Tuple      				=	torch.where(y_gelu_aa < 1_0.0    ,  1    ,  0			)

     self.assertTrue(torch.max(UpperCAmelCase			).item() == 1_0.0			)
     self.assertTrue(torch.allclose(y_gelu * clipped_mask    ,  y_gelu_aa * clipped_mask			)			)
 def   A_			(   self						:   str			)    ->			List[str]:
     get_activation('gelu'			)
     get_activation('gelu_10'			)
     get_activation('gelu_fast'			)
     get_activation('gelu_new'			)
     get_activation('gelu_python'			)
     get_activation('gelu_pytorch_tanh'			)
     get_activation('linear'			)
     get_activation('mish'			)
     get_activation('quick_gelu'			)
     get_activation('relu'			)
     get_activation('sigmoid'			)
     get_activation('silu'			)
     get_activation('swish'			)
     get_activation('tanh'			)
     with self.assertRaises(UpperCAmelCase			):
         get_activation('bogus'			)
     with self.assertRaises(UpperCAmelCase			):
         get_activation(UpperCAmelCase			)







 def   A_			(   self						:   Any			)    ->			Optional[Any]:
     lowerCamelCase__					:   Optional[Any]      				=	get_activation('gelu'			)
     lowerCamelCase__					:   Union[str, Any]      				=	1
     lowerCamelCase__					:   Optional[Any]      				=	get_activation('gelu'			)
     self.assertEqual(acta.a    ,  1			)
     with self.assertRaises(UpperCAmelCase			):
         lowerCamelCase__					:   str      				=	acta.a





