


import gc
import unittest

import numpy as np
import torch
import torch.nn.functional as F
from transformers import (
    ClapTextConfig,
    ClapTextModelWithProjection,
    RobertaTokenizer,
    SpeechTaHifiGan,
    SpeechTaHifiGanConfig,
)

from diffusers import (
    AudioLDMPipeline,
    AutoencoderKL,
    DDIMScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class A  (   UpperCamelCase_    ,							unittest.TestCase     ):
      UpperCamelCase__							:  Union[str, Any]      =AudioLDMPipeline
      UpperCamelCase__							:  int      =TEXT_TO_AUDIO_PARAMS
      UpperCamelCase__							:  Any      =TEXT_TO_AUDIO_BATCH_PARAMS
      UpperCamelCase__							:  Dict      =frozenset(
          [
              'num_inference_steps',
              'num_waveforms_per_prompt',
              'generator',
              'latents',
              'output_type',
              'return_dict',
              'callback',
              'callback_steps',
          ]     )






      def        lowerCamelCase       (					self						:     Dict )					->						str:
             """simple docstring"""




             torch.manual_seed(0 )
             _lowerCamelCase       :		Optional[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, 64)			,   class_embed_type='simple_projection'			,   projection_class_embeddings_input_dim=32			,   class_embeddings_concat=lowercase_			,   )
             _lowerCamelCase       :		Optional[Any]		=DDIMScheduler(
                 beta_start=0.00085			,   beta_end=0.012			,   beta_schedule='scaled_linear'			,   clip_sample=lowercase_			,   set_alpha_to_one=lowercase_			,   )
             torch.manual_seed(0 )
             _lowerCamelCase       :		Union[str, Any]		=AutoencoderKL(
                 block_out_channels=[32, 64]			,   in_channels=1			,   out_channels=1			,   down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']			,   up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']			,   latent_channels=4			,   )
             torch.manual_seed(0 )
             _lowerCamelCase       :		List[str]		=ClapTextConfig(
                 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			,   projection_dim=32			,   )
             _lowerCamelCase       :		Union[str, Any]		=ClapTextModelWithProjection(lowercase_ )
             _lowerCamelCase       :		int		=RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'			,   model_max_length=77 )

             _lowerCamelCase       :		int		=SpeechTaHifiGanConfig(
                 model_in_dim=8			,   sampling_rate=1_6000			,   upsample_initial_channel=16			,   upsample_rates=[2, 2]			,   upsample_kernel_sizes=[4, 4]			,   resblock_kernel_sizes=[3, 7]			,   resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]]			,   normalize_before=lowercase_			,   )

             _lowerCamelCase       :		Tuple		=SpeechTaHifiGan(lowercase_ )

             _lowerCamelCase       :		List[Any]		={
                 'unet': unet,
                 'scheduler': scheduler,
                 'vae': vae,
                 'text_encoder': text_encoder,
                 'tokenizer': tokenizer,
                 'vocoder': vocoder,
             }
             return components






      def        lowerCamelCase       (					self						:     Tuple			,   lowercase_						:     Dict			,   lowercase_						:     List[Any]=0 )					->						Optional[Any]:
             """simple docstring"""




             if str(lowercase_ ).startswith('mps' ):
                    _lowerCamelCase       :		List[Any]		=torch.manual_seed(lowercase_ )
             else:
                    _lowerCamelCase       :		List[str]		=torch.Generator(device=lowercase_ ).manual_seed(lowercase_ )
             _lowerCamelCase       :		Tuple		={
                 'prompt': 'A hammer hitting a wooden surface',
                 'generator': generator,
                 'num_inference_steps': 2,
                 'guidance_scale': 6.0,
             }
             return inputs






      def        lowerCamelCase       (					self						:     List[str] )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Tuple		='cpu'  # ensure determinism for the device-dependent torch.Generator

             _lowerCamelCase       :		Any		=self.get_dummy_components()
             _lowerCamelCase       :		Union[str, Any]		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Optional[Any]		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe(**lowercase_ )
             _lowerCamelCase       :		Any		=output.audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) == 256

             _lowerCamelCase       :		int		=audio[:10]
             _lowerCamelCase       :		Any		=np.array(
                 [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033] )

             assert np.abs(audio_slice - expected_slice ).max() < 1E-2






      def        lowerCamelCase       (					self						:     Any )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		str		=self.get_dummy_components()
             _lowerCamelCase       :		List[Any]		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		Any		=audioldm_pipe.to(lowercase_ )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Tuple		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		Tuple		=3 * [inputs['prompt']]

             # forward
             _lowerCamelCase       :		Union[str, Any]		=audioldm_pipe(**lowercase_ )
             _lowerCamelCase       :		Any		=output.audios[0]

             _lowerCamelCase       :		Any		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		Tuple		=3 * [inputs.pop('prompt' )]

             _lowerCamelCase       :		Dict		=audioldm_pipe.tokenizer(
                 lowercase_			,   padding='max_length'			,   max_length=audioldm_pipe.tokenizer.model_max_length			,   truncation=lowercase_			,   return_tensors='pt'			,   )
             _lowerCamelCase       :		Union[str, Any]		=text_inputs['input_ids'].to(lowercase_ )

             _lowerCamelCase       :		Optional[Any]		=audioldm_pipe.text_encoder(
                 lowercase_			,   )
             _lowerCamelCase       :		Tuple		=prompt_embeds.text_embeds
             # additional L_2 normalization over each hidden-state
             _lowerCamelCase       :		Optional[int]		=F.normalize(lowercase_			,   dim=-1 )

             _lowerCamelCase       :		List[str]		=prompt_embeds

             # forward
             _lowerCamelCase       :		List[Any]		=audioldm_pipe(**lowercase_ )
             _lowerCamelCase       :		Union[str, Any]		=output.audios[0]

             assert np.abs(audio_a - audio_a ).max() < 1E-2






      def        lowerCamelCase       (					self						:     Optional[int] )					->						int:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		=self.get_dummy_components()
             _lowerCamelCase       :		Dict		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		Optional[Any]		=audioldm_pipe.to(lowercase_ )
             _lowerCamelCase       :		List[str]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		int		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		Union[str, Any]		=3 * ['this is a negative prompt']
             _lowerCamelCase       :		Any		=negative_prompt
             _lowerCamelCase       :		str		=3 * [inputs['prompt']]

             # forward
             _lowerCamelCase       :		List[Any]		=audioldm_pipe(**lowercase_ )
             _lowerCamelCase       :		Optional[int]		=output.audios[0]

             _lowerCamelCase       :		Any		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		Optional[int]		=3 * [inputs.pop('prompt' )]

             _lowerCamelCase       :		Union[str, Any]		=[]
             for p in [prompt, negative_prompt]:
                    _lowerCamelCase       :		Any		=audioldm_pipe.tokenizer(
                        lowercase_			,   padding='max_length'			,   max_length=audioldm_pipe.tokenizer.model_max_length			,   truncation=lowercase_			,   return_tensors='pt'			,   )
                    _lowerCamelCase       :		List[str]		=text_inputs['input_ids'].to(lowercase_ )

                    _lowerCamelCase       :		int		=audioldm_pipe.text_encoder(
                        lowercase_			,   )
                    _lowerCamelCase       :		List[Any]		=text_embeds.text_embeds
                    # additional L_2 normalization over each hidden-state
                    _lowerCamelCase       :		Union[str, Any]		=F.normalize(lowercase_			,   dim=-1 )

                    embeds.append(lowercase_ )

             _lowerCamelCase	,     _lowerCamelCase       :		Optional[Any]		=embeds

             # forward
             _lowerCamelCase       :		int		=audioldm_pipe(**lowercase_ )
             _lowerCamelCase       :		Any		=output.audios[0]

             assert np.abs(audio_a - audio_a ).max() < 1E-2






      def        lowerCamelCase       (					self						:     Tuple )					->						Dict:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		='cpu'  # ensure determinism for the device-dependent torch.Generator
             _lowerCamelCase       :		Optional[Any]		=self.get_dummy_components()
             _lowerCamelCase       :		str		=PNDMScheduler(skip_prk_steps=lowercase_ )
             _lowerCamelCase       :		Dict		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		Any		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Tuple		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		int		='egg cracking'
             _lowerCamelCase       :		Dict		=audioldm_pipe(**lowercase_			,   negative_prompt=lowercase_ )
             _lowerCamelCase       :		Any		=output.audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) == 256

             _lowerCamelCase       :		Any		=audio[:10]
             _lowerCamelCase       :		Union[str, Any]		=np.array(
                 [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032] )

             assert np.abs(audio_slice - expected_slice ).max() < 1E-2






      def        lowerCamelCase       (					self						:     str )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Any		='cpu'  # ensure determinism for the device-dependent torch.Generator
             _lowerCamelCase       :		Any		=self.get_dummy_components()
             _lowerCamelCase       :		Union[str, Any]		=PNDMScheduler(skip_prk_steps=lowercase_ )
             _lowerCamelCase       :		Tuple		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		List[str]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Any		='A hammer hitting a wooden surface'

             # test num_waveforms_per_prompt=1 (default)
             _lowerCamelCase       :		List[Any]		=audioldm_pipe(lowercase_			,   num_inference_steps=2 ).audios

             assert audios.shape == (1, 256)

             # test num_waveforms_per_prompt=1 (default) for batch of prompts
             _lowerCamelCase       :		List[Any]		=2
             _lowerCamelCase       :		Any		=audioldm_pipe([prompt] * batch_size			,   num_inference_steps=2 ).audios

             assert audios.shape == (batch_size, 256)

             # test num_waveforms_per_prompt for single prompt
             _lowerCamelCase       :		str		=2
             _lowerCamelCase       :		int		=audioldm_pipe(lowercase_			,   num_inference_steps=2			,   num_waveforms_per_prompt=lowercase_ ).audios

             assert audios.shape == (num_waveforms_per_prompt, 256)

             # test num_waveforms_per_prompt for batch of prompts
             _lowerCamelCase       :		Union[str, Any]		=2
             _lowerCamelCase       :		Any		=audioldm_pipe(
                 [prompt] * batch_size			,   num_inference_steps=2			,   num_waveforms_per_prompt=lowercase_ ).audios

             assert audios.shape == (batch_size * num_waveforms_per_prompt, 256)






      def        lowerCamelCase       (					self						:     int )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[int]		='cpu'  # ensure determinism for the device-dependent torch.Generator
             _lowerCamelCase       :		Dict		=self.get_dummy_components()
             _lowerCamelCase       :		Union[str, Any]		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )
             _lowerCamelCase       :		Any		=audioldm_pipe.vocoder.config.sampling_rate

             _lowerCamelCase       :		List[str]		=self.get_dummy_inputs(lowercase_ )
             _lowerCamelCase       :		List[str]		=audioldm_pipe(audio_length_in_s=0.016			,   **lowercase_ )
             _lowerCamelCase       :		Optional[int]		=output.audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) / vocoder_sampling_rate == 0.016

             _lowerCamelCase       :		List[Any]		=audioldm_pipe(audio_length_in_s=0.032			,   **lowercase_ )
             _lowerCamelCase       :		Any		=output.audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) / vocoder_sampling_rate == 0.032






      def        lowerCamelCase       (					self						:     int )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=self.get_dummy_components()
             _lowerCamelCase       :		str		=AudioLDMPipeline(**lowercase_ )
             _lowerCamelCase       :		Any		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		str		=['hey']

             _lowerCamelCase       :		Any		=audioldm_pipe(lowercase_			,   num_inference_steps=1 )
             _lowerCamelCase       :		List[Any]		=output.audios.shape
             assert audio_shape == (1, 256)

             _lowerCamelCase       :		Optional[Any]		=audioldm_pipe.vocoder.config
             config.model_in_dim *= 2
             _lowerCamelCase       :		Optional[Any]		=SpeechTaHifiGan(lowercase_ ).to(lowercase_ )
             _lowerCamelCase       :		Optional[int]		=audioldm_pipe(lowercase_			,   num_inference_steps=1 )
             _lowerCamelCase       :		List[str]		=output.audios.shape
             # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram
             assert audio_shape == (1, 256)






      def        lowerCamelCase       (					self						:     List[Any] )					->						Any:
             """simple docstring"""




             self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowercase_ )






      def        lowerCamelCase       (					self						:     Any )					->						List[str]:
             """simple docstring"""




             self._test_inference_batch_single_identical(test_mean_pixel_difference=lowercase_ )






      @unittest.skipIf(
          torch_device != 'cuda' or not is_xformers_available()			,   reason='XFormers attention is only available with CUDA and `xformers` installed'			,   )
      def        lowerCamelCase       (					self						:     List[str] )					->						int:
             """simple docstring"""




             self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowercase_ )




