



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







					def __init__(							self   :  Any      ,		_A   :  Union[str, Any]      ,		_A   :  List[str]=13      ,		_A   :  int=7      ,		_A   :  str=True      ,		_A   :  Dict=True      ,		_A   :  int=True      ,		_A   :  Union[str, Any]=True      ,		_A   :  Union[str, Any]=99      ,		_A   :  Tuple=32      ,		_A   :  Any=5      ,		_A   :  Dict=4      ,		_A   :  List[Any]=37      ,		_A   :  List[Any]="gelu"      ,		_A   :  List[Any]=0.1      ,		_A   :  Union[str, Any]=0.1      ,		_A   :  Optional[Any]=512      ,		_A   :  int=16      ,		_A   :  Optional[int]=2      ,		_A   :  Dict=0.02      ,		_A   :  List[str]=3      ,		_A   :  Any=None      ,		_A   :  List[Any]=20      ,		_A   :  Tuple=30      ,		_A   :  Tuple=2      ,		_A   :  Optional[int]=2      ,		_A   :  Any=10      ,		):
											A__						:				Dict         =			parent
											A__						:				Optional[int]         =			batch_size
											A__						:				str         =			seq_length
											A__						:				str         =			is_training
											A__						:				Dict         =			use_input_mask
											A__						:				str         =			use_token_type_ids
											A__						:				Optional[Any]         =			use_labels
											A__						:				Union[str, Any]         =			vocab_size
											A__						:				Optional[int]         =			hidden_size
											A__						:				Dict         =			num_hidden_layers
											A__						:				Union[str, Any]         =			num_attention_heads
											A__						:				int         =			intermediate_size
											A__						:				int         =			hidden_act
											A__						:				List[Any]         =			hidden_dropout_prob
											A__						:				Dict         =			attention_probs_dropout_prob
											A__						:				Any         =			max_position_embeddings
											A__						:				Tuple         =			type_vocab_size
											A__						:				Dict         =			type_sequence_label_size
											A__						:				Dict         =			initializer_range
											A__						:				Dict         =			num_labels
											A__						:				List[str]         =			scope
											A__						:				Union[str, Any]         =			max_xpath_tag_unit_embeddings
											A__						:				Any         =			max_xpath_subs_unit_embeddings
											A__						:				Any         =			tag_pad_id
											A__						:				Dict         =			subs_pad_id
											A__						:				Optional[int]         =			max_depth







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

											A__						:				Dict         =			ids_tensor(
											    [self.batch_size, self.seq_length, self.max_depth]      ,		self.max_xpath_tag_unit_embeddings)

											A__						:				List[Any]         =			ids_tensor(
											    [self.batch_size, self.seq_length, self.max_depth]      ,		self.max_xpath_subs_unit_embeddings)

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

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

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

											A__						:				Tuple         =			self.get_config()

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







					def 			_lowercase      (							self   :  List[str]):
											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 			_lowercase      (							self   :  Tuple      ,		_A   :  List[Any]      ,		_A   :  Union[str, Any]      ,		_A   :  List[Any]      ,		_A   :  List[str]      ,		_A   :  Any      ,		_A   :  Tuple      ,		_A   :  Optional[int]      ,		_A   :  int      ,		):
											A__						:				List[Any]         =			MarkupLMModel(config=_A)
											model.to(_A)
											model.eval()
											print("Configs:"      ,		model.config.tag_pad_id      ,		model.config.subs_pad_id)
											A__						:				Union[str, Any]         =			model(_A      ,		attention_mask=_A      ,		token_type_ids=_A)
											A__						:				Optional[Any]         =			model(_A      ,		token_type_ids=_A)
											A__						:				Dict         =			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 			_lowercase      (							self   :  Dict      ,		_A   :  Dict      ,		_A   :  Any      ,		_A   :  List[str]      ,		_A   :  Optional[int]      ,		_A   :  int      ,		_A   :  Optional[Any]      ,		_A   :  Tuple      ,		_A   :  List[str]      ,		):
											A__						:				Tuple         =			self.num_labels
											A__						:				Tuple         =			MarkupLMForSequenceClassification(_A)
											model.to(_A)
											model.eval()
											A__						:				Dict         =			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 			_lowercase      (							self   :  Any      ,		_A   :  Any      ,		_A   :  int      ,		_A   :  List[Any]      ,		_A   :  Optional[int]      ,		_A   :  Dict      ,		_A   :  Dict      ,		_A   :  Union[str, Any]      ,		_A   :  Any      ,		):
											A__						:				Union[str, Any]         =			self.num_labels
											A__						:				Optional[int]         =			MarkupLMForTokenClassification(config=_A)
											model.to(_A)
											model.eval()
											A__						:				Optional[int]         =			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 			_lowercase      (							self   :  Optional[int]      ,		_A   :  Dict      ,		_A   :  Optional[int]      ,		_A   :  Tuple      ,		_A   :  Optional[Any]      ,		_A   :  Any      ,		_A   :  Any      ,		_A   :  Tuple      ,		_A   :  List[str]      ,		):
											A__						:				str         =			MarkupLMForQuestionAnswering(config=_A)
											model.to(_A)
											model.eval()
											A__						:				List[Any]         =			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 			_lowercase      (							self   :  str):
											A__						:				str         =			self.prepare_config_and_inputs()
											(
											    (
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							(
											    A__
											)     ,							
											)						:				Optional[int]         =			config_and_inputs
											A__						:				List[Any]         =			{
											    "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__	(						UpperCamelCase       ,				UpperCamelCase       ,				unittest.TestCase    ):
					__A  :     int     =    (
					    (
					        MarkupLMModel,
					        MarkupLMForSequenceClassification,
					        MarkupLMForTokenClassification,
					        MarkupLMForQuestionAnswering,
					    )
					    if is_torch_available()
					    else None
					)
					__A  :     List[Any]     =    (
					    {
					        'feature-extraction': MarkupLMModel,
					        'question-answering': MarkupLMForQuestionAnswering,
					        'text-classification': MarkupLMForSequenceClassification,
					        'token-classification': MarkupLMForTokenClassification,
					        'zero-shot': MarkupLMForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)







					def 			_lowercase      (							self   :  str      ,		_A   :  Union[str, Any]      ,		_A   :  List[Any]      ,		_A   :  Optional[Any]      ,		_A   :  Union[str, Any]      ,		_A   :  Dict):
											# ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
											# (batch of pretokenized examples).
											return True







					def 			_lowercase      (							self   :  Union[str, Any]):
											A__						:				List[str]         =			MarkupLMModelTester(self)
											A__						:				Optional[int]         =			ConfigTester(self      ,		config_class=_A      ,		hidden_size=37)







					def 			_lowercase      (							self   :  Optional[int]):
											self.config_tester.run_common_tests()







					def 			_lowercase      (							self   :  Tuple):
											A__						:				Optional[Any]         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)







					def 			_lowercase      (							self   :  Optional[int]):
											A__						:				Optional[int]         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*_A)







					def 			_lowercase      (							self   :  Optional[Any]):
											A__						:				List[Any]         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*_A)







					def 			_lowercase      (							self   :  Optional[int]):
											A__						:				str         =			self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*_A)




