from dataclasses import dataclass
from typing import Optional, Tuple, Union

import flax
import jax.numpy as jnp
from scipy import integrate

from ..configuration_utils import ConfigMixin, register_to_config
from .scheduling_utils_flax import (
    CommonSchedulerState,
    FlaxKarrasDiffusionSchedulers,
    FlaxSchedulerMixin,
    FlaxSchedulerOutput,
    broadcast_to_shape_from_left,
)



@flax.struct.dataclass
class 					snake_case__       :
   _SCREAMING_SNAKE_CASE	: Optional[int]						    =      4_2

   # setable values
   _SCREAMING_SNAKE_CASE	: str						    =      4_2
   _SCREAMING_SNAKE_CASE	: Union[str, Any]						    =      4_2
   _SCREAMING_SNAKE_CASE	: int						    =      4_2
   _SCREAMING_SNAKE_CASE	: int						    =      None

   # running values
   _SCREAMING_SNAKE_CASE	: Optional[int]						    =      None



   @classmethod
   def 					UpperCAmelCase__       (				cls   :   Tuple			,	A__   :   str			,	A__   :   List[str]			,	A__   :   Tuple			,	A__   :   List[str]   )							->     List[Any]:




         '''simple docstring'''




         return cls(common=_lowerCAmelCase			,	init_noise_sigma=_lowerCAmelCase			,	timesteps=_lowerCAmelCase			,	sigmas=_lowerCAmelCase   )



@dataclass
class 					snake_case__       (				__lowerCamelCase							):
   _SCREAMING_SNAKE_CASE	: Any						    =      4_2



