





'''simple docstring'''



import argparse
import json
from pathlib import Path

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

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


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




def       _lowerCAmelCase  ( lowerCamelCase_	:     int						,				lowerCamelCase_	:     Optional[int]=False						,				lowerCamelCase_	:     Dict=False ):
			__lowercase   =							"""backbone.""" if is_semantic else """"""

			__lowercase   =							[]
			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'''),
			    ] )

			if has_lm_head:
						# mask token + shared relative position bias + layernorm
						rename_keys.extend(
						    [
						        ('''mask_token''', '''beit.embeddings.mask_token'''),
						        (
						            '''rel_pos_bias.relative_position_bias_table''',
						            '''beit.encoder.relative_position_bias.relative_position_bias_table''',
						        ),
						        (
						            '''rel_pos_bias.relative_position_index''',
						            '''beit.encoder.relative_position_bias.relative_position_index''',
						        ),
						        ('''norm.weight''', '''layernorm.weight'''),
						        ('''norm.bias''', '''layernorm.bias'''),
						    ] )
			elif is_semantic:
						# semantic segmentation classification heads
						rename_keys.extend(
						    [
						        ('''decode_head.conv_seg.weight''', '''decode_head.classifier.weight'''),
						        ('''decode_head.conv_seg.bias''', '''decode_head.classifier.bias'''),
						        ('''auxiliary_head.conv_seg.weight''', '''auxiliary_head.classifier.weight'''),
						        ('''auxiliary_head.conv_seg.bias''', '''auxiliary_head.classifier.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       _lowerCAmelCase  ( lowerCamelCase_	:     str						,				lowerCamelCase_	:     Optional[int]						,				lowerCamelCase_	:     List[str]=False						,				lowerCamelCase_	:     List[str]=False ):
			for i in range(config.num_hidden_layers ):
						__lowercase   =							"""backbone.""" if is_semantic else """"""
						# queries, keys and values
						__lowercase   =							state_dict.pop(f"{prefix}blocks.{i}.attn.qkv.weight" )
						__lowercase   =							state_dict.pop(f"{prefix}blocks.{i}.attn.q_bias" )
						__lowercase   =							state_dict.pop(f"{prefix}blocks.{i}.attn.v_bias" )

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

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

						__lowercase   =							gamma_a
						__lowercase   =							gamma_a

						# relative_position bias table + index
						if not has_lm_head:
									# each layer has its own relative position bias
									__lowercase   =							state_dict.pop(f"{prefix}blocks.{i}.attn.relative_position_bias_table" )
									__lowercase   =							state_dict.pop(f"{prefix}blocks.{i}.attn.relative_position_index" )

									__lowercase   =							table
									__lowercase   =							index




def       _lowerCAmelCase  ( lowerCamelCase_	:     List[str]						,				lowerCamelCase_	:     Optional[int]						,				lowerCamelCase_	:     Tuple ):
			__lowercase   =							dct.pop(lowerCamelCase_ )
			__lowercase   =							val




def       _lowerCAmelCase  ( ):
			__lowercase   =							"""http://images.cocodataset.org/val2017/000000039769.jpg"""
			__lowercase   =							Image.open(requests.get(lowerCamelCase_						,				stream=lowerCamelCase_ ).raw )
			return im




