


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:				Any,A_:				str,A_:				Dict=13,A_:				Union[str, Any]=7,A_:				Dict=True,A_:				int=True,A_:				Union[str, Any]=True,A_:				str=True,A_:				Tuple=99,A_:				Optional[Any]=32,A_:				Optional[int]=5,A_:				List[str]=4,A_:				str=37,A_:				Dict="gelu",A_:				Dict=0.1,A_:				List[Any]=0.1,A_:				Optional[int]=512,A_:				List[Any]=16,A_:				Optional[int]=2,A_:				List[Any]=0.0_2,A_:				List[Any]=3,A_:				Optional[Any]=None,A_:				int=20,A_:				List[Any]=30,A_:				Optional[Any]=2,A_:				str=2,A_:				int=10,):






            '''simple docstring'''




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







       def 						snake_case_					(	self:				Optional[Any]			):






            '''simple docstring'''




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

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

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

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

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

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

            __UpperCamelCase				=	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:				Dict			):






            '''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:				Dict,A_:				Tuple,A_:				str,A_:				Union[str, Any],A_:				Union[str, Any],A_:				Any,A_:				Tuple,A_:				List[str],A_:				List[str],):






            '''simple docstring'''




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






            '''simple docstring'''




            __UpperCamelCase				=	self.num_labels
            __UpperCamelCase				=	MarkupLMForSequenceClassification(_A			)
            model.to(_A			)
            model.eval()
            __UpperCamelCase				=	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 						snake_case_					(	self:				Optional[int],A_:				Dict,A_:				Optional[int],A_:				int,A_:				str,A_:				List[str],A_:				Union[str, Any],A_:				Any,A_:				Tuple,):






            '''simple docstring'''




            __UpperCamelCase				=	self.num_labels
            __UpperCamelCase				=	MarkupLMForTokenClassification(config=_A			)
            model.to(_A			)
            model.eval()
            __UpperCamelCase				=	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 						snake_case_					(	self:				Optional[int],A_:				Dict,A_:				int,A_:				Optional[int],A_:				Dict,A_:				Tuple,A_:				List[str],A_:				int,A_:				List[Any],):






            '''simple docstring'''




            __UpperCamelCase				=	MarkupLMForQuestionAnswering(config=_A			)
            model.to(_A			)
            model.eval()
            __UpperCamelCase				=	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 						snake_case_					(	self:				Optional[int]			):






            '''simple docstring'''




            __UpperCamelCase				=	self.prepare_config_and_inputs()
            (
                __UpperCamelCase   
            )				=	config_and_inputs
            __UpperCamelCase				=	{
                "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		):
       _lowercase    							=      (
           (
               MarkupLMModel,
               MarkupLMForSequenceClassification,
               MarkupLMForTokenClassification,
               MarkupLMForQuestionAnswering,
           )
           if is_torch_available()
           else None
       )
       _lowercase    							=      (
           {
               """feature-extraction""": MarkupLMModel,
               """question-answering""": MarkupLMForQuestionAnswering,
               """text-classification""": MarkupLMForSequenceClassification,
               """token-classification""": MarkupLMForTokenClassification,
               """zero-shot""": MarkupLMForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )







       def 						snake_case_					(	self:				int,A_:				str,A_:				List[str],A_:				Dict,A_:				Optional[int],A_:				List[str]			):






            '''simple docstring'''




            return True







       def 						snake_case_					(	self:				int			):






            '''simple docstring'''




            __UpperCamelCase				=	MarkupLMModelTester(self			)
            __UpperCamelCase				=	ConfigTester(self,config_class=_A,hidden_size=37			)







       def 						snake_case_					(	self:				Dict			):






            '''simple docstring'''




            self.config_tester.run_common_tests()







       def 						snake_case_					(	self:				List[str]			):






            '''simple docstring'''




            __UpperCamelCase				=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_A			)







       def 						snake_case_					(	self:				Dict			):






            '''simple docstring'''




            __UpperCamelCase				=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*_A			)







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            __UpperCamelCase				=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*_A			)







       def 						snake_case_					(	self:				Optional[Any]			):






            '''simple docstring'''




            __UpperCamelCase				=	self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*_A			)






def 						_A						(						)			->				Any:


     """simple docstring"""


     __UpperCamelCase				=	"\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 					__lowerCamelCase	(unittest.TestCase		):







       @cached_property
       def 						snake_case_					(	self:				Any			):






            '''simple docstring'''




            __UpperCamelCase				=	MarkupLMFeatureExtractor()
            __UpperCamelCase				=	MarkupLMTokenizer.from_pretrained('microsoft/markuplm-base'			)

            return MarkupLMProcessor(_A,_A			)







       @slow
       def 						snake_case_					(	self:				Union[str, Any]			):






            '''simple docstring'''




            __UpperCamelCase				=	MarkupLMModel.from_pretrained('microsoft/markuplm-base'			).to(_A			)

            __UpperCamelCase				=	self.default_processor

            __UpperCamelCase				=	processor(prepare_html_string(),return_tensors='pt'			)
            __UpperCamelCase				=	inputs.to(_A			)

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

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

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






import inspect
import json
import os
import tempfile
import unittest

from transformers.models.speech_to_text_a import SpeechaTextaTokenizer
from transformers.models.speech_to_text_a.tokenization_speech_to_text_a import VOCAB_FILES_NAMES

from ...test_tokenization_common import TokenizerTesterMixin



class 					__lowerCamelCase	(_a							,     unittest.TestCase		):
       _lowercase    							=      SpeechaTextaTokenizer
       _lowercase    							=      False







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            super().setUp()

            __UpperCamelCase				=	'<s> <pad> </s> <unk> here@@ a couple of@@ words for the he@@ re@@ vocab'.split(' '			)
            __UpperCamelCase				=	['he re</w> 123', 'here a 1456']
            __UpperCamelCase				=	dict(zip(A_,range(len(A_			)			)			)			)

            __UpperCamelCase				=	{'pad_token': '<pad>', 'unk_token': '<unk>', 'bos_token': '<s>', 'eos_token': '</s>'}

            __UpperCamelCase				=	tempfile.mkdtemp()
            __UpperCamelCase				=	os.path.join(self.tmpdirname,VOCAB_FILES_NAMES['vocab_file']			)
            __UpperCamelCase				=	os.path.join(self.tmpdirname,VOCAB_FILES_NAMES['merges_file']			)

            with open(self.vocab_file,'w',encoding='utf-8'			) as fp:
                 fp.write(json.dumps(A_			) + '\n'			)

            with open(self.merges_file,'w'			) as fp:
                 fp.write('\n'.join(A_			)			)







       def 						snake_case_					(	self:				Optional[int]			):






            '''simple docstring'''




            __UpperCamelCase				=	list(self.get_tokenizer().get_vocab().keys()			)

            self.assertEqual(vocab_keys[0],'<s>'			)
            self.assertEqual(vocab_keys[1],'<pad>'			)
            self.assertEqual(vocab_keys[-1],'vocab'			)
            self.assertEqual(len(A_			),14			)







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            self.assertEqual(self.get_tokenizer().vocab_size,14			)







       def 						snake_case_					(	self:				Dict			):






            '''simple docstring'''




            __UpperCamelCase				=	SpeechaTextaTokenizer.from_pretrained(self.tmpdirname			)

            # make sure @@ is correctly concatenated
            __UpperCamelCase				=	[4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
            __UpperCamelCase				=	tokenizer.decode(A_			)

            self.assertTrue(output_string == 'herecouple words ofthe'			)







       def 						snake_case_					(	self:				int			):






            '''simple docstring'''




            __UpperCamelCase				=	SpeechaTextaTokenizer.from_pretrained(self.tmpdirname			)

            with tempfile.TemporaryDirectory() as tmp_dirname:
                 tokenizer.save_pretrained(A_			)
                 os.remove(os.path.join(A_,'merges.txt'			)			)

                 # load tokenizer without merges file should not throw an error
                 __UpperCamelCase				=	SpeechaTextaTokenizer.from_pretrained(A_			)

            with tempfile.TemporaryDirectory() as tmp_dirname:
                 # save tokenizer and load again
                 tokenizer.save_pretrained(A_			)
                 __UpperCamelCase				=	SpeechaTextaTokenizer.from_pretrained(A_			)

            self.assertIsNotNone(A_			)







       def 						snake_case_					(	self:				str			):






            '''simple docstring'''




            if not self.test_slow_tokenizer:
                 return

            __UpperCamelCase				=	inspect.signature(self.tokenizer_class.__init__			)
            __UpperCamelCase				=	self.get_tokenizer()

            for parameter_name, parameter in signature.parameters.items():
                 if parameter.default != inspect.Parameter.empty and parameter_name != "merges_file":
                      self.assertIn(A_,tokenizer.init_kwargs			)


