


"""simple docstring"""



import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import BeitConfig, BeitForImageClassification, BeitForMaskedImageModeling, BeitImageProcessor
from transformers.image_utils import PILImageResampling
from transformers.utils import logging


logging.set_verbosity_info()
lowercase_							=							logging.get_logger(__name__)
def      UpperCAmelCase     (       _lowercase :   Union[str, Any]			,   _lowercase :   str=False			,   _lowercase :   str=False	)						-> List[str]:


	"""simple docstring"""




	lowerCAmelCase_      							=			'''backbone.''' if is_semantic else ''''''

	lowerCAmelCase_      							=			[]
	for i in range(config.num_hidden_layers	):
		# encoder layers: output projection, 2 feedforward neural networks and 2 layernorms
		rename_keys.append((F"""{prefix}blocks.{i}.norm1.weight""", F"""beit.encoder.layer.{i}.layernorm_before.weight""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.norm1.bias""", F"""beit.encoder.layer.{i}.layernorm_before.bias""")	)
		rename_keys.append(
		    (F"""{prefix}blocks.{i}.attn.proj.weight""", F"""beit.encoder.layer.{i}.attention.output.dense.weight""")	)
		rename_keys.append(
		    (F"""{prefix}blocks.{i}.attn.proj.bias""", F"""beit.encoder.layer.{i}.attention.output.dense.bias""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.norm2.weight""", F"""beit.encoder.layer.{i}.layernorm_after.weight""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.norm2.bias""", F"""beit.encoder.layer.{i}.layernorm_after.bias""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.mlp.fc1.weight""", F"""beit.encoder.layer.{i}.intermediate.dense.weight""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.mlp.fc1.bias""", F"""beit.encoder.layer.{i}.intermediate.dense.bias""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.mlp.fc2.weight""", F"""beit.encoder.layer.{i}.output.dense.weight""")	)
		rename_keys.append((F"""{prefix}blocks.{i}.mlp.fc2.bias""", F"""beit.encoder.layer.{i}.output.dense.bias""")	)

	# projection layer + position embeddings
	rename_keys.extend(
	    [
	        (F"""{prefix}cls_token""", '''beit.embeddings.cls_token'''),
	        (F"""{prefix}patch_embed.proj.weight""", '''beit.embeddings.patch_embeddings.projection.weight'''),
	        (F"""{prefix}patch_embed.proj.bias""", '''beit.embeddings.patch_embeddings.projection.bias'''),
	        (F"""{prefix}pos_embed""", '''beit.embeddings.position_embeddings'''),
	    ]	)

	if has_lm_head:
		# mask token + layernorm
		rename_keys.extend(
		    [
		        ('''mask_token''', '''beit.embeddings.mask_token'''),
		        ('''norm.weight''', '''layernorm.weight'''),
		        ('''norm.bias''', '''layernorm.bias'''),
		    ]	)
	else:
		# layernorm + classification head
		rename_keys.extend(
		    [
		        ('''fc_norm.weight''', '''beit.pooler.layernorm.weight'''),
		        ('''fc_norm.bias''', '''beit.pooler.layernorm.bias'''),
		        ('''head.weight''', '''classifier.weight'''),
		        ('''head.bias''', '''classifier.bias'''),
		    ]	)

	return rename_keys
def      UpperCAmelCase     (       _lowercase :   str			,   _lowercase :   Union[str, Any]			,   _lowercase :   Tuple=False			,   _lowercase :   str=False	)						-> List[str]:


	"""simple docstring"""




	for i in range(config.num_hidden_layers	):
		lowerCAmelCase_      							=			'''backbone.''' if is_semantic else ''''''
		# queries, keys and values
		lowerCAmelCase_      							=			state_dict.pop(F"""{prefix}blocks.{i}.attn.qkv.weight"""	)
		lowerCAmelCase_      							=			state_dict.pop(F"""{prefix}blocks.{i}.attn.q_bias"""	)
		lowerCAmelCase_      							=			state_dict.pop(F"""{prefix}blocks.{i}.attn.v_bias"""	)

		lowerCAmelCase_      							=			in_proj_weight[
		    : config.hidden_size, :
		]
		lowerCAmelCase_      							=			q_bias
		lowerCAmelCase_      							=			in_proj_weight[
		    config.hidden_size : config.hidden_size * 2, :
		]
		lowerCAmelCase_      							=			in_proj_weight[
		    -config.hidden_size :, :
		]
		lowerCAmelCase_      							=			v_bias

		# gamma_1 and gamma_2
		# we call them lambda because otherwise they are renamed when using .from_pretrained
		lowerCAmelCase_      							=			state_dict.pop(F"""{prefix}blocks.{i}.gamma_1"""	)
		lowerCAmelCase_      							=			state_dict.pop(F"""{prefix}blocks.{i}.gamma_2"""	)

		lowerCAmelCase_      							=			gamma_a
		lowerCAmelCase_      							=			gamma_a
