






'''simple docstring'''







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__     :
     '''simple docstring'''







     def __init__(self     :						str					,					lowercase__     :						str					,					lowercase__     :						Union[str, Any]=13					,					lowercase__     :						int=7					,					lowercase__     :						int=True					,					lowercase__     :						Union[str, Any]=True					,					lowercase__     :						Tuple=True					,					lowercase__     :						Any=True					,					lowercase__     :						Any=99					,					lowercase__     :						Any=32					,					lowercase__     :						Tuple=5					,					lowercase__     :						Dict=4					,					lowercase__     :						Optional[Any]=37					,					lowercase__     :						Dict="gelu"					,					lowercase__     :						List[str]=0.1					,					lowercase__     :						str=0.1					,					lowercase__     :						Dict=512					,					lowercase__     :						List[Any]=16					,					lowercase__     :						Dict=2					,					lowercase__     :						Dict=0.02					,					lowercase__     :						Optional[int]=3					,					lowercase__     :						int=None					,					lowercase__     :						Optional[int]=20					,					lowercase__     :						Any=30					,					lowercase__     :						List[Any]=2					,					lowercase__     :						Any=2					,					lowercase__     :						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      _UpperCamelCase (self     :						Tuple       )	->							Union[str, Any]:
           __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      _UpperCamelCase (self     :						int       )	->							List[Any]:
           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					,					lowercase__     :						Optional[Any]					,					lowercase__     :						Tuple					,					lowercase__     :						str					,					lowercase__     :						Optional[int]					,					lowercase__     :						List[str]					,					lowercase__     :						Optional[int]					,					lowercase__     :						Any					,					lowercase__     :						Any					,					)	->							List[str]:
           __A		 =   MarkupLMModel(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           print("Configs:"					,					model.config.tag_pad_id					,					model.config.subs_pad_id       )
           __A		 =   model(lowercase__					,					attention_mask=lowercase__					,					token_type_ids=lowercase__       )
           __A		 =   model(lowercase__					,					token_type_ids=lowercase__       )
           __A		 =   model(lowercase__       )
           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     :						Dict					,					lowercase__     :						Optional[int]					,					lowercase__     :						Optional[Any]					,					lowercase__     :						Any					,					lowercase__     :						Optional[Any]					,					lowercase__     :						str					,					lowercase__     :						Optional[int]					,					lowercase__     :						Tuple					,					lowercase__     :						Optional[Any]					,					)	->							int:
           __A		 =   self.num_labels
           __A		 =   MarkupLMForSequenceClassification(lowercase__       )
           model.to(lowercase__       )
           model.eval()
           __A		 =   model(
               lowercase__					,					xpath_tags_seq=lowercase__					,					xpath_subs_seq=lowercase__					,					attention_mask=lowercase__					,					token_type_ids=lowercase__					,					labels=lowercase__					,					)
           self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.num_labels)       )






     def      _UpperCamelCase (self     :						Dict					,					lowercase__     :						Tuple					,					lowercase__     :						Dict					,					lowercase__     :						str					,					lowercase__     :						List[Any]					,					lowercase__     :						List[str]					,					lowercase__     :						Any					,					lowercase__     :						Any					,					lowercase__     :						List[str]					,					)	->							Dict:
           __A		 =   self.num_labels
           __A		 =   MarkupLMForTokenClassification(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           __A		 =   model(
               lowercase__					,					xpath_tags_seq=lowercase__					,					xpath_subs_seq=lowercase__					,					attention_mask=lowercase__					,					token_type_ids=lowercase__					,					labels=lowercase__					,					)
           self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.seq_length, self.num_labels)       )






     def      _UpperCamelCase (self     :						int					,					lowercase__     :						Optional[Any]					,					lowercase__     :						Tuple					,					lowercase__     :						Optional[Any]					,					lowercase__     :						Union[str, Any]					,					lowercase__     :						int					,					lowercase__     :						Union[str, Any]					,					lowercase__     :						Tuple					,					lowercase__     :						List[Any]					,					)	->							Tuple:
           __A		 =   MarkupLMForQuestionAnswering(config=lowercase__       )
           model.to(lowercase__       )
           model.eval()
           __A		 =   model(
               lowercase__					,					xpath_tags_seq=lowercase__					,					xpath_subs_seq=lowercase__					,					attention_mask=lowercase__					,					token_type_ids=lowercase__					,					start_positions=lowercase__					,					end_positions=lowercase__					,					)
           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     :						int       )	->							Any:
           __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        a__     (      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     :						Optional[Any]					,					lowercase__     :						int					,					lowercase__     :						List[str]					,					lowercase__     :						Any					,					lowercase__     :						int					,					lowercase__     :						int       )	->							Optional[Any]:
           # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
           # (batch of pretokenized examples).
           return True






     def      _UpperCamelCase (self     :						List[str]       )	->							Any:
           __A		 =   MarkupLMModelTester(self       )
           __A		 =   ConfigTester(self					,					config_class=lowercase__					,					hidden_size=37       )






     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							List[Any]:
           self.config_tester.run_common_tests()






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






     def      _UpperCamelCase (self     :						List[str]       )	->							Any:
           __A		 =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowercase__       )






     def      _UpperCamelCase (self     :						str       )	->							Union[str, Any]:
           __A		 =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*lowercase__       )






     def      _UpperCamelCase (self     :						List[Any]       )	->							Any:
           __A		 =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowercase__       )







def     _UpperCamelCase				(  ):
      __A		 =   "\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        a__     (      unittest.TestCase    ):
     '''simple docstring'''







     @cached_property
     def      _UpperCamelCase (self     :						Tuple       )	->							Optional[int]:
           # TODO use from_pretrained here
           __A		 =   MarkupLMFeatureExtractor()
           __A		 =   MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"       )

           return MarkupLMProcessor(lowercase__					,					lowercase__       )






     @slow
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							Union[str, Any]:
           __A		 =   MarkupLMModel.from_pretrained("microsoft/markuplm-base"       ).to(lowercase__       )

           __A		 =   self.default_processor

           __A		 =   processor(prepare_html_string()					,					return_tensors="pt"       )
           __A		 =   inputs.to(lowercase__       )

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

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

           __A		 =   torch.tensor(
               [[0.06_75, -0.00_52, 0.50_01], [-0.22_81, 0.08_02, 0.21_92], [-0.05_83, -0.33_11, 0.11_85]]       ).to(lowercase__       )

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










'''simple docstring'''







import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment






class        a__     (      unittest.TestCase    ):
     '''simple docstring'''







     def      _UpperCamelCase (self     :						int       )	->							Optional[int]:
           __A		 =   inspect.getfile(accelerate.test_utils       )
           __A		 =   os.path.sep.join(mod_file.split(os.path.sep       )[:-1] + ["scripts", "test_sync.py"]       )






     @require_cpu
     def      _UpperCamelCase (self     :						str       )	->							Optional[Any]:
           debug_launcher(test_sync.main					,					num_processes=1       )






     @require_cpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							Optional[Any]:
           debug_launcher(test_sync.main       )






     @require_single_gpu
     def      _UpperCamelCase (self     :						Union[str, Any]       )	->							List[str]:
           test_sync.main()






     @require_multi_gpu
     def      _UpperCamelCase (self     :						Optional[int]       )	->							List[str]:
           print(f"Found {torch.cuda.device_count()} devices."       )
           __A		 =   ["torchrun", f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
           with patch_environment(omp_num_threads=1       ):
                 execute_subprocess_async(lowercase__					,					env=os.environ.copy()       )


