

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





       """simple docstring"""


       def __init__(      self ,				snake_case ,				snake_case=13 ,				snake_case=7 ,				snake_case=True ,				snake_case=True ,				snake_case=True ,				snake_case=True ,				snake_case=99 ,				snake_case=32 ,				snake_case=5 ,				snake_case=4 ,				snake_case=37 ,				snake_case="gelu" ,				snake_case=0.1 ,				snake_case=0.1 ,				snake_case=512 ,				snake_case=16 ,				snake_case=2 ,				snake_case=0.02 ,				snake_case=3 ,				snake_case=None ,				snake_case=20 ,				snake_case=30 ,				snake_case=2 ,				snake_case=2 ,				snake_case=10 ,				):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

        lowerCAmelCase__      :							List[Any]    =							ids_tensor(
            [self.batch_size, self.seq_length, self.max_depth] ,				self.max_xpath_tag_unit_embeddings      )

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

        lowerCAmelCase__      :							Dict    =							None
        if self.use_input_mask:
         lowerCAmelCase__      :							Optional[Any]    =							ids_tensor([self.batch_size, self.seq_length] ,				vocab_size=2      )

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

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

        lowerCAmelCase__      :							Tuple    =							self.get_config()

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






       def  SCREAMING_SNAKE_CASE_  (      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  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							MarkupLMModel(config=snake_case      )
        model.to(snake_case      )
        model.eval()
        print("Configs:" ,				model.config.tag_pad_id ,				model.config.subs_pad_id      )
        lowerCAmelCase__      :							Tuple    =							model(snake_case ,				attention_mask=snake_case ,				token_type_ids=snake_case      )
        lowerCAmelCase__      :							Any    =							model(snake_case ,				token_type_ids=snake_case      )
        lowerCAmelCase__      :							str    =							model(snake_case      )
        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  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				):





        """simple docstring"""

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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				):





        """simple docstring"""

        lowerCAmelCase__      :							Any    =							self.num_labels
        lowerCAmelCase__      :							Any    =							MarkupLMForTokenClassification(config=snake_case      )
        model.to(snake_case      )
        model.eval()
        lowerCAmelCase__      :							Dict    =							model(
            snake_case ,				xpath_tags_seq=snake_case ,				xpath_subs_seq=snake_case ,				attention_mask=snake_case ,				token_type_ids=snake_case ,				labels=snake_case ,				)
        self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)      )






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				):





        """simple docstring"""

        lowerCAmelCase__      :							Optional[Any]    =							MarkupLMForQuestionAnswering(config=snake_case      )
        model.to(snake_case      )
        model.eval()
        lowerCAmelCase__      :							Tuple    =							model(
            snake_case ,				xpath_tags_seq=snake_case ,				xpath_subs_seq=snake_case ,				attention_mask=snake_case ,				token_type_ids=snake_case ,				start_positions=snake_case ,				end_positions=snake_case ,				)
        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  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							self.prepare_config_and_inputs()
        (
            (
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	(
            lowerCAmelCase__
        ) ,	
        )      :							Optional[int]    =							config_and_inputs
        lowerCAmelCase__      :							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  __a				(	__magic_name__					,  __magic_name__					,  unittest.TestCase					):





       """simple docstring"""


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






       def  SCREAMING_SNAKE_CASE_  (      self ,				snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case      ):





        """simple docstring"""

        return True






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							MarkupLMModelTester(self      )
        lowerCAmelCase__      :							Optional[int]    =							ConfigTester(self ,				config_class=snake_case ,				hidden_size=37      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        self.config_tester.run_common_tests()






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							str    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Tuple    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							Union[str, Any]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case      )






       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							List[str]    =							self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case      )




def 			SCREAMING_SNAKE_CASE       (   ) ->	Tuple:
 lowerCAmelCase__      :							str    =							"\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  __a				(	unittest.TestCase					):





       """simple docstring"""


       @cached_property
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

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

        return MarkupLMProcessor(snake_case ,				snake_case      )






       @slow
       def  SCREAMING_SNAKE_CASE_  (      self      ):





        """simple docstring"""

        lowerCAmelCase__      :							str    =							MarkupLMModel.from_pretrained("microsoft/markuplm-base"      ).to(snake_case      )

        lowerCAmelCase__      :							List[str]    =							self.default_processor

        lowerCAmelCase__      :							Any    =							processor(prepare_html_string() ,				return_tensors="pt"      )
        lowerCAmelCase__      :							Optional[int]    =							inputs.to(snake_case      )

        # forward pass
        with torch.no_grad():
         lowerCAmelCase__      :							int    =							model(**snake_case      )

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

        lowerCAmelCase__      :							str    =							torch.tensor(
            [[0.0_675, -0.0_052, 0.5_001], [-0.2_281, 0.0_802, 0.2_192], [-0.0_583, -0.3_311, 0.1_185]]      ).to(snake_case      )

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









"""simple docstring"""

import logging
import os
from dataclasses import dataclass, field
from typing import Dict, List, Optional

import numpy as np
import torch
from utils_hans import HansDataset, InputFeatures, hans_processors, hans_tasks_num_labels

import transformers
from transformers import (
    AutoConfig,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    default_data_collator,
    set_seed,
)
from transformers.trainer_utils import is_main_process


_UpperCamelCase		    =						logging.getLogger(__name__)






