


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




 '''simple docstring'''
 __lowercase					:		List[str]     				=    AudioLDMPipeline
 __lowercase					:		Tuple     				=    TEXT_TO_AUDIO_PARAMS
 __lowercase					:		Tuple     				=    TEXT_TO_AUDIO_BATCH_PARAMS
 __lowercase					:		Optional[Any]     				=    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    )
  __A	:		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=__lowercase   ,   )
  __A	:		Optional[Any]	  =      DDIMScheduler(
      beta_start=0.0_0_0_8_5   ,   beta_end=0.0_1_2   ,   beta_schedule='scaled_linear'   ,   clip_sample=__lowercase   ,   set_alpha_to_one=__lowercase   ,   )
  torch.manual_seed(0    )
  __A	:		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    )
  __A	:		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=1_000   ,   projection_dim=32   ,   )
  __A	:		List[str]	  =      ClapTextModelWithProjection(__lowercase    )
  __A	:		Any	  =      RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'   ,   model_max_length=77    )

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

  __A	:		Optional[Any]	  =      SpeechTaHifiGan(__lowercase    )

  __A	:		str	  =      {
      'unet': unet,
      'scheduler': scheduler,
      'vae': vae,
      'text_encoder': text_encoder,
      'tokenizer': tokenizer,
      'vocoder': vocoder,
  }
  return components


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase=0    ):

  """simple docstring"""







  if str(__lowercase    ).startswith('mps'    ):
   __A	:		Union[str, Any]	  =      torch.manual_seed(__lowercase    )
  else:
   __A	:		Any	  =      torch.Generator(device=__lowercase    ).manual_seed(__lowercase    )
  __A	:		Union[str, Any]	  =      {
      '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"""







  __A	:		List[Any]	  =      'cpu'  # ensure determinism for the device-dependent torch.Generator

  __A	:		Tuple	  =      self.get_dummy_components()
  __A	:		int	  =      AudioLDMPipeline(**__lowercase    )
  __A	:		Tuple	  =      audioldm_pipe.to(__lowercase    )
  audioldm_pipe.set_progress_bar_config(disable=__lowercase    )

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

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

  __A	:		Tuple	  =      audio[:10]
  __A	:		Optional[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 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      self.get_dummy_components()
  __A	:		Tuple	  =      AudioLDMPipeline(**__lowercase    )
  __A	:		Union[str, Any]	  =      audioldm_pipe.to(__lowercase    )
  __A	:		str	  =      audioldm_pipe.to(__lowercase    )
  audioldm_pipe.set_progress_bar_config(disable=__lowercase    )

  __A	:		Optional[int]	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		str	  =      3 * [inputs['prompt']]

  # forward
  __A	:		str	  =      audioldm_pipe(**__lowercase    )
  __A	:		Optional[int]	  =      output.audios[0]

  __A	:		Union[str, Any]	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		Any	  =      3 * [inputs.pop('prompt'    )]

  __A	:		List[Any]	  =      audioldm_pipe.tokenizer(
      __lowercase   ,   padding='max_length'   ,   max_length=audioldm_pipe.tokenizer.model_max_length   ,   truncation=__lowercase   ,   return_tensors='pt'   ,   )
  __A	:		int	  =      text_inputs['input_ids'].to(__lowercase    )

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

  __A	:		Union[str, Any]	  =      prompt_embeds

  # forward
  __A	:		str	  =      audioldm_pipe(**__lowercase    )
  __A	:		List[str]	  =      output.audios[0]

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


 def 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		Union[str, Any]	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		Dict	  =      3 * ['this is a negative prompt']
  __A	:		Dict	  =      negative_prompt
  __A	:		Tuple	  =      3 * [inputs['prompt']]

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

  __A	:		Union[str, Any]	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		int	  =      3 * [inputs.pop('prompt'    )]

  __A	:		Tuple	  =      []
  for p in [prompt, negative_prompt]:
   __A	:		Any	  =      audioldm_pipe.tokenizer(
       __lowercase   ,   padding='max_length'   ,   max_length=audioldm_pipe.tokenizer.model_max_length   ,   truncation=__lowercase   ,   return_tensors='pt'   ,   )
   __A	:		int	  =      text_inputs['input_ids'].to(__lowercase    )

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

   embeds.append(__lowercase    )

  __A  ,__A	:		List[str]	  =      embeds

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

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


 def 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		Tuple	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		Optional[Any]	  =      'egg cracking'
  __A	:		Optional[Any]	  =      audioldm_pipe(**__lowercase   ,   negative_prompt=__lowercase    )
  __A	:		List[str]	  =      output.audios[0]

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

  __A	:		Optional[Any]	  =      audio[:10]
  __A	:		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 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		Union[str, Any]	  =      'A hammer hitting a wooden surface'

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

  assert audios.shape == (1, 256)

  # test num_waveforms_per_prompt=1 (default) for batch of prompts
  __A	:		int	  =      2
  __A	:		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
  __A	:		int	  =      2
  __A	:		str	  =      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
  __A	:		Tuple	  =      2
  __A	:		Tuple	  =      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"""







  __A	:		Optional[int]	  =      'cpu'  # ensure determinism for the device-dependent torch.Generator
  __A	:		List[str]	  =      self.get_dummy_components()
  __A	:		int	  =      AudioLDMPipeline(**__lowercase    )
  __A	:		Optional[int]	  =      audioldm_pipe.to(__lowercase    )
  audioldm_pipe.set_progress_bar_config(disable=__lowercase    )
  __A	:		Optional[int]	  =      audioldm_pipe.vocoder.config.sampling_rate

  __A	:		Dict	  =      self.get_dummy_inputs(__lowercase    )
  __A	:		Optional[int]	  =      audioldm_pipe(audio_length_in_s=0.0_1_6   ,   **__lowercase    )
  __A	:		Tuple	  =      output.audios[0]

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

  __A	:		int	  =      audioldm_pipe(audio_length_in_s=0.0_3_2   ,   **__lowercase    )
  __A	:		List[Any]	  =      output.audios[0]

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


 def 			snake_case__ (   self    ):

  """simple docstring"""







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

  __A	:		Optional[int]	  =      ['hey']

  __A	:		Tuple	  =      audioldm_pipe(__lowercase   ,   num_inference_steps=1    )
  __A	:		int	  =      output.audios.shape
  assert audio_shape == (1, 256)

  __A	:		Union[str, Any]	  =      audioldm_pipe.vocoder.config
  config.model_in_dim *= 2
  __A	:		Any	  =      SpeechTaHifiGan(__lowercase    ).to(__lowercase    )
  __A	:		Any	  =      audioldm_pipe(__lowercase   ,   num_inference_steps=1    )
  __A	:		Dict	  =      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     _lowerCamelCase       (     unittest.TestCase					):




 '''simple docstring'''


 def 			snake_case__ (   self    ):

  """simple docstring"""







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


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase="cpu"   ,   __lowercase=torch.floataa   ,   __lowercase=0    ):

  """simple docstring"""







  __A	:		List[Any]	  =      torch.Generator(device=__lowercase    ).manual_seed(__lowercase    )
  __A	:		Union[str, Any]	  =      np.random.RandomState(__lowercase    ).standard_normal((1, 8, 128, 16)    )
  __A	:		int	  =      torch.from_numpy(__lowercase    ).to(device=__lowercase   ,   dtype=__lowercase    )
  __A	:		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"""







  __A	:		Dict	  =      AudioLDMPipeline.from_pretrained('cvssp/audioldm'    )
  __A	:		List[Any]	  =      audioldm_pipe.to(__lowercase    )
  audioldm_pipe.set_progress_bar_config(disable=__lowercase    )

  __A	:		Union[str, Any]	  =      self.get_inputs(__lowercase    )
  __A	:		int	  =      25
  __A	:		Dict	  =      audioldm_pipe(**__lowercase    ).audios[0]

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

  __A	:		Optional[int]	  =      audio[77_230:77_240]
  __A	:		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]    )
  __A	:		Tuple	  =      np.abs(expected_slice - audio_slice    ).max()
  assert max_diff < 1E-2







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Any	  =      AudioLDMPipeline.from_pretrained('cvssp/audioldm'    )
  __A	:		str	  =      LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config    )
  __A	:		List[str]	  =      audioldm_pipe.to(__lowercase    )
  audioldm_pipe.set_progress_bar_config(disable=__lowercase    )

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

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

  __A	:		int	  =      audio[27_780:27_790]
  __A	:		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]    )
  __A	:		List[Any]	  =      np.abs(expected_slice - audio_slice    ).max()
  assert max_diff < 3E-2










