
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()
snake_case :		List[Any]							      =  logging.get_logger(__name__)






def   lowerCAmelCase_						(					_snake_case      :  Any   )				->		Dict:







	'''simple docstring'''


	__magic_name__		:		str	   =     ASTConfig()

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

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

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

	return config






def   lowerCAmelCase_						(					_snake_case      :  Optional[int]   )				->		Tuple:







	'''simple docstring'''


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

	return name






def   lowerCAmelCase_						(					_snake_case      :  Dict      ,   _snake_case      :  Optional[int]   )				->		Any:







	'''simple docstring'''


	for key in orig_state_dict.copy().keys():
		__magic_name__		:		str	   =     orig_state_dict.pop(A_   )

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

	return orig_state_dict






def   lowerCAmelCase_						(					_snake_case      :  Tuple   )				->		Dict:







	'''simple docstring'''


	__magic_name__		:		Union[str, Any]	   =     [
	    "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(A_      ,   A_   )






@torch.no_grad()
def   lowerCAmelCase_						(					_snake_case      :  Dict      ,   _snake_case      :  List[Any]      ,   _snake_case      :  Dict=False   )				->		Optional[int]:







	'''simple docstring'''


	__magic_name__		:		Optional[int]	   =     get_audio_spectrogram_transformer_config(A_   )

	__magic_name__		:		Dict	   =     {
	    "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
	__magic_name__		:		Any	   =     model_name_to_url[model_name]
	__magic_name__		:		List[Any]	   =     torch.hub.load_state_dict_from_url(A_      ,   map_location="cpu"   )
	# remove some keys
	remove_keys(A_   )
	# rename some keys
	__magic_name__		:		Union[str, Any]	   =     convert_state_dict(A_      ,   A_   )

	# load 🤗 model
	__magic_name__		:		List[str]	   =     ASTForAudioClassification(A_   )
	model.eval()

	model.load_state_dict(A_   )

	# verify outputs on dummy input
	# source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62
	__magic_name__		:		Tuple	   =     -4.2_677_393 if "speech-commands" not in model_name else -6.845_978
	__magic_name__		:		Union[str, Any]	   =     4.5_689_974 if "speech-commands" not in model_name else 5.5_654_526
	__magic_name__		:		str	   =     1024 if "speech-commands" not in model_name else 128
	__magic_name__		:		List[Any]	   =     ASTFeatureExtractor(mean=A_      ,   std=A_      ,   max_length=A_   )

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

		__magic_name__		:		Tuple	   =     torchaudio.load(A_   )
		__magic_name__		:		Optional[Any]	   =     waveform.squeeze().numpy()

	__magic_name__		:		Union[str, Any]	   =     feature_extractor(A_      ,   sampling_rate=16000      ,   return_tensors="pt"   )

	# forward pass
	__magic_name__		:		Optional[Any]	   =     model(**A_   )
	__magic_name__		:		List[str]	   =     outputs.logits

	if model_name == "ast-finetuned-audioset-10-10-0.4593":
		__magic_name__		:		Any	   =     torch.tensor([-0.8_760, -7.0_042, -8.6_602]   )
	elif model_name == "ast-finetuned-audioset-10-10-0.450":
		__magic_name__		:		Optional[int]	   =     torch.tensor([-1.1_986, -7.0_903, -8.2_718]   )
	elif model_name == "ast-finetuned-audioset-10-10-0.448":
		__magic_name__		:		List[str]	   =     torch.tensor([-2.6_128, -8.0_080, -9.4_344]   )
	elif model_name == "ast-finetuned-audioset-10-10-0.448-v2":
		__magic_name__		:		Tuple	   =     torch.tensor([-1.5_080, -7.4_534, -8.8_917]   )
	elif model_name == "ast-finetuned-audioset-12-12-0.447":
		__magic_name__		:		int	   =     torch.tensor([-0.5_050, -6.5_833, -8.0_843]   )
	elif model_name == "ast-finetuned-audioset-14-14-0.443":
		__magic_name__		:		Any	   =     torch.tensor([-0.3_826, -7.0_336, -8.2_413]   )
	elif model_name == "ast-finetuned-audioset-16-16-0.442":
		__magic_name__		:		Dict	   =     torch.tensor([-1.2_113, -6.9_101, -8.3_470]   )
	elif model_name == "ast-finetuned-speech-commands-v2":
		__magic_name__		:		Union[str, Any]	   =     torch.tensor([6.1_589, -8.0_566, -8.7_984]   )
	else:
		raise ValueError("Unknown model name"   )
	if not torch.allclose(logits[0, :3]      ,   A_      ,   atol=1E-4   ):
		raise ValueError("Logits don't match"   )
	print("Looks ok!"   )

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

	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__":
				snake_case :		Any							      =  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."
				)

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




"""simple docstring"""







import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()






class 						_A  (  _a    ,_a    ,_a    ,unittest.TestCase  ):







    """simple docstring"""




    UpperCAmelCase       :							str	=       StableDiffusionInpaintPipeline
    UpperCAmelCase       :							Optional[Any]	=       TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
    UpperCAmelCase       :							Any	=       TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
    UpperCAmelCase       :							Union[str, Any]	=       frozenset(
        []  )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
    UpperCAmelCase       :							int	=       frozenset([]  )






    def 	__snake_case							(					self				:    Dict):
      torch.manual_seed(0)
      a						:		List[Any]      							=			UNetaDConditionModel(
          block_out_channels=(32, 64)				, layers_per_block=2				, sample_size=32				, in_channels=9				, out_channels=4				, down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")				, up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")				, cross_attention_dim=32				, attention_head_dim=(2, 4)				, use_linear_projection=__UpperCAmelCase				, )
      a						:		Tuple      							=			PNDMScheduler(skip_prk_steps=__UpperCAmelCase)
      torch.manual_seed(0)
      a						:		str      							=			AutoencoderKL(
          block_out_channels=[32, 64]				, in_channels=3				, out_channels=3				, down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]				, up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]				, latent_channels=4				, sample_size=128				, )
      torch.manual_seed(0)
      a						:		List[str]      							=			CLIPTextConfig(
          bos_token_id=0				, eos_token_id=2				, hidden_size=32				, intermediate_size=37				, layer_norm_eps=1e-05				, num_attention_heads=4				, num_hidden_layers=5				, pad_token_id=1				, vocab_size=1000				, hidden_act="gelu"				, projection_dim=512				, )
      a						:		Any      							=			CLIPTextModel(__UpperCAmelCase)
      a						:		List[str]      							=			CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip")

      a						:		Optional[Any]      							=			{
          "unet": unet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "safety_checker": None,
          "feature_extractor": None,
      }
      return components






    def 	__snake_case							(					self				:    Union[str, Any]				, __UpperCAmelCase				:    Any				, __UpperCAmelCase				:    Union[str, Any]=0):
      # TODO: use tensor inputs instead of PIL, this is here just to leave the old expected_slices untouched
      a						:		Dict      							=			floats_tensor((1, 3, 32, 32)				, rng=random.Random(__UpperCAmelCase)).to(__UpperCAmelCase)
      a						:		List[str]      							=			image.cpu().permute(0				, 2				, 3				, 1)[0]
      a						:		Union[str, Any]      							=			Image.fromarray(np.uinta(__UpperCAmelCase)).convert("RGB").resize((64, 64))
      a						:		Dict      							=			Image.fromarray(np.uinta(image + 4)).convert("RGB").resize((64, 64))
      if str(__UpperCAmelCase).startswith("mps"):
        a						:		Tuple      							=			torch.manual_seed(__UpperCAmelCase)
      else:
        a						:		Tuple      							=			torch.Generator(device=__UpperCAmelCase).manual_seed(__UpperCAmelCase)
      a						:		Optional[Any]      							=			{
          "prompt": "A painting of a squirrel eating a burger",
          "image": init_image,
          "mask_image": mask_image,
          "generator": generator,
          "num_inference_steps": 2,
          "guidance_scale": 6.0,
          "output_type": "numpy",
      }
      return inputs






    def 	__snake_case							(					self				:    List[str]):
      a						:		Union[str, Any]      							=			"cpu"  # ensure determinism for the device-dependent torch.Generator
      a						:		Tuple      							=			self.get_dummy_components()
      a						:		Optional[int]      							=			StableDiffusionInpaintPipeline(**__UpperCAmelCase)
      a						:		int      							=			sd_pipe.to(__UpperCAmelCase)
      sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase)

      a						:		Any      							=			self.get_dummy_inputs(__UpperCAmelCase)
      a						:		Optional[int]      							=			sd_pipe(**__UpperCAmelCase).images
      a						:		Optional[int]      							=			image[0, -3:, -3:, -1]

      assert image.shape == (1, 64, 64, 3)
      a						:		int      							=			np.array([0.4_727, 0.5_735, 0.3_941, 0.5_446, 0.5_926, 0.4_394, 0.5_062, 0.4_654, 0.4_476])

      assert np.abs(image_slice.flatten() - expected_slice).max() < 1e-2






    def 	__snake_case							(					self				:    str):
      super().test_inference_batch_single_identical(expected_max_diff=3e-3)






