


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






       def __init__(   self  :    int  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    str=1_3  ,     snake_case__  :    int=7  ,     snake_case__  :    List[Any]=True  ,     snake_case__  :    Optional[int]=True  ,     snake_case__  :    Any=False  ,     snake_case__  :    Optional[int]=True  ,     snake_case__  :    Optional[Any]=9_9  ,     snake_case__  :    Dict=6_4  ,     snake_case__  :    Union[str, Any]=5  ,     snake_case__  :    Union[str, Any]=4  ,     snake_case__  :    List[Any]=6_4  ,     snake_case__  :    List[Any]="gelu"  ,     snake_case__  :    Tuple=0.1  ,     snake_case__  :    Any=0.1  ,     snake_case__  :    Dict=5_1_2  ,     snake_case__  :    Optional[Any]=1_6  ,     snake_case__  :    str=2  ,     snake_case__  :    Union[str, Any]=0.02  ,     snake_case__  :    List[Any]=3  ,     snake_case__  :    List[Any]=4  ,     snake_case__  :    Dict=None  ,     ):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size				)

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

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

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






       def  __snake_case						(   self  :    Tuple				):

              '''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  __snake_case						(   self  :    Dict  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Dict  ,     snake_case__  :    int  ,     snake_case__  :    List[Any]				):

              '''simple docstring'''






              lowercase		:str       							=				MPNetModel(config=__a				)
              model.to(__a				)
              model.eval()
              lowercase		:Optional[int]       							=				model(__a  ,     __a				)
              lowercase		:List[Any]       							=				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  :    Tuple  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    Any  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    List[Any]  ,     snake_case__  :    str				):

              '''simple docstring'''






              lowercase		:Tuple       							=				MPNetForQuestionAnswering(config=__a				)
              model.to(__a				)
              model.eval()
              lowercase		:List[str]       							=				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  :    Union[str, Any]  ,     snake_case__  :    Dict  ,     snake_case__  :    int  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    List[str]  ,     snake_case__  :    Optional[int]				):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    int  ,     snake_case__  :    Tuple  ,     snake_case__  :    Any  ,     snake_case__  :    Dict  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    str  ,     snake_case__  :    str				):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    str  ,     snake_case__  :    List[Any]  ,     snake_case__  :    Union[str, Any]  ,     snake_case__  :    str  ,     snake_case__  :    Any  ,     snake_case__  :    List[str]  ,     snake_case__  :    List[str]				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.num_labels
              lowercase		:List[Any]       							=				MPNetForTokenClassification(config=__a				)
              model.to(__a				)
              model.eval()
              lowercase		:Dict       							=				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  :    Optional[Any]				):

              '''simple docstring'''






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







@require_torch
class 					__magic_name__		(			_UpperCamelCase			,						_UpperCamelCase			,						unittest.TestCase     ):
       __A			:      Optional[Any]        =   (
           (
               MPNetForMaskedLM,
               MPNetForMultipleChoice,
               MPNetForQuestionAnswering,
               MPNetForSequenceClassification,
               MPNetForTokenClassification,
               MPNetModel,
           )
           if is_torch_available()
           else ()
       )
       __A			:      Dict        =   (
           {
               "feature-extraction": MPNetModel,
               "fill-mask": MPNetForMaskedLM,
               "question-answering": MPNetForQuestionAnswering,
               "text-classification": MPNetForSequenceClassification,
               "token-classification": MPNetForTokenClassification,
               "zero-shot": MPNetForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __A			:      int        =   False
       __A			:      Union[str, Any]        =   True






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              lowercase		:int       							=				MPNetModelTester(self				)
              lowercase		:str       							=				ConfigTester(self  ,     config_class=__a  ,     hidden_size=3_7				)






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              self.config_tester.run_common_tests()






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_model(*__a				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:int       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_sequence_classification(*__a				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_multiple_choice(*__a				)






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_token_classification(*__a				)







       def  __snake_case						(   self  :    List[str]				):

              '''simple docstring'''






              lowercase		:int       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_mpnet_for_question_answering(*__a				)







@require_torch
class 					__magic_name__		(			unittest.TestCase     ):






       @slow
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Tuple       							=				MPNetModel.from_pretrained('''microsoft/mpnet-base'''				)
              lowercase		:str       							=				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]]				)
              lowercase		:int       							=				model(__a				)[0]
              lowercase		:Tuple       							=				torch.Size((1, 1_1, 7_6_8)				)
              self.assertEqual(output.shape  ,     __a				)
              lowercase		:Optional[int]       							=				torch.tensor(
                  [[[-0.05_50, 0.19_43, -0.07_40], [-0.05_62, 0.22_11, -0.05_79], [-0.04_37, 0.33_37, -0.06_41]]]				)
              # compare the actual values for a slice.
              self.assertTrue(torch.allclose(output[:, :3, :3]  ,     __a  ,     atol=1e-4				)				)





"""simple docstring"""



from typing import TYPE_CHECKING

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


UpperCAmelCase      		=			{
    '''configuration_layoutlmv3''': [
        '''LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''LayoutLMv3Config''',
        '''LayoutLMv3OnnxConfig''',
    ],
    '''processing_layoutlmv3''': ['''LayoutLMv3Processor'''],
    '''tokenization_layoutlmv3''': ['''LayoutLMv3Tokenizer'''],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''LayoutLMv3TokenizerFast''']

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''LayoutLMv3ForQuestionAnswering''',
     '''LayoutLMv3ForSequenceClassification''',
     '''LayoutLMv3ForTokenClassification''',
     '''LayoutLMv3Model''',
     '''LayoutLMv3PreTrainedModel''',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			[
     '''TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST''',
     '''TFLayoutLMv3ForQuestionAnswering''',
     '''TFLayoutLMv3ForSequenceClassification''',
     '''TFLayoutLMv3ForTokenClassification''',
     '''TFLayoutLMv3Model''',
     '''TFLayoutLMv3PreTrainedModel''',
 ]

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase      		=			['''LayoutLMv3FeatureExtractor''']
 UpperCAmelCase      		=			['''LayoutLMv3ImageProcessor''']


if TYPE_CHECKING:
 from .configuration_layoutlmva import (
     LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
     LayoutLMvaConfig,
     LayoutLMvaOnnxConfig,
 )
 from .processing_layoutlmva import LayoutLMvaProcessor
 from .tokenization_layoutlmva import LayoutLMvaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_layoutlmva import (
      LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      LayoutLMvaForQuestionAnswering,
      LayoutLMvaForSequenceClassification,
      LayoutLMvaForTokenClassification,
      LayoutLMvaModel,
      LayoutLMvaPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_layoutlmva import (
      TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFLayoutLMvaForQuestionAnswering,
      TFLayoutLMvaForSequenceClassification,
      TFLayoutLMvaForTokenClassification,
      TFLayoutLMvaModel,
      TFLayoutLMvaPreTrainedModel,
  )

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
  from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
 import sys

 UpperCAmelCase      		=			_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

