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    :




					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.0_2       ,			__UpperCamelCase=3       ,			__UpperCamelCase=None       ,			__UpperCamelCase=20       ,			__UpperCamelCase=30       ,			__UpperCamelCase=2       ,			__UpperCamelCase=2       ,			__UpperCamelCase=10       ,			)	->    Any:



									'''simple docstring'''







									snake_case__   :      Dict   				= parent
									snake_case__   :      Optional[int]   				= batch_size
									snake_case__   :      str   				= seq_length
									snake_case__   :      str   				= is_training
									snake_case__   :      Dict   				= use_input_mask
									snake_case__   :      str   				= use_token_type_ids
									snake_case__   :      Optional[Any]   				= use_labels
									snake_case__   :      Union[str, Any]   				= vocab_size
									snake_case__   :      Optional[int]   				= hidden_size
									snake_case__   :      Dict   				= num_hidden_layers
									snake_case__   :      Union[str, Any]   				= num_attention_heads
									snake_case__   :      int   				= intermediate_size
									snake_case__   :      int   				= hidden_act
									snake_case__   :      List[Any]   				= hidden_dropout_prob
									snake_case__   :      Dict   				= attention_probs_dropout_prob
									snake_case__   :      Any   				= max_position_embeddings
									snake_case__   :      Tuple   				= type_vocab_size
									snake_case__   :      Dict   				= type_sequence_label_size
									snake_case__   :      Dict   				= initializer_range
									snake_case__   :      Dict   				= num_labels
									snake_case__   :      List[str]   				= scope
									snake_case__   :      Union[str, Any]   				= max_xpath_tag_unit_embeddings
									snake_case__   :      Any   				= max_xpath_subs_unit_embeddings
									snake_case__   :      Any   				= tag_pad_id
									snake_case__   :      Dict   				= subs_pad_id
									snake_case__   :      Optional[int]   				= max_depth




					def      __a     (  self						)	->    int:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= ids_tensor([self.batch_size, self.seq_length]       ,			self.vocab_size						)

									snake_case__   :      Dict   				= ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]       ,			self.max_xpath_tag_unit_embeddings						)

									snake_case__   :      List[Any]   				= ids_tensor(
									    [self.batch_size, self.seq_length, self.max_depth]       ,			self.max_xpath_subs_unit_embeddings						)

									snake_case__   :      Union[str, Any]   				= None
									if self.use_input_mask:
													snake_case__   :      Union[str, Any]   				= ids_tensor([self.batch_size, self.seq_length]       ,			vocab_size=2						)

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

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

									snake_case__   :      Tuple   				= self.get_config()

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




					def      __a     (  self						)	->    Tuple:



									'''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      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      List[Any]   				= MarkupLMModel(config=_A						)
									model.to(_A						)
									model.eval()
									print('Configs:'       ,			model.config.tag_pad_id       ,			model.config.subs_pad_id						)
									snake_case__   :      Union[str, Any]   				= model(_A       ,			attention_mask=_A       ,			token_type_ids=_A						)
									snake_case__   :      Optional[Any]   				= model(_A       ,			token_type_ids=_A						)
									snake_case__   :      Dict   				= model(_A						)
									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      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    List[str]:



									'''simple docstring'''







									snake_case__   :      Tuple   				= self.num_labels
									snake_case__   :      Tuple   				= MarkupLMForSequenceClassification(_A						)
									model.to(_A						)
									model.eval()
									snake_case__   :      Dict   				= model(
									    _A       ,			xpath_tags_seq=_A       ,			xpath_subs_seq=_A       ,			attention_mask=_A       ,			token_type_ids=_A       ,			labels=_A       ,			)
									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.num_labels)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Union[str, Any]   				= self.num_labels
									snake_case__   :      Optional[int]   				= MarkupLMForTokenClassification(config=_A						)
									model.to(_A						)
									model.eval()
									snake_case__   :      Optional[int]   				= model(
									    _A       ,			xpath_tags_seq=_A       ,			xpath_subs_seq=_A       ,			attention_mask=_A       ,			token_type_ids=_A       ,			labels=_A       ,			)
									self.parent.assertEqual(result.logits.shape       ,			(self.batch_size, self.seq_length, self.num_labels)						)




					def      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			)	->    Tuple:



									'''simple docstring'''







									snake_case__   :      str   				= MarkupLMForQuestionAnswering(config=_A						)
									model.to(_A						)
									model.eval()
									snake_case__   :      List[Any]   				= model(
									    _A       ,			xpath_tags_seq=_A       ,			xpath_subs_seq=_A       ,			attention_mask=_A       ,			token_type_ids=_A       ,			start_positions=_A       ,			end_positions=_A       ,			)
									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      __a     (  self						)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      str   				= self.prepare_config_and_inputs()
									(
									    snake_case__							
									)   :      Optional[int]   				= config_and_inputs
									snake_case__   :      List[Any]   				= {
									    "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    (     _lowerCamelCase       ,_lowerCamelCase       ,unittest.TestCase      ):
					__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      __a     (  self       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase       ,			__UpperCamelCase						)	->    List[Any]:



									'''simple docstring'''







									return True




					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      List[str]   				= MarkupLMModelTester(self						)
									snake_case__   :      Optional[int]   				= ConfigTester(self       ,			config_class=_A       ,			hidden_size=37						)




					def      __a     (  self						)	->    Optional[int]:



									'''simple docstring'''







									self.config_tester.run_common_tests()




					def      __a     (  self						)	->    int:



									'''simple docstring'''







									snake_case__   :      Optional[Any]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_A						)




					def      __a     (  self						)	->    Dict:



									'''simple docstring'''







									snake_case__   :      Optional[int]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_A						)




					def      __a     (  self						)	->    Union[str, Any]:



									'''simple docstring'''







									snake_case__   :      List[Any]   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_A						)




					def      __a     (  self						)	->    Optional[Any]:



									'''simple docstring'''







									snake_case__   :      str   				= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_A						)




def 					UpperCamelCase__		(							)  ->   Dict:
				snake_case__   :      Dict   				= "\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      ):




					@cached_property
					def      __a     (  self						)	->    List[Any]:



									'''simple docstring'''







									snake_case__   :      Optional[Any]   				= MarkupLMFeatureExtractor()
									snake_case__   :      Optional[Any]   				= MarkupLMTokenizer.from_pretrained('microsoft/markuplm-base'						)

									return MarkupLMProcessor(_A       ,			_A						)




					@slow
					def      __a     (  self						)	->    Tuple:



									'''simple docstring'''







									snake_case__   :      Dict   				= MarkupLMModel.from_pretrained('microsoft/markuplm-base'						).to(_A						)

									snake_case__   :      Tuple   				= self.default_processor

									snake_case__   :      Optional[Any]   				= processor(prepare_html_string()       ,			return_tensors='pt'						)
									snake_case__   :      List[Any]   				= inputs.to(_A						)

									# forward pass
									with torch.no_grad():
													snake_case__   :      Tuple   				= model(**_A						)

									# verify the last hidden states
									snake_case__   :      Tuple   				= torch.Size([1, 14, 768]						)
									self.assertEqual(outputs.last_hidden_state.shape       ,			_A						)

									snake_case__   :      Optional[Any]   				= torch.tensor(
									    [[0.0_6_7_5, -0.0_0_5_2, 0.5_0_0_1], [-0.2_2_8_1, 0.0_8_0_2, 0.2_1_9_2], [-0.0_5_8_3, -0.3_3_1_1, 0.1_1_8_5]]						).to(_A						)

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







from itertools import compress, repeat
from math import ceil, sqrt
def 						snake_case__							(       __lowercase							)		->		list[int]:







						"""simple docstring"""

						if num <= 2:
												return []
						if num == 3:
												return [2]

						# Odd sieve for numbers in range [3, num - 1]
						A__						:				Dict         =			bytearray(b"\x01"							) * ((num >> 1) - 1)

						for i in range(3				,  int(sqrt(__lowercase							)							) + 1				,  2							):
												if sieve[(i >> 1) - 1]:
																		A__						:				int         =			i**2
																		A__						:				List[str]         =			repeat(
																		    0				,  ceil((num - i_squared) / (i << 1)							)							)

						return [2] + list(compress(range(3				,  __lowercase				,  2							)				,  __lowercase							)							)


if __name__ == "__main__":
		import doctest

		doctest.testmod()