


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

 '''simple docstring'''

 def __init__(	self		,      _UpperCAmelCase		,      _UpperCAmelCase=13		,      _UpperCAmelCase=7		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=True		,      _UpperCAmelCase=99		,      _UpperCAmelCase=32		,      _UpperCAmelCase=5		,      _UpperCAmelCase=4		,      _UpperCAmelCase=37		,      _UpperCAmelCase="gelu"		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=0.1		,      _UpperCAmelCase=5_12		,      _UpperCAmelCase=16		,      _UpperCAmelCase=2		,      _UpperCAmelCase=0.02		,      _UpperCAmelCase=3		,      _UpperCAmelCase=None		,      _UpperCAmelCase=20		,      _UpperCAmelCase=30		,      _UpperCAmelCase=2		,      _UpperCAmelCase=2		,      _UpperCAmelCase=10		,      ):
       snake_case_       =      parent
       snake_case_       =      batch_size
       snake_case_       =      seq_length
       snake_case_       =      is_training
       snake_case_       =      use_input_mask
       snake_case_       =      use_token_type_ids
       snake_case_       =      use_labels
       snake_case_       =      vocab_size
       snake_case_       =      hidden_size
       snake_case_       =      num_hidden_layers
       snake_case_       =      num_attention_heads
       snake_case_       =      intermediate_size
       snake_case_       =      hidden_act
       snake_case_       =      hidden_dropout_prob
       snake_case_       =      attention_probs_dropout_prob
       snake_case_       =      max_position_embeddings
       snake_case_       =      type_vocab_size
       snake_case_       =      type_sequence_label_size
       snake_case_       =      initializer_range
       snake_case_       =      num_labels
       snake_case_       =      scope
       snake_case_       =      max_xpath_tag_unit_embeddings
       snake_case_       =      max_xpath_subs_unit_embeddings
       snake_case_       =      tag_pad_id
       snake_case_       =      subs_pad_id
       snake_case_       =      max_depth



 def 						UpperCamelCase__     (	self					):
       snake_case_       =      ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size					)

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

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

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

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

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

       snake_case_       =      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					):
       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		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      ):
       snake_case_       =      MarkupLMModel(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       print('''Configs:'''		,      model.config.tag_pad_id		,      model.config.subs_pad_id					)
       snake_case_       =      model(_UpperCAmelCase		,      attention_mask=_UpperCAmelCase		,      token_type_ids=_UpperCAmelCase					)
       snake_case_       =      model(_UpperCAmelCase		,      token_type_ids=_UpperCAmelCase					)
       snake_case_       =      model(_UpperCAmelCase					)
       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		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      ):
       snake_case_       =      self.num_labels
       snake_case_       =      MarkupLMForSequenceClassification(_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       snake_case_       =      model(
           _UpperCAmelCase		,      xpath_tags_seq=_UpperCAmelCase		,      xpath_subs_seq=_UpperCAmelCase		,      attention_mask=_UpperCAmelCase		,      token_type_ids=_UpperCAmelCase		,      labels=_UpperCAmelCase		,      )
       self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)					)



 def 						UpperCamelCase__     (	self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      ):
       snake_case_       =      self.num_labels
       snake_case_       =      MarkupLMForTokenClassification(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       snake_case_       =      model(
           _UpperCAmelCase		,      xpath_tags_seq=_UpperCAmelCase		,      xpath_subs_seq=_UpperCAmelCase		,      attention_mask=_UpperCAmelCase		,      token_type_ids=_UpperCAmelCase		,      labels=_UpperCAmelCase		,      )
       self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels)					)



 def 						UpperCamelCase__     (	self		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      _UpperCAmelCase		,      ):
       snake_case_       =      MarkupLMForQuestionAnswering(config=_UpperCAmelCase					)
       model.to(_UpperCAmelCase					)
       model.eval()
       snake_case_       =      model(
           _UpperCAmelCase		,      xpath_tags_seq=_UpperCAmelCase		,      xpath_subs_seq=_UpperCAmelCase		,      attention_mask=_UpperCAmelCase		,      token_type_ids=_UpperCAmelCase		,      start_positions=_UpperCAmelCase		,      end_positions=_UpperCAmelCase		,      )
       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					):
       snake_case_       =      self.prepare_config_and_inputs()
       (
           (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    (
           snake_case_
       )       ,    
       )       =      config_and_inputs
       snake_case_       =      {
           '''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_						( lowerCamelCase__					,						lowerCamelCase__					,						unittest.TestCase       ):

 '''simple docstring'''

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



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



 def 						UpperCamelCase__     (	self					):
       snake_case_       =      MarkupLMModelTester(self					)
       snake_case_       =      ConfigTester(self		,      config_class=_UpperCAmelCase		,      hidden_size=37					)



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



 def 						UpperCamelCase__     (	self					):
       snake_case_       =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase					)



 def 						UpperCamelCase__     (	self					):
       snake_case_       =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase					)



 def 						UpperCamelCase__     (	self					):
       snake_case_       =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase					)





 def 						UpperCamelCase__     (	self					):
       snake_case_       =      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase					)






def        __lowerCAmelCase    ()->  Tuple:




      """simple docstring"""

      snake_case_       =      '''
    <!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 lowerCAmelCase_						( unittest.TestCase       ):

 '''simple docstring'''

 @cached_property
 def 						UpperCamelCase__     (	self					):
       # TODO use from_pretrained here
       snake_case_       =      MarkupLMFeatureExtractor()
       snake_case_       =      MarkupLMTokenizer.from_pretrained('''microsoft/markuplm-base'''					)

       return MarkupLMProcessor(_UpperCAmelCase		,      _UpperCAmelCase					)





 @slow
 def 						UpperCamelCase__     (	self					):
       snake_case_       =      MarkupLMModel.from_pretrained('''microsoft/markuplm-base'''					).to(_UpperCAmelCase					)

       snake_case_       =      self.default_processor

       snake_case_       =      processor(prepare_html_string()		,      return_tensors='''pt'''					)
       snake_case_       =      inputs.to(_UpperCAmelCase					)

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

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

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

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



import argparse
import os


UpperCAmelCase			=    {
    """pytorch""": {
        """1.13""": {
            """torch""": """1.13.1""",
            """torchvision""": """0.14.1""",
            """torchaudio""": """0.13.1""",
            """python""": 3.9,
            """cuda""": """cu116""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu116"""
            ),
            """base_image""": """nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.12""": {
            """torch""": """1.12.1""",
            """torchvision""": """0.13.1""",
            """torchaudio""": """0.12.1""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.11""": {
            """torch""": """1.11.0""",
            """torchvision""": """0.12.0""",
            """torchaudio""": """0.11.0""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """1.10""": {
            """torch""": """1.10.2""",
            """torchvision""": """0.11.3""",
            """torchaudio""": """0.10.2""",
            """python""": 3.9,
            """cuda""": """cu113""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2"""
                """ --extra-index-url https://download.pytorch.org/whl/cu113"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        """1.9""": {
            """torch""": """1.9.1""",
            """torchvision""": """0.10.1""",
            """torchaudio""": """0.9.1""",
            """python""": 3.9,
            """cuda""": """cu111""",
            """install""": (
                """python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1"""
                """ --extra-index-url https://download.pytorch.org/whl/cu111"""
            ),
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
    },
    """tensorflow""": {
        """2.11""": {
            """tensorflow""": """2.11.1""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.11.1""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.10""": {
            """tensorflow""": """2.10.1""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.10.1""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.9""": {
            """tensorflow""": """2.9.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.9.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.8""": {
            """tensorflow""": """2.8.2""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.8.2""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.7""": {
            """tensorflow""": """2.7.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.7.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.6""": {
            """tensorflow""": """2.6.5""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.6.5""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
        """2.5""": {
            """tensorflow""": """2.5.3""",
            """install""": """python3 -m pip install --no-cache-dir -U tensorflow==2.5.3""",
            """base_image""": """nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04""",
        },
    },
}


if __name__ == "__main__":
   UpperCAmelCase			=    argparse.ArgumentParser("""Choose the framework and version to install""")
   parser.add_argument(
       """--framework""", help="""The framework to install. Should be `torch` or `tensorflow`""", type=str, required=True
   )
   parser.add_argument("""--version""", help="""The version of the framework to install.""", type=str, required=True)
   UpperCAmelCase			=    parser.parse_args()

   UpperCAmelCase			=    past_versions_testing[args.framework][args.version]

   os.system(f'''echo "export INSTALL_CMD=\'{info['install']}\'" >> ~/.profile''')
   print(f'''echo "export INSTALL_CMD=\'{info['install']}\'" >> ~/.profile''')

   UpperCAmelCase			=    """"""
   if args.framework == "pytorch":
      UpperCAmelCase			=    info["""cuda"""]
   os.system(f'''echo \"export CUDA=\'{cuda}\'\" >> ~/.profile''')
   print(f'''echo \"export CUDA=\'{cuda}\'\" >> ~/.profile''')