






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			:





							"""simple docstring"""






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







													_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 	UpperCamelCase   (							self   ):
													"""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 	UpperCamelCase   (							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 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   ):
													"""simple docstring"""







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







													_UpperCAmelCase     =      self.num_labels
													_UpperCAmelCase     =      MarkupLMForSequenceClassification(__lowerCAmelCase   )
													model.to(__lowerCAmelCase   )
													model.eval()
													_UpperCAmelCase     =      model(
													    __lowerCAmelCase	,   xpath_tags_seq=__lowerCAmelCase	,   xpath_subs_seq=__lowerCAmelCase	,   attention_mask=__lowerCAmelCase	,   token_type_ids=__lowerCAmelCase	,   labels=__lowerCAmelCase	,   )
													self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.num_labels)   )

							def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   ):
													"""simple docstring"""







													_UpperCAmelCase     =      self.num_labels
													_UpperCAmelCase     =      MarkupLMForTokenClassification(config=__lowerCAmelCase   )
													model.to(__lowerCAmelCase   )
													model.eval()
													_UpperCAmelCase     =      model(
													    __lowerCAmelCase	,   xpath_tags_seq=__lowerCAmelCase	,   xpath_subs_seq=__lowerCAmelCase	,   attention_mask=__lowerCAmelCase	,   token_type_ids=__lowerCAmelCase	,   labels=__lowerCAmelCase	,   )
													self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.num_labels)   )

							def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   ):
													"""simple docstring"""







													_UpperCAmelCase     =      MarkupLMForQuestionAnswering(config=__lowerCAmelCase   )
													model.to(__lowerCAmelCase   )
													model.eval()
													_UpperCAmelCase     =      model(
													    __lowerCAmelCase	,   xpath_tags_seq=__lowerCAmelCase	,   xpath_subs_seq=__lowerCAmelCase	,   attention_mask=__lowerCAmelCase	,   token_type_ids=__lowerCAmelCase	,   start_positions=__lowerCAmelCase	,   end_positions=__lowerCAmelCase	,   )
													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 	UpperCamelCase   (							self   ):
													"""simple docstring"""







													_UpperCAmelCase     =      self.prepare_config_and_inputs()
													(
													    (
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _UpperCAmelCase					
													) ,			(
													    _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			(				lowerCAmelCase__					,	lowerCAmelCase__					,	unittest.TestCase):





							"""simple docstring"""






							UpperCamelCase__								=				(
							    (
							        MarkupLMModel,
							        MarkupLMForSequenceClassification,
							        MarkupLMForTokenClassification,
							        MarkupLMForQuestionAnswering,
							    )
							    if is_torch_available()
							    else None
							)
							UpperCamelCase__								=				(
							    {
							        '''feature-extraction''': MarkupLMModel,
							        '''question-answering''': MarkupLMForQuestionAnswering,
							        '''text-classification''': MarkupLMForSequenceClassification,
							        '''token-classification''': MarkupLMForTokenClassification,
							        '''zero-shot''': MarkupLMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)

							def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase   ):
													"""simple docstring"""







													return True

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







													_UpperCAmelCase     =      MarkupLMModelTester(self   )
													_UpperCAmelCase     =      ConfigTester(self	,   config_class=__lowerCAmelCase	,   hidden_size=37   )

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







													self.config_tester.run_common_tests()

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







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

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







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

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







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

							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







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



def __A   (				)->  Tuple:





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





							"""simple docstring"""






							@cached_property
							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







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

													return MarkupLMProcessor(__lowerCAmelCase	,   __lowerCAmelCase   )

							@slow
							def 	UpperCamelCase   (							self   ):
													"""simple docstring"""







													_UpperCAmelCase     =      MarkupLMModel.from_pretrained('microsoft/markuplm-base'   ).to(__lowerCAmelCase   )

													_UpperCAmelCase     =      self.default_processor

													_UpperCAmelCase     =      processor(prepare_html_string()	,   return_tensors='pt'   )
													_UpperCAmelCase     =      inputs.to(__lowerCAmelCase   )

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

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

													_UpperCAmelCase     =      torch.tensor(
													    [[0.06_75, -0.00_52, 0.50_01], [-0.22_81, 0.08_02, 0.21_92], [-0.05_83, -0.33_11, 0.11_85]]   ).to(__lowerCAmelCase   )

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








import unittest

import pytest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    MBartaaTokenizerFast,
    MBartConfig,
    MBartForConditionalGeneration,
    TranslationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch, slow

from .test_pipelines_common import ANY





@is_pipeline_test
class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=				MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
	UpperCamelCase__								=				TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							if isinstance(model.config	,   UpperCAmelCase   ):
													_UpperCAmelCase ,			_UpperCAmelCase     =      list(tokenizer.lang_code_to_id.keys()   )[:2]
													_UpperCAmelCase     =      TranslationPipeline(model=UpperCAmelCase	,   tokenizer=UpperCAmelCase	,   src_lang=UpperCAmelCase	,   tgt_lang=UpperCAmelCase   )
							else:
													_UpperCAmelCase     =      TranslationPipeline(model=UpperCAmelCase	,   tokenizer=UpperCAmelCase   )
							return translator, ["Some string", "Some other text"]

	def 	UpperCamelCase   (							self	,   UpperCAmelCase	,   UpperCAmelCase   ):
							"""simple docstring"""







							_UpperCAmelCase     =      translator('Some string'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}]   )

							_UpperCAmelCase     =      translator(['Some string']   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}]   )

							_UpperCAmelCase     =      translator(['Some string', 'other string']   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': ANY(UpperCAmelCase   )}, {'translation_text': ANY(UpperCAmelCase   )}]   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_ro'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='pt'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
							                ' Beide Beide'
							            )
							        }
							    ]	,   )

	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_ro'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='tf'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide Beide'
							                ' Beide Beide'
							            )
							        }
							    ]	,   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_de'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='pt'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
							                ' urine urine urine urine urine urine urine'
							            )
							        }
							    ]	,   )

	@require_tf
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      pipeline('translation_en_to_de'	,   model='patrickvonplaten/t5-tiny-random'	,   framework='tf'   )
							_UpperCAmelCase     =      translator('This is a test string'	,   max_length=20   )
							self.assertEqual(
							    UpperCAmelCase	,   [
							        {
							            'translation_text': (
							                'monoton monoton monoton monoton monoton monoton monoton monoton monoton monoton urine urine'
							                ' urine urine urine urine urine urine urine'
							            )
							        }
							    ]	,   )





class __lowerCamelCase			(				unittest.TestCase):





	"""simple docstring"""






	@require_torch
	@slow
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							with self.assertRaises(UpperCAmelCase   ):
													pipeline(task='translation_cn_to_ar'   )

							# but we do for this one
							_UpperCAmelCase     =      pipeline(task='translation_en_to_de'   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'en'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'de'   )

	@require_torch
	@slow
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      MBartForConditionalGeneration.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'   )
							_UpperCAmelCase     =      MBartaaTokenizerFast.from_pretrained('facebook/mbart-large-50-many-to-many-mmt'   )

							_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase	,   tokenizer=UpperCAmelCase   )
							# Missing src_lang, tgt_lang
							with self.assertRaises(UpperCAmelCase   ):
													translator('This is a test'   )

							_UpperCAmelCase     =      translator('This is a test'	,   src_lang='en_XX'	,   tgt_lang='ar_AR'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'هذا إختبار'}]   )

							_UpperCAmelCase     =      translator('This is a test'	,   src_lang='en_XX'	,   tgt_lang='hi_IN'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'यह एक परीक्षण है'}]   )

							# src_lang, tgt_lang can be defined at pipeline call time
							_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase	,   tokenizer=UpperCAmelCase	,   src_lang='en_XX'	,   tgt_lang='ar_AR'   )
							_UpperCAmelCase     =      translator('This is a test'   )
							self.assertEqual(UpperCAmelCase	,   [{'translation_text': 'هذا إختبار'}]   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      'patrickvonplaten/t5-tiny-random'
							_UpperCAmelCase     =      pipeline(task='translation_cn_to_ar'	,   model=UpperCAmelCase   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'cn'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'ar'   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							_UpperCAmelCase     =      'patrickvonplaten/t5-tiny-random'
							with pytest.warns(UpperCAmelCase	,   match=R'.*translation_en_to_de.*'   ):
													_UpperCAmelCase     =      pipeline(task='translation'	,   model=UpperCAmelCase   )
							self.assertEqual(translator.task	,   'translation_en_to_de'   )
							self.assertEqual(translator._preprocess_params['src_lang']	,   'en'   )
							self.assertEqual(translator._preprocess_params['tgt_lang']	,   'de'   )

	@require_torch
	def 	UpperCamelCase   (							self   ):
							"""simple docstring"""







							with self.assertRaises(UpperCAmelCase   ):
													pipeline(task='translation'   )
