




'''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   :  Tuple						,						lowercase__   :  Union[str, Any]						,						lowercase__   :  Any=13						,						lowercase__   :  int=7						,						lowercase__   :  Optional[int]=True						,						lowercase__   :  List[str]=True						,						lowercase__   :  Union[str, Any]=False						,						lowercase__   :  Optional[int]=True						,						lowercase__   :  List[str]=99						,						lowercase__   :  Optional[int]=64						,						lowercase__   :  int=5						,						lowercase__   :  Optional[int]=4						,						lowercase__   :  str=64						,						lowercase__   :  Dict="gelu"						,						lowercase__   :  int=0.1						,						lowercase__   :  List[str]=0.1						,						lowercase__   :  List[str]=512						,						lowercase__   :  Union[str, Any]=16						,						lowercase__   :  Optional[Any]=2						,						lowercase__   :  str=0.02						,						lowercase__   :  List[Any]=3						,						lowercase__   :  Optional[Any]=4						,						lowercase__   :  Optional[int]=None						,						):


           '''simple docstring'''

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






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

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






      def 							lowercase_						( self   :  Optional[int]       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  ids_tensor([self.batch_size, self.seq_length]						,						self.vocab_size       )

           a_     :							List[str]     						=  None
           if self.use_input_mask:
                a_     :							Tuple     						=  random_attention_mask([self.batch_size, self.seq_length]       )

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

           a_     :							Dict     						=  self.get_config()
           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






      def 							lowercase_						( self   :  Dict       ):


           '''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 							lowercase_						( self   :  Any						,						lowercase__   :  Optional[int]						,						lowercase__   :  Any						,						lowercase__   :  List[str]						,						lowercase__   :  str						,						lowercase__   :  Optional[int]						,						lowercase__   :  Any       ):


           '''simple docstring'''

           a_     :							List[str]     						=  MPNetModel(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							List[Any]     						=  model(lowercase__						,						lowercase__       )
           a_     :							Union[str, Any]     						=  model(lowercase__       )
           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 							lowercase_						( self   :  Dict						,						lowercase__   :  Dict						,						lowercase__   :  List[str]						,						lowercase__   :  List[Any]						,						lowercase__   :  Tuple						,						lowercase__   :  List[Any]						,						lowercase__   :  List[Any]       ):


           '''simple docstring'''

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


           '''simple docstring'''

           a_     :							List[Any]     						=  self.num_labels
           a_     :							Dict     						=  MPNetForSequenceClassification(lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							Tuple     						=  model(lowercase__						,						attention_mask=lowercase__						,						labels=lowercase__       )
           self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.num_labels)       )






      def 							lowercase_						( self   :  Union[str, Any]						,						lowercase__   :  List[Any]						,						lowercase__   :  Dict						,						lowercase__   :  int						,						lowercase__   :  Optional[int]						,						lowercase__   :  Optional[Any]						,						lowercase__   :  Any       ):


           '''simple docstring'''

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






      def 							lowercase_						( self   :  Tuple						,						lowercase__   :  Union[str, Any]						,						lowercase__   :  str						,						lowercase__   :  str						,						lowercase__   :  Any						,						lowercase__   :  List[Any]						,						lowercase__   :  Dict       ):


           '''simple docstring'''

           a_     :							Optional[int]     						=  self.num_labels
           a_     :							Dict     						=  MPNetForTokenClassification(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           a_     :							Optional[Any]     						=  model(lowercase__						,						attention_mask=lowercase__						,						labels=lowercase__       )
           self.parent.assertEqual(result.logits.shape						,						(self.batch_size, self.seq_length, self.num_labels)       )






      def 							lowercase_						( self   :  List[Any]       ):


           '''simple docstring'''

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







@require_torch
class      SCREAMING_SNAKE_CASE    (						snake_case_							,      snake_case_							,      unittest.TestCase				):
      __magic_name__  :       Tuple   							=					(
          (
              MPNetForMaskedLM,
              MPNetForMultipleChoice,
              MPNetForQuestionAnswering,
              MPNetForSequenceClassification,
              MPNetForTokenClassification,
              MPNetModel,
          )
          if is_torch_available()
          else ()
      )
      __magic_name__  :       Tuple   							=					(
          {
              '''feature-extraction''': MPNetModel,
              '''fill-mask''': MPNetForMaskedLM,
              '''question-answering''': MPNetForQuestionAnswering,
              '''text-classification''': MPNetForSequenceClassification,
              '''token-classification''': MPNetForTokenClassification,
              '''zero-shot''': MPNetForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __magic_name__  :       Optional[int]   							=					False
      __magic_name__  :       List[Any]   							=					True






      def 							lowercase_						( self   :  List[str]       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  MPNetModelTester(self       )
           a_     :							Optional[Any]     						=  ConfigTester(self						,						config_class=lowercase__						,						hidden_size=37       )






      def 							lowercase_						( self   :  List[str]       ):


           '''simple docstring'''

           self.config_tester.run_common_tests()






      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           a_     :							Optional[int]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_model(*lowercase__       )






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


           '''simple docstring'''

           a_     :							Optional[int]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowercase__       )






      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

           a_     :							Optional[Any]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowercase__       )






      def 							lowercase_						( self   :  Optional[Any]       ):


           '''simple docstring'''

           a_     :							Union[str, Any]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_token_classification(*lowercase__       )






      def 							lowercase_						( self   :  Any       ):


           '''simple docstring'''

           a_     :							Union[str, Any]     						=  self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_mpnet_for_question_answering(*lowercase__       )







@require_torch
class      SCREAMING_SNAKE_CASE    (						unittest.TestCase				):






      @slow
      def 							lowercase_						( self   :  Tuple       ):


           '''simple docstring'''

           a_     :							Tuple     						=  MPNetModel.from_pretrained("""microsoft/mpnet-base"""       )
           a_     :							Optional[int]     						=  torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
           a_     :							Optional[Any]     						=  model(lowercase__       )[0]
           a_     :							List[str]     						=  torch.Size((1, 11, 768)       )
           self.assertEqual(output.shape						,						lowercase__       )
           a_     :							str     						=  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]						,						lowercase__						,						atol=1e-4       )       )






'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


lowerCAmelCase_      :  Optional[Any]				     =  {
    'configuration_mobilevit': ['MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MobileViTConfig', 'MobileViTOnnxConfig'],
}

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  int				     =  ['MobileViTFeatureExtractor']
 lowerCAmelCase_      :  int				     =  ['MobileViTImageProcessor']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  str				     =  [
     'MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'MobileViTForImageClassification',
     'MobileViTForSemanticSegmentation',
     'MobileViTModel',
     'MobileViTPreTrainedModel',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCAmelCase_      :  Optional[Any]				     =  [
     'TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFMobileViTForImageClassification',
     'TFMobileViTForSemanticSegmentation',
     'TFMobileViTModel',
     'TFMobileViTPreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_mobilevit import MOBILEVIT_PRETRAINED_CONFIG_ARCHIVE_MAP, MobileViTConfig, MobileViTOnnxConfig

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_mobilevit import MobileViTFeatureExtractor
  from .image_processing_mobilevit import MobileViTImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mobilevit import (
      MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      MobileViTForImageClassification,
      MobileViTForSemanticSegmentation,
      MobileViTModel,
      MobileViTPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_mobilevit import (
      TF_MOBILEVIT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFMobileViTForImageClassification,
      TFMobileViTForSemanticSegmentation,
      TFMobileViTModel,
      TFMobileViTPreTrainedModel,
  )


else:
 import sys

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