





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    ,							)->   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 						SCREAMING_SNAKE_CASE__			(   self      )->   Dict:
     __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      )->   Tuple:
     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    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							)->   Union[str, Any]:
     __A    =  MarkupLMModel(config=UpperCamelCase__      )
     model.to(UpperCamelCase__      )
     model.eval()
     print('''Configs:'''    ,							model.config.tag_pad_id    ,							model.config.subs_pad_id      )
     __A    =  model(UpperCamelCase__    ,							attention_mask=UpperCamelCase__    ,							token_type_ids=UpperCamelCase__      )
     __A    =  model(UpperCamelCase__    ,							token_type_ids=UpperCamelCase__      )
     __A    =  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 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							)->   Union[str, Any]:
     __A    =  self.num_labels
     __A    =  MarkupLMForSequenceClassification(UpperCamelCase__      )
     model.to(UpperCamelCase__      )
     model.eval()
     __A    =  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 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							)->   Dict:
     __A    =  self.num_labels
     __A    =  MarkupLMForTokenClassification(config=UpperCamelCase__      )
     model.to(UpperCamelCase__      )
     model.eval()
     __A    =  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 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							)->   List[str]:
     __A    =  MarkupLMForQuestionAnswering(config=UpperCamelCase__      )
     model.to(UpperCamelCase__      )
     model.eval()
     __A    =  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 						SCREAMING_SNAKE_CASE__			(   self      )->   str:
     __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 		_lowerCAmelCase(		__A						,       __A						,       unittest.TestCase):

  """simple docstring"""







  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 						SCREAMING_SNAKE_CASE__			(   self    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase    ,							UpperCAmelCase      )->   Any:
     # 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      )->   Any:
     __A    =  MarkupLMModelTester(self      )
     __A    =  ConfigTester(self    ,							config_class=UpperCamelCase__    ,							hidden_size=37      )

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   str:
     self.config_tester.run_common_tests()

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Tuple:
     __A    =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase__      )

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Union[str, Any]:
     __A    =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase__      )

  def 						SCREAMING_SNAKE_CASE__			(   self      )->   Any:
     __A    =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*UpperCamelCase__      )





  def 						SCREAMING_SNAKE_CASE__			(   self      )->   List[str]:
     __A    =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*UpperCamelCase__      )
def  __UpperCamelCase    (						)    ->		Dict:


   '''simple docstring'''







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

  """simple docstring"""







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

     return MarkupLMProcessor(UpperCamelCase__    ,							UpperCamelCase__      )





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

     __A    =  self.default_processor

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

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

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

     __A    =  torch.tensor(
         [[0.0675, -0.0052, 0.5001], [-0.2281, 0.0802, 0.2192], [-0.0583, -0.3311, 0.1185]]      ).to(UpperCamelCase__      )

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









def        _lowerCamelCase(      lowerCAmelCase__					:		int = 99_8001   ):





				'''simple docstring'''







				for number in range(n - 1     ,     9999     ,     -1   ):
								SCREAMING_SNAKE_CASE_ :						int    				= str(lowerCAmelCase__   )

								# checks whether 'str_number' is a palindrome.
								if str_number == str_number[::-1]:
												SCREAMING_SNAKE_CASE_ :						str    				= 999

												# if 'number' is a product of two 3-digit numbers
												# then number is the answer otherwise fetch next number.
												while divisor != 99:
																if (number % divisor == 0) and (len(str(number // divisor   )   ) == 3.0):
																				return number
																divisor -= 1
				raise ValueError('That number is larger than our acceptable range.'   )


if __name__ == "__main__":
				print(f"""{solution() = }""")