





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




   def __init__(self      :Tuple       ,  _UpperCamelCase      :List[str]       ,  _UpperCamelCase      :Optional[int]=13       ,  _UpperCamelCase      :Optional[Any]=7       ,  _UpperCamelCase      :List[str]=True       ,  _UpperCamelCase      :str=True       ,  _UpperCamelCase      :Tuple=True       ,  _UpperCamelCase      :Optional[Any]=True       ,  _UpperCamelCase      :Union[str, Any]=99       ,  _UpperCamelCase      :Optional[Any]=32       ,  _UpperCamelCase      :Any=5       ,  _UpperCamelCase      :str=4       ,  _UpperCamelCase      :Optional[Any]=37       ,  _UpperCamelCase      :List[str]="gelu"       ,  _UpperCamelCase      :str=0.1       ,  _UpperCamelCase      :Union[str, Any]=0.1       ,  _UpperCamelCase      :List[Any]=512       ,  _UpperCamelCase      :Tuple=16       ,  _UpperCamelCase      :List[Any]=2       ,  _UpperCamelCase      :Dict=0.0_2       ,  _UpperCamelCase      :Union[str, Any]=3       ,  _UpperCamelCase      :int=None       ,  _UpperCamelCase      :Dict=20       ,  _UpperCamelCase      :Any=30       ,  _UpperCamelCase      :Optional[Any]=2       ,  _UpperCamelCase      :Dict=2       ,  _UpperCamelCase      :List[str]=10       ,  )->   Tuple:
         __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 _lowerCAmelCase (self      :Optional[Any]					)->   str:
         __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 _lowerCAmelCase (self      :Union[str, Any]					)->   List[str]:
         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 _lowerCAmelCase (self      :Union[str, Any]       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :Dict       ,  _UpperCamelCase      :List[str]       ,  _UpperCamelCase      :Dict       ,  _UpperCamelCase      :Optional[Any]       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :Dict       ,  )->   Dict:
         __A			  =							MarkupLMModel(config=_lowerCamelCase					)
         model.to(_lowerCamelCase					)
         model.eval()
         print('''Configs:'''       ,  model.config.tag_pad_id       ,  model.config.subs_pad_id					)
         __A			  =							model(_lowerCamelCase       ,  attention_mask=_lowerCamelCase       ,  token_type_ids=_lowerCamelCase					)
         __A			  =							model(_lowerCamelCase       ,  token_type_ids=_lowerCamelCase					)
         __A			  =							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 _lowerCAmelCase (self      :Union[str, Any]       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :int       ,  )->   Any:
         __A			  =							self.num_labels
         __A			  =							MarkupLMForSequenceClassification(_lowerCamelCase					)
         model.to(_lowerCamelCase					)
         model.eval()
         __A			  =							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 _lowerCAmelCase (self      :Union[str, Any]       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :int       ,  _UpperCamelCase      :Optional[Any]       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :Dict       ,  )->   Any:
         __A			  =							self.num_labels
         __A			  =							MarkupLMForTokenClassification(config=_lowerCamelCase					)
         model.to(_lowerCamelCase					)
         model.eval()
         __A			  =							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 _lowerCAmelCase (self      :int       ,  _UpperCamelCase      :List[Any]       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :List[str]       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :List[str]       ,  _UpperCamelCase      :Tuple       ,  )->   Dict:
         __A			  =							MarkupLMForQuestionAnswering(config=_lowerCamelCase					)
         model.to(_lowerCamelCase					)
         model.eval()
         __A			  =							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 _lowerCAmelCase (self      :Any					)->   int:
         __A			  =							self.prepare_config_and_inputs()
         (
             __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 A_   (     a__					, a__					, unittest.TestCase  ):
   lowerCAmelCase__  	=					(
       (
           MarkupLMModel,
           MarkupLMForSequenceClassification,
           MarkupLMForTokenClassification,
           MarkupLMForQuestionAnswering,
       )
       if is_torch_available()
       else None
   )
   lowerCAmelCase__  	=					(
       {
           'feature-extraction': MarkupLMModel,
           'question-answering': MarkupLMForQuestionAnswering,
           'text-classification': MarkupLMForSequenceClassification,
           'token-classification': MarkupLMForTokenClassification,
           'zero-shot': MarkupLMForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )




   def _lowerCAmelCase (self      :Optional[int]       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :Tuple       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :Any					)->   str:
         # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
         # (batch of pretokenized examples).
         return True




   def _lowerCAmelCase (self      :Optional[Any]					)->   Optional[Any]:
         __A			  =							MarkupLMModelTester(self					)
         __A			  =							ConfigTester(self       ,  config_class=_lowerCamelCase       ,  hidden_size=37					)




   def _lowerCAmelCase (self      :Tuple					)->   int:
         self.config_tester.run_common_tests()




   def _lowerCAmelCase (self      :List[Any]					)->   Optional[int]:
         __A			  =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCamelCase					)




   def _lowerCAmelCase (self      :Tuple					)->   Optional[Any]:
         __A			  =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase					)




   def _lowerCAmelCase (self      :Optional[Any]					)->   Tuple:
         __A			  =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase					)






   def _lowerCAmelCase (self      :Optional[int]					)->   Optional[int]:
         __A			  =							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase					)





