




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





     """simple docstring"""



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






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

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]     )      ->      Dict:






         """simple docstring"""
         a__  			=							ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size     )

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

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

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

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

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

         a__  			=							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			:							str     )      ->      List[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 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]    ,					A			:							int    ,					A			:							Union[str, Any]    ,					A			:							Optional[int]    ,					A			:							int    ,					A			:							Optional[Any]    ,					A			:							Optional[int]    ,					A			:							Union[str, Any]    ,					A			:							Dict    ,					)      ->      Any:






         """simple docstring"""
         a__  			=							MarkupLMModel(config=A     )
         model.to(A     )
         model.eval()
         print("""Configs:"""    ,					model.config.tag_pad_id    ,					model.config.subs_pad_id     )
         a__  			=							model(A    ,					attention_mask=A    ,					token_type_ids=A     )
         a__  			=							model(A    ,					token_type_ids=A     )
         a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							Dict    ,					A			:							int    ,					A			:							int    ,					A			:							Tuple    ,					A			:							Tuple    ,					A			:							Tuple    ,					A			:							Any    ,					A			:							Optional[int]    ,					A			:							int    ,					)      ->      List[str]:






         """simple docstring"""
         a__  			=							self.num_labels
         a__  			=							MarkupLMForSequenceClassification(A     )
         model.to(A     )
         model.eval()
         a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]    ,					A			:							Tuple    ,					A			:							Tuple    ,					A			:							Optional[int]    ,					A			:							int    ,					A			:							Dict    ,					A			:							str    ,					A			:							List[str]    ,					A			:							Union[str, Any]    ,					)      ->      Union[str, Any]:






         """simple docstring"""
         a__  			=							self.num_labels
         a__  			=							MarkupLMForTokenClassification(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]    ,					A			:							int    ,					A			:							str    ,					A			:							Optional[Any]    ,					A			:							List[str]    ,					A			:							str    ,					A			:							Tuple    ,					A			:							List[str]    ,					A			:							str    ,					)      ->      List[str]:






         """simple docstring"""
         a__  			=							MarkupLMForQuestionAnswering(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      str:






         """simple docstring"""
         a__  			=							self.prepare_config_and_inputs()
         (
             (
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		
         )  			=							config_and_inputs
         a__  			=							{
             """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 		__UpperCAmelCase(		lowercase__							,						lowercase__							,						unittest.TestCase			):





     """simple docstring"""



     __UpperCAmelCase						:			List[Any]							=     (
         (
             MarkupLMModel,
             MarkupLMForSequenceClassification,
             MarkupLMForTokenClassification,
             MarkupLMForQuestionAnswering,
         )
         if is_torch_available()
         else None
     )
     __UpperCAmelCase						:			Union[str, 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			:							Union[str, Any]    ,					A			:							int    ,					A			:							List[str]    ,					A			:							List[str]    ,					A			:							Any    ,					A			:							str     )      ->      int:






         """simple docstring"""
         # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
         # (batch of pretokenized examples).
         return True

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






         """simple docstring"""
         a__  			=							MarkupLMModelTester(self     )
         a__  			=							ConfigTester(self    ,					config_class=A    ,					hidden_size=37     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							int     )      ->      List[str]:






         """simple docstring"""
         self.config_tester.run_common_tests()

     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      Optional[int]:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      Dict:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							int     )      ->      Dict:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A     )






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






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A     )






def 							__lowerCAmelCase			():
    a__  			=							"""
    <!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 		__UpperCAmelCase(		unittest.TestCase			):





     """simple docstring"""



     @cached_property
     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]     )      ->      Tuple:






         """simple docstring"""
         # TODO use from_pretrained here
         a__  			=							MarkupLMFeatureExtractor()
         a__  			=							MarkupLMTokenizer.from_pretrained("""microsoft/markuplm-base"""     )

         return MarkupLMProcessor(A    ,					A     )






     @slow
     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      List[str]:






         """simple docstring"""
         a__  			=							MarkupLMModel.from_pretrained("""microsoft/markuplm-base"""     ).to(A     )

         a__  			=							self.default_processor

         a__  			=							processor(prepare_html_string()    ,					return_tensors="""pt"""     )
         a__  			=							inputs.to(A     )

         # forward pass
         with torch.no_grad():
             a__  			=							model(**A     )

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

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












import math
from collections import OrderedDict

import torch
from packaging import version
from torch import Tensor, nn

from .utils import logging


SCREAMING_SNAKE_CASE_    :       Tuple       =							logging.get_logger(__name__)







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



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






         """simple docstring"""
         super().__init__()
         if version.parse(torch.__version__     ) < version.parse("""1.12.0"""     ):
             raise ImportError(
                 f"""You are using torch=={torch.__version__}, but torch>=1.12.0 is required to use """
                 """PytorchGELUTanh. Please upgrade torch."""     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							str    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return nn.functional.gelu(A    ,					approximate="""tanh"""     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							str    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return 0.5 * input * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi     ) * (input + 0.0_4_4_7_1_5 * torch.pow(A    ,					3.0     ))     ))







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def __init__(   self			:							Optional[Any]    ,					A			:							bool = False     )      ->      Optional[Any]:






         """simple docstring"""
         super().__init__()
         if use_gelu_python:
             a__  			=							self._gelu_python
         else:
             a__  			=							nn.functional.gelu

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return input * 0.5 * (1.0 + torch.erf(input / math.sqrt(2.0     )     ))






     def 					__SCREAMING_SNAKE_CASE		(   self			:							Union[str, Any]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return self.act(A     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							Any    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return 0.5 * input * (1.0 + torch.tanh(input * 0.7_9_7_8_8_4_5_6_0_8 * (1.0 + 0.0_4_4_7_1_5 * input * input)     ))







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return input * torch.sigmoid(1.7_0_2 * input     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def __init__(   self			:							Tuple    ,					A			:							float    ,					A			:							float     )      ->      Optional[Any]:






         """simple docstring"""
         if min > max:
             raise ValueError(f"""min should be < max (got min: {min}, max: {max})"""     )

         super().__init__()
         a__  			=							min
         a__  			=							max






     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return torch.clip(gelu(A     )    ,					self.min    ,					self.max     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def __init__(   self			:							Tuple     )      ->      List[Any]:






         """simple docstring"""
         super().__init__()
         a__  			=							math.sqrt(2 / math.pi     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							int    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return 0.5 * input * (1 + torch.tanh(self.precomputed_constant * (input + 0.0_4_4_7_1_5 * torch.pow(A    ,					3     ))     ))







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return nn.functional.silu(A     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def __init__(   self			:							Optional[Any]     )      ->      List[str]:






         """simple docstring"""
         super().__init__()
         if version.parse(torch.__version__     ) < version.parse("""1.9.0"""     ):
             a__  			=							self._mish_python
         else:
             a__  			=							nn.functional.mish

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Any    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return input * torch.tanh(nn.functional.softplus(A     )     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							Union[str, Any]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return self.act(A     )







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]    ,					A			:							Tensor     )      ->      Tensor:






         """simple docstring"""
         return input







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]    ,					A			:							str    ,					A			:							Dict=0.7_0_7_1_0_7    ,					A			:							Union[str, Any]=0.2_8_2_0_9_5     )      ->      int:






         """simple docstring"""
         a__  			=							(input - mu).div(sigma * math.sqrt(2.0     )     )
         return 0.5 * (1.0 + torch.erf(A     ))







class 		__UpperCAmelCase(		nn.Module			):





     """simple docstring"""



     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]    ,					A			:							List[Any]     )      ->      Tuple:






         """simple docstring"""
         a__  			=							nn.functional.relu(A     )
         a__  			=							torch.square(A     )
         return squared







class 		__UpperCAmelCase(		lowercase__			):





     """simple docstring"""



     def __getitem__(   self			:							List[str]    ,					A			:							int     )      ->      Any:






         """simple docstring"""
         a__  			=							super().__getitem__(A     )
         a__			,		a__  			=							content if isinstance(A    ,					A     ) else (content, {})
         return cls(**A     )






SCREAMING_SNAKE_CASE_    :       List[Any]       =							{
    '''gelu''': GELUActivation,
    '''gelu_10''': (ClippedGELUActivation, {'''min''': -10, '''max''': 10}),
    '''gelu_fast''': FastGELUActivation,
    '''gelu_new''': NewGELUActivation,
    '''gelu_python''': (GELUActivation, {'''use_gelu_python''': True}),
    '''gelu_pytorch_tanh''': PytorchGELUTanh,
    '''gelu_accurate''': AccurateGELUActivation,
    '''laplace''': LaplaceActivation,
    '''linear''': LinearActivation,
    '''mish''': MishActivation,
    '''quick_gelu''': QuickGELUActivation,
    '''relu''': nn.ReLU,
    '''relu2''': ReLUSquaredActivation,
    '''relu6''': nn.ReLUa,
    '''sigmoid''': nn.Sigmoid,
    '''silu''': SiLUActivation,
    '''swish''': SiLUActivation,
    '''tanh''': nn.Tanh,
}
SCREAMING_SNAKE_CASE_    :       Any       =							ClassInstantier(ACTaCLS)
def 							__lowerCAmelCase			(UpperCAmelCase_		):
    if activation_string in ACTaFN:
        return ACTaFN[activation_string]
    else:
        raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()		)}"""		)


# For backwards compatibility with: from activations import gelu_python
SCREAMING_SNAKE_CASE_    :       Optional[int]       =							get_activation('''gelu_python''')
SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							get_activation('''gelu_new''')
SCREAMING_SNAKE_CASE_    :       str       =							get_activation('''gelu''')
SCREAMING_SNAKE_CASE_    :       Optional[Any]       =							get_activation('''gelu_fast''')
SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							get_activation('''quick_gelu''')
SCREAMING_SNAKE_CASE_    :       Tuple       =							get_activation('''silu''')
SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							get_activation('''mish''')







SCREAMING_SNAKE_CASE_    :       Tuple       =							get_activation('''linear''')






