






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 						_UpperCAmelCase	(	_A					,				unittest.TestCase		):




      """simple docstring"""



      A    =     AudioLDMPipeline
      A    =     TEXT_TO_AUDIO_PARAMS
      A    =     TEXT_TO_AUDIO_BATCH_PARAMS
      A    =     frozenset(
          [
              '''num_inference_steps''',
              '''num_waveforms_per_prompt''',
              '''generator''',
              '''latents''',
              '''output_type''',
              '''return_dict''',
              '''callback''',
              '''callback_steps''',
          ]		)
      def       snake_case_   ( self		):



          '''simple docstring'''
          torch.manual_seed(0		)
          lowerCAmelCase__ :Dict   				=					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__ :List[str]   				=					DDIMScheduler(
              beta_start=0.0_0085     ,						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[Any]   				=					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=1_000     ,						projection_dim=32     ,						)
          lowerCAmelCase__ :Optional[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=16_000     ,						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__ :Union[str, Any]   				=					SpeechTaHifiGan(__lowercase		)

          lowerCAmelCase__ :int   				=					{
              "unet": unet,
              "scheduler": scheduler,
              "vae": vae,
              "text_encoder": text_encoder,
              "tokenizer": tokenizer,
              "vocoder": vocoder,
          }
          return components
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase=0		):



          '''simple docstring'''







          if str(__lowercase		).startswith("mps"		):
              lowerCAmelCase__ :Optional[Any]   				=					torch.manual_seed(__lowercase		)
          else:
              lowerCAmelCase__ :Dict   				=					torch.Generator(device=__lowercase		).manual_seed(__lowercase		)
          lowerCAmelCase__ :Dict   				=					{
              "prompt": "A hammer hitting a wooden surface",
              "generator": generator,
              "num_inference_steps": 2,
              "guidance_scale": 6.0,
          }
          return inputs
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					"cpu"  # ensure determinism for the device-dependent torch.Generator

          lowerCAmelCase__ :Optional[int]   				=					self.get_dummy_components()
          lowerCAmelCase__ :Optional[Any]   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :Optional[Any]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Dict   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :Optional[Any]   				=					audioldm_pipe(**__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					output.audios[0]

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

          lowerCAmelCase__ :int   				=					audio[:10]
          lowerCAmelCase__ :Optional[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       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :List[str]   				=					self.get_dummy_components()
          lowerCAmelCase__ :str   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :Any   				=					audioldm_pipe.to(__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Tuple   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :List[str]   				=					3 * [inputs["prompt"]]

          # forward
          lowerCAmelCase__ :Union[str, Any]   				=					audioldm_pipe(**__lowercase		)
          lowerCAmelCase__ :Optional[Any]   				=					output.audios[0]

          lowerCAmelCase__ :Tuple   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					3 * [inputs.pop("prompt"		)]

          lowerCAmelCase__ :str   				=					audioldm_pipe.tokenizer(
              __lowercase     ,						padding="max_length"     ,						max_length=audioldm_pipe.tokenizer.model_max_length     ,						truncation=__lowercase     ,						return_tensors="pt"     ,						)
          lowerCAmelCase__ :Any   				=					text_inputs["input_ids"].to(__lowercase		)

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

          lowerCAmelCase__ :Optional[int]   				=					prompt_embeds

          # forward
          lowerCAmelCase__ :Union[str, Any]   				=					audioldm_pipe(**__lowercase		)
          lowerCAmelCase__ :Tuple   				=					output.audios[0]

          assert np.abs(audio_a - audio_a		).max() < 1e-2
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Dict   				=					self.get_dummy_components()
          lowerCAmelCase__ :str   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :Dict   				=					audioldm_pipe.to(__lowercase		)
          lowerCAmelCase__ :Tuple   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Union[str, Any]   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					3 * ["this is a negative prompt"]
          lowerCAmelCase__ :List[Any]   				=					negative_prompt
          lowerCAmelCase__ :Any   				=					3 * [inputs["prompt"]]

          # forward
          lowerCAmelCase__ :Tuple   				=					audioldm_pipe(**__lowercase		)
          lowerCAmelCase__ :List[Any]   				=					output.audios[0]

          lowerCAmelCase__ :Dict   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :Any   				=					3 * [inputs.pop("prompt"		)]

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

              lowerCAmelCase__ :Dict   				=					audioldm_pipe.text_encoder(
                  __lowercase     ,						)
              lowerCAmelCase__ :Optional[Any]   				=					text_embeds.text_embeds
              # additional L_2 normalization over each hidden-state
              lowerCAmelCase__ :Optional[int]   				=					F.normalize(__lowercase     ,						dim=-1		)

              embeds.append(__lowercase		)

          lowerCAmelCase__				,lowerCAmelCase__ :Any   				=					embeds

          # forward
          lowerCAmelCase__ :Tuple   				=					audioldm_pipe(**__lowercase		)
          lowerCAmelCase__ :int   				=					output.audios[0]

          assert np.abs(audio_a - audio_a		).max() < 1e-2
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Union[str, Any]   				=					"cpu"  # ensure determinism for the device-dependent torch.Generator
          lowerCAmelCase__ :Optional[int]   				=					self.get_dummy_components()
          lowerCAmelCase__ :Optional[Any]   				=					PNDMScheduler(skip_prk_steps=__lowercase		)
          lowerCAmelCase__ :str   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :int   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :List[Any]   				=					"egg cracking"
          lowerCAmelCase__ :Tuple   				=					audioldm_pipe(**__lowercase     ,						negative_prompt=__lowercase		)
          lowerCAmelCase__ :List[str]   				=					output.audios[0]

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

          lowerCAmelCase__ :Union[str, Any]   				=					audio[:10]
          lowerCAmelCase__ :List[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       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :int   				=					"cpu"  # ensure determinism for the device-dependent torch.Generator
          lowerCAmelCase__ :Dict   				=					self.get_dummy_components()
          lowerCAmelCase__ :Tuple   				=					PNDMScheduler(skip_prk_steps=__lowercase		)
          lowerCAmelCase__ :int   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Dict   				=					"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__ :Optional[int]   				=					2
          lowerCAmelCase__ :Union[str, 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__ :Optional[int]   				=					2
          lowerCAmelCase__ :Dict   				=					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__ :Dict   				=					2
          lowerCAmelCase__ :Union[str, 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       snake_case_   ( self		):



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

          lowerCAmelCase__ :Optional[int]   				=					self.get_dummy_inputs(__lowercase		)
          lowerCAmelCase__ :Optional[int]   				=					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__ :Any   				=					audioldm_pipe(audio_length_in_s=0.032     ,						**__lowercase		)
          lowerCAmelCase__ :int   				=					output.audios[0]

          assert audio.ndim == 1
          assert len(__lowercase		) / vocoder_sampling_rate == 0.032
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :int   				=					self.get_dummy_components()
          lowerCAmelCase__ :Union[str, Any]   				=					AudioLDMPipeline(**__lowercase		)
          lowerCAmelCase__ :str   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :List[str]   				=					["hey"]

          lowerCAmelCase__ :Dict   				=					audioldm_pipe(__lowercase     ,						num_inference_steps=1		)
          lowerCAmelCase__ :Dict   				=					output.audios.shape
          assert audio_shape == (1, 256)

          lowerCAmelCase__ :Tuple   				=					audioldm_pipe.vocoder.config
          config.model_in_dim *= 2
          lowerCAmelCase__ :Tuple   				=					SpeechTaHifiGan(__lowercase		).to(__lowercase		)
          lowerCAmelCase__ :Any   				=					audioldm_pipe(__lowercase     ,						num_inference_steps=1		)
          lowerCAmelCase__ :List[Any]   				=					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       snake_case_   ( self		):



          '''simple docstring'''
          self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__lowercase		)
      def       snake_case_   ( self		):



          '''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       snake_case_   ( self		):



          '''simple docstring'''
          self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__lowercase		)







@slow
class 						_UpperCAmelCase	(	unittest.TestCase		):




      """simple docstring"""



      def       snake_case_   ( self		):



          '''simple docstring'''
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()
      def       snake_case_   ( self     ,						_lowerCAmelCase     ,						_lowerCAmelCase="cpu"     ,						_lowerCAmelCase=torch.floataa     ,						_lowerCAmelCase=0		):



          '''simple docstring'''
          lowerCAmelCase__ :Any   				=					torch.Generator(device=__lowercase		).manual_seed(__lowercase		)
          lowerCAmelCase__ :List[Any]   				=					np.random.RandomState(__lowercase		).standard_normal((1, 8, 128, 16)		)
          lowerCAmelCase__ :Optional[Any]   				=					torch.from_numpy(__lowercase		).to(device=__lowercase     ,						dtype=__lowercase		)
          lowerCAmelCase__ :Optional[Any]   				=					{
              "prompt": "A hammer hitting a wooden surface",
              "latents": latents,
              "generator": generator,
              "num_inference_steps": 3,
              "guidance_scale": 2.5,
          }
          return inputs
      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Union[str, Any]   				=					AudioLDMPipeline.from_pretrained("cvssp/audioldm"		)
          lowerCAmelCase__ :Union[str, Any]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Optional[Any]   				=					self.get_inputs(__lowercase		)
          lowerCAmelCase__ :List[Any]   				=					25
          lowerCAmelCase__ :Any   				=					audioldm_pipe(**__lowercase		).audios[0]

          assert audio.ndim == 1
          assert len(__lowercase		) == 81_920

          lowerCAmelCase__ :str   				=					audio[77_230:77_240]
          lowerCAmelCase__ :List[Any]   				=					np.array(
              [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315]		)
          lowerCAmelCase__ :int   				=					np.abs(expected_slice - audio_slice		).max()
          assert max_diff < 1e-2



      def       snake_case_   ( self		):



          '''simple docstring'''
          lowerCAmelCase__ :Tuple   				=					AudioLDMPipeline.from_pretrained("cvssp/audioldm"		)
          lowerCAmelCase__ :List[str]   				=					LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config		)
          lowerCAmelCase__ :Optional[int]   				=					audioldm_pipe.to(__lowercase		)
          audioldm_pipe.set_progress_bar_config(disable=__lowercase		)

          lowerCAmelCase__ :Optional[int]   				=					self.get_inputs(__lowercase		)
          lowerCAmelCase__ :Tuple   				=					audioldm_pipe(**__lowercase		).audios[0]

          assert audio.ndim == 1
          assert len(__lowercase		) == 81_920

          lowerCAmelCase__ :List[Any]   				=					audio[27_780:27_790]
          lowerCAmelCase__ :List[str]   				=					np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212]		)
          lowerCAmelCase__ :int   				=					np.abs(expected_slice - audio_slice		).max()
          assert max_diff < 3e-2






'''simple docstring'''

def 					__UpperCamelCase				(						lowercase__    :	int			):






     '''simple docstring'''





     if upper_limit < 0:
          raise ValueError('Limit for the Catalan sequence must be ≥ 0'			)

     __lowercase      		=[0] * (upper_limit + 1)

     # Base case: C(0) = C(1) = 1
     __lowercase      		=1
     if upper_limit > 0:
          __lowercase      		=1

     # Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
     for i in range(2,       upper_limit + 1			):
          for j in range(lowercase__			):
               catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

     return catalan_list


if __name__ == "__main__":
       print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''')
       print('''\n*** Enter -1 at any time to quit ***''')
       print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''')
       try:
              while True:
                     UpperCAmelCase					 =	int(input().strip())
                     if N < 0:
                            print('''\n********* Goodbye!! ************''')
                            break
                     else:
                            print(F'''The Catalan numbers from 0 through {N} are:''')
                            print(catalan_numbers(N))
                            print('''Try another upper limit for the sequence: ''', end='''''')
       except (NameError, ValueError):
              print('''\n********* Invalid input, goodbye! ************\n''')

       import doctest

       doctest.testmod()


