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


    '''simple docstring'''





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




         '''simple docstring'''



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




         '''simple docstring'''



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

         __A					:					Union[str, Any]								=		ids_tensor(
             [self.batch_size, self.seq_length, self.max_depth]					,       self.max_xpath_tag_unit_embeddings	)

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

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

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

         __A					:					Optional[int]								=		None
         __A					:					List[str]								=		None
         if self.use_labels:
              __A					:					Union[str, Any]								=		ids_tensor([self.batch_size]					,       self.type_sequence_label_size	)
              __A					:					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 						_a							(  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 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					str								=		MarkupLMModel(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         print('Configs:'					,       model.config.tag_pad_id					,       model.config.subs_pad_id	)
         __A					:					Tuple								=		model(__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase	)
         __A					:					Dict								=		model(__lowerCamelCase					,       token_type_ids=__lowerCamelCase	)
         __A					:					Dict								=		model(__lowerCamelCase	)
         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					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Optional[Any]								=		self.num_labels
         __A					:					Tuple								=		MarkupLMForSequenceClassification(__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					Union[str, Any]								=		model(
             __lowerCamelCase					,       xpath_tags_seq=__lowerCamelCase					,       xpath_subs_seq=__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase					,       )
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.num_labels)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					Any								=		self.num_labels
         __A					:					int								=		MarkupLMForTokenClassification(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					List[Any]								=		model(
             __lowerCamelCase					,       xpath_tags_seq=__lowerCamelCase					,       xpath_subs_seq=__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       labels=__lowerCamelCase					,       )
         self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.seq_length, self.num_labels)	)
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       ):




         '''simple docstring'''



         __A					:					List[Any]								=		MarkupLMForQuestionAnswering(config=__lowerCamelCase	)
         model.to(__lowerCamelCase	)
         model.eval()
         __A					:					int								=		model(
             __lowerCamelCase					,       xpath_tags_seq=__lowerCamelCase					,       xpath_subs_seq=__lowerCamelCase					,       attention_mask=__lowerCamelCase					,       token_type_ids=__lowerCamelCase					,       start_positions=__lowerCamelCase					,       end_positions=__lowerCamelCase					,       )
         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	):




         '''simple docstring'''



         __A					:					Optional[Any]								=		self.prepare_config_and_inputs()
         (
             (
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		(
             __A
         )						,		
         )					:					List[Any]								=		config_and_inputs
         __A					:					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 							__snake_case(					A_				,	A_				,	unittest.TestCase	):


    '''simple docstring'''





    _UpperCAmelCase       =			(
        (
            MarkupLMModel,
            MarkupLMForSequenceClassification,
            MarkupLMForTokenClassification,
            MarkupLMForQuestionAnswering,
        )
        if is_torch_available()
        else None
    )
    _UpperCAmelCase       =			(
        {
            "feature-extraction": MarkupLMModel,
            "question-answering": MarkupLMForQuestionAnswering,
            "text-classification": MarkupLMForSequenceClassification,
            "token-classification": MarkupLMForTokenClassification,
            "zero-shot": MarkupLMForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    def 						_a							(  self					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase	):




         '''simple docstring'''



         return True
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					int								=		MarkupLMModelTester(self	)
         __A					:					List[str]								=		ConfigTester(self					,       config_class=__lowerCamelCase					,       hidden_size=37	)
    def 						_a							(  self	):




         '''simple docstring'''



         self.config_tester.run_common_tests()
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Optional[int]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[str]								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase	)
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					str								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase	)

    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					Any								=		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase	)







def 						_lowercase				(  )    ->     Optional[Any]:







     '''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 							__snake_case(					unittest.TestCase	):


    '''simple docstring'''





    @cached_property
    def 						_a							(  self	):




         '''simple docstring'''



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

         return MarkupLMProcessor(__lowerCamelCase					,       __lowerCamelCase	)

    @slow
    def 						_a							(  self	):




         '''simple docstring'''



         __A					:					List[Any]								=		MarkupLMModel.from_pretrained('microsoft/markuplm-base'	).to(__lowerCamelCase	)

         __A					:					Optional[Any]								=		self.default_processor

         __A					:					List[Any]								=		processor(prepare_html_string()					,       return_tensors='pt'	)
         __A					:					Union[str, Any]								=		inputs.to(__lowerCamelCase	)

         # forward pass
         with torch.no_grad():
              __A					:					Optional[int]								=		model(**__lowerCamelCase	)

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

         __A					:					str								=		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(__lowerCamelCase	)

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




"""simple docstring"""







def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				list[int]						)    ->     tuple[int, int]:







     '''simple docstring'''


     if len(_SCREAMING_SNAKE_CASE						) == 1:
          return a[0], a[0]
     else:
          # split A into half.
          __A					:					List[str]								=		a[: len(_SCREAMING_SNAKE_CASE						) // 2]
          __A					:					Optional[Any]								=		a[len(_SCREAMING_SNAKE_CASE						) // 2 :]

          # 2 sub problems, 1/2 of original size.
          __A						,		__A					:					int								=		max_difference(_SCREAMING_SNAKE_CASE						)
          __A						,		__A					:					str								=		max_difference(_SCREAMING_SNAKE_CASE						)

          # get min of first and max of second
          # linear time
          __A					:					str								=		min(_SCREAMING_SNAKE_CASE						)
          __A					:					Optional[int]								=		max(_SCREAMING_SNAKE_CASE						)

          # 3 cases, either (small1, big1),
          # (min_first, max_second), (small2, big2)
          # constant comparisons
          if biga - smalla > max_second - min_first and biga - smalla > biga - smalla:
               return smalla, biga
          elif biga - smalla > max_second - min_first:
               return smalla, biga
          else:
               return min_first, max_second


if __name__ == "__main__":
       import doctest

       doctest.testmod()



