


"""simple docstring"""






import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available



def 			A__		(					_UpperCAmelCase    :      str				,    _UpperCAmelCase    :      str				,    _UpperCAmelCase    :      Optional[int]				,    _UpperCAmelCase    :      List[Any]=None	)							->	List[Any]:

	'''simple docstring'''


	snake_case__  :						List[Any]		=      None
	# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
	if str(weight.dtype	) == "torch.bfloat16":
		# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
		snake_case__  :						Tuple		=      weight.view(torch.intaa	)
		snake_case__  :						Any		=      "bfloat16"
	snake_case__  :						Any		=      weight.cpu().numpy()
	snake_case__  :						Union[str, Any]		=      os.path.join(a__				,    F"""{weight_name}.dat"""	)
	if index is not None:
		if dtype is None:
			snake_case__  :						List[Any]		=      str(array.dtype	)
		snake_case__  :						Dict		=      {"dtype": dtype, "shape": list(array.shape	)}
	if array.ndim == 0:
		snake_case__  :						str		=      array[None]
	snake_case__  :						str		=      np.memmap(a__				,    dtype=array.dtype				,    mode="w+"				,    shape=array.shape	)
	snake_case__  :						Any		=      array[:]
	file_array.flush()
	return index



def 			A__		(					_UpperCAmelCase    :      List[Any]				,    _UpperCAmelCase    :      Any	)							->	List[Any]:

	'''simple docstring'''


	snake_case__  :						Union[str, Any]		=      tuple(weight_info["shape"]	)
	if shape == ():
		# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
		snake_case__  :						Optional[Any]		=      (1,)

	snake_case__  :						List[Any]		=      weight_info["dtype"]
	if dtype == "bfloat16":
		# NumPy does not support bfloat16 so this was saved as a int16
		snake_case__  :						List[Any]		=      "int16"

	snake_case__  :						str		=      np.memmap(a__				,    dtype=a__				,    shape=a__				,    mode="r"	)

	if len(weight_info["shape"]	) == 0:
		snake_case__  :						Tuple		=      weight[0]
	snake_case__  :						Union[str, Any]		=      torch.tensor(a__	)
	if weight_info["dtype"] == "bfloat16":
		snake_case__  :						str		=      weight.view(torch.bfloataa	)

	return weight



def 			A__		(					_UpperCAmelCase    :      List[str]				,    _UpperCAmelCase    :      Optional[int]	)							->	Optional[int]:

	'''simple docstring'''


	if index is None or len(a__	) == 0:
		# Nothing to save
		return

	snake_case__  :						Optional[int]		=      os.path.join(a__				,    "index.json"	)
	if os.path.isfile(a__	):
		with open(a__				,    "r"				,    encoding="utf-8"	) as f:
			snake_case__  :						Union[str, Any]		=      json.load(a__	)
	else:
		snake_case__  :						int		=      {}
	current_index.update(a__	)

	with open(a__				,    "w"				,    encoding="utf-8"	) as f:
		json.dump(a__				,    a__				,    indent=2	)



def 			A__		(					_UpperCAmelCase    :      Union[str, os.PathLike]				,    _UpperCAmelCase    :      Dict[str, torch.Tensor]	)							->	List[Any]:

	'''simple docstring'''


	os.makedirs(a__				,    exist_ok=a__	)
	snake_case__  :						Optional[Any]		=      {}
	for name, parameter in state_dict.items():
		snake_case__  :						Dict		=      offload_weight(a__				,    a__				,    a__				,    index=a__	)

	# Update index
	save_offload_index(a__				,    a__	)


class 			SCREAMING_SNAKE_CASE_  (   __A):




							'''simple docstring'''





							def __init__(						self	,					lowerCamelCase__	,					lowerCamelCase__)   ->	Dict:







								'''simple docstring'''




								snake_case__  :						List[Any]		=      dataset
								snake_case__  :						str		=      prefix




							def __getitem__(						self	,					lowerCamelCase__)   ->	Any:







								'''simple docstring'''




								return self.dataset[f"""{self.prefix}{key}"""]




							def __iter__(						self)   ->	Optional[int]:







								'''simple docstring'''




								return iter([key for key in self.dataset if key.startswith(self.prefix)])







							def __len__(						self)   ->	List[Any]:







								'''simple docstring'''




								return len(self.dataset)


