




"""simple docstring"""

import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import cached_download, hf_hub_download, hf_hub_url
from PIL import Image

from transformers import DetaConfig, DetaForObjectDetection, DetaImageProcessor
from transformers.utils import logging


logging.set_verbosity_info()
_lowerCamelCase					:     str				       =    logging.get_logger(__name__)

def 						lowercase_ (						):




	"""simple docstring"""






	A_					:			Union[str, Any]						=						DetaConfig(
	    num_queries=900					, encoder_ffn_dim=2048					, decoder_ffn_dim=2048					, num_feature_levels=5					, assign_first_stage=_UpperCAmelCase					, with_box_refine=_UpperCAmelCase					, two_stage=_UpperCAmelCase					, )

	# set labels
	A_					:			Union[str, Any]						=						91
	A_					:			Any						=						'''huggingface/label-files'''
	A_					:			Dict						=						'''coco-detection-id2label.json'''
	A_					:			List[str]						=						json.load(open(cached_download(hf_hub_url(_UpperCAmelCase					, _UpperCAmelCase					, repo_type='''dataset'''		)		)					, '''r'''		)		)
	A_					:			Optional[int]						=						{int(_UpperCAmelCase		): v for k, v in idalabel.items()}
	A_					:			List[str]						=						idalabel
	A_					:			Tuple						=						{v: k for k, v in idalabel.items()}

	return config

def 						lowercase_ (						_UpperCAmelCase		):




	"""simple docstring"""






	A_					:			int						=						[]

	# stem
	# fmt: off
	rename_keys.append(('''backbone.0.body.conv1.weight''', '''model.backbone.model.embedder.embedder.convolution.weight''')		)
	rename_keys.append(('''backbone.0.body.bn1.weight''', '''model.backbone.model.embedder.embedder.normalization.weight''')		)
	rename_keys.append(('''backbone.0.body.bn1.bias''', '''model.backbone.model.embedder.embedder.normalization.bias''')		)
	rename_keys.append(('''backbone.0.body.bn1.running_mean''', '''model.backbone.model.embedder.embedder.normalization.running_mean''')		)
	rename_keys.append(('''backbone.0.body.bn1.running_var''', '''model.backbone.model.embedder.embedder.normalization.running_var''')		)
	# stages
	for stage_idx in range(len(config.backbone_config.depths		)		):
		for layer_idx in range(config.backbone_config.depths[stage_idx]		):
			# shortcut
			if layer_idx == 0:
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.0.weight""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.convolution.weight""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.weight""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.weight""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.bias""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.bias""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_mean""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_mean""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.downsample.1.running_var""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.shortcut.normalization.running_var""",
				    )		)
			# 3 convs
			for i in range(3		):
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.conv{i+1}.weight""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.convolution.weight""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.weight""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.weight""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.bias""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.bias""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_mean""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_mean""",
				    )		)
				rename_keys.append(
				    (
				        f"""backbone.0.body.layer{stage_idx + 1}.{layer_idx}.bn{i+1}.running_var""",
				        f"""model.backbone.model.encoder.stages.{stage_idx}.layers.{layer_idx}.layer.{i}.normalization.running_var""",
				    )		)
    # transformer encoder
	for i in range(config.encoder_layers		):
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.sampling_offsets.weight""", f"""model.encoder.layers.{i}.self_attn.sampling_offsets.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.sampling_offsets.bias""", f"""model.encoder.layers.{i}.self_attn.sampling_offsets.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.attention_weights.weight""", f"""model.encoder.layers.{i}.self_attn.attention_weights.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.attention_weights.bias""", f"""model.encoder.layers.{i}.self_attn.attention_weights.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.value_proj.weight""", f"""model.encoder.layers.{i}.self_attn.value_proj.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.value_proj.bias""", f"""model.encoder.layers.{i}.self_attn.value_proj.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.output_proj.weight""", f"""model.encoder.layers.{i}.self_attn.output_proj.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.self_attn.output_proj.bias""", f"""model.encoder.layers.{i}.self_attn.output_proj.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.norm1.weight""", f"""model.encoder.layers.{i}.self_attn_layer_norm.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.norm1.bias""", f"""model.encoder.layers.{i}.self_attn_layer_norm.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.linear1.weight""", f"""model.encoder.layers.{i}.fc1.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.linear1.bias""", f"""model.encoder.layers.{i}.fc1.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.linear2.weight""", f"""model.encoder.layers.{i}.fc2.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.linear2.bias""", f"""model.encoder.layers.{i}.fc2.bias""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.norm2.weight""", f"""model.encoder.layers.{i}.final_layer_norm.weight""")		)
		rename_keys.append((f"""transformer.encoder.layers.{i}.norm2.bias""", f"""model.encoder.layers.{i}.final_layer_norm.bias""")		)

	# transformer decoder
	for i in range(config.decoder_layers		):
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.sampling_offsets.weight""", f"""model.decoder.layers.{i}.encoder_attn.sampling_offsets.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.sampling_offsets.bias""", f"""model.decoder.layers.{i}.encoder_attn.sampling_offsets.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.attention_weights.weight""", f"""model.decoder.layers.{i}.encoder_attn.attention_weights.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.attention_weights.bias""", f"""model.decoder.layers.{i}.encoder_attn.attention_weights.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.value_proj.weight""", f"""model.decoder.layers.{i}.encoder_attn.value_proj.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.value_proj.bias""", f"""model.decoder.layers.{i}.encoder_attn.value_proj.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.output_proj.weight""", f"""model.decoder.layers.{i}.encoder_attn.output_proj.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.cross_attn.output_proj.bias""", f"""model.decoder.layers.{i}.encoder_attn.output_proj.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm1.weight""", f"""model.decoder.layers.{i}.encoder_attn_layer_norm.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm1.bias""", f"""model.decoder.layers.{i}.encoder_attn_layer_norm.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.self_attn.out_proj.weight""", f"""model.decoder.layers.{i}.self_attn.out_proj.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.self_attn.out_proj.bias""", f"""model.decoder.layers.{i}.self_attn.out_proj.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm2.weight""", f"""model.decoder.layers.{i}.self_attn_layer_norm.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm2.bias""", f"""model.decoder.layers.{i}.self_attn_layer_norm.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.linear1.weight""", f"""model.decoder.layers.{i}.fc1.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.linear1.bias""", f"""model.decoder.layers.{i}.fc1.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.linear2.weight""", f"""model.decoder.layers.{i}.fc2.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.linear2.bias""", f"""model.decoder.layers.{i}.fc2.bias""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm3.weight""", f"""model.decoder.layers.{i}.final_layer_norm.weight""")		)
		rename_keys.append((f"""transformer.decoder.layers.{i}.norm3.bias""", f"""model.decoder.layers.{i}.final_layer_norm.bias""")		)

	# fmt: on

	return rename_keys

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase		):




	"""simple docstring"""






	A_					:			List[Any]						=						dct.pop(_UpperCAmelCase		)
	A_					:			Any						=						val

