





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


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



									"""simple docstring"""



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


							def        _snake_case					(							self      :List[Any]						)				->				List[str]:



									"""simple docstring"""



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

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

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

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

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

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

									SCREAMING_SNAKE_CASE__								= self.get_config()

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


							def        _snake_case					(							self      :Optional[Any]						)				->				str:



									"""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        _snake_case					(							self      :int							,			__A      :List[str]							,			__A      :Any							,			__A      :str							,			__A      :Dict							,			__A      :Tuple							,			__A      :Optional[int]							,			__A      :Tuple							,			__A      :str							,			)				->				List[str]:



									"""simple docstring"""



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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.num_labels
									SCREAMING_SNAKE_CASE__								= MarkupLMForSequenceClassification(__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= 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        _snake_case					(							self      :Dict							,			__A      :List[str]							,			__A      :int							,			__A      :Union[str, Any]							,			__A      :Tuple							,			__A      :Optional[int]							,			__A      :List[str]							,			__A      :Dict							,			__A      :Tuple							,			)				->				Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.num_labels
									SCREAMING_SNAKE_CASE__								= MarkupLMForTokenClassification(config=__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= 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        _snake_case					(							self      :List[str]							,			__A      :str							,			__A      :Any							,			__A      :Any							,			__A      :Union[str, Any]							,			__A      :List[str]							,			__A      :int							,			__A      :Dict							,			__A      :str							,			)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= MarkupLMForQuestionAnswering(config=__A						)
									model.to(__A						)
									model.eval()
									SCREAMING_SNAKE_CASE__								= 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        _snake_case					(							self      :Dict						)				->				Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.prepare_config_and_inputs()
									(
									    (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  (
									    SCREAMING_SNAKE_CASE__
									)		,  
									)								= config_and_inputs
									SCREAMING_SNAKE_CASE__								= {
									    """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 					UpperCamelCase_    (       UpperCamelCase__			,					UpperCamelCase__			,					unittest.TestCase      ):
							lowerCamelCase_          =   (
							    (
							        MarkupLMModel,
							        MarkupLMForSequenceClassification,
							        MarkupLMForTokenClassification,
							        MarkupLMForQuestionAnswering,
							    )
							    if is_torch_available()
							    else None
							)
							lowerCamelCase_          =   (
							    {
							        "feature-extraction": MarkupLMModel,
							        "question-answering": MarkupLMForQuestionAnswering,
							        "text-classification": MarkupLMForSequenceClassification,
							        "token-classification": MarkupLMForTokenClassification,
							        "zero-shot": MarkupLMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)


							def        _snake_case					(							self      :Optional[int]							,			__A      :Optional[Any]							,			__A      :int							,			__A      :Union[str, Any]							,			__A      :Tuple							,			__A      :int						)				->				int:



									"""simple docstring"""



									return True


							def        _snake_case					(							self      :Union[str, Any]						)				->				Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= MarkupLMModelTester(self						)
									SCREAMING_SNAKE_CASE__								= ConfigTester(self							,			config_class=__A							,			hidden_size=37						)


							def        _snake_case					(							self      :Tuple						)				->				Optional[Any]:



									"""simple docstring"""



									self.config_tester.run_common_tests()


							def        _snake_case					(							self      :Optional[int]						)				->				List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__A						)


							def        _snake_case					(							self      :List[Any]						)				->				List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*__A						)


							def        _snake_case					(							self      :List[str]						)				->				Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*__A						)


							def        _snake_case					(							self      :List[Any]						)				->				List[str]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*__A						)






