
"""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 A_      :




  '''simple docstring'''







  def __init__(						self							,  lowercase_							,  lowercase_=13							,  lowercase_=32							,  lowercase_=3							,  lowercase_=4							,  lowercase_=[10, 20, 30, 40]							,  lowercase_=[2, 2, 3, 2]							,  lowercase_=True							,  lowercase_=True							,  lowercase_=37							,  lowercase_="gelu"							,  lowercase_=10							,  lowercase_=0.02							,  lowercase_=3							,  lowercase_=None							,  ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       parent
     UpperCAmelCase_   :   int     	=       batch_size
     UpperCAmelCase_   :   int     	=       image_size
     UpperCAmelCase_   :   Union[str, Any]     	=       num_channels
     UpperCAmelCase_   :   Any     	=       num_stages
     UpperCAmelCase_   :   Dict     	=       hidden_sizes
     UpperCAmelCase_   :   List[Any]     	=       depths
     UpperCAmelCase_   :   Tuple     	=       is_training
     UpperCAmelCase_   :   Union[str, Any]     	=       use_labels
     UpperCAmelCase_   :   List[str]     	=       intermediate_size
     UpperCAmelCase_   :   Union[str, Any]     	=       hidden_act
     UpperCAmelCase_   :   Tuple     	=       type_sequence_label_size
     UpperCAmelCase_   :   Any     	=       initializer_range
     UpperCAmelCase_   :   Union[str, Any]     	=       scope







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[str]     	=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

     UpperCAmelCase_   :   Any     	=       None
     if self.use_labels:
        UpperCAmelCase_   :   str     	=       ids_tensor([self.batch_size]							,  self.type_sequence_label_size    )

     UpperCAmelCase_   :   int     	=       self.get_config()

     return config, pixel_values, labels







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     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=lowercase_							,  initializer_range=self.initializer_range							,  )







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       TFConvNextModel(config=lowercase_    )
     UpperCAmelCase_   :   Tuple     	=       model(lowercase_							,  training=lowercase_    )
     # 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 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       self.type_sequence_label_size
     UpperCAmelCase_   :   int     	=       TFConvNextForImageClassification(lowercase_    )
     UpperCAmelCase_   :   int     	=       model(lowercase_							,  labels=lowercase_							,  training=lowercase_    )
     self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.type_sequence_label_size)    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Any     	=       self.prepare_config_and_inputs()
     UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :   Any     	=       config_and_inputs
     UpperCAmelCase_   :   List[Any]     	=       {"pixel_values": pixel_values}
     return config, inputs_dict




@require_tf
class A_      (lowercase__							,lowercase__							,unittest.TestCase ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		str    	=						(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
  SCREAMING_SNAKE_CASE__     :		Dict    	=						(
      {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
      if is_tf_available()
      else {}
  )

  SCREAMING_SNAKE_CASE__     :		Tuple    	=						False
  SCREAMING_SNAKE_CASE__     :		Union[str, Any]    	=						False
  SCREAMING_SNAKE_CASE__     :		Union[str, Any]    	=						False
  SCREAMING_SNAKE_CASE__     :		str    	=						False
  SCREAMING_SNAKE_CASE__     :		Any    	=						False







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   str     	=       TFConvNextModelTester(self    )
     UpperCAmelCase_   :   Union[str, Any]     	=       ConfigTester(
         self							,  config_class=lowercase_							,  has_text_modality=lowercase_							,  hidden_size=37							,  )







  @unittest.skip(reason="ConvNext does not use inputs_embeds"    )
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     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 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     super().test_keras_fit()







  @unittest.skip(reason="ConvNext does not support input and output embeddings"    )
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     pass







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_	,  UpperCAmelCase_   :   Optional[int]     	=       self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase_   :   int     	=       ["pixel_values"]
        self.assertListEqual(arg_names[:1]							,  lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_    )







  @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 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     super().test_dataset_conversion()







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     def check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_    ):
        UpperCAmelCase_   :   Dict     	=       model_class(lowercase_    )

        UpperCAmelCase_   :   List[str]     	=       model(**self._prepare_for_class(lowercase_							,  lowercase_    )    )
        UpperCAmelCase_   :   List[Any]     	=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        UpperCAmelCase_   :   List[Any]     	=       self.model_tester.num_stages
        self.assertEqual(len(lowercase_    )							,  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]							,  )

     UpperCAmelCase_	,  UpperCAmelCase_   :   str     	=       self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        UpperCAmelCase_   :   Dict     	=       True
        check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_    )

        # check that output_hidden_states also work using config
        del inputs_dict["output_hidden_states"]
        UpperCAmelCase_   :   Any     	=       True

        check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_	,  UpperCAmelCase_   :   Union[str, Any]     	=       self.model_tester.prepare_config_and_inputs_for_common()

     def check_equivalence(lowercase_							,  lowercase_							,  lowercase_							,  lowercase_={}    ):
        UpperCAmelCase_   :   Optional[int]     	=       model(lowercase_							,  return_dict=lowercase_							,  **lowercase_    )
        UpperCAmelCase_   :   Optional[Any]     	=       model(lowercase_							,  return_dict=lowercase_							,  **lowercase_    ).to_tuple()

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

           recursive_check(lowercase_							,  lowercase_    )

     for model_class in self.all_model_classes:
        UpperCAmelCase_   :   Optional[int]     	=       model_class(lowercase_    )

        UpperCAmelCase_   :   Optional[int]     	=       self._prepare_for_class(lowercase_							,  lowercase_    )
        UpperCAmelCase_   :   int     	=       self._prepare_for_class(lowercase_							,  lowercase_    )
        check_equivalence(lowercase_							,  lowercase_							,  lowercase_    )

        UpperCAmelCase_   :   Optional[Any]     	=       self._prepare_for_class(lowercase_							,  lowercase_							,  return_labels=lowercase_    )
        UpperCAmelCase_   :   List[str]     	=       self._prepare_for_class(lowercase_							,  lowercase_							,  return_labels=lowercase_    )
        check_equivalence(lowercase_							,  lowercase_							,  lowercase_    )

        UpperCAmelCase_   :   str     	=       self._prepare_for_class(lowercase_							,  lowercase_    )
        UpperCAmelCase_   :   Tuple     	=       self._prepare_for_class(lowercase_							,  lowercase_    )
        check_equivalence(lowercase_							,  lowercase_							,  lowercase_							,  {"output_hidden_states": True}    )

        UpperCAmelCase_   :   Any     	=       self._prepare_for_class(lowercase_							,  lowercase_							,  return_labels=lowercase_    )
        UpperCAmelCase_   :   Optional[Any]     	=       self._prepare_for_class(lowercase_							,  lowercase_							,  return_labels=lowercase_    )
        check_equivalence(lowercase_							,  lowercase_							,  lowercase_							,  {"output_hidden_states": True}    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_    )







  @slow
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"    )
     self.assertIsNotNone(lowercase_    )







def       __a	(						):
   UpperCAmelCase_   :   Tuple     	=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
   return image


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




  '''simple docstring'''







  @cached_property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"    ) if is_vision_available() else None







  @slow
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"    )

     UpperCAmelCase_   :   str     	=       self.default_image_processor
     UpperCAmelCase_   :   Optional[Any]     	=       prepare_img()
     UpperCAmelCase_   :   Dict     	=       image_processor(images=lowercase_							,  return_tensors="tf"    )

     # forward pass
     UpperCAmelCase_   :   Optional[int]     	=       model(**lowercase_    )

     # verify the logits
     UpperCAmelCase_   :   List[str]     	=       tf.TensorShape((1, 1000)    )
     self.assertEqual(outputs.logits.shape							,  lowercase_    )

     UpperCAmelCase_   :   Any     	=       tf.constant([-0.02_60, -0.47_39, 0.19_11]    )

     tf.debugging.assert_near(outputs.logits[0, :3]							,  lowercase_							,  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__         =     '''./tests/fixtures/elise_format0.mid'''





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






						def       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									torch.manual_seed(0       )
									snake_case__          =						SpectrogramNotesEncoder(
									    max_length=2_0_4_8					,   vocab_size=1_5_3_6					,   d_model=7_6_8					,   dropout_rate=0.1					,   num_layers=1					,   num_heads=1					,   d_kv=4					,   d_ff=2_0_4_8					,   feed_forward_proj="""gated-gelu"""					,   )

									snake_case__          =						SpectrogramContEncoder(
									    input_dims=1_2_8					,   targets_context_length=2_5_6					,   d_model=7_6_8					,   dropout_rate=0.1					,   num_layers=1					,   num_heads=1					,   d_kv=4					,   d_ff=2_0_4_8					,   feed_forward_proj="""gated-gelu"""					,   )

									snake_case__          =						TaFilmDecoder(
									    input_dims=1_2_8					,   targets_length=2_5_6					,   max_decoder_noise_time=2_00_00.0					,   d_model=7_6_8					,   num_layers=1					,   num_heads=1					,   d_kv=4					,   d_ff=2_0_4_8					,   dropout_rate=0.1					,   )

									snake_case__          =						DDPMScheduler()

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






						def       __lowerCAmelCase(   self						:						Tuple					,   __UpperCamelCase						:						Any					,   __UpperCamelCase						:						str=0       ):



									'''simple docstring'''






									if str(__UpperCamelCase       ).startswith("""mps"""       ):
												snake_case__          =						torch.manual_seed(__UpperCamelCase       )
									else:
												snake_case__          =						torch.Generator(device=__UpperCamelCase       ).manual_seed(__UpperCamelCase       )
									snake_case__          =						{
									    """input_tokens""": [
									        [1_1_3_4, 9_0, 1_1_3_5, 1_1_3_3, 1_0_8_0, 1_1_2, 1_1_3_2, 1_0_8_0, 1_1_3_3, 1_0_7_9, 1_3_3, 1_1_3_2, 1_0_7_9, 1_1_3_3, 1] + [0] * 2_0_3_3
									    ],
									    """generator""": generator,
									    """num_inference_steps""": 4,
									    """output_type""": """mel""",
									}
									return inputs






						def       __lowerCAmelCase(   self						:						Optional[Any]       ):



									'''simple docstring'''






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

									snake_case__          =						self.get_dummy_inputs(__UpperCamelCase       )
									snake_case__          =						pipe(**__UpperCamelCase       )
									snake_case__          =						output.audios

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

									assert mel_slice.shape == (3, 3)
									snake_case__          =						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       __lowerCAmelCase(   self						:						Tuple       ):



									'''simple docstring'''






									return super().test_save_load_local()






						@skip_mps
						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									return super().test_dict_tuple_outputs_equivalent()






						@skip_mps
						def       __lowerCAmelCase(   self						:						Optional[int]       ):



									'''simple docstring'''






									return super().test_save_load_optional_components()






						@skip_mps
						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									return super().test_attention_slicing_forward_pass()






						def       __lowerCAmelCase(   self						:						Dict       ):



									'''simple docstring'''






									pass






						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									pass







						@skip_mps
						def       __lowerCAmelCase(   self						:						Dict       ):



									'''simple docstring'''






									return super().test_progress_bar()







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






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






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






						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






									snake_case__          =						torch_device

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

									snake_case__          =						pipe.to(__UpperCamelCase       )
									pipe.set_progress_bar_config(disable=__UpperCamelCase       )

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

									snake_case__          =						MidiProcessor()
									snake_case__          =						processor(__UpperCamelCase       )

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






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									snake_case__          =						torch_device

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

									snake_case__          =						processor(__UpperCamelCase       )
									# just run two denoising loops
									snake_case__          =						input_tokens[:2]

									snake_case__          =						torch.manual_seed(0       )
									snake_case__          =						pipe(__UpperCamelCase					,   num_inference_steps=2					,   generator=__UpperCamelCase       )

									snake_case__          =						output.audios[0]

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







						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






									snake_case__          =						torch_device

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

									snake_case__          =						MidiProcessor()

									snake_case__          =						processor(__UpperCamelCase       )

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

									snake_case__          =						torch.manual_seed(0       )
									snake_case__          =						pipe(__UpperCamelCase					,   num_inference_steps=1_0_0					,   generator=__UpperCamelCase       )

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