


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 A__     :



  """simple docstring"""



  def __init__( self			:  str				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  int=13				,				lowerCamelCase__			:  str=7				,				lowerCamelCase__			:  Optional[int]=True				,				lowerCamelCase__			:  Optional[int]=True				,				lowerCamelCase__			:  Optional[int]=False				,				lowerCamelCase__			:  Any=True				,				lowerCamelCase__			:  Optional[int]=99				,				lowerCamelCase__			:  Optional[Any]=64				,				lowerCamelCase__			:  Any=5				,				lowerCamelCase__			:  Tuple=4				,				lowerCamelCase__			:  Dict=64				,				lowerCamelCase__			:  int="gelu"				,				lowerCamelCase__			:  int=0.1				,				lowerCamelCase__			:  List[Any]=0.1				,				lowerCamelCase__			:  Dict=512				,				lowerCamelCase__			:  int=16				,				lowerCamelCase__			:  Tuple=2				,				lowerCamelCase__			:  Union[str, Any]=0.02				,				lowerCamelCase__			:  List[Any]=3				,				lowerCamelCase__			:  List[str]=4				,				lowerCamelCase__			:  int=None				,				):
        a__	:  Optional[Any]     =   parent
        a__	:  List[str]     =   batch_size
        a__	:  Tuple     =   seq_length
        a__	:  Dict     =   is_training
        a__	:  str     =   use_input_mask
        a__	:  int     =   use_token_type_ids
        a__	:  Any     =   use_labels
        a__	:  List[Any]     =   vocab_size
        a__	:  Optional[Any]     =   hidden_size
        a__	:  int     =   num_hidden_layers
        a__	:  Optional[Any]     =   num_attention_heads
        a__	:  Union[str, Any]     =   intermediate_size
        a__	:  Tuple     =   hidden_act
        a__	:  Dict     =   hidden_dropout_prob
        a__	:  List[str]     =   attention_probs_dropout_prob
        a__	:  Any     =   max_position_embeddings
        a__	:  int     =   type_vocab_size
        a__	:  List[Any]     =   type_sequence_label_size
        a__	:  Tuple     =   initializer_range
        a__	:  Any     =   num_labels
        a__	:  Optional[int]     =   num_choices
        a__	:  Union[str, Any]     =   scope

  def    _UpperCamelCase( self			:  Tuple       ):
        return MPNetConfig.from_pretrained("microsoft/mpnet-base"       )

  def    _UpperCamelCase( self			:  List[Any]       ):
        a__	:  Any     =   ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size       )

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

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

        a__	:  Optional[int]     =   self.get_config()
        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

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

  def    _UpperCamelCase( self			:  str				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Optional[int]				,				lowerCamelCase__			:  List[str]				,				lowerCamelCase__			:  Union[str, Any]				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Tuple       ):
        a__	:  str     =   self.num_choices
        a__	:  List[Any]     =   MPNetForMultipleChoice(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  Optional[Any]     =   input_ids.unsqueeze(1       ).expand(-1				,				self.num_choices				,				-1       ).contiguous()
        a__	:  str     =   input_mask.unsqueeze(1       ).expand(-1				,				self.num_choices				,				-1       ).contiguous()
        a__	:  List[Any]     =   model(
            lowerCamelCase__				,				attention_mask=lowerCamelCase__				,				labels=lowerCamelCase__				,				)
        self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_choices)       )

  def    _UpperCamelCase( self			:  Tuple				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Any				,				lowerCamelCase__			:  Optional[Any]				,				lowerCamelCase__			:  str				,				lowerCamelCase__			:  List[str]				,				lowerCamelCase__			:  List[str]       ):
        a__	:  Any     =   self.num_labels
        a__	:  Any     =   MPNetForTokenClassification(config=lowerCamelCase__       )
        model.to(lowerCamelCase__       )
        model.eval()
        a__	:  int     =   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			:  Optional[int]       ):
        a__	:  Any     =   self.prepare_config_and_inputs()
        ((a__),	(a__),	(a__),	(a__),	(a__),	(a__))	:  Optional[Any]     =   config_and_inputs
        a__	:  Optional[int]     =   {"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict




@require_torch
class A__     ( A__				,		A__				,		unittest.TestCase  ):



  """simple docstring"""



  _lowercase									=		(
      (
          MPNetForMaskedLM,
          MPNetForMultipleChoice,
          MPNetForQuestionAnswering,
          MPNetForSequenceClassification,
          MPNetForTokenClassification,
          MPNetModel,
      )
      if is_torch_available()
      else ()
  )
  _lowercase									=		(
      {
          'feature-extraction': MPNetModel,
          'fill-mask': MPNetForMaskedLM,
          'question-answering': MPNetForQuestionAnswering,
          'text-classification': MPNetForSequenceClassification,
          'token-classification': MPNetForTokenClassification,
          'zero-shot': MPNetForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  _lowercase									=		False
  _lowercase									=		True

  def    _UpperCamelCase( self			:  Dict       ):
        a__	:  str     =   MPNetModelTester(self       )
        a__	:  List[Any]     =   ConfigTester(self				,				config_class=lowerCamelCase__				,				hidden_size=37       )

  def    _UpperCamelCase( self			:  Tuple       ):
        self.config_tester.run_common_tests()

  def    _UpperCamelCase( self			:  List[str]       ):
        a__	:  List[str]     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_model(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Union[str, Any]       ):
        a__	:  Union[str, Any]     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_sequence_classification(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  str       ):
        a__	:  Any     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_multiple_choice(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Optional[int]       ):
        a__	:  Dict     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_token_classification(*lowerCamelCase__       )

  def    _UpperCamelCase( self			:  Optional[int]       ):
        a__	:  Any     =   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_mpnet_for_question_answering(*lowerCamelCase__       )




@require_torch
class A__     ( unittest.TestCase  ):



  """simple docstring"""



  @slow
  def    _UpperCamelCase( self			:  int       ):
        a__	:  Dict     =   MPNetModel.from_pretrained("microsoft/mpnet-base"       )
        a__	:  List[Any]     =   torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]       )
        a__	:  List[str]     =   model(lowerCamelCase__       )[0]
        a__	:  Any     =   torch.Size((1, 11, 768)       )
        self.assertEqual(output.shape				,				lowerCamelCase__       )
        a__	:  Tuple     =   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       )       )








"""simple docstring"""


from typing import TYPE_CHECKING

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


__magic_name__       :					Dict						=			{
    """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:
		__magic_name__       :					Optional[Any]						=			["""LayoutLMv3TokenizerFast"""]

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

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

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__magic_name__       :					Union[str, Any]						=			["""LayoutLMv3FeatureExtractor"""]
		__magic_name__       :					Any						=			["""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

		__magic_name__       :					Union[str, Any]						=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



