

"""simple docstring"""


import functools
import operator

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


a       :int     =			logging.get_logger(__name__)

a       :int     =			{
    "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   __a				(UpperCamelCase_):






     '''simple docstring'''
     _SCREAMING_SNAKE_CASE    :Optional[int]							=			'wav2vec2'

     def __init__(    self						,     _a=32						,     _a=768						,     _a=12						,     _a=12						,     _a=3_072						,     _a="gelu"						,     _a=0.1						,     _a=0.1						,     _a=0.1						,     _a=0.0						,     _a=0.0						,     _a=0.1						,     _a=0.1						,     _a=0.02						,     _a=1E-5						,     _a="group"						,     _a="gelu"						,     _a=(512, 512, 512, 512, 512, 512, 512)						,     _a=(5, 2, 2, 2, 2, 2, 2)						,     _a=(10, 3, 3, 3, 3, 2, 2)						,     _a=False						,     _a=128						,     _a=16						,     _a=False						,     _a=True						,     _a=0.05						,     _a=10						,     _a=2						,     _a=0.0						,     _a=10						,     _a=0						,     _a=320						,     _a=2						,     _a=0.1						,     _a=100						,     _a=256						,     _a=256						,     _a=0.1						,     _a="sum"						,     _a=False						,     _a=False						,     _a=256						,     _a=(512, 512, 512, 512, 1_500)						,     _a=(5, 3, 3, 1, 1)						,     _a=(1, 2, 3, 1, 1)						,     _a=512						,     _a=0						,     _a=1						,     _a=2						,     _a=False						,     _a=3						,     _a=2						,     _a=3						,     _a=None						,     _a=None						,     **_a						,     )	->   Any:


           """simple docstring"""


           super().__init__(**SCREAMING_SNAKE_CASE_						,     pad_token_id=SCREAMING_SNAKE_CASE_						,     bos_token_id=SCREAMING_SNAKE_CASE_						,     eos_token_id=SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   Optional[Any]   		=						hidden_size
           SCREAMING_SNAKE_CASE__ :   Any   		=						feat_extract_norm
           SCREAMING_SNAKE_CASE__ :   int   		=						feat_extract_activation
           SCREAMING_SNAKE_CASE__ :   List[str]   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   Optional[Any]   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   int   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   Any   		=						conv_bias
           SCREAMING_SNAKE_CASE__ :   Dict   		=						num_conv_pos_embeddings
           SCREAMING_SNAKE_CASE__ :   Union[str, Any]   		=						num_conv_pos_embedding_groups
           SCREAMING_SNAKE_CASE__ :   Optional[int]   		=						len(self.conv_dim	)
           SCREAMING_SNAKE_CASE__ :   int   		=						num_hidden_layers
           SCREAMING_SNAKE_CASE__ :   Union[str, Any]   		=						intermediate_size
           SCREAMING_SNAKE_CASE__ :   str   		=						hidden_act
           SCREAMING_SNAKE_CASE__ :   Union[str, Any]   		=						num_attention_heads
           SCREAMING_SNAKE_CASE__ :   Dict   		=						hidden_dropout
           SCREAMING_SNAKE_CASE__ :   List[Any]   		=						attention_dropout
           SCREAMING_SNAKE_CASE__ :   Union[str, Any]   		=						activation_dropout
           SCREAMING_SNAKE_CASE__ :   List[str]   		=						feat_proj_dropout
           SCREAMING_SNAKE_CASE__ :   Any   		=						final_dropout
           SCREAMING_SNAKE_CASE__ :   int   		=						layerdrop
           SCREAMING_SNAKE_CASE__ :   str   		=						layer_norm_eps
           SCREAMING_SNAKE_CASE__ :   Optional[int]   		=						initializer_range
           SCREAMING_SNAKE_CASE__ :   Optional[int]   		=						vocab_size
           SCREAMING_SNAKE_CASE__ :   Union[str, Any]   		=						do_stable_layer_norm
           SCREAMING_SNAKE_CASE__ :   Dict   		=						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__ :   int   		=						mask_time_length
           SCREAMING_SNAKE_CASE__ :   Optional[Any]   		=						mask_time_min_masks
           SCREAMING_SNAKE_CASE__ :   Tuple   		=						mask_feature_prob
           SCREAMING_SNAKE_CASE__ :   str   		=						mask_feature_length
           SCREAMING_SNAKE_CASE__ :   Dict   		=						mask_feature_min_masks

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

           # ctc loss
           SCREAMING_SNAKE_CASE__ :   Optional[int]   		=						ctc_loss_reduction
           SCREAMING_SNAKE_CASE__ :   Tuple   		=						ctc_zero_infinity

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

           # SequenceClassification-specific parameter. Feel free to ignore for other classes.
           SCREAMING_SNAKE_CASE__ :   List[Any]   		=						classifier_proj_size

           # XVector-specific parameters. Feel free to ignore for other classes.
           SCREAMING_SNAKE_CASE__ :   Dict   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   List[Any]   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   int   		=						list(SCREAMING_SNAKE_CASE_	)
           SCREAMING_SNAKE_CASE__ :   List[str]   		=						xvector_output_dim





     @property
     def        _a      (    self	)	->   Dict:


           """simple docstring"""


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






import argparse

import requests
import torch
from PIL import Image

from transformers import ViTMAEConfig, ViTMAEForPreTraining, ViTMAEImageProcessor




def 							a		(				snake_case__:   List[Any]   ):


				'''simple docstring'''
				if "cls_token" in name:
								lowercase_	    =   name.replace('''cls_token'''		,  '''vit.embeddings.cls_token'''   )
				if "mask_token" in name:
								lowercase_	    =   name.replace('''mask_token'''		,  '''decoder.mask_token'''   )
				if "decoder_pos_embed" in name:
								lowercase_	    =   name.replace('''decoder_pos_embed'''		,  '''decoder.decoder_pos_embed'''   )
				if "pos_embed" in name and "decoder" not in name:
								lowercase_	    =   name.replace('''pos_embed'''		,  '''vit.embeddings.position_embeddings'''   )
				if "patch_embed.proj" in name:
								lowercase_	    =   name.replace('''patch_embed.proj'''		,  '''vit.embeddings.patch_embeddings.projection'''   )
				if "patch_embed.norm" in name:
								lowercase_	    =   name.replace('''patch_embed.norm'''		,  '''vit.embeddings.norm'''   )
				if "decoder_blocks" in name:
								lowercase_	    =   name.replace('''decoder_blocks'''		,  '''decoder.decoder_layers'''   )
				if "blocks" in name:
								lowercase_	    =   name.replace('''blocks'''		,  '''vit.encoder.layer'''   )
				if "attn.proj" in name:
								lowercase_	    =   name.replace('''attn.proj'''		,  '''attention.output.dense'''   )
				if "attn" in name:
								lowercase_	    =   name.replace('''attn'''		,  '''attention.self'''   )
				if "norm1" in name:
								lowercase_	    =   name.replace('''norm1'''		,  '''layernorm_before'''   )
				if "norm2" in name:
								lowercase_	    =   name.replace('''norm2'''		,  '''layernorm_after'''   )
				if "mlp.fc1" in name:
								lowercase_	    =   name.replace('''mlp.fc1'''		,  '''intermediate.dense'''   )
				if "mlp.fc2" in name:
								lowercase_	    =   name.replace('''mlp.fc2'''		,  '''output.dense'''   )
				if "decoder_embed" in name:
								lowercase_	    =   name.replace('''decoder_embed'''		,  '''decoder.decoder_embed'''   )
				if "decoder_norm" in name:
								lowercase_	    =   name.replace('''decoder_norm'''		,  '''decoder.decoder_norm'''   )
				if "decoder_pred" in name:
								lowercase_	    =   name.replace('''decoder_pred'''		,  '''decoder.decoder_pred'''   )
				if "norm.weight" in name and "decoder" not in name:
								lowercase_	    =   name.replace('''norm.weight'''		,  '''vit.layernorm.weight'''   )
				if "norm.bias" in name and "decoder" not in name:
								lowercase_	    =   name.replace('''norm.bias'''		,  '''vit.layernorm.bias'''   )

				return name




def 							a		(				snake_case__:   Optional[int]		,  snake_case__:   Any   ):


				'''simple docstring'''
				for key in orig_state_dict.copy().keys():
								lowercase_	    =   orig_state_dict.pop(snake_case__   )

								if "qkv" in key:
												lowercase_	    =   key.split('''.'''   )
												lowercase_	    =   int(key_split[1]   )
												if "decoder_blocks" in key:
																lowercase_	    =   config.decoder_hidden_size
																lowercase_	    =   '''decoder.decoder_layers.'''
																if "weight" in key:
																				lowercase_	    =   val[:dim, :]
																				lowercase_	    =   val[dim : dim * 2, :]
																				lowercase_	    =   val[-dim:, :]
																elif "bias" in key:
																				lowercase_	    =   val[:dim]
																				lowercase_	    =   val[dim : dim * 2]
																				lowercase_	    =   val[-dim:]
												else:
																lowercase_	    =   config.hidden_size
																lowercase_	    =   '''vit.encoder.layer.'''
																if "weight" in key:
																				lowercase_	    =   val[:dim, :]
																				lowercase_	    =   val[dim : dim * 2, :]
																				lowercase_	    =   val[-dim:, :]
																elif "bias" in key:
																				lowercase_	    =   val[:dim]
																				lowercase_	    =   val[dim : dim * 2]
																				lowercase_	    =   val[-dim:]

								else:
												lowercase_	    =   val

				return orig_state_dict




def 							a		(				snake_case__:   str		,  snake_case__:   int   ):


				'''simple docstring'''
				lowercase_	    =   ViTMAEConfig()
				if "large" in checkpoint_url:
								lowercase_	    =   1_024
								lowercase_	    =   4_096
								lowercase_	    =   24
								lowercase_	    =   16
				elif "huge" in checkpoint_url:
								lowercase_	    =   14
								lowercase_	    =   1_280
								lowercase_	    =   5_120
								lowercase_	    =   32
								lowercase_	    =   16

				lowercase_	    =   ViTMAEForPreTraining(snake_case__   )

				lowercase_	    =   torch.hub.load_state_dict_from_url(snake_case__		,  map_location='''cpu'''   )['''model''']

				lowercase_	    =   ViTMAEImageProcessor(size=config.image_size   )

				lowercase_	    =   convert_state_dict(snake_case__		,  snake_case__   )

				model.load_state_dict(snake_case__   )
				model.eval()

				lowercase_	    =   '''https://user-images.githubusercontent.com/11435359/147738734-196fd92f-9260-48d5-ba7e-bf103d29364d.jpg'''

				lowercase_	    =   Image.open(requests.get(snake_case__		,  stream=snake_case__   ).raw   )
				lowercase_	    =   ViTMAEImageProcessor(size=config.image_size   )
				lowercase_	    =   image_processor(images=snake_case__		,  return_tensors='''pt'''   )

				# forward pass
				torch.manual_seed(2   )
				lowercase_	    =   model(**snake_case__   )
				lowercase_	    =   outputs.logits

				if "large" in checkpoint_url:
								lowercase_	    =   torch.tensor(
								    [[-0.7_3_0_9, -0.7_1_2_8, -1.0_1_6_9], [-1.0_1_6_1, -0.9_0_5_8, -1.1_8_7_8], [-1.0_4_7_8, -0.9_4_1_1, -1.1_9_1_1]]   )
				elif "huge" in checkpoint_url:
								lowercase_	    =   torch.tensor(
								    [[-1.1_5_9_9, -0.9_1_9_9, -1.2_2_2_1], [-1.1_9_5_2, -0.9_2_6_9, -1.2_3_0_7], [-1.2_1_4_3, -0.9_3_3_7, -1.2_2_6_2]]   )
				else:
								lowercase_	    =   torch.tensor(
								    [[-0.9_1_9_2, -0.8_4_8_1, -1.1_2_5_9], [-1.1_3_4_9, -1.0_0_3_4, -1.2_5_9_9], [-1.1_7_5_7, -1.0_4_2_9, -1.2_7_2_6]]   )

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

				print(F'''Saving model to {pytorch_dump_folder_path}'''   )
				model.save_pretrained(snake_case__   )

				print(F'''Saving image processor to {pytorch_dump_folder_path}'''   )
				image_processor.save_pretrained(snake_case__   )


if __name__ == "__main__":
						__a         =   argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    '--checkpoint_url',
						    default='https://dl.fbaipublicfiles.com/mae/visualize/mae_visualize_vit_base.pth',
						    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_mae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path)




