

'''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 					snake_case   :





  """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=512      ,  UpperCamelCase=16      ,  UpperCamelCase=2      ,  UpperCamelCase=0.02      ,  UpperCamelCase=3      ,  UpperCamelCase=None      ,  UpperCamelCase=20      ,  UpperCamelCase=30      ,  UpperCamelCase=2      ,  UpperCamelCase=2      ,  UpperCamelCase=10      ,  ):







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






  def   snake_case		(   self					):







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

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

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

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

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

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

   lowerCamelCase_								=  self.get_config()

   return (
       config,
       input_ids,
       xpath_tags_seq,
       xpath_subs_seq,
       token_type_ids,
       input_mask,
       sequence_labels,
       token_labels,
   )






  def   snake_case		(   self					):







   """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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  MarkupLMModel(config=UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   print("Configs:"      ,  model.config.tag_pad_id      ,  model.config.subs_pad_id					)
   lowerCamelCase_								=  model(UpperCamelCase      ,  attention_mask=UpperCamelCase      ,  token_type_ids=UpperCamelCase					)
   lowerCamelCase_								=  model(UpperCamelCase      ,  token_type_ids=UpperCamelCase					)
   lowerCamelCase_								=  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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_labels
   lowerCamelCase_								=  MarkupLMForSequenceClassification(UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   lowerCamelCase_								=  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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  self.num_labels
   lowerCamelCase_								=  MarkupLMForTokenClassification(config=UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   lowerCamelCase_								=  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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  ):







   """simple docstring"""
   lowerCamelCase_								=  MarkupLMForQuestionAnswering(config=UpperCamelCase					)
   model.to(UpperCamelCase					)
   model.eval()
   lowerCamelCase_								=  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   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.prepare_config_and_inputs()
   (
       (
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,(
       lowerCamelCase_
   )			,
   )								=  config_and_inputs
   lowerCamelCase_								=  {
       "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 					snake_case   (   lowercase      ,						lowercase      ,						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   snake_case		(   self      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase      ,  UpperCamelCase					):







   """simple docstring"""
   return True






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  MarkupLMModelTester(self					)
   lowerCamelCase_								=  ConfigTester(self      ,  config_class=UpperCamelCase      ,  hidden_size=37					)






  def   snake_case		(   self					):







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






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*UpperCamelCase					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase					)






  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*UpperCamelCase					)







  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_question_answering(*UpperCamelCase					)





def __snake_case (				):
 lowerCamelCase_								=  "\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 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




  @cached_property
  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  MarkupLMFeatureExtractor()
   lowerCamelCase_								=  MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"					)

   return MarkupLMProcessor(UpperCamelCase      ,  UpperCamelCase					)







  @slow
  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  MarkupLMModel.from_pretrained("microsoft/markuplm-base"					).to(UpperCamelCase					)

   lowerCamelCase_								=  self.default_processor

   lowerCamelCase_								=  processor(prepare_html_string()      ,  return_tensors="pt"					)
   lowerCamelCase_								=  inputs.to(UpperCamelCase					)

   # forward pass
   with torch.no_grad():
    lowerCamelCase_								=  model(**UpperCamelCase					)

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

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







'''simple docstring'''

import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
     import tensorflow as tf

     from transformers.activations_tf import get_tf_activation





@require_tf
class 					snake_case   (   unittest.TestCase      ):





  """simple docstring"""




  def   snake_case		(   self					):







   """simple docstring"""
   lowerCamelCase_								=  tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]					)
   lowerCamelCase_								=  get_tf_activation("gelu"					)
   lowerCamelCase_								=  get_tf_activation("gelu_10"					)

   lowerCamelCase_								=  gelu(UpperCamelCase					)
   lowerCamelCase_								=  geluaa(UpperCamelCase					)

   lowerCamelCase_								=  tf.where(y_gelu_aa < 10.0      ,  1.0      ,  0.0					)

   self.assertEqual(tf.math.reduce_max(UpperCamelCase					).numpy().item()      ,  10.0					)
   self.assertTrue(np.allclose(y_gelu * clipped_mask      ,  y_gelu_aa * clipped_mask					)					)







  def   snake_case		(   self					):







   """simple docstring"""
   get_tf_activation("gelu"					)
   get_tf_activation("gelu_10"					)
   get_tf_activation("gelu_fast"					)
   get_tf_activation("gelu_new"					)
   get_tf_activation("glu"					)
   get_tf_activation("mish"					)
   get_tf_activation("quick_gelu"					)
   get_tf_activation("relu"					)
   get_tf_activation("sigmoid"					)
   get_tf_activation("silu"					)
   get_tf_activation("swish"					)
   get_tf_activation("tanh"					)
   with self.assertRaises(UpperCamelCase					):
    get_tf_activation("bogus"					)
   with self.assertRaises(UpperCamelCase					):
    get_tf_activation(UpperCamelCase					)



