



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


						def __init__( self						:  Union[str, Any]      ,       _a						:  Tuple      ,       _a						:  Optional[Any]=1_3      ,       _a						:  Optional[int]=7      ,       _a						:  List[str]=True      ,       _a						:  int=True      ,       _a						:  int=True      ,       _a						:  Tuple=True      ,       _a						:  Tuple=9_9      ,       _a						:  Tuple=3_2      ,       _a						:  Any=5      ,       _a						:  Optional[int]=4      ,       _a						:  Optional[Any]=3_7      ,       _a						:  List[Any]="gelu"      ,       _a						:  Any=0.1      ,       _a						:  List[Any]=0.1      ,       _a						:  Tuple=5_1_2      ,       _a						:  int=1_6      ,       _a						:  int=2      ,       _a						:  List[Any]=0.0_2      ,       _a						:  Optional[int]=3      ,       _a						:  List[str]=None      ,       _a						:  List[Any]=2_0      ,       _a						:  Dict=3_0      ,       _a						:  Dict=2      ,       _a						:  Optional[int]=2      ,       _a						:  str=1_0      ,       ):
													a__:       Union[str, Any]											=parent
													a__:       Optional[Any]											=batch_size
													a__:       Dict											=seq_length
													a__:       int											=is_training
													a__:       Optional[Any]											=use_input_mask
													a__:       Dict											=use_token_type_ids
													a__:       Dict											=use_labels
													a__:       List[Any]											=vocab_size
													a__:       str											=hidden_size
													a__:       Tuple											=num_hidden_layers
													a__:       Union[str, Any]											=num_attention_heads
													a__:       Optional[int]											=intermediate_size
													a__:       Tuple											=hidden_act
													a__:       List[str]											=hidden_dropout_prob
													a__:       Optional[Any]											=attention_probs_dropout_prob
													a__:       Any											=max_position_embeddings
													a__:       Dict											=type_vocab_size
													a__:       List[str]											=type_sequence_label_size
													a__:       Tuple											=initializer_range
													a__:       Tuple											=num_labels
													a__:       Dict											=scope
													a__:       int											=max_xpath_tag_unit_embeddings
													a__:       Optional[int]											=max_xpath_subs_unit_embeddings
													a__:       Optional[Any]											=tag_pad_id
													a__:       Union[str, Any]											=subs_pad_id
													a__:       Dict											=max_depth


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Tuple											=ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size			)

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

													a__:       Tuple											=ids_tensor(
													    [self.batch_size, self.seq_length, self.max_depth]      ,       self.max_xpath_subs_unit_embeddings			)

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

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

													a__:       Optional[int]											=None
													a__:       Dict											=None
													if self.use_labels:
																				a__:       Optional[int]											=ids_tensor([self.batch_size]      ,       self.type_sequence_label_size			)
																				a__:       int											=ids_tensor([self.batch_size, self.seq_length]      ,       self.num_labels			)

													a__:       Optional[Any]											=self.get_config()

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


						def   _lowerCamelCase			( self						:  Dict			):
													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   _lowerCamelCase			( self						:  Dict      ,       _a						:  List[str]      ,       _a						:  Union[str, Any]      ,       _a						:  int      ,       _a						:  str      ,       _a						:  int      ,       _a						:  int      ,       _a						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       ):
													a__:       Optional[int]											=MarkupLMModel(config=_a			)
													model.to(_a			)
													model.eval()
													print("Configs:"      ,       model.config.tag_pad_id      ,       model.config.subs_pad_id			)
													a__:       List[str]											=model(_a      ,       attention_mask=_a      ,       token_type_ids=_a			)
													a__:       List[str]											=model(_a      ,       token_type_ids=_a			)
													a__:       List[Any]											=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   _lowerCamelCase			( self						:  int      ,       _a						:  Tuple      ,       _a						:  int      ,       _a						:  Dict      ,       _a						:  Tuple      ,       _a						:  int      ,       _a						:  Any      ,       _a						:  Dict      ,       _a						:  List[Any]      ,       ):
													a__:       List[str]											=self.num_labels
													a__:       Optional[int]											=MarkupLMForSequenceClassification(_a			)
													model.to(_a			)
													model.eval()
													a__:       Union[str, Any]											=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   _lowerCamelCase			( self						:  Tuple      ,       _a						:  int      ,       _a						:  Any      ,       _a						:  int      ,       _a						:  Optional[Any]      ,       _a						:  Optional[int]      ,       _a						:  Any      ,       _a						:  Optional[int]      ,       _a						:  Dict      ,       ):
													a__:       Union[str, Any]											=self.num_labels
													a__:       Optional[int]											=MarkupLMForTokenClassification(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       List[Any]											=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   _lowerCamelCase			( self						:  Any      ,       _a						:  Tuple      ,       _a						:  Any      ,       _a						:  Any      ,       _a						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  int      ,       _a						:  Any      ,       _a						:  Optional[Any]      ,       ):
													a__:       Any											=MarkupLMForQuestionAnswering(config=_a			)
													model.to(_a			)
													model.eval()
													a__:       Dict											=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   _lowerCamelCase			( self						:  Optional[Any]			):
													a__:       Optional[Any]											=self.prepare_config_and_inputs()
													(
													    a__	
													):       List[str]											=config_and_inputs
													a__:       Optional[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 	lowerCamelCase__ (		A_				,					A_				,					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   _lowerCamelCase			( self						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  str      ,       _a						:  Union[str, Any]      ,       _a						:  Optional[Any]      ,       _a						:  Tuple			):
													return True


						def   _lowerCamelCase			( self						:  Union[str, Any]			):
													a__:       Optional[int]											=MarkupLMModelTester(self			)
													a__:       str											=ConfigTester(self      ,       config_class=_a      ,       hidden_size=3_7			)


						def   _lowerCamelCase			( self						:  Optional[int]			):
													self.config_tester.run_common_tests()


						def   _lowerCamelCase			( self						:  Optional[int]			):
													a__:       List[Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_a			)


						def   _lowerCamelCase			( self						:  Optional[int]			):
													a__:       Optional[int]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_a			)


						def   _lowerCamelCase			( self						:  List[Any]			):
													a__:       Dict											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*_a			)







						def   _lowerCamelCase			( self						:  Optional[Any]			):
													a__:       List[Any]											=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_a			)





