"""simple docstring"""

import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import PriorTransformer, UnCLIPPipeline, UnCLIPScheduler, UNetaDConditionModel, UNetaDModel
from diffusers.pipelines.unclip.text_proj import UnCLIPTextProjModel
from diffusers.utils import load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()






class 		_lowercase (						lowercase__					,					unittest.TestCase      ):



						"""simple docstring"""




						lowercase__       =  UnCLIPPipeline
						lowercase__       =  TEXT_TO_IMAGE_PARAMS - {
						    '''negative_prompt''',
						    '''height''',
						    '''width''',
						    '''negative_prompt_embeds''',
						    '''guidance_scale''',
						    '''prompt_embeds''',
						    '''cross_attention_kwargs''',
						}
						lowercase__       =  TEXT_TO_IMAGE_BATCH_PARAMS
						lowercase__       =  [
						    '''generator''',
						    '''return_dict''',
						    '''prior_num_inference_steps''',
						    '''decoder_num_inference_steps''',
						    '''super_res_num_inference_steps''',
						]
						lowercase__       =  False





						@property
						def   UpperCAmelCase_ (				self							:    List[Any]				)				->				Dict:
													'''simple docstring'''



													return 32





						@property
						def   UpperCAmelCase_ (				self							:    List[Any]				)				->				str:
													'''simple docstring'''



													return 32





						@property
						def   UpperCAmelCase_ (				self							:    Optional[int]				)				->				Any:
													'''simple docstring'''



													return self.time_input_dim





						@property
						def   UpperCAmelCase_ (				self							:    List[Any]				)				->				int:
													'''simple docstring'''



													return self.time_input_dim * 4





						@property
						def   UpperCAmelCase_ (				self							:    int				)				->				Optional[Any]:
													'''simple docstring'''



													return 100





						@property
						def   UpperCAmelCase_ (				self							:    int				)				->				Optional[int]:
													'''simple docstring'''



													__UpperCamelCase							=CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''				)
													return tokenizer





						@property
						def   UpperCAmelCase_ (				self							:    int				)				->				Tuple:
													'''simple docstring'''



													torch.manual_seed(0				)
													__UpperCamelCase							=CLIPTextConfig(
													    bos_token_id=0     , eos_token_id=2     , hidden_size=self.text_embedder_hidden_size     , projection_dim=self.text_embedder_hidden_size     , intermediate_size=37     , layer_norm_eps=1E-05     , num_attention_heads=4     , num_hidden_layers=5     , pad_token_id=1     , vocab_size=1000     , )
													return CLIPTextModelWithProjection(__lowerCamelCase				)





						@property
						def   UpperCAmelCase_ (				self							:    Any				)				->				Any:
													'''simple docstring'''



													torch.manual_seed(0				)

													__UpperCamelCase							={
													    "num_attention_heads": 2,
													    "attention_head_dim": 12,
													    "embedding_dim": self.text_embedder_hidden_size,
													    "num_layers": 1,
													}

													__UpperCamelCase							=PriorTransformer(**__lowerCamelCase				)
													return model





						@property
						def   UpperCAmelCase_ (				self							:    int				)				->				Optional[int]:
													'''simple docstring'''



													torch.manual_seed(0				)

													__UpperCamelCase							={
													    "clip_embeddings_dim": self.text_embedder_hidden_size,
													    "time_embed_dim": self.time_embed_dim,
													    "cross_attention_dim": self.cross_attention_dim,
													}

													__UpperCamelCase							=UnCLIPTextProjModel(**__lowerCamelCase				)
													return model





						@property
						def   UpperCAmelCase_ (				self							:    Union[str, Any]				)				->				Dict:
													'''simple docstring'''



													torch.manual_seed(0				)

													__UpperCamelCase							={
													    "sample_size": 32,
													    # RGB in channels
													    "in_channels": 3,
													    # Out channels is double in channels because predicts mean and variance
													    "out_channels": 6,
													    "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
													    "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
													    "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
													    "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
													    "layers_per_block": 1,
													    "cross_attention_dim": self.cross_attention_dim,
													    "attention_head_dim": 4,
													    "resnet_time_scale_shift": "scale_shift",
													    "class_embed_type": "identity",
													}

													__UpperCamelCase							=UNetaDConditionModel(**__lowerCamelCase				)
													return model





						@property
						def   UpperCAmelCase_ (				self							:    Optional[Any]				)				->				Tuple:
													'''simple docstring'''



													return {
													    "sample_size": 64,
													    "layers_per_block": 1,
													    "down_block_types": ("ResnetDownsampleBlock2D", "ResnetDownsampleBlock2D"),
													    "up_block_types": ("ResnetUpsampleBlock2D", "ResnetUpsampleBlock2D"),
													    "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
													    "in_channels": 6,
													    "out_channels": 3,
													}





						@property
						def   UpperCAmelCase_ (				self							:    List[Any]				)				->				Dict:
													'''simple docstring'''



													torch.manual_seed(0				)

													__UpperCamelCase							=UNetaDModel(**self.dummy_super_res_kwargs				)
													return model





						@property
						def   UpperCAmelCase_ (				self							:    Tuple				)				->				Optional[int]:
													'''simple docstring'''



													torch.manual_seed(1				)

													__UpperCamelCase							=UNetaDModel(**self.dummy_super_res_kwargs				)
													return model





						def   UpperCAmelCase_ (				self							:    str				)				->				Optional[Any]:
													'''simple docstring'''



													__UpperCamelCase							=self.dummy_prior
													__UpperCamelCase							=self.dummy_decoder
													__UpperCamelCase							=self.dummy_text_proj
													__UpperCamelCase							=self.dummy_text_encoder
													__UpperCamelCase							=self.dummy_tokenizer
													__UpperCamelCase							=self.dummy_super_res_first
													__UpperCamelCase							=self.dummy_super_res_last

													__UpperCamelCase							=UnCLIPScheduler(
													    variance_type='''fixed_small_log'''     , prediction_type='''sample'''     , num_train_timesteps=1000     , clip_sample_range=5.0     , )

													__UpperCamelCase							=UnCLIPScheduler(
													    variance_type='''learned_range'''     , prediction_type='''epsilon'''     , num_train_timesteps=1000     , )

													__UpperCamelCase							=UnCLIPScheduler(
													    variance_type='''fixed_small_log'''     , prediction_type='''epsilon'''     , num_train_timesteps=1000     , )

													__UpperCamelCase							={
													    "prior": prior,
													    "decoder": decoder,
													    "text_proj": text_proj,
													    "text_encoder": text_encoder,
													    "tokenizer": tokenizer,
													    "super_res_first": super_res_first,
													    "super_res_last": super_res_last,
													    "prior_scheduler": prior_scheduler,
													    "decoder_scheduler": decoder_scheduler,
													    "super_res_scheduler": super_res_scheduler,
													}

													return components





						def   UpperCAmelCase_ (				self							:    Any     , UpperCamelCase__							:    Optional[Any]     , UpperCamelCase__							:    Optional[Any]=0				)				->				Union[str, Any]:
													'''simple docstring'''



													if str(__lowerCamelCase				).startswith('''mps'''				):
																				__UpperCamelCase							=torch.manual_seed(__lowerCamelCase				)
													else:
																				__UpperCamelCase							=torch.Generator(device=__lowerCamelCase				).manual_seed(__lowerCamelCase				)
													__UpperCamelCase							={
													    "prompt": "horse",
													    "generator": generator,
													    "prior_num_inference_steps": 2,
													    "decoder_num_inference_steps": 2,
													    "super_res_num_inference_steps": 2,
													    "output_type": "numpy",
													}
													return inputs





						def   UpperCAmelCase_ (				self							:    Optional[Any]				)				->				Tuple:
													'''simple docstring'''



													__UpperCamelCase							="cpu"

													__UpperCamelCase							=self.get_dummy_components()

													__UpperCamelCase							=self.pipeline_class(**__lowerCamelCase				)
													__UpperCamelCase							=pipe.to(__lowerCamelCase				)

													pipe.set_progress_bar_config(disable=__lowerCamelCase				)

													__UpperCamelCase							=pipe(**self.get_dummy_inputs(__lowerCamelCase				)				)
													__UpperCamelCase							=output.images

													__UpperCamelCase							=pipe(
													    **self.get_dummy_inputs(__lowerCamelCase				)     , return_dict=__lowerCamelCase     , )[0]

													__UpperCamelCase							=image[0, -3:, -3:, -1]
													__UpperCamelCase							=image_from_tuple[0, -3:, -3:, -1]

													assert image.shape == (1, 64, 64, 3)

													__UpperCamelCase							=np.array(
													    [
													        0.99_97,
													        0.99_88,
													        0.00_28,
													        0.99_97,
													        0.99_84,
													        0.99_65,
													        0.00_29,
													        0.99_86,
													        0.00_25,
													    ]				)

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





						def   UpperCAmelCase_ (				self							:    Dict				)				->				Optional[int]:
													'''simple docstring'''



													__UpperCamelCase							=torch.device('''cpu'''				)






													class 		_lowercase :



																			"""simple docstring"""




																			lowercase__       =  1






													__UpperCamelCase							=self.get_dummy_components()

													__UpperCamelCase							=self.pipeline_class(**__lowerCamelCase				)
													__UpperCamelCase							=pipe.to(__lowerCamelCase				)

													__UpperCamelCase							=components["prior"]
													__UpperCamelCase							=components["decoder"]
													__UpperCamelCase							=components["super_res_first"]
													__UpperCamelCase							=components["tokenizer"]
													__UpperCamelCase							=components["text_encoder"]

													__UpperCamelCase							=torch.Generator(device=__lowerCamelCase				).manual_seed(0				)
													__UpperCamelCase							=prior.dtype
													__UpperCamelCase							=1

													__UpperCamelCase							=(batch_size, prior.config.embedding_dim)
													__UpperCamelCase							=pipe.prepare_latents(
													    __lowerCamelCase     , dtype=__lowerCamelCase     , device=__lowerCamelCase     , generator=__lowerCamelCase     , latents=__lowerCamelCase     , scheduler=DummyScheduler()				)
													__UpperCamelCase							=(batch_size, decoder.config.in_channels, decoder.config.sample_size, decoder.config.sample_size)
													__UpperCamelCase							=pipe.prepare_latents(
													    __lowerCamelCase     , dtype=__lowerCamelCase     , device=__lowerCamelCase     , generator=__lowerCamelCase     , latents=__lowerCamelCase     , scheduler=DummyScheduler()				)

													__UpperCamelCase							=(
													    batch_size,
													    super_res_first.config.in_channels // 2,
													    super_res_first.config.sample_size,
													    super_res_first.config.sample_size,
													)
													__UpperCamelCase							=pipe.prepare_latents(
													    __lowerCamelCase     , dtype=__lowerCamelCase     , device=__lowerCamelCase     , generator=__lowerCamelCase     , latents=__lowerCamelCase     , scheduler=DummyScheduler()				)

													pipe.set_progress_bar_config(disable=__lowerCamelCase				)

													__UpperCamelCase							="this is a prompt example"

													__UpperCamelCase							=torch.Generator(device=__lowerCamelCase				).manual_seed(0				)
													__UpperCamelCase							=pipe(
													    [prompt]     , generator=__lowerCamelCase     , prior_num_inference_steps=2     , decoder_num_inference_steps=2     , super_res_num_inference_steps=2     , prior_latents=__lowerCamelCase     , decoder_latents=__lowerCamelCase     , super_res_latents=__lowerCamelCase     , output_type='''np'''     , )
													__UpperCamelCase							=output.images

													__UpperCamelCase							=tokenizer(
													    __lowerCamelCase     , padding='''max_length'''     , max_length=tokenizer.model_max_length     , return_tensors='''pt'''     , )
													__UpperCamelCase							=text_encoder(text_inputs.input_ids				)
													__UpperCamelCase							=text_inputs.attention_mask

													__UpperCamelCase							=torch.Generator(device=__lowerCamelCase				).manual_seed(0				)
													__UpperCamelCase							=pipe(
													    generator=__lowerCamelCase     , prior_num_inference_steps=2     , decoder_num_inference_steps=2     , super_res_num_inference_steps=2     , prior_latents=__lowerCamelCase     , decoder_latents=__lowerCamelCase     , super_res_latents=__lowerCamelCase     , text_model_output=__lowerCamelCase     , text_attention_mask=__lowerCamelCase     , output_type='''np'''     , )[0]

													# make sure passing text embeddings manually is identical
													assert np.abs(image - image_from_text				).max() < 1E-4





						@skip_mps
						def   UpperCAmelCase_ (				self							:    str				)				->				Dict:
													'''simple docstring'''



													__UpperCamelCase							=torch_device == "cpu"

													self._test_attention_slicing_forward_pass(test_max_difference=__lowerCamelCase     , expected_max_diff=0.01				)





						@skip_mps
						def   UpperCAmelCase_ (				self							:    Dict				)				->				Optional[Any]:
													'''simple docstring'''



													__UpperCamelCase							=torch_device == "cpu"
													__UpperCamelCase							=True
													__UpperCamelCase							=[
													    "prior_num_inference_steps",
													    "decoder_num_inference_steps",
													    "super_res_num_inference_steps",
													]

													self._test_inference_batch_single_identical(
													    test_max_difference=__lowerCamelCase     , relax_max_difference=__lowerCamelCase     , additional_params_copy_to_batched_inputs=__lowerCamelCase     , )





						def   UpperCAmelCase_ (				self							:    str				)				->				Optional[int]:
													'''simple docstring'''



													__UpperCamelCase							=[
													    "prior_num_inference_steps",
													    "decoder_num_inference_steps",
													    "super_res_num_inference_steps",
													]

													if torch_device == "mps":
																				# TODO: MPS errors with larger batch sizes
																				__UpperCamelCase							=[2, 3]
																				self._test_inference_batch_consistent(
																				    batch_sizes=__lowerCamelCase     , additional_params_copy_to_batched_inputs=__lowerCamelCase     , )
													else:
																				self._test_inference_batch_consistent(
																				    additional_params_copy_to_batched_inputs=__lowerCamelCase				)





						@skip_mps
						def   UpperCAmelCase_ (				self							:    List[Any]				)				->				Optional[Any]:
													'''simple docstring'''



													return super().test_dict_tuple_outputs_equivalent()





						@skip_mps
						def   UpperCAmelCase_ (				self							:    Dict				)				->				List[Any]:
													'''simple docstring'''



													return super().test_save_load_local()





						@skip_mps
						def   UpperCAmelCase_ (				self							:    Optional[int]				)				->				List[Any]:
													'''simple docstring'''



													return super().test_save_load_optional_components()






