




import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->Dict:






					"""simple docstring"""






					__magic_name__ :							Optional[int]		       =							{}
					for name in unet.attn_processors.keys():
										__magic_name__ :							Dict		       =							None if name.endswith('''attn1.processor'''	) else unet.config.cross_attention_dim
										if name.startswith('''mid_block'''	):
															__magic_name__ :							List[Any]		       =							unet.config.block_out_channels[-1]
										elif name.startswith('''up_blocks'''	):
															__magic_name__ :							Tuple		       =							int(name[len('''up_blocks.'''	)]	)
															__magic_name__ :							Tuple		       =							list(reversed(unet.config.block_out_channels	)	)[block_id]
										elif name.startswith('''down_blocks'''	):
															__magic_name__ :							str		       =							int(name[len('''down_blocks.'''	)]	)
															__magic_name__ :							List[Any]		       =							unet.config.block_out_channels[block_id]
										__magic_name__ :							Optional[int]		       =							(
										    LoRAAttnProcessor2_0 if hasattr(UpperCAmelCase,     '''scaled_dot_product_attention'''	) else LoRAAttnProcessor
										)
										__magic_name__ :							Optional[Any]		       =							lora_attn_processor_class(
										    hidden_size=UpperCAmelCase,     cross_attention_dim=UpperCAmelCase	)
					__magic_name__ :							Tuple		       =							AttnProcsLayers(UpperCAmelCase	)
					return lora_attn_procs, unet_lora_layers







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->str:






					"""simple docstring"""






					__magic_name__ :							List[str]		       =							{}
					__magic_name__ :							List[str]		       =							(
					    LoRAAttnProcessor2_0 if hasattr(UpperCAmelCase,     '''scaled_dot_product_attention'''	) else LoRAAttnProcessor
					)
					for name, module in text_encoder.named_modules():
										if name.endswith(UpperCAmelCase	):
															__magic_name__ :							Optional[int]		       =							lora_attn_processor_class(
															    hidden_size=module.out_proj.out_features,     cross_attention_dim=UpperCAmelCase	)
					return text_lora_attn_procs







def 			lowerCAmelCase  (     UpperCAmelCase	)  ->Optional[int]:






					"""simple docstring"""






					__magic_name__ :							Dict		       =							create_text_encoder_lora_attn_procs(UpperCAmelCase	)
					__magic_name__ :							str		       =							AttnProcsLayers(UpperCAmelCase	)
					return text_encoder_lora_layers







def 			lowerCAmelCase  (     UpperCAmelCase,     UpperCAmelCase=False	)  ->int:






					"""simple docstring"""






					for _, attn_proc in text_lora_attn_procs.items():
										# set up.weights
										for layer_name, layer_module in attn_proc.named_modules():
															if layer_name.endswith('''_lora'''	):
																				__magic_name__ :							List[Any]		       =							(
																				    torch.randn_like(layer_module.up.weight	)
																				    if randn_weight
																				    else torch.zeros_like(layer_module.up.weight	)
																				)
																				__magic_name__ :							List[Any]		       =							torch.nn.Parameter(UpperCAmelCase	)





