


'''simple docstring'''

import unittest

from transformers import MarkupLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     MarkupLMForQuestionAnswering,
     MarkupLMForSequenceClassification,
     MarkupLMForTokenClassification,
     MarkupLMModel,
 )

# TODO check dependencies
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer
class 						__lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self					:					Optional[int]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					List[str]=13     ,		UpperCamelCase					:					Dict=7     ,		UpperCamelCase					:					Any=True     ,		UpperCamelCase					:					int=True     ,		UpperCamelCase					:					Tuple=True     ,		UpperCamelCase					:					str=True     ,		UpperCamelCase					:					Optional[int]=99     ,		UpperCamelCase					:					Any=32     ,		UpperCamelCase					:					Any=5     ,		UpperCamelCase					:					int=4     ,		UpperCamelCase					:					Tuple=37     ,		UpperCamelCase					:					Dict="gelu"     ,		UpperCamelCase					:					str=0.1     ,		UpperCamelCase					:					Any=0.1     ,		UpperCamelCase					:					int=512     ,		UpperCamelCase					:					int=16     ,		UpperCamelCase					:					Dict=2     ,		UpperCamelCase					:					List[Any]=0.02     ,		UpperCamelCase					:					List[Any]=3     ,		UpperCamelCase					:					int=None     ,		UpperCamelCase					:					Union[str, Any]=20     ,		UpperCamelCase					:					str=30     ,		UpperCamelCase					:					Optional[int]=2     ,		UpperCamelCase					:					Union[str, Any]=2     ,		UpperCamelCase					:					Any=10     ,		):

             '''simple docstring'''







             lowercase__       =				parent
             lowercase__       =				batch_size
             lowercase__       =				seq_length
             lowercase__       =				is_training
             lowercase__       =				use_input_mask
             lowercase__       =				use_token_type_ids
             lowercase__       =				use_labels
             lowercase__       =				vocab_size
             lowercase__       =				hidden_size
             lowercase__       =				num_hidden_layers
             lowercase__       =				num_attention_heads
             lowercase__       =				intermediate_size
             lowercase__       =				hidden_act
             lowercase__       =				hidden_dropout_prob
             lowercase__       =				attention_probs_dropout_prob
             lowercase__       =				max_position_embeddings
             lowercase__       =				type_vocab_size
             lowercase__       =				type_sequence_label_size
             lowercase__       =				initializer_range
             lowercase__       =				num_labels
             lowercase__       =				scope
             lowercase__       =				max_xpath_tag_unit_embeddings
             lowercase__       =				max_xpath_subs_unit_embeddings
             lowercase__       =				tag_pad_id
             lowercase__       =				subs_pad_id
             lowercase__       =				max_depth



      def    UpperCamelCase__	(self					:					List[str]				):

             '''simple docstring'''







             lowercase__       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size				)

             lowercase__       =				ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]     ,		self.max_xpath_tag_unit_embeddings				)

             lowercase__       =				ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]     ,		self.max_xpath_subs_unit_embeddings				)

             lowercase__       =				None
             if self.use_input_mask:
                    lowercase__       =				ids_tensor([self.batch_size, self.seq_length]     ,		vocab_size=2				)

             lowercase__       =				None
             if self.use_token_type_ids:
                    lowercase__       =				ids_tensor([self.batch_size, self.seq_length]     ,		self.type_vocab_size				)

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

             lowercase__       =				self.get_config()

             return (
                 config,
                 input_ids,
                 xpath_tags_seq,
                 xpath_subs_seq,
                 token_type_ids,
                 input_mask,
                 sequence_labels,
                 token_labels,
             )



      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             return MarkupLMConfig(
                 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     ,		type_vocab_size=self.type_vocab_size     ,		initializer_range=self.initializer_range     ,		max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings     ,		max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings     ,		tag_pad_id=self.tag_pad_id     ,		subs_pad_id=self.subs_pad_id     ,		max_depth=self.max_depth     ,		)



      def    UpperCamelCase__	(self					:					Union[str, Any]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Tuple     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					Tuple     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					List[str]     ,		):

             '''simple docstring'''







             lowercase__       =				MarkupLMModel(config=UpperCamelCase				)
             model.to(UpperCamelCase				)
             model.eval()
             print('''Configs:'''     ,		model.config.tag_pad_id     ,		model.config.subs_pad_id				)
             lowercase__       =				model(UpperCamelCase     ,		attention_mask=UpperCamelCase     ,		token_type_ids=UpperCamelCase				)
             lowercase__       =				model(UpperCamelCase     ,		token_type_ids=UpperCamelCase				)
             lowercase__       =				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    UpperCamelCase__	(self					:					Dict     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Tuple     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					Union[str, Any]     ,		):

             '''simple docstring'''







             lowercase__       =				self.num_labels
             lowercase__       =				MarkupLMForSequenceClassification(UpperCamelCase				)
             model.to(UpperCamelCase				)
             model.eval()
             lowercase__       =				model(
                 UpperCamelCase     ,		xpath_tags_seq=UpperCamelCase     ,		xpath_subs_seq=UpperCamelCase     ,		attention_mask=UpperCamelCase     ,		token_type_ids=UpperCamelCase     ,		labels=UpperCamelCase     ,		)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.num_labels)				)



      def    UpperCamelCase__	(self					:					Union[str, Any]     ,		UpperCamelCase					:					Optional[int]     ,		UpperCamelCase					:					Optional[Any]     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					List[str]     ,		UpperCamelCase					:					Optional[Any]     ,		):

             '''simple docstring'''







             lowercase__       =				self.num_labels
             lowercase__       =				MarkupLMForTokenClassification(config=UpperCamelCase				)
             model.to(UpperCamelCase				)
             model.eval()
             lowercase__       =				model(
                 UpperCamelCase     ,		xpath_tags_seq=UpperCamelCase     ,		xpath_subs_seq=UpperCamelCase     ,		attention_mask=UpperCamelCase     ,		token_type_ids=UpperCamelCase     ,		labels=UpperCamelCase     ,		)
             self.parent.assertEqual(result.logits.shape     ,		(self.batch_size, self.seq_length, self.num_labels)				)



      def    UpperCamelCase__	(self					:					Optional[Any]     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					Union[str, Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					str     ,		UpperCamelCase					:					Any     ,		UpperCamelCase					:					str     ,		):

             '''simple docstring'''







             lowercase__       =				MarkupLMForQuestionAnswering(config=UpperCamelCase				)
             model.to(UpperCamelCase				)
             model.eval()
             lowercase__       =				model(
                 UpperCamelCase     ,		xpath_tags_seq=UpperCamelCase     ,		xpath_subs_seq=UpperCamelCase     ,		attention_mask=UpperCamelCase     ,		token_type_ids=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    UpperCamelCase__	(self					:					Optional[int]				):

             '''simple docstring'''







             lowercase__       =				self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,(
                 lowercase__
             )    ,
             )       =				config_and_inputs
             lowercase__       =				{
                 '''input_ids''': input_ids,
                 '''xpath_tags_seq''': xpath_tags_seq,
                 '''xpath_subs_seq''': xpath_subs_seq,
                 '''token_type_ids''': token_type_ids,
                 '''attention_mask''': input_mask,
             }
             return config, inputs_dict






@require_torch
class 						__lowerCAmelCase		(lowercase_						,  lowercase_						,  unittest.TestCase  ):





      '''simple docstring'''






      lowerCAmelCase__      :  Optional[int]						      =							(
          (
              MarkupLMModel,
              MarkupLMForSequenceClassification,
              MarkupLMForTokenClassification,
              MarkupLMForQuestionAnswering,
          )
          if is_torch_available()
          else None
      )
      lowerCAmelCase__      :  Optional[Any]						      =							(
          {
              """feature-extraction""": MarkupLMModel,
              """question-answering""": MarkupLMForQuestionAnswering,
              """text-classification""": MarkupLMForSequenceClassification,
              """token-classification""": MarkupLMForTokenClassification,
              """zero-shot""": MarkupLMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )



      def    UpperCamelCase__	(self					:					Optional[int]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					List[Any]     ,		UpperCamelCase					:					int     ,		UpperCamelCase					:					Dict     ,		UpperCamelCase					:					List[Any]				):

             '''simple docstring'''







             return True



      def    UpperCamelCase__	(self					:					str				):

             '''simple docstring'''







             lowercase__       =				MarkupLMModelTester(self				)
             lowercase__       =				ConfigTester(self     ,		config_class=UpperCamelCase     ,		hidden_size=37				)



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

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					List[Any]				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase				)



      def    UpperCamelCase__	(self					:					Tuple				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*UpperCamelCase				)






      def    UpperCamelCase__	(self					:					Dict				):

             '''simple docstring'''







             lowercase__       =				self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*UpperCamelCase				)






