

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 (
    BertTokenizer,
    ViltConfig,
    ViltForImageAndTextRetrieval,
    ViltForImagesAndTextClassification,
    ViltForMaskedLM,
    ViltForQuestionAnswering,
    ViltImageProcessor,
    ViltProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
_UpperCAmelCase						:  Union[str, Any]									=			logging.get_logger(__name__)





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__=False  ,				UpperCamelCase__=False  ,				UpperCamelCase__=False ):







							'''simple docstring'''






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

							# embeddings
							rename_keys.extend(
							    [
							        # text embeddings
							        ('text_embeddings.word_embeddings.weight', 'vilt.embeddings.text_embeddings.word_embeddings.weight'),
							        (
							            'text_embeddings.position_embeddings.weight',
							            'vilt.embeddings.text_embeddings.position_embeddings.weight',
							        ),
							        ('text_embeddings.position_ids', 'vilt.embeddings.text_embeddings.position_ids'),
							        (
							            'text_embeddings.token_type_embeddings.weight',
							            'vilt.embeddings.text_embeddings.token_type_embeddings.weight',
							        ),
							        ('text_embeddings.LayerNorm.weight', 'vilt.embeddings.text_embeddings.LayerNorm.weight'),
							        ('text_embeddings.LayerNorm.bias', 'vilt.embeddings.text_embeddings.LayerNorm.bias'),
							        # patch embeddings
							        ('transformer.cls_token', 'vilt.embeddings.cls_token'),
							        ('transformer.patch_embed.proj.weight', 'vilt.embeddings.patch_embeddings.projection.weight'),
							        ('transformer.patch_embed.proj.bias', 'vilt.embeddings.patch_embeddings.projection.bias'),
							        ('transformer.pos_embed', 'vilt.embeddings.position_embeddings'),
							        # token type embeddings
							        ('token_type_embeddings.weight', 'vilt.embeddings.token_type_embeddings.weight'),
							    ] )

							# final layernorm + pooler
							rename_keys.extend(
							    [
							        ('transformer.norm.weight', 'vilt.layernorm.weight'),
							        ('transformer.norm.bias', 'vilt.layernorm.bias'),
							        ('pooler.dense.weight', 'vilt.pooler.dense.weight'),
							        ('pooler.dense.bias', 'vilt.pooler.dense.bias'),
							    ] )

							# classifier head(s)
							if vqa_model:
														# classification head
														rename_keys.extend(
														    [
														        ('vqa_classifier.0.weight', 'classifier.0.weight'),
														        ('vqa_classifier.0.bias', 'classifier.0.bias'),
														        ('vqa_classifier.1.weight', 'classifier.1.weight'),
														        ('vqa_classifier.1.bias', 'classifier.1.bias'),
														        ('vqa_classifier.3.weight', 'classifier.3.weight'),
														        ('vqa_classifier.3.bias', 'classifier.3.bias'),
														    ] )
							elif nlvr_model:
														# classification head
														rename_keys.extend(
														    [
														        ('nlvr2_classifier.0.weight', 'classifier.0.weight'),
														        ('nlvr2_classifier.0.bias', 'classifier.0.bias'),
														        ('nlvr2_classifier.1.weight', 'classifier.1.weight'),
														        ('nlvr2_classifier.1.bias', 'classifier.1.bias'),
														        ('nlvr2_classifier.3.weight', 'classifier.3.weight'),
														        ('nlvr2_classifier.3.bias', 'classifier.3.bias'),
														    ] )
							else:
														pass

							return rename_keys





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							for i in range(config.num_hidden_layers ):
														snake_case_     							=       'vilt.'
														# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
														snake_case_     							=       state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.weight''' )
														snake_case_     							=       state_dict.pop(F'''transformer.blocks.{i}.attn.qkv.bias''' )
														# next, add query, keys and values (in that order) to the state dict
														snake_case_     							=       in_proj_weight[
														    : config.hidden_size, :
														]
														snake_case_     							=       in_proj_bias[: config.hidden_size]
														snake_case_     							=       in_proj_weight[
														    config.hidden_size : config.hidden_size * 2, :
														]
														snake_case_     							=       in_proj_bias[
														    config.hidden_size : config.hidden_size * 2
														]
														snake_case_     							=       in_proj_weight[
														    -config.hidden_size :, :
														]
														snake_case_     							=       in_proj_bias[-config.hidden_size :]





def    __lowerCamelCase      (					UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       ['head.weight', 'head.bias']
							for k in ignore_keys:
														state_dict.pop(UpperCamelCase__  ,				UpperCamelCase__ )





def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       dct.pop(UpperCamelCase__ )
							snake_case_     							=       val





@torch.no_grad()
def    __lowerCamelCase      (					UpperCamelCase__  ,				UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       ViltConfig(image_size=384  ,				patch_size=32  ,				tie_word_embeddings=UpperCamelCase__ )
							snake_case_     							=       False
							snake_case_     							=       False
							snake_case_     							=       False
							snake_case_     							=       False
							if "vqa" in checkpoint_url:
														snake_case_     							=       True
														snake_case_     							=       3129
														snake_case_     							=       'huggingface/label-files'
														snake_case_     							=       'vqa2-id2label.json'
														snake_case_     							=       json.load(open(hf_hub_download(UpperCamelCase__  ,				UpperCamelCase__  ,				repo_type='dataset' )  ,				'r' ) )
														snake_case_     							=       {int(UpperCamelCase__ ): v for k, v in idalabel.items()}
														snake_case_     							=       idalabel
														snake_case_     							=       {v: k for k, v in idalabel.items()}
														snake_case_     							=       ViltForQuestionAnswering(UpperCamelCase__ )
							elif "nlvr" in checkpoint_url:
														snake_case_     							=       True
														snake_case_     							=       2
														snake_case_     							=       {0: 'False', 1: 'True'}
														snake_case_     							=       {v: k for k, v in config.idalabel.items()}
														snake_case_     							=       3
														snake_case_     							=       ViltForImagesAndTextClassification(UpperCamelCase__ )
							elif "irtr" in checkpoint_url:
														snake_case_     							=       True
														snake_case_     							=       ViltForImageAndTextRetrieval(UpperCamelCase__ )
							elif "mlm_itm" in checkpoint_url:
														snake_case_     							=       True
														snake_case_     							=       ViltForMaskedLM(UpperCamelCase__ )
							else:
														raise ValueError('Unknown model type' )

							# load state_dict of original model, remove and rename some keys
							snake_case_     							=       torch.hub.load_state_dict_from_url(UpperCamelCase__  ,				map_location='cpu' )['state_dict']
							snake_case_     							=       create_rename_keys(UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ )
							for src, dest in rename_keys:
														rename_key(UpperCamelCase__  ,				UpperCamelCase__  ,				UpperCamelCase__ )
							read_in_q_k_v(UpperCamelCase__  ,				UpperCamelCase__ )
							if mlm_model or irtr_model:
														snake_case_     							=       ['itm_score.fc.weight', 'itm_score.fc.bias']
														for k in ignore_keys:
																					state_dict.pop(UpperCamelCase__  ,				UpperCamelCase__ )

    # load state dict into HuggingFace model
							model.eval()
							if mlm_model:
														snake_case_							,       snake_case_     							=       model.load_state_dict(UpperCamelCase__  ,				strict=UpperCamelCase__ )
														assert missing_keys == ["mlm_score.decoder.bias"]
							else:
														model.load_state_dict(UpperCamelCase__ )

							# Define processor
							snake_case_     							=       ViltImageProcessor(size=384 )
							snake_case_     							=       BertTokenizer.from_pretrained('bert-base-uncased' )
							snake_case_     							=       ViltProcessor(UpperCamelCase__  ,				UpperCamelCase__ )

							# Forward pass on example inputs (image + text)
							if nlvr_model:
														snake_case_     							=       Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'  ,				stream=UpperCamelCase__ ).raw )
														snake_case_     							=       Image.open(requests.get('https://lil.nlp.cornell.edu/nlvr/exs/ex0_0.jpg'  ,				stream=UpperCamelCase__ ).raw )
														snake_case_     							=       (
														    'The left image contains twice the number of dogs as the right image, and at least two dogs in total are'
														    ' standing.'
														)
														snake_case_     							=       processor(UpperCamelCase__  ,				UpperCamelCase__  ,				return_tensors='pt' )
														snake_case_     							=       processor(UpperCamelCase__  ,				UpperCamelCase__  ,				return_tensors='pt' )
														snake_case_     							=       model(
														    input_ids=encoding_a.input_ids  ,				pixel_values=encoding_a.pixel_values  ,				pixel_values_a=encoding_a.pixel_values  ,				)
							else:
														snake_case_     							=       Image.open(requests.get('http://images.cocodataset.org/val2017/000000039769.jpg'  ,				stream=UpperCamelCase__ ).raw )
														if mlm_model:
																					snake_case_     							=       'a bunch of [MASK] laying on a [MASK].'
														else:
																					snake_case_     							=       'How many cats are there?'
														snake_case_     							=       processor(UpperCamelCase__  ,				UpperCamelCase__  ,				return_tensors='pt' )
														snake_case_     							=       model(**UpperCamelCase__ )

							# Verify outputs
							if mlm_model:
														snake_case_     							=       torch.Size([1, 11, 30522] )
														snake_case_     							=       torch.tensor([-12.50_61, -12.51_23, -12.51_74] )
														assert outputs.logits.shape == expected_shape
														assert torch.allclose(outputs.logits[0, 0, :3]  ,				UpperCamelCase__  ,				atol=1E-4 )

														# verify masked token prediction equals "cats"
														snake_case_     							=       outputs.logits[0, 4, :].argmax(-1 ).item()
														assert tokenizer.decode([predicted_id] ) == "cats"
							elif vqa_model:
														snake_case_     							=       torch.Size([1, 3129] )
														snake_case_     							=       torch.tensor([-15.94_95, -18.14_72, -10.30_41] )
														assert torch.allclose(outputs.logits[0, :3]  ,				UpperCamelCase__  ,				atol=1E-4 )
														assert outputs.logits.shape == expected_shape
														assert torch.allclose(outputs.logits[0, 0, :3]  ,				UpperCamelCase__  ,				atol=1E-4 )

														# verify vqa prediction equals "2"
														snake_case_     							=       outputs.logits.argmax(-1 ).item()
														assert model.config.idalabel[predicted_idx] == "2"
							elif nlvr_model:
														snake_case_     							=       torch.Size([1, 2] )
														snake_case_     							=       torch.tensor([-2.87_21, 2.12_91] )
														assert torch.allclose(outputs.logits[0, :3]  ,				UpperCamelCase__  ,				atol=1E-4 )
														assert outputs.logits.shape == expected_shape

							Path(UpperCamelCase__ ).mkdir(exist_ok=UpperCamelCase__ )
							print(F'''Saving model and processor to {pytorch_dump_folder_path}''' )
							model.save_pretrained(UpperCamelCase__ )
							processor.save_pretrained(UpperCamelCase__ )


if __name__ == "__main__":
					_UpperCAmelCase						:  List[Any]									=			argparse.ArgumentParser()
					# Required parameters
					parser.add_argument(
					    """--checkpoint_url""",
					    default="""https://github.com/dandelin/ViLT/releases/download/200k/vilt_200k_mlm_itm.ckpt""",
					    type=str,
					    help="""URL of the checkpoint you'd like to convert.""",
					)
					parser.add_argument(
					    """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
					)

					_UpperCAmelCase						:  Any									=			parser.parse_args()
					convert_vilt_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)







import argparse
import struct
import unittest







class 					lowercase							:




				def __init__(   self ,  snake_case			):
											snake_case_     							=       data

											# Initialize hash values
											snake_case_     							=       [
											    0x6A09E667,
											    0xBB67AE85,
											    0x3C6EF372,
											    0xA54FF53A,
											    0x510E527F,
											    0x9B05688C,
											    0x1F83D9AB,
											    0x5BE0CD19,
											]

											# Initialize round constants
											snake_case_     							=       [
											    0x428A2F98,
											    0x71374491,
											    0xB5C0FBCF,
											    0xE9B5DBA5,
											    0x3956C25B,
											    0x59F111F1,
											    0x923F82A4,
											    0xAB1C5ED5,
											    0xD807AA98,
											    0x12835B01,
											    0x243185BE,
											    0x550C7DC3,
											    0x72BE5D74,
											    0x80DEB1FE,
											    0x9BDC06A7,
											    0xC19BF174,
											    0xE49B69C1,
											    0xEFBE4786,
											    0x0FC19DC6,
											    0x240CA1CC,
											    0x2DE92C6F,
											    0x4A7484AA,
											    0x5CB0A9DC,
											    0x76F988DA,
											    0x983E5152,
											    0xA831C66D,
											    0xB00327C8,
											    0xBF597FC7,
											    0xC6E00BF3,
											    0xD5A79147,
											    0x06CA6351,
											    0x14292967,
											    0x27B70A85,
											    0x2E1B2138,
											    0x4D2C6DFC,
											    0x53380D13,
											    0x650A7354,
											    0x766A0ABB,
											    0x81C2C92E,
											    0x92722C85,
											    0xA2BFE8A1,
											    0xA81A664B,
											    0xC24B8B70,
											    0xC76C51A3,
											    0xD192E819,
											    0xD6990624,
											    0xF40E3585,
											    0x106AA070,
											    0x19A4C116,
											    0x1E376C08,
											    0x2748774C,
											    0x34B0BCB5,
											    0x391C0CB3,
											    0x4ED8AA4A,
											    0x5B9CCA4F,
											    0x682E6FF3,
											    0x748F82EE,
											    0x78A5636F,
											    0x84C87814,
											    0x8CC70208,
											    0x90BEFFFA,
											    0xA4506CEB,
											    0xBEF9A3F7,
											    0xC67178F2,
											]

											snake_case_     							=       self.preprocessing(self.data			)
											self.final_hash()




				@staticmethod
				def        a				(   snake_case			):
											snake_case_     							=       b'\x80' + (b'\x00' * (63 - (len(snake_case			) + 8) % 64))
											snake_case_     							=       struct.pack('>Q' ,  (len(snake_case			) * 8)			)
											return data + padding + big_endian_integer




				def        a				(   self			):
											# Convert into blocks of 64 bytes
											snake_case_     							=       [
											    self.preprocessed_data[x : x + 64]
											    for x in range(0 ,  len(self.preprocessed_data			) ,  64			)
											]

											for block in self.blocks:
																		# Convert the given block into a list of 4 byte integers
																		snake_case_     							=       list(struct.unpack('>16L' ,  snake_case			)			)
																		# add 48 0-ed integers
																		words += [0] * 48

																		snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_     							=       self.hashes

																		for index in range(0 ,  64			):
																									if index > 15:
																																# modify the zero-ed indexes at the end of the array
																																snake_case_     							=       (
																																    self.ror(words[index - 15] ,  7			)
																																    ^ self.ror(words[index - 15] ,  18			)
																																    ^ (words[index - 15] >> 3)
																																)
																																snake_case_     							=       (
																																    self.ror(words[index - 2] ,  17			)
																																    ^ self.ror(words[index - 2] ,  19			)
																																    ^ (words[index - 2] >> 10)
																																)

																																snake_case_     							=       (
																																    words[index - 16] + sa + words[index - 7] + sa
																																) % 0x100000000

																									# Compression
																									snake_case_     							=       self.ror(snake_case ,  6			) ^ self.ror(snake_case ,  11			) ^ self.ror(snake_case ,  25			)
																									snake_case_     							=       (e & f) ^ ((~e & 0xFFFFFFFF) & g)
																									snake_case_     							=       (
																									    h + sa + ch + self.round_constants[index] + words[index]
																									) % 0x100000000
																									snake_case_     							=       self.ror(snake_case ,  2			) ^ self.ror(snake_case ,  13			) ^ self.ror(snake_case ,  22			)
																									snake_case_     							=       (a & b) ^ (a & c) ^ (b & c)
																									snake_case_     							=       (sa + maj) % 0x100000000

																									snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_							,       snake_case_     							=       (
																									    g,
																									    f,
																									    e,
																									    ((d + tempa) % 0x100000000),
																									    c,
																									    b,
																									    a,
																									    ((tempa + tempa) % 0x100000000),
																									)

																		snake_case_     							=       [a, b, c, d, e, f, g, h]

																		# Modify final values
																		snake_case_     							=       [
																		    ((element + mutated_hash_values[index]) % 0x100000000)
																		    for index, element in enumerate(self.hashes			)
																		]

											snake_case_     							=       ''.join([hex(snake_case			)[2:].zfill(8			) for value in self.hashes]			)




				def        a				(   self ,  snake_case ,  snake_case			):
											return 0xFFFFFFFF & (value << (32 - rotations)) | (value >> rotations)







class 					lowercase							(			unittest.TestCase     ):




				def        a				(   self			):
											import hashlib

											snake_case_     							=       bytes('Test String' ,  'utf-8'			)
											self.assertEqual(SHAaaa(snake_case			).hash ,  hashlib.shaaaa(snake_case			).hexdigest()			)





def    __lowerCamelCase      (					):







							'''simple docstring'''






							import doctest

							doctest.testmod()

							snake_case_     							=       argparse.ArgumentParser()
							parser.add_argument(
							    '-s'  ,				'--string'  ,				dest='input_string'  ,				default='Hello World!! Welcome to Cryptography'  ,				help='Hash the string'  ,				)
							parser.add_argument(
							    '-f'  ,				'--file'  ,				dest='input_file'  ,				help='Hash contents of a file' )

							snake_case_     							=       parser.parse_args()

							snake_case_     							=       args.input_string

							# hash input should be a bytestring
							if args.input_file:
														with open(args.input_file  ,				'rb' ) as f:
																					snake_case_     							=       f.read()
							else:
														snake_case_     							=       bytes(UpperCamelCase__  ,				'utf-8' )

							print(SHAaaa(UpperCamelCase__ ).hash )


if __name__ == "__main__":
					main()




