
"""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				(					_snake_case		,					unittest.TestCase):
    __lowercase							: Tuple  =					AudioLDMPipeline
    __lowercase							: str  =					TEXT_TO_AUDIO_PARAMS
    __lowercase							: str  =					TEXT_TO_AUDIO_BATCH_PARAMS
    __lowercase							: Optional[int]  =					frozenset(
        [
            """num_inference_steps""",
            """num_waveforms_per_prompt""",
            """generator""",
            """latents""",
            """output_type""",
            """return_dict""",
            """callback""",
            """callback_steps""",
        ])
    def 					lowerCamelCase__	(    self     ):
      torch.manual_seed(0     )
      _snake_case     :						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=snake_case_						,    )
      _snake_case     :						List[Any]						=   DDIMScheduler(
          beta_start=0.00085						,    beta_end=0.012						,    beta_schedule="scaled_linear"						,    clip_sample=snake_case_						,    set_alpha_to_one=snake_case_						,    )
      torch.manual_seed(0     )
      _snake_case     :						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     )
      _snake_case     :						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						,    )
      _snake_case     :						Tuple						=   ClapTextModelWithProjection(snake_case_     )
      _snake_case     :						Union[str, Any]						=   RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta"						,    model_max_length=77     )

      _snake_case     :						int						=   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=snake_case_						,    )

      _snake_case     :						Tuple						=   SpeechTaHifiGan(snake_case_     )

      _snake_case     :						Optional[int]						=   {
          "unet": unet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
          "vocoder": vocoder,
      }
      return components
    def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_=0     ):
      if str(snake_case_     ).startswith("mps"     ):
        _snake_case     :						Optional[Any]						=   torch.manual_seed(snake_case_     )
      else:
        _snake_case     :						List[Any]						=   torch.Generator(device=snake_case_     ).manual_seed(snake_case_     )
      _snake_case     :						Union[str, Any]						=   {
          "prompt": "A hammer hitting a wooden surface",
          "generator": generator,
          "num_inference_steps": 2,
          "guidance_scale": 6.0,
      }
      return inputs
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Optional[Any]						=   "cpu"  # ensure determinism for the device-dependent torch.Generator

      _snake_case     :						Optional[int]						=   self.get_dummy_components()
      _snake_case     :						Optional[int]						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						Any						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						List[str]						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						List[Any]						=   audioldm_pipe(**snake_case_     )
      _snake_case     :						Optional[Any]						=   output.audios[0]

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

      _snake_case     :						Union[str, Any]						=   audio[:10]
      _snake_case     :						int						=   np.array(
          [-0.0050, 0.0050, -0.0060, 0.0033, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0033]     )

      assert np.abs(audio_slice - expected_slice     ).max() < 1E-2
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						int						=   self.get_dummy_components()
      _snake_case     :						Any						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						Any						=   audioldm_pipe.to(snake_case_     )
      _snake_case     :						List[str]						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Union[str, Any]						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						str						=   3 * [inputs["prompt"]]

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

      _snake_case     :						Optional[int]						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						str						=   3 * [inputs.pop("prompt"     )]

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

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

      _snake_case     :						List[Any]						=   prompt_embeds

      # forward
      _snake_case     :						Tuple						=   audioldm_pipe(**snake_case_     )
      _snake_case     :						Optional[int]						=   output.audios[0]

      assert np.abs(audio_a - audio_a     ).max() < 1E-2
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Any						=   self.get_dummy_components()
      _snake_case     :						str						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						str						=   audioldm_pipe.to(snake_case_     )
      _snake_case     :						List[Any]						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						str						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						Any						=   3 * ["this is a negative prompt"]
      _snake_case     :						int						=   negative_prompt
      _snake_case     :						Any						=   3 * [inputs["prompt"]]

      # forward
      _snake_case     :						int						=   audioldm_pipe(**snake_case_     )
      _snake_case     :						List[str]						=   output.audios[0]

      _snake_case     :						str						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						str						=   3 * [inputs.pop("prompt"     )]

      _snake_case     :						Optional[int]						=   []
      for p in [prompt, negative_prompt]:
        _snake_case     :						str						=   audioldm_pipe.tokenizer(
            snake_case_						,    padding="max_length"						,    max_length=audioldm_pipe.tokenizer.model_max_length						,    truncation=snake_case_						,    return_tensors="pt"						,    )
        _snake_case     :						Tuple						=   text_inputs["input_ids"].to(snake_case_     )

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

        embeds.append(snake_case_     )

      _snake_case     :						List[Any]						=   embeds

      # forward
      _snake_case     :						str						=   audioldm_pipe(**snake_case_     )
      _snake_case     :						Tuple						=   output.audios[0]

      assert np.abs(audio_a - audio_a     ).max() < 1E-2
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						str						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
      _snake_case     :						List[Any]						=   self.get_dummy_components()
      _snake_case     :						List[Any]						=   PNDMScheduler(skip_prk_steps=snake_case_     )
      _snake_case     :						int						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						int						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						int						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						Tuple						=   "egg cracking"
      _snake_case     :						Tuple						=   audioldm_pipe(**snake_case_						,    negative_prompt=snake_case_     )
      _snake_case     :						str						=   output.audios[0]

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

      _snake_case     :						Dict						=   audio[:10]
      _snake_case     :						Dict						=   np.array(
          [-0.0051, 0.0050, -0.0060, 0.0034, -0.0026, 0.0033, -0.0027, 0.0033, -0.0028, 0.0032]     )

      assert np.abs(audio_slice - expected_slice     ).max() < 1E-2
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						str						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
      _snake_case     :						Any						=   self.get_dummy_components()
      _snake_case     :						Optional[Any]						=   PNDMScheduler(skip_prk_steps=snake_case_     )
      _snake_case     :						int						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						Dict						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						List[str]						=   "A hammer hitting a wooden surface"

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

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

      # test num_waveforms_per_prompt=1 (default) for batch of prompts
      _snake_case     :						Any						=   2
      _snake_case     :						Union[str, Any]						=   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
      _snake_case     :						Optional[Any]						=   2
      _snake_case     :						Optional[int]						=   audioldm_pipe(snake_case_						,    num_inference_steps=2						,    num_waveforms_per_prompt=snake_case_     ).audios

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

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

      assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_56)
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						str						=   "cpu"  # ensure determinism for the device-dependent torch.Generator
      _snake_case     :						List[Any]						=   self.get_dummy_components()
      _snake_case     :						Union[str, Any]						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						Dict						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )
      _snake_case     :						Any						=   audioldm_pipe.vocoder.config.sampling_rate

      _snake_case     :						Optional[int]						=   self.get_dummy_inputs(snake_case_     )
      _snake_case     :						List[Any]						=   audioldm_pipe(audio_length_in_s=0.016						,    **snake_case_     )
      _snake_case     :						str						=   output.audios[0]

      assert audio.ndim == 1
      assert len(snake_case_     ) / vocoder_sampling_rate == 0.016

      _snake_case     :						Optional[int]						=   audioldm_pipe(audio_length_in_s=0.032						,    **snake_case_     )
      _snake_case     :						Union[str, Any]						=   output.audios[0]

      assert audio.ndim == 1
      assert len(snake_case_     ) / vocoder_sampling_rate == 0.032
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						str						=   self.get_dummy_components()
      _snake_case     :						Union[str, Any]						=   AudioLDMPipeline(**snake_case_     )
      _snake_case     :						Optional[int]						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Any						=   ["hey"]

      _snake_case     :						Any						=   audioldm_pipe(snake_case_						,    num_inference_steps=1     )
      _snake_case     :						Any						=   output.audios.shape
      assert audio_shape == (1, 2_56)

      _snake_case     :						Optional[Any]						=   audioldm_pipe.vocoder.config
      config.model_in_dim *= 2
      _snake_case     :						List[str]						=   SpeechTaHifiGan(snake_case_     ).to(snake_case_     )
      _snake_case     :						List[Any]						=   audioldm_pipe(snake_case_						,    num_inference_steps=1     )
      _snake_case     :						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 					lowerCamelCase__	(    self     ):
      self._test_attention_slicing_forward_pass(test_mean_pixel_difference=snake_case_     )
    def 					lowerCamelCase__	(    self     ):
      self._test_inference_batch_single_identical(test_mean_pixel_difference=snake_case_     )


    @unittest.skipIf(
        torch_device != "cuda" or not is_xformers_available()						,    reason="XFormers attention is only available with CUDA and `xformers` installed"						,    )
    def 					lowerCamelCase__	(    self     ):
      self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=snake_case_     )






@slow
class      _UpperCAmelCase				(					unittest.TestCase):
    def 					lowerCamelCase__	(    self     ):
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()
    def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_="cpu"						,    snake_case_=torch.floataa						,    snake_case_=0     ):
      _snake_case     :						int						=   torch.Generator(device=snake_case_     ).manual_seed(snake_case_     )
      _snake_case     :						int						=   np.random.RandomState(snake_case_     ).standard_normal((1, 8, 1_28, 16)     )
      _snake_case     :						str						=   torch.from_numpy(snake_case_     ).to(device=snake_case_						,    dtype=snake_case_     )
      _snake_case     :						List[str]						=   {
          "prompt": "A hammer hitting a wooden surface",
          "latents": latents,
          "generator": generator,
          "num_inference_steps": 3,
          "guidance_scale": 2.5,
      }
      return inputs
    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						Optional[Any]						=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"     )
      _snake_case     :						Any						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Any						=   self.get_inputs(snake_case_     )
      _snake_case     :						Tuple						=   25
      _snake_case     :						Any						=   audioldm_pipe(**snake_case_     ).audios[0]

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

      _snake_case     :						Tuple						=   audio[7_72_30:7_72_40]
      _snake_case     :						Any						=   np.array(
          [-0.4884, -0.4607, 0.0023, 0.5007, 0.5896, 0.5151, 0.3813, -0.0208, -0.3687, -0.4315]     )
      _snake_case     :						int						=   np.abs(expected_slice - audio_slice     ).max()
      assert max_diff < 1E-2


    def 					lowerCamelCase__	(    self     ):
      _snake_case     :						str						=   AudioLDMPipeline.from_pretrained("cvssp/audioldm"     )
      _snake_case     :						Dict						=   LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config     )
      _snake_case     :						Tuple						=   audioldm_pipe.to(snake_case_     )
      audioldm_pipe.set_progress_bar_config(disable=snake_case_     )

      _snake_case     :						Any						=   self.get_inputs(snake_case_     )
      _snake_case     :						str						=   audioldm_pipe(**snake_case_     ).audios[0]

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

      _snake_case     :						Any						=   audio[2_77_80:2_77_90]
      _snake_case     :						Dict						=   np.array([-0.2131, -0.0873, -0.0124, -0.0189, 0.0569, 0.1373, 0.1883, 0.2886, 0.3297, 0.2212]     )
      _snake_case     :						List[str]						=   np.abs(expected_slice - audio_slice     ).max()
      assert max_diff < 3E-2



"""simple docstring"""
def 				a__   (   a   :				int		):

  """simple docstring"""







  if not isinstance(a       ,     a		):
    raise TypeError("Input value must be an 'int' type"		)

  _snake_case     :						Union[str, Any]						=   0
  while number:
    position += 1
    number >>= 1

  return position


if __name__ == "__main__":
    import doctest

    doctest.testmod()