def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase		):




	"""simple docstring"""






	A_					:			Optional[Any]						=						config.d_model
	for i in range(config.decoder_layers		):
		# read in weights + bias of input projection layer of self-attention
		A_					:			Dict						=						state_dict.pop(f"""transformer.decoder.layers.{i}.self_attn.in_proj_weight"""		)
		A_					:			Any						=						state_dict.pop(f"""transformer.decoder.layers.{i}.self_attn.in_proj_bias"""		)
		# next, add query, keys and values (in that order) to the state dict
		A_					:			Any						=						in_proj_weight[:hidden_size, :]
		A_					:			List[str]						=						in_proj_bias[:hidden_size]
		A_					:			Optional[Any]						=						in_proj_weight[
		    hidden_size : hidden_size * 2, :
		]
		A_					:			List[Any]						=						in_proj_bias[hidden_size : hidden_size * 2]
		A_					:			Optional[int]						=						in_proj_weight[-hidden_size:, :]
		A_					:			Optional[int]						=						in_proj_bias[-hidden_size:]

def 						lowercase_ (						):




	"""simple docstring"""






	A_					:			Any						=						'''http://images.cocodataset.org/val2017/000000039769.jpg'''
	A_					:			int						=						Image.open(requests.get(_UpperCAmelCase					, stream=_UpperCAmelCase		).raw		)

	return im

