"""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,					_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'''



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


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				ids_tensor([self.batch_size, self.seq_length],					self.vocab_size	)

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

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

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

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

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

        lowercase__								=				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'''



        lowercase__								=				MarkupLMModel(config=_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        print("Configs:",					model.config.tag_pad_id,					model.config.subs_pad_id	)
        lowercase__								=				model(_UpperCAmelCase,					attention_mask=_UpperCAmelCase,					token_type_ids=_UpperCAmelCase	)
        lowercase__								=				model(_UpperCAmelCase,					token_type_ids=_UpperCAmelCase	)
        lowercase__								=				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'''



        lowercase__								=				self.num_labels
        lowercase__								=				MarkupLMForSequenceClassification(_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        lowercase__								=				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'''



        lowercase__								=				self.num_labels
        lowercase__								=				MarkupLMForTokenClassification(config=_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        lowercase__								=				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'''



        lowercase__								=				MarkupLMForQuestionAnswering(config=_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        lowercase__								=				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'''



        lowercase__								=				self.prepare_config_and_inputs()
        (
            (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    
        )								=				config_and_inputs
        lowercase__								=				{
            "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__     ( _a       ,       _a       ,       unittest.TestCase						):
   snake_case_				=   (
       (
           MarkupLMModel,
           MarkupLMForSequenceClassification,
           MarkupLMForTokenClassification,
           MarkupLMForQuestionAnswering,
       )
       if is_torch_available()
       else None
   )
   snake_case_				=   (
       {
           "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'''



        lowercase__								=				MarkupLMModelTester(self	)
        lowercase__								=				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'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase	)







def        __a (		):






     '''simple docstring'''



     lowercase__								=				"\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 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				MarkupLMFeatureExtractor()
        lowercase__								=				MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"	)

        return MarkupLMProcessor(_UpperCAmelCase,					_UpperCAmelCase	)


   @slow
   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				MarkupLMModel.from_pretrained("microsoft/markuplm-base"	).to(_UpperCAmelCase	)

        lowercase__								=				self.default_processor

        lowercase__								=				processor(prepare_html_string(),					return_tensors="pt"	)
        lowercase__								=				inputs.to(_UpperCAmelCase	)

        # forward pass
        with torch.no_grad():
             lowercase__								=				model(**_UpperCAmelCase	)

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

        lowercase__								=				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 copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


lowerCAmelCase_:       List[str]			     =       logging.get_logger(__name__)






class  a__     ( _a						):
   snake_case_				=   "speech-encoder-decoder"
   snake_case_				=   True


   def __init__(  self,					**_UpperCAmelCase	):
        '''simple docstring'''



        super().__init__(**_UpperCAmelCase	)
        if "encoder" not in kwargs or "decoder" not in kwargs:
             raise ValueError(
                 F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
                 F''' `decoder` sub-configurations are passed, but only {kwargs}'''	)

        lowercase__								=				kwargs.pop("encoder"	)
        lowercase__								=				encoder_config.pop("model_type"	)
        lowercase__								=				kwargs.pop("decoder"	)
        lowercase__								=				decoder_config.pop("model_type"	)

        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				True


   @classmethod
   def 						snake_case__						(  cls,					_UpperCAmelCase,					_UpperCAmelCase,					**_UpperCAmelCase	):
        '''simple docstring'''



        logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"	)
        lowercase__								=				True
        lowercase__								=				True

        return cls(encoder=encoder_config.to_dict(),					decoder=decoder_config.to_dict(),					**_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				copy.deepcopy(self.__dict__	)
        lowercase__								=				self.encoder.to_dict()
        lowercase__								=				self.decoder.to_dict()
        lowercase__								=				self.__class__.model_type
        return output