def      UpperCAmelCase     (       _lowercase :   str			,   _lowercase :   Dict			,   _lowercase :   Optional[Any]	)						-> Optional[Any]:


	"""simple docstring"""




	lowerCAmelCase_      							=			dct.pop(_lowercase	)
	lowerCAmelCase_      							=			val
def      UpperCAmelCase     (       )						-> Dict:


	"""simple docstring"""




	lowerCAmelCase_      							=			'''http://images.cocodataset.org/val2017/000000039769.jpg'''
	lowerCAmelCase_      							=			Image.open(requests.get(_lowercase			,   stream=_lowercase	).raw	)
	return im
@torch.no_grad()
def      UpperCAmelCase     (       _lowercase :   Any			,   _lowercase :   Optional[Any]			,   _lowercase :   List[str]=False	)						-> Optional[int]:


	"""simple docstring"""




	lowerCAmelCase_      							=			False if '''rvlcdip''' in checkpoint_url else True
	lowerCAmelCase_      							=			BeitConfig(use_absolute_position_embeddings=_lowercase			,   use_mask_token=_lowercase	)

	# size of the architecture
	if "large" in checkpoint_url or "dit-l" in checkpoint_url:
		lowerCAmelCase_      							=			1_0_2_4
		lowerCAmelCase_      							=			4_0_9_6
		lowerCAmelCase_      							=			2_4
		lowerCAmelCase_      							=			1_6

	# labels
	if "rvlcdip" in checkpoint_url:
		lowerCAmelCase_      							=			1_6
		lowerCAmelCase_      							=			'''huggingface/label-files'''
		lowerCAmelCase_      							=			'''rvlcdip-id2label.json'''
		lowerCAmelCase_      							=			json.load(open(hf_hub_download(_lowercase			,   _lowercase			,   repo_type='''dataset'''	)			,   '''r'''	)	)
		lowerCAmelCase_      							=			{int(_lowercase	): v for k, v in idalabel.items()}
		lowerCAmelCase_      							=			idalabel
		lowerCAmelCase_      							=			{v: k for k, v in idalabel.items()}

	# load state_dict of original model, remove and rename some keys
	lowerCAmelCase_      							=			torch.hub.load_state_dict_from_url(_lowercase			,   map_location='''cpu'''	)['''model''']

	lowerCAmelCase_      							=			create_rename_keys(_lowercase			,   has_lm_head=_lowercase	)
	for src, dest in rename_keys:
		rename_key(_lowercase			,   _lowercase			,   _lowercase	)
	read_in_q_k_v(_lowercase			,   _lowercase			,   has_lm_head=_lowercase	)

	# load HuggingFace model
	lowerCAmelCase_      							=			BeitForMaskedImageModeling(_lowercase	) if has_lm_head else BeitForImageClassification(_lowercase	)
	model.eval()
	model.load_state_dict(_lowercase	)

	# Check outputs on an image
	lowerCAmelCase_      							=			BeitImageProcessor(
	    size=config.image_size			,   resample=PILImageResampling.BILINEAR			,   do_center_crop=_lowercase	)
	lowerCAmelCase_      							=			prepare_img()

	lowerCAmelCase_      							=			image_processor(images=_lowercase			,   return_tensors='''pt'''	)
	lowerCAmelCase_      							=			encoding['''pixel_values''']

	lowerCAmelCase_      							=			model(_lowercase	)
	lowerCAmelCase_      							=			outputs.logits

	# verify logits
	lowerCAmelCase_      							=			[1, 1_6] if '''rvlcdip''' in checkpoint_url else [1, 1_9_6, 8_1_9_2]
	assert logits.shape == torch.Size(_lowercase	), "Shape of logits not as expected"

	Path(_lowercase	).mkdir(exist_ok=_lowercase	)
	print(F"""Saving model to {pytorch_dump_folder_path}"""	)
	model.save_pretrained(_lowercase	)
	print(F"""Saving image processor to {pytorch_dump_folder_path}"""	)
	image_processor.save_pretrained(_lowercase	)

	if push_to_hub:
		if has_lm_head:
			lowerCAmelCase_      							=			'''dit-base''' if '''base''' in checkpoint_url else '''dit-large'''
		else:
			lowerCAmelCase_      							=			'''dit-base-finetuned-rvlcdip''' if '''dit-b''' in checkpoint_url else '''dit-large-finetuned-rvlcdip'''
		image_processor.push_to_hub(
		    repo_path_or_name=Path(_lowercase			,   _lowercase	)			,   organization='''nielsr'''			,   commit_message='''Add image processor'''			,   use_temp_dir=_lowercase			,   )
		model.push_to_hub(
		    repo_path_or_name=Path(_lowercase			,   _lowercase	)			,   organization='''nielsr'''			,   commit_message='''Add model'''			,   use_temp_dir=_lowercase			,   )