def _SCREAMING_SNAKE_CASE   () -> Any:






       """simple docstring"""







       lowercase__       =				'''
    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>

    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>

    </body>
    </html>
    '''

       return html_string

@require_torch
class 						__lowerCAmelCase		(unittest.TestCase  ):





      '''simple docstring'''






      @cached_property
      def    UpperCamelCase__	(self					:					Tuple				):

             '''simple docstring'''







             lowercase__       =				MarkupLMFeatureExtractor()
             lowercase__       =				MarkupLMTokenizer.from_pretrained('''microsoft/markuplm-base'''				)

             return MarkupLMProcessor(UpperCamelCase     ,		UpperCamelCase				)






      @slow
      def    UpperCamelCase__	(self					:					Any				):

             '''simple docstring'''







             lowercase__       =				MarkupLMModel.from_pretrained('''microsoft/markuplm-base'''				).to(UpperCamelCase				)

             lowercase__       =				self.default_processor

             lowercase__       =				processor(prepare_html_string()     ,		return_tensors='''pt'''				)
             lowercase__       =				inputs.to(UpperCamelCase				)

             # forward pass
             with torch.no_grad():
                    lowercase__       =				model(**UpperCamelCase				)

             # verify the last hidden states
             lowercase__       =				torch.Size([1, 14, 768]				)
             self.assertEqual(outputs.last_hidden_state.shape     ,		UpperCamelCase				)

             lowercase__       =				torch.tensor(
                 [[0.06_75, -0.00_52, 0.50_01], [-0.22_81, 0.08_02, 0.21_92], [-0.05_83, -0.33_11, 0.11_85]]				).to(UpperCamelCase				)

             self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]     ,		UpperCamelCase     ,		atol=1E-4				)				)








'''simple docstring'''

from typing import TYPE_CHECKING

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


lowerCamelCase							:     Optional[Any]								=			{
    'configuration_mobilevitv2': [
        'MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileViTV2Config',
        'MobileViTV2OnnxConfig',
    ],
}


try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 lowerCamelCase							:     Optional[int]								=			[
     'MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST',
     'MobileViTV2ForImageClassification',
     'MobileViTV2ForSemanticSegmentation',
     'MobileViTV2Model',
     'MobileViTV2PreTrainedModel',
 ]

if TYPE_CHECKING:
 from .configuration_mobilevitva import (
     MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
     MobileViTVaConfig,
     MobileViTVaOnnxConfig,
 )

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_mobilevitva import (
      MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
      MobileViTVaForImageClassification,
      MobileViTVaForSemanticSegmentation,
      MobileViTVaModel,
      MobileViTVaPreTrainedModel,
  )

else:
 import sys

 lowerCamelCase							:     Dict								=			_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