def _a							(      )      ->		List[Any]:







      '''simple docstring'''






      __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 A_   (     unittest.TestCase  ):




   @cached_property
   def _lowerCAmelCase (self      :int					)->   Any:
         # TODO use from_pretrained here
         __A			  =							MarkupLMFeatureExtractor()
         __A			  =							MarkupLMTokenizer.from_pretrained('''microsoft/markuplm-base'''					)

         return MarkupLMProcessor(_lowerCamelCase       ,  _lowerCamelCase					)






   @slow
   def _lowerCAmelCase (self      :str					)->   int:
         __A			  =							MarkupLMModel.from_pretrained('''microsoft/markuplm-base'''					).to(_lowerCamelCase					)

         __A			  =							self.default_processor

         __A			  =							processor(prepare_html_string()       ,  return_tensors='''pt'''					)
         __A			  =							inputs.to(_lowerCamelCase					)

         # forward pass
         with torch.no_grad():
               __A			  =							model(**_lowerCamelCase					)

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

         __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(_lowerCamelCase					)

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







import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


a__				:      Tuple    		=     [nn.LayerNorm]

a__				:      Tuple    		=     logging.get_logger(__name__)

a__				:      int    		=     version.parse(version.parse(torch.__version__).base_version)

a__				:      Dict    		=     parsed_torch_version_base >= version.parse('''2.0''')
a__				:      Any    		=     parsed_torch_version_base >= version.parse('''1.12''')
a__				:      Union[str, Any]    		=     parsed_torch_version_base >= version.parse('''1.11''')
a__				:      Union[str, Any]    		=     parsed_torch_version_base < version.parse('''1.11''')







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ ,						a__ ,						self   ):



    """simple docstring"""



    from torch import _softmax_backward_data

    if is_torch_less_than_1_11:
        return _softmax_backward_data(a__ ,						a__ ,						parent.dim ,						self   )
    else:
        return _softmax_backward_data(a__ ,						a__ ,						parent.dim ,						self.dtype   )







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ = 0   ):



    """simple docstring"""



    SCREAMING_SNAKE_CASE				:				Optional[Any]				      = index.to(layer.weight.device   )
    SCREAMING_SNAKE_CASE				:				Optional[Any]				      = layer.weight.index_select(a__ ,						a__   ).clone().detach()
    if layer.bias is not None:
        if dim == 1:
            SCREAMING_SNAKE_CASE				:				int				      = layer.bias.clone().detach()
        else:
            SCREAMING_SNAKE_CASE				:				List[Any]				      = layer.bias[index].clone().detach()
    SCREAMING_SNAKE_CASE				:				List[Any]				      = list(layer.weight.size()   )
    SCREAMING_SNAKE_CASE				:				List[Any]				      = len(a__   )
    SCREAMING_SNAKE_CASE				:				str				      = nn.Linear(new_size[1] ,						new_size[0] ,						bias=layer.bias is not None   ).to(layer.weight.device   )
    SCREAMING_SNAKE_CASE				:				Optional[Any]				      = False
    new_layer.weight.copy_(W.contiguous()   )
    SCREAMING_SNAKE_CASE				:				Tuple				      = True
    if layer.bias is not None:
        SCREAMING_SNAKE_CASE				:				Dict				      = False
        new_layer.bias.copy_(b.contiguous()   )
        SCREAMING_SNAKE_CASE				:				Tuple				      = True
    return new_layer






