
import argparse
import json
from pathlib import Path

import torch
import torchaudio
from datasets import load_dataset
from huggingface_hub import hf_hub_download

from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification
from transformers.utils import logging


logging.set_verbosity_info()
lowercase						:      Any									= logging.get_logger(__name__)




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Optional[int])      ->					Optional[Any]:






							'''simple docstring'''





							__UpperCamelCase			:     List[str]         =      ASTConfig()

							if "10-10" in model_name:
														pass
							elif "speech-commands" in model_name:
														__UpperCamelCase			:     Optional[Any]         =      128
							elif "12-12" in model_name:
														__UpperCamelCase			:     Tuple         =      12
														__UpperCamelCase			:     Tuple         =      12
							elif "14-14" in model_name:
														__UpperCamelCase			:     str         =      14
														__UpperCamelCase			:     int         =      14
							elif "16-16" in model_name:
														__UpperCamelCase			:     int         =      16
														__UpperCamelCase			:     Any         =      16
							else:
														raise ValueError("Model not supported")

							__UpperCamelCase			:     Union[str, Any]         =      "huggingface/label-files"
							if "speech-commands" in model_name:
														__UpperCamelCase			:     List[Any]         =      35
														__UpperCamelCase			:     Optional[Any]         =      "speech-commands-v2-id2label.json"
							else:
														__UpperCamelCase			:     Dict         =      527
														__UpperCamelCase			:     Optional[int]         =      "audioset-id2label.json"

							__UpperCamelCase			:     List[Any]         =      json.load(open(hf_hub_download(_lowerCamelCase		,						_lowerCamelCase		,						repo_type="dataset")		,						"r"))
							__UpperCamelCase			:     List[str]         =      {int(_lowerCamelCase): v for k, v in idalabel.items()}
							__UpperCamelCase			:     int         =      idalabel
							__UpperCamelCase			:     Tuple         =      {v: k for k, v in idalabel.items()}

							return config




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Any)      ->					Optional[int]:






							'''simple docstring'''





							if "module.v" in name:
														__UpperCamelCase			:     str         =      name.replace("module.v"		,						"audio_spectrogram_transformer")
							if "cls_token" in name:
														__UpperCamelCase			:     str         =      name.replace("cls_token"		,						"embeddings.cls_token")
							if "dist_token" in name:
														__UpperCamelCase			:     List[str]         =      name.replace("dist_token"		,						"embeddings.distillation_token")
							if "pos_embed" in name:
														__UpperCamelCase			:     List[str]         =      name.replace("pos_embed"		,						"embeddings.position_embeddings")
							if "patch_embed.proj" in name:
														__UpperCamelCase			:     Any         =      name.replace("patch_embed.proj"		,						"embeddings.patch_embeddings.projection")
							# transformer blocks
							if "blocks" in name:
														__UpperCamelCase			:     int         =      name.replace("blocks"		,						"encoder.layer")
							if "attn.proj" in name:
														__UpperCamelCase			:     str         =      name.replace("attn.proj"		,						"attention.output.dense")
							if "attn" in name:
														__UpperCamelCase			:     str         =      name.replace("attn"		,						"attention.self")
							if "norm1" in name:
														__UpperCamelCase			:     Optional[Any]         =      name.replace("norm1"		,						"layernorm_before")
							if "norm2" in name:
														__UpperCamelCase			:     int         =      name.replace("norm2"		,						"layernorm_after")
							if "mlp.fc1" in name:
														__UpperCamelCase			:     Union[str, Any]         =      name.replace("mlp.fc1"		,						"intermediate.dense")
							if "mlp.fc2" in name:
														__UpperCamelCase			:     int         =      name.replace("mlp.fc2"		,						"output.dense")
							# final layernorm
							if "audio_spectrogram_transformer.norm" in name:
														__UpperCamelCase			:     Optional[Any]         =      name.replace("audio_spectrogram_transformer.norm"		,						"audio_spectrogram_transformer.layernorm")
							# classifier head
							if "module.mlp_head.0" in name:
														__UpperCamelCase			:     Any         =      name.replace("module.mlp_head.0"		,						"classifier.layernorm")
							if "module.mlp_head.1" in name:
														__UpperCamelCase			:     Dict         =      name.replace("module.mlp_head.1"		,						"classifier.dense")

							return name




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    int		,						_lowerCamelCase				:    Optional[Any])      ->					Any:






							'''simple docstring'''





							for key in orig_state_dict.copy().keys():
														__UpperCamelCase			:     str         =      orig_state_dict.pop(_lowerCamelCase)

														if "qkv" in key:
																					__UpperCamelCase			:     str         =      key.split(".")
																					__UpperCamelCase			:     Optional[Any]         =      int(key_split[3])
																					__UpperCamelCase			:     List[str]         =      config.hidden_size
																					if "weight" in key:
																												__UpperCamelCase			:     Union[str, Any]         =      val[:dim, :]
																												__UpperCamelCase			:     Tuple         =      val[dim : dim * 2, :]
																												__UpperCamelCase			:     List[str]         =      val[-dim:, :]
																					else:
																												__UpperCamelCase			:     Optional[int]         =      val[:dim]
																												__UpperCamelCase			:     Dict         =      val[dim : dim * 2]
																												__UpperCamelCase			:     Union[str, Any]         =      val[-dim:]
														else:
																					__UpperCamelCase			:     Optional[Any]         =      val

							return orig_state_dict




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    str)      ->					Dict:






							'''simple docstring'''





							__UpperCamelCase			:     Dict         =      [
							    "module.v.head.weight",
							    "module.v.head.bias",
							    "module.v.head_dist.weight",
							    "module.v.head_dist.bias",
							]
							for k in ignore_keys:
														state_dict.pop(_lowerCamelCase		,						_lowerCamelCase)