class 					snake_case__       (				__lowerCamelCase					,						__lowerCamelCase							):
   _SCREAMING_SNAKE_CASE	: int						    =      [e.name for e in FlaxKarrasDiffusionSchedulers]

   _SCREAMING_SNAKE_CASE	: Dict						    =      4_2



   @property
   def 					UpperCAmelCase__       (				self   :   List[Any]   )							->     Any:




         '''simple docstring'''




         return True



   @register_to_config
   def __init__(				self   :   Optional[Any]			,	A__   :   Any = 10_00			,	A__   :   Union[str, Any] = 0.0001			,	A__   :   List[Any] = 0.02			,	A__   :   str = "linear"			,	A__   :   str = None			,	A__   :   List[str] = "epsilon"			,	A__   :   Dict = jnp.floataa			,	)							->     str:




         '''simple docstring'''




         snake_case_     :			Optional[Any]       =       dtype



   def 					UpperCAmelCase__       (				self   :   str			,	A__   :   Optional[Any] = None   )							->     Any:




         '''simple docstring'''




         if common is None:
               snake_case_     :			Optional[int]       =       CommonSchedulerState.create(self   )

         snake_case_     :			Any       =       jnp.arange(0			,	self.config.num_train_timesteps   ).round()[::-1]
         snake_case_     :			Optional[int]       =       ((1 - common.alphas_cumprod) / common.alphas_cumprod) ** 0.5

         # standard deviation of the initial noise distribution
         snake_case_     :			List[str]       =       sigmas.max()

         return LMSDiscreteSchedulerState.create(
             common=_lowerCAmelCase			,	init_noise_sigma=_lowerCAmelCase			,	timesteps=_lowerCAmelCase			,	sigmas=_lowerCAmelCase			,	)



   def 					UpperCAmelCase__       (				self   :   Optional[int]			,	A__   :   Union[str, Any]			,	A__   :   Optional[int]			,	A__   :   int   )							->     Optional[int]:




         '''simple docstring'''




         (snake_case_		)     :			Optional[int]       =       jnp.where(state.timesteps == timestep			,	size=1   )
         snake_case_     :			Tuple       =       step_index[0]

         snake_case_     :			List[str]       =       state.sigmas[step_index]
         snake_case_     :			Optional[Any]       =       sample / ((sigma**2 + 1) ** 0.5)
         return sample



   def 					UpperCAmelCase__       (				self   :   Any			,	A__   :   int			,	A__   :   Optional[Any]			,	A__   :   str			,	A__   :   Union[str, Any]   )							->     Union[str, Any]:




         '''simple docstring'''




         def lms_derivative(A__   :   Union[str, Any]   ):
               snake_case_     :			List[Any]       =       1.0
               for k in range(_lowerCAmelCase   ):
                     if current_order == k:
                           continue
                     prod *= (tau - state.sigmas[t - k]) / (state.sigmas[t - current_order] - state.sigmas[t - k])
               return prod

         snake_case_     :			Optional[Any]       =       integrate.quad(_lowerCAmelCase			,	state.sigmas[t]			,	state.sigmas[t + 1]			,	epsrel=1E-4   )[0]

         return integrated_coeff



   def 					UpperCAmelCase__       (				self   :   Any			,	A__   :   Tuple			,	A__   :   Optional[Any]			,	A__   :   Any = ()   )							->     Any:




         '''simple docstring'''




         snake_case_     :			List[Any]       =       jnp.linspace(self.config.num_train_timesteps - 1			,	0			,	_lowerCAmelCase			,	dtype=self.dtype   )

         snake_case_     :			Tuple       =       jnp.floor(_lowerCAmelCase   ).astype(jnp.intaa   )
         snake_case_     :			Optional[Any]       =       jnp.ceil(_lowerCAmelCase   ).astype(jnp.intaa   )

         snake_case_     :			List[Any]       =       jnp.mod(_lowerCAmelCase			,	1.0   )

         snake_case_     :			int       =       ((1 - state.common.alphas_cumprod) / state.common.alphas_cumprod) ** 0.5
         snake_case_     :			Union[str, Any]       =       (1 - frac) * sigmas[low_idx] + frac * sigmas[high_idx]
         snake_case_     :			str       =       jnp.concatenate([sigmas, jnp.array([0.0]			,	dtype=self.dtype   )]   )

         snake_case_     :			str       =       timesteps.astype(jnp.intaa   )

         # initial running values
         snake_case_     :			Tuple       =       jnp.zeros((0,) + shape			,	dtype=self.dtype   )

         return state.replace(
             timesteps=_lowerCAmelCase			,	sigmas=_lowerCAmelCase			,	num_inference_steps=_lowerCAmelCase			,	derivatives=_lowerCAmelCase			,	)



   def 					UpperCAmelCase__       (				self   :   Optional[Any]			,	A__   :   int			,	A__   :   int			,	A__   :   Optional[int]			,	A__   :   Any			,	A__   :   Optional[Any] = 4			,	A__   :   Union[str, Any] = True			,	)							->     int:




         '''simple docstring'''




         if state.num_inference_steps is None:
               raise ValueError(
                   "Number of inference steps is 'None', you need to run 'set_timesteps' after creating the scheduler"   )

         snake_case_     :			Union[str, Any]       =       state.sigmas[timestep]

         # 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
         if self.config.prediction_type == "epsilon":
               snake_case_     :			Optional[Any]       =       sample - sigma * model_output
         elif self.config.prediction_type == "v_prediction":
               # * c_out + input * c_skip
               snake_case_     :			Dict       =       model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
         else:
               raise ValueError(
                   f"prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"   )

         # 2. Convert to an ODE derivative
         snake_case_     :			Dict       =       (sample - pred_original_sample) / sigma
         snake_case_     :			Union[str, Any]       =       state.replace(derivatives=jnp.append(state.derivatives			,	_lowerCAmelCase   )   )
         if len(state.derivatives   ) > order:
               snake_case_     :			Optional[int]       =       state.replace(derivatives=jnp.delete(state.derivatives			,	0   )   )

         # 3. Compute linear multistep coefficients
         snake_case_     :			Union[str, Any]       =       min(timestep + 1			,	_lowerCAmelCase   )
         snake_case_     :			List[Any]       =       [self.get_lms_coefficient(_lowerCAmelCase			,	_lowerCAmelCase			,	_lowerCAmelCase			,	_lowerCAmelCase   ) for curr_order in range(_lowerCAmelCase   )]

         # 4. Compute previous sample based on the derivatives path
         snake_case_     :			Dict       =       sample + sum(
             coeff * derivative for coeff, derivative in zip(_lowerCAmelCase			,	reversed(state.derivatives   )   )   )

         if not return_dict:
               return (prev_sample, state)

         return FlaxLMSSchedulerOutput(prev_sample=_lowerCAmelCase			,	state=_lowerCAmelCase   )



   def 					UpperCAmelCase__       (				self   :   Tuple			,	A__   :   Any			,	A__   :   List[str]			,	A__   :   str			,	A__   :   Dict			,	)							->     Tuple:




         '''simple docstring'''




         snake_case_     :			int       =       state.sigmas[timesteps].flatten()
         snake_case_     :			Dict       =       broadcast_to_shape_from_left(_lowerCAmelCase			,	noise.shape   )

         snake_case_     :			Any       =       original_samples + noise * sigma

         return noisy_samples



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




         '''simple docstring'''




         return self.config.num_train_timesteps




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