class     a_       (			nn.Module							):



       """simple docstring"""






       def __init__(		self				,       _lowerCamelCase				,       _lowerCamelCase   )					->int:
           super().__init__()
           SCREAMING_SNAKE_CASE				:				List[str]				      = nf
           SCREAMING_SNAKE_CASE				:				List[Any]				      = nn.Parameter(torch.empty(_lowerCamelCase				,       _lowerCamelCase   )   )
           SCREAMING_SNAKE_CASE				:				Tuple				      = nn.Parameter(torch.zeros(_lowerCamelCase   )   )
           nn.init.normal_(self.weight				,       std=0.0_2   )






       def 	__lowerCAmelCase     (		self				,       _lowerCamelCase   )					->Tuple:
           SCREAMING_SNAKE_CASE				:				Optional[Any]				      = x.size()[:-1] + (self.nf,)
           SCREAMING_SNAKE_CASE				:				int				      = torch.addmm(self.bias				,       x.view(-1				,       x.size(-1   )   )				,       self.weight   )
           SCREAMING_SNAKE_CASE				:				Tuple				      = x.view(_lowerCamelCase   )
           return x







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ = 1   ):



    """simple docstring"""



    SCREAMING_SNAKE_CASE				:				str				      = index.to(layer.weight.device   )
    SCREAMING_SNAKE_CASE				:				Any				      = layer.weight.index_select(a__ ,						a__   ).clone().detach()
    if dim == 0:
        SCREAMING_SNAKE_CASE				:				Tuple				      = layer.bias.clone().detach()
    else:
        SCREAMING_SNAKE_CASE				:				Optional[int]				      = layer.bias[index].clone().detach()
    SCREAMING_SNAKE_CASE				:				int				      = list(layer.weight.size()   )
    SCREAMING_SNAKE_CASE				:				Tuple				      = len(a__   )
    SCREAMING_SNAKE_CASE				:				Any				      = ConvaD(new_size[1] ,						new_size[0]   ).to(layer.weight.device   )
    SCREAMING_SNAKE_CASE				:				Optional[int]				      = False
    new_layer.weight.copy_(W.contiguous()   )
    SCREAMING_SNAKE_CASE				:				int				      = True
    SCREAMING_SNAKE_CASE				:				Any				      = False
    new_layer.bias.copy_(b.contiguous()   )
    SCREAMING_SNAKE_CASE				:				int				      = True
    return new_layer







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ = None   ):



    """simple docstring"""



    if isinstance(a__ ,						nn.Linear   ):
        return prune_linear_layer(a__ ,						a__ ,						dim=0 if dim is None else dim   )
    elif isinstance(a__ ,						a__   ):
        return prune_convad_layer(a__ ,						a__ ,						dim=1 if dim is None else dim   )
    else:
        raise ValueError(F"""Can't prune layer of class {layer.__class__}"""   )







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ ,						*a__   ):



    """simple docstring"""



    assert len(a__   ) > 0, F"""{input_tensors} has to be a tuple/list of tensors"""

    # inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
    SCREAMING_SNAKE_CASE				:				str				      = len(inspect.signature(a__   ).parameters   )
    if num_args_in_forward_chunk_fn != len(a__   ):
        raise ValueError(
            F"""forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(a__   )} input """
            '''tensors are given'''   )

    if chunk_size > 0:
        SCREAMING_SNAKE_CASE				:				Dict				      = input_tensors[0].shape[chunk_dim]
        for input_tensor in input_tensors:
            if input_tensor.shape[chunk_dim] != tensor_shape:
                raise ValueError(
                    F"""All input tenors have to be of the same shape: {tensor_shape}, """
                    F"""found shape {input_tensor.shape[chunk_dim]}"""   )

        if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
            raise ValueError(
                F"""The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk """
                F"""size {chunk_size}"""   )

        SCREAMING_SNAKE_CASE				:				Any				      = input_tensors[0].shape[chunk_dim] // chunk_size

        # chunk input tensor into tuples
        SCREAMING_SNAKE_CASE				:				Optional[int]				      = tuple(input_tensor.chunk(a__ ,						dim=a__   ) for input_tensor in input_tensors   )
        # apply forward fn to every tuple
        SCREAMING_SNAKE_CASE				:				Tuple				      = tuple(forward_fn(*a__   ) for input_tensors_chunk in zip(*a__   )   )
        # concatenate output at same dimension
        return torch.cat(a__ ,						dim=a__   )

    return forward_fn(*a__   )







def 				UpperCAmelCase_(    a__ ,						a__ ,						a__ ,						a__   ):



    """simple docstring"""



    SCREAMING_SNAKE_CASE				:				Optional[int]				      = torch.ones(a__ ,						a__   )
    SCREAMING_SNAKE_CASE				:				str				      = set(a__   ) - already_pruned_heads  # Convert to set and remove already pruned heads
    for head in heads:
        # Compute how many pruned heads are before the head and move the index accordingly
        SCREAMING_SNAKE_CASE				:				Optional[Any]				      = head - sum(1 if h < head else 0 for h in already_pruned_heads   )
        SCREAMING_SNAKE_CASE				:				str				      = 0
    SCREAMING_SNAKE_CASE				:				List[str]				      = mask.view(-1   ).contiguous().eq(1   )
    SCREAMING_SNAKE_CASE				:				torch.LongTensor				      = torch.arange(len(a__   )   )[mask].long()
    return heads, index







def 				UpperCAmelCase_(    *a__ ,						a__ = None   ):



    """simple docstring"""



    return torch.meshgrid(*a__ ,						indexing=a__   )







def 				UpperCAmelCase_(    a__   ):



    """simple docstring"""



    return tensor.device, storage_ptr(a__   ), storage_size(a__   )