def 						snake_case__							(       )		->		Dict:







						"""simple docstring"""

						A__						:				Dict         =			"\n    <!DOCTYPE html>\n    <html>\n    <head>\n    <title>Page Title</title>\n    </head>\n    <body>\n\n    <h1>This is a Heading</h1>\n    <p>This is a paragraph.</p>\n\n    </body>\n    </html>\n    "

						return html_string




@require_torch
class 				lowerCAmelCase__	(						unittest.TestCase    ):







					@cached_property
					def 			_lowercase      (							self   :  Optional[Any]):
											# TODO use from_pretrained here
											A__						:				Optional[Any]         =			MarkupLMFeatureExtractor()
											A__						:				Optional[Any]         =			MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base")

											return MarkupLMProcessor(_A      ,		_A)







					@slow
					def 			_lowercase      (							self   :  Optional[Any]):
											A__						:				Dict         =			MarkupLMModel.from_pretrained("microsoft/markuplm-base").to(_A)

											A__						:				Tuple         =			self.default_processor

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

											# forward pass
											with torch.no_grad():
																	A__						:				Tuple         =			model(**_A)

											# verify the last hidden states
											A__						:				Tuple         =			torch.Size([1, 14, 768])
											self.assertEqual(outputs.last_hidden_state.shape      ,		_A)

											A__						:				Optional[Any]         =			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))




def 						snake_case__							(       __lowercase							)		->		int:







						"""simple docstring"""

						A__						:				Any         =			0
						for plain_chr in plain_text:
												A__						:				List[str]         =			(
												    ord(__lowercase							) + (hash_value << 6) + (hash_value << 1_6) - hash_value
												)
						return hash_value