





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       (    UpperCamelCase__						,				unittest.TestCase):
      _lowercase				:					Dict						=			AudioLDMPipeline
      _lowercase				:					Tuple						=			TEXT_TO_AUDIO_PARAMS
      _lowercase				:					str						=			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    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        torch.manual_seed(0							)
        a__	:					List[Any]										=UNetaDConditionModel(
            block_out_channels=(3_2, 6_4)		,      layers_per_block=2		,      sample_size=3_2		,      in_channels=4		,      out_channels=4		,      down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")		,      up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")		,      cross_attention_dim=(3_2, 6_4)		,      class_embed_type="simple_projection"		,      projection_class_embeddings_input_dim=3_2		,      class_embeddings_concat=lowerCAmelCase__		,      )
        a__	:					Optional[int]										=DDIMScheduler(
            beta_start=0.0_00_85		,      beta_end=0.0_12		,      beta_schedule="scaled_linear"		,      clip_sample=lowerCAmelCase__		,      set_alpha_to_one=lowerCAmelCase__		,      )
        torch.manual_seed(0							)
        a__	:					str										=AutoencoderKL(
            block_out_channels=[3_2, 6_4]		,      in_channels=1		,      out_channels=1		,      down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]		,      up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]		,      latent_channels=4		,      )
        torch.manual_seed(0							)
        a__	:					int										=ClapTextConfig(
            bos_token_id=0		,      eos_token_id=2		,      hidden_size=3_2		,      intermediate_size=3_7		,      layer_norm_eps=1E-05		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1_0_0_0		,      projection_dim=3_2		,      )
        a__	:					str										=ClapTextModelWithProjection(lowerCAmelCase__							)
        a__	:					int										=RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta"		,      model_max_length=7_7							)

        a__	:					Any										=SpeechTaHifiGanConfig(
            model_in_dim=8		,      sampling_rate=1_6_0_0_0		,      upsample_initial_channel=1_6		,      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=lowerCAmelCase__		,      )

        a__	:					Dict										=SpeechTaHifiGan(lowerCAmelCase__							)

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







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__=0							)   ->						Optional[int]:



        '''simple docstring'''




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







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					Tuple										="cpu"  # ensure determinism for the device-dependent torch.Generator

        a__	:					List[Any]										=self.get_dummy_components()
        a__	:					List[Any]										=AudioLDMPipeline(**lowerCAmelCase__							)
        a__	:					Tuple										=audioldm_pipe.to(lowerCAmelCase__							)
        audioldm_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

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

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) == 2_5_6

        a__	:					Optional[Any]										=audio[:1_0]
        a__	:					Optional[int]										=np.array(
            [-0.00_50, 0.00_50, -0.00_60, 0.00_33, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_33]							)

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







      def    _lowercase  (    self							)   ->						int:



        '''simple docstring'''




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

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

        # forward
        a__	:					List[Any]										=audioldm_pipe(**lowerCAmelCase__							)
        a__	:					Optional[int]										=output.audios[0]

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

        a__	:					str										=audioldm_pipe.tokenizer(
            lowerCAmelCase__		,      padding="max_length"		,      max_length=audioldm_pipe.tokenizer.model_max_length		,      truncation=lowerCAmelCase__		,      return_tensors="pt"		,      )
        a__	:					Any										=text_inputs["input_ids"].to(lowerCAmelCase__							)

        a__	:					int										=audioldm_pipe.text_encoder(
            lowerCAmelCase__		,      )
        a__	:					Any										=prompt_embeds.text_embeds
        # additional L_2 normalization over each hidden-state
        a__	:					Any										=F.normalize(lowerCAmelCase__		,      dim=-1							)

        a__	:					List[Any]										=prompt_embeds

        # forward
        a__	:					int										=audioldm_pipe(**lowerCAmelCase__							)
        a__	:					Optional[Any]										=output.audios[0]

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







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Any										=self.get_dummy_components()
        a__	:					Union[str, Any]										=AudioLDMPipeline(**lowerCAmelCase__							)
        a__	:					Optional[Any]										=audioldm_pipe.to(lowerCAmelCase__							)
        a__	:					str										=audioldm_pipe.to(lowerCAmelCase__							)
        audioldm_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					Any										=self.get_dummy_inputs(lowerCAmelCase__							)
        a__	:					Optional[int]										=3 * ["this is a negative prompt"]
        a__	:					int										=negative_prompt
        a__	:					List[str]										=3 * [inputs["prompt"]]

        # forward
        a__	:					List[str]										=audioldm_pipe(**lowerCAmelCase__							)
        a__	:					List[Any]										=output.audios[0]

        a__	:					int										=self.get_dummy_inputs(lowerCAmelCase__							)
        a__	:					Dict										=3 * [inputs.pop("prompt"							)]

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

          a__	:					int										=audioldm_pipe.text_encoder(
              lowerCAmelCase__		,      )
          a__	:					str										=text_embeds.text_embeds
          # additional L_2 normalization over each hidden-state
          a__	:					int										=F.normalize(lowerCAmelCase__		,      dim=-1							)

          embeds.append(lowerCAmelCase__							)

        a__					,							a__	:					str										=embeds

        # forward
        a__	:					List[Any]										=audioldm_pipe(**lowerCAmelCase__							)
        a__	:					str										=output.audios[0]

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







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




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

        a__	:					List[Any]										=self.get_dummy_inputs(lowerCAmelCase__							)
        a__	:					int										="egg cracking"
        a__	:					str										=audioldm_pipe(**lowerCAmelCase__		,      negative_prompt=lowerCAmelCase__							)
        a__	:					List[str]										=output.audios[0]

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) == 2_5_6

        a__	:					Optional[Any]										=audio[:1_0]
        a__	:					List[str]										=np.array(
            [-0.00_51, 0.00_50, -0.00_60, 0.00_34, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_32]							)

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







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




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

        a__	:					int										="A hammer hitting a wooden surface"

        # test num_waveforms_per_prompt=1 (default)
        a__	:					int										=audioldm_pipe(lowerCAmelCase__		,      num_inference_steps=2							).audios

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

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

        assert audios.shape == (batch_size, 2_5_6)

        # test num_waveforms_per_prompt for single prompt
        a__	:					List[Any]										=2
        a__	:					Any										=audioldm_pipe(lowerCAmelCase__		,      num_inference_steps=2		,      num_waveforms_per_prompt=lowerCAmelCase__							).audios

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

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

        assert audios.shape == (batch_size * num_waveforms_per_prompt, 2_5_6)







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




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

        a__	:					str										=self.get_dummy_inputs(lowerCAmelCase__							)
        a__	:					Union[str, Any]										=audioldm_pipe(audio_length_in_s=0.0_16		,      **lowerCAmelCase__							)
        a__	:					Optional[int]										=output.audios[0]

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) / vocoder_sampling_rate == 0.0_16

        a__	:					Dict										=audioldm_pipe(audio_length_in_s=0.0_32		,      **lowerCAmelCase__							)
        a__	:					List[str]										=output.audios[0]

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) / vocoder_sampling_rate == 0.0_32







      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__	:					List[str]										=self.get_dummy_components()
        a__	:					Tuple										=AudioLDMPipeline(**lowerCAmelCase__							)
        a__	:					List[str]										=audioldm_pipe.to(lowerCAmelCase__							)
        audioldm_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					str										=["hey"]

        a__	:					int										=audioldm_pipe(lowerCAmelCase__		,      num_inference_steps=1							)
        a__	:					str										=output.audios.shape
        assert audio_shape == (1, 2_5_6)

        a__	:					Any										=audioldm_pipe.vocoder.config
        config.model_in_dim *= 2
        a__	:					Dict										=SpeechTaHifiGan(lowerCAmelCase__							).to(lowerCAmelCase__							)
        a__	:					List[str]										=audioldm_pipe(lowerCAmelCase__		,      num_inference_steps=1							)
        a__	:					Optional[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_5_6)







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowerCAmelCase__							)







      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        self._test_inference_batch_single_identical(test_mean_pixel_difference=lowerCAmelCase__							)







      @unittest.skipIf(
          torch_device != "cuda" or not is_xformers_available()		,      reason="XFormers attention is only available with CUDA and `xformers` installed"		,      )
      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCAmelCase__							)







