




"""simple docstring"""






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






   '''simple docstring'''



   lowerCamelCase__   =    AudioLDMPipeline
   lowerCamelCase__   =    TEXT_TO_AUDIO_PARAMS
   lowerCamelCase__   =    TEXT_TO_AUDIO_BATCH_PARAMS
   lowerCamelCase__   =    frozenset(
       [
           """num_inference_steps""",
           """num_waveforms_per_prompt""",
           """generator""",
           """latents""",
           """output_type""",
           """return_dict""",
           """callback""",
           """callback_steps""",
       ]		)
   def       A_					(						self						):
       torch.manual_seed(0						)
       _lowerCamelCase  :    List[str]					=					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=__lowerCamelCase						,   )
       _lowerCamelCase  :    List[str]					=					DDIMScheduler(
           beta_start=0.0_00_85						,   beta_end=0.0_12						,   beta_schedule='scaled_linear'						,   clip_sample=__lowerCamelCase						,   set_alpha_to_one=__lowerCamelCase						,   )
       torch.manual_seed(0						)
       _lowerCamelCase  :    Optional[int]					=					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  :    Any					=					ClapTextModelWithProjection(__lowerCamelCase						)
       _lowerCamelCase  :    List[str]					=					RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'						,   model_max_length=77						)

       _lowerCamelCase  :    List[str]					=					SpeechTaHifiGanConfig(
           model_in_dim=8						,   sampling_rate=16000						,   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=__lowerCamelCase						,   )

       _lowerCamelCase  :    Optional[int]					=					SpeechTaHifiGan(__lowerCamelCase						)

       _lowerCamelCase  :    Union[str, Any]					=					{
           "unet": unet,
           "scheduler": scheduler,
           "vae": vae,
           "text_encoder": text_encoder,
           "tokenizer": tokenizer,
           "vocoder": vocoder,
       }
       return components
   def       A_					(						self						,   lowercase						,   lowercase=0						):
       if str(__lowerCamelCase						).startswith('mps'						):
           _lowerCamelCase  :    Any					=					torch.manual_seed(__lowerCamelCase						)
       else:
           _lowerCamelCase  :    Union[str, Any]					=					torch.Generator(device=__lowerCamelCase						).manual_seed(__lowerCamelCase						)
       _lowerCamelCase  :    List[Any]					=					{
           "prompt": "A hammer hitting a wooden surface",
           "generator": generator,
           "num_inference_steps": 2,
           "guidance_scale": 6.0,
       }
       return inputs
   def       A_					(						self						):
       _lowerCamelCase  :    int					=					"cpu"  # ensure determinism for the device-dependent torch.Generator

       _lowerCamelCase  :    Dict					=					self.get_dummy_components()
       _lowerCamelCase  :    List[str]					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    Tuple					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

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

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

       _lowerCamelCase  :    int					=					audio[:10]
       _lowerCamelCase  :    Dict					=					np.array(
           [-0.00_50, 0.00_50, -0.00_60, 0.00_33, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_33]						)

       assert np.abs(audio_slice - expected_slice						).max() < 1E-2
   def       A_					(						self						):
       _lowerCamelCase  :    List[str]					=					self.get_dummy_components()
       _lowerCamelCase  :    List[Any]					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    Tuple					=					audioldm_pipe.to(__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    str					=					self.get_dummy_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					3 * [inputs["prompt"]]

       # forward
       _lowerCamelCase  :    List[str]					=					audioldm_pipe(**__lowerCamelCase						)
       _lowerCamelCase  :    Dict					=					output.audios[0]

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

       _lowerCamelCase  :    Optional[int]					=					audioldm_pipe.tokenizer(
           __lowerCamelCase						,   padding='max_length'						,   max_length=audioldm_pipe.tokenizer.model_max_length						,   truncation=__lowerCamelCase						,   return_tensors='pt'						,   )
       _lowerCamelCase  :    str					=					text_inputs["input_ids"].to(__lowerCamelCase						)

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

       _lowerCamelCase  :    Union[str, Any]					=					prompt_embeds

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

       assert np.abs(audio_a - audio_a						).max() < 1E-2
   def       A_					(						self						):
       _lowerCamelCase  :    Optional[Any]					=					self.get_dummy_components()
       _lowerCamelCase  :    List[str]					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    List[Any]					=					audioldm_pipe.to(__lowerCamelCase						)
       _lowerCamelCase  :    Optional[int]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    Optional[Any]					=					self.get_dummy_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    int					=					3 * ["this is a negative prompt"]
       _lowerCamelCase  :    Dict					=					negative_prompt
       _lowerCamelCase  :    str					=					3 * [inputs["prompt"]]

       # forward
       _lowerCamelCase  :    Tuple					=					audioldm_pipe(**__lowerCamelCase						)
       _lowerCamelCase  :    str					=					output.audios[0]

       _lowerCamelCase  :    List[str]					=					self.get_dummy_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    Dict					=					3 * [inputs.pop('prompt'						)]

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

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

           embeds.append(__lowerCamelCase						)

       _lowerCamelCase  :    List[Any]					=					embeds

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

       assert np.abs(audio_a - audio_a						).max() < 1E-2
   def       A_					(						self						):
       _lowerCamelCase  :    Dict					=					"cpu"  # ensure determinism for the device-dependent torch.Generator
       _lowerCamelCase  :    List[Any]					=					self.get_dummy_components()
       _lowerCamelCase  :    Optional[int]					=					PNDMScheduler(skip_prk_steps=__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    List[str]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    int					=					self.get_dummy_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					"egg cracking"
       _lowerCamelCase  :    List[Any]					=					audioldm_pipe(**__lowerCamelCase						,   negative_prompt=__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					output.audios[0]

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

       _lowerCamelCase  :    Any					=					audio[:10]
       _lowerCamelCase  :    Tuple					=					np.array(
           [-0.00_51, 0.00_50, -0.00_60, 0.00_34, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_32]						)

       assert np.abs(audio_slice - expected_slice						).max() < 1E-2
   def       A_					(						self						):
       _lowerCamelCase  :    Tuple					=					"cpu"  # ensure determinism for the device-dependent torch.Generator
       _lowerCamelCase  :    Optional[Any]					=					self.get_dummy_components()
       _lowerCamelCase  :    List[Any]					=					PNDMScheduler(skip_prk_steps=__lowerCamelCase						)
       _lowerCamelCase  :    Tuple					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    Union[str, Any]					=					"A hammer hitting a wooden surface"

       # test num_waveforms_per_prompt=1 (default)
       _lowerCamelCase  :    Optional[int]					=					audioldm_pipe(__lowerCamelCase						,   num_inference_steps=2						).audios

       assert audios.shape == (1, 256)

       # test num_waveforms_per_prompt=1 (default) for batch of prompts
       _lowerCamelCase  :    str					=					2
       _lowerCamelCase  :    List[str]					=					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  :    Union[str, Any]					=					2
       _lowerCamelCase  :    Optional[Any]					=					audioldm_pipe(__lowerCamelCase						,   num_inference_steps=2						,   num_waveforms_per_prompt=__lowerCamelCase						).audios

       assert audios.shape == (num_waveforms_per_prompt, 256)

       # test num_waveforms_per_prompt for batch of prompts
       _lowerCamelCase  :    Optional[int]					=					2
       _lowerCamelCase  :    Tuple					=					audioldm_pipe(
           [prompt] * batch_size						,   num_inference_steps=2						,   num_waveforms_per_prompt=__lowerCamelCase						).audios

       assert audios.shape == (batch_size * num_waveforms_per_prompt, 256)
   def       A_					(						self						):
       _lowerCamelCase  :    Union[str, Any]					=					"cpu"  # ensure determinism for the device-dependent torch.Generator
       _lowerCamelCase  :    int					=					self.get_dummy_components()
       _lowerCamelCase  :    List[Any]					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					audioldm_pipe.vocoder.config.sampling_rate

       _lowerCamelCase  :    Tuple					=					self.get_dummy_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					audioldm_pipe(audio_length_in_s=0.0_16						,   **__lowerCamelCase						)
       _lowerCamelCase  :    str					=					output.audios[0]

       assert audio.ndim == 1
       assert len(__lowerCamelCase						) / vocoder_sampling_rate == 0.0_16

       _lowerCamelCase  :    Dict					=					audioldm_pipe(audio_length_in_s=0.0_32						,   **__lowerCamelCase						)
       _lowerCamelCase  :    Dict					=					output.audios[0]

       assert audio.ndim == 1
       assert len(__lowerCamelCase						) / vocoder_sampling_rate == 0.0_32
   def       A_					(						self						):
       _lowerCamelCase  :    Any					=					self.get_dummy_components()
       _lowerCamelCase  :    List[str]					=					AudioLDMPipeline(**__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    Tuple					=					["hey"]

       _lowerCamelCase  :    Tuple					=					audioldm_pipe(__lowerCamelCase						,   num_inference_steps=1						)
       _lowerCamelCase  :    Optional[int]					=					output.audios.shape
       assert audio_shape == (1, 256)

       _lowerCamelCase  :    Tuple					=					audioldm_pipe.vocoder.config
       config.model_in_dim *= 2
       _lowerCamelCase  :    Tuple					=					SpeechTaHifiGan(__lowerCamelCase						).to(__lowerCamelCase						)
       _lowerCamelCase  :    Any					=					audioldm_pipe(__lowerCamelCase						,   num_inference_steps=1						)
       _lowerCamelCase  :    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       A_					(						self						):
       self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__lowerCamelCase						)
   def       A_					(						self						):
       self._test_inference_batch_single_identical(test_mean_pixel_difference=__lowerCamelCase						)






   @unittest.skipIf(
       torch_device != 'cuda' or not is_xformers_available()						,   reason='XFormers attention is only available with CUDA and `xformers` installed'						,   )
   def       A_					(						self						):
       self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__lowerCamelCase						)






@slow
class lowerCAmelCase__			(  unittest.TestCase		):






   '''simple docstring'''



   def       A_					(						self						):
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
   def       A_					(						self						,   lowercase						,   lowercase="cpu"						,   lowercase=torch.floataa						,   lowercase=0						):
       _lowerCamelCase  :    List[str]					=					torch.Generator(device=__lowerCamelCase						).manual_seed(__lowerCamelCase						)
       _lowerCamelCase  :    Optional[Any]					=					np.random.RandomState(__lowerCamelCase						).standard_normal((1, 8, 128, 16)						)
       _lowerCamelCase  :    Optional[Any]					=					torch.from_numpy(__lowerCamelCase						).to(device=__lowerCamelCase						,   dtype=__lowerCamelCase						)
       _lowerCamelCase  :    int					=					{
           "prompt": "A hammer hitting a wooden surface",
           "latents": latents,
           "generator": generator,
           "num_inference_steps": 3,
           "guidance_scale": 2.5,
       }
       return inputs
   def       A_					(						self						):
       _lowerCamelCase  :    int					=					AudioLDMPipeline.from_pretrained('cvssp/audioldm'						)
       _lowerCamelCase  :    List[Any]					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

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

       assert audio.ndim == 1
       assert len(__lowerCamelCase						) == 81920

       _lowerCamelCase  :    str					=					audio[77230:77240]
       _lowerCamelCase  :    Tuple					=					np.array(
           [-0.48_84, -0.46_07, 0.00_23, 0.50_07, 0.58_96, 0.51_51, 0.38_13, -0.02_08, -0.36_87, -0.43_15]						)
       _lowerCamelCase  :    Union[str, Any]					=					np.abs(expected_slice - audio_slice						).max()
       assert max_diff < 1E-2






   def       A_					(						self						):
       _lowerCamelCase  :    List[str]					=					AudioLDMPipeline.from_pretrained('cvssp/audioldm'						)
       _lowerCamelCase  :    str					=					LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config						)
       _lowerCamelCase  :    str					=					audioldm_pipe.to(__lowerCamelCase						)
       audioldm_pipe.set_progress_bar_config(disable=__lowerCamelCase						)

       _lowerCamelCase  :    List[str]					=					self.get_inputs(__lowerCamelCase						)
       _lowerCamelCase  :    str					=					audioldm_pipe(**__lowerCamelCase						).audios[0]

       assert audio.ndim == 1
       assert len(__lowerCamelCase						) == 81920

       _lowerCamelCase  :    Optional[int]					=					audio[27780:27790]
       _lowerCamelCase  :    Any					=					np.array([-0.21_31, -0.08_73, -0.01_24, -0.01_89, 0.05_69, 0.13_73, 0.18_83, 0.28_86, 0.32_97, 0.22_12]						)
       _lowerCamelCase  :    Union[str, Any]					=					np.abs(expected_slice - audio_slice						).max()
       assert max_diff < 3E-2





"""simple docstring"""






def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    List[str]					=					abs(lowercase__	)
				_lowerCamelCase  :    Optional[int]					=					0
				while n > 0:
								res += n % 10
								n //= 10
				return res







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    List[str]					=					abs(lowercase__	)
				return n if n < 10 else n % 10 + sum_of_digits(n // 10	)







def   _snake_case    (       lowercase__	):
				return sum(int(lowercase__	) for c in str(abs(lowercase__	)	)	)







def   _snake_case    (       ):
				from collections.abc import Callable
				from timeit import timeit

				def benchmark_a_function(lowercase__							,					lowercase__	) -> None:
								_lowerCamelCase  :    int					=					f'''{func.__name__}({value})'''
								_lowerCamelCase  :    Optional[Any]					=					timeit(f'''__main__.{call}'''							,					setup='import __main__'	)
								print(f'''{call:56} = {func(lowercase__	)} -- {timing:.4f} seconds'''	)

				for value in (262144, 1125899906842624, 1267650600228229401496703205376):
								for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact):
												benchmark_a_function(lowercase__							,					lowercase__	)
								print()


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			benchmark()