


from __future__ import annotations

from enum import Enum


class 		_SCREAMING_SNAKE_CASE    (	A    ):
     __SCREAMING_SNAKE_CASE    				=						2_4
     __SCREAMING_SNAKE_CASE    				=						2_1
     __SCREAMING_SNAKE_CASE    				=						1_8
     __SCREAMING_SNAKE_CASE    				=						1_5
     __SCREAMING_SNAKE_CASE    				=						1_2
     __SCREAMING_SNAKE_CASE    				=						9
     __SCREAMING_SNAKE_CASE    				=						6
     __SCREAMING_SNAKE_CASE    				=						3
     __SCREAMING_SNAKE_CASE    				=						2
     __SCREAMING_SNAKE_CASE    				=						1
     __SCREAMING_SNAKE_CASE    				=						-1
     __SCREAMING_SNAKE_CASE    				=						-2
     __SCREAMING_SNAKE_CASE    				=						-3
     __SCREAMING_SNAKE_CASE    				=						-6
     __SCREAMING_SNAKE_CASE    				=						-9
     __SCREAMING_SNAKE_CASE    				=						-1_2
     __SCREAMING_SNAKE_CASE    				=						-1_5
     __SCREAMING_SNAKE_CASE    				=						-1_8
     __SCREAMING_SNAKE_CASE    				=						-2_1
     __SCREAMING_SNAKE_CASE    				=						-2_4


class 		_SCREAMING_SNAKE_CASE    (	A    ):
     __SCREAMING_SNAKE_CASE    				=						8
     __SCREAMING_SNAKE_CASE    				=						7
     __SCREAMING_SNAKE_CASE    				=						6
     __SCREAMING_SNAKE_CASE    				=						5
     __SCREAMING_SNAKE_CASE    				=						4
     __SCREAMING_SNAKE_CASE    				=						3
     __SCREAMING_SNAKE_CASE    				=						2
     __SCREAMING_SNAKE_CASE    				=						1




def       a__						(		snake_case__				:       float	,      snake_case__				:       str | SIUnit	,      snake_case__				:       str | SIUnit	,      ):
  if isinstance(snake_case__	,      snake_case__    ):
    _UpperCAmelCase		:       List[str]       =    SIUnit[known_prefix.lower()]
  if isinstance(snake_case__	,      snake_case__    ):
    _UpperCAmelCase		:       Optional[Any]       =    SIUnit[unknown_prefix.lower()]
  _UpperCAmelCase		:       float       =    known_amount * (
      10 ** (known_prefix.value - unknown_prefix.value)
  )
  return unknown_amount




def       a__						(		snake_case__				:       float	,      snake_case__				:       str | BinaryUnit	,      snake_case__				:       str | BinaryUnit	,      ):
  if isinstance(snake_case__	,      snake_case__    ):
    _UpperCAmelCase		:       Optional[Any]       =    BinaryUnit[known_prefix.lower()]
  if isinstance(snake_case__	,      snake_case__    ):
    _UpperCAmelCase		:       List[Any]       =    BinaryUnit[unknown_prefix.lower()]
  _UpperCAmelCase		:       float       =    known_amount * (
      2 ** ((known_prefix.value - unknown_prefix.value) * 10)
  )
  return unknown_amount