@slow
class 				__lowerCAmelCase       (    unittest.TestCase):







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




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







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__="cpu"		,      lowerCAmelCase__=torch.floataa		,      lowerCAmelCase__=0							)   ->						List[str]:



        '''simple docstring'''




        a__	:					Optional[Any]										=torch.Generator(device=lowerCAmelCase__							).manual_seed(lowerCAmelCase__							)
        a__	:					str										=np.random.RandomState(lowerCAmelCase__							).standard_normal((1, 8, 1_2_8, 1_6)							)
        a__	:					Optional[Any]										=torch.from_numpy(lowerCAmelCase__							).to(device=lowerCAmelCase__		,      dtype=lowerCAmelCase__							)
        a__	:					List[str]										={
            "prompt": "A hammer hitting a wooden surface",
            "latents": latents,
            "generator": generator,
            "num_inference_steps": 3,
            "guidance_scale": 2.5,
        }
        return inputs







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					Any										=AudioLDMPipeline.from_pretrained("cvssp/audioldm"							)
        a__	:					str										=audioldm_pipe.to(lowerCAmelCase__							)
        audioldm_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					Any										=self.get_inputs(lowerCAmelCase__							)
        a__	:					Optional[Any]										=2_5
        a__	:					Union[str, Any]										=audioldm_pipe(**lowerCAmelCase__							).audios[0]

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) == 8_1_9_2_0

        a__	:					Union[str, Any]										=audio[7_7_2_3_0:7_7_2_4_0]
        a__	:					Union[str, Any]										=np.array(
            [-0.48_84, -0.46_07, 0.00_23, 0.50_07, 0.58_96, 0.51_51, 0.38_13, -0.02_08, -0.36_87, -0.43_15]							)
        a__	:					Optional[Any]										=np.abs(expected_slice - audio_slice							).max()
        assert max_diff < 1E-2







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					int										=AudioLDMPipeline.from_pretrained("cvssp/audioldm"							)
        a__	:					Optional[int]										=LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config							)
        a__	:					str										=audioldm_pipe.to(lowerCAmelCase__							)
        audioldm_pipe.set_progress_bar_config(disable=lowerCAmelCase__							)

        a__	:					Any										=self.get_inputs(lowerCAmelCase__							)
        a__	:					List[Any]										=audioldm_pipe(**lowerCAmelCase__							).audios[0]

        assert audio.ndim == 1
        assert len(lowerCAmelCase__							) == 8_1_9_2_0

        a__	:					int										=audio[2_7_7_8_0:2_7_7_9_0]
        a__	:					Optional[int]										=np.array([-0.21_31, -0.08_73, -0.01_24, -0.01_89, 0.05_69, 0.13_73, 0.18_83, 0.28_86, 0.32_97, 0.22_12]							)
        a__	:					Any										=np.abs(expected_slice - audio_slice							).max()
        assert max_diff < 3E-2