@torch.no_grad()
def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Optional[int]		,						_lowerCamelCase				:    Dict		,						_lowerCamelCase				:    Optional[Any]=False)      ->					Tuple:






							'''simple docstring'''





							__UpperCamelCase			:     Optional[Any]         =      get_audio_spectrogram_transformer_config(_lowerCamelCase)

							__UpperCamelCase			:     Any         =      {
							    "ast-finetuned-audioset-10-10-0.4593": (
							        "https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-10-10-0.450": (
							        "https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-10-10-0.448": (
							        "https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-10-10-0.448-v2": (
							        "https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-12-12-0.447": (
							        "https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-14-14-0.443": (
							        "https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1"
							    ),
							    "ast-finetuned-audioset-16-16-0.442": (
							        "https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1"
							    ),
							    "ast-finetuned-speech-commands-v2": (
							        "https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1"
							    ),
							}

							# load original state_dict
							__UpperCamelCase			:     str         =      model_name_to_url[model_name]
							__UpperCamelCase			:     str         =      torch.hub.load_state_dict_from_url(_lowerCamelCase		,						map_location="cpu")
							# remove some keys
							remove_keys(_lowerCamelCase)
							# rename some keys
							__UpperCamelCase			:     Union[str, Any]         =      convert_state_dict(_lowerCamelCase		,						_lowerCamelCase)

							# load 🤗 model
							__UpperCamelCase			:     Dict         =      ASTForAudioClassification(_lowerCamelCase)
							model.eval()

							model.load_state_dict(_lowerCamelCase)

							# verify outputs on dummy input
							# source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62
							__UpperCamelCase			:     Union[str, Any]         =      -4.2_6_7_7_3_9_3 if "speech-commands" not in model_name else -6.8_4_5_9_7_8
							__UpperCamelCase			:     Optional[Any]         =      4.5_6_8_9_9_7_4 if "speech-commands" not in model_name else 5.5_6_5_4_5_2_6
							__UpperCamelCase			:     List[str]         =      1_024 if "speech-commands" not in model_name else 128
							__UpperCamelCase			:     Optional[int]         =      ASTFeatureExtractor(mean=_lowerCamelCase		,						std=_lowerCamelCase		,						max_length=_lowerCamelCase)

							if "speech-commands" in model_name:
														__UpperCamelCase			:     Tuple         =      load_dataset("speech_commands"		,						"v0.02"		,						split="validation")
														__UpperCamelCase			:     Union[str, Any]         =      dataset[0]["audio"]["array"]
							else:
														__UpperCamelCase			:     Union[str, Any]         =      hf_hub_download(
														    repo_id="nielsr/audio-spectogram-transformer-checkpoint"		,						filename="sample_audio.flac"		,						repo_type="dataset"		,						)

														__UpperCamelCase     ,		__UpperCamelCase			:     Any         =      torchaudio.load(_lowerCamelCase)
														__UpperCamelCase			:     Union[str, Any]         =      waveform.squeeze().numpy()

							__UpperCamelCase			:     Optional[int]         =      feature_extractor(_lowerCamelCase		,						sampling_rate=16_000		,						return_tensors="pt")

							# forward pass
							__UpperCamelCase			:     str         =      model(**_lowerCamelCase)
							__UpperCamelCase			:     str         =      outputs.logits

							if model_name == "ast-finetuned-audioset-10-10-0.4593":
														__UpperCamelCase			:     int         =      torch.tensor([-0.8_7_6_0, -7.0_0_4_2, -8.6_6_0_2])
							elif model_name == "ast-finetuned-audioset-10-10-0.450":
														__UpperCamelCase			:     Any         =      torch.tensor([-1.1_9_8_6, -7.0_9_0_3, -8.2_7_1_8])
							elif model_name == "ast-finetuned-audioset-10-10-0.448":
														__UpperCamelCase			:     List[Any]         =      torch.tensor([-2.6_1_2_8, -8.0_0_8_0, -9.4_3_4_4])
							elif model_name == "ast-finetuned-audioset-10-10-0.448-v2":
														__UpperCamelCase			:     Union[str, Any]         =      torch.tensor([-1.5_0_8_0, -7.4_5_3_4, -8.8_9_1_7])
							elif model_name == "ast-finetuned-audioset-12-12-0.447":
														__UpperCamelCase			:     str         =      torch.tensor([-0.5_0_5_0, -6.5_8_3_3, -8.0_8_4_3])
							elif model_name == "ast-finetuned-audioset-14-14-0.443":
														__UpperCamelCase			:     Optional[int]         =      torch.tensor([-0.3_8_2_6, -7.0_3_3_6, -8.2_4_1_3])
							elif model_name == "ast-finetuned-audioset-16-16-0.442":
														__UpperCamelCase			:     Optional[Any]         =      torch.tensor([-1.2_1_1_3, -6.9_1_0_1, -8.3_4_7_0])
							elif model_name == "ast-finetuned-speech-commands-v2":
														__UpperCamelCase			:     List[Any]         =      torch.tensor([6.1_5_8_9, -8.0_5_6_6, -8.7_9_8_4])
							else:
														raise ValueError("Unknown model name")
							if not torch.allclose(logits[0, :3]		,						_lowerCamelCase		,						atol=1e-4):
														raise ValueError("Logits don't match")
							print("Looks ok!")

							if pytorch_dump_folder_path is not None:
														Path(_lowerCamelCase).mkdir(exist_ok=_lowerCamelCase)
														print(F'Saving model {model_name} to {pytorch_dump_folder_path}')
														model.save_pretrained(_lowerCamelCase)
														print(F'Saving feature extractor to {pytorch_dump_folder_path}')
														feature_extractor.save_pretrained(_lowerCamelCase)

							if push_to_hub:
														print("Pushing model and feature extractor to the hub...")
														model.push_to_hub(F'MIT/{model_name}')
														feature_extractor.push_to_hub(F'MIT/{model_name}')


if __name__ == "__main__":
		lowercase						:      str									= argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '--model_name',
		    default='ast-finetuned-audioset-10-10-0.4593',
		    type=str,
		    help='Name of the Audio Spectrogram Transformer 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.'
		)
		parser.add_argument(
		    '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
		)

		lowercase						:      List[Any]									= parser.parse_args()
		convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)

from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Sequence[float]		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int)      ->					tuple[int | None, int | None, float]:






							'''simple docstring'''





							if not arr:
														return None, None, 0
							if low == high:
														return low, high, arr[low]

							__UpperCamelCase			:     Tuple         =      (low + high) // 2
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Dict         =      max_subarray(_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase)
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Optional[int]         =      max_subarray(_lowerCamelCase		,						mid + 1		,						_lowerCamelCase)
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Dict         =      max_cross_sum(_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase)
							if left_sum >= right_sum and left_sum >= cross_sum:
														return left_low, left_high, left_sum
							elif right_sum >= left_sum and right_sum >= cross_sum:
														return right_low, right_high, right_sum
							return cross_left, cross_right, cross_sum




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Sequence[float]		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int)      ->					tuple[int, int, float]:






							'''simple docstring'''





							__UpperCamelCase     ,		__UpperCamelCase			:     Union[str, Any]         =      float("-inf"), -1
							__UpperCamelCase     ,		__UpperCamelCase			:     Optional[int]         =      float("-inf"), -1

							__UpperCamelCase			:     int | float         =      0
							for i in range(_lowerCamelCase		,						low - 1		,						-1):
														summ += arr[i]
														if summ > left_sum:
																					__UpperCamelCase			:     Tuple         =      summ
																					__UpperCamelCase			:     Optional[int]         =      i

							__UpperCamelCase			:     Union[str, Any]         =      0
							for i in range(mid + 1		,						high + 1):
														summ += arr[i]
														if summ > right_sum:
																					__UpperCamelCase			:     Union[str, Any]         =      summ
																					__UpperCamelCase			:     Optional[int]         =      i

							return max_left, max_right, (left_sum + right_sum)




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    int)      ->					float:






							'''simple docstring'''





							__UpperCamelCase			:     Tuple         =      [randint(1		,						_lowerCamelCase) for _ in range(_lowerCamelCase)]
							__UpperCamelCase			:     Optional[Any]         =      time.time()
							max_subarray(_lowerCamelCase		,						0		,						input_size - 1)
							__UpperCamelCase			:     Any         =      time.time()
							return end - start




def 			_SCREAMING_SNAKE_CASE      (	)      ->					None:






							'''simple docstring'''





							__UpperCamelCase			:     Dict         =      [10, 100, 1_000, 10_000, 50_000, 100_000, 200_000, 300_000, 400_000, 500_000]
							__UpperCamelCase			:     Union[str, Any]         =      [time_max_subarray(_lowerCamelCase) for input_size in input_sizes]
							print("No of Inputs\t\tTime Taken")
							for input_size, runtime in zip(_lowerCamelCase		,						_lowerCamelCase):
														print(_lowerCamelCase		,						"\t\t"		,						_lowerCamelCase)
							plt.plot(_lowerCamelCase		,						_lowerCamelCase)
							plt.xlabel("Number of Inputs")
							plt.ylabel("Time taken in seconds")
							plt.show()


if __name__ == "__main__":
		from doctest import testmod

		testmod()