



'''simple docstring'''




import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase__ :			Optional[Any]												=				logging.get_logger(__name__)

UpperCamelCase__ :			Any												=				{
    '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''',
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}






class 		_UpperCamelCase						(							lowerCamelCase__					):


			'''simple docstring'''

			_A   :	Optional[int]    					=	'''wav2vec2'''

			def __init__(							self  :				Union[str, Any]							,  lowerCAmelCase__  :				Optional[int]=3_2							,  lowerCAmelCase__  :				Optional[int]=7_6_8							,  lowerCAmelCase__  :				Tuple=1_2							,  lowerCAmelCase__  :				List[str]=1_2							,  lowerCAmelCase__  :				Dict=3_0_7_2							,  lowerCAmelCase__  :				Optional[int]="gelu"							,  lowerCAmelCase__  :				List[Any]=0.1							,  lowerCAmelCase__  :				Optional[Any]=0.1							,  lowerCAmelCase__  :				Any=0.1							,  lowerCAmelCase__  :				Dict=0.0							,  lowerCAmelCase__  :				Union[str, Any]=0.0							,  lowerCAmelCase__  :				List[Any]=0.1							,  lowerCAmelCase__  :				int=0.1							,  lowerCAmelCase__  :				Dict=0.02							,  lowerCAmelCase__  :				Optional[Any]=1E-5							,  lowerCAmelCase__  :				Optional[int]="group"							,  lowerCAmelCase__  :				List[Any]="gelu"							,  lowerCAmelCase__  :				List[str]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)							,  lowerCAmelCase__  :				Optional[int]=(5, 2, 2, 2, 2, 2, 2)							,  lowerCAmelCase__  :				Dict=(1_0, 3, 3, 3, 3, 2, 2)							,  lowerCAmelCase__  :				int=False							,  lowerCAmelCase__  :				Union[str, Any]=1_2_8							,  lowerCAmelCase__  :				str=1_6							,  lowerCAmelCase__  :				int=False							,  lowerCAmelCase__  :				List[str]=True							,  lowerCAmelCase__  :				Any=0.05							,  lowerCAmelCase__  :				Optional[int]=1_0							,  lowerCAmelCase__  :				str=2							,  lowerCAmelCase__  :				int=0.0							,  lowerCAmelCase__  :				str=1_0							,  lowerCAmelCase__  :				str=0							,  lowerCAmelCase__  :				Any=3_2_0							,  lowerCAmelCase__  :				Union[str, Any]=2							,  lowerCAmelCase__  :				int=0.1							,  lowerCAmelCase__  :				int=1_0_0							,  lowerCAmelCase__  :				Optional[Any]=2_5_6							,  lowerCAmelCase__  :				int=2_5_6							,  lowerCAmelCase__  :				int=0.1							,  lowerCAmelCase__  :				Union[str, Any]="sum"							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Union[str, Any]=False							,  lowerCAmelCase__  :				Union[str, Any]=2_5_6							,  lowerCAmelCase__  :				int=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)							,  lowerCAmelCase__  :				str=(5, 3, 3, 1, 1)							,  lowerCAmelCase__  :				Tuple=(1, 2, 3, 1, 1)							,  lowerCAmelCase__  :				List[Any]=5_1_2							,  lowerCAmelCase__  :				Any=0							,  lowerCAmelCase__  :				Optional[int]=1							,  lowerCAmelCase__  :				Any=2							,  lowerCAmelCase__  :				Tuple=False							,  lowerCAmelCase__  :				List[Any]=3							,  lowerCAmelCase__  :				Dict=2							,  lowerCAmelCase__  :				List[str]=3							,  lowerCAmelCase__  :				Optional[int]=None							,  lowerCAmelCase__  :				Any=None							,  **lowerCAmelCase__  :				str							,  ):





					"""simple docstring"""






					super().__init__(**lowerCAmelCase__							,  pad_token_id=lowerCAmelCase__							,  bos_token_id=lowerCAmelCase__							,  eos_token_id=lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	hidden_size
					__SCREAMING_SNAKE_CASE :	List[str]					 =	feat_extract_norm
					__SCREAMING_SNAKE_CASE :	List[str]					 =	feat_extract_activation
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Dict					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	conv_bias
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	num_conv_pos_embeddings
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_conv_pos_embedding_groups
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	len(self.conv_dim		)
					__SCREAMING_SNAKE_CASE :	Tuple					 =	num_hidden_layers
					__SCREAMING_SNAKE_CASE :	int					 =	intermediate_size
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	hidden_act
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	num_attention_heads
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	hidden_dropout
					__SCREAMING_SNAKE_CASE :	Dict					 =	attention_dropout
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	activation_dropout
					__SCREAMING_SNAKE_CASE :	str					 =	feat_proj_dropout
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	final_dropout
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	layerdrop
					__SCREAMING_SNAKE_CASE :	Dict					 =	layer_norm_eps
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	initializer_range
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	vocab_size
					__SCREAMING_SNAKE_CASE :	Dict					 =	do_stable_layer_norm
					__SCREAMING_SNAKE_CASE :	int					 =	use_weighted_layer_sum

					if (
					    (len(self.conv_stride		) != self.num_feat_extract_layers)
					    or (len(self.conv_kernel		) != self.num_feat_extract_layers)
					    or (len(self.conv_dim		) != self.num_feat_extract_layers)
					):
							raise ValueError(
							    """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
							    """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
							    F" {len(self.conv_dim		)}`, `len(config.conv_stride) = {len(self.conv_stride		)}`,"
							    F" `len(config.conv_kernel) = {len(self.conv_kernel		)}`."		)

					# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
					__SCREAMING_SNAKE_CASE :	Tuple					 =	apply_spec_augment
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	mask_time_prob
					__SCREAMING_SNAKE_CASE :	str					 =	mask_time_length
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	mask_time_min_masks
					__SCREAMING_SNAKE_CASE :	Tuple					 =	mask_feature_prob
					__SCREAMING_SNAKE_CASE :	Dict					 =	mask_feature_length
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	mask_feature_min_masks

					# parameters for pretraining with codevector quantized representations
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_codevectors_per_group
					__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	num_codevector_groups
					__SCREAMING_SNAKE_CASE :	int					 =	contrastive_logits_temperature
					__SCREAMING_SNAKE_CASE :	List[str]					 =	feat_quantizer_dropout
					__SCREAMING_SNAKE_CASE :	Dict					 =	num_negatives
					__SCREAMING_SNAKE_CASE :	List[Any]					 =	codevector_dim
					__SCREAMING_SNAKE_CASE :	List[str]					 =	proj_codevector_dim
					__SCREAMING_SNAKE_CASE :	List[str]					 =	diversity_loss_weight

					# ctc loss
					__SCREAMING_SNAKE_CASE :	Dict					 =	ctc_loss_reduction
					__SCREAMING_SNAKE_CASE :	str					 =	ctc_zero_infinity

					# adapter
					__SCREAMING_SNAKE_CASE :	int					 =	add_adapter
					__SCREAMING_SNAKE_CASE :	List[str]					 =	adapter_kernel_size
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	adapter_stride
					__SCREAMING_SNAKE_CASE :	List[str]					 =	num_adapter_layers
					__SCREAMING_SNAKE_CASE :	int					 =	output_hidden_size or hidden_size
					__SCREAMING_SNAKE_CASE :	Dict					 =	adapter_attn_dim

					# SequenceClassification-specific parameter. Feel free to ignore for other classes.
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	classifier_proj_size

					# XVector-specific parameters. Feel free to ignore for other classes.
					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	List[str]					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	list(lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	str					 =	xvector_output_dim



			@property
			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






					return functools.reduce(operator.mul							,  self.conv_stride							,  1		)




'''simple docstring'''




import argparse
import json
from pathlib import Path

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

from transformers import AutoImageProcessor, SwinvaConfig, SwinvaForImageClassification


def        lowerCAmelCase_     (			_lowerCamelCase:     Tuple      ):
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	SwinvaConfig()
		__SCREAMING_SNAKE_CASE :	List[Any]					 =	swinva_name.split("""_"""      )

		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	name_split[1]
		if "to" in name_split[3]:
				__SCREAMING_SNAKE_CASE :	Dict					 =	int(name_split[3][-3:]      )
		else:
				__SCREAMING_SNAKE_CASE :	str					 =	int(name_split[3]      )
		if "to" in name_split[2]:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	int(name_split[2][-2:]      )
		else:
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	int(name_split[2][6:]      )

		if model_size == "tiny":
				__SCREAMING_SNAKE_CASE :	Dict					 =	96
				__SCREAMING_SNAKE_CASE :	List[str]					 =	(2, 2, 6, 2)
				__SCREAMING_SNAKE_CASE :	List[Any]					 =	(3, 6, 12, 24)
		elif model_size == "small":
				__SCREAMING_SNAKE_CASE :	List[str]					 =	96
				__SCREAMING_SNAKE_CASE :	int					 =	(2, 2, 18, 2)
				__SCREAMING_SNAKE_CASE :	int					 =	(3, 6, 12, 24)
		elif model_size == "base":
				__SCREAMING_SNAKE_CASE :	int					 =	1_28
				__SCREAMING_SNAKE_CASE :	str					 =	(2, 2, 18, 2)
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	(4, 8, 16, 32)
		else:
				__SCREAMING_SNAKE_CASE :	List[str]					 =	1_92
				__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	(2, 2, 18, 2)
				__SCREAMING_SNAKE_CASE :	Dict					 =	(6, 12, 24, 48)

		if "to" in swinva_name:
				__SCREAMING_SNAKE_CASE :	int					 =	(12, 12, 12, 6)

		if ("22k" in swinva_name) and ("to" not in swinva_name):
				__SCREAMING_SNAKE_CASE :	int					 =	2_18_41
				__SCREAMING_SNAKE_CASE :	str					 =	"""huggingface/label-files"""
				__SCREAMING_SNAKE_CASE :	List[str]					 =	"""imagenet-22k-id2label.json"""
				__SCREAMING_SNAKE_CASE :	List[str]					 =	json.load(open(hf_hub_download(_lowerCamelCase ,   _lowerCamelCase ,   repo_type="""dataset"""      ) ,   """r"""      )      )
				__SCREAMING_SNAKE_CASE :	List[Any]					 =	{int(_lowerCamelCase      ): v for k, v in idalabel.items()}
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	idalabel
				__SCREAMING_SNAKE_CASE :	str					 =	{v: k for k, v in idalabel.items()}

		else:
				__SCREAMING_SNAKE_CASE :	str					 =	10_00
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	"""huggingface/label-files"""
				__SCREAMING_SNAKE_CASE :	Any					 =	"""imagenet-1k-id2label.json"""
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	json.load(open(hf_hub_download(_lowerCamelCase ,   _lowerCamelCase ,   repo_type="""dataset"""      ) ,   """r"""      )      )
				__SCREAMING_SNAKE_CASE :	int					 =	{int(_lowerCamelCase      ): v for k, v in idalabel.items()}
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	idalabel
				__SCREAMING_SNAKE_CASE :	Dict					 =	{v: k for k, v in idalabel.items()}

		__SCREAMING_SNAKE_CASE :	Any					 =	img_size
		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	num_classes
		__SCREAMING_SNAKE_CASE :	int					 =	embed_dim
		__SCREAMING_SNAKE_CASE :	Dict					 =	depths
		__SCREAMING_SNAKE_CASE :	str					 =	num_heads
		__SCREAMING_SNAKE_CASE :	int					 =	window_size

		return config


def        lowerCAmelCase_     (			_lowerCamelCase:     int      ):
		if "patch_embed.proj" in name:
				__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	name.replace("""patch_embed.proj""" ,   """embeddings.patch_embeddings.projection"""      )
		if "patch_embed.norm" in name:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	name.replace("""patch_embed.norm""" ,   """embeddings.norm"""      )
		if "layers" in name:
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	"""encoder.""" + name
		if "attn.proj" in name:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	name.replace("""attn.proj""" ,   """attention.output.dense"""      )
		if "attn" in name:
				__SCREAMING_SNAKE_CASE :	Any					 =	name.replace("""attn""" ,   """attention.self"""      )
		if "norm1" in name:
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	name.replace("""norm1""" ,   """layernorm_before"""      )
		if "norm2" in name:
				__SCREAMING_SNAKE_CASE :	Dict					 =	name.replace("""norm2""" ,   """layernorm_after"""      )
		if "mlp.fc1" in name:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	name.replace("""mlp.fc1""" ,   """intermediate.dense"""      )
		if "mlp.fc2" in name:
				__SCREAMING_SNAKE_CASE :	List[Any]					 =	name.replace("""mlp.fc2""" ,   """output.dense"""      )
		if "q_bias" in name:
				__SCREAMING_SNAKE_CASE :	Tuple					 =	name.replace("""q_bias""" ,   """query.bias"""      )
		if "k_bias" in name:
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	name.replace("""k_bias""" ,   """key.bias"""      )
		if "v_bias" in name:
				__SCREAMING_SNAKE_CASE :	List[str]					 =	name.replace("""v_bias""" ,   """value.bias"""      )
		if "cpb_mlp" in name:
				__SCREAMING_SNAKE_CASE :	str					 =	name.replace("""cpb_mlp""" ,   """continuous_position_bias_mlp"""      )
		if name == "norm.weight":
				__SCREAMING_SNAKE_CASE :	Tuple					 =	"""layernorm.weight"""
		if name == "norm.bias":
				__SCREAMING_SNAKE_CASE :	Optional[int]					 =	"""layernorm.bias"""

		if "head" in name:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	name.replace("""head""" ,   """classifier"""      )
		else:
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	"""swinv2.""" + name

		return name


def        lowerCAmelCase_     (			_lowerCamelCase:     int ,   _lowerCamelCase:     Optional[Any]      ):
		for key in orig_state_dict.copy().keys():
				__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	orig_state_dict.pop(_lowerCamelCase      )

				if "mask" in key:
						continue
				elif "qkv" in key:
						__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	key.split("""."""      )
						__SCREAMING_SNAKE_CASE :	List[str]					 =	int(key_split[1]      )
						__SCREAMING_SNAKE_CASE :	Dict					 =	int(key_split[3]      )
						__SCREAMING_SNAKE_CASE :	str					 =	model.swinva.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

						if "weight" in key:
								__SCREAMING_SNAKE_CASE :	Optional[int]					 =	val[:dim, :]
								__SCREAMING_SNAKE_CASE :	str					 =	val[dim : dim * 2, :]
								__SCREAMING_SNAKE_CASE :	Dict					 =	val[-dim:, :]
						else:
								__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	val[:dim]
								__SCREAMING_SNAKE_CASE :	int					 =	val[
								    dim : dim * 2
								]
								__SCREAMING_SNAKE_CASE :	int					 =	val[-dim:]
				else:
						__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	val

		return orig_state_dict


def        lowerCAmelCase_     (			_lowerCamelCase:     Tuple ,   _lowerCamelCase:     int      ):
		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	timm.create_model(_lowerCamelCase ,   pretrained=_lowerCamelCase      )
		timm_model.eval()

		__SCREAMING_SNAKE_CASE :	int					 =	get_swinva_config(_lowerCamelCase      )
		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	SwinvaForImageClassification(_lowerCamelCase      )
		model.eval()

		__SCREAMING_SNAKE_CASE :	Optional[int]					 =	convert_state_dict(timm_model.state_dict() ,   _lowerCamelCase      )
		model.load_state_dict(_lowerCamelCase      )

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

		__SCREAMING_SNAKE_CASE :	List[Any]					 =	AutoImageProcessor.from_pretrained("""microsoft/{}""".format(swinva_name.replace("""_""" ,   """-"""      )      )      )
		__SCREAMING_SNAKE_CASE :	Optional[Any]					 =	Image.open(requests.get(_lowerCamelCase ,   stream=_lowerCamelCase      ).raw      )
		__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	image_processor(images=_lowerCamelCase ,   return_tensors="""pt"""      )

		__SCREAMING_SNAKE_CASE :	int					 =	timm_model(inputs["""pixel_values"""]      )
		__SCREAMING_SNAKE_CASE :	Dict					 =	model(**_lowerCamelCase      ).logits

		assert torch.allclose(_lowerCamelCase ,   _lowerCamelCase ,   atol=1E-3      )

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

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

		model.push_to_hub(
		    repo_path_or_name=Path(_lowerCamelCase ,   _lowerCamelCase      ) ,   organization="""nandwalritik""" ,   commit_message="""Add model""" ,   )


if __name__ == "__main__":
						UpperCamelCase__ :			Union[str, Any]												=				argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '''--swinv2_name''',
						    default='''swinv2_tiny_patch4_window8_256''',
						    type=str,
						    help='''Name of the Swinv2 timm model 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__ :			Optional[int]												=				parser.parse_args()
						convert_swinva_checkpoint(args.swinva_name, args.pytorch_dump_folder_path)