from collections import Counter

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

__lowerCAmelCase	: List[Any]  				=datasets.load_iris()

__lowerCAmelCase	: Tuple  				=np.array(data['data'])
__lowerCAmelCase	: Dict  				=np.array(data['target'])
__lowerCAmelCase	: List[str]  				=data['target_names']

__lowerCAmelCase				,__lowerCAmelCase				,__lowerCAmelCase				,__lowerCAmelCase	: str  				=train_test_split(X, y)


def        _UpperCamelCase							(      lowercase__    ,       lowercase__      ):
						return np.linalg.norm(np.array(lowercase__      ) - np.array(lowercase__      )      )


def        _UpperCamelCase							(      lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__    ,       lowercase__=5      ):
						__SCREAMING_SNAKE_CASE						:     Optional[int]					=		zip(lowercase__    ,       lowercase__      )
						# List of distances of all points from the point to be classified
						__SCREAMING_SNAKE_CASE						:     Dict					=		[]
						for data_point in data:
												__SCREAMING_SNAKE_CASE						:     Tuple					=		euclidean_distance(data_point[0]    ,       lowercase__      )
												distances.append((distance, data_point[1])      )
						# Choosing 'k' points with the least distances.
						__SCREAMING_SNAKE_CASE						:     int					=		[i[1] for i in sorted(lowercase__      )[:k]]
						# Most commonly occurring class among them
						# is the class into which the point is classified
						__SCREAMING_SNAKE_CASE						:     Any					=		Counter(lowercase__      ).most_common(1      )[0][0]
						return classes[result]


if __name__ == "__main__":
						print(classifier(X_train, y_train, classes, [4.4, 3.1, 1.3, 1.4]))