@nightly
class 		_lowercase (						unittest.TestCase      ):



						"""simple docstring"""




						def   UpperCAmelCase_ (				self							:    Tuple				)				->				Tuple:
													'''simple docstring'''



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





						def   UpperCAmelCase_ (				self							:    str				)				->				Any:
													'''simple docstring'''



													__UpperCamelCase							=load_numpy(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
													    '''/unclip/karlo_v1_alpha_horse_cpu.npy'''				)

													__UpperCamelCase							=UnCLIPPipeline.from_pretrained('''kakaobrain/karlo-v1-alpha'''				)
													pipeline.set_progress_bar_config(disable=__lowerCamelCase				)

													__UpperCamelCase							=torch.manual_seed(0				)
													__UpperCamelCase							=pipeline(
													    '''horse'''     , num_images_per_prompt=1     , generator=__lowerCamelCase     , output_type='''np'''     , )

													__UpperCamelCase							=output.images[0]

													assert image.shape == (256, 256, 3)
													assert np.abs(expected_image - image				).max() < 1E-1






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



						"""simple docstring"""




						def   UpperCAmelCase_ (				self							:    Any				)				->				Union[str, Any]:
													'''simple docstring'''



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





						def   UpperCAmelCase_ (				self							:    Union[str, Any]				)				->				Union[str, Any]:
													'''simple docstring'''



													__UpperCamelCase							=load_numpy(
													    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
													    '''/unclip/karlo_v1_alpha_horse_fp16.npy'''				)

													__UpperCamelCase							=UnCLIPPipeline.from_pretrained('''kakaobrain/karlo-v1-alpha'''     , torch_dtype=torch.floataa				)
													__UpperCamelCase							=pipeline.to(__lowerCamelCase				)
													pipeline.set_progress_bar_config(disable=__lowerCamelCase				)

													__UpperCamelCase							=torch.Generator(device='''cpu'''				).manual_seed(0				)
													__UpperCamelCase							=pipeline(
													    '''horse'''     , generator=__lowerCamelCase     , output_type='''np'''     , )

													__UpperCamelCase							=output.images[0]

													assert image.shape == (256, 256, 3)

													assert_mean_pixel_difference(__lowerCamelCase     , __lowerCamelCase				)





						def   UpperCAmelCase_ (				self							:    Dict				)				->				Dict:
													'''simple docstring'''



													torch.cuda.empty_cache()
													torch.cuda.reset_max_memory_allocated()
													torch.cuda.reset_peak_memory_stats()

													__UpperCamelCase							=UnCLIPPipeline.from_pretrained('''kakaobrain/karlo-v1-alpha'''     , torch_dtype=torch.floataa				)
													__UpperCamelCase							=pipe.to(__lowerCamelCase				)
													pipe.set_progress_bar_config(disable=__lowerCamelCase				)
													pipe.enable_attention_slicing()
													pipe.enable_sequential_cpu_offload()

													__UpperCamelCase							=pipe(
													    '''horse'''     , num_images_per_prompt=1     , prior_num_inference_steps=2     , decoder_num_inference_steps=2     , super_res_num_inference_steps=2     , output_type='''np'''     , )

													__UpperCamelCase							=torch.cuda.max_memory_allocated()
													# make sure that less than 7 GB is allocated
													assert mem_bytes < 7 * 10**9







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