'''simple docstring'''
from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin

@dataclass
class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		torch.FloatTensor


class     _lowerCamelCase       (     snake_case_     ,		snake_case_					):




 '''simple docstring'''


 @register_to_config
 def __init__(   self   ,   __lowercase = 16   ,   __lowercase = 88   ,   __lowercase = None   ,   __lowercase = None   ,   __lowercase = 1   ,   __lowercase = 0.0   ,   __lowercase = 32   ,   __lowercase = None   ,   __lowercase = False   ,   __lowercase = None   ,   __lowercase = "geglu"   ,   __lowercase = True   ,   __lowercase = True   ,   ):

  """simple docstring"""







  super().__init__()
  __A	:		Optional[int]	  =      num_attention_heads
  __A	:		Dict	  =      attention_head_dim
  __A	:		Optional[int]	  =      num_attention_heads * attention_head_dim

  __A	:		Optional[Any]	  =      in_channels

  __A	:		Optional[int]	  =      torch.nn.GroupNorm(num_groups=__lowercase   ,   num_channels=__lowercase   ,   eps=1E-6   ,   affine=__lowercase    )
  __A	:		Optional[Any]	  =      nn.Linear(__lowercase   ,   __lowercase    )

  # 3. Define transformers blocks
  __A	:		List[Any]	  =      nn.ModuleList(
      [
          BasicTransformerBlock(
              __lowercase   ,   __lowercase   ,   __lowercase   ,   dropout=__lowercase   ,   cross_attention_dim=__lowercase   ,   activation_fn=__lowercase   ,   attention_bias=__lowercase   ,   double_self_attention=__lowercase   ,   norm_elementwise_affine=__lowercase   ,   )
          for d in range(__lowercase    )
      ]    )

  __A	:		Union[str, Any]	  =      nn.Linear(__lowercase   ,   __lowercase    )







 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase=None   ,   __lowercase=None   ,   __lowercase=None   ,   __lowercase=1   ,   __lowercase=None   ,   __lowercase = True   ,   ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		int	  =      hidden_states.shape
  __A	:		Union[str, Any]	  =      batch_frames // num_frames

  __A	:		Union[str, Any]	  =      hidden_states

  __A	:		List[str]	  =      hidden_states[None, :].reshape(__lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    )
  __A	:		List[Any]	  =      hidden_states.permute(0   ,   2   ,   1   ,   3   ,   4    )

  __A	:		Dict	  =      self.norm(__lowercase    )
  __A	:		List[Any]	  =      hidden_states.permute(0   ,   3   ,   4   ,   2   ,   1    ).reshape(batch_size * height * width   ,   __lowercase   ,   __lowercase    )

  __A	:		Optional[Any]	  =      self.proj_in(__lowercase    )

  # 2. Blocks
  for block in self.transformer_blocks:
   __A	:		List[Any]	  =      block(
       __lowercase   ,   encoder_hidden_states=__lowercase   ,   timestep=__lowercase   ,   cross_attention_kwargs=__lowercase   ,   class_labels=__lowercase   ,   )

  # 3. Output
  __A	:		Optional[int]	  =      self.proj_out(__lowercase    )
  __A	:		int	  =      (
      hidden_states[None, None, :]
      .reshape(__lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    )
      .permute(0   ,   3   ,   4   ,   1   ,   2    )
      .contiguous()
  )
  __A	:		List[str]	  =      hidden_states.reshape(__lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    )

  __A	:		str	  =      hidden_states + residual

  if not return_dict:
   return (output,)

  return TransformerTemporalModelOutput(sample=__lowercase    )






