

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      torch.manual_seed(0  )
      _lowercase      :				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      ,    )
      _lowercase      :				int      					=       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  )
      _lowercase      :				Dict      					=       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  )
      _lowercase      :				int      					=       ClapTextConfig(
          bos_token_id=0      ,    eos_token_id=2      ,    hidden_size=32      ,    intermediate_size=37      ,    layer_norm_eps=1E-0_5      ,    num_attention_heads=4      ,    num_hidden_layers=5      ,    pad_token_id=1      ,    vocab_size=1000      ,    projection_dim=32      ,    )
      _lowercase      :				Union[str, Any]      					=       ClapTextModelWithProjection(_UpperCamelCase  )
      _lowercase      :				Optional[Any]      					=       RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta"      ,    model_max_length=77  )

      _lowercase      :				Tuple      					=       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=_UpperCamelCase      ,    )

      _lowercase      :				Union[str, Any]      					=       SpeechTaHifiGan(_UpperCamelCase  )

      _lowercase      :				Union[str, Any]      					=       {
          "unet": unet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "vocoder": vocoder,
      }
      return components


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase=0  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       "cpu"  # ensure determinism for the device-dependent torch.Generator

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

      _lowercase      :				Optional[int]      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				str      					=       audioldm_pipe(**_UpperCamelCase  )
      _lowercase      :				Union[str, Any]      					=       output.audios[0]

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

      _lowercase      :				Any      					=       audio[:10]
      _lowercase      :				int      					=       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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[str]      					=       self.get_dummy_components()
      _lowercase      :				Optional[Any]      					=       AudioLDMPipeline(**_UpperCamelCase  )
      _lowercase      :				Optional[int]      					=       audioldm_pipe.to(_UpperCamelCase  )
      _lowercase      :				Tuple      					=       audioldm_pipe.to(_UpperCamelCase  )
      audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase  )

      _lowercase      :				Tuple      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				Optional[int]      					=       3 * [inputs["prompt"]]

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

      _lowercase      :				int      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				Any      					=       3 * [inputs.pop("prompt"  )]

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

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

      _lowercase      :				Any      					=       prompt_embeds

      # forward
      _lowercase      :				Optional[int]      					=       audioldm_pipe(**_UpperCamelCase  )
      _lowercase      :				Dict      					=       output.audios[0]

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				int      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				Optional[int]      					=       3 * ["this is a negative prompt"]
      _lowercase      :				Tuple      					=       negative_prompt
      _lowercase      :				Dict      					=       3 * [inputs["prompt"]]

      # forward
      _lowercase      :				Optional[int]      					=       audioldm_pipe(**_UpperCamelCase  )
      _lowercase      :				Optional[int]      					=       output.audios[0]

      _lowercase      :				int      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				List[Any]      					=       3 * [inputs.pop("prompt"  )]

      _lowercase      :				Dict      					=       []
      for p in [prompt, negative_prompt]:
         _lowercase      :				str      					=       audioldm_pipe.tokenizer(
             _UpperCamelCase      ,    padding="max_length"      ,    max_length=audioldm_pipe.tokenizer.model_max_length      ,    truncation=_UpperCamelCase      ,    return_tensors="pt"      ,    )
         _lowercase      :				Optional[Any]      					=       text_inputs["input_ids"].to(_UpperCamelCase  )

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

         embeds.append(_UpperCamelCase  )

      _lowercase					,				_lowercase      :				Tuple      					=       embeds

      # forward
      _lowercase      :				List[Any]      					=       audioldm_pipe(**_UpperCamelCase  )
      _lowercase      :				int      					=       output.audios[0]

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				Optional[Any]      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				int      					=       "egg cracking"
      _lowercase      :				Tuple      					=       audioldm_pipe(**_UpperCamelCase      ,    negative_prompt=_UpperCamelCase  )
      _lowercase      :				Union[str, Any]      					=       output.audios[0]

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

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





      """simple docstring"""





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

      _lowercase      :				Dict      					=       "A hammer hitting a wooden surface"

      # test num_waveforms_per_prompt=1 (default)
      _lowercase      :				List[str]      					=       audioldm_pipe(_UpperCamelCase      ,    num_inference_steps=2  ).audios

      assert audios.shape == (1, 256)

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

      assert audios.shape == (batch_size, 256)

      # test num_waveforms_per_prompt for single prompt
      _lowercase      :				List[str]      					=       2
      _lowercase      :				Dict      					=       audioldm_pipe(_UpperCamelCase      ,    num_inference_steps=2      ,    num_waveforms_per_prompt=_UpperCamelCase  ).audios

      assert audios.shape == (num_waveforms_per_prompt, 256)

      # test num_waveforms_per_prompt for batch of prompts
      _lowercase      :				int      					=       2
      _lowercase      :				List[str]      					=       audioldm_pipe(
          [prompt] * batch_size      ,    num_inference_steps=2      ,    num_waveforms_per_prompt=_UpperCamelCase  ).audios

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				Optional[int]      					=       self.get_dummy_inputs(_UpperCamelCase  )
      _lowercase      :				str      					=       audioldm_pipe(audio_length_in_s=0.0_1_6      ,    **_UpperCamelCase  )
      _lowercase      :				Dict      					=       output.audios[0]

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

      _lowercase      :				int      					=       audioldm_pipe(audio_length_in_s=0.0_3_2      ,    **_UpperCamelCase  )
      _lowercase      :				Union[str, Any]      					=       output.audios[0]

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       self.get_dummy_components()
      _lowercase      :				Tuple      					=       AudioLDMPipeline(**_UpperCamelCase  )
      _lowercase      :				Optional[Any]      					=       audioldm_pipe.to(_UpperCamelCase  )
      audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase  )

      _lowercase      :				Optional[Any]      					=       ["hey"]

      _lowercase      :				Optional[int]      					=       audioldm_pipe(_UpperCamelCase      ,    num_inference_steps=1  )
      _lowercase      :				Any      					=       output.audios.shape
      assert audio_shape == (1, 256)

      _lowercase      :				str      					=       audioldm_pipe.vocoder.config
      config.model_in_dim *= 2
      _lowercase      :				List[str]      					=       SpeechTaHifiGan(_UpperCamelCase  ).to(_UpperCamelCase  )
      _lowercase      :				List[str]      					=       audioldm_pipe(_UpperCamelCase      ,    num_inference_steps=1  )
      _lowercase      :				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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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





      """simple docstring"""





      self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_UpperCamelCase  )




