


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig

from diffusers import AutoencoderKL, PaintByExamplePipeline, PNDMScheduler, UNetaDConditionModel
from diffusers.pipelines.paint_by_example import PaintByExampleImageEncoder
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class      _lowercase					(				lowercase__				,    unittest.TestCase):


       """simple docstring"""




       A__   		=     PaintByExamplePipeline
       A__   		=     IMAGE_GUIDED_IMAGE_INPAINTING_PARAMS
       A__   		=     IMAGE_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
       A__   		=     frozenset([])  # TO_DO: update the image_prams once refactored VaeImageProcessor.preprocess





       def 		lowerCAmelCase	(  self				:     Any			):


             '''simple docstring'''



             torch.manual_seed(0			)
             lowerCamelCase__						:							str       =    UNetaDConditionModel(
                 block_out_channels=(32, 64)   , layers_per_block=2   , sample_size=32   , in_channels=9   , out_channels=4   , down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")   , up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")   , cross_attention_dim=32   , )
             lowerCamelCase__						:							Any       =    PNDMScheduler(skip_prk_steps=__lowerCamelCase			)
             torch.manual_seed(0			)
             lowerCamelCase__						:							Any       =    AutoencoderKL(
                 block_out_channels=[32, 64]   , in_channels=3   , out_channels=3   , down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]   , up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]   , latent_channels=4   , )
             torch.manual_seed(0			)
             lowerCamelCase__						:							Tuple       =    CLIPVisionConfig(
                 hidden_size=32   , projection_dim=32   , intermediate_size=37   , layer_norm_eps=1E-0_5   , num_attention_heads=4   , num_hidden_layers=5   , image_size=32   , patch_size=4   , )
             lowerCamelCase__						:							str       =    PaintByExampleImageEncoder(__lowerCamelCase   , proj_size=32			)
             lowerCamelCase__						:							Any       =    CLIPImageProcessor(crop_size=32   , size=32			)

             lowerCamelCase__						:							Dict       =    {
                 "unet": unet,
                 "scheduler": scheduler,
                 "vae": vae,
                 "image_encoder": image_encoder,
                 "safety_checker": None,
                 "feature_extractor": feature_extractor,
             }
             return components





       def 		lowerCAmelCase	(  self				:     Dict   , __lowerCamelCase				:     Optional[int]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    np.array(image.convert("RGB"			)			)
             lowerCamelCase__						:							Any       =    image[None].transpose(0   , 3   , 1   , 2			)
             lowerCamelCase__						:							List[str]       =    torch.from_numpy(__lowerCamelCase			).to(dtype=torch.floataa			) / 1_2_7.5 - 1.0
             return image





       def 		lowerCAmelCase	(  self				:     Union[str, Any]   , __lowerCamelCase				:     int="cpu"   , __lowerCamelCase				:     List[Any]=0			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    floats_tensor((1, 3, 32, 32)   , rng=random.Random(__lowerCamelCase			)			).to(__lowerCamelCase			)
             lowerCamelCase__						:							int       =    image.cpu().permute(0   , 2   , 3   , 1			)[0]
             lowerCamelCase__						:							Dict       =    Image.fromarray(np.uinta(__lowerCamelCase			)			).convert("RGB"			).resize((64, 64)			)
             lowerCamelCase__						:							Union[str, Any]       =    Image.fromarray(np.uinta(image + 4			)			).convert("RGB"			).resize((64, 64)			)
             lowerCamelCase__						:							Tuple       =    Image.fromarray(np.uinta(__lowerCamelCase			)			).convert("RGB"			).resize((32, 32)			)

             if str(__lowerCamelCase			).startswith("mps"			):
                   lowerCamelCase__						:							List[str]       =    torch.manual_seed(__lowerCamelCase			)
             else:
                   lowerCamelCase__						:							int       =    torch.Generator(device=__lowerCamelCase			).manual_seed(__lowerCamelCase			)
             lowerCamelCase__						:							Any       =    {
                 "example_image": example_image,
                 "image": init_image,
                 "mask_image": mask_image,
                 "generator": generator,
                 "num_inference_steps": 2,
                 "guidance_scale": 6.0,
                 "output_type": "numpy",
             }
             return inputs





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.get_dummy_components()

             # make sure here that pndm scheduler skips prk
             lowerCamelCase__						:							Any       =    PaintByExamplePipeline(**__lowerCamelCase			)
             lowerCamelCase__						:							Optional[int]       =    pipe.to("cpu"			)
             pipe.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							int       =    self.get_dummy_inputs()
             lowerCamelCase__						:							Union[str, Any]       =    pipe(**__lowerCamelCase			)
             lowerCamelCase__						:							int       =    output.images

             lowerCamelCase__						:							Union[str, Any]       =    image[0, -3:, -3:, -1]

             assert image.shape == (1, 64, 64, 3)
             lowerCamelCase__						:							Optional[int]       =    np.array([0.4_6_8_6, 0.5_6_8_7, 0.4_0_0_7, 0.5_2_1_8, 0.5_7_4_1, 0.4_4_8_2, 0.4_9_4_0, 0.4_6_2_9, 0.4_5_0_3]			)

             assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    "cpu"
             lowerCamelCase__						:							Union[str, Any]       =    self.get_dummy_inputs()
             inputs.pop("mask_image"			)
             lowerCamelCase__						:							Optional[Any]       =    self.convert_to_pt(inputs.pop("image"			)			)
             lowerCamelCase__						:							Tuple       =    image.clamp(0   , 1			) / 2

             # make sure here that pndm scheduler skips prk
             lowerCamelCase__						:							Dict       =    PaintByExamplePipeline(**self.get_dummy_components()			)
             lowerCamelCase__						:							Any       =    pipe.to(__lowerCamelCase			)
             pipe.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							List[str]       =    pipe(image=__lowerCamelCase   , mask_image=mask_image[:, 0]   , **__lowerCamelCase			)
             lowerCamelCase__						:							int       =    output.images

             lowerCamelCase__						:							Dict       =    image.cpu().permute(0   , 2   , 3   , 1			)[0]
             lowerCamelCase__						:							Dict       =    mask_image.cpu().permute(0   , 2   , 3   , 1			)[0]

             lowerCamelCase__						:							int       =    Image.fromarray(np.uinta(__lowerCamelCase			)			).convert("RGB"			)
             lowerCamelCase__						:							Optional[Any]       =    Image.fromarray(np.uinta(__lowerCamelCase			)			).convert("RGB"			)

             lowerCamelCase__						:							Tuple       =    pipe(**self.get_dummy_inputs()			)
             lowerCamelCase__						:							Any       =    output.images

             assert out_a.shape == (1, 64, 64, 3)
             assert np.abs(out_a.flatten() - out_a.flatten()			).max() < 5E-2





       def 		lowerCAmelCase	(  self				:     Any			):


             '''simple docstring'''



             super().test_inference_batch_single_identical(expected_max_diff=3E-3			)







@slow
@require_torch_gpu
class      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase	(  self				:     Optional[int]			):


             '''simple docstring'''



             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()





       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/paint_by_example/dog_in_bucket.png"			)
             lowerCamelCase__						:							str       =    load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/paint_by_example/mask.png"			)
             lowerCamelCase__						:							Optional[Any]       =    load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/paint_by_example/panda.jpg"			)

             lowerCamelCase__						:							List[str]       =    PaintByExamplePipeline.from_pretrained("Fantasy-Studio/Paint-by-Example"			)
             lowerCamelCase__						:							Tuple       =    pipe.to(__lowerCamelCase			)
             pipe.set_progress_bar_config(disable=__lowerCamelCase			)

             lowerCamelCase__						:							Optional[Any]       =    torch.manual_seed(321			)
             lowerCamelCase__						:							Any       =    pipe(
                 image=__lowerCamelCase   , mask_image=__lowerCamelCase   , example_image=__lowerCamelCase   , generator=__lowerCamelCase   , guidance_scale=5.0   , num_inference_steps=50   , output_type="np"   , )

             lowerCamelCase__						:							Optional[int]       =    output.images

             lowerCamelCase__						:							Union[str, Any]       =    image[0, -3:, -3:, -1]

             assert image.shape == (1, 512, 512, 3)
             lowerCamelCase__						:							Union[str, Any]       =    np.array([0.4_8_3_4, 0.4_8_1_1, 0.4_8_7_4, 0.5_1_2_2, 0.5_0_8_1, 0.5_1_4_4, 0.5_2_9_1, 0.5_2_9_0, 0.5_3_7_4]			)

             assert np.abs(image_slice.flatten() - expected_slice			).max() < 1E-2






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


       """simple docstring"""




       def __init__(  self				:     Dict   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     Union[str, Any]=13   , __lowerCamelCase				:     Optional[Any]=7   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     str=True   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     List[str]=True   , __lowerCamelCase				:     Optional[int]=99   , __lowerCamelCase				:     Dict=32   , __lowerCamelCase				:     List[str]=5   , __lowerCamelCase				:     str=4   , __lowerCamelCase				:     str=37   , __lowerCamelCase				:     Any="gelu"   , __lowerCamelCase				:     Dict=0.1   , __lowerCamelCase				:     Optional[Any]=0.1   , __lowerCamelCase				:     List[str]=512   , __lowerCamelCase				:     str=16   , __lowerCamelCase				:     List[Any]=2   , __lowerCamelCase				:     int=0.0_2   , __lowerCamelCase				:     Any=3   , __lowerCamelCase				:     Optional[int]=None   , __lowerCamelCase				:     str=20   , __lowerCamelCase				:     Union[str, Any]=30   , __lowerCamelCase				:     str=2   , __lowerCamelCase				:     Dict=2   , __lowerCamelCase				:     str=10   , ):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[int]       =    parent
             lowerCamelCase__						:							Optional[Any]       =    batch_size
             lowerCamelCase__						:							Union[str, Any]       =    seq_length
             lowerCamelCase__						:							Union[str, Any]       =    is_training
             lowerCamelCase__						:							Tuple       =    use_input_mask
             lowerCamelCase__						:							Tuple       =    use_token_type_ids
             lowerCamelCase__						:							Union[str, Any]       =    use_labels
             lowerCamelCase__						:							str       =    vocab_size
             lowerCamelCase__						:							int       =    hidden_size
             lowerCamelCase__						:							List[Any]       =    num_hidden_layers
             lowerCamelCase__						:							List[Any]       =    num_attention_heads
             lowerCamelCase__						:							Optional[Any]       =    intermediate_size
             lowerCamelCase__						:							int       =    hidden_act
             lowerCamelCase__						:							Optional[Any]       =    hidden_dropout_prob
             lowerCamelCase__						:							Optional[Any]       =    attention_probs_dropout_prob
             lowerCamelCase__						:							Optional[int]       =    max_position_embeddings
             lowerCamelCase__						:							Optional[Any]       =    type_vocab_size
             lowerCamelCase__						:							Tuple       =    type_sequence_label_size
             lowerCamelCase__						:							List[Any]       =    initializer_range
             lowerCamelCase__						:							str       =    num_labels
             lowerCamelCase__						:							Dict       =    scope
             lowerCamelCase__						:							List[Any]       =    max_xpath_tag_unit_embeddings
             lowerCamelCase__						:							str       =    max_xpath_subs_unit_embeddings
             lowerCamelCase__						:							Optional[Any]       =    tag_pad_id
             lowerCamelCase__						:							Optional[Any]       =    subs_pad_id
             lowerCamelCase__						:							Optional[int]       =    max_depth





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size			)

             lowerCamelCase__						:							List[Any]       =    ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]   , self.max_xpath_tag_unit_embeddings			)

             lowerCamelCase__						:							int       =    ids_tensor(
                 [self.batch_size, self.seq_length, self.max_depth]   , self.max_xpath_subs_unit_embeddings			)

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

             lowerCamelCase__						:							int       =    None
             if self.use_token_type_ids:
                   lowerCamelCase__						:							Union[str, Any]       =    ids_tensor([self.batch_size, self.seq_length]   , self.type_vocab_size			)

             lowerCamelCase__						:							Any       =    None
             lowerCamelCase__						:							str       =    None
             if self.use_labels:
                   lowerCamelCase__						:							Dict       =    ids_tensor([self.batch_size]   , self.type_sequence_label_size			)
                   lowerCamelCase__						:							Optional[Any]       =    ids_tensor([self.batch_size, self.seq_length]   , self.num_labels			)

             lowerCamelCase__						:							int       =    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				:     int			):


             '''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 		lowerCAmelCase	(  self				:     Optional[Any]   , __lowerCamelCase				:     Union[str, Any]   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     str   , __lowerCamelCase				:     Tuple   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



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


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.num_labels
             lowerCamelCase__						:							str       =    MarkupLMForSequenceClassification(__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Optional[int]       =    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 		lowerCAmelCase	(  self				:     Any   , __lowerCamelCase				:     Optional[int]   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     List[str]   , __lowerCamelCase				:     List[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     str   , ):


             '''simple docstring'''



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


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMForQuestionAnswering(config=__lowerCamelCase			)
             model.to(__lowerCamelCase			)
             model.eval()
             lowerCamelCase__						:							Union[str, Any]       =    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 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.prepare_config_and_inputs()
             (
                 (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  (
                 lowerCamelCase__
             )       ,  
             )						:							Optional[int]       =    config_and_inputs
             lowerCamelCase__						:							List[str]       =    {
                 "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      _lowercase					(				lowercase__				,    lowercase__				,    unittest.TestCase):


       """simple docstring"""




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





       def 		lowerCAmelCase	(  self				:     int   , __lowerCamelCase				:     Optional[Any]   , __lowerCamelCase				:     int   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     Any   , __lowerCamelCase				:     List[str]			):


             '''simple docstring'''



             return True





       def 		lowerCAmelCase	(  self				:     List[str]			):


             '''simple docstring'''



             lowerCamelCase__						:							Any       =    MarkupLMModelTester(self			)
             lowerCamelCase__						:							Any       =    ConfigTester(self   , config_class=__lowerCamelCase   , hidden_size=37			)





       def 		lowerCAmelCase	(  self				:     Dict			):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     int			):


             '''simple docstring'''



             lowerCamelCase__						:							Dict       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Optional[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase			)





       def 		lowerCAmelCase	(  self				:     List[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							List[Any]       =    self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase			)



def  lowercase_						(						):

      """simple docstring"""




      lowerCamelCase__						:							int       =    "\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      _lowercase					(				unittest.TestCase):


       """simple docstring"""




       @cached_property
       def 		lowerCAmelCase	(  self				:     Tuple			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMFeatureExtractor()
             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMTokenizer.from_pretrained("microsoft/markuplm-base"			)

             return MarkupLMProcessor(__lowerCamelCase   , __lowerCamelCase			)





       @slow
       def 		lowerCAmelCase	(  self				:     Optional[Any]			):


             '''simple docstring'''



             lowerCamelCase__						:							Union[str, Any]       =    MarkupLMModel.from_pretrained("microsoft/markuplm-base"			).to(__lowerCamelCase			)

             lowerCamelCase__						:							Dict       =    self.default_processor

             lowerCamelCase__						:							Dict       =    processor(prepare_html_string()   , return_tensors="pt"			)
             lowerCamelCase__						:							Optional[Any]       =    inputs.to(__lowerCamelCase			)

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

             # verify the last hidden states
             lowerCamelCase__						:							Dict       =    torch.Size([1, 14, 768]			)
             self.assertEqual(outputs.last_hidden_state.shape   , __lowerCamelCase			)

             lowerCamelCase__						:							Dict       =    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(__lowerCamelCase			)

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


