"""simple docstring"""




from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
				from PIL import Image

				from transformers import ConvNextImageProcessor
class      UpperCamelCase__:





						def __init__(							self		,__UpperCAmelCase		,__UpperCAmelCase=13		,__UpperCAmelCase=32		,__UpperCAmelCase=3		,__UpperCAmelCase=4		,__UpperCAmelCase=[10, 20, 30, 40]		,__UpperCAmelCase=[2, 2, 3, 2]		,__UpperCAmelCase=True		,__UpperCAmelCase=True		,__UpperCAmelCase=37		,__UpperCAmelCase="gelu"		,__UpperCAmelCase=10		,__UpperCAmelCase=0.0_2		,__UpperCAmelCase=3		,__UpperCAmelCase=None		,)       ->					Union[str, Any]:
													A__						  =				parent
													A__						  =				batch_size
													A__						  =				image_size
													A__						  =				num_channels
													A__						  =				num_stages
													A__						  =				hidden_sizes
													A__						  =				depths
													A__						  =				is_training
													A__						  =				use_labels
													A__						  =				intermediate_size
													A__						  =				hidden_act
													A__						  =				type_sequence_label_size
													A__						  =				initializer_range
													A__						  =				scope





						def 					snake_case__						(							self  )       ->					str:
													A__						  =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

													A__						  =				None
													if self.use_labels:
																				A__						  =				ids_tensor([self.batch_size]		,self.type_sequence_label_size  )

													A__						  =				self.get_config()

													return config, pixel_values, labels





						def 					snake_case__						(							self  )       ->					Tuple:
													return ConvNextConfig(
													    num_channels=self.num_channels		,hidden_sizes=self.hidden_sizes		,depths=self.depths		,num_stages=self.num_stages		,hidden_act=self.hidden_act		,is_decoder=lowerCamelCase__		,initializer_range=self.initializer_range		,)





						def 					snake_case__						(							self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase  )       ->					Tuple:
													A__						  =				TFConvNextModel(config=lowerCamelCase__  )
													A__						  =				model(lowerCamelCase__		,training=lowerCamelCase__  )
													# expected last hidden states: B, C, H // 32, W // 32
													self.parent.assertEqual(
													    result.last_hidden_state.shape		,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		,)





						def 					snake_case__						(							self		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase  )       ->					List[Any]:
													A__						  =				self.type_sequence_label_size
													A__						  =				TFConvNextForImageClassification(lowerCamelCase__  )
													A__						  =				model(lowerCamelCase__		,labels=lowerCamelCase__		,training=lowerCamelCase__  )
													self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.type_sequence_label_size)  )





						def 					snake_case__						(							self  )       ->					Optional[int]:
													A__						  =				self.prepare_config_and_inputs()
													A__  ,	A__  ,	A__						  =				config_and_inputs
													A__						  =				{'pixel_values': pixel_values}
													return config, inputs_dict






