


import argparse

import fairseq
import torch
from torch import nn

from transformers import (
    MBartaaTokenizer,
    MBartConfig,
    MBartForCausalLM,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


logging.set_verbosity_info()
SCREAMING_SNAKE_CASE__     					=			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__     					=			{
    "post_extract_proj": "feature_projection.projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "self_attn.k_proj": "encoder.layers.*.attention.k_proj",
    "self_attn.v_proj": "encoder.layers.*.attention.v_proj",
    "self_attn.q_proj": "encoder.layers.*.attention.q_proj",
    "self_attn.out_proj": "encoder.layers.*.attention.out_proj",
    "self_attn_layer_norm": "encoder.layers.*.layer_norm",
    "fc1": "encoder.layers.*.feed_forward.intermediate_dense",
    "fc2": "encoder.layers.*.feed_forward.output_dense",
    "final_layer_norm": "encoder.layers.*.final_layer_norm",
    "encoder.layer_norm": "encoder.layer_norm",
    "w2v_model.layer_norm": "feature_projection.layer_norm",
    "quantizer.weight_proj": "quantizer.weight_proj",
    "quantizer.vars": "quantizer.codevectors",
    "project_q": "project_q",
    "final_proj": "project_hid",
    "w2v_encoder.proj": "lm_head",
    "mask_emb": "masked_spec_embed",
}
SCREAMING_SNAKE_CASE__     					=			[
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
]


def     lowercase	(    a     , a     , a     , a     , a ):



		'''simple docstring'''




		for attribute in key.split("." ):
				SCREAMING_SNAKE_CASE_				:List[str]				     =						getattr(a     , a )

		if weight_type is not None:
				SCREAMING_SNAKE_CASE_				:str				     =						getattr(a     , a ).shape
		else:
				SCREAMING_SNAKE_CASE_				:int				     =						hf_pointer.shape

		assert hf_shape == value.shape, (
		    F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"
		    F" {value.shape} for {full_name}"
		)

		if weight_type == "weight":
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						value
		elif weight_type == "weight_g":
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						value
		elif weight_type == "weight_v":
				SCREAMING_SNAKE_CASE_				:Dict				     =						value
		elif weight_type == "bias":
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						value
		else:
				SCREAMING_SNAKE_CASE_				:int				     =						value

		logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}." )


def     lowercase	(    a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Dict				     =						[]
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						fairseq_model.state_dict()

		SCREAMING_SNAKE_CASE_				:Tuple				     =						hf_model.feature_extractor
		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						hf_model.adapter

		for name, value in fairseq_dict.items():
				SCREAMING_SNAKE_CASE_				:Any				     =						False
				if "conv_layers" in name:
						load_conv_layer(
						    a     , a     , a     , a     , hf_model.config.feat_extract_norm == "group"     , )
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						True
				elif any(x in name for x in ["adaptor", "w2v_encoder.proj.", "w2v_proj_ln."] ):
						load_adapter(a     , a     , a     , a )
						SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						True
				else:
						for key, mapped_key in MAPPING.items():
								if key in name or key.split("w2v_model." )[-1] == name.split("." )[0]:
										SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						True
										if "*" in mapped_key:
												SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						name.split(a )[0].split("." )[-2]
												SCREAMING_SNAKE_CASE_				:List[Any]				     =						mapped_key.replace("*"     , a )
										if "weight_g" in name:
												SCREAMING_SNAKE_CASE_				:List[str]				     =						"weight_g"
										elif "weight_v" in name:
												SCREAMING_SNAKE_CASE_				:List[Any]				     =						"weight_v"
										elif "bias" in name:
												SCREAMING_SNAKE_CASE_				:Optional[int]				     =						"bias"
										elif "weight" in name:
												SCREAMING_SNAKE_CASE_				:int				     =						"weight"
										else:
												SCREAMING_SNAKE_CASE_				:List[str]				     =						None
										set_recursively(a     , a     , a     , a     , a )
								continue
				if not is_used:
						unused_weights.append(a )

		logger.warning(F"Unused weights: {unused_weights}" )


def     lowercase	(    a     , a     , a     , a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:int				     =						full_name.split("conv_layers." )[-1]
		SCREAMING_SNAKE_CASE_				:str				     =						name.split("." )
		SCREAMING_SNAKE_CASE_				:List[str]				     =						int(items[0] )
		SCREAMING_SNAKE_CASE_				:Dict				     =						int(items[1] )

		if type_id == 0:
				if "bias" in name:
						assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
						    F"{full_name} has size {value.shape}, but"
						    F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."
						)
						SCREAMING_SNAKE_CASE_				:Tuple				     =						value
						logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." )
				elif "weight" in name:
						assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
						    F"{full_name} has size {value.shape}, but"
						    F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."
						)
						SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						value
						logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}." )
		elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
				if "bias" in name:
						assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
						    F"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was"
						    " found."
						)
						SCREAMING_SNAKE_CASE_				:str				     =						value
						logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." )
				elif "weight" in name:
						assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
						    F"{full_name} has size {value.shape}, but"
						    F" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."
						)
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						value
						logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}." )
		else:
				unused_weights.append(a )