class 		A__    (       unittest.TestCase			):






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




						"""simple docstring"""






						torch.manual_seed(0						)
						__magic_name__ :							int		       =							UNetaDConditionModel(
						    block_out_channels=(32, 64)    ,					layers_per_block=2    ,					sample_size=32    ,					in_channels=4    ,					out_channels=4    ,					down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')    ,					up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')    ,					cross_attention_dim=32    ,					)
						__magic_name__ :							List[str]		       =							DDIMScheduler(
						    beta_start=0.0_0_0_8_5    ,					beta_end=0.0_1_2    ,					beta_schedule='''scaled_linear'''    ,					clip_sample=lowerCamelCase    ,					set_alpha_to_one=lowerCamelCase    ,					steps_offset=1    ,					)
						torch.manual_seed(0						)
						__magic_name__ :							Optional[int]		       =							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    ,					)
						__magic_name__ :							Optional[Any]		       =							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    ,					)
						__magic_name__ :							Optional[Any]		       =							CLIPTextModel(lowerCamelCase						)
						__magic_name__ :							Dict		       =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''						)

						__magic_name__       ,			__magic_name__ :							Union[str, Any]		       =							create_unet_lora_layers(lowerCamelCase						)
						__magic_name__ :							List[str]		       =							create_text_encoder_lora_layers(lowerCamelCase						)

						__magic_name__ :							int		       =							{
						    '''unet''': unet,
						    '''scheduler''': scheduler,
						    '''vae''': vae,
						    '''text_encoder''': text_encoder,
						    '''tokenizer''': tokenizer,
						    '''safety_checker''': None,
						    '''feature_extractor''': None,
						}
						__magic_name__ :							Tuple		       =							{
						    '''unet_lora_layers''': unet_lora_layers,
						    '''text_encoder_lora_layers''': text_encoder_lora_layers,
						    '''unet_lora_attn_procs''': unet_lora_attn_procs,
						}
						return pipeline_components, lora_components






	def 			lowercase				(     self						)				->       int:




						"""simple docstring"""






						__magic_name__ :							str		       =							1
						__magic_name__ :							Dict		       =							10
						__magic_name__ :							Any		       =							4
						__magic_name__ :							int		       =							(32, 32)

						__magic_name__ :							List[str]		       =							torch.manual_seed(0						)
						__magic_name__ :							Dict		       =							floats_tensor((batch_size, num_channels) + sizes						)
						__magic_name__ :							Any		       =							torch.randint(1    ,					lowerCamelCase    ,					size=(batch_size, sequence_length)    ,					generator=lowerCamelCase						)

						__magic_name__ :							Any		       =							{
						    '''prompt''': '''A painting of a squirrel eating a burger''',
						    '''generator''': generator,
						    '''num_inference_steps''': 2,
						    '''guidance_scale''': 6.0,
						    '''output_type''': '''numpy''',
						}

						return noise, input_ids, pipeline_inputs

						# copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Tuple		       =							77

						__magic_name__ :							Optional[int]		       =							torch.randint(2    ,					56    ,					size=(1, max_seq_length)    ,					generator=torch.manual_seed(0						)						)

						__magic_name__ :							Dict		       =							{}
						__magic_name__ :							int		       =							inputs
						return prepared_inputs






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




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							Any		       =							self.get_dummy_components()
						LoraLoaderMixin.save_lora_weights(
						    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
						self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)






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




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							Optional[int]		       =							self.get_dummy_components()
						__magic_name__ :							List[str]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							int		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__       ,			__magic_name__       ,			__magic_name__ :							Optional[int]		       =							self.get_dummy_inputs()

						__magic_name__ :							List[Any]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							str		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							str		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							str		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       str:




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							List[Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[int]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							List[Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__       ,			__magic_name__       ,			__magic_name__ :							Any		       =							self.get_dummy_inputs()

						__magic_name__ :							int		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Optional[Any]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					safe_serialization=lowerCamelCase    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.safetensors'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							Union[str, Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							lora_components['''unet_lora_attn_procs''']
						__magic_name__ :							str		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							Union[str, Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__       ,			__magic_name__       ,			__magic_name__ :							Any		       =							self.get_dummy_inputs()

						__magic_name__ :							Union[str, Any]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[str]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											__magic_name__ :							Union[str, Any]		       =							sd_pipe.unet
											unet.set_attn_processor(lowerCamelCase						)
											unet.save_attn_procs(lowerCamelCase						)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Tuple		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							List[Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






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




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							List[Any]		       =							self.get_dummy_components()
						__magic_name__ :							Tuple		       =							StableDiffusionPipeline(**lowerCamelCase						)

						__magic_name__ :							Dict		       =							self.get_dummy_tokens()

						# inference without lora
						__magic_name__ :							List[Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora.shape == (1, 77, 32)

						# create lora_attn_procs with zeroed out up.weights
						__magic_name__ :							Dict		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							int		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

						# create lora_attn_procs with randn up.weights
						__magic_name__ :							int		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							Optional[int]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert not torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							int		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)

						__magic_name__ :							Union[str, Any]		       =							self.get_dummy_tokens()

						# inference without lora
						__magic_name__ :							Union[str, Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora.shape == (1, 77, 32)

						# create lora_attn_procs with randn up.weights
						__magic_name__ :							Tuple		       =							create_text_encoder_lora_attn_procs(pipe.text_encoder						)
						set_lora_up_weights(lowerCamelCase    ,					randn_weight=lowerCamelCase						)

						# monkey patch
						pipe._modify_text_encoder(lowerCamelCase						)

						# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
						del text_attn_procs
						gc.collect()

						# inference with lora
						__magic_name__ :							Optional[int]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_with_lora.shape == (1, 77, 32)

						assert not torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "lora outputs should be different to without lora outputs"

						# remove monkey patch
						pipe._remove_text_encoder_monkey_patch()

						# inference with removed lora
						__magic_name__ :							Optional[Any]		       =							pipe.text_encoder(**lowerCamelCase						)[0]
						assert outputs_without_lora_removed.shape == (1, 77, 32)

						assert torch.allclose(
						    lowerCamelCase    ,					lowerCamelCase						), "remove lora monkey patch should restore the original outputs"






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							str		       =							self.get_dummy_components()
						__magic_name__ :							List[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							str		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__       ,			__magic_name__       ,			__magic_name__ :							List[str]		       =							self.get_dummy_inputs()

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							int		       =							lora_images[0, -3:, -3:, -1]

						__magic_name__ :							int		       =							sd_pipe(**lowerCamelCase    ,					cross_attention_kwargs={'''scale''': 0.5}						).images
						__magic_name__ :							str		       =							lora_images_with_scale[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(
						    torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)






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




						"""simple docstring"""






						with tempfile.TemporaryDirectory() as tmpdirname:
											self.create_lora_weight_file(lowerCamelCase						)

											__magic_name__       ,			__magic_name__ :							Tuple		       =							self.get_dummy_components()
											__magic_name__ :							str		       =							StableDiffusionPipeline(**lowerCamelCase						)
											__magic_name__ :							Optional[int]		       =							sd_pipe.to(lowerCamelCase						)
											sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

											# check if vanilla attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					(AttnProcessor, AttnProcessor2_0)						)

            # load LoRA weight file
											sd_pipe.load_lora_weights(lowerCamelCase						)

											# check if lora attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					__magic_name__ :							Dict		       =							(
																					    LoRAAttnProcessor2_0 if hasattr(lowerCamelCase    ,					'''scaled_dot_product_attention'''						) else LoRAAttnProcessor
																					)
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)






	@unittest.skipIf(torch_device != '''cuda'''    ,					'''This test is supposed to run on GPU'''						)
	def 			lowercase				(     self						)				->       Optional[Any]:




						"""simple docstring"""






						with tempfile.TemporaryDirectory() as tmpdirname:
											self.create_lora_weight_file(lowerCamelCase						)

											__magic_name__       ,			__magic_name__ :							Any		       =							self.get_dummy_components()
											__magic_name__ :							List[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
											__magic_name__ :							Optional[int]		       =							sd_pipe.to(lowerCamelCase						)
											sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

											# enable XFormers
											sd_pipe.enable_xformers_memory_efficient_attention()

											# check if xFormers attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)

            # load LoRA weight file
											sd_pipe.load_lora_weights(lowerCamelCase						)

											# check if lora attention processors are used
											for _, module in sd_pipe.unet.named_modules():
																if isinstance(lowerCamelCase    ,					lowerCamelCase						):
																					self.assertIsInstance(module.processor    ,					lowerCamelCase						)






	@unittest.skipIf(torch_device != '''cuda'''    ,					'''This test is supposed to run on GPU'''						)
	def 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__       ,			__magic_name__ :							Union[str, Any]		       =							self.get_dummy_components()
						__magic_name__ :							Optional[Any]		       =							StableDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							Union[str, Any]		       =							sd_pipe.to(lowerCamelCase						)
						sd_pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__       ,			__magic_name__       ,			__magic_name__ :							List[Any]		       =							self.get_dummy_inputs()

						# enable XFormers
						sd_pipe.enable_xformers_memory_efficient_attention()

						__magic_name__ :							List[str]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Union[str, Any]		       =							original_images[0, -3:, -3:, -1]

						with tempfile.TemporaryDirectory() as tmpdirname:
											LoraLoaderMixin.save_lora_weights(
											    save_directory=lowerCamelCase    ,					unet_lora_layers=lora_components['''unet_lora_layers''']    ,					text_encoder_lora_layers=lora_components['''text_encoder_lora_layers''']    ,					)
											self.assertTrue(os.path.isfile(os.path.join(lowerCamelCase    ,					'''pytorch_lora_weights.bin'''						)						)						)
											sd_pipe.load_lora_weights(lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							sd_pipe(**lowerCamelCase						).images
						__magic_name__ :							Union[str, Any]		       =							lora_images[0, -3:, -3:, -1]

						# Outputs shouldn't match.
						self.assertFalse(torch.allclose(torch.from_numpy(lowerCamelCase						)    ,					torch.from_numpy(lowerCamelCase						)						)						)









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()


lowercase_		=							'''./tests/fixtures/elise_format0.mid'''





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






	def 			lowercase				(     self						)				->       Any:




						"""simple docstring"""






						torch.manual_seed(0						)
						__magic_name__ :							Optional[Any]		       =							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'''    ,					)

						__magic_name__ :							Any		       =							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'''    ,					)

						__magic_name__ :							List[Any]		       =							TaFilmDecoder(
						    input_dims=128    ,					targets_length=256    ,					max_decoder_noise_time=2_0_0_0_0.0    ,					d_model=768    ,					num_layers=1    ,					num_heads=1    ,					d_kv=4    ,					d_ff=2048    ,					dropout_rate=0.1    ,					)

						__magic_name__ :							Any		       =							DDPMScheduler()

						__magic_name__ :							Dict		       =							{
						    '''notes_encoder''': notes_encoder.eval(),
						    '''continuous_encoder''': continuous_encoder.eval(),
						    '''decoder''': decoder.eval(),
						    '''scheduler''': scheduler,
						    '''melgan''': None,
						}
						return components






	def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase=0						)				->       Dict:




						"""simple docstring"""






						if str(lowerCamelCase						).startswith('''mps'''						):
											__magic_name__ :							List[str]		       =							torch.manual_seed(lowerCamelCase						)
						else:
											__magic_name__ :							Any		       =							torch.Generator(device=lowerCamelCase						).manual_seed(lowerCamelCase						)
						__magic_name__ :							Any		       =							{
						    '''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 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Optional[Any]		       =							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
						__magic_name__ :							Union[str, Any]		       =							self.get_dummy_components()
						__magic_name__ :							str		       =							SpectrogramDiffusionPipeline(**lowerCamelCase						)
						__magic_name__ :							List[Any]		       =							pipe.to(lowerCamelCase						)
						pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							Tuple		       =							self.get_dummy_inputs(lowerCamelCase						)
						__magic_name__ :							List[str]		       =							pipe(**lowerCamelCase						)
						__magic_name__ :							Any		       =							output.audios

						__magic_name__ :							Union[str, Any]		       =							mel[0, -3:, -3:]

						assert mel_slice.shape == (3, 3)
						__magic_name__ :							Dict		       =							np.array(
						    [-1_1.5_1_2_9_2_5, -4.7_8_8_2_1_5, -0.4_6_1_7_2_9_0_5, -2.0_5_1_7_1_5, -1_0.5_3_9_1_4_7, -1_0.9_7_0_9_6_3, -9.0_9_1_6_3_4, 4.0, 4.0]						)
						assert np.abs(mel_slice.flatten() - expected_slice						).max() < 1e-2






	@skip_mps
	def 			lowercase				(     self						)				->       List[Any]:




						"""simple docstring"""






						return super().test_save_load_local()






	@skip_mps
	def 			lowercase				(     self						)				->       str:




						"""simple docstring"""






						return super().test_dict_tuple_outputs_equivalent()






	@skip_mps
	def 			lowercase				(     self						)				->       List[Any]:




						"""simple docstring"""






						return super().test_save_load_optional_components()






	@skip_mps
	def 			lowercase				(     self						)				->       Union[str, Any]:




						"""simple docstring"""






						return super().test_attention_slicing_forward_pass()






	def 			lowercase				(     self						)				->       Tuple:




						"""simple docstring"""






						pass






	def 			lowercase				(     self						)				->       int:




						"""simple docstring"""






						pass






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




						"""simple docstring"""






						return super().test_progress_bar()





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






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




						"""simple docstring"""






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






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




						"""simple docstring"""






						__magic_name__ :							List[Any]		       =							torch_device

						__magic_name__ :							Any		       =							SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''						)
						__magic_name__ :							Optional[int]		       =							pipe.melgan
						__magic_name__ :							List[str]		       =							None

						__magic_name__ :							Tuple		       =							pipe.to(lowerCamelCase						)
						pipe.set_progress_bar_config(disable=lowerCamelCase						)

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

						__magic_name__ :							int		       =							MidiProcessor()
						__magic_name__ :							List[str]		       =							processor(lowerCamelCase						)

						__magic_name__ :							Dict		       =							input_tokens[:3]
						__magic_name__ :							Any		       =							torch.manual_seed(0						)
						pipe(lowerCamelCase    ,					num_inference_steps=5    ,					generator=lowerCamelCase    ,					callback=lowerCamelCase    ,					output_type='''mel'''						)






	def 			lowercase				(     self						)				->       int:




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							torch_device

						__magic_name__ :							int		       =							SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''						)
						__magic_name__ :							str		       =							pipe.to(lowerCamelCase						)
						pipe.set_progress_bar_config(disable=lowerCamelCase						)
						__magic_name__ :							Any		       =							MidiProcessor()

						__magic_name__ :							Optional[Any]		       =							processor(lowerCamelCase						)
						# just run two denoising loops
						__magic_name__ :							Any		       =							input_tokens[:2]

						__magic_name__ :							int		       =							torch.manual_seed(0						)
						__magic_name__ :							Any		       =							pipe(lowerCamelCase    ,					num_inference_steps=2    ,					generator=lowerCamelCase						)

						__magic_name__ :							Union[str, Any]		       =							output.audios[0]

						assert abs(np.abs(lowerCamelCase						).sum() - 3_6_1_2.8_4_1						) < 1e-1






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




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							torch_device

						__magic_name__ :							str		       =							SpectrogramDiffusionPipeline.from_pretrained('''google/music-spectrogram-diffusion'''						)
						__magic_name__ :							List[Any]		       =							pipe.to(lowerCamelCase						)
						pipe.set_progress_bar_config(disable=lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							MidiProcessor()

						__magic_name__ :							str		       =							processor(lowerCamelCase						)

						# just run 4 denoising loops
						__magic_name__ :							List[str]		       =							input_tokens[:4]

						__magic_name__ :							Union[str, Any]		       =							torch.manual_seed(0						)
						__magic_name__ :							Dict		       =							pipe(lowerCamelCase    ,					num_inference_steps=100    ,					generator=lowerCamelCase						)

						__magic_name__ :							Optional[int]		       =							output.audios[0]
						assert abs(np.abs(lowerCamelCase						).sum() - 9_3_8_9.1_1_1_1						) < 5e-2



