


'''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   UpperCAmelCase_    (a_   ,						unittest.TestCase    ):





    """simple docstring"""






    lowerCamelCase    :   Any					=     AudioLDMPipeline
    lowerCamelCase    :   Dict					=     TEXT_TO_AUDIO_PARAMS
    lowerCamelCase    :   List[Any]					=     TEXT_TO_AUDIO_BATCH_PARAMS
    lowerCamelCase    :   int					=     frozenset(
        [
            'num_inference_steps',
            'num_waveforms_per_prompt',
            'generator',
            'latents',
            'output_type',
            'return_dict',
            'callback',
            'callback_steps',
        ]    )







    def  lowercase_							(				self      )							->	Optional[int]:
         torch.manual_seed(0      )
         __lowerCamelCase  :      Union[str, Any]														=						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=UpperCamelCase_     ,	)
         __lowerCamelCase  :      Union[str, Any]														=						DDIMScheduler(
             beta_start=0.0_0_0_8_5     ,	beta_end=0.0_1_2     ,	beta_schedule='scaled_linear'     ,	clip_sample=UpperCamelCase_     ,	set_alpha_to_one=UpperCamelCase_     ,	)
         torch.manual_seed(0      )
         __lowerCamelCase  :      Tuple														=						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  :      Optional[int]														=						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=10_00     ,	projection_dim=32     ,	)
         __lowerCamelCase  :      Optional[Any]														=						ClapTextModelWithProjection(UpperCamelCase_      )
         __lowerCamelCase  :      Optional[Any]														=						RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'     ,	model_max_length=77      )

         __lowerCamelCase  :      Union[str, Any]														=						SpeechTaHifiGanConfig(
             model_in_dim=8     ,	sampling_rate=1_60_00     ,	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=UpperCamelCase_     ,	)

         __lowerCamelCase  :      Optional[Any]														=						SpeechTaHifiGan(UpperCamelCase_      )

         __lowerCamelCase  :      Tuple														=						{
             'unet': unet,
             'scheduler': scheduler,
             'vae': vae,
             'text_encoder': text_encoder,
             'tokenizer': tokenizer,
             'vocoder': vocoder,
         }
         return components







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_=0      )							->	Any:

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







    def  lowercase_							(				self      )							->	Union[str, Any]:
         __lowerCamelCase  :      Tuple														=						'cpu'  # ensure determinism for the device-dependent torch.Generator

         __lowerCamelCase  :      List[str]														=						self.get_dummy_components()
         __lowerCamelCase  :      Optional[Any]														=						AudioLDMPipeline(**UpperCamelCase_      )
         __lowerCamelCase  :      Any														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

         __lowerCamelCase  :      Union[str, Any]														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      List[str]														=						audioldm_pipe(**UpperCamelCase_      )
         __lowerCamelCase  :      str														=						output.audios[0]

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) == 2_56

         __lowerCamelCase  :      Optional[int]														=						audio[:10]
         __lowerCamelCase  :      Tuple														=						np.array(
             [-0.0_0_5_0, 0.0_0_5_0, -0.0_0_6_0, 0.0_0_3_3, -0.0_0_2_6, 0.0_0_3_3, -0.0_0_2_7, 0.0_0_3_3, -0.0_0_2_8, 0.0_0_3_3]      )

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







    def  lowercase_							(				self      )							->	Dict:
         __lowerCamelCase  :      List[str]														=						self.get_dummy_components()
         __lowerCamelCase  :      List[str]														=						AudioLDMPipeline(**UpperCamelCase_      )
         __lowerCamelCase  :      int														=						audioldm_pipe.to(UpperCamelCase_      )
         __lowerCamelCase  :      str														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

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

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

         __lowerCamelCase  :      int														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      Dict														=						3 * [inputs.pop('prompt'      )]

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

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

         __lowerCamelCase  :      Dict														=						prompt_embeds

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

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







    def  lowercase_							(				self      )							->	Optional[Any]:
         __lowerCamelCase  :      Optional[Any]														=						self.get_dummy_components()
         __lowerCamelCase  :      List[str]														=						AudioLDMPipeline(**UpperCamelCase_      )
         __lowerCamelCase  :      List[Any]														=						audioldm_pipe.to(UpperCamelCase_      )
         __lowerCamelCase  :      List[str]														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

         __lowerCamelCase  :      Optional[int]														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      List[Any]														=						3 * ['this is a negative prompt']
         __lowerCamelCase  :      List[Any]														=						negative_prompt
         __lowerCamelCase  :      str														=						3 * [inputs['prompt']]

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

         __lowerCamelCase  :      Optional[Any]														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      Any														=						3 * [inputs.pop('prompt'      )]

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

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

              embeds.append(UpperCamelCase_      )

         __lowerCamelCase							,			__lowerCamelCase  :      int														=						embeds

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

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







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

         __lowerCamelCase  :      Union[str, Any]														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      List[str]														=						'egg cracking'
         __lowerCamelCase  :      str														=						audioldm_pipe(**UpperCamelCase_     ,	negative_prompt=UpperCamelCase_      )
         __lowerCamelCase  :      Dict														=						output.audios[0]

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) == 2_56

         __lowerCamelCase  :      Optional[Any]														=						audio[:10]
         __lowerCamelCase  :      str														=						np.array(
             [-0.0_0_5_1, 0.0_0_5_0, -0.0_0_6_0, 0.0_0_3_4, -0.0_0_2_6, 0.0_0_3_3, -0.0_0_2_7, 0.0_0_3_3, -0.0_0_2_8, 0.0_0_3_2]      )

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







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

         __lowerCamelCase  :      Optional[Any]														=						'A hammer hitting a wooden surface'

         # test num_waveforms_per_prompt=1 (default)
         __lowerCamelCase  :      Dict														=						audioldm_pipe(UpperCamelCase_     ,	num_inference_steps=2      ).audios

         assert audios.shape == (1, 2_56)

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

         assert audios.shape == (batch_size, 2_56)

         # test num_waveforms_per_prompt for single prompt
         __lowerCamelCase  :      Optional[Any]														=						2
         __lowerCamelCase  :      List[Any]														=						audioldm_pipe(UpperCamelCase_     ,	num_inference_steps=2     ,	num_waveforms_per_prompt=UpperCamelCase_      ).audios

         assert audios.shape == (num_waveforms_per_prompt, 2_56)

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

         assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56)







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

         __lowerCamelCase  :      List[str]														=						self.get_dummy_inputs(UpperCamelCase_      )
         __lowerCamelCase  :      Tuple														=						audioldm_pipe(audio_length_in_s=0.0_1_6     ,	**UpperCamelCase_      )
         __lowerCamelCase  :      Union[str, Any]														=						output.audios[0]

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) / vocoder_sampling_rate == 0.0_1_6

         __lowerCamelCase  :      str														=						audioldm_pipe(audio_length_in_s=0.0_3_2     ,	**UpperCamelCase_      )
         __lowerCamelCase  :      int														=						output.audios[0]

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) / vocoder_sampling_rate == 0.0_3_2







    def  lowercase_							(				self      )							->	Union[str, Any]:
         __lowerCamelCase  :      Union[str, Any]														=						self.get_dummy_components()
         __lowerCamelCase  :      Union[str, Any]														=						AudioLDMPipeline(**UpperCamelCase_      )
         __lowerCamelCase  :      Union[str, Any]														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

         __lowerCamelCase  :      Optional[Any]														=						['hey']

         __lowerCamelCase  :      List[str]														=						audioldm_pipe(UpperCamelCase_     ,	num_inference_steps=1      )
         __lowerCamelCase  :      List[Any]														=						output.audios.shape
         assert audio_shape == (1, 2_56)

         __lowerCamelCase  :      List[Any]														=						audioldm_pipe.vocoder.config
         config.model_in_dim *= 2
         __lowerCamelCase  :      List[Any]														=						SpeechTaHifiGan(UpperCamelCase_      ).to(UpperCamelCase_      )
         __lowerCamelCase  :      Optional[int]														=						audioldm_pipe(UpperCamelCase_     ,	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, 2_56)







    def  lowercase_							(				self      )							->	Optional[Any]:
         self._test_attention_slicing_forward_pass(test_mean_pixel_difference=UpperCamelCase_      )







    def  lowercase_							(				self      )							->	int:
         self._test_inference_batch_single_identical(test_mean_pixel_difference=UpperCamelCase_      )







    @unittest.skipIf(
        torch_device != 'cuda' or not is_xformers_available()     ,	reason='XFormers attention is only available with CUDA and `xformers` installed'     ,	)
    def  lowercase_							(				self      )							->	List[Any]:
         self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=UpperCamelCase_      )







@slow
class   UpperCAmelCase_    (unittest.TestCase    ):





    """simple docstring"""






    def  lowercase_							(				self      )							->	Optional[Any]:
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()







    def  lowercase_							(				self     ,	SCREAMING_SNAKE_CASE_     ,	SCREAMING_SNAKE_CASE_="cpu"     ,	SCREAMING_SNAKE_CASE_=torch.floataa     ,	SCREAMING_SNAKE_CASE_=0      )							->	Any:
         __lowerCamelCase  :      str														=						torch.Generator(device=UpperCamelCase_      ).manual_seed(UpperCamelCase_      )
         __lowerCamelCase  :      str														=						np.random.RandomState(UpperCamelCase_      ).standard_normal((1, 8, 1_28, 16)      )
         __lowerCamelCase  :      Tuple														=						torch.from_numpy(UpperCamelCase_      ).to(device=UpperCamelCase_     ,	dtype=UpperCamelCase_      )
         __lowerCamelCase  :      Any														=						{
             'prompt': 'A hammer hitting a wooden surface',
             'latents': latents,
             'generator': generator,
             'num_inference_steps': 3,
             'guidance_scale': 2.5,
         }
         return inputs







    def  lowercase_							(				self      )							->	Optional[int]:
         __lowerCamelCase  :      Union[str, Any]														=						AudioLDMPipeline.from_pretrained('cvssp/audioldm'      )
         __lowerCamelCase  :      Optional[Any]														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

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

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) == 8_19_20

         __lowerCamelCase  :      str														=						audio[7_72_30:7_72_40]
         __lowerCamelCase  :      Union[str, Any]														=						np.array(
             [-0.4_8_8_4, -0.4_6_0_7, 0.0_0_2_3, 0.5_0_0_7, 0.5_8_9_6, 0.5_1_5_1, 0.3_8_1_3, -0.0_2_0_8, -0.3_6_8_7, -0.4_3_1_5]      )
         __lowerCamelCase  :      Union[str, Any]														=						np.abs(expected_slice - audio_slice      ).max()
         assert max_diff < 1E-2







    def  lowercase_							(				self      )							->	List[str]:
         __lowerCamelCase  :      str														=						AudioLDMPipeline.from_pretrained('cvssp/audioldm'      )
         __lowerCamelCase  :      Union[str, Any]														=						LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config      )
         __lowerCamelCase  :      str														=						audioldm_pipe.to(UpperCamelCase_      )
         audioldm_pipe.set_progress_bar_config(disable=UpperCamelCase_      )

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

         assert audio.ndim == 1
         assert len(UpperCamelCase_      ) == 8_19_20

         __lowerCamelCase  :      str														=						audio[2_77_80:2_77_90]
         __lowerCamelCase  :      Tuple														=						np.array([-0.2_1_3_1, -0.0_8_7_3, -0.0_1_2_4, -0.0_1_8_9, 0.0_5_6_9, 0.1_3_7_3, 0.1_8_8_3, 0.2_8_8_6, 0.3_2_9_7, 0.2_2_1_2]      )
         __lowerCamelCase  :      Any														=						np.abs(expected_slice - audio_slice      ).max()
         assert max_diff < 3E-2














"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case : int     		=		{}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : str     		=		["""NllbTokenizer"""]

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 snake_case : Dict     		=		["""NllbTokenizerFast"""]


if TYPE_CHECKING:
 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb import NllbTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_nllb_fast import NllbTokenizerFast

else:
 import sys

 snake_case : Dict     		=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)