





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




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





             """simple docstring"""






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




       def 					_UpperCamelCase  (		self							)    ->   List[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						ids_tensor([self.batch_size, self.seq_length]	,self.vocab_size							)

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

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

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

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

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

             __SCREAMING_SNAKE_CASE     :Tuple	       =						self.get_config()

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




       def 					_UpperCamelCase  (		self							)    ->   Union[str, 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 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Optional[int]	       =						MarkupLMModel(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             print('''Configs:'''	,model.config.tag_pad_id	,model.config.subs_pad_id							)
             __SCREAMING_SNAKE_CASE     :Tuple	       =						model(SCREAMING_SNAKE_CASE__	,attention_mask=SCREAMING_SNAKE_CASE__	,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE     :List[str]	       =						model(SCREAMING_SNAKE_CASE__	,token_type_ids=SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE     :Dict	       =						model(SCREAMING_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 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,)    ->   Optional[int]:





             """simple docstring"""






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




       def 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,)    ->   Any:





             """simple docstring"""






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




       def 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						MarkupLMForQuestionAnswering(config=SCREAMING_SNAKE_CASE__							)
             model.to(SCREAMING_SNAKE_CASE__							)
             model.eval()
             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						model(
                 SCREAMING_SNAKE_CASE__	,xpath_tags_seq=SCREAMING_SNAKE_CASE__	,xpath_subs_seq=SCREAMING_SNAKE_CASE__	,attention_mask=SCREAMING_SNAKE_CASE__	,token_type_ids=SCREAMING_SNAKE_CASE__	,start_positions=SCREAMING_SNAKE_CASE__	,end_positions=SCREAMING_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 					_UpperCamelCase  (		self							)    ->   List[str]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						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
             )       ,
             )     :int	       =						config_and_inputs
             __SCREAMING_SNAKE_CASE     :List[str]	       =						{
                 '''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       _SCREAMING_SNAKE_CASE(     _a					,			_a					,			unittest.TestCase						):
       SCREAMING_SNAKE_CASE_     :   List[Any]					=						(
           (
               MarkupLMModel,
               MarkupLMForSequenceClassification,
               MarkupLMForTokenClassification,
               MarkupLMForQuestionAnswering,
           )
           if is_torch_available()
           else None
       )
       SCREAMING_SNAKE_CASE_     :   List[Any]					=						(
           {
               'feature-extraction': MarkupLMModel,
               'question-answering': MarkupLMForQuestionAnswering,
               'text-classification': MarkupLMForSequenceClassification,
               'token-classification': MarkupLMForTokenClassification,
               'zero-shot': MarkupLMForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )




       def 					_UpperCamelCase  (		self	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__							)    ->   str:





             """simple docstring"""






             return True




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :str	       =						MarkupLMModelTester(self							)
             __SCREAMING_SNAKE_CASE     :List[str]	       =						ConfigTester(self	,config_class=SCREAMING_SNAKE_CASE__	,hidden_size=37							)




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





             """simple docstring"""






             self.config_tester.run_common_tests()




       def 					_UpperCamelCase  (		self							)    ->   str:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Dict	       =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   Optional[Any]:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[Any]	       =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__							)




       def 					_UpperCamelCase  (		self							)    ->   int:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Optional[int]	       =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*SCREAMING_SNAKE_CASE__							)




def   __lowerCamelCase     (							)    ->   List[Any]:
      __SCREAMING_SNAKE_CASE     :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       _SCREAMING_SNAKE_CASE(     unittest.TestCase						):




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						MarkupLMFeatureExtractor()
             __SCREAMING_SNAKE_CASE     :List[str]	       =						MarkupLMTokenizer.from_pretrained('''microsoft/markuplm-base'''							)

             return MarkupLMProcessor(SCREAMING_SNAKE_CASE__	,SCREAMING_SNAKE_CASE__							)




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





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Any	       =						MarkupLMModel.from_pretrained('''microsoft/markuplm-base'''							).to(SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						self.default_processor

             __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						processor(prepare_html_string()	,return_tensors='''pt'''							)
             __SCREAMING_SNAKE_CASE     :Dict	       =						inputs.to(SCREAMING_SNAKE_CASE__							)

             # forward pass
             with torch.no_grad():
                   __SCREAMING_SNAKE_CASE     :List[Any]	       =						model(**SCREAMING_SNAKE_CASE__							)

             # verify the last hidden states
             __SCREAMING_SNAKE_CASE     :Tuple	       =						torch.Size([1, 14, 7_68]							)
             self.assertEqual(outputs.last_hidden_state.shape	,SCREAMING_SNAKE_CASE__							)

             __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						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(SCREAMING_SNAKE_CASE__							)

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






"""simple docstring"""
def   __lowerCamelCase     (							a_  :	str							)    ->   int:
      __SCREAMING_SNAKE_CASE     :List[Any]	       =						0
      for plain_chr in plain_text:
            __SCREAMING_SNAKE_CASE     :List[str]	       =						(
                ord(a_							) + (hash_value << 6) + (hash_value << 16) - hash_value
            )
      return hash_value