@require_tf
class      UpperCamelCase__(      __A		,    __A		,    unittest.TestCase			):
						lowerCAmelCase__	:    List[Any]    				=		(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
						lowerCAmelCase__	:    List[Any]    				=		(
						    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
						    if is_tf_available()
						    else {}
						)

						lowerCAmelCase__	:    List[str]    				=		False
						lowerCAmelCase__	:    List[str]    				=		False
						lowerCAmelCase__	:    List[str]    				=		False
						lowerCAmelCase__	:    Tuple    				=		False
						lowerCAmelCase__	:    Tuple    				=		False





						def 					snake_case__						(							self  )       ->					Dict:
													A__						  =				TFConvNextModelTester(self  )
													A__						  =				ConfigTester(
													    self		,config_class=lowerCamelCase__		,has_text_modality=lowerCamelCase__		,hidden_size=37		,)





						@unittest.skip(reason='ConvNext does not use inputs_embeds'  )
						def 					snake_case__						(							self  )       ->					str:
													pass





						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices('GPU'  )  ) == 0		,reason='TF does not support backprop for grouped convolutions on CPU.'		,)
						@slow
						def 					snake_case__						(							self  )       ->					List[Any]:
													super().test_keras_fit()





						@unittest.skip(reason='ConvNext does not support input and output embeddings'  )
						def 					snake_case__						(							self  )       ->					Optional[int]:
													pass





						def 					snake_case__						(							self  )       ->					int:
													A__  ,	A__						  =				self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A__						  =				model_class(lowerCamelCase__  )
																				A__						  =				inspect.signature(model.call  )
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				A__						  =				[*signature.parameters.keys()]

																				A__						  =				['pixel_values']
																				self.assertListEqual(arg_names[:1]		,lowerCamelCase__  )





						def 					snake_case__						(							self  )       ->					List[Any]:
													A__						  =				self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCamelCase__  )





						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices('GPU'  )  ) == 0		,reason='TF does not support backprop for grouped convolutions on CPU.'		,)
						def 					snake_case__						(							self  )       ->					Dict:
													super().test_dataset_conversion()





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



													def check_hidden_states_output(__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase  ):
																				A__						  =				model_class(lowerCamelCase__  )

																				A__						  =				model(**self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__  )  )
																				A__						  =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																				A__						  =				self.model_tester.num_stages
																				self.assertEqual(len(lowerCamelCase__  )		,expected_num_stages + 1  )

																				# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
																				self.assertListEqual(
																				    list(hidden_states[0].shape[-2:]  )		,[self.model_tester.image_size // 4, self.model_tester.image_size // 4]		,)

													A__  ,	A__						  =				self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				A__						  =				True
																				check_hidden_states_output(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__  )

																				# check that output_hidden_states also work using config
																				del inputs_dict["output_hidden_states"]
																				A__						  =				True

																				check_hidden_states_output(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__  )





						def 					snake_case__						(							self  )       ->					List[str]:
													A__  ,	A__						  =				self.model_tester.prepare_config_and_inputs_for_common()

													def check_equivalence(__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase		,__UpperCAmelCase={}  ):
																				A__						  =				model(lowerCamelCase__		,return_dict=lowerCamelCase__		,**lowerCamelCase__  )
																				A__						  =				model(lowerCamelCase__		,return_dict=lowerCamelCase__		,**lowerCamelCase__  ).to_tuple()

																				def recursive_check(__UpperCAmelCase		,__UpperCAmelCase  ):
																											if isinstance(lowerCamelCase__		,(List, Tuple)  ):
																																		for tuple_iterable_value, dict_iterable_value in zip(lowerCamelCase__		,lowerCamelCase__  ):
																																									recursive_check(lowerCamelCase__		,lowerCamelCase__  )
																											elif tuple_object is None:
																																		return
																											else:
																																		self.assertTrue(
																																		    all(tf.equal(lowerCamelCase__		,lowerCamelCase__  )  )		,msg=(
																																		        'Tuple and dict output are not equal. Difference:'
																																		        f''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object  )  )}'''
																																		    )		,)

																											recursive_check(lowerCamelCase__		,lowerCamelCase__  )

													for model_class in self.all_model_classes:
																				A__						  =				model_class(lowerCamelCase__  )

																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__  )
																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__  )
																				check_equivalence(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__  )

																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__		,return_labels=lowerCamelCase__  )
																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__		,return_labels=lowerCamelCase__  )
																				check_equivalence(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__  )

																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__  )
																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__  )
																				check_equivalence(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__		,{'output_hidden_states': True}  )

																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__		,return_labels=lowerCamelCase__  )
																				A__						  =				self._prepare_for_class(lowerCamelCase__		,lowerCamelCase__		,return_labels=lowerCamelCase__  )
																				check_equivalence(lowerCamelCase__		,lowerCamelCase__		,lowerCamelCase__		,{'output_hidden_states': True}  )





						def 					snake_case__						(							self  )       ->					Union[str, Any]:
													A__						  =				self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*lowerCamelCase__  )





						@slow
						def 					snake_case__						(							self  )       ->					Union[str, Any]:
													A__						  =				TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'  )
													self.assertIsNotNone(lowerCamelCase__  )






def 	UpperCAmelCase       (						):






							"""simple docstring"""





							A__						  =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
							return image


@require_tf
@require_vision
class      UpperCamelCase__(      unittest.TestCase			):





						@cached_property
						def 					snake_case__						(							self  )       ->					List[Any]:
													return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'  ) if is_vision_available() else None





						@slow
						def 					snake_case__						(							self  )       ->					Any:
													A__						  =				TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'  )

													A__						  =				self.default_image_processor
													A__						  =				prepare_img()
													A__						  =				image_processor(images=lowerCamelCase__		,return_tensors='tf'  )

													# forward pass
													A__						  =				model(**lowerCamelCase__  )

													# verify the logits
													A__						  =				tf.TensorShape((1, 10_00)  )
													self.assertEqual(outputs.logits.shape		,lowerCamelCase__  )

													A__						  =				tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]  )

													tf.debugging.assert_near(outputs.logits[0, :3]		,lowerCamelCase__		,atol=1e-4  )











'''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