@torch.no_grad()
def 						lowercase_ (						_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase		):




	"""simple docstring"""






	A_					:			str						=						get_deta_config()

	# load original state dict
	if model_name == "deta-resnet-50":
		A_					:			str						=						'''adet_checkpoint0011.pth'''
	elif model_name == "deta-resnet-50-24-epochs":
		A_					:			List[Any]						=						'''adet_2x_checkpoint0023.pth'''
	else:
		raise ValueError(f"""Model name {model_name} not supported"""		)
	A_					:			str						=						hf_hub_download(repo_id='''nielsr/deta-checkpoints'''					, filename=_UpperCAmelCase		)
	A_					:			Any						=						torch.load(_UpperCAmelCase					, map_location='''cpu'''		)['''model''']

	# rename keys
	A_					:			Optional[Any]						=						create_rename_keys(_UpperCAmelCase		)
	for src, dest in rename_keys:
		rename_key(_UpperCAmelCase					, _UpperCAmelCase					, _UpperCAmelCase		)
	read_in_decoder_q_k_v(_UpperCAmelCase					, _UpperCAmelCase		)

	# fix some prefixes
	for key in state_dict.copy().keys():
		if "transformer.decoder.class_embed" in key or "transformer.decoder.bbox_embed" in key:
			A_					:			Dict						=						state_dict.pop(_UpperCAmelCase		)
			A_					:			int						=						val
		if "input_proj" in key:
			A_					:			Dict						=						state_dict.pop(_UpperCAmelCase		)
			A_					:			str						=						val
		if "level_embed" in key or "pos_trans" in key or "pix_trans" in key or "enc_output" in key:
			A_					:			int						=						state_dict.pop(_UpperCAmelCase		)
			A_					:			Tuple						=						val

    # finally, create HuggingFace model and load state dict
	A_					:			str						=						DetaForObjectDetection(_UpperCAmelCase		)
	model.load_state_dict(_UpperCAmelCase		)
	model.eval()

	A_					:			Optional[int]						=						'''cuda''' if torch.cuda.is_available() else '''cpu'''
	model.to(_UpperCAmelCase		)

	# load image processor
	A_					:			Any						=						DetaImageProcessor(format='''coco_detection'''		)

	# verify our conversion on image
	A_					:			List[str]						=						prepare_img()
	A_					:			List[Any]						=						processor(images=_UpperCAmelCase					, return_tensors='''pt'''		)
	A_					:			Optional[int]						=						encoding['''pixel_values''']
	A_					:			Tuple						=						model(pixel_values.to(_UpperCAmelCase		)		)

	# verify logits
	if model_name == "deta-resnet-50":
		A_					:			Optional[int]						=						torch.tensor(
		    [[-7.3978, -2.5406, -4.1668], [-8.2684, -3.9933, -3.8096], [-7.0515, -3.7973, -5.8516]]		)
		A_					:			Optional[int]						=						torch.tensor([[0.5043, 0.4973, 0.9998], [0.2542, 0.5489, 0.4748], [0.5490, 0.2765, 0.0570]]		)
	elif model_name == "deta-resnet-50-24-epochs":
		A_					:			Optional[Any]						=						torch.tensor(
		    [[-7.1688, -2.4857, -4.8669], [-7.8630, -3.8154, -4.2674], [-7.2730, -4.1865, -5.5323]]		)
		A_					:			List[str]						=						torch.tensor([[0.5021, 0.4971, 0.9994], [0.2546, 0.5486, 0.4731], [0.1686, 0.1986, 0.2142]]		)

	assert torch.allclose(outputs.logits[0, :3, :3]					, expected_logits.to(_UpperCAmelCase		)					, atol=1E-4		)
	assert torch.allclose(outputs.pred_boxes[0, :3, :3]					, expected_boxes.to(_UpperCAmelCase		)					, atol=1E-4		)
	print('''Everything ok!'''		)

	if pytorch_dump_folder_path:
		# Save model and processor
		logger.info(f"""Saving PyTorch model and processor to {pytorch_dump_folder_path}..."""		)
		Path(_UpperCAmelCase		).mkdir(exist_ok=_UpperCAmelCase		)
		model.save_pretrained(_UpperCAmelCase		)
		processor.save_pretrained(_UpperCAmelCase		)

	# Push to hub
	if push_to_hub:
		print('''Pushing model and processor to hub...'''		)
		model.push_to_hub(f"""jozhang97/{model_name}"""		)
		processor.push_to_hub(f"""jozhang97/{model_name}"""		)