class 			SCREAMING_SNAKE_CASE_  (   __A):




							'''simple docstring'''





							def __init__(						self	,					lowerCamelCase__ = None	,					lowerCamelCase__ = None	,					lowerCamelCase__ = None	,					lowerCamelCase__=None	,					)   ->	str:







								'''simple docstring'''




								if state_dict is None and save_folder is None:
									raise ValueError("Need either a `state_dict` or a `save_folder` containing offloaded weights.")

								snake_case__  :						Union[str, Any]		=      {} if state_dict is None else state_dict
								snake_case__  :						Optional[int]		=      save_folder
								if index is None and save_folder is not None:
									with open(os.path.join(lowerCamelCase__	,					"index.json")) as f:
										snake_case__  :						Dict		=      json.load(lowerCamelCase__)
								snake_case__  :						Tuple		=      {} if index is None else index
								snake_case__  :						Dict		=      list(self.state_dict.keys())
								self.all_keys.extend([key for key in self.index if key not in self.all_keys])
								snake_case__  :						Optional[Any]		=      device




							def __getitem__(						self	,					lowerCamelCase__)   ->	List[Any]:







								'''simple docstring'''




								if key in self.state_dict:
									return self.state_dict[key]
								snake_case__  :						Any		=      self.index[key]
								if weight_info.get("safetensors_file") is not None:
									if not is_safetensors_available():
										raise ImportError("These offloaded weights require the use of safetensors: `pip install safetensors`.")

									from safetensors import safe_open

									snake_case__  :						List[Any]		=      "cpu" if self.device is None else self.device
									with safe_open(weight_info["safetensors_file"]	,					framework="pt"	,					device=lowerCamelCase__) as f:
										snake_case__  :						int		=      f.get_tensor(weight_info.get("weight_name"	,					lowerCamelCase__))

									if "dtype" in weight_info:
										return tensor.to(getattr(lowerCamelCase__	,					weight_info["dtype"]))
									else:
										return tensor

								snake_case__  :						Dict		=      os.path.join(self.save_folder	,					f"""{key}.dat""")
								return load_offloaded_weight(lowerCamelCase__	,					lowerCamelCase__)




							def __iter__(						self)   ->	Optional[Any]:







								'''simple docstring'''




								return iter(self.all_keys)







							def __len__(						self)   ->	Union[str, Any]:







								'''simple docstring'''




								return len(self.all_keys)



def 			A__		(					_UpperCAmelCase    :      Dict[str, torch.Tensor]				,    _UpperCAmelCase    :      List[str]	)							->	int:

	'''simple docstring'''


	snake_case__  :						Union[str, Any]		=      {}
	for module_name in submodule_names:
		# We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
		# submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
		result.update(
		    {
		        key: param
		        for key, param in state_dict.items()
		        if key == module_name or key.startswith(module_name + "."	)
		    }	)
	return result











'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


A				: List[str]					      =			"""./tests/fixtures/elise_format0.mid"""