@dataclass
class  __a				:





       """simple docstring"""


       __UpperCamelCase				:			str          =	field(
           metadata={'help': 'Path to pretrained model or model identifier from huggingface.co/models'}					)
       __UpperCamelCase				:			Optional[str]          =	field(
           default=__magic_name__					,  metadata={'help': 'Pretrained config name or path if not the same as model_name'}					)
       __UpperCamelCase				:			Optional[str]          =	field(
           default=__magic_name__					,  metadata={'help': 'Pretrained tokenizer name or path if not the same as model_name'}					)
       __UpperCamelCase				:			Optional[str]          =	field(
           default=__magic_name__					,  metadata={'help': 'Where do you want to store the pretrained models downloaded from huggingface.co'}					,  )






@dataclass
class  __a				:





       """simple docstring"""


       __UpperCamelCase				:			str          =	field(
           metadata={'help': 'The name of the task to train selected in the list: ' + ', '.join(hans_processors.keys()					)}					)
       __UpperCamelCase				:			str          =	field(
           metadata={'help': 'The input data dir. Should contain the .tsv files (or other data files) for the task.'}					)
       __UpperCamelCase				:			int          =	field(
           default=128					,  metadata={
               'help': (
                   'The maximum total input sequence length after tokenization. Sequences longer '
                   'than this will be truncated, sequences shorter will be padded.'
               )
           }					,  )
       __UpperCamelCase				:			bool          =	field(
           default=__magic_name__					,  metadata={'help': 'Overwrite the cached training and evaluation sets'}					)




def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	Dict[str, torch.Tensor]:
 lowerCAmelCase__      :							List[Any]    =							default_data_collator(lowercase__      )
 lowerCAmelCase__      :							Union[str, Any]    =							batch.pop("pairID"				,				lowercase__      )
 return batch



def 			SCREAMING_SNAKE_CASE       (   ) ->	int:
 # 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.

 lowerCAmelCase__      :							str    =							HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)      )
 lowerCAmelCase__ ,	lowerCAmelCase__ ,	lowerCAmelCase__      :							List[Any]    =							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"				,				lowercase__      )

 # Set seed
 set_seed(training_args.seed      )

 try:
  lowerCAmelCase__      :							Tuple    =							hans_tasks_num_labels[data_args.task_name]
 except KeyError:
  raise ValueError("Task not found: %s" % (data_args.task_name)      )

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

 lowerCAmelCase__      :							int    =							AutoConfig.from_pretrained(
     model_args.config_name if model_args.config_name else model_args.model_name_or_path				,				num_labels=lowercase__				,				finetuning_task=data_args.task_name				,				cache_dir=model_args.cache_dir				,				)
 lowerCAmelCase__      :							Optional[int]    =							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				,				)
 lowerCAmelCase__      :							Union[str, Any]    =							AutoModelForSequenceClassification.from_pretrained(
     model_args.model_name_or_path				,				from_tf=bool(".ckpt" in model_args.model_name_or_path      )				,				config=lowercase__				,				cache_dir=model_args.cache_dir				,				)

 # Get datasets
 lowerCAmelCase__      :							Dict    =							(
     HansDataset(
         data_dir=data_args.data_dir				,				tokenizer=lowercase__				,				task=data_args.task_name				,				max_seq_length=data_args.max_seq_length				,				overwrite_cache=data_args.overwrite_cache				,				)
     if training_args.do_train
     else None
 )
 lowerCAmelCase__      :							Union[str, Any]    =							(
     HansDataset(
         data_dir=data_args.data_dir				,				tokenizer=lowercase__				,				task=data_args.task_name				,				max_seq_length=data_args.max_seq_length				,				overwrite_cache=data_args.overwrite_cache				,				evaluate=lowercase__				,				)
     if training_args.do_eval
     else None
 )

 # Initialize our Trainer
 lowerCAmelCase__      :							Any    =							Trainer(
     model=lowercase__				,				args=lowercase__				,				train_dataset=lowercase__				,				eval_dataset=lowercase__				,				data_collator=lowercase__				,				)

 # 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      )

    # Evaluation
 if training_args.do_eval:
  logger.info("*** Evaluate ***"      )

  lowerCAmelCase__      :							Tuple    =							trainer.predict(lowercase__      )
  lowerCAmelCase__      :							Optional[Any]    =							output.predictions
  lowerCAmelCase__      :							Dict    =							np.argmax(lowercase__				,				axis=1      )

  lowerCAmelCase__      :							int    =							[ex.pairID for ex in eval_dataset]
  lowerCAmelCase__      :							Tuple    =							os.path.join(training_args.output_dir				,				"hans_predictions.txt"      )
  lowerCAmelCase__      :							Tuple    =							eval_dataset.get_labels()
  if trainer.is_world_master():
   with open(lowercase__				,				"w"      ) as writer:
    writer.write("pairID,gold_label\n"      )
    for pid, pred in zip(lowercase__				,				lowercase__      ):
     writer.write("ex" + str(lowercase__      ) + "," + label_list[int(lowercase__      )] + "\n"      )

  trainer._log(output.metrics      )



def 			SCREAMING_SNAKE_CASE       (   lowercase__      ) ->	Optional[Any]:
 # For xla_spawn (TPUs)
 main()


if __name__ == "__main__":
    main()