if __name__ == "__main__":
		lowercase_							=							argparse.ArgumentParser()

		parser.add_argument(
		    '--checkpoint_url',
		    default='https://layoutlm.blob.core.windows.net/dit/dit-pts/dit-base-224-p16-500k-62d53a.pth',
		    type=str,
		    help='URL to the original PyTorch checkpoint (.pth file).',
		)
		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',
		)
		lowercase_							=							parser.parse_args()
		convert_dit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.push_to_hub)



"""simple docstring"""



from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
from google.protobuf.internal import builder as _builder


# @@protoc_insertion_point(imports)

lowercase_							=							_symbol_database.Default()


lowercase_							=							_descriptor_pool.Default().AddSerializedFile(
    b'\n\x19sentencepiece_model.proto\x12\rsentencepiece"\x80\x0c\n\x0bTrainerSpec\x12\r\n\x05input\x18\x01 \x03(\t\x12\x14\n\x0cinput_format\x18\x07 \x01(\t\x12\x14\n\x0cmodel_prefix\x18\x02 \x01(\t\x12\x41\n\nmodel_type\x18\x03 \x01(\x0e\x32$.sentencepiece.TrainerSpec.ModelType:\x07UNIGRAM\x12\x18\n\nvocab_size\x18\x04 \x01(\x05:\x04\x38\x30\x30\x30\x12\x17\n\x0f\x61\x63\x63\x65pt_language\x18\x05 \x03(\t\x12 \n\x15self_test_sample_size\x18\x06 \x01(\x05:\x01\x30\x12*\n\x1b\x65nable_differential_privacy\x18\x32 \x01(\x08:\x05\x66\x61lse\x12+\n differential_privacy_noise_level\x18\x33 \x01(\x02:\x01\x30\x12\x32\n\'differential_privacy_clipping_threshold\x18\x34 \x01(\x04:\x01\x30\x12"\n\x12\x63haracter_coverage\x18\n \x01(\x02:\x06\x30.9995\x12\x1e\n\x13input_sentence_size\x18\x0b \x01(\x04:\x01\x30\x12$\n\x16shuffle_input_sentence\x18\x13 \x01(\x08:\x04true\x12 \n\x14mining_sentence_size\x18\x0c \x01(\x05\x42\x02\x18\x01\x12"\n\x16training_sentence_size\x18\r \x01(\x05\x42\x02\x18\x01\x12(\n\x17seed_sentencepiece_size\x18\x0e \x01(\x05:\x07\x31\x30\x30\x30\x30\x30\x30\x12\x1e\n\x10shrinking_factor\x18\x0f \x01(\x02:\x04\x30.75\x12!\n\x13max_sentence_length\x18\x12 \x01(\x05:\x04\x34\x31\x39\x32\x12\x17\n\x0bnum_threads\x18\x10 \x01(\x05:\x02\x31\x36\x12\x1d\n\x12num_sub_iterations\x18\x11 \x01(\x05:\x01\x32\x12$\n\x18max_sentencepiece_length\x18\x14 \x01(\x05:\x02\x31\x36\x12%\n\x17split_by_unicode_script\x18\x15 \x01(\x08:\x04true\x12\x1d\n\x0fsplit_by_number\x18\x17 \x01(\x08:\x04true\x12!\n\x13split_by_whitespace\x18\x16 \x01(\x08:\x04true\x12)\n\x1atreat_whitespace_as_suffix\x18\x18 \x01(\x08:\x05\x66\x61lse\x12+\n\x1c\x61llow_whitespace_only_pieces\x18\x1a \x01(\x08:\x05\x66\x61lse\x12\x1b\n\x0csplit_digits\x18\x19 \x01(\x08:\x05\x66\x61lse\x12#\n\x19pretokenization_delimiter\x18\x35 \x01(\t:\x00\x12\x17\n\x0f\x63ontrol_symbols\x18\x1e \x03(\t\x12\x1c\n\x14user_defined_symbols\x18\x1f \x03(\t\x12\x16\n\x0erequired_chars\x18$ \x01(\t\x12\x1c\n\rbyte_fallback\x18# \x01(\x08:\x05\x66\x61lse\x12+\n\x1dvocabulary_output_piece_score\x18  \x01(\x08:\x04true\x12\x1e\n\x10hard_vocab_limit\x18! \x01(\x08:\x04true\x12\x1c\n\ruse_all_vocab\x18" \x01(\x08:\x05\x66\x61lse\x12\x11\n\x06unk_id\x18( \x01(\x05:\x01\x30\x12\x11\n\x06\x62os_id\x18) \x01(\x05:\x01\x31\x12\x11\n\x06\x65os_id\x18* \x01(\x05:\x01\x32\x12\x12\n\x06pad_id\x18+ \x01(\x05:\x02-1\x12\x18\n\tunk_piece\x18- \x01(\t:\x05<unk>\x12\x16\n\tbos_piece\x18. \x01(\t:\x03<s>\x12\x17\n\teos_piece\x18/ \x01(\t:\x04</s>\x12\x18\n\tpad_piece\x18\x30 \x01(\t:\x05<pad>\x12\x1a\n\x0bunk_surface\x18, \x01(\t:\x05 \xe2\x81\x87 \x12+\n\x1ctrain_extremely_large_corpus\x18\x31 \x01(\x08:\x05\x66\x61lse"5\n\tModelType\x12\x0b\n\x07UNIGRAM\x10\x01\x12\x07\n\x03\x42PE\x10\x02\x12\x08\n\x04WORD\x10\x03\x12\x08\n\x04\x43HAR\x10\x04*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xd1\x01\n\x0eNormalizerSpec\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x1c\n\x14precompiled_charsmap\x18\x02 \x01(\x0c\x12\x1e\n\x10\x61\x64\x64_dummy_prefix\x18\x03 \x01(\x08:\x04true\x12&\n\x18remove_extra_whitespaces\x18\x04 \x01(\x08:\x04true\x12 \n\x12\x65scape_whitespaces\x18\x05 \x01(\x08:\x04true\x12\x1e\n\x16normalization_rule_tsv\x18\x06 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"y\n\x0cSelfTestData\x12\x33\n\x07samples\x18\x01 \x03(\x0b\x32".sentencepiece.SelfTestData.Sample\x1a)\n\x06Sample\x12\r\n\x05input\x18\x01 \x01(\t\x12\x10\n\x08\x65xpected\x18\x02 \x01(\t*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02"\xfe\x03\n\nModelProto\x12\x37\n\x06pieces\x18\x01 \x03(\x0b\x32\'.sentencepiece.ModelProto.SentencePiece\x12\x30\n\x0ctrainer_spec\x18\x02 \x01(\x0b\x32\x1a.sentencepiece.TrainerSpec\x12\x36\n\x0fnormalizer_spec\x18\x03 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x12\x33\n\x0eself_test_data\x18\x04 \x01(\x0b\x32\x1b.sentencepiece.SelfTestData\x12\x38\n\x11\x64\x65normalizer_spec\x18\x05 \x01(\x0b\x32\x1d.sentencepiece.NormalizerSpec\x1a\xd2\x01\n\rSentencePiece\x12\r\n\x05piece\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x12\x42\n\x04type\x18\x03 \x01(\x0e\x32,.sentencepiece.ModelProto.SentencePiece.Type:\x06NORMAL"T\n\x04Type\x12\n\n\x06NORMAL\x10\x01\x12\x0b\n\x07UNKNOWN\x10\x02\x12\x0b\n\x07\x43ONTROL\x10\x03\x12\x10\n\x0cUSER_DEFINED\x10\x04\x12\x08\n\x04\x42YTE\x10\x06\x12\n\n\x06UNUSED\x10\x05*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02*\t\x08\xc8\x01\x10\x80\x80\x80\x80\x02\x42\x02H\x03'
)

lowercase_							=							globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'sentencepiece_model_pb2', _globals)
if _descriptor._USE_C_DESCRIPTORS is False:
		lowercase_							=							None
		lowercase_							=							b'H\003'
		# (generated by protobuf compiler, but `_TRAINERSPEC` is not defined)
		# _TRAINERSPEC.fields_by_name["mining_sentence_size"]._options = None
		# _TRAINERSPEC.fields_by_name["mining_sentence_size"]._serialized_options = b"\030\001"
		# _TRAINERSPEC.fields_by_name["training_sentence_size"]._options = None
		# _TRAINERSPEC.fields_by_name["training_sentence_size"]._serialized_options = b"\030\001"
		lowercase_							=							45
		lowercase_							=							15_81
		lowercase_							=							15_17
		lowercase_							=							15_70
		lowercase_							=							15_84
		lowercase_							=							17_93
		lowercase_							=							17_95
		lowercase_							=							19_16
		lowercase_							=							18_64
		lowercase_							=							19_05
		lowercase_							=							19_19
		lowercase_							=							24_29
		lowercase_							=							22_08
		lowercase_							=							24_18
		lowercase_							=							23_23
		lowercase_							=							24_07
# @@protoc_insertion_point(module_scope)