if __name__ == "__main__":
 import doctest

 doctest.testmod()









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       ,				A_       ,				A_=13       ,				A_=7       ,				A_=True       ,				A_=True       ,				A_=True       ,				A_=True       ,				A_=99       ,				A_=32       ,				A_=5       ,				A_=4       ,				A_=37       ,				A_="gelu"       ,				A_=0.1       ,				A_=0.1       ,				A_=5_12       ,				A_=16       ,				A_=2       ,				A_=0.0_2       ,				A_=3       ,				A_=None       ,				A_=20       ,				A_=30       ,				A_=2       ,				A_=2       ,				A_=10       ,				):
       _UpperCAmelCase		:       str       =    parent
       _UpperCAmelCase		:       Dict       =    batch_size
       _UpperCAmelCase		:       Optional[int]       =    seq_length
       _UpperCAmelCase		:       Union[str, Any]       =    is_training
       _UpperCAmelCase		:       Tuple       =    use_input_mask
       _UpperCAmelCase		:       Dict       =    use_token_type_ids
       _UpperCAmelCase		:       Dict       =    use_labels
       _UpperCAmelCase		:       Optional[int]       =    vocab_size
       _UpperCAmelCase		:       List[str]       =    hidden_size
       _UpperCAmelCase		:       Tuple       =    num_hidden_layers
       _UpperCAmelCase		:       Tuple       =    num_attention_heads
       _UpperCAmelCase		:       Optional[int]       =    intermediate_size
       _UpperCAmelCase		:       str       =    hidden_act
       _UpperCAmelCase		:       Optional[Any]       =    hidden_dropout_prob
       _UpperCAmelCase		:       List[str]       =    attention_probs_dropout_prob
       _UpperCAmelCase		:       Union[str, Any]       =    max_position_embeddings
       _UpperCAmelCase		:       List[Any]       =    type_vocab_size
       _UpperCAmelCase		:       Union[str, Any]       =    type_sequence_label_size
       _UpperCAmelCase		:       List[Any]       =    initializer_range
       _UpperCAmelCase		:       Union[str, Any]       =    num_labels
       _UpperCAmelCase		:       Any       =    scope
       _UpperCAmelCase		:       List[str]       =    max_xpath_tag_unit_embeddings
       _UpperCAmelCase		:       Tuple       =    max_xpath_subs_unit_embeddings
       _UpperCAmelCase		:       Optional[Any]       =    tag_pad_id
       _UpperCAmelCase		:       Union[str, Any]       =    subs_pad_id
       _UpperCAmelCase		:       Optional[int]       =    max_depth


     def 					__snake_case(     self						):
       _UpperCAmelCase		:       Dict       =    ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size						)

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

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

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

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

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

       _UpperCAmelCase		:       List[Any]       =    self.get_config()

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


     def 					__snake_case(     self						):
       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 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
       _UpperCAmelCase		:       Tuple       =    MarkupLMModel(config=A_						)
       model.to(A_						)
       model.eval()
       print("""Configs:"""       ,				model.config.tag_pad_id       ,				model.config.subs_pad_id						)
       _UpperCAmelCase		:       List[Any]       =    model(A_       ,				attention_mask=A_       ,				token_type_ids=A_						)
       _UpperCAmelCase		:       Tuple       =    model(A_       ,				token_type_ids=A_						)
       _UpperCAmelCase		:       Union[str, Any]       =    model(A_						)
       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 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
       _UpperCAmelCase		:       Optional[int]       =    self.num_labels
       _UpperCAmelCase		:       List[Any]       =    MarkupLMForSequenceClassification(A_						)
       model.to(A_						)
       model.eval()
       _UpperCAmelCase		:       Any       =    model(
           A_       ,				xpath_tags_seq=A_       ,				xpath_subs_seq=A_       ,				attention_mask=A_       ,				token_type_ids=A_       ,				labels=A_       ,				)
       self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)						)


     def 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
       _UpperCAmelCase		:       List[Any]       =    self.num_labels
       _UpperCAmelCase		:       Any       =    MarkupLMForTokenClassification(config=A_						)
       model.to(A_						)
       model.eval()
       _UpperCAmelCase		:       Tuple       =    model(
           A_       ,				xpath_tags_seq=A_       ,				xpath_subs_seq=A_       ,				attention_mask=A_       ,				token_type_ids=A_       ,				labels=A_       ,				)
       self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.num_labels)						)


     def 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_       ,				):
       _UpperCAmelCase		:       List[Any]       =    MarkupLMForQuestionAnswering(config=A_						)
       model.to(A_						)
       model.eval()
       _UpperCAmelCase		:       List[Any]       =    model(
           A_       ,				xpath_tags_seq=A_       ,				xpath_subs_seq=A_       ,				attention_mask=A_       ,				token_type_ids=A_       ,				start_positions=A_       ,				end_positions=A_       ,				)
       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 					__snake_case(     self						):
       _UpperCAmelCase		:       Union[str, Any]       =    self.prepare_config_and_inputs()
       (
           (
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),(
           _UpperCAmelCase
       ),
       )		:       List[str]       =    config_and_inputs
       _UpperCAmelCase		:       Optional[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 		_SCREAMING_SNAKE_CASE    (	A       ,  A       ,  unittest.TestCase    ):
     __SCREAMING_SNAKE_CASE    				=						(
         (
             MarkupLMModel,
             MarkupLMForSequenceClassification,
             MarkupLMForTokenClassification,
             MarkupLMForQuestionAnswering,
         )
         if is_torch_available()
         else None
     )
     __SCREAMING_SNAKE_CASE    				=						(
         {
             '''feature-extraction''': MarkupLMModel,
             '''question-answering''': MarkupLMForQuestionAnswering,
             '''text-classification''': MarkupLMForSequenceClassification,
             '''token-classification''': MarkupLMForTokenClassification,
             '''zero-shot''': MarkupLMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )


     def 					__snake_case(     self       ,				A_       ,				A_       ,				A_       ,				A_       ,				A_						):
       # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
       # (batch of pretokenized examples).
       return True


     def 					__snake_case(     self						):
       _UpperCAmelCase		:       Union[str, Any]       =    MarkupLMModelTester(self						)
       _UpperCAmelCase		:       str       =    ConfigTester(self       ,				config_class=A_       ,				hidden_size=37						)


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


     def 					__snake_case(     self						):
       _UpperCAmelCase		:       List[Any]       =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_						)


     def 					__snake_case(     self						):
       _UpperCAmelCase		:       Optional[int]       =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*A_						)


     def 					__snake_case(     self						):
       _UpperCAmelCase		:       Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*A_						)




     def 					__snake_case(     self						):
       _UpperCAmelCase		:       Any       =    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*A_						)




def       a__						(		):
  _UpperCAmelCase		:       Any       =    """
    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>

    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>

    </body>
    </html>
    """

  return html_string





@require_torch
class 		_SCREAMING_SNAKE_CASE    (	unittest.TestCase    ):


     @cached_property
     def 					__snake_case(     self						):
       # TODO use from_pretrained here
       _UpperCAmelCase		:       int       =    MarkupLMFeatureExtractor()
       _UpperCAmelCase		:       int       =    MarkupLMTokenizer.from_pretrained("""microsoft/markuplm-base"""						)

       return MarkupLMProcessor(A_       ,				A_						)




     @slow
     def 					__snake_case(     self						):
       _UpperCAmelCase		:       List[Any]       =    MarkupLMModel.from_pretrained("""microsoft/markuplm-base"""						).to(A_						)

       _UpperCAmelCase		:       Optional[Any]       =    self.default_processor

       _UpperCAmelCase		:       str       =    processor(prepare_html_string()       ,				return_tensors="""pt"""						)
       _UpperCAmelCase		:       List[str]       =    inputs.to(A_						)

       # forward pass
       with torch.no_grad():
         _UpperCAmelCase		:       Optional[Any]       =    model(**A_						)

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

       _UpperCAmelCase		:       List[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(A_						)

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