def 			SCREAMING_SNAKE_CASE__							(    ):
		SCREAMING_SNAKE_CASE__								= """
    <!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 					UpperCamelCase_    (       unittest.TestCase      ):


							@cached_property
							def        _snake_case					(							self      :Any						)				->				Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= MarkupLMFeatureExtractor()
									SCREAMING_SNAKE_CASE__								= MarkupLMTokenizer.from_pretrained("""microsoft/markuplm-base"""						)

									return MarkupLMProcessor(__A							,			__A						)


							@slow
							def        _snake_case					(							self      :Optional[Any]						)				->				Dict:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								= MarkupLMModel.from_pretrained("""microsoft/markuplm-base"""						).to(__A						)

									SCREAMING_SNAKE_CASE__								= self.default_processor

									SCREAMING_SNAKE_CASE__								= processor(prepare_html_string()							,			return_tensors="""pt"""						)
									SCREAMING_SNAKE_CASE__								= inputs.to(__A						)

									# forward pass
									with torch.no_grad():
											SCREAMING_SNAKE_CASE__								= model(**__A						)

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

									SCREAMING_SNAKE_CASE__								= torch.tensor(
									    [[0.0_6_7_5, -0.0_0_5_2, 0.5_0_0_1], [-0.2_2_8_1, 0.0_8_0_2, 0.2_1_9_2], [-0.0_5_8_3, -0.3_3_1_1, 0.1_1_8_5]]						).to(__A						)

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






import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


_lowerCamelCase							=     500000

_lowerCamelCase			,    _lowerCamelCase							=     os.path.split(__file__)
_lowerCamelCase							=     os.path.join(RESULTS_BASEPATH, 'results', RESULTS_FILENAME.replace('.py', '.json'))






@get_duration
def 			SCREAMING_SNAKE_CASE__							(    UpperCamelCase__:							datasets.Dataset      ):
		SCREAMING_SNAKE_CASE__								= dataset.select(range(0   ,				len(UpperCamelCase__      )   ,				2      )      )






@get_duration
def 			SCREAMING_SNAKE_CASE__							(    UpperCamelCase__:							datasets.Dataset      ):
		SCREAMING_SNAKE_CASE__								= dataset.sort("""numbers"""      )






@get_duration
def 			SCREAMING_SNAKE_CASE__							(    UpperCamelCase__:							datasets.Dataset      ):
		SCREAMING_SNAKE_CASE__								= dataset.shuffle()






@get_duration
def 			SCREAMING_SNAKE_CASE__							(    UpperCamelCase__:							datasets.Dataset      ):
		SCREAMING_SNAKE_CASE__								= dataset.train_test_split(0.1      )






@get_duration
def 			SCREAMING_SNAKE_CASE__							(    UpperCamelCase__:							datasets.Dataset   ,				UpperCamelCase__:							Tuple=10      ):
		for shard_id in range(UpperCamelCase__      ):
				SCREAMING_SNAKE_CASE__								= dataset.shard(UpperCamelCase__   ,				UpperCamelCase__      )






def 			SCREAMING_SNAKE_CASE__							(    ):
		SCREAMING_SNAKE_CASE__								= {"""num examples""": SPEED_TEST_N_EXAMPLES}
		SCREAMING_SNAKE_CASE__								= (select, sort, shuffle, train_test_split, shard)
		with tempfile.TemporaryDirectory() as tmp_dir:
				print("""generating dataset"""      )
				SCREAMING_SNAKE_CASE__								= datasets.Features({"""text""": datasets.Value("""string"""      ), """numbers""": datasets.Value("""float32"""      )}      )
				SCREAMING_SNAKE_CASE__								= generate_example_dataset(
				    os.path.join(UpperCamelCase__   ,				"""dataset.arrow"""      )   ,				UpperCamelCase__   ,				num_examples=UpperCamelCase__      )
				print("""Functions"""      )
				for func in functions:
						print(func.__name__      )
						SCREAMING_SNAKE_CASE__								= func(UpperCamelCase__      )

		with open(UpperCamelCase__   ,				"""wb"""      ) as f:
				f.write(json.dumps(UpperCamelCase__      ).encode("""utf-8"""      )      )


if __name__ == "__main__":  # useful to run the profiler
	benchmark_indices_mapping()