





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

			"""simple docstring"""







			def __init__(			self	,      _A	,      _A=1_3	,      _A=7	,      _A=True	,      _A=True	,      _A=True	,      _A=True	,      _A=9_9	,      _A=3_2	,      _A=5	,      _A=4	,      _A=3_7	,      _A="gelu"	,      _A=0.1	,      _A=0.1	,      _A=5_1_2	,      _A=1_6	,      _A=2	,      _A=0.02	,      _A=3	,      _A=None	,      _A=2_0	,      _A=3_0	,      _A=2	,      _A=2	,      _A=1_0	,      ):






									'''simple docstring'''







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







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		Optional[int]							      = ids_tensor([self.batch_size, self.seq_length]	,      self.vocab_size						)

									UpperCamelCase							:		Union[str, Any]							      = ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]	,      self.max_xpath_tag_unit_embeddings						)

									UpperCamelCase							:		int							      = ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]	,      self.max_xpath_subs_unit_embeddings						)

									UpperCamelCase							:		Any							      = None
									if self.use_input_mask:
															UpperCamelCase							:		List[str]							      = ids_tensor([self.batch_size, self.seq_length]	,      vocab_size=2						)

									UpperCamelCase							:		int							      = None
									if self.use_token_type_ids:
															UpperCamelCase							:		Union[str, Any]							      = ids_tensor([self.batch_size, self.seq_length]	,      self.type_vocab_size						)

									UpperCamelCase							:		str							      = None
									UpperCamelCase							:		Union[str, Any]							      = None
									if self.use_labels:
															UpperCamelCase							:		List[str]							      = ids_tensor([self.batch_size]	,      self.type_sequence_label_size						)
															UpperCamelCase							:		Union[str, Any]							      = ids_tensor([self.batch_size, self.seq_length]	,      self.num_labels						)

									UpperCamelCase							:		int							      = self.get_config()

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







			def      _a			(			self						):






									'''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      _a			(			self	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      ):






									'''simple docstring'''







									UpperCamelCase							:		List[str]							      = MarkupLMModel(config=_A						)
									model.to(_A						)
									model.eval()
									print("""Configs:"""	,      model.config.tag_pad_id	,      model.config.subs_pad_id						)
									UpperCamelCase							:		Optional[int]							      = model(_A	,      attention_mask=_A	,      token_type_ids=_A						)
									UpperCamelCase							:		Any							      = model(_A	,      token_type_ids=_A						)
									UpperCamelCase							:		Optional[int]							      = 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      _a			(			self	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      ):






									'''simple docstring'''







									UpperCamelCase							:		Optional[Any]							      = self.num_labels
									UpperCamelCase							:		str							      = MarkupLMForSequenceClassification(_A						)
									model.to(_A						)
									model.eval()
									UpperCamelCase							:		Union[str, Any]							      = model(
									    _A	,      xpath_tags_seq=_A	,      xpath_subs_seq=_A	,      attention_mask=_A	,      token_type_ids=_A	,      labels=_A	,      )
									self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.num_labels)						)







			def      _a			(			self	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      ):






									'''simple docstring'''







									UpperCamelCase							:		Optional[Any]							      = self.num_labels
									UpperCamelCase							:		Any							      = MarkupLMForTokenClassification(config=_A						)
									model.to(_A						)
									model.eval()
									UpperCamelCase							:		List[str]							      = model(
									    _A	,      xpath_tags_seq=_A	,      xpath_subs_seq=_A	,      attention_mask=_A	,      token_type_ids=_A	,      labels=_A	,      )
									self.parent.assertEqual(result.logits.shape	,      (self.batch_size, self.seq_length, self.num_labels)						)







			def      _a			(			self	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      _A	,      ):






									'''simple docstring'''







									UpperCamelCase							:		Optional[Any]							      = MarkupLMForQuestionAnswering(config=_A						)
									model.to(_A						)
									model.eval()
									UpperCamelCase							:		Tuple							      = model(
									    _A	,      xpath_tags_seq=_A	,      xpath_subs_seq=_A	,      attention_mask=_A	,      token_type_ids=_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      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		int							      = self.prepare_config_and_inputs()
									(
									    (
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	(
									    UpperCamelCase       
									)      ,	       
									)							:		Optional[Any]							      = config_and_inputs
									UpperCamelCase							:		Dict							      = {
									    """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        lowercase__ (      _UpperCAmelCase				,    _UpperCAmelCase				,    unittest.TestCase  ):

			"""simple docstring"""
			__lowerCAmelCase    :							Tuple								=     (
			    (
			        MarkupLMModel,
			        MarkupLMForSequenceClassification,
			        MarkupLMForTokenClassification,
			        MarkupLMForQuestionAnswering,
			    )
			    if is_torch_available()
			    else None
			)
			__lowerCAmelCase    :							Any								=     (
			    {
			        """feature-extraction""": MarkupLMModel,
			        """question-answering""": MarkupLMForQuestionAnswering,
			        """text-classification""": MarkupLMForSequenceClassification,
			        """token-classification""": MarkupLMForTokenClassification,
			        """zero-shot""": MarkupLMForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)







			def      _a			(			self	,      _A	,      _A	,      _A	,      _A	,      _A						):






									'''simple docstring'''







									return True







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		Any							      = MarkupLMModelTester(self						)
									UpperCamelCase							:		Dict							      = ConfigTester(self	,      config_class=_A	,      hidden_size=3_7						)







			def      _a			(			self						):






									'''simple docstring'''







									self.config_tester.run_common_tests()







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		int							      = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_A						)







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		str							      = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_A						)







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		Optional[int]							      = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_A						)







			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		Dict							      = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_A						)


def     UpperCamelCase       ():
						UpperCamelCase							:		Optional[Any]							      = """
    <!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        lowercase__ (      unittest.TestCase  ):

			"""simple docstring"""







			@cached_property
			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		int							      = MarkupLMFeatureExtractor()
									UpperCamelCase							:		Dict							      = MarkupLMTokenizer.from_pretrained("""microsoft/markuplm-base"""						)

									return MarkupLMProcessor(_A	,      _A						)







			@slow
			def      _a			(			self						):






									'''simple docstring'''







									UpperCamelCase							:		int							      = MarkupLMModel.from_pretrained("""microsoft/markuplm-base"""						).to(_A						)

									UpperCamelCase							:		List[str]							      = self.default_processor

									UpperCamelCase							:		str							      = processor(prepare_html_string()	,      return_tensors="""pt"""						)
									UpperCamelCase							:		Optional[Any]							      = inputs.to(_A						)

									# forward pass
									with torch.no_grad():
															UpperCamelCase							:		Union[str, Any]							      = model(**_A						)

									# verify the last hidden states
									UpperCamelCase							:		Tuple							      = torch.Size([1, 1_4, 7_6_8]						)
									self.assertEqual(outputs.last_hidden_state.shape	,      _A						)

									UpperCamelCase							:		Optional[int]							      = 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(_A						)

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










"""simple docstring"""


def     UpperCamelCase       (SCREAMING_SNAKE_CASE    ):
						UpperCamelCase							:		Tuple							      = 0
						for plain_chr in plain_text:
												UpperCamelCase							:		List[str]							      = (
												    ord(SCREAMING_SNAKE_CASE    ) + (hash_value << 6) + (hash_value << 16) - hash_value
												)
						return hash_value