UpperCAmelCase							=       "./tests/fixtures/elise_format0.mid"



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



   def 					UpperCAmelCase__       (				self   :   Tuple   )							->     Optional[Any]:




         '''simple docstring'''




         torch.manual_seed(0   )
         snake_case_     :			List[Any]       =       SpectrogramNotesEncoder(
             max_length=20_48			,	vocab_size=15_36			,	d_model=7_68			,	dropout_rate=0.1			,	num_layers=1			,	num_heads=1			,	d_kv=4			,	d_ff=20_48			,	feed_forward_proj="gated-gelu"			,	)

         snake_case_     :			int       =       SpectrogramContEncoder(
             input_dims=1_28			,	targets_context_length=2_56			,	d_model=7_68			,	dropout_rate=0.1			,	num_layers=1			,	num_heads=1			,	d_kv=4			,	d_ff=20_48			,	feed_forward_proj="gated-gelu"			,	)

         snake_case_     :			Any       =       TaFilmDecoder(
             input_dims=1_28			,	targets_length=2_56			,	max_decoder_noise_time=2_0000.0			,	d_model=7_68			,	num_layers=1			,	num_heads=1			,	d_kv=4			,	d_ff=20_48			,	dropout_rate=0.1			,	)

         snake_case_     :			List[Any]       =       DDPMScheduler()

         snake_case_     :			List[Any]       =       {
             "notes_encoder": notes_encoder.eval(),
             "continuous_encoder": continuous_encoder.eval(),
             "decoder": decoder.eval(),
             "scheduler": scheduler,
             "melgan": None,
         }
         return components



   def 					UpperCAmelCase__       (				self   :   Dict			,	A__   :   int			,	A__   :   Dict=0   )							->     Tuple:




         '''simple docstring'''




         if str(A__   ).startswith("mps"   ):
               snake_case_     :			List[Any]       =       torch.manual_seed(A__   )
         else:
               snake_case_     :			List[Any]       =       torch.Generator(device=A__   ).manual_seed(A__   )
         snake_case_     :			Optional[int]       =       {
             "input_tokens": [
                 [11_34, 90, 11_35, 11_33, 10_80, 1_12, 11_32, 10_80, 11_33, 10_79, 1_33, 11_32, 10_79, 11_33, 1] + [0] * 20_33
             ],
             "generator": generator,
             "num_inference_steps": 4,
             "output_type": "mel",
         }
         return inputs



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




         '''simple docstring'''




         snake_case_     :			List[Any]       =       "cpu"  # ensure determinism for the device-dependent torch.Generator
         snake_case_     :			List[Any]       =       self.get_dummy_components()
         snake_case_     :			str       =       SpectrogramDiffusionPipeline(**A__   )
         snake_case_     :			Optional[int]       =       pipe.to(A__   )
         pipe.set_progress_bar_config(disable=A__   )

         snake_case_     :			List[Any]       =       self.get_dummy_inputs(A__   )
         snake_case_     :			Union[str, Any]       =       pipe(**A__   )
         snake_case_     :			str       =       output.audios

         snake_case_     :			List[Any]       =       mel[0, -3:, -3:]

         assert mel_slice.shape == (3, 3)
         snake_case_     :			Any       =       np.array(
             [-11.51_2925, -4.78_8215, -0.4617_2905, -2.05_1715, -10.53_9147, -10.97_0963, -9.09_1634, 4.0, 4.0]   )
         assert np.abs(mel_slice.flatten() - expected_slice   ).max() < 1E-2



   @skip_mps
   def 					UpperCAmelCase__       (				self   :   Any   )							->     Tuple:




         '''simple docstring'''




         return super().test_save_load_local()



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




         '''simple docstring'''




         return super().test_dict_tuple_outputs_equivalent()



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




         '''simple docstring'''




         return super().test_save_load_optional_components()



   @skip_mps
   def 					UpperCAmelCase__       (				self   :   Optional[int]   )							->     List[str]:




         '''simple docstring'''




         return super().test_attention_slicing_forward_pass()



   def 					UpperCAmelCase__       (				self   :   List[str]   )							->     List[Any]:




         '''simple docstring'''




         pass



   def 					UpperCAmelCase__       (				self   :   Dict   )							->     str:




         '''simple docstring'''




         pass



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




         '''simple docstring'''




         return super().test_progress_bar()



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



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




         '''simple docstring'''




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



   def 					UpperCAmelCase__       (				self   :   int   )							->     Dict:




         '''simple docstring'''




         snake_case_     :			Optional[int]       =       torch_device

         snake_case_     :			List[str]       =       SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"   )
         snake_case_     :			Dict       =       pipe.melgan
         snake_case_     :			Optional[Any]       =       None

         snake_case_     :			int       =       pipe.to(A__   )
         pipe.set_progress_bar_config(disable=A__   )

         def callback(A__   :   Dict			,	A__   :   Optional[Any]   ):
               # decode mel to audio
               snake_case_     :			str       =       melgan(input_features=mel_output.astype(np.floataa   )   )[0]
               assert len(audio[0]   ) == 8_19_20 * (step + 1)
               # simulate that audio is played
               return audio

         snake_case_     :			List[str]       =       MidiProcessor()
         snake_case_     :			Dict       =       processor(A__   )

         snake_case_     :			Optional[Any]       =       input_tokens[:3]
         snake_case_     :			Dict       =       torch.manual_seed(0   )
         pipe(A__			,	num_inference_steps=5			,	generator=A__			,	callback=A__			,	output_type="mel"   )



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




         '''simple docstring'''




         snake_case_     :			Any       =       torch_device

         snake_case_     :			List[Any]       =       SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"   )
         snake_case_     :			Any       =       pipe.to(A__   )
         pipe.set_progress_bar_config(disable=A__   )
         snake_case_     :			Any       =       MidiProcessor()

         snake_case_     :			List[Any]       =       processor(A__   )
         # just run two denoising loops
         snake_case_     :			str       =       input_tokens[:2]

         snake_case_     :			Tuple       =       torch.manual_seed(0   )
         snake_case_     :			int       =       pipe(A__			,	num_inference_steps=2			,	generator=A__   )

         snake_case_     :			int       =       output.audios[0]

         assert abs(np.abs(A__   ).sum() - 3612.841   ) < 1E-1



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




         '''simple docstring'''




         snake_case_     :			Union[str, Any]       =       torch_device

         snake_case_     :			Union[str, Any]       =       SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion"   )
         snake_case_     :			Tuple       =       pipe.to(A__   )
         pipe.set_progress_bar_config(disable=A__   )

         snake_case_     :			int       =       MidiProcessor()

         snake_case_     :			int       =       processor(A__   )

         # just run 4 denoising loops
         snake_case_     :			Union[str, Any]       =       input_tokens[:4]

         snake_case_     :			Optional[Any]       =       torch.manual_seed(0   )
         snake_case_     :			Optional[Any]       =       pipe(A__			,	num_inference_steps=1_00			,	generator=A__   )

         snake_case_     :			List[str]       =       output.audios[0]
         assert abs(np.abs(A__   ).sum() - 9389.1111   ) < 5E-2



