






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



    '''simple docstring'''


    def __init__(      self   ,			__lowercase   ,			__lowercase=13   ,			__lowercase=7   ,			__lowercase=True   ,			__lowercase=True   ,			__lowercase=True   ,			__lowercase=True   ,			__lowercase=99   ,			__lowercase=32   ,			__lowercase=5   ,			__lowercase=4   ,			__lowercase=37   ,			__lowercase="gelu"   ,			__lowercase=0.1   ,			__lowercase=0.1   ,			__lowercase=512   ,			__lowercase=16   ,			__lowercase=2   ,			__lowercase=0.02   ,			__lowercase=3   ,			__lowercase=None   ,			__lowercase=20   ,			__lowercase=30   ,			__lowercase=2   ,			__lowercase=2   ,			__lowercase=10   ,			):
     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 		A__      (      self			):
     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 		A__      (      self			):
     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   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			):
     UpperCAmelCase__ 						=				MarkupLMModel(config=__lowercase			)
     model.to(__lowercase			)
     model.eval()
     print("""Configs:"""   ,			model.config.tag_pad_id   ,			model.config.subs_pad_id			)
     UpperCAmelCase__ 						=				model(__lowercase   ,			attention_mask=__lowercase   ,			token_type_ids=__lowercase			)
     UpperCAmelCase__ 						=				model(__lowercase   ,			token_type_ids=__lowercase			)
     UpperCAmelCase__ 						=				model(__lowercase			)
     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   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			):
     UpperCAmelCase__ 						=				self.num_labels
     UpperCAmelCase__ 						=				MarkupLMForSequenceClassification(__lowercase			)
     model.to(__lowercase			)
     model.eval()
     UpperCAmelCase__ 						=				model(
         __lowercase   ,			xpath_tags_seq=__lowercase   ,			xpath_subs_seq=__lowercase   ,			attention_mask=__lowercase   ,			token_type_ids=__lowercase   ,			labels=__lowercase   ,			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.num_labels)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			):
     UpperCAmelCase__ 						=				self.num_labels
     UpperCAmelCase__ 						=				MarkupLMForTokenClassification(config=__lowercase			)
     model.to(__lowercase			)
     model.eval()
     UpperCAmelCase__ 						=				model(
         __lowercase   ,			xpath_tags_seq=__lowercase   ,			xpath_subs_seq=__lowercase   ,			attention_mask=__lowercase   ,			token_type_ids=__lowercase   ,			labels=__lowercase   ,			)
     self.parent.assertEqual(result.logits.shape   ,			(self.batch_size, self.seq_length, self.num_labels)			)


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			):
     UpperCAmelCase__ 						=				MarkupLMForQuestionAnswering(config=__lowercase			)
     model.to(__lowercase			)
     model.eval()
     UpperCAmelCase__ 						=				model(
         __lowercase   ,			xpath_tags_seq=__lowercase   ,			xpath_subs_seq=__lowercase   ,			attention_mask=__lowercase   ,			token_type_ids=__lowercase   ,			start_positions=__lowercase   ,			end_positions=__lowercase   ,			)
     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			):
     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 	_UpperCamelCase ( __UpperCamelCase		,     __UpperCamelCase		,     unittest.TestCase							):



    '''simple docstring'''


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


    def 		A__      (      self   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase   ,			__lowercase			):
     # ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
     # (batch of pretokenized examples).
     return True


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				MarkupLMModelTester(self			)
     UpperCAmelCase__ 						=				ConfigTester(self   ,			config_class=__lowercase   ,			hidden_size=37			)


    def 		A__      (      self			):
     self.config_tester.run_common_tests()


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*__lowercase			)


    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*__lowercase			)






    def 		A__      (      self			):
     UpperCAmelCase__ 						=				self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*__lowercase			)




def       snake_case__		(   )    ->List[Any]:
 UpperCAmelCase__ 						=				"""
    <!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 	_UpperCamelCase ( unittest.TestCase							):



    '''simple docstring'''


    @cached_property
    def 		A__      (      self			):
     # TODO use from_pretrained here
     UpperCAmelCase__ 						=				MarkupLMFeatureExtractor()
     UpperCAmelCase__ 						=				MarkupLMTokenizer.from_pretrained("""microsoft/markuplm-base"""			)

     return MarkupLMProcessor(__lowercase   ,			__lowercase			)






    @slow
    def 		A__      (      self			):
     UpperCAmelCase__ 						=				MarkupLMModel.from_pretrained("""microsoft/markuplm-base"""			).to(__lowercase			)

     UpperCAmelCase__ 						=				self.default_processor

     UpperCAmelCase__ 						=				processor(prepare_html_string()   ,			return_tensors="""pt"""			)
     UpperCAmelCase__ 						=				inputs.to(__lowercase			)

     # forward pass
     with torch.no_grad():
      UpperCAmelCase__ 						=				model(**__lowercase			)

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

     UpperCAmelCase__ 						=				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(__lowercase			)

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











"""simple docstring"""



import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
 import torch

 from transformers import GPTaLMHeadModel


@require_torch
@require_sentencepiece
@require_tokenizers
class 	_UpperCamelCase ( unittest.TestCase							):



    '''simple docstring'''


    @slow
    @unittest.skip("""Model is not available."""			)
    def 		A__      (      self			):
     UpperCAmelCase__ 						=				"""nvidia/megatron-gpt2-345m/"""
     if "MYDIR" in os.environ:
      UpperCAmelCase__ 						=				os.path.join(os.environ["""MYDIR"""]   ,			__lowercase			)
     UpperCAmelCase__ 						=				GPTaLMHeadModel.from_pretrained(__lowercase			)
     model.to(__lowercase			)
     model.half()

     UpperCAmelCase__ 						=				torch.tensor(
         [[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]   ,			device=__lowercase   ,			dtype=torch.long   ,			)

     with torch.no_grad():
      UpperCAmelCase__ 						=				model(__lowercase			).logits

     UpperCAmelCase__ 						=				torch.Size((1, 9, 50257)			)
     self.assertEqual(output.shape   ,			__lowercase			)

     UpperCAmelCase__ 						=				torch.tensor(
         [
             4.9_414,
             -0.2_920,
             -1.2_148,
             -4.0_273,
             -0.5_161,
             -5.2_109,
             -1.2_412,
             -1.8_301,
             -1.7_734,
             -4.7_148,
             -0.2_317,
             -1.0_811,
             -2.1_777,
             0.4_141,
             -3.7_969,
             -4.0_586,
             -2.5_332,
             -3.3_809,
             4.3_867,
         ]   ,			device=__lowercase   ,			dtype=torch.half   ,			)

     for i in range(19			):
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				8 * i // 17, 2792 * i  # along the diagonal
      UpperCAmelCase__						,	UpperCAmelCase__ 						=				output[0, r, c], expected_diag[i]
      UpperCAmelCase__ 						=				F'''row={r} col={c} computed={computed} expected={expected}'''
      self.assertAlmostEqual(__lowercase   ,			__lowercase   ,			delta=1e-4   ,			msg=__lowercase			)