@slow
class A  (   unittest.TestCase     ):






      def        lowerCamelCase       (					self						:     Dict )					->						Dict:
             """simple docstring"""




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






      def        lowerCamelCase       (					self						:     int			,   lowercase_						:     List[str]			,   lowercase_						:     Any="cpu"			,   lowercase_						:     str=torch.floataa			,   lowercase_						:     List[Any]=0 )					->						Optional[int]:
             """simple docstring"""




             _lowerCamelCase       :		Optional[Any]		=torch.Generator(device=lowercase_ ).manual_seed(lowercase_ )
             _lowerCamelCase       :		int		=np.random.RandomState(lowercase_ ).standard_normal((1, 8, 128, 16) )
             _lowerCamelCase       :		Tuple		=torch.from_numpy(lowercase_ ).to(device=lowercase_			,   dtype=lowercase_ )
             _lowerCamelCase       :		List[str]		={
                 'prompt': 'A hammer hitting a wooden surface',
                 'latents': latents,
                 'generator': generator,
                 'num_inference_steps': 3,
                 'guidance_scale': 2.5,
             }
             return inputs






      def        lowerCamelCase       (					self						:     List[str] )					->						Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase       :		str		=AudioLDMPipeline.from_pretrained('cvssp/audioldm' )
             _lowerCamelCase       :		int		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Optional[Any]		=self.get_inputs(lowercase_ )
             _lowerCamelCase       :		Dict		=25
             _lowerCamelCase       :		str		=audioldm_pipe(**lowercase_ ).audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) == 8_1920

             _lowerCamelCase       :		Dict		=audio[7_7230:7_7240]
             _lowerCamelCase       :		Union[str, Any]		=np.array(
                 [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315] )
             _lowerCamelCase       :		Any		=np.abs(expected_slice - audio_slice ).max()
             assert max_diff < 1E-2






      def        lowerCamelCase       (					self						:     Union[str, Any] )					->						List[Any]:
             """simple docstring"""




             _lowerCamelCase       :		Any		=AudioLDMPipeline.from_pretrained('cvssp/audioldm' )
             _lowerCamelCase       :		Tuple		=LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe.to(lowercase_ )
             audioldm_pipe.set_progress_bar_config(disable=lowercase_ )

             _lowerCamelCase       :		Tuple		=self.get_inputs(lowercase_ )
             _lowerCamelCase       :		List[Any]		=audioldm_pipe(**lowercase_ ).audios[0]

             assert audio.ndim == 1
             assert len(lowercase_ ) == 8_1920

             _lowerCamelCase       :		List[str]		=audio[2_7780:2_7790]
             _lowerCamelCase       :		str		=np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212] )
             _lowerCamelCase       :		List[str]		=np.abs(expected_slice - audio_slice ).max()
             assert max_diff < 3E-2






