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


  """simple docstring"""

  A_    							=						AudioLDMPipeline
  A_    							=						TEXT_TO_AUDIO_PARAMS
  A_    							=						TEXT_TO_AUDIO_BATCH_PARAMS
  A_    							=						frozenset(
      [
          '''num_inference_steps''',
          '''num_waveforms_per_prompt''',
          '''generator''',
          '''latents''',
          '''output_type''',
          '''return_dict''',
          '''callback''',
          '''callback_steps''',
      ]      )




  def 		lowerCamelCase__  (		self      :       int      ):







    '''simple docstring'''


    torch.manual_seed(0      )
    lowercase_     =       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_   ,				)
    lowercase_     =       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      )
    lowercase_     =       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_     =       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   ,				)
    lowercase_     =       ClapTextModelWithProjection(lowercase_      )
    lowercase_     =       RobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-random-roberta"""   ,				model_max_length=77      )

    lowercase_     =       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_   ,				)

    lowercase_     =       SpeechTaHifiGan(lowercase_      )

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




  def 		lowerCamelCase__  (		self      :       str   ,				lowercase_      :       Union[str, Any]   ,				lowercase_      :       List[Any]=0      ):







    '''simple docstring'''


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




  def 		lowerCamelCase__  (		self      :       int      ):







    '''simple docstring'''


    lowercase_     =       """cpu"""  # ensure determinism for the device-dependent torch.Generator

    lowercase_     =       self.get_dummy_components()
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       audioldm_pipe(**lowercase_      )
    lowercase_     =       output.audios[0]

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

    lowercase_     =       audio[:10]
    lowercase_     =       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      :       Tuple      ):







    '''simple docstring'''


    lowercase_     =       self.get_dummy_components()
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       3 * [inputs["""prompt"""]]

    # forward
    lowercase_     =       audioldm_pipe(**lowercase_      )
    lowercase_     =       output.audios[0]

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       3 * [inputs.pop("""prompt"""      )]

    lowercase_     =       audioldm_pipe.tokenizer(
        lowercase_   ,				padding="""max_length"""   ,				max_length=audioldm_pipe.tokenizer.model_max_length   ,				truncation=lowercase_   ,				return_tensors="""pt"""   ,				)
    lowercase_     =       text_inputs["""input_ids"""].to(lowercase_      )

    lowercase_     =       audioldm_pipe.text_encoder(
        lowercase_   ,				)
    lowercase_     =       prompt_embeds.text_embeds
    # additional L_2 normalization over each hidden-state
    lowercase_     =       F.normalize(lowercase_   ,				dim=-1      )

    lowercase_     =       prompt_embeds

    # forward
    lowercase_     =       audioldm_pipe(**lowercase_      )
    lowercase_     =       output.audios[0]

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




  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


    lowercase_     =       self.get_dummy_components()
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       3 * ["""this is a negative prompt"""]
    lowercase_     =       negative_prompt
    lowercase_     =       3 * [inputs["""prompt"""]]

    # forward
    lowercase_     =       audioldm_pipe(**lowercase_      )
    lowercase_     =       output.audios[0]

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       3 * [inputs.pop("""prompt"""      )]

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

      lowercase_     =       audioldm_pipe.text_encoder(
          lowercase_   ,				)
      lowercase_     =       text_embeds.text_embeds
      # additional L_2 normalization over each hidden-state
      lowercase_     =       F.normalize(lowercase_   ,				dim=-1      )

      embeds.append(lowercase_      )

    lowercase_     ,	lowercase_     =       embeds

    # forward
    lowercase_     =       audioldm_pipe(**lowercase_      )
    lowercase_     =       output.audios[0]

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




  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


    lowercase_     =       """cpu"""  # ensure determinism for the device-dependent torch.Generator
    lowercase_     =       self.get_dummy_components()
    lowercase_     =       PNDMScheduler(skip_prk_steps=lowercase_      )
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       """egg cracking"""
    lowercase_     =       audioldm_pipe(**lowercase_   ,				negative_prompt=lowercase_      )
    lowercase_     =       output.audios[0]

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

    lowercase_     =       audio[:10]
    lowercase_     =       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      :       Optional[Any]      ):







    '''simple docstring'''


    lowercase_     =       """cpu"""  # ensure determinism for the device-dependent torch.Generator
    lowercase_     =       self.get_dummy_components()
    lowercase_     =       PNDMScheduler(skip_prk_steps=lowercase_      )
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       """A hammer hitting a wooden surface"""

    # test num_waveforms_per_prompt=1 (default)
    lowercase_     =       audioldm_pipe(lowercase_   ,				num_inference_steps=2      ).audios

    assert audios.shape == (1, 256)

    # test num_waveforms_per_prompt=1 (default) for batch of prompts
    lowercase_     =       2
    lowercase_     =       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_     =       2
    lowercase_     =       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
    lowercase_     =       2
    lowercase_     =       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 		lowerCamelCase__  (		self      :       Tuple      ):







    '''simple docstring'''


    lowercase_     =       """cpu"""  # ensure determinism for the device-dependent torch.Generator
    lowercase_     =       self.get_dummy_components()
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )
    lowercase_     =       audioldm_pipe.vocoder.config.sampling_rate

    lowercase_     =       self.get_dummy_inputs(lowercase_      )
    lowercase_     =       audioldm_pipe(audio_length_in_s=0.0_1_6   ,				**lowercase_      )
    lowercase_     =       output.audios[0]

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

    lowercase_     =       audioldm_pipe(audio_length_in_s=0.0_3_2   ,				**lowercase_      )
    lowercase_     =       output.audios[0]

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




  def 		lowerCamelCase__  (		self      :       Optional[Any]      ):







    '''simple docstring'''


    lowercase_     =       self.get_dummy_components()
    lowercase_     =       AudioLDMPipeline(**lowercase_      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       ["""hey"""]

    lowercase_     =       audioldm_pipe(lowercase_   ,				num_inference_steps=1      )
    lowercase_     =       output.audios.shape
    assert audio_shape == (1, 256)

    lowercase_     =       audioldm_pipe.vocoder.config
    config.model_in_dim *= 2
    lowercase_     =       SpeechTaHifiGan(lowercase_      ).to(lowercase_      )
    lowercase_     =       audioldm_pipe(lowercase_   ,				num_inference_steps=1      )
    lowercase_     =       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      :       Optional[int]      ):







    '''simple docstring'''


    self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowercase_      )




  def 		lowerCamelCase__  (		self      :       Optional[int]      ):







    '''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 		lowerCamelCase__  (		self      :       int      ):







    '''simple docstring'''


    self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowercase_      )






@slow
class       _a    (		unittest.TestCase      ):


  """simple docstring"""

  def 		lowerCamelCase__  (		self      :       List[str]      ):







    '''simple docstring'''


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




  def 		lowerCamelCase__  (		self      :       Tuple   ,				lowercase_      :       Optional[Any]   ,				lowercase_      :       int="cpu"   ,				lowercase_      :       int=torch.floataa   ,				lowercase_      :       int=0      ):







    '''simple docstring'''


    lowercase_     =       torch.Generator(device=lowercase_      ).manual_seed(lowercase_      )
    lowercase_     =       np.random.RandomState(lowercase_      ).standard_normal((1, 8, 128, 16)      )
    lowercase_     =       torch.from_numpy(lowercase_      ).to(device=lowercase_   ,				dtype=lowercase_      )
    lowercase_     =       {
        """prompt""": """A hammer hitting a wooden surface""",
        """latents""": latents,
        """generator""": generator,
        """num_inference_steps""": 3,
        """guidance_scale""": 2.5,
    }
    return inputs




  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


    lowercase_     =       AudioLDMPipeline.from_pretrained("""cvssp/audioldm"""      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_inputs(lowercase_      )
    lowercase_     =       25
    lowercase_     =       audioldm_pipe(**lowercase_      ).audios[0]

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

    lowercase_     =       audio[77_230:77_240]
    lowercase_     =       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_     =       np.abs(expected_slice - audio_slice      ).max()
    assert max_diff < 1e-2





  def 		lowerCamelCase__  (		self      :       str      ):







    '''simple docstring'''


    lowercase_     =       AudioLDMPipeline.from_pretrained("""cvssp/audioldm"""      )
    lowercase_     =       LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config      )
    lowercase_     =       audioldm_pipe.to(lowercase_      )
    audioldm_pipe.set_progress_bar_config(disable=lowercase_      )

    lowercase_     =       self.get_inputs(lowercase_      )
    lowercase_     =       audioldm_pipe(**lowercase_      ).audios[0]

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

    lowercase_     =       audio[27_780:27_790]
    lowercase_     =       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_     =       np.abs(expected_slice - audio_slice      ).max()
    assert max_diff < 3e-2


'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


__snake_case						  =		logging.get_logger(__name__)

__snake_case						  =		{
    """microsoft/table-transformer-detection""": (
        """https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json"""
    ),
}





class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						'''table-transformer'''
  A_    							=						['''past_key_values''']
  A_    							=						{
      '''hidden_size''': '''d_model''',
      '''num_attention_heads''': '''encoder_attention_heads''',
  }




  def __init__(		self      :       str   ,				lowercase_      :       str=True   ,				lowercase_      :       List[str]=None   ,				lowercase_      :       Dict=3   ,				lowercase_      :       Optional[Any]=100   ,				lowercase_      :       Optional[int]=6   ,				lowercase_      :       Optional[Any]=2_048   ,				lowercase_      :       List[Any]=8   ,				lowercase_      :       Optional[Any]=6   ,				lowercase_      :       int=2_048   ,				lowercase_      :       Any=8   ,				lowercase_      :       Optional[Any]=0.0   ,				lowercase_      :       int=0.0   ,				lowercase_      :       Dict=True   ,				lowercase_      :       int="relu"   ,				lowercase_      :       Tuple=256   ,				lowercase_      :       Dict=0.1   ,				lowercase_      :       List[str]=0.0   ,				lowercase_      :       List[str]=0.0   ,				lowercase_      :       int=0.0_2   ,				lowercase_      :       List[str]=1.0   ,				lowercase_      :       Optional[int]=False   ,				lowercase_      :       List[Any]="sine"   ,				lowercase_      :       Optional[int]="resnet50"   ,				lowercase_      :       Union[str, Any]=True   ,				lowercase_      :       Union[str, Any]=False   ,				lowercase_      :       List[str]=1   ,				lowercase_      :       Any=5   ,				lowercase_      :       Optional[int]=2   ,				lowercase_      :       Dict=1   ,				lowercase_      :       Optional[int]=1   ,				lowercase_      :       Dict=5   ,				lowercase_      :       Optional[int]=2   ,				lowercase_      :       Optional[Any]=0.1   ,				**lowercase_      :       List[str]   ,				):







    '''simple docstring'''


    if backbone_config is not None and use_timm_backbone:
      raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""      )

    if not use_timm_backbone:
      if backbone_config is None:
        logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""      )
        lowercase_     =       CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]      )
      elif isinstance(lowercase_   ,				lowercase_      ):
        lowercase_     =       backbone_config.get("""model_type"""      )
        lowercase_     =       CONFIG_MAPPING[backbone_model_type]
        lowercase_     =       config_class.from_dict(lowercase_      )
      # set timm attributes to None
      lowercase_     ,	lowercase_     ,	lowercase_     =       None, None, None

    lowercase_     =       use_timm_backbone
    lowercase_     =       backbone_config
    lowercase_     =       num_channels
    lowercase_     =       num_queries
    lowercase_     =       d_model
    lowercase_     =       encoder_ffn_dim
    lowercase_     =       encoder_layers
    lowercase_     =       encoder_attention_heads
    lowercase_     =       decoder_ffn_dim
    lowercase_     =       decoder_layers
    lowercase_     =       decoder_attention_heads
    lowercase_     =       dropout
    lowercase_     =       attention_dropout
    lowercase_     =       activation_dropout
    lowercase_     =       activation_function
    lowercase_     =       init_std
    lowercase_     =       init_xavier_std
    lowercase_     =       encoder_layerdrop
    lowercase_     =       decoder_layerdrop
    lowercase_     =       encoder_layers
    lowercase_     =       auxiliary_loss
    lowercase_     =       position_embedding_type
    lowercase_     =       backbone
    lowercase_     =       use_pretrained_backbone
    lowercase_     =       dilation
    # Hungarian matcher
    lowercase_     =       class_cost
    lowercase_     =       bbox_cost
    lowercase_     =       giou_cost
    # Loss coefficients
    lowercase_     =       mask_loss_coefficient
    lowercase_     =       dice_loss_coefficient
    lowercase_     =       bbox_loss_coefficient
    lowercase_     =       giou_loss_coefficient
    lowercase_     =       eos_coefficient
    super().__init__(is_encoder_decoder=lowercase_   ,				**lowercase_      )




  @property
  def 		lowerCamelCase__  (		self      :       Tuple      ):







    '''simple docstring'''


    return self.encoder_attention_heads





  @property
  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


    return self.d_model






class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						version.parse('''1.11'''      )




  @property
  def 		lowerCamelCase__  (		self      :       int      ):







    '''simple docstring'''


    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ("""pixel_mask""", {0: """batch"""}),
        ]      )




  @property
  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    return 1e-5





  @property
  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    return 12

