


'''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				:		Optional[Any]    							=						AudioLDMPipeline
 __lowercase				:		Any    							=						TEXT_TO_AUDIO_PARAMS
 __lowercase				:		Optional[Any]    							=						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  :		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=__lowercase      ,      )
  __A  :		int       =			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[Any]       =			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  :		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  :		str       =			ClapTextModelWithProjection(__lowercase						)
  __A  :		Tuple       =			RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'      ,      model_max_length=77						)

  __A  :		List[Any]       =			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  :		int       =			{
      '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  :		int       =			torch.manual_seed(__lowercase						)
  else:
   __A  :		Optional[int]       =			torch.Generator(device=__lowercase						).manual_seed(__lowercase						)
  __A  :		str       =			{
      '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  :		Tuple       =			'cpu'  # ensure determinism for the device-dependent torch.Generator

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

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

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

  __A  :		int       =			audio[:10]
  __A  :		Dict       =			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  :		Union[str, Any]       =			self.get_dummy_components()
  __A  :		Any       =			AudioLDMPipeline(**__lowercase						)
  __A  :		List[Any]       =			audioldm_pipe.to(__lowercase						)
  __A  :		List[str]       =			audioldm_pipe.to(__lowercase						)
  audioldm_pipe.set_progress_bar_config(disable=__lowercase						)

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

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

  __A  :		Tuple       =			self.get_dummy_inputs(__lowercase						)
  __A  :		Tuple       =			3 * [inputs.pop('prompt'						)]

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

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

  __A  :		Any       =			prompt_embeds

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

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


 def  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		Optional[int]       =			self.get_dummy_inputs(__lowercase						)
  __A  :		Optional[int]       =			3 * ['this is a negative prompt']
  __A  :		str       =			negative_prompt
  __A  :		Optional[int]       =			3 * [inputs['prompt']]

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

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

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

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

   embeds.append(__lowercase						)

  __A     ,__A  :		Union[str, Any]       =			embeds

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

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


 def  snake_case__   (      self						):

  """simple docstring"""







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

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

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

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

  """simple docstring"""







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

  __A  :		Tuple       =			'A hammer hitting a wooden surface'

  # test num_waveforms_per_prompt=1 (default)
  __A  :		Any       =			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  :		Union[str, Any]       =			2
  __A  :		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
  __A  :		Dict       =			2
  __A  :		Tuple       =			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  :		Optional[Any]       =			2
  __A  :		Union[str, Any]       =			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  :		Any       =			'cpu'  # ensure determinism for the device-dependent torch.Generator
  __A  :		List[str]       =			self.get_dummy_components()
  __A  :		Union[str, Any]       =			AudioLDMPipeline(**__lowercase						)
  __A  :		List[Any]       =			audioldm_pipe.to(__lowercase						)
  audioldm_pipe.set_progress_bar_config(disable=__lowercase						)
  __A  :		List[Any]       =			audioldm_pipe.vocoder.config.sampling_rate

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

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

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

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


 def  snake_case__   (      self						):

  """simple docstring"""







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

  __A  :		Tuple       =			['hey']

  __A  :		Union[str, Any]       =			audioldm_pipe(__lowercase      ,      num_inference_steps=1						)
  __A  :		Any       =			output.audios.shape
  assert audio_shape == (1, 256)

  __A  :		Optional[int]       =			audioldm_pipe.vocoder.config
  config.model_in_dim *= 2
  __A  :		Tuple       =			SpeechTaHifiGan(__lowercase						).to(__lowercase						)
  __A  :		List[str]       =			audioldm_pipe(__lowercase      ,      num_inference_steps=1						)
  __A  :		List[str]       =			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  :		str       =			torch.Generator(device=__lowercase						).manual_seed(__lowercase						)
  __A  :		List[str]       =			np.random.RandomState(__lowercase						).standard_normal((1, 8, 128, 16)						)
  __A  :		Optional[int]       =			torch.from_numpy(__lowercase						).to(device=__lowercase      ,      dtype=__lowercase						)
  __A  :		str       =			{
      '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  :		Any       =			AudioLDMPipeline.from_pretrained('cvssp/audioldm'						)
  __A  :		Any       =			audioldm_pipe.to(__lowercase						)
  audioldm_pipe.set_progress_bar_config(disable=__lowercase						)

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

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

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







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Tuple       =			AudioLDMPipeline.from_pretrained('cvssp/audioldm'						)
  __A  :		int       =			LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config						)
  __A  :		Dict       =			audioldm_pipe.to(__lowercase						)
  audioldm_pipe.set_progress_bar_config(disable=__lowercase						)

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

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

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










'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_ :     Dict								=      logging.get_logger(__name__)

UpperCAmelCase_ :     Optional[int]								=      {
    'sayakpaul/vit-msn-base': 'https://huggingface.co/sayakpaul/vit-msn-base/resolve/main/config.json',
    # See all ViT MSN models at https://huggingface.co/models?filter=vit_msn
}

class 						_lowerCamelCase					(    snake_case_				):




 '''simple docstring'''
 __lowercase				:		Tuple    							=						'''vit_msn'''


 def __init__(      self      ,      __lowercase=768      ,      __lowercase=12      ,      __lowercase=12      ,      __lowercase=3_072      ,      __lowercase="gelu"      ,      __lowercase=0.0      ,      __lowercase=0.0      ,      __lowercase=0.0_2      ,      __lowercase=1E-06      ,      __lowercase=224      ,      __lowercase=16      ,      __lowercase=3      ,      __lowercase=True      ,      **__lowercase      ,      ):

  """simple docstring"""







  super().__init__(**__lowercase						)

  __A  :		Tuple       =			hidden_size
  __A  :		Optional[int]       =			num_hidden_layers
  __A  :		str       =			num_attention_heads
  __A  :		Dict       =			intermediate_size
  __A  :		Optional[Any]       =			hidden_act
  __A  :		Union[str, Any]       =			hidden_dropout_prob
  __A  :		Optional[Any]       =			attention_probs_dropout_prob
  __A  :		List[str]       =			initializer_range
  __A  :		Dict       =			layer_norm_eps
  __A  :		List[Any]       =			image_size
  __A  :		Optional[int]       =			patch_size
  __A  :		List[Any]       =			num_channels
  __A  :		Optional[Any]       =			qkv_bias