# using dfs for finding eulerian path traversal




def 	a_      (    SCREAMING_SNAKE_CASE__					:      Any		,			SCREAMING_SNAKE_CASE__					:      List[str]		,			SCREAMING_SNAKE_CASE__					:      List[str]		,			SCREAMING_SNAKE_CASE__					:      Optional[int]=None     ):
       '''simple docstring'''





       _lowerCamelCase       :		Optional[Any]		=(path or []) + [u]
       for v in graph[u]:
              if visited_edge[u][v] is False:
                     _lowerCamelCase	,     _lowerCamelCase       :		Dict		=True, True
                     _lowerCamelCase       :		Optional[Any]		=dfs(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       return path




def 	a_      (    SCREAMING_SNAKE_CASE__					:      List[str]		,			SCREAMING_SNAKE_CASE__					:      Optional[Any]     ):
       '''simple docstring'''





       _lowerCamelCase       :		Union[str, Any]		=0
       _lowerCamelCase       :		Union[str, Any]		=-1
       for i in range(SCREAMING_SNAKE_CASE__     ):
              if i not in graph.keys():
                     continue
              if len(graph[i]     ) % 2 == 1:
                     odd_degree_nodes += 1
                     _lowerCamelCase       :		Tuple		=i
       if odd_degree_nodes == 0:
              return 1, odd_node
       if odd_degree_nodes == 2:
              return 2, odd_node
       return 3, odd_node




def 	a_      (    SCREAMING_SNAKE_CASE__					:      Union[str, Any]		,			SCREAMING_SNAKE_CASE__					:      Any     ):
       '''simple docstring'''





       _lowerCamelCase       :		Tuple		=[[False for _ in range(max_node + 1     )] for _ in range(max_node + 1     )]
       _lowerCamelCase	,     _lowerCamelCase       :		Optional[int]		=check_circuit_or_path(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       if check == 3:
              print('graph is not Eulerian'     )
              print('no path'     )
              return
       _lowerCamelCase       :		Any		=1
       if check == 2:
              _lowerCamelCase       :		Tuple		=odd_node
              print('graph has a Euler path'     )
       if check == 1:
              print('graph has a Euler cycle'     )
       _lowerCamelCase       :		int		=dfs(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       print(SCREAMING_SNAKE_CASE__     )




def 	a_      (    ):
       '''simple docstring'''





       _lowerCamelCase       :		Union[str, Any]		={1: [2, 3, 4], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [4]}
       _lowerCamelCase       :		str		={1: [2, 3, 4, 5], 2: [1, 3], 3: [1, 2], 4: [1, 5], 5: [1, 4]}
       _lowerCamelCase       :		List[Any]		={1: [2, 3, 4], 2: [1, 3, 4], 3: [1, 2], 4: [1, 2, 5], 5: [4]}
       _lowerCamelCase       :		Any		={1: [2, 3], 2: [1, 3], 3: [1, 2]}
       _lowerCamelCase       :		Dict		={
           1: [],
           2: []
           # all degree is zero
       }
       _lowerCamelCase       :		str		=10
       check_euler(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       check_euler(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       check_euler(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       check_euler(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )
       check_euler(SCREAMING_SNAKE_CASE__		,			SCREAMING_SNAKE_CASE__     )


if __name__ == "__main__":
 main()