def     lowercase	(    a     , a     , a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Tuple				     =						full_name.split("adaptor." )[-1]
		SCREAMING_SNAKE_CASE_				:Tuple				     =						name.split("." )

		if items[1].isdigit():
				SCREAMING_SNAKE_CASE_				:int				     =						int(items[1] )
		else:
				SCREAMING_SNAKE_CASE_				:Tuple				     =						None

		if "adaptor" not in full_name:
				if "proj_ln" in full_name:
						# has to be layer norm
						if "bias" in name:
								assert (
								    value.shape == adapter.proj_layer_norm.bias.data.shape
								), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found."
								SCREAMING_SNAKE_CASE_				:int				     =						value
								logger.info(F"Adapter proj layer norm bias was initialized from {full_name}." )
						if "weight" in name:
								assert (
								    value.shape == adapter.proj_layer_norm.weight.data.shape
								), F"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found."
								SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						value
				else:
						# has to be projection layer
						if "bias" in name:
								assert (
								    value.shape == adapter.proj.bias.data.shape
								), F"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found."
								SCREAMING_SNAKE_CASE_				:int				     =						value
								logger.info(F"Adapter proj layer bias was initialized from {full_name}." )
						if "weight" in name:
								assert (
								    value.shape == adapter.proj.weight.data.shape
								), F"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found."
								SCREAMING_SNAKE_CASE_				:Dict				     =						value
								logger.info(F"Adapter proj layer weight was initialized from {full_name}." )
		elif isinstance(a     , a ):
				if "bias" in name:
						assert (
						    value.shape == adapter.layers[layer_id].conv.bias.data.shape
						), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found."
						SCREAMING_SNAKE_CASE_				:Tuple				     =						value
						logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." )
				elif "weight" in name:
						assert (
						    value.shape == adapter.layers[layer_id].conv.weight.data.shape
						), F"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found."
						SCREAMING_SNAKE_CASE_				:Any				     =						value
						logger.info(F"Adapter layer {layer_id} bias was initialized from {full_name}." )
		else:
				unused_weights.append(a )


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Tuple				     =						emb.weight.shape
		SCREAMING_SNAKE_CASE_				:Dict				     =						nn.Linear(a     , a     , bias=a )
		SCREAMING_SNAKE_CASE_				:Tuple				     =						emb.weight.data
		return lin_layer


@torch.no_grad()
def     lowercase	(    a     , a     , a     , a     , a     , a     , a     , a     , a     , a     , a     , ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						WavaVecaConfig.from_pretrained(
		    a     , add_adapter=a     , adapter_stride=a     , adapter_kernel_size=a     , use_auth_token=a     , output_hidden_size=a     , )
		SCREAMING_SNAKE_CASE_				:int				     =						MBartConfig.from_pretrained(a )

		# load model
		SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						fairseq.checkpoint_utils.load_model_ensemble_and_task(
		    [checkpoint_path]     , arg_overrides={
		        "config_yaml": config_yaml_path,
		        "data": "/".join(dict_path.split("/" )[:-1] ),
		        "w2v_path": checkpoint_path,
		        "load_pretrained_decoder_from": None,
		    }     , )
		SCREAMING_SNAKE_CASE_				:str				     =						model[0].eval()

		# load feature extractor
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						WavaVecaFeatureExtractor.from_pretrained(a     , use_auth_token=a )

		# set weights for wav2vec2 encoder
		SCREAMING_SNAKE_CASE_				:Dict				     =						WavaVecaModel(a )

		recursively_load_weights_wavaveca(model.encoder     , a )

		# load decoder weights
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						MBartForCausalLM(a )
		SCREAMING_SNAKE_CASE_     ,				SCREAMING_SNAKE_CASE_				:List[Any]				     =						hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()     , strict=a )
		logger.warning(F"The following keys are missing when loading the decoder weights: {missing_keys}" )
		logger.warning(F"The following keys are unexpected when loading the decoder weights: {unexpected_keys}" )

		SCREAMING_SNAKE_CASE_				:int				     =						SpeechEncoderDecoderModel(encoder=a     , decoder=a )
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						False

		SCREAMING_SNAKE_CASE_				:int				     =						MBartaaTokenizer(a )
		tokenizer.save_pretrained(a )

		SCREAMING_SNAKE_CASE_				:Any				     =						hf_wavavec.config.to_dict()
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						tokenizer.pad_token_id
		SCREAMING_SNAKE_CASE_				:str				     =						tokenizer.bos_token_id
		SCREAMING_SNAKE_CASE_				:List[str]				     =						tokenizer.eos_token_id
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						"mbart50"
		SCREAMING_SNAKE_CASE_				:str				     =						"wav2vec2"

		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						tokenizer.eos_token_id
		SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						25_0004
		SCREAMING_SNAKE_CASE_				:int				     =						tokenizer.eos_token_id

		SCREAMING_SNAKE_CASE_				:int				     =						SpeechEncoderDecoderConfig.from_dict(a )

		hf_wavavec.save_pretrained(a )
		feature_extractor.save_pretrained(a )


if __name__ == "__main__":
		SCREAMING_SNAKE_CASE__     					=			argparse.ArgumentParser()
		parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
		parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint")
		parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model")
		parser.add_argument("--config_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model")
		parser.add_argument(
		    "--encoder_config_path",
		    default="facebook/wav2vec2-xls-r-1b",
		    type=str,
		    help="Path to hf encoder wav2vec2 checkpoint config",
		)
		parser.add_argument(
		    "--decoder_config_path",
		    default="facebook/mbart-large-50-one-to-many-mmt",
		    type=str,
		    help="Path to hf decoder checkpoint config",
		)
		parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers")
		parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers")
		parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers")
		parser.add_argument("--encoder_output_dim", default=1_024, type=int, help="encoder output dim")
		parser.add_argument("--start_token_id", default=250_004, type=int, help="`decoder_start_token_id` of model config")

		SCREAMING_SNAKE_CASE__     					=			parser.parse_args()
		convert_wavaveca_checkpoint(
		    args.checkpoint_path,
		    args.pytorch_dump_folder_path,
		    args.dict_path,
		    args.config_yaml_path,
		    encoder_config_path=args.encoder_config_path,
		    decoder_config_path=args.decoder_config_path,
		    add_adapter=args.add_adapter,
		    adapter_kernel_size=args.adapter_kernel_size,
		    adapter_stride=args.adapter_stride,
		    decoder_start_token_id=args.start_token_id,
		    encoder_output_dim=args.encoder_output_dim,
		)










import argparse
import json

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

from transformers import ViTImageProcessor, ViTMSNConfig, ViTMSNModel
from transformers.image_utils import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD


torch.set_grad_enabled(False)


def     lowercase	(    a     , a=False ):



		'''simple docstring'''




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

		# projection layer + position embeddings
		rename_keys.extend(
		    [
		        ("module.cls_token", "vit.embeddings.cls_token"),
		        ("module.patch_embed.proj.weight", "vit.embeddings.patch_embeddings.projection.weight"),
		        ("module.patch_embed.proj.bias", "vit.embeddings.patch_embeddings.projection.bias"),
		        ("module.pos_embed", "vit.embeddings.position_embeddings"),
		    ] )

		if base_model:
				# layernorm + pooler
				rename_keys.extend(
				    [
				        ("module.norm.weight", "layernorm.weight"),
				        ("module.norm.bias", "layernorm.bias"),
				    ] )

				# if just the base model, we should remove "vit" from all keys that start with "vit"
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						[(pair[0], pair[1][4:]) if pair[1].startswith("vit" ) else pair for pair in rename_keys]
		else:
				# layernorm + classification head
				rename_keys.extend(
				    [
				        ("norm.weight", "vit.layernorm.weight"),
				        ("norm.bias", "vit.layernorm.bias"),
				        ("head.weight", "classifier.weight"),
				        ("head.bias", "classifier.bias"),
				    ] )

		return rename_keys


def     lowercase	(    a     , a     , a=False ):



		'''simple docstring'''




		for i in range(config.num_hidden_layers ):
				if base_model:
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						""
				else:
						SCREAMING_SNAKE_CASE_				:List[Any]				     =						"vit."
				# read in weights + bias of input projection layer (in timm, this is a single matrix + bias)
				SCREAMING_SNAKE_CASE_				:str				     =						state_dict.pop(F"module.blocks.{i}.attn.qkv.weight" )
				SCREAMING_SNAKE_CASE_				:Dict				     =						state_dict.pop(F"module.blocks.{i}.attn.qkv.bias" )
				# next, add query, keys and values (in that order) to the state dict
				SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						in_proj_weight[
				    : config.hidden_size, :
				]
				SCREAMING_SNAKE_CASE_				:Tuple				     =						in_proj_bias[: config.hidden_size]
				SCREAMING_SNAKE_CASE_				:Dict				     =						in_proj_weight[
				    config.hidden_size : config.hidden_size * 2, :
				]
				SCREAMING_SNAKE_CASE_				:Any				     =						in_proj_bias[
				    config.hidden_size : config.hidden_size * 2
				]
				SCREAMING_SNAKE_CASE_				:str				     =						in_proj_weight[
				    -config.hidden_size :, :
				]
				SCREAMING_SNAKE_CASE_				:int				     =						in_proj_bias[-config.hidden_size :]


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						["head.weight", "head.bias"]
		for k in ignore_keys:
				state_dict.pop(a     , a )


def     lowercase	(    a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:int				     =						[
		    "module.fc.fc1.weight",
		    "module.fc.fc1.bias",
		    "module.fc.bn1.weight",
		    "module.fc.bn1.bias",
		    "module.fc.bn1.running_mean",
		    "module.fc.bn1.running_var",
		    "module.fc.bn1.num_batches_tracked",
		    "module.fc.fc2.weight",
		    "module.fc.fc2.bias",
		    "module.fc.bn2.weight",
		    "module.fc.bn2.bias",
		    "module.fc.bn2.running_mean",
		    "module.fc.bn2.running_var",
		    "module.fc.bn2.num_batches_tracked",
		    "module.fc.fc3.weight",
		    "module.fc.fc3.bias",
		]
		for k in ignore_keys:
				state_dict.pop(a     , a )


def     lowercase	(    a     , a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						dct.pop(a )
		SCREAMING_SNAKE_CASE_				:Dict				     =						val


def     lowercase	(    a     , a ):



		'''simple docstring'''




		SCREAMING_SNAKE_CASE_				:List[str]				     =						ViTMSNConfig()
		SCREAMING_SNAKE_CASE_				:Tuple				     =						1000

		SCREAMING_SNAKE_CASE_				:List[str]				     =						"datasets/huggingface/label-files"
		SCREAMING_SNAKE_CASE_				:int				     =						"imagenet-1k-id2label.json"
		SCREAMING_SNAKE_CASE_				:str				     =						json.load(open(hf_hub_download(a     , a )     , "r" ) )
		SCREAMING_SNAKE_CASE_				:List[str]				     =						{int(a ): v for k, v in idalabel.items()}
		SCREAMING_SNAKE_CASE_				:List[Any]				     =						idalabel
		SCREAMING_SNAKE_CASE_				:Tuple				     =						{v: k for k, v in idalabel.items()}

		if "s16" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						384
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						1536
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						6
		elif "l16" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:str				     =						1024
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						4096
				SCREAMING_SNAKE_CASE_				:Dict				     =						24
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						16
				SCREAMING_SNAKE_CASE_				:Dict				     =						0.1
		elif "b4" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:Dict				     =						4
		elif "l7" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						7
				SCREAMING_SNAKE_CASE_				:List[Any]				     =						1024
				SCREAMING_SNAKE_CASE_				:str				     =						4096
				SCREAMING_SNAKE_CASE_				:Tuple				     =						24
				SCREAMING_SNAKE_CASE_				:Dict				     =						16
				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						0.1

		SCREAMING_SNAKE_CASE_				:List[Any]				     =						ViTMSNModel(a )

		SCREAMING_SNAKE_CASE_				:Union[str, Any]				     =						torch.hub.load_state_dict_from_url(a     , map_location="cpu" )["target_encoder"]

		SCREAMING_SNAKE_CASE_				:Tuple				     =						ViTImageProcessor(size=config.image_size )

		remove_projection_head(a )
		SCREAMING_SNAKE_CASE_				:int				     =						create_rename_keys(a     , base_model=a )

		for src, dest in rename_keys:
				rename_key(a     , a     , a )
		read_in_q_k_v(a     , a     , base_model=a )

		model.load_state_dict(a )
		model.eval()

		SCREAMING_SNAKE_CASE_				:List[Any]				     =						"http://images.cocodataset.org/val2017/000000039769.jpg"

		SCREAMING_SNAKE_CASE_				:Dict				     =						Image.open(requests.get(a     , stream=a ).raw )
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						ViTImageProcessor(
		    size=config.image_size     , image_mean=a     , image_std=a )
		SCREAMING_SNAKE_CASE_				:Any				     =						image_processor(images=a     , return_tensors="pt" )

		# forward pass
		torch.manual_seed(2 )
		SCREAMING_SNAKE_CASE_				:Tuple				     =						model(**a )
		SCREAMING_SNAKE_CASE_				:Optional[int]				     =						outputs.last_hidden_state

		# The following Colab Notebook was used to generate these outputs:
		# https://colab.research.google.com/gist/sayakpaul/3672419a04f5997827503fd84079bdd1/scratchpad.ipynb
		if "s16" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:Dict				     =						torch.tensor([[-1.0_915, -1.4_876, -1.1_809]] )
		elif "b16" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:Dict				     =						torch.tensor([[14.2_889, -18.9_045, 11.7_281]] )
		elif "l16" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:Optional[int]				     =						torch.tensor([[41.5_028, -22.8_681, 45.6_475]] )
		elif "b4" in checkpoint_url:
				SCREAMING_SNAKE_CASE_				:List[str]				     =						torch.tensor([[-4.3_868, 5.2_932, -0.4_137]] )
		else:
				SCREAMING_SNAKE_CASE_				:List[str]				     =						torch.tensor([[-0.1_792, -0.6_465, 2.4_263]] )

		# verify logits
		assert torch.allclose(last_hidden_state[:, 0, :3]     , a     , atol=1e-4 )

		print(F"Saving model to {pytorch_dump_folder_path}" )
		model.save_pretrained(a )

		print(F"Saving image processor to {pytorch_dump_folder_path}" )
		image_processor.save_pretrained(a )


if __name__ == "__main__":
		SCREAMING_SNAKE_CASE__     					=			argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    "--checkpoint_url",
		    default="https://dl.fbaipublicfiles.com/msn/vits16_800ep.pth.tar",
		    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."
		)

		SCREAMING_SNAKE_CASE__     					=			parser.parse_args()
		convert_vit_msn_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)






