




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







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




         """simple docstring"""


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







   def UpperCamelCase      (	self   :      Any				):




         """simple docstring"""


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

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

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

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

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

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

         _UpperCAmelCase													=					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   :      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 UpperCamelCase      (	self   :      int   ,   snake_case__   :      List[Any]   ,   snake_case__   :      Optional[Any]   ,   snake_case__   :      Tuple   ,   snake_case__   :      Tuple   ,   snake_case__   :      List[Any]   ,   snake_case__   :      Optional[Any]   ,   snake_case__   :      Optional[Any]   ,   snake_case__   :      Optional[Any]   ,   ):




         """simple docstring"""


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




         """simple docstring"""


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




         """simple docstring"""


         _UpperCAmelCase													=					self.num_labels
         _UpperCAmelCase													=					MarkupLMForTokenClassification(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()
         _UpperCAmelCase													=					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 UpperCamelCase      (	self   :      List[str]   ,   snake_case__   :      int   ,   snake_case__   :      Tuple   ,   snake_case__   :      Dict   ,   snake_case__   :      Any   ,   snake_case__   :      Dict   ,   snake_case__   :      List[Any]   ,   snake_case__   :      Any   ,   snake_case__   :      Any   ,   ):




         """simple docstring"""


         _UpperCAmelCase													=					MarkupLMForQuestionAnswering(config=snake_case__				)
         model.to(snake_case__				)
         model.eval()
         _UpperCAmelCase													=					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 UpperCamelCase      (	self   :      Optional[Any]				):




         """simple docstring"""


         _UpperCAmelCase													=					self.prepare_config_and_inputs()
         (
             (
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,			(
             _UpperCAmelCase			
         )							,						
         )													=					config_and_inputs
         _UpperCAmelCase													=					{
             "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    __lowerCAmelCase			(	UpperCAmelCase__ ,		UpperCAmelCase__ ,		unittest.TestCase			):
   snake_case_				:			str								=					(
       (
           MarkupLMModel,
           MarkupLMForSequenceClassification,
           MarkupLMForTokenClassification,
           MarkupLMForQuestionAnswering,
       )
       if is_torch_available()
       else None
   )
   snake_case_				:			Any								=					(
       {
           "feature-extraction": MarkupLMModel,
           "question-answering": MarkupLMForQuestionAnswering,
           "text-classification": MarkupLMForSequenceClassification,
           "token-classification": MarkupLMForTokenClassification,
           "zero-shot": MarkupLMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )







   def UpperCamelCase      (	self   :      Optional[Any]   ,   snake_case__   :      Dict   ,   snake_case__   :      List[Any]   ,   snake_case__   :      List[str]   ,   snake_case__   :      int   ,   snake_case__   :      Optional[Any]				):




         """simple docstring"""


         return True







   def UpperCamelCase      (	self   :      List[str]				):




         """simple docstring"""


         _UpperCAmelCase													=					MarkupLMModelTester(self				)
         _UpperCAmelCase													=					ConfigTester(self   ,   config_class=snake_case__   ,   hidden_size=37				)







   def UpperCamelCase      (	self   :      Any				):




         """simple docstring"""


         self.config_tester.run_common_tests()







   def UpperCamelCase      (	self   :      Optional[Any]				):




         """simple docstring"""


         _UpperCAmelCase													=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*snake_case__				)







   def UpperCamelCase      (	self   :      int				):




         """simple docstring"""


         _UpperCAmelCase													=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*snake_case__				)







   def UpperCamelCase      (	self   :      Any				):




         """simple docstring"""


         _UpperCAmelCase													=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*snake_case__				)







   def UpperCamelCase      (	self   :      str				):




         """simple docstring"""


         _UpperCAmelCase													=					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*snake_case__				)




def 							__SCREAMING_SNAKE_CASE  (      ):

      '''simple docstring'''


      _UpperCAmelCase													=					"\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    __lowerCAmelCase			(	unittest.TestCase			):







   @cached_property
   def UpperCamelCase      (	self   :      Any				):




         """simple docstring"""


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

         return MarkupLMProcessor(snake_case__   ,   snake_case__				)







   @slow
   def UpperCamelCase      (	self   :      Optional[Any]				):




         """simple docstring"""


         _UpperCAmelCase													=					MarkupLMModel.from_pretrained("microsoft/markuplm-base"				).to(snake_case__				)

         _UpperCAmelCase													=					self.default_processor

         _UpperCAmelCase													=					processor(prepare_html_string()   ,   return_tensors="pt"				)
         _UpperCAmelCase													=					inputs.to(snake_case__				)

         # forward pass
         with torch.no_grad():
               _UpperCAmelCase													=					model(**snake_case__				)

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

         _UpperCAmelCase													=					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				)				)










from __future__ import annotations


def 							__SCREAMING_SNAKE_CASE  (      snake_case_						):

						'''simple docstring'''


						if len(snake_case_						) == 0:
												raise ValueError("find_max() arg is an empty sequence"						)
						_UpperCAmelCase													=					nums[0]
						for x in nums:
												if x > max_num:
																		_UpperCAmelCase													=					x
						return max_num


if __name__ == "__main__":
				import doctest

				doctest.testmod(verbose=True)




