





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,__lowerCamelCase,__lowerCamelCase=13,__lowerCamelCase=7,__lowerCamelCase=True,__lowerCamelCase=True,__lowerCamelCase=False,__lowerCamelCase=True,__lowerCamelCase=99,__lowerCamelCase=64,__lowerCamelCase=5,__lowerCamelCase=4,__lowerCamelCase=64,__lowerCamelCase="gelu",__lowerCamelCase=0.1,__lowerCamelCase=0.1,__lowerCamelCase=512,__lowerCamelCase=16,__lowerCamelCase=2,__lowerCamelCase=0.02,__lowerCamelCase=3,__lowerCamelCase=4,__lowerCamelCase=None,):
        A__		   =		parent
        A__		   =		batch_size
        A__		   =		seq_length
        A__		   =		is_training
        A__		   =		use_input_mask
        A__		   =		use_token_type_ids
        A__		   =		use_labels
        A__		   =		vocab_size
        A__		   =		hidden_size
        A__		   =		num_hidden_layers
        A__		   =		num_attention_heads
        A__		   =		intermediate_size
        A__		   =		hidden_act
        A__		   =		hidden_dropout_prob
        A__		   =		attention_probs_dropout_prob
        A__		   =		max_position_embeddings
        A__		   =		type_vocab_size
        A__		   =		type_sequence_label_size
        A__		   =		initializer_range
        A__		   =		num_labels
        A__		   =		num_choices
        A__		   =		scope



 def 							UpperCamelCase     (				self					):
        return MPNetConfig.from_pretrained('''microsoft/mpnet-base'''					)



 def 							UpperCamelCase     (				self					):
        A__		   =		ids_tensor([self.batch_size, self.seq_length],self.vocab_size					)

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

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

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



 def 							UpperCamelCase     (				self					):
        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,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		MPNetModel(config=__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()
        A__		   =		model(__lowerCamelCase,__lowerCamelCase					)
        A__		   =		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,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		MPNetForQuestionAnswering(config=__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()
        A__		   =		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,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		self.num_labels
        A__		   =		MPNetForSequenceClassification(__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()
        A__		   =		model(__lowerCamelCase,attention_mask=__lowerCamelCase,labels=__lowerCamelCase					)
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_labels)					)



 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		self.num_choices
        A__		   =		MPNetForMultipleChoice(config=__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()
        A__		   =		input_ids.unsqueeze(1					).expand(-1,self.num_choices,-1					).contiguous()
        A__		   =		input_mask.unsqueeze(1					).expand(-1,self.num_choices,-1					).contiguous()
        A__		   =		model(
            __lowerCamelCase,attention_mask=__lowerCamelCase,labels=__lowerCamelCase,)
        self.parent.assertEqual(result.logits.shape,(self.batch_size, self.num_choices)					)



 def 							UpperCamelCase     (				self,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase,__lowerCamelCase					):
        A__		   =		self.num_labels
        A__		   =		MPNetForTokenClassification(config=__lowerCamelCase					)
        model.to(__lowerCamelCase					)
        model.eval()
        A__		   =		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					):
        A__		   =		self.prepare_config_and_inputs()
        ((A__)   ,      (A__)   ,      (A__)   ,      (A__)   ,      (A__)   ,      (A__))		   =		config_and_inputs
        A__		   =		{'''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 							UpperCamelCase     (				self					):
        A__		   =		MPNetModelTester(self					)
        A__		   =		ConfigTester(self,config_class=__lowerCamelCase,hidden_size=37					)



 def 							UpperCamelCase     (				self					):
        self.config_tester.run_common_tests()



 def 							UpperCamelCase     (				self					):
        A__		   =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_model(*__lowerCamelCase					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_sequence_classification(*__lowerCamelCase					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_multiple_choice(*__lowerCamelCase					)



 def 							UpperCamelCase     (				self					):
        A__		   =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_token_classification(*__lowerCamelCase					)




 def 							UpperCamelCase     (				self					):
        A__		   =		self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_question_answering(*__lowerCamelCase					)




@require_torch
class        SCREAMING_SNAKE_CASE__					(		unittest.TestCase ):



 @slow
 def 							UpperCamelCase     (				self					):
        A__		   =		MPNetModel.from_pretrained('''microsoft/mpnet-base'''					)
        A__		   =		torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]					)
        A__		   =		model(__lowerCamelCase					)[0]
        A__		   =		torch.Size((1, 11, 768)					)
        self.assertEqual(output.shape,__lowerCamelCase					)
        A__		   =		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					)					)












from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a__: int							   =						{
    'configuration_m2m_100': ['M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP', 'M2M100Config', 'M2M100OnnxConfig'],
    'tokenization_m2m_100': ['M2M100Tokenizer'],
}


try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       a__: List[Any]							   =						[
           'M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST',
           'M2M100ForConditionalGeneration',
           'M2M100Model',
           'M2M100PreTrainedModel',
       ]


if TYPE_CHECKING:
       from .configuration_mam_aaa import M2M_100_PRETRAINED_CONFIG_ARCHIVE_MAP, MaMaaaConfig, MaMaaaOnnxConfig
       from .tokenization_mam_aaa import MaMaaaTokenizer

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_mam_aaa import (
                  M2M_100_PRETRAINED_MODEL_ARCHIVE_LIST,
                  MaMaaaForConditionalGeneration,
                  MaMaaaModel,
                  MaMaaaPreTrainedModel,
              )


else:
       import sys

       a__: List[str]							   =						_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