if __name__ == "__main__":
		_lowerCamelCase					:     Dict				       =    argparse.ArgumentParser()

		parser.add_argument(
		    '--model_name',
		    type=str,
		    default='deta-resnet-50',
		    choices=['deta-resnet-50', 'deta-resnet-50-24-epochs'],
		    help='Name of the model you\'d like to convert.',
		)
		parser.add_argument(
		    '--pytorch_dump_folder_path',
		    default=None,
		    type=str,
		    help='Path to the folder to output PyTorch model.',
		)
		parser.add_argument(
		    '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
		)
		_lowerCamelCase					:     Union[str, Any]				       =    parser.parse_args()
		convert_deta_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)











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







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



					"""simple docstring"""

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







				def    a_					(							self				:   int    ):



					"""simple docstring"""

					A_					:			Optional[int]						=						ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size    )

					A_					:			Union[str, Any]						=						ids_tensor(
					    [self.batch_size, self.seq_length, self.max_depth] ,       self.max_xpath_tag_unit_embeddings    )

					A_					:			Optional[Any]						=						ids_tensor(
					    [self.batch_size, self.seq_length, self.max_depth] ,       self.max_xpath_subs_unit_embeddings    )

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

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

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

					A_					:			int						=						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				:   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    a_					(							self				:   int ,       _lowerCamelCase				:   List[Any] ,       _lowerCamelCase				:   int ,       _lowerCamelCase				:   int ,       _lowerCamelCase				:   List[Any] ,       _lowerCamelCase				:   Optional[int] ,       _lowerCamelCase				:   Optional[Any] ,       _lowerCamelCase				:   int ,       _lowerCamelCase				:   Optional[int] ,       ):



					"""simple docstring"""

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



					"""simple docstring"""

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



					"""simple docstring"""

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



					"""simple docstring"""

					A_					:			Dict						=						MarkupLMForQuestionAnswering(config=_lowerCamelCase    )
					model.to(_lowerCamelCase    )
					model.eval()
					A_					:			List[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    a_					(							self				:   List[str]    ):



					"""simple docstring"""

					A_					:			str						=						self.prepare_config_and_inputs()
					(
					    (
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	(
					    A_
					)				,	
					)					:			List[Any]						=						config_and_inputs
					A_					:			Tuple						=						{
					    '''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 (      __UpperCAmelCase       ,		__UpperCAmelCase       ,		unittest.TestCase):
				__lowerCAmelCase      :      Union[str, Any]					    =				(
				    (
				        MarkupLMModel,
				        MarkupLMForSequenceClassification,
				        MarkupLMForTokenClassification,
				        MarkupLMForQuestionAnswering,
				    )
				    if is_torch_available()
				    else None
				)
				__lowerCAmelCase      :      Optional[int]					    =				(
				    {
				        """feature-extraction""": MarkupLMModel,
				        """question-answering""": MarkupLMForQuestionAnswering,
				        """text-classification""": MarkupLMForSequenceClassification,
				        """token-classification""": MarkupLMForTokenClassification,
				        """zero-shot""": MarkupLMForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)







				def    a_					(							self				:   List[str] ,       _lowerCamelCase				:   Optional[int] ,       _lowerCamelCase				:   Dict ,       _lowerCamelCase				:   Optional[int] ,       _lowerCamelCase				:   Optional[Any] ,       _lowerCamelCase				:   Any    ):



					"""simple docstring"""

					return True







				def    a_					(							self				:   int    ):



					"""simple docstring"""

					A_					:			Tuple						=						MarkupLMModelTester(self    )
					A_					:			List[str]						=						ConfigTester(self ,       config_class=_lowerCamelCase ,       hidden_size=37    )







				def    a_					(							self				:   List[str]    ):



					"""simple docstring"""

					self.config_tester.run_common_tests()







				def    a_					(							self				:   str    ):



					"""simple docstring"""

					A_					:			str						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_lowerCamelCase    )







				def    a_					(							self				:   Tuple    ):



					"""simple docstring"""

					A_					:			Tuple						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_lowerCamelCase    )







				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			Union[str, Any]						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase    )







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Tuple						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_lowerCamelCase    )

def 						lowercase_ (						):




	"""simple docstring"""






	A_					:			Dict						=						'''
    <!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     lowercase (      unittest.TestCase):







				@cached_property
				def    a_					(							self				:   Tuple    ):



					"""simple docstring"""

					A_					:			int						=						MarkupLMFeatureExtractor()
					A_					:			Optional[int]						=						MarkupLMTokenizer.from_pretrained('''microsoft/markuplm-base'''    )

					return MarkupLMProcessor(_lowerCamelCase ,       _lowerCamelCase    )







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



					"""simple docstring"""

					A_					:			List[Any]						=						MarkupLMModel.from_pretrained('''microsoft/markuplm-base'''    ).to(_lowerCamelCase    )

					A_					:			List[Any]						=						self.default_processor

					A_					:			List[Any]						=						processor(prepare_html_string() ,       return_tensors='''pt'''    )
					A_					:			Tuple						=						inputs.to(_lowerCamelCase    )

					# forward pass
					with torch.no_grad():
						A_					:			Tuple						=						model(**_lowerCamelCase    )

					# verify the last hidden states
					A_					:			Dict						=						torch.Size([1, 14, 7_68]    )
					self.assertEqual(outputs.last_hidden_state.shape ,       _lowerCamelCase    )

					A_					:			Dict						=						torch.tensor(
					    [[0.0675, -0.0052, 0.5001], [-0.2281, 0.0802, 0.2192], [-0.0583, -0.3311, 0.1185]]    ).to(_lowerCamelCase    )

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





