





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__   (       __snake_case       ,   unittest.TestCase			):
      _UpperCAmelCase	:Union[str, Any]						    =   AudioLDMPipeline
      _UpperCAmelCase	:Any						    =   TEXT_TO_AUDIO_PARAMS
      _UpperCAmelCase	:Union[str, Any]						    =   TEXT_TO_AUDIO_BATCH_PARAMS
      _UpperCAmelCase	:List[str]						    =   frozenset(
          [
              'num_inference_steps',
              'num_waveforms_per_prompt',
              'generator',
              'latents',
              'output_type',
              'return_dict',
              'callback',
              'callback_steps',
          ]			)





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       torch.manual_seed(0   )
       UpperCamelCase	:					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=A_       ,							)
       UpperCamelCase	:					int       	=   DDIMScheduler(
           beta_start=0.0_00_85       ,							beta_end=0.0_12       ,							beta_schedule="scaled_linear"       ,							clip_sample=A_       ,							set_alpha_to_one=A_       ,							)
       torch.manual_seed(0   )
       UpperCamelCase	:					List[str]       	=   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   )
       UpperCamelCase	:					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=1000       ,							projection_dim=32       ,							)
       UpperCamelCase	:					Union[str, Any]       	=   ClapTextModelWithProjection(A_   )
       UpperCamelCase	:					int       	=   RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta"       ,							model_max_length=77   )

       UpperCamelCase	:					Dict       	=   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=A_       ,							)

       UpperCamelCase	:					Optional[int]       	=   SpeechTaHifiGan(A_   )

       UpperCamelCase	:					str       	=   {
           "unet": unet,
           "scheduler": scheduler,
           "vae": vae,
           "text_encoder": text_encoder,
           "tokenizer": tokenizer,
           "vocoder": vocoder,
       }
       return components





      def        __UpperCamelCase( self       ,							A_       ,							A_=0   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   "cpu"  # ensure determinism for the device-dependent torch.Generator

       UpperCamelCase	:					Optional[Any]       	=   self.get_dummy_components()
       UpperCamelCase	:					Any       	=   AudioLDMPipeline(**A_   )
       UpperCamelCase	:					Tuple       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

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

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

       UpperCamelCase	:					str       	=   audio[:10]
       UpperCamelCase	:					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        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[Any]       	=   self.get_dummy_components()
       UpperCamelCase	:					Tuple       	=   AudioLDMPipeline(**A_   )
       UpperCamelCase	:					Dict       	=   audioldm_pipe.to(A_   )
       UpperCamelCase	:					Tuple       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

       UpperCamelCase	:					List[str]       	=   self.get_dummy_inputs(A_   )
       UpperCamelCase	:					List[Any]       	=   3 * [inputs["prompt"]]

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

       UpperCamelCase	:					str       	=   self.get_dummy_inputs(A_   )
       UpperCamelCase	:					Any       	=   3 * [inputs.pop("prompt"   )]

       UpperCamelCase	:					Any       	=   audioldm_pipe.tokenizer(
           A_       ,							padding="max_length"       ,							max_length=audioldm_pipe.tokenizer.model_max_length       ,							truncation=A_       ,							return_tensors="pt"       ,							)
       UpperCamelCase	:					Dict       	=   text_inputs["input_ids"].to(A_   )

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

       UpperCamelCase	:					Any       	=   prompt_embeds

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

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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Any       	=   self.get_dummy_components()
       UpperCamelCase	:					int       	=   AudioLDMPipeline(**A_   )
       UpperCamelCase	:					Union[str, Any]       	=   audioldm_pipe.to(A_   )
       UpperCamelCase	:					Dict       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

       UpperCamelCase	:					List[Any]       	=   self.get_dummy_inputs(A_   )
       UpperCamelCase	:					int       	=   3 * ["this is a negative prompt"]
       UpperCamelCase	:					Optional[int]       	=   negative_prompt
       UpperCamelCase	:					List[Any]       	=   3 * [inputs["prompt"]]

       # forward
       UpperCamelCase	:					Optional[Any]       	=   audioldm_pipe(**A_   )
       UpperCamelCase	:					Dict       	=   output.audios[0]

       UpperCamelCase	:					Union[str, Any]       	=   self.get_dummy_inputs(A_   )
       UpperCamelCase	:					Optional[int]       	=   3 * [inputs.pop("prompt"   )]

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

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

        embeds.append(A_   )

       UpperCamelCase	,    UpperCamelCase	:					Any       	=   embeds

       # forward
       UpperCamelCase	:					Tuple       	=   audioldm_pipe(**A_   )
       UpperCamelCase	:					Dict       	=   output.audios[0]

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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					List[Any]       	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
       UpperCamelCase	:					Tuple       	=   self.get_dummy_components()
       UpperCamelCase	:					Dict       	=   PNDMScheduler(skip_prk_steps=A_   )
       UpperCamelCase	:					List[str]       	=   AudioLDMPipeline(**A_   )
       UpperCamelCase	:					List[Any]       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

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

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

       UpperCamelCase	:					List[str]       	=   audio[:10]
       UpperCamelCase	:					Dict       	=   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        __UpperCamelCase( self   ):
       '''simple docstring'''




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

       UpperCamelCase	:					Optional[int]       	=   "A hammer hitting a wooden surface"

       # test num_waveforms_per_prompt=1 (default)
       UpperCamelCase	:					Optional[Any]       	=   audioldm_pipe(A_       ,							num_inference_steps=2   ).audios

       assert audios.shape == (1, 256)

       # test num_waveforms_per_prompt=1 (default) for batch of prompts
       UpperCamelCase	:					int       	=   2
       UpperCamelCase	:					List[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
       UpperCamelCase	:					Any       	=   2
       UpperCamelCase	:					List[Any]       	=   audioldm_pipe(A_       ,							num_inference_steps=2       ,							num_waveforms_per_prompt=A_   ).audios

       assert audios.shape == (num_waveforms_per_prompt, 256)

       # test num_waveforms_per_prompt for batch of prompts
       UpperCamelCase	:					Any       	=   2
       UpperCamelCase	:					Dict       	=   audioldm_pipe(
           [prompt] * batch_size       ,							num_inference_steps=2       ,							num_waveforms_per_prompt=A_   ).audios

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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
       UpperCamelCase	:					Dict       	=   self.get_dummy_components()
       UpperCamelCase	:					List[Any]       	=   AudioLDMPipeline(**A_   )
       UpperCamelCase	:					Optional[int]       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )
       UpperCamelCase	:					Tuple       	=   audioldm_pipe.vocoder.config.sampling_rate

       UpperCamelCase	:					Union[str, Any]       	=   self.get_dummy_inputs(A_   )
       UpperCamelCase	:					List[Any]       	=   audioldm_pipe(audio_length_in_s=0.0_16       ,							**A_   )
       UpperCamelCase	:					List[str]       	=   output.audios[0]

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

       UpperCamelCase	:					List[Any]       	=   audioldm_pipe(audio_length_in_s=0.0_32       ,							**A_   )
       UpperCamelCase	:					Tuple       	=   output.audios[0]

       assert audio.ndim == 1
       assert len(A_   ) / vocoder_sampling_rate == 0.0_32





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




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

       UpperCamelCase	:					int       	=   ["hey"]

       UpperCamelCase	:					str       	=   audioldm_pipe(A_       ,							num_inference_steps=1   )
       UpperCamelCase	:					Optional[int]       	=   output.audios.shape
       assert audio_shape == (1, 256)

       UpperCamelCase	:					Optional[Any]       	=   audioldm_pipe.vocoder.config
       config.model_in_dim *= 2
       UpperCamelCase	:					Tuple       	=   SpeechTaHifiGan(A_   ).to(A_   )
       UpperCamelCase	:					List[Any]       	=   audioldm_pipe(A_       ,							num_inference_steps=1   )
       UpperCamelCase	:					int       	=   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        __UpperCamelCase( self   ):
       '''simple docstring'''




       self._test_attention_slicing_forward_pass(test_mean_pixel_difference=A_   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       self._test_inference_batch_single_identical(test_mean_pixel_difference=A_   )





      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available()       ,							reason="XFormers attention is only available with CUDA and `xformers` installed"       ,							)
      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=A_   )