@torch.no_grad()
def       _lowerCAmelCase  ( lowerCamelCase_	:     Union[str, Any]						,				lowerCamelCase_	:     List[Any] ):
			__lowercase   =							BeitConfig()
			__lowercase   =							False
			__lowercase   =							False
			__lowercase   =							"""huggingface/label-files"""
			# set config parameters based on URL
			if checkpoint_url[-9:-4] == "pt22k":
						# masked image modeling
						__lowercase   =							True
						__lowercase   =							True
						__lowercase   =							True
			elif checkpoint_url[-9:-4] == "ft22k":
						# intermediate fine-tuning on ImageNet-22k
						__lowercase   =							True
						__lowercase   =							2_1_8_4_1
						__lowercase   =							"""imagenet-22k-id2label.json"""
						__lowercase   =							json.load(open(hf_hub_download(lowerCamelCase_						,				lowerCamelCase_						,				repo_type='''dataset''' )						,				'''r''' ) )
						__lowercase   =							{int(lowerCamelCase_ ): v for k, v in idalabel.items()}
						# this dataset contains 21843 labels but the model only has 21841
						# we delete the classes as mentioned in https://github.com/google-research/big_transfer/issues/18
						del idalabel[9_2_0_5]
						del idalabel[1_5_0_2_7]
						__lowercase   =							idalabel
						__lowercase   =							{v: k for k, v in idalabel.items()}
			elif checkpoint_url[-8:-4] == "to1k":
						# fine-tuning on ImageNet-1k
						__lowercase   =							True
						__lowercase   =							1_0_0_0
						__lowercase   =							"""imagenet-1k-id2label.json"""
						__lowercase   =							json.load(open(hf_hub_download(lowerCamelCase_						,				lowerCamelCase_						,				repo_type='''dataset''' )						,				'''r''' ) )
						__lowercase   =							{int(lowerCamelCase_ ): v for k, v in idalabel.items()}
						__lowercase   =							idalabel
						__lowercase   =							{v: k for k, v in idalabel.items()}
						if "384" in checkpoint_url:
									__lowercase   =							3_8_4
						if "512" in checkpoint_url:
									__lowercase   =							5_1_2
			elif "ade20k" in checkpoint_url:
						# fine-tuning
						__lowercase   =							True
						__lowercase   =							1_5_0
						__lowercase   =							"""ade20k-id2label.json"""
						__lowercase   =							json.load(open(hf_hub_download(lowerCamelCase_						,				lowerCamelCase_						,				repo_type='''dataset''' )						,				'''r''' ) )
						__lowercase   =							{int(lowerCamelCase_ ): v for k, v in idalabel.items()}
						__lowercase   =							idalabel
						__lowercase   =							{v: k for k, v in idalabel.items()}
						__lowercase   =							6_4_0
						__lowercase   =							True
			else:
						raise ValueError('''Checkpoint not supported, URL should either end with \'pt22k\', \'ft22k\', \'to1k\' or \'ade20k\'''' )

			# size of the architecture
			if "base" in checkpoint_url:
						pass
			elif "large" in checkpoint_url:
						__lowercase   =							1_0_2_4
						__lowercase   =							4_0_9_6
						__lowercase   =							2_4
						__lowercase   =							1_6
						if "ade20k" in checkpoint_url:
									__lowercase   =							6_4_0
									__lowercase   =							[7, 1_1, 1_5, 2_3]
			else:
						raise ValueError('''Should either find \'base\' or \'large\' in checkpoint URL''' )

			# load state_dict of original model, remove and rename some keys
			__lowercase   =							torch.hub.load_state_dict_from_url(lowerCamelCase_						,				map_location='''cpu'''						,				check_hash=lowerCamelCase_ )
			__lowercase   =							state_dict["""model"""] if """ade20k""" not in checkpoint_url else state_dict["""state_dict"""]

			__lowercase   =							create_rename_keys(lowerCamelCase_						,				has_lm_head=lowerCamelCase_						,				is_semantic=lowerCamelCase_ )
			for src, dest in rename_keys:
						rename_key(lowerCamelCase_						,				lowerCamelCase_						,				lowerCamelCase_ )
			read_in_q_k_v(lowerCamelCase_						,				lowerCamelCase_						,				has_lm_head=lowerCamelCase_						,				is_semantic=lowerCamelCase_ )
			if is_semantic:
						# add prefix to decoder keys
						for key, val in state_dict.copy().items():
									__lowercase   =							state_dict.pop(lowerCamelCase_ )
									if key.startswith('''backbone.fpn''' ):
												__lowercase   =							key.replace('''backbone.fpn'''						,				'''fpn''' )
									__lowercase   =							val

    # load HuggingFace model
			if checkpoint_url[-9:-4] == "pt22k":
						__lowercase   =							BeitForMaskedImageModeling(lowerCamelCase_ )
			elif "ade20k" in checkpoint_url:
						__lowercase   =							BeitForSemanticSegmentation(lowerCamelCase_ )
			else:
						__lowercase   =							BeitForImageClassification(lowerCamelCase_ )
			model.eval()
			model.load_state_dict(lowerCamelCase_ )

			# Check outputs on an image
			if is_semantic:
						__lowercase   =							BeitImageProcessor(size=config.image_size						,				do_center_crop=lowerCamelCase_ )
						__lowercase   =							load_dataset('''hf-internal-testing/fixtures_ade20k'''						,				split='''test''' )
						__lowercase   =							Image.open(ds[0]['''file'''] )
			else:
						__lowercase   =							BeitImageProcessor(
						    size=config.image_size						,				resample=PILImageResampling.BILINEAR						,				do_center_crop=lowerCamelCase_ )
						__lowercase   =							prepare_img()

			__lowercase   =							image_processor(images=lowerCamelCase_						,				return_tensors='''pt''' )
			__lowercase   =							encoding["""pixel_values"""]

			__lowercase   =							model(lowerCamelCase_ )
			__lowercase   =							outputs.logits

			# verify logits
			__lowercase   =							torch.Size([1, 1_0_0_0] )
			if checkpoint_url[:-4].endswith('''beit_base_patch16_224_pt22k''' ):
						__lowercase   =							torch.Size([1, 1_9_6, 8_1_9_2] )
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_224_pt22k''' ):
						__lowercase   =							torch.Size([1, 1_9_6, 8_1_9_2] )
			elif checkpoint_url[:-4].endswith('''beit_base_patch16_224_pt22k_ft22k''' ):
						__lowercase   =							torch.Size([1, 2_1_8_4_1] )
						__lowercase   =							torch.tensor([2.22_88, 2.46_71, 0.73_95] )
						__lowercase   =							2_3_9_7
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_224_pt22k_ft22k''' ):
						__lowercase   =							torch.Size([1, 2_1_8_4_1] )
						__lowercase   =							torch.tensor([1.68_81, -0.27_87, 0.59_01] )
						__lowercase   =							2_3_9_6
			elif checkpoint_url[:-4].endswith('''beit_base_patch16_224_pt22k_ft1k''' ):
						__lowercase   =							torch.tensor([0.12_41, 0.07_98, -0.65_69] )
						__lowercase   =							2_8_5
			elif checkpoint_url[:-4].endswith('''beit_base_patch16_224_pt22k_ft22kto1k''' ):
						__lowercase   =							torch.tensor([-1.23_85, -1.09_87, -1.01_08] )
						__lowercase   =							2_8_1
			elif checkpoint_url[:-4].endswith('''beit_base_patch16_384_pt22k_ft22kto1k''' ):
						__lowercase   =							torch.tensor([-1.53_03, -0.94_84, -0.31_47] )
						__lowercase   =							7_6_1
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_224_pt22k_ft1k''' ):
						__lowercase   =							torch.tensor([0.46_10, -0.09_28, 0.20_86] )
						__lowercase   =							7_6_1
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_224_pt22k_ft22kto1k''' ):
						__lowercase   =							torch.tensor([-0.48_04, 0.62_57, -0.18_37] )
						__lowercase   =							7_6_1
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_384_pt22k_ft22kto1k''' ):
						__lowercase   =							torch.tensor([[-0.51_22, 0.51_17, -0.21_13]] )
						__lowercase   =							7_6_1
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_512_pt22k_ft22kto1k''' ):
						__lowercase   =							torch.tensor([-0.30_62, 0.72_61, 0.48_52] )
						__lowercase   =							7_6_1
			elif checkpoint_url[:-4].endswith('''beit_base_patch16_640_pt22k_ft22ktoade20k''' ):
						__lowercase   =							(1, 1_5_0, 1_6_0, 1_6_0)
						__lowercase   =							torch.tensor(
						    [
						        [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
						        [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
						        [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
						    ] )
			elif checkpoint_url[:-4].endswith('''beit_large_patch16_640_pt22k_ft22ktoade20k''' ):
						__lowercase   =							(1, 1_5_0, 1_6_0, 1_6_0)
						__lowercase   =							torch.tensor(
						    [
						        [[-4.33_05, -2.30_49, -3.01_61], [-2.95_91, -1.53_05, -2.22_51], [-3.41_98, -1.80_04, -2.90_62]],
						        [[-5.89_22, -3.74_35, -4.39_78], [-4.20_63, -2.78_72, -3.47_55], [-4.27_91, -3.18_74, -4.16_81]],
						        [[0.98_95, 4.34_67, 4.76_63], [4.24_76, 5.68_30, 6.15_18], [4.55_50, 6.24_95, 6.51_54]],
						    ] )
			else:
						raise ValueError('''Can\'t verify logits as model is not supported''' )

			assert logits.shape == expected_shape, "Shape of logits not as expected"
			if not has_lm_head:
						if is_semantic:
									assert torch.allclose(
									    logits[0, :3, :3, :3]						,				lowerCamelCase_						,				atol=1E-3 ), "First elements of logits not as expected"
						else:
									print('''Predicted class idx:'''						,				logits.argmax(-1 ).item() )
									assert torch.allclose(
									    logits[0, :3]						,				lowerCamelCase_						,				atol=1E-3 ), "First elements of logits not as expected"
									assert logits.argmax(-1 ).item() == expected_class_idx, "Predicted class index not as expected"

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


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

	parser.add_argument(
	    '''--checkpoint_url''',
	    default='''https://conversationhub.blob.core.windows.net/beit-share-public/beit/beit_base_patch16_224_pt22k_ft22kto1k.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.'''
	)
	_SCREAMING_SNAKE_CASE    =			parser.parse_args()
	convert_beit_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)







import argparse
import os
from functools import reduce

import fairseq
import torch
from datasets import load_dataset

from transformers import WavaVecaProcessor, logging
from transformers.models.dataavec.configuration_dataavec_audio import DataaVecAudioConfig

# Copied from https://github.com/pytorch/fairseq/blob/main/examples/data2vec/models/data2vec_audio.py
from transformers.models.dataavec.dataavec_audio import DataaVecAudioModel as Dummy  # noqa: F401
from transformers.models.dataavec.modeling_dataavec_audio import DataaVecAudioForCTC, DataaVecAudioModel


logging.set_verbosity_info()
lowerCamelCase_  :       int					      =     logging.get_logger(__name__)

lowerCamelCase_  :       Optional[int]					      =     {
    """post_extract_proj""": """feature_projection.projection""",
    """models.0.layer_norm""": """feature_projection.layer_norm""",
    """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""",
    """w2v_encoder.proj""": """lm_head""",
    """mask_emb""": """masked_spec_embed""",
}
lowerCamelCase_  :       Any					      =     [
    """lm_head""",
]







def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )     ->		List[Any]:
				for attribute in key.split("""."""  ):
								UpperCamelCase_:							List[str]		=  getattr(lowerCamelCase   ,			lowerCamelCase  )

				if weight_type is not None:
								UpperCamelCase_:							Tuple		=  getattr(lowerCamelCase   ,			lowerCamelCase  ).shape
				else:
								UpperCamelCase_:							List[Any]		=  hf_pointer.shape

				if hf_shape != value.shape:
								raise ValueError(
								    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":
								UpperCamelCase_:							str		=  value
				elif weight_type == "weight_g":
								UpperCamelCase_:							List[str]		=  value
				elif weight_type == "weight_v":
								UpperCamelCase_:							Tuple		=  value
				elif weight_type == "bias":
								UpperCamelCase_:							Any		=  value
				else:
								UpperCamelCase_:							Dict		=  value

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







def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )     ->		Union[str, Any]:
				UpperCamelCase_:							Optional[int]		=  []
				UpperCamelCase_:							Tuple		=  fairseq_model.state_dict()

				if not is_headless:
								UpperCamelCase_:							Dict		=  hf_model.dataavec_audio.feature_extractor
								UpperCamelCase_:							Tuple		=  hf_model.dataavec_audio.encoder.pos_conv_embed

				else:
								UpperCamelCase_:							Tuple		=  hf_model.feature_extractor
								UpperCamelCase_:							int		=  hf_model.encoder.pos_conv_embed

				for name, value in fairseq_dict.items():
								UpperCamelCase_:							Union[str, Any]		=  False
								if "conv_layers" in name:
												load_conv_layer(
												    lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			)
												UpperCamelCase_:							Optional[Any]		=  True
								elif "pos_conv" in name:
												load_pos_conv_layer(
												    lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			)
												UpperCamelCase_:							Optional[Any]		=  True
								else:
												for key, mapped_key in MAPPING.items():
																if not is_headless:
																				UpperCamelCase_:							List[str]		=  """data2vec_audio.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
																if key in name or key.split("""w2v_model."""  )[-1] == name.split("""."""  )[0]:
																				UpperCamelCase_:							str		=  True
																				if "*" in mapped_key:
																								UpperCamelCase_:							Any		=  name.split(lowerCamelCase  )[0].split("""."""  )[-2]
																								UpperCamelCase_:							int		=  mapped_key.replace("""*"""   ,			lowerCamelCase  )
																				if "weight_g" in name:
																								UpperCamelCase_:							Tuple		=  """weight_g"""
																				elif "weight_v" in name:
																								UpperCamelCase_:							List[Any]		=  """weight_v"""
																				elif "bias" in name:
																								UpperCamelCase_:							Union[str, Any]		=  """bias"""
																				elif "weight" in name:
																								# TODO: don't match quantizer.weight_proj
																								UpperCamelCase_:							str		=  """weight"""
																				else:
																								UpperCamelCase_:							Tuple		=  None
																				set_recursively(lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )
																continue
								if not is_used:
												unused_weights.append(lowerCamelCase  )

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







def 				A__    (  lowerCamelCase   ,			lowerCamelCase  )     ->		Dict:
				UpperCamelCase_:							Union[str, Any]		=  path.split("""."""  )
				return reduce(lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )







def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )     ->		str:
				UpperCamelCase_:							Union[str, Any]		=  access_by_string(lowerCamelCase   ,			lowerCamelCase  )
				UpperCamelCase_:							str		=  hf_weight.data

				if fsq_value.shape != hf_value.shape:
								raise ValueError(F'''{full_name} has size {fsq_value.shape}, but {hf_value.shape} was found.'''  )
				UpperCamelCase_:							Optional[int]		=  fsq_value
				logger.info(F'''{full_name} was correctly initialized from {hf_weight_path}.'''  )







def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )     ->		Optional[int]:
				UpperCamelCase_:							str		=  full_name.split("""conv_layers."""  )[-1]
				UpperCamelCase_:							Union[str, Any]		=  name.split("""."""  )
				UpperCamelCase_:							Union[str, Any]		=  int(items[0]  )
				UpperCamelCase_:							Union[str, Any]		=  int(items[1]  )

				UpperCamelCase_:							Tuple		=  name.split("""."""  )[-1]
				if type_id == 0:
								UpperCamelCase_:							List[Any]		=  """conv"""
				elif type_id == 2:
								UpperCamelCase_:							Tuple		=  """layer_norm"""
				else:
								unused_weights.append(lowerCamelCase  )
								return

				set_weights(lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			F'''conv_layers.{layer_id}.{layer_type}.{weight_type}'''  )







def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase  )     ->		Optional[Any]:
				UpperCamelCase_:							Optional[int]		=  full_name.split("""pos_conv."""  )[-1]
				UpperCamelCase_:							Any		=  name.split("""."""  )
				UpperCamelCase_:							List[str]		=  int(items[0]  )
				UpperCamelCase_:							str		=  int(items[1]  )

				UpperCamelCase_:							List[str]		=  name.split("""."""  )[-1]
				if type_id != 0:
								unused_weights.append(lowerCamelCase  )
								return
				else:
								UpperCamelCase_:							Tuple		=  """conv"""

				set_weights(lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase   ,			F'''layers.{layer_id}.{layer_type}.{weight_type}'''  )







@torch.no_grad()
def 				A__    (  lowerCamelCase   ,			lowerCamelCase   ,			lowerCamelCase=None   ,			lowerCamelCase=None   ,			lowerCamelCase=True  )     ->		Dict:
				if config_path is not None:
								UpperCamelCase_:							str		=  DataaVecAudioConfig.from_pretrained(lowerCamelCase  )
				else:
								UpperCamelCase_:							Tuple		=  DataaVecAudioConfig()

				if not is_finetuned:
								# Modify final_proj layer name
								UpperCamelCase_:							Union[str, Any]		=  DataaVecAudioModel(lowerCamelCase  )
								UpperCamelCase_:							Optional[int]		=  os.path.dirname(lowerCamelCase  )

								UpperCamelCase_:							Optional[Any]		=  torch.load(lowerCamelCase  )
								UpperCamelCase_:							List[str]		=  state_dict["""model"""].pop("""final_proj.0.weight"""  )
								UpperCamelCase_:							int		=  state_dict["""model"""].pop("""final_proj.0.bias"""  )
								UpperCamelCase_:							Optional[int]		=  os.path.join(lowerCamelCase   ,			"""converted.pt"""  )
								torch.save(lowerCamelCase   ,			lowerCamelCase  )
				else:
								UpperCamelCase_:							int		=  DataaVecAudioForCTC(lowerCamelCase  )
								UpperCamelCase_:							Optional[int]		=  checkpoint_path

				def load_dataavec(lowerCamelCase  ):
								UpperCamelCase_,      UpperCamelCase_,      UpperCamelCase_:							Optional[int]		=  fairseq.checkpoint_utils.load_model_ensemble_and_task([path]  )
								return model[0].eval()

				UpperCamelCase_:							List[Any]		=  load_dataavec(lowerCamelCase  )

				recursively_load_weights(lowerCamelCase   ,			lowerCamelCase   ,			not is_finetuned  )

				UpperCamelCase_:							Tuple		=  WavaVecaProcessor.from_pretrained("""facebook/wav2vec2-large-lv60"""  )

				UpperCamelCase_:							Dict		=  load_dataset("""patrickvonplaten/librispeech_asr_dummy"""   ,			"""clean"""   ,			split="""validation"""  )
				UpperCamelCase_:							Optional[Any]		=  [x["""array"""] for x in ds[:4]["""audio"""]]

				UpperCamelCase_:							List[Any]		=  processor(lowerCamelCase   ,			return_tensors="""pt"""   ,			padding=lowerCamelCase  )

				UpperCamelCase_:							str		=  inputs.input_values
				UpperCamelCase_:							Dict		=  inputs.attention_mask
				#    input_values = inputs.input_values[:, :-1]
				#    attention_mask = inputs.attention_mask[:, :-1]

				hf_wavavec.eval()
				model.eval()
				if is_finetuned:
								UpperCamelCase_:							Optional[Any]		=  model(source=lowerCamelCase   ,			padding_mask=(1 - attention_mask)   ,			mask=lowerCamelCase   ,			features_only=lowerCamelCase  )[
								    """encoder_out"""
								].transpose(0   ,			1  )
								UpperCamelCase_:							Tuple		=  hf_wavavec(lowerCamelCase   ,			attention_mask=lowerCamelCase  )["""logits"""]

								UpperCamelCase_:							Optional[Any]		=  torch.argmax(lowerCamelCase   ,			dim=-1  )
								UpperCamelCase_:							Any		=  processor.batch_decode(lowerCamelCase  )

								print(F'''Expected Output: {ds[:4]["text"]}, Pred: {output_string}'''  )
				else:
								UpperCamelCase_:							Optional[Any]		=  model(source=lowerCamelCase   ,			padding_mask=(1 - attention_mask)   ,			mask=lowerCamelCase   ,			features_only=lowerCamelCase  )[
								    """layer_results"""
								][-1][0].transpose(0   ,			1  )
								UpperCamelCase_:							List[str]		=  hf_wavavec(lowerCamelCase   ,			attention_mask=lowerCamelCase  )["""last_hidden_state"""]

				print(our_output.shape   ,			their_output.shape  )
				UpperCamelCase_:							Optional[Any]		=  torch.max(torch.abs(our_output - their_output  )  ).item()
				print(F'''max_absolute_diff = {max_absolute_diff}'''  )  # ~ 1e-7
				UpperCamelCase_:							List[str]		=  torch.allclose(lowerCamelCase   ,			lowerCamelCase   ,			atol=1E-3  )
				print("""Do both models output the same tensors?"""   ,			"""🔥""" if success else """💩"""  )
				if not success:
								raise Exception("""Something went wRoNg"""  )

				hf_wavavec.save_pretrained(lowerCamelCase  )

				if is_finetuned:
								processor.save_pretrained(lowerCamelCase  )
				else:
								processor.feature_extractor.save_pretrained(lowerCamelCase  )


if __name__ == "__main__":
							lowerCamelCase_  :       str					      =     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_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
							parser.add_argument(
							    """--not_finetuned""", action="""store_true""", help="""Whether the model to convert is a fine-tuned model or not"""
							)
							lowerCamelCase_  :       Dict					      =     parser.parse_args()
							convert_wavaveca_checkpoint(
							    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
							)