@unittest.skip("""The note-seq package currently throws an error on import"""	)
class 			SCREAMING_SNAKE_CASE(							__A  , unittest.TestCase	):
       snake_case_					:	List[str]       =   SpectrogramDiffusionPipeline
       snake_case_					:	Any       =   PipelineTesterMixin.required_optional_params - {
           """callback""",
           """latents""",
           """callback_steps""",
           """output_type""",
           """num_images_per_prompt""",
       }
       snake_case_					:	int       =   False
       snake_case_					:	Tuple       =   False
       snake_case_					:	Tuple       =   TOKENS_TO_AUDIO_GENERATION_PARAMS
       snake_case_					:	Optional[int]       =   TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS

       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           torch.manual_seed(0	)
           __lowercase     							=		SpectrogramNotesEncoder(
               max_length=2048   ,						vocab_size=1536   ,						d_model=768   ,						dropout_rate=0.1   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						feed_forward_proj="""gated-gelu"""   ,						)

           __lowercase     							=		SpectrogramContEncoder(
               input_dims=128   ,						targets_context_length=256   ,						d_model=768   ,						dropout_rate=0.1   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						feed_forward_proj="""gated-gelu"""   ,						)

           __lowercase     							=		TaFilmDecoder(
               input_dims=128   ,						targets_length=256   ,						max_decoder_noise_time=2_00_00.0   ,						d_model=768   ,						num_layers=1   ,						num_heads=1   ,						d_kv=4   ,						d_ff=2048   ,						dropout_rate=0.1   ,						)

           __lowercase     							=		DDPMScheduler()

           __lowercase     							=		{
               """notes_encoder""": notes_encoder.eval(),
               """continuous_encoder""": continuous_encoder.eval(),
               """decoder""": decoder.eval(),
               """scheduler""": scheduler,
               """melgan""": None,
           }
           return components

       def        snake_case__     (       self   ,						lowerCamelCase__   ,						lowerCamelCase__=0	)       ->   Any:

           """simple docstring"""



           if str(lowerCamelCase__	).startswith("""mps"""	):
               __lowercase     							=		torch.manual_seed(lowerCamelCase__	)
           else:
               __lowercase     							=		torch.Generator(device=lowerCamelCase__	).manual_seed(lowerCamelCase__	)
           __lowercase     							=		{
               """input_tokens""": [
                   [1134, 90, 1135, 1133, 1080, 112, 1132, 1080, 1133, 1079, 133, 1132, 1079, 1133, 1] + [0] * 2033
               ],
               """generator""": generator,
               """num_inference_steps""": 4,
               """output_type""": """mel""",
           }
           return inputs

       def        snake_case__     (       self	)       ->   str:

           """simple docstring"""



           __lowercase     							=		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
           __lowercase     							=		self.get_dummy_components()
           __lowercase     							=		SpectrogramDiffusionPipeline(**lowerCamelCase__	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           __lowercase     							=		self.get_dummy_inputs(lowerCamelCase__	)
           __lowercase     							=		pipe(**lowerCamelCase__	)
           __lowercase     							=		output.audios

           __lowercase     							=		mel[0, -3:, -3:]

           assert mel_slice.shape == (3, 3)
           __lowercase     							=		np.array(
               [-11.51_29_25, -4.78_82_15, -0.46_17_29_05, -2.05_17_15, -10.53_91_47, -10.97_09_63, -9.09_16_34, 4.0, 4.0]	)
           assert np.abs(mel_slice.flatten() - expected_slice	).max() < 1E-2

       @skip_mps
       def        snake_case__     (       self	)       ->   Any:

           """simple docstring"""



           return super().test_save_load_local()

       @skip_mps
       def        snake_case__     (       self	)       ->   Tuple:

           """simple docstring"""



           return super().test_dict_tuple_outputs_equivalent()

       @skip_mps
       def        snake_case__     (       self	)       ->   Any:

           """simple docstring"""



           return super().test_save_load_optional_components()

       @skip_mps
       def        snake_case__     (       self	)       ->   List[str]:

           """simple docstring"""



           return super().test_attention_slicing_forward_pass()

       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           pass

       def        snake_case__     (       self	)       ->   Union[str, Any]:

           """simple docstring"""



           pass




       @skip_mps
       def        snake_case__     (       self	)       ->   Optional[Any]:

           """simple docstring"""



           return super().test_progress_bar()






@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class 			SCREAMING_SNAKE_CASE(							unittest.TestCase	):

       def        snake_case__     (       self	)       ->   Tuple:

           """simple docstring"""



           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()

       def        snake_case__     (       self	)       ->   List[Any]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.melgan
           __lowercase     							=		None

           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           def callback(lowerCamelCase__   ,						lowerCamelCase__	):
               # decode mel to audio
               __lowercase     							=		melgan(input_features=mel_output.astype(np.floataa	)	)[0]
               assert len(audio[0]	) == 8_1920 * (step + 1)
               # simulate that audio is played
               return audio

           __lowercase     							=		MidiProcessor()
           __lowercase     							=		processor(lowerCamelCase__	)

           __lowercase     							=		input_tokens[:3]
           __lowercase     							=		torch.manual_seed(0	)
           pipe(lowerCamelCase__   ,						num_inference_steps=5   ,						generator=lowerCamelCase__   ,						callback=lowerCamelCase__   ,						output_type="""mel"""	)

       def        snake_case__     (       self	)       ->   Union[str, Any]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)
           __lowercase     							=		MidiProcessor()

           __lowercase     							=		processor(lowerCamelCase__	)
           # just run two denoising loops
           __lowercase     							=		input_tokens[:2]

           __lowercase     							=		torch.manual_seed(0	)
           __lowercase     							=		pipe(lowerCamelCase__   ,						num_inference_steps=2   ,						generator=lowerCamelCase__	)

           __lowercase     							=		output.audios[0]

           assert abs(np.abs(lowerCamelCase__	).sum() - 36_12.8_41	) < 1E-1




       def        snake_case__     (       self	)       ->   List[str]:

           """simple docstring"""



           __lowercase     							=		torch_device

           __lowercase     							=		SpectrogramDiffusionPipeline.from_pretrained("""google/music-spectrogram-diffusion"""	)
           __lowercase     							=		pipe.to(lowerCamelCase__	)
           pipe.set_progress_bar_config(disable=lowerCamelCase__	)

           __lowercase     							=		MidiProcessor()

           __lowercase     							=		processor(lowerCamelCase__	)

           # just run 4 denoising loops
           __lowercase     							=		input_tokens[:4]

           __lowercase     							=		torch.manual_seed(0	)
           __lowercase     							=		pipe(lowerCamelCase__   ,						num_inference_steps=100   ,						generator=lowerCamelCase__	)

           __lowercase     							=		output.audios[0]
           assert abs(np.abs(lowerCamelCase__	).sum() - 93_89.11_11	) < 5E-2




