






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        _a  (     UpperCamelCase_					:				Optional[int]    ,   UpperCamelCase_					:				str=False   ) ->   Optional[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						[]
						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"
												lowerCAmelCase__       =						[(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        _a  (     UpperCamelCase_					:				Union[str, Any]    ,   UpperCamelCase_					:				Union[str, Any]    ,   UpperCamelCase_					:				Tuple=False   ) ->   Optional[int]:
						"""simple docstring"""







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


def        _a  (     UpperCamelCase_					:				Any   ) ->   Optional[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						["head.weight", "head.bias"]
						for k in ignore_keys:
												state_dict.pop(UpperCamelCase_    ,   UpperCamelCase_   )


def        _a  (     UpperCamelCase_					:				int   ) ->   Optional[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						[
						    "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(UpperCamelCase_    ,   UpperCamelCase_   )


def        _a  (     UpperCamelCase_					:				Tuple    ,   UpperCamelCase_					:				Dict    ,   UpperCamelCase_					:				Any   ) ->   Union[str, Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						dct.pop(UpperCamelCase_   )
						lowerCAmelCase__       =						val


def        _a  (     UpperCamelCase_					:				Tuple    ,   UpperCamelCase_					:				Optional[Any]   ) ->   List[Any]:
						"""simple docstring"""







						lowerCAmelCase__       =						ViTMSNConfig()
						lowerCAmelCase__       =						1_000

						lowerCAmelCase__       =						"datasets/huggingface/label-files"
						lowerCAmelCase__       =						"imagenet-1k-id2label.json"
						lowerCAmelCase__       =						json.load(open(hf_hub_download(UpperCamelCase_    ,   UpperCamelCase_   )    ,   "r"   )   )
						lowerCAmelCase__       =						{int(UpperCamelCase_   ): v for k, v in idalabel.items()}
						lowerCAmelCase__       =						idalabel
						lowerCAmelCase__       =						{v: k for k, v in idalabel.items()}

						if "s16" in checkpoint_url:
												lowerCAmelCase__       =						384
												lowerCAmelCase__       =						1_536
												lowerCAmelCase__       =						6
						elif "l16" in checkpoint_url:
												lowerCAmelCase__       =						1_024
												lowerCAmelCase__       =						4_096
												lowerCAmelCase__       =						24
												lowerCAmelCase__       =						16
												lowerCAmelCase__       =						0.1
						elif "b4" in checkpoint_url:
												lowerCAmelCase__       =						4
						elif "l7" in checkpoint_url:
												lowerCAmelCase__       =						7
												lowerCAmelCase__       =						1_024
												lowerCAmelCase__       =						4_096
												lowerCAmelCase__       =						24
												lowerCAmelCase__       =						16
												lowerCAmelCase__       =						0.1

						lowerCAmelCase__       =						ViTMSNModel(UpperCamelCase_   )

						lowerCAmelCase__       =						torch.hub.load_state_dict_from_url(UpperCamelCase_    ,   map_location="cpu"   )["target_encoder"]

						lowerCAmelCase__       =						ViTImageProcessor(size=config.image_size   )

						remove_projection_head(UpperCamelCase_   )
						lowerCAmelCase__       =						create_rename_keys(UpperCamelCase_    ,   base_model=UpperCamelCase_   )

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

						model.load_state_dict(UpperCamelCase_   )
						model.eval()

						lowerCAmelCase__       =						"http://images.cocodataset.org/val2017/000000039769.jpg"

						lowerCAmelCase__       =						Image.open(requests.get(UpperCamelCase_    ,   stream=UpperCamelCase_   ).raw   )
						lowerCAmelCase__       =						ViTImageProcessor(
						    size=config.image_size    ,   image_mean=UpperCamelCase_    ,   image_std=UpperCamelCase_   )
						lowerCAmelCase__       =						image_processor(images=UpperCamelCase_    ,   return_tensors="pt"   )

						# forward pass
						torch.manual_seed(2   )
						lowerCAmelCase__       =						model(**UpperCamelCase_   )
						lowerCAmelCase__       =						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:
												lowerCAmelCase__       =						torch.tensor([[-1.0_915, -1.4_876, -1.1_809]]   )
						elif "b16" in checkpoint_url:
												lowerCAmelCase__       =						torch.tensor([[14.2_889, -18.9_045, 11.7_281]]   )
						elif "l16" in checkpoint_url:
												lowerCAmelCase__       =						torch.tensor([[41.5_028, -22.8_681, 45.6_475]]   )
						elif "b4" in checkpoint_url:
												lowerCAmelCase__       =						torch.tensor([[-4.3_868, 5.2_932, -0.4_137]]   )
						else:
												lowerCAmelCase__       =						torch.tensor([[-0.1_792, -0.6_465, 2.4_263]]   )

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

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

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


if __name__ == "__main__":
	a_							=    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.'''
	)

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














import os
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen, xsplitext
from ..table import array_cast
from ..utils.py_utils import no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
	from .features import FeatureType


a_,						a_,						a_							=    False, False, False


@dataclass
class      lowercase__     :
	a_         =None
	a_         =True
	a_         =True
	a_         =None
	# Automatically constructed
	a_         ="dict"
	a_         =pa.struct({"""bytes""": pa.binary(), """path""": pa.string()}	)
	a_         =field(default="""Audio""",					init=_UpperCAmelCase,					repr=_UpperCAmelCase	)

	def __call__(							self       )->					Optional[int]:







							'''simple docstring'''





							return self.pa_type

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase       )->					dict:







							'''simple docstring'''





							try:
													import soundfile as sf  # soundfile is a dependency of librosa, needed to decode audio files.
							except ImportError as err:
													raise ImportError("To support encoding audio data, please install 'soundfile'."       ) from err
							if isinstance(__UpperCAmelCase		,    __UpperCAmelCase       ):
													return {"bytes": None, "path": value}
							elif isinstance(__UpperCAmelCase		,    __UpperCAmelCase       ):
													return {"bytes": value, "path": None}
							elif "array" in value:
													# convert the audio array to wav bytes
													lowerCAmelCase__       =						BytesIO()
													sf.write(__UpperCAmelCase		,    value["array"]		,    value["sampling_rate"]		,    format="wav"       )
													return {"bytes": buffer.getvalue(), "path": None}
							elif value.get("path"       ) is not None and os.path.isfile(value["path"]       ):
													# we set "bytes": None to not duplicate the data if they're already available locally
													if value["path"].endswith("pcm"       ):
																			# "PCM" only has raw audio bytes
																			if value.get("sampling_rate"       ) is None:
																									# At least, If you want to convert "PCM-byte" to "WAV-byte", you have to know sampling rate
																									raise KeyError("To use PCM files, please specify a 'sampling_rate' in Audio object"       )
																			if value.get("bytes"       ):
																									# If we already had PCM-byte, we don`t have to make "read file, make bytes" (just use it!)
																									lowerCAmelCase__       =						np.frombuffer(value["bytes"]		,    dtype=np.intaa       ).astype(np.floataa       ) / 32767
																			else:
																									lowerCAmelCase__       =						np.memmap(value["path"]		,    dtype="h"		,    mode="r"       ).astype(np.floataa       ) / 32767

																			lowerCAmelCase__       =						BytesIO(bytes()       )
																			sf.write(__UpperCAmelCase		,    __UpperCAmelCase		,    value["sampling_rate"]		,    format="wav"       )
																			return {"bytes": buffer.getvalue(), "path": None}
													else:
																			return {"bytes": None, "path": value.get("path"       )}
							elif value.get("bytes"       ) is not None or value.get("path"       ) is not None:
													# store the audio bytes, and path is used to infer the audio format using the file extension
													return {"bytes": value.get("bytes"       ), "path": value.get("path"       )}
							else:
													raise ValueError(
													    F"An audio sample should have one of 'path' or 'bytes' but they are missing or None in {value}."       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase		,    __UpperCAmelCase = None       )->					dict:







							'''simple docstring'''





							if not self.decode:
													raise RuntimeError("Decoding is disabled for this feature. Please use Audio(decode=True) instead."       )

							lowerCAmelCase__      ,							lowerCAmelCase__       =						(value["path"], BytesIO(value["bytes"]       )) if value["bytes"] is not None else (value["path"], None)
							if path is None and file is None:
													raise ValueError(F"An audio sample should have one of 'path' or 'bytes' but both are None in {value}."       )

							try:
													import librosa
													import soundfile as sf
							except ImportError as err:
													raise ImportError("To support decoding audio files, please install 'librosa' and 'soundfile'."       ) from err

							lowerCAmelCase__       =						xsplitext(__UpperCAmelCase       )[1][1:].lower() if path is not None else None
							if not config.IS_OPUS_SUPPORTED and audio_format == "opus":
													raise RuntimeError(
													    "Decoding 'opus' files requires system library 'libsndfile'>=1.0.31, "
													    "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. "       )
							elif not config.IS_MP3_SUPPORTED and audio_format == "mp3":
													raise RuntimeError(
													    "Decoding 'mp3' files requires system library 'libsndfile'>=1.1.0, "
													    "You can try to update `soundfile` python library: `pip install \"soundfile>=0.12.1\"`. "       )

							if file is None:
													lowerCAmelCase__       =						token_per_repo_id or {}
													lowerCAmelCase__       =						path.split("::"       )[-1]
													try:
																			lowerCAmelCase__       =						string_to_dict(__UpperCAmelCase		,    config.HUB_DATASETS_URL       )["repo_id"]
																			lowerCAmelCase__       =						token_per_repo_id[repo_id]
													except (ValueError, KeyError):
																			lowerCAmelCase__       =						None

													with xopen(__UpperCAmelCase		,    "rb"		,    use_auth_token=__UpperCAmelCase       ) as f:
																			lowerCAmelCase__      ,							lowerCAmelCase__       =						sf.read(__UpperCAmelCase       )

							else:
													lowerCAmelCase__      ,							lowerCAmelCase__       =						sf.read(__UpperCAmelCase       )

							lowerCAmelCase__       =						array.T
							if self.mono:
													lowerCAmelCase__       =						librosa.to_mono(__UpperCAmelCase       )
							if self.sampling_rate and self.sampling_rate != sampling_rate:
													lowerCAmelCase__       =						librosa.resample(__UpperCAmelCase		,    orig_sr=__UpperCAmelCase		,    target_sr=self.sampling_rate       )
													lowerCAmelCase__       =						self.sampling_rate

							return {"path": path, "array": array, "sampling_rate": sampling_rate}

	def 		UpperCAmelCase  (							self       )->					Union["FeatureType", Dict[str, "FeatureType"]]:







							'''simple docstring'''





							from .features import Value

							if self.decode:
													raise ValueError("Cannot flatten a decoded Audio feature."       )
							return {
							    "bytes": Value("binary"       ),
							    "path": Value("string"       ),
							}

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase       )->					pa.StructArray:







							'''simple docstring'''





							if pa.types.is_string(storage.type       ):
													lowerCAmelCase__       =						pa.array([None] * len(__UpperCAmelCase       )		,    type=pa.binary()       )
													lowerCAmelCase__       =						pa.StructArray.from_arrays([bytes_array, storage]		,    ["bytes", "path"]		,    mask=storage.is_null()       )
							elif pa.types.is_binary(storage.type       ):
													lowerCAmelCase__       =						pa.array([None] * len(__UpperCAmelCase       )		,    type=pa.string()       )
													lowerCAmelCase__       =						pa.StructArray.from_arrays([storage, path_array]		,    ["bytes", "path"]		,    mask=storage.is_null()       )
							elif pa.types.is_struct(storage.type       ) and storage.type.get_all_field_indices("array"       ):
													lowerCAmelCase__       =						pa.array([Audio().encode_example(__UpperCAmelCase       ) if x is not None else None for x in storage.to_pylist()]       )
							elif pa.types.is_struct(storage.type       ):
													if storage.type.get_field_index("bytes"       ) >= 0:
																			lowerCAmelCase__       =						storage.field("bytes"       )
													else:
																			lowerCAmelCase__       =						pa.array([None] * len(__UpperCAmelCase       )		,    type=pa.binary()       )
													if storage.type.get_field_index("path"       ) >= 0:
																			lowerCAmelCase__       =						storage.field("path"       )
													else:
																			lowerCAmelCase__       =						pa.array([None] * len(__UpperCAmelCase       )		,    type=pa.string()       )
													lowerCAmelCase__       =						pa.StructArray.from_arrays([bytes_array, path_array]		,    ["bytes", "path"]		,    mask=storage.is_null()       )
							return array_cast(__UpperCAmelCase		,    self.pa_type       )


	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase       )->					pa.StructArray:







							'''simple docstring'''





							@no_op_if_value_is_null
							def path_to_bytes(__UpperCAmelCase       ):
													with xopen(__UpperCAmelCase		,    "rb"       ) as f:
																			lowerCAmelCase__       =						f.read()
													return bytes_

							lowerCAmelCase__       =						pa.array(
							    [
							        (path_to_bytes(x["path"]       ) if x["bytes"] is None else x["bytes"]) if x is not None else None
							        for x in storage.to_pylist()
							    ]		,    type=pa.binary()		,    )
							lowerCAmelCase__       =						pa.array(
							    [os.path.basename(__UpperCAmelCase       ) if path is not None else None for path in storage.field("path"       ).to_pylist()]		,    type=pa.string()		,    )
							lowerCAmelCase__       =						pa.StructArray.from_arrays([bytes_array, path_array]		,    ["bytes", "path"]		,    mask=bytes_array.is_null()       )
							return array_cast(__UpperCAmelCase		,    self.pa_type       )