def 						__lowerCamelCase       (      ):
							a__:       List[Any]											='''
    <!DOCTYPE html>
    <html>
    <head>
    <title>Page Title</title>
    </head>
    <body>

    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>

    </body>
    </html>
    '''

							return html_string







@require_torch
class 	lowerCamelCase__ (		unittest.TestCase   ):


						@cached_property
						def   _lowerCamelCase			( self						:  str			):
													a__:       Optional[Any]											=MarkupLMFeatureExtractor()
													a__:       str											=MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"			)

													return MarkupLMProcessor(_a      ,       _a			)







						@slow
						def   _lowerCamelCase			( self						:  int			):
													a__:       Tuple											=MarkupLMModel.from_pretrained("microsoft/markuplm-base"			).to(_a			)

													a__:       List[Any]											=self.default_processor

													a__:       int											=processor(prepare_html_string()      ,       return_tensors="pt"			)
													a__:       Optional[int]											=inputs.to(_a			)

													# forward pass
													with torch.no_grad():
																				a__:       Union[str, Any]											=model(**_a			)

													# verify the last hidden states
													a__:       Optional[int]											=torch.Size([1, 1_4, 7_6_8]			)
													self.assertEqual(outputs.last_hidden_state.shape      ,       _a			)

													a__:       int											=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 ...utils import is_transformers_available
from .pipeline_latent_diffusion_superresolution import LDMSuperResolutionPipeline


if is_transformers_available():
		from .pipeline_latent_diffusion import LDMBertModel, LDMTextToImagePipeline