@slow
class        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase="cpu"      ,    _UpperCamelCase=torch.floataa      ,    _UpperCamelCase=0  ):





      """simple docstring"""





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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				List[Any]      					=       AudioLDMPipeline.from_pretrained("cvssp/audioldm"  )
      _lowercase      :				Any      					=       audioldm_pipe.to(_UpperCamelCase  )
      audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase  )

      _lowercase      :				List[str]      					=       self.get_inputs(_UpperCamelCase  )
      _lowercase      :				Optional[Any]      					=       25
      _lowercase      :				str      					=       audioldm_pipe(**_UpperCamelCase  ).audios[0]

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

      _lowercase      :				Optional[int]      					=       audio[77230:77240]
      _lowercase      :				List[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]  )
      _lowercase      :				Union[str, Any]      					=       np.abs(expected_slice - audio_slice  ).max()
      assert max_diff < 1E-2






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       AudioLDMPipeline.from_pretrained("cvssp/audioldm"  )
      _lowercase      :				Union[str, Any]      					=       LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config  )
      _lowercase      :				Optional[Any]      					=       audioldm_pipe.to(_UpperCamelCase  )
      audioldm_pipe.set_progress_bar_config(disable=_UpperCamelCase  )

      _lowercase      :				List[str]      					=       self.get_inputs(_UpperCamelCase  )
      _lowercase      :				str      					=       audioldm_pipe(**_UpperCamelCase  ).audios[0]

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

      _lowercase      :				str      					=       audio[27780:27790]
      _lowercase      :				List[str]      					=       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]  )
      _lowercase      :				Optional[int]      					=       np.abs(expected_slice - audio_slice  ).max()
      assert max_diff < 3E-2









'''simple docstring'''
import warnings

from ...utils import logging
from .image_processing_chinese_clip import ChineseCLIPImageProcessor


_snake_case         =  logging.get_logger(__name__)


class        a__  (							lowerCamelCase_  ):


   def __init__(			self      ,    *_UpperCamelCase      ,    **_UpperCamelCase  ):





      """simple docstring"""





      warnings.warn(
          "The class ChineseCLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers."
          " Please use ChineseCLIPImageProcessor instead."      ,    _UpperCamelCase      ,    )
      super().__init__(*_UpperCamelCase      ,    **_UpperCamelCase  )






