



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



			def __init__( self       :							List[str]      ,  _lowercase       :							str      ,  _lowercase       :							str=13      ,  _lowercase       :							List[str]=7      ,  _lowercase       :							Optional[int]=True      ,  _lowercase       :							Any=True      ,  _lowercase       :							Tuple=True      ,  _lowercase       :							Dict=True      ,  _lowercase       :							Optional[Any]=99      ,  _lowercase       :							List[str]=32      ,  _lowercase       :							List[Any]=5      ,  _lowercase       :							int=4      ,  _lowercase       :							Dict=37      ,  _lowercase       :							List[Any]="gelu"      ,  _lowercase       :							Any=0.1      ,  _lowercase       :							int=0.1      ,  _lowercase       :							List[str]=512      ,  _lowercase       :							Any=16      ,  _lowercase       :							Union[str, Any]=2      ,  _lowercase       :							List[str]=0.02      ,  _lowercase       :							Optional[Any]=3      ,  _lowercase       :							str=None      ,  _lowercase       :							int=20      ,  _lowercase       :							Optional[Any]=30      ,  _lowercase       :							int=2      ,  _lowercase       :							Dict=2      ,  _lowercase       :							Tuple=10      ,  )       ->				Union[str, Any]:
									snake_case      : Any							=   parent
									snake_case      : Optional[Any]							=   batch_size
									snake_case      : Optional[Any]							=   seq_length
									snake_case      : str							=   is_training
									snake_case      : List[Any]							=   use_input_mask
									snake_case      : Any							=   use_token_type_ids
									snake_case      : Optional[int]							=   use_labels
									snake_case      : Any							=   vocab_size
									snake_case      : Any							=   hidden_size
									snake_case      : Optional[Any]							=   num_hidden_layers
									snake_case      : Any							=   num_attention_heads
									snake_case      : List[Any]							=   intermediate_size
									snake_case      : str							=   hidden_act
									snake_case      : Any							=   hidden_dropout_prob
									snake_case      : List[str]							=   attention_probs_dropout_prob
									snake_case      : Union[str, Any]							=   max_position_embeddings
									snake_case      : Any							=   type_vocab_size
									snake_case      : Dict							=   type_sequence_label_size
									snake_case      : int							=   initializer_range
									snake_case      : str							=   num_labels
									snake_case      : str							=   scope
									snake_case      : List[str]							=   max_xpath_tag_unit_embeddings
									snake_case      : Optional[int]							=   max_xpath_subs_unit_embeddings
									snake_case      : Optional[int]							=   tag_pad_id
									snake_case      : Optional[Any]							=   subs_pad_id
									snake_case      : List[Any]							=   max_depth



			def 							__lowercase				( self       :							int		)       ->				Tuple:
									snake_case      : List[str]							=   ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size		)

									snake_case      : Any							=   ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]      ,  self.max_xpath_tag_unit_embeddings		)

									snake_case      : int							=   ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]      ,  self.max_xpath_subs_unit_embeddings		)

									snake_case      : int							=   None
									if self.use_input_mask:
															snake_case      : Optional[Any]							=   ids_tensor([self.batch_size, self.seq_length]      ,  vocab_size=2		)

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

									snake_case      : Optional[Any]							=   None
									snake_case      : str							=   None
									if self.use_labels:
															snake_case      : Optional[int]							=   ids_tensor([self.batch_size]      ,  self.type_sequence_label_size		)
															snake_case      : int							=   ids_tensor([self.batch_size, self.seq_length]      ,  self.num_labels		)

									snake_case      : Dict							=   self.get_config()

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



			def 							__lowercase				( self       :							Dict		)       ->				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 							__lowercase				( self       :							int      ,  _lowercase       :							Union[str, Any]      ,  _lowercase       :							Union[str, Any]      ,  _lowercase       :							List[Any]      ,  _lowercase       :							List[Any]      ,  _lowercase       :							Dict      ,  _lowercase       :							int      ,  _lowercase       :							Any      ,  _lowercase       :							int      ,  )       ->				Optional[Any]:
									snake_case      : Optional[Any]							=   MarkupLMModel(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									print("Configs:"      ,  model.config.tag_pad_id      ,  model.config.subs_pad_id		)
									snake_case      : Union[str, Any]							=   model(_lowercase      ,  attention_mask=_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : Tuple							=   model(_lowercase      ,  token_type_ids=_lowercase		)
									snake_case      : Dict							=   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 							__lowercase				( self       :							str      ,  _lowercase       :							Optional[int]      ,  _lowercase       :							Any      ,  _lowercase       :							Tuple      ,  _lowercase       :							List[Any]      ,  _lowercase       :							Dict      ,  _lowercase       :							Union[str, Any]      ,  _lowercase       :							Optional[int]      ,  _lowercase       :							List[Any]      ,  )       ->				Optional[Any]:
									snake_case      : List[str]							=   self.num_labels
									snake_case      : str							=   MarkupLMForSequenceClassification(_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : Optional[Any]							=   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 							__lowercase				( self       :							str      ,  _lowercase       :							Tuple      ,  _lowercase       :							List[Any]      ,  _lowercase       :							str      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							List[str]      ,  _lowercase       :							str      ,  _lowercase       :							List[Any]      ,  _lowercase       :							Dict      ,  )       ->				int:
									snake_case      : List[Any]							=   self.num_labels
									snake_case      : Optional[Any]							=   MarkupLMForTokenClassification(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : Optional[int]							=   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 							__lowercase				( self       :							Union[str, Any]      ,  _lowercase       :							int      ,  _lowercase       :							List[str]      ,  _lowercase       :							Optional[int]      ,  _lowercase       :							Optional[Any]      ,  _lowercase       :							List[Any]      ,  _lowercase       :							Any      ,  _lowercase       :							str      ,  _lowercase       :							int      ,  )       ->				int:
									snake_case      : Optional[int]							=   MarkupLMForQuestionAnswering(config=_lowercase		)
									model.to(_lowercase		)
									model.eval()
									snake_case      : Any							=   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 							__lowercase				( self       :							Dict		)       ->				Optional[int]:
									snake_case      : List[Any]							=   self.prepare_config_and_inputs()
									(
									    (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, (
									    snake_case
									)						, 
									)      : str							=   config_and_inputs
									snake_case      : Tuple							=   {
									    "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     (   SCREAMING_SNAKE_CASE__    ,  SCREAMING_SNAKE_CASE__    ,  unittest.TestCase):
			__magic_name__    				=    (
			    (
			        MarkupLMModel,
			        MarkupLMForSequenceClassification,
			        MarkupLMForTokenClassification,
			        MarkupLMForQuestionAnswering,
			    )
			    if is_torch_available()
			    else None
			)
			__magic_name__    				=    (
			    {
			        """feature-extraction""": MarkupLMModel,
			        """question-answering""": MarkupLMForQuestionAnswering,
			        """text-classification""": MarkupLMForSequenceClassification,
			        """token-classification""": MarkupLMForTokenClassification,
			        """zero-shot""": MarkupLMForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)



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



			def 							__lowercase				( self       :							Dict		)       ->				Optional[Any]:
									snake_case      : Optional[int]							=   MarkupLMModelTester(self		)
									snake_case      : int							=   ConfigTester(self      ,  config_class=_lowercase      ,  hidden_size=37		)



			def 							__lowercase				( self       :							List[Any]		)       ->				Dict:
									self.config_tester.run_common_tests()



			def 							__lowercase				( self       :							int		)       ->				List[str]:
									snake_case      : str							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase		)



			def 							__lowercase				( self       :							List[str]		)       ->				List[str]:
									snake_case      : Union[str, Any]							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_lowercase		)



			def 							__lowercase				( self       :							Tuple		)       ->				Optional[Any]:
									snake_case      : int							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_lowercase		)



			def 							__lowercase				( self       :							str		)       ->				Union[str, Any]:
									snake_case      : Dict							=   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_lowercase		)



def    __SCREAMING_SNAKE_CASE		( ):
						"""simple docstring"""






						snake_case      : Any							=   "\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):



			@cached_property
			def 							__lowercase				( self       :							Tuple		)       ->				Tuple:
									# TODO use from_pretrained here
									snake_case      : List[str]							=   MarkupLMFeatureExtractor()
									snake_case      : Optional[Any]							=   MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"		)

									return MarkupLMProcessor(_lowercase      ,  _lowercase		)



			@slow
			def 							__lowercase				( self       :							Optional[int]		)       ->				Any:
									snake_case      : Tuple							=   MarkupLMModel.from_pretrained("microsoft/markuplm-base"		).to(_lowercase		)

									snake_case      : Optional[int]							=   self.default_processor

									snake_case      : str							=   processor(prepare_html_string()      ,  return_tensors="pt"		)
									snake_case      : Optional[int]							=   inputs.to(_lowercase		)

									# forward pass
									with torch.no_grad():
															snake_case      : List[Any]							=   model(**_lowercase		)

									# verify the last hidden states
									snake_case      : Union[str, Any]							=   torch.Size([1, 14, 768]		)
									self.assertEqual(outputs.last_hidden_state.shape      ,  _lowercase		)

									snake_case      : Tuple							=   torch.tensor(
									    [[0.0675, -0.0052, 0.5001], [-0.2281, 0.0802, 0.2192], [-0.0583, -0.3311, 0.1185]]		).to(_lowercase		)

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








"""simple docstring"""







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import OwlViTImageProcessor

class 			_a     (   unittest.TestCase):



			def __init__( self       :							Optional[int]      ,  _lowercase       :							int      ,  _lowercase       :							Union[str, Any]=7      ,  _lowercase       :							Union[str, Any]=3      ,  _lowercase       :							int=18      ,  _lowercase       :							Any=30      ,  _lowercase       :							str=400      ,  _lowercase       :							str=True      ,  _lowercase       :							Dict=None      ,  _lowercase       :							int=True      ,  _lowercase       :							Union[str, Any]=None      ,  _lowercase       :							str=True      ,  _lowercase       :							Union[str, Any]=[0.48145466, 0.4578275, 0.40821073]      ,  _lowercase       :							str=[0.26862954, 0.26130258, 0.27577711]      ,  _lowercase       :							Optional[int]=True      ,  )       ->				Optional[int]:
									snake_case      : str							=   parent
									snake_case      : Any							=   batch_size
									snake_case      : Optional[int]							=   num_channels
									snake_case      : Any							=   image_size
									snake_case      : Optional[Any]							=   min_resolution
									snake_case      : int							=   max_resolution
									snake_case      : List[Any]							=   do_resize
									snake_case      : Dict							=   size if size is not None else {"height": 18, "width": 18}
									snake_case      : str							=   do_center_crop
									snake_case      : List[str]							=   crop_size if crop_size is not None else {"height": 18, "width": 18}
									snake_case      : str							=   do_normalize
									snake_case      : str							=   image_mean
									snake_case      : Optional[Any]							=   image_std
									snake_case      : str							=   do_convert_rgb



			def 							__lowercase				( self       :							Tuple		)       ->				Union[str, Any]:
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_convert_rgb": self.do_convert_rgb,
									}



@require_torch
@require_vision
class 			_a     (   SCREAMING_SNAKE_CASE__    ,  unittest.TestCase):
			__magic_name__    				=    OwlViTImageProcessor if is_vision_available() else None



			def 							__lowercase				( self       :							Any		)       ->				List[Any]:
									snake_case      : Dict							=   OwlViTImageProcessingTester(self		)



			@property
			def 							__lowercase				( self       :							Optional[int]		)       ->				List[Any]:
									return self.image_processor_tester.prepare_image_processor_dict()



			def 							__lowercase				( self       :							List[Any]		)       ->				Union[str, Any]:
									snake_case      : List[Any]							=   self.image_processing_class(**self.image_processor_dict		)
									self.assertTrue(hasattr(_lowercase      ,  "do_resize"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "size"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_center_crop"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "center_crop"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_normalize"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "image_mean"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "image_std"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_convert_rgb"		)		)



			def 							__lowercase				( self       :							Any		)       ->				Any:
									snake_case      : List[Any]							=   self.image_processing_class.from_dict(self.image_processor_dict		)
									self.assertEqual(image_processor.size      ,  {"height": 18, "width": 18}		)
									self.assertEqual(image_processor.crop_size      ,  {"height": 18, "width": 18}		)

									snake_case      : int							=   self.image_processing_class.from_dict(self.image_processor_dict      ,  size=42      ,  crop_size=84		)
									self.assertEqual(image_processor.size      ,  {"height": 42, "width": 42}		)
									self.assertEqual(image_processor.crop_size      ,  {"height": 84, "width": 84}		)



			def 							__lowercase				( self       :							Any		)       ->				Optional[int]:
									# Initialize image_processing
									snake_case      : List[Any]							=   self.image_processing_class(**self.image_processor_dict		)
									# create random PIL images
									snake_case      : Tuple							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase		)

									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  Image.Image		)

									# Test not batched input
									snake_case      : Dict							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : Optional[Any]							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



			def 							__lowercase				( self       :							Optional[Any]		)       ->				Dict:
									# Initialize image_processing
									snake_case      : Optional[int]							=   self.image_processing_class(**self.image_processor_dict		)
									# create random numpy tensors
									snake_case      : int							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase      ,  numpify=_lowercase		)
									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  np.ndarray		)

									# Test not batched input
									snake_case      : str							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : str							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



			def 							__lowercase				( self       :							List[Any]		)       ->				List[str]:
									# Initialize image_processing
									snake_case      : Any							=   self.image_processing_class(**self.image_processor_dict		)
									# create random PyTorch tensors
									snake_case      : Dict							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase      ,  torchify=_lowercase		)
									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  torch.Tensor		)

									# Test not batched input
									snake_case      : Union[str, Any]							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : Any							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



