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




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



									'''simple docstring'''







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




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



									'''simple docstring'''







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

									snake_case__ :     str 							=				ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]   ,	self.max_xpath_tag_unit_embeddings		)

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

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

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

									snake_case__ :     Dict 							=				None
									snake_case__ :     Tuple 							=				None
									if self.use_labels:
													snake_case__ :     Optional[int] 							=				ids_tensor([self.batch_size]   ,	self.type_sequence_label_size		)
													snake_case__ :     Union[str, Any] 							=				ids_tensor([self.batch_size, self.seq_length]   ,	self.num_labels		)

									snake_case__ :     Optional[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		)					->  Any:



									'''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   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	)					->  Dict:



									'''simple docstring'''







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



									'''simple docstring'''







									snake_case__ :     Optional[Any] 							=				self.num_labels
									snake_case__ :     str 							=				MarkupLMForSequenceClassification(__UpperCamelCase		)
									model.to(__UpperCamelCase		)
									model.eval()
									snake_case__ :     List[str] 							=				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       __a      (       self   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	)					->  Optional[int]:



									'''simple docstring'''







									snake_case__ :     Tuple 							=				self.num_labels
									snake_case__ :     int 							=				MarkupLMForTokenClassification(config=__UpperCamelCase		)
									model.to(__UpperCamelCase		)
									model.eval()
									snake_case__ :     Optional[Any] 							=				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       __a      (       self   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	)					->  Optional[int]:



									'''simple docstring'''







									snake_case__ :     Tuple 							=				MarkupLMForQuestionAnswering(config=__UpperCamelCase		)
									model.to(__UpperCamelCase		)
									model.eval()
									snake_case__ :     Dict 							=				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       __a      (       self		)					->  Dict:



									'''simple docstring'''







									snake_case__ :     Optional[Any] 							=				self.prepare_config_and_inputs()
									(
									    (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  (
									    snake_case__
									)							,  
									) :     Tuple 							=				config_and_inputs
									snake_case__ :     Tuple 							=				{
									    '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 			__snake_case  (      _lowerCamelCase   ,_lowerCamelCase   ,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       __a      (       self   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase   ,	__UpperCamelCase		)					->  Dict:



									'''simple docstring'''







									return True




					def       __a      (       self		)					->  Dict:



									'''simple docstring'''







									snake_case__ :     Optional[int] 							=				MarkupLMModelTester(self		)
									snake_case__ :     List[str] 							=				ConfigTester(self   ,	config_class=__UpperCamelCase   ,	hidden_size=37		)




					def       __a      (       self		)					->  str:



									'''simple docstring'''







									self.config_tester.run_common_tests()




					def       __a      (       self		)					->  Dict:



									'''simple docstring'''







									snake_case__ :     Dict 							=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__UpperCamelCase		)




					def       __a      (       self		)					->  str:



									'''simple docstring'''







									snake_case__ :     Tuple 							=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase		)




					def       __a      (       self		)					->  Tuple:



									'''simple docstring'''







									snake_case__ :     int 							=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase		)




					def       __a      (       self		)					->  str:



									'''simple docstring'''







									snake_case__ :     str 							=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase		)




def 			UpperCamelCase__						(    )       -> List[str]:
				snake_case__ :     List[Any] 							=				'\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 			__snake_case  (      unittest.TestCase     ):




					@cached_property
					def       __a      (       self		)					->  Any:



									'''simple docstring'''







									snake_case__ :     Tuple 							=				MarkupLMFeatureExtractor()
									snake_case__ :     Dict 							=				MarkupLMTokenizer.from_pretrained('microsoft/markuplm-base'		)

									return MarkupLMProcessor(__UpperCamelCase   ,	__UpperCamelCase		)




					@slow
					def       __a      (       self		)					->  Tuple:



									'''simple docstring'''







									snake_case__ :     Union[str, Any] 							=				MarkupLMModel.from_pretrained('microsoft/markuplm-base'		).to(__UpperCamelCase		)

									snake_case__ :     Tuple 							=				self.default_processor

									snake_case__ :     Optional[int] 							=				processor(prepare_html_string()   ,	return_tensors='pt'		)
									snake_case__ :     Union[str, Any] 							=				inputs.to(__UpperCamelCase		)

									# forward pass
									with torch.no_grad():
													snake_case__ :     List[str] 							=				model(**__UpperCamelCase		)

									# verify the last hidden states
									snake_case__ :     List[Any] 							=				torch.Size([1, 14, 768]		)
									self.assertEqual(outputs.last_hidden_state.shape   ,	__UpperCamelCase		)

									snake_case__ :     Dict 							=				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(__UpperCamelCase		)

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



import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


lowerCAmelCase__					:       Tuple		=       logging.getLogger(__name__)





@dataclass
class 			__snake_case  :
					__lowerCamelCase	 =       field(
					    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}     )
					__lowerCamelCase	 =       field(
					    default=_lowerCamelCase   ,metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}     )
					__lowerCamelCase	 =       field(
					    default=_lowerCamelCase   ,metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}     )
					__lowerCamelCase	 =       field(default=_lowerCamelCase   ,metadata={"""help""": """Set this flag to use fast tokenization."""}     )
					# If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
					# or just modify its tokenizer_config.json.
					__lowerCamelCase	 =       field(
					    default=_lowerCamelCase   ,metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}   ,)




def 			UpperCamelCase__						(    )       -> Optional[Any]:
				# See all possible arguments in src/transformers/training_args.py
				# or by passing the --help flag to this script.
				# We now keep distinct sets of args, for a cleaner separation of concerns.

				snake_case__ :     Optional[Any] 							=				HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)   )

				if len(sys.argv   ) == 2 and sys.argv[1].endswith('.json'   ):
								# If we pass only one argument to the script and it's the path to a json file,
								# let's parse it to get our arguments.
								snake_case__							,  snake_case__							,  snake_case__ :     Tuple 							=				parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]   )   )
				else:
								snake_case__							,  snake_case__							,  snake_case__ :     Optional[int] 							=				parser.parse_args_into_dataclasses()

				if (
				    os.path.exists(training_args.output_dir   )
				    and os.listdir(training_args.output_dir   )
				    and training_args.do_train
				    and not training_args.overwrite_output_dir
				):
								raise ValueError(
								    F"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
								    ' --overwrite_output_dir to overcome.'   )

				# Setup logging
				logging.basicConfig(
				    format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'      ,			datefmt='%m/%d/%Y %H:%M:%S'      ,			level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN      ,			)
				logger.warning(
				    'Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s'      ,			training_args.local_rank      ,			training_args.device      ,			training_args.n_gpu      ,			bool(training_args.local_rank != -1   )      ,			training_args.fpaa      ,			)
				# Set the verbosity to info of the Transformers logger (on main process only):
				if is_main_process(training_args.local_rank   ):
								transformers.utils.logging.set_verbosity_info()
								transformers.utils.logging.enable_default_handler()
								transformers.utils.logging.enable_explicit_format()
				logger.info('Training/evaluation parameters %s'      ,			A__   )

				# Prepare Question-Answering task
				# Load pretrained model and tokenizer
				#
				# Distributed training:
				# The .from_pretrained methods guarantee that only one local process can concurrently
				# download model & vocab.

				snake_case__ :     Optional[int] 							=				AutoConfig.from_pretrained(
				    model_args.config_name if model_args.config_name else model_args.model_name_or_path      ,			cache_dir=model_args.cache_dir      ,			)
				snake_case__ :     str 							=				AutoTokenizer.from_pretrained(
				    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path      ,			cache_dir=model_args.cache_dir      ,			use_fast=A__      ,			)
				snake_case__ :     Optional[int] 							=				AutoModelForQuestionAnswering.from_pretrained(
				    model_args.model_name_or_path      ,			from_tf=bool('.ckpt' in model_args.model_name_or_path   )      ,			config=A__      ,			cache_dir=model_args.cache_dir      ,			)

				# Get datasets
				snake_case__ :     Dict 							=				hasattr(model.config      ,			'lang2id'   )
				snake_case__ :     List[str] 							=				(
				    SquadDataset(
				        A__      ,			tokenizer=A__      ,			is_language_sensitive=A__      ,			cache_dir=model_args.cache_dir   )
				    if training_args.do_train
				    else None
				)
				snake_case__ :     str 							=				(
				    SquadDataset(
				        A__      ,			tokenizer=A__      ,			mode='dev'      ,			is_language_sensitive=A__      ,			cache_dir=model_args.cache_dir      ,			)
				    if training_args.do_eval
				    else None
				)

				# Data collator
				snake_case__ :     Dict 							=				DataCollatorWithPadding(A__      ,			pad_to_multiple_of=8   ) if training_args.fpaa else None

				# Initialize our Trainer
				snake_case__ :     str 							=				Trainer(
				    model=A__      ,			args=A__      ,			train_dataset=A__      ,			eval_dataset=A__      ,			data_collator=A__      ,			)

				# Training
				if training_args.do_train:
								trainer.train(
								    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path   ) else None   )
								trainer.save_model()
								# For convenience, we also re-save the tokenizer to the same directory,
								# so that you can share your model easily on huggingface.co/models =)
								if trainer.is_world_master():
												tokenizer.save_pretrained(training_args.output_dir   )
def 			UpperCamelCase__						(    A__   )       -> Tuple:
				# For xla_spawn (TPUs)
				main()


if __name__ == "__main__":
				main()


