






import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import YolosForObjectDetection, YolosModel
  from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor
class 		SCREAMING_SNAKE_CASE__	:

  '''simple docstring'''







  def __init__(      self		:			Dict       ,				lowerCamelCase		:			Dict       ,				lowerCamelCase		:			Any=13       ,				lowerCamelCase		:			List[Any]=[30, 30]       ,				lowerCamelCase		:			Optional[Any]=2       ,				lowerCamelCase		:			Tuple=3       ,				lowerCamelCase		:			int=True       ,				lowerCamelCase		:			Dict=True       ,				lowerCamelCase		:			Tuple=32       ,				lowerCamelCase		:			Optional[int]=5       ,				lowerCamelCase		:			Tuple=4       ,				lowerCamelCase		:			Dict=37       ,				lowerCamelCase		:			int="gelu"       ,				lowerCamelCase		:			Dict=0.1       ,				lowerCamelCase		:			str=0.1       ,				lowerCamelCase		:			Dict=10       ,				lowerCamelCase		:			Any=0.02       ,				lowerCamelCase		:			str=3       ,				lowerCamelCase		:			int=None       ,				lowerCamelCase		:			int=8       ,				lowerCamelCase		:			Any=10       ,				)						->	List[str]:


         """simple docstring"""
         _UpperCAmelCase							=    parent
         _UpperCAmelCase							=    batch_size
         _UpperCAmelCase							=    image_size
         _UpperCAmelCase							=    patch_size
         _UpperCAmelCase							=    num_channels
         _UpperCAmelCase							=    is_training
         _UpperCAmelCase							=    use_labels
         _UpperCAmelCase							=    hidden_size
         _UpperCAmelCase							=    num_hidden_layers
         _UpperCAmelCase							=    num_attention_heads
         _UpperCAmelCase							=    intermediate_size
         _UpperCAmelCase							=    hidden_act
         _UpperCAmelCase							=    hidden_dropout_prob
         _UpperCAmelCase							=    attention_probs_dropout_prob
         _UpperCAmelCase							=    type_sequence_label_size
         _UpperCAmelCase							=    initializer_range
         _UpperCAmelCase							=    num_labels
         _UpperCAmelCase							=    scope
         _UpperCAmelCase							=    n_targets
         _UpperCAmelCase							=    num_detection_tokens
         # we set the expected sequence length (which is used in several tests)
         # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
         _UpperCAmelCase							=    (image_size[1] // patch_size) * (image_size[0] // patch_size)
         _UpperCAmelCase							=    num_patches + 1 + self.num_detection_tokens


  def  lowerCamelCase       (      self		:			List[Any]							)						->	Optional[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]							)

         _UpperCAmelCase							=    None
         if self.use_labels:
                # labels is a list of Dict (each Dict being the labels for a given example in the batch)
                _UpperCAmelCase							=    []
                for i in range(self.batch_size							):
                       _UpperCAmelCase							=    {}
                       _UpperCAmelCase							=    torch.randint(
                           high=self.num_labels       ,				size=(self.n_targets,)       ,				device=lowerCamelCase							)
                       _UpperCAmelCase							=    torch.rand(self.n_targets       ,				4       ,				device=lowerCamelCase							)
                       labels.append(lowerCamelCase							)

         _UpperCAmelCase							=    self.get_config()

         return config, pixel_values, labels


  def  lowerCamelCase       (      self		:			List[str]							)						->	Dict:


         """simple docstring"""
         return YolosConfig(
             image_size=self.image_size       ,				patch_size=self.patch_size       ,				num_channels=self.num_channels       ,				hidden_size=self.hidden_size       ,				num_hidden_layers=self.num_hidden_layers       ,				num_attention_heads=self.num_attention_heads       ,				intermediate_size=self.intermediate_size       ,				hidden_act=self.hidden_act       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				is_decoder=lowerCamelCase       ,				initializer_range=self.initializer_range       ,				num_detection_tokens=self.num_detection_tokens       ,				num_labels=self.num_labels       ,				)


  def  lowerCamelCase       (      self		:			Dict       ,				lowerCamelCase		:			Union[str, Any]       ,				lowerCamelCase		:			str       ,				lowerCamelCase		:			str							)						->	Tuple:


         """simple docstring"""
         _UpperCAmelCase							=    YolosModel(config=lowerCamelCase							)
         model.to(lowerCamelCase							)
         model.eval()
         _UpperCAmelCase							=    model(lowerCamelCase							)
         self.parent.assertEqual(
             result.last_hidden_state.shape       ,				(self.batch_size, self.expected_seq_len, self.hidden_size)							)


  def  lowerCamelCase       (      self		:			List[Any]       ,				lowerCamelCase		:			str       ,				lowerCamelCase		:			Union[str, Any]       ,				lowerCamelCase		:			Tuple							)						->	str:


         """simple docstring"""
         _UpperCAmelCase							=    YolosForObjectDetection(lowerCamelCase							)
         model.to(lowerCamelCase							)
         model.eval()

         _UpperCAmelCase							=    model(pixel_values=lowerCamelCase							)
         _UpperCAmelCase							=    model(lowerCamelCase							)

         self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_detection_tokens, self.num_labels + 1)							)
         self.parent.assertEqual(result.pred_boxes.shape       ,				(self.batch_size, self.num_detection_tokens, 4)							)

         _UpperCAmelCase							=    model(pixel_values=lowerCamelCase       ,				labels=lowerCamelCase							)

         self.parent.assertEqual(result.loss.shape       ,				()							)
         self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_detection_tokens, self.num_labels + 1)							)
         self.parent.assertEqual(result.pred_boxes.shape       ,				(self.batch_size, self.num_detection_tokens, 4)							)





  def  lowerCamelCase       (      self		:			Dict							)						->	Optional[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    self.prepare_config_and_inputs()
         _UpperCAmelCase	,			_UpperCAmelCase	,			_UpperCAmelCase							=    config_and_inputs
         _UpperCAmelCase							=    {"""pixel_values""": pixel_values}
         return config, inputs_dict



@require_torch
class 		SCREAMING_SNAKE_CASE__	(  UpperCAmelCase					,      UpperCAmelCase					,      unittest.TestCase     ):

  '''simple docstring'''







  _lowerCamelCase    =  (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
  _lowerCamelCase    =  (
      {'''feature-extraction''': YolosModel, '''object-detection''': YolosForObjectDetection} if is_torch_available() else {}
  )

  _lowerCamelCase    =  False
  _lowerCamelCase    =  False
  _lowerCamelCase    =  False
  _lowerCamelCase    =  False


  def  lowerCamelCase       (      self		:			Optional[Any]       ,				lowerCamelCase		:			Optional[Any]       ,				lowerCamelCase		:			Optional[Any]       ,				lowerCamelCase		:			Tuple=False							)						->	str:


         """simple docstring"""
         _UpperCAmelCase							=    super()._prepare_for_class(lowerCamelCase       ,				lowerCamelCase       ,				return_labels=lowerCamelCase							)

         if return_labels:
                if model_class.__name__ == "YolosForObjectDetection":
                       _UpperCAmelCase							=    []
                       for i in range(self.model_tester.batch_size							):
                              _UpperCAmelCase							=    {}
                              _UpperCAmelCase							=    torch.ones(
                                  size=(self.model_tester.n_targets,)       ,				device=lowerCamelCase       ,				dtype=torch.long							)
                              _UpperCAmelCase							=    torch.ones(
                                  self.model_tester.n_targets       ,				4       ,				device=lowerCamelCase       ,				dtype=torch.float							)
                              labels.append(lowerCamelCase							)
                       _UpperCAmelCase							=    labels

         return inputs_dict


  def  lowerCamelCase       (      self		:			Dict							)						->	int:


         """simple docstring"""
         _UpperCAmelCase							=    YolosModelTester(self							)
         _UpperCAmelCase							=    ConfigTester(self       ,				config_class=lowerCamelCase       ,				has_text_modality=lowerCamelCase       ,				hidden_size=37							)


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


         """simple docstring"""
         self.config_tester.run_common_tests()


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


         """simple docstring"""
         # YOLOS does not use inputs_embeds
         pass


  def  lowerCamelCase       (      self		:			List[Any]							)						->	str:


         """simple docstring"""
         _UpperCAmelCase	,			_UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                self.assertIsInstance(model.get_input_embeddings()       ,				(nn.Module)							)
                _UpperCAmelCase							=    model.get_output_embeddings()
                self.assertTrue(x is None or isinstance(lowerCamelCase       ,				nn.Linear							)							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	Tuple:


         """simple docstring"""
         _UpperCAmelCase	,			_UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                _UpperCAmelCase							=    inspect.signature(model.forward							)
                # signature.parameters is an OrderedDict => so arg_names order is deterministic
                _UpperCAmelCase							=    [*signature.parameters.keys()]

                _UpperCAmelCase							=    ["""pixel_values"""]
                self.assertListEqual(arg_names[:1]       ,				lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	Dict:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*lowerCamelCase							)


  def  lowerCamelCase       (      self		:			str							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase	,			_UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs_for_common()
         _UpperCAmelCase							=    True

         # in YOLOS, the seq_len is different
         _UpperCAmelCase							=    self.model_tester.expected_seq_len
         for model_class in self.all_model_classes:
                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    False
                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                model.to(lowerCamelCase							)
                model.eval()
                with torch.no_grad():
                       _UpperCAmelCase							=    model(**self._prepare_for_class(lowerCamelCase       ,				lowerCamelCase							)							)
                _UpperCAmelCase							=    outputs.attentions
                self.assertEqual(len(lowerCamelCase							)       ,				self.model_tester.num_hidden_layers							)

                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                model.to(lowerCamelCase							)
                model.eval()
                with torch.no_grad():
                       _UpperCAmelCase							=    model(**self._prepare_for_class(lowerCamelCase       ,				lowerCamelCase							)							)
                _UpperCAmelCase							=    outputs.attentions
                self.assertEqual(len(lowerCamelCase							)       ,				self.model_tester.num_hidden_layers							)

                self.assertListEqual(
                    list(attentions[0].shape[-3:]							)       ,				[self.model_tester.num_attention_heads, seq_len, seq_len]       ,				)
                _UpperCAmelCase							=    len(lowerCamelCase							)

                # Check attention is always last and order is fine
                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    True
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                model.to(lowerCamelCase							)
                model.eval()
                with torch.no_grad():
                       _UpperCAmelCase							=    model(**self._prepare_for_class(lowerCamelCase       ,				lowerCamelCase							)							)

                _UpperCAmelCase							=    1
                self.assertEqual(out_len + added_hidden_states       ,				len(lowerCamelCase							)							)

                _UpperCAmelCase							=    outputs.attentions

                self.assertEqual(len(lowerCamelCase							)       ,				self.model_tester.num_hidden_layers							)
                self.assertListEqual(
                    list(self_attentions[0].shape[-3:]							)       ,				[self.model_tester.num_attention_heads, seq_len, seq_len]       ,				)


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


         """simple docstring"""
         def check_hidden_states_output(lowerCamelCase		:			Dict       ,				lowerCamelCase		:			Optional[int]       ,				lowerCamelCase		:			Dict							):
                _UpperCAmelCase							=    model_class(lowerCamelCase							)
                model.to(lowerCamelCase							)
                model.eval()

                with torch.no_grad():
                       _UpperCAmelCase							=    model(**self._prepare_for_class(lowerCamelCase       ,				lowerCamelCase							)							)

                _UpperCAmelCase							=    outputs.hidden_states

                _UpperCAmelCase							=    getattr(
                    self.model_tester       ,				"""expected_num_hidden_layers"""       ,				self.model_tester.num_hidden_layers + 1							)
                self.assertEqual(len(lowerCamelCase							)       ,				lowerCamelCase							)

                # YOLOS has a different seq_length
                _UpperCAmelCase							=    self.model_tester.expected_seq_len

                self.assertListEqual(
                    list(hidden_states[0].shape[-2:]							)       ,				[seq_length, self.model_tester.hidden_size]       ,				)

         _UpperCAmelCase	,			_UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
                _UpperCAmelCase							=    True
                check_hidden_states_output(lowerCamelCase       ,				lowerCamelCase       ,				lowerCamelCase							)

                # check that output_hidden_states also work using config
                del inputs_dict["output_hidden_states"]
                _UpperCAmelCase							=    True

                check_hidden_states_output(lowerCamelCase       ,				lowerCamelCase       ,				lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	List[Any]:


         """simple docstring"""
         _UpperCAmelCase							=    self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_object_detection(*lowerCamelCase							)





  @slow
  def  lowerCamelCase       (      self		:			str							)						->	Dict:


         """simple docstring"""
         for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _UpperCAmelCase							=    YolosModel.from_pretrained(lowerCamelCase							)
                self.assertIsNotNone(lowerCamelCase							)




def       _SCREAMING_SNAKE_CASE (							)				->						Union[str, Any]:
       _UpperCAmelCase							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
       return image





@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE__	(  unittest.TestCase     ):

  '''simple docstring'''







  @cached_property
  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	Tuple:


         """simple docstring"""
         return AutoImageProcessor.from_pretrained("""hustvl/yolos-small"""							) if is_vision_available() else None





  @slow
  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	List[str]:


         """simple docstring"""
         _UpperCAmelCase							=    YolosForObjectDetection.from_pretrained("""hustvl/yolos-small"""							).to(lowerCamelCase							)

         _UpperCAmelCase							=    self.default_image_processor
         _UpperCAmelCase							=    prepare_img()
         _UpperCAmelCase							=    image_processor(images=lowerCamelCase       ,				return_tensors="""pt"""							).to(lowerCamelCase							)

         # forward pass
         with torch.no_grad():
                _UpperCAmelCase							=    model(inputs.pixel_values							)

         # verify outputs
         _UpperCAmelCase							=    torch.Size((1, 100, 92)							)
         self.assertEqual(outputs.logits.shape       ,				lowerCamelCase							)

         _UpperCAmelCase							=    torch.tensor(
             [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]       ,				device=lowerCamelCase       ,				)
         _UpperCAmelCase							=    torch.tensor(
             [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]       ,				device=lowerCamelCase							)
         self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]       ,				lowerCamelCase       ,				atol=1E-4							)							)
         self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]       ,				lowerCamelCase       ,				atol=1E-4							)							)

         # verify postprocessing
         _UpperCAmelCase							=    image_processor.post_process_object_detection(
             lowerCamelCase       ,				threshold=0.3       ,				target_sizes=[image.size[::-1]]							)[0]
         _UpperCAmelCase							=    torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]							).to(lowerCamelCase							)
         _UpperCAmelCase							=    [75, 75, 17, 63, 17]
         _UpperCAmelCase							=    torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]							).to(lowerCamelCase							)

         self.assertEqual(len(results["""scores"""]							)       ,				5							)
         self.assertTrue(torch.allclose(results["""scores"""]       ,				lowerCamelCase       ,				atol=1E-4							)							)
         self.assertSequenceEqual(results["""labels"""].tolist()       ,				lowerCamelCase							)
         self.assertTrue(torch.allclose(results["""boxes"""][0, :]       ,				lowerCamelCase							)							)


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 SCREAMING_SNAKE_CASE	(		__snake_case				,    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				):

				"""simple docstring"""


				torch.manual_seed(0				)
				snake_case_   		=			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=__UpperCamelCase			,					)
				snake_case_   		=			DDIMScheduler(
				    beta_start=0.0_0085			,					beta_end=0.012			,					beta_schedule='scaled_linear'			,					clip_sample=__UpperCamelCase			,					set_alpha_to_one=__UpperCamelCase			,					)
				torch.manual_seed(0				)
				snake_case_   		=			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_   		=			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_   		=			ClapTextModelWithProjection(__UpperCamelCase				)
				snake_case_   		=			RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'			,					model_max_length=77				)

				snake_case_   		=			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=__UpperCamelCase			,					)

				snake_case_   		=			SpeechTaHifiGan(__UpperCamelCase				)

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


			def       __lowerCAmelCase			(     self			,					__UpperCamelCase			,					__UpperCamelCase=0				):

				"""simple docstring"""


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


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


				snake_case_   		=			'cpu'  # ensure determinism for the device-dependent torch.Generator

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

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

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

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

				"""simple docstring"""


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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			3 * [inputs['prompt']]

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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			3 * [inputs.pop('prompt'				)]

				snake_case_   		=			audioldm_pipe.tokenizer(
				    __UpperCamelCase			,					padding='max_length'			,					max_length=audioldm_pipe.tokenizer.model_max_length			,					truncation=__UpperCamelCase			,					return_tensors='pt'			,					)
				snake_case_   		=			text_inputs['input_ids'].to(__UpperCamelCase				)

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

				snake_case_   		=			prompt_embeds

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

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


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			3 * ['this is a negative prompt']
				snake_case_   		=			negative_prompt
				snake_case_   		=			3 * [inputs['prompt']]

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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			3 * [inputs.pop('prompt'				)]

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

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

					embeds.append(__UpperCamelCase				)

				snake_case_		,							snake_case_   		=			embeds

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

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


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			'egg cracking'
				snake_case_   		=			audioldm_pipe(**__UpperCamelCase			,					negative_prompt=__UpperCamelCase				)
				snake_case_   		=			output.audios[0]

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

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

				"""simple docstring"""


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

				snake_case_   		=			'A hammer hitting a wooden surface'

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

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

				# test num_waveforms_per_prompt=1 (default) for batch of prompts
				snake_case_   		=			2
				snake_case_   		=			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_   		=			2
				snake_case_   		=			audioldm_pipe(__UpperCamelCase			,					num_inference_steps=2			,					num_waveforms_per_prompt=__UpperCamelCase				).audios

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

				# test num_waveforms_per_prompt for batch of prompts
				snake_case_   		=			2
				snake_case_   		=			audioldm_pipe(
				    [prompt] * batch_size			,					num_inference_steps=2			,					num_waveforms_per_prompt=__UpperCamelCase				).audios

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


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


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

				snake_case_   		=			self.get_dummy_inputs(__UpperCamelCase				)
				snake_case_   		=			audioldm_pipe(audio_length_in_s=0.016			,					**__UpperCamelCase				)
				snake_case_   		=			output.audios[0]

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

				snake_case_   		=			audioldm_pipe(audio_length_in_s=0.032			,					**__UpperCamelCase				)
				snake_case_   		=			output.audios[0]

				assert audio.ndim == 1
				assert len(__UpperCamelCase				) / vocoder_sampling_rate == 0.032


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


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

				snake_case_   		=			['hey']

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

				snake_case_   		=			audioldm_pipe.vocoder.config
				config.model_in_dim *= 2
				snake_case_   		=			SpeechTaHifiGan(__UpperCamelCase				).to(__UpperCamelCase				)
				snake_case_   		=			audioldm_pipe(__UpperCamelCase			,					num_inference_steps=1				)
				snake_case_   		=			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				):

				"""simple docstring"""


				self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__UpperCamelCase				)


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


				self._test_inference_batch_single_identical(test_mean_pixel_difference=__UpperCamelCase				)







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

				"""simple docstring"""


				self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__UpperCamelCase				)







@slow
class SCREAMING_SNAKE_CASE	(		unittest.TestCase				):






			"""simple docstring"""




			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


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


			def       __lowerCAmelCase			(     self			,					__UpperCamelCase			,					__UpperCamelCase="cpu"			,					__UpperCamelCase=torch.floataa			,					__UpperCamelCase=0				):

				"""simple docstring"""


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


			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


				snake_case_   		=			AudioLDMPipeline.from_pretrained('cvssp/audioldm'				)
				snake_case_   		=			audioldm_pipe.to(__UpperCamelCase				)
				audioldm_pipe.set_progress_bar_config(disable=__UpperCamelCase				)

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

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

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







			def       __lowerCAmelCase			(     self				):

				"""simple docstring"""


				snake_case_   		=			AudioLDMPipeline.from_pretrained('cvssp/audioldm'				)
				snake_case_   		=			LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config				)
				snake_case_   		=			audioldm_pipe.to(__UpperCamelCase				)
				audioldm_pipe.set_progress_bar_config(disable=__UpperCamelCase				)

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

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

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