@slow
@require_torch_gpu
class 						_A  (  unittest.TestCase  ):







    """simple docstring"""




    def 	__snake_case							(					self				:    Union[str, Any]):
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()






    def 	__snake_case							(					self				:    Dict):
      a						:		str      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          "/sd2-inpaint/init_image.png")
      a						:		str      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png")
      a						:		List[Any]      							=			load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"
          "/yellow_cat_sitting_on_a_park_bench.npy")

      a						:		Tuple      							=			"stabilityai/stable-diffusion-2-inpainting"
      a						:		Optional[Any]      							=			StableDiffusionInpaintPipeline.from_pretrained(__UpperCAmelCase				, safety_checker=__UpperCAmelCase)
      pipe.to(__UpperCAmelCase)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase)
      pipe.enable_attention_slicing()

      a						:		Any      							=			"Face of a yellow cat, high resolution, sitting on a park bench"

      a						:		str      							=			torch.manual_seed(0)
      a						:		Union[str, Any]      							=			pipe(
          prompt=__UpperCAmelCase				, image=__UpperCAmelCase				, mask_image=__UpperCAmelCase				, generator=__UpperCAmelCase				, output_type="np"				, )
      a						:		List[str]      							=			output.images[0]

      assert image.shape == (512, 512, 3)
      assert np.abs(expected_image - image).max() < 9e-3






    def 	__snake_case							(					self				:    Any):
      a						:		str      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          "/sd2-inpaint/init_image.png")
      a						:		Optional[Any]      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png")
      a						:		str      							=			load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"
          "/yellow_cat_sitting_on_a_park_bench_fp16.npy")

      a						:		Optional[Any]      							=			"stabilityai/stable-diffusion-2-inpainting"
      a						:		Any      							=			StableDiffusionInpaintPipeline.from_pretrained(
          __UpperCAmelCase				, torch_dtype=torch.floataa				, safety_checker=__UpperCAmelCase				, )
      pipe.to(__UpperCAmelCase)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase)
      pipe.enable_attention_slicing()

      a						:		Optional[int]      							=			"Face of a yellow cat, high resolution, sitting on a park bench"

      a						:		Dict      							=			torch.manual_seed(0)
      a						:		List[Any]      							=			pipe(
          prompt=__UpperCAmelCase				, image=__UpperCAmelCase				, mask_image=__UpperCAmelCase				, generator=__UpperCAmelCase				, output_type="np"				, )
      a						:		Optional[Any]      							=			output.images[0]

      assert image.shape == (512, 512, 3)
      assert np.abs(expected_image - image).max() < 5e-1






    def 	__snake_case							(					self				:    int):
      torch.cuda.empty_cache()
      torch.cuda.reset_max_memory_allocated()
      torch.cuda.reset_peak_memory_stats()

      a						:		Tuple      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          "/sd2-inpaint/init_image.png")
      a						:		Optional[int]      							=			load_image(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png")

      a						:		Optional[Any]      							=			"stabilityai/stable-diffusion-2-inpainting"
      a						:		Optional[int]      							=			PNDMScheduler.from_pretrained(__UpperCAmelCase				, subfolder="scheduler")
      a						:		int      							=			StableDiffusionInpaintPipeline.from_pretrained(
          __UpperCAmelCase				, safety_checker=__UpperCAmelCase				, scheduler=__UpperCAmelCase				, torch_dtype=torch.floataa				, )
      pipe.to(__UpperCAmelCase)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase)
      pipe.enable_attention_slicing(1)
      pipe.enable_sequential_cpu_offload()

      a						:		Optional[int]      							=			"Face of a yellow cat, high resolution, sitting on a park bench"

      a						:		Optional[int]      							=			torch.manual_seed(0)
      a						:		str      							=			pipe(
          prompt=__UpperCAmelCase				, image=__UpperCAmelCase				, mask_image=__UpperCAmelCase				, generator=__UpperCAmelCase				, num_inference_steps=2				, output_type="np"				, )

      a						:		int      							=			torch.cuda.max_memory_allocated()
      # make sure that less than 2.65 GB is allocated
      assert mem_bytes < 2.65 * 10**9