@slow
class 					A__   (       unittest.TestCase			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self       ,							A_       ,							A_="cpu"       ,							A_=torch.floataa       ,							A_=0   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					str       	=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"   )
       UpperCamelCase	:					List[Any]       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

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

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

       UpperCamelCase	:					int       	=   audio[7_7230:7_7240]
       UpperCamelCase	:					Dict       	=   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]   )
       UpperCamelCase	:					Tuple       	=   np.abs(expected_slice - audio_slice   ).max()
       assert max_diff < 1e-2





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"   )
       UpperCamelCase	:					Tuple       	=   LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config   )
       UpperCamelCase	:					Optional[Any]       	=   audioldm_pipe.to(A_   )
       audioldm_pipe.set_progress_bar_config(disable=A_   )

       UpperCamelCase	:					int       	=   self.get_inputs(A_   )
       UpperCamelCase	:					Optional[int]       	=   audioldm_pipe(**A_   ).audios[0]

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

       UpperCamelCase	:					Optional[int]       	=   audio[2_7780:2_7790]
       UpperCamelCase	:					List[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]   )
       UpperCamelCase	:					List[str]       	=   np.abs(expected_slice - audio_slice   ).max()
       assert max_diff < 3e-2




import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, ClassLabel, Features
from .base import TaskTemplate







@dataclass(frozen=lowerCamelCase_      )
class        snake_case_   (lowerCamelCase_      ):
					UpperCAmelCase__					:    str          =			field(default='''audio-classification'''	,						metadata={'''include_in_asdict_even_if_is_default''': True}      )
					UpperCAmelCase__					:    ClassVar[Features]          =			Features({'''audio''': Audio()}      )
					UpperCAmelCase__					:    ClassVar[Features]          =			Features({'''labels''': ClassLabel}      )
					UpperCAmelCase__					:    str          =			"audio"
					UpperCAmelCase__					:    str          =			"labels"
					def  lowerCamelCase__(      self   :Optional[int]			,__snake_case   :int      )					->    str:
												if self.label_column not in features:
																			raise ValueError(F'Column {self.label_column} is not present in features.'      )
												if not isinstance(features[self.label_column]			,__snake_case      ):
																			raise ValueError(F'Column {self.label_column} is not a ClassLabel.'      )
												a__             =     copy.deepcopy(self      )
												a__             =     self.label_schema.copy()
												a__             =     features[self.label_column]
												a__             =     label_schema
												return task_template



					@property
					def  lowerCamelCase__(      self   :Dict      )					->    Dict[str, str]:
												return {
												    self.audio_column: "audio",
												    self.label_column: "labels",
												}




