





import sys
import turtle







def    A				(							UpperCAmelCase  ,  UpperCAmelCase				):
					return (pa[0] + pa[0]) / 2, (pa[1] + pa[1]) / 2







def    A				(							UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  ):
					my_pen.up()
					my_pen.goto(vertexa[0]  ,  vertexa[1]				)
					my_pen.down()
					my_pen.goto(vertexa[0]  ,  vertexa[1]				)
					my_pen.goto(vertexa[0]  ,  vertexa[1]				)
					my_pen.goto(vertexa[0]  ,  vertexa[1]				)

					if depth == 0:
										return

					triangle(UpperCAmelCase  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  depth - 1				)
					triangle(UpperCAmelCase  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  depth - 1				)
					triangle(UpperCAmelCase  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  get_mid(UpperCAmelCase  ,  UpperCAmelCase				)  ,  depth - 1				)


if __name__ == "__main__":
							if len(sys.argv) != 2:
														raise ValueError(
														    'Correct format for using this script: '
														    'python fractals.py <int:depth_for_fractal>'
														)
							__lowerCAmelCase :Any     =    turtle.Turtle()
							my_pen.ht()
							my_pen.speed(5)
							my_pen.pencolor('red')

							__lowerCAmelCase :Optional[int]     =    [(-1_75, -1_25), (0, 1_75), (1_75, -1_25)]  # vertices of triangle
							triangle(vertices[0], vertices[1], vertices[2], int(sys.argv[1]))






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    ):
		lowerCamelCase__     :Optional[int]   				=		AudioLDMPipeline
		lowerCamelCase__     :List[Any]   				=		TEXT_TO_AUDIO_PARAMS
		lowerCamelCase__     :List[Any]   				=		TEXT_TO_AUDIO_BATCH_PARAMS
		lowerCamelCase__     :Dict   				=		frozenset(
		    [
		        'num_inference_steps',
		        'num_waveforms_per_prompt',
		        'generator',
		        'latents',
		        'output_type',
		        'return_dict',
		        'callback',
		        'callback_steps',
		    ]    )







		def        lowercase       (    self				)      ->      List[str]:


							'''simple docstring'''

							torch.manual_seed(0				)
							_snake_case       :					Union[str, 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=__snake_case ,						)
							_snake_case       :					int				       =   DDIMScheduler(
							    beta_start=0.0_00_85 ,						beta_end=0.0_12 ,						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=[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				)
							_snake_case       :					Tuple				       =   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 ,						)
							_snake_case       :					Optional[int]				       =   ClapTextModelWithProjection(__snake_case				)
							_snake_case       :					Optional[Any]				       =   RobertaTokenizer.from_pretrained("hf-internal-testing/tiny-random-roberta" ,						model_max_length=7_7				)

							_snake_case       :					Dict				       =   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=__snake_case ,						)

							_snake_case       :					Dict				       =   SpeechTaHifiGan(__snake_case				)

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







		def        lowercase       (    self ,						__snake_case ,						__snake_case=0				)      ->      Union[str, Any]:


							'''simple docstring'''

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







		def        lowercase       (    self				)      ->      Any:


							'''simple docstring'''

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

							_snake_case       :					Union[str, Any]				       =   self.get_dummy_components()
							_snake_case       :					Any				       =   AudioLDMPipeline(**__snake_case				)
							_snake_case       :					List[str]				       =   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       :					Any				       =   audioldm_pipe(**__snake_case				)
							_snake_case       :					Tuple				       =   output.audios[0]

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

							_snake_case       :					Any				       =   audio[:1_0]
							_snake_case       :					Dict				       =   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				)      ->      Tuple:


							'''simple docstring'''

							_snake_case       :					Tuple				       =   self.get_dummy_components()
							_snake_case       :					Any				       =   AudioLDMPipeline(**__snake_case				)
							_snake_case       :					Union[str, 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       :					Optional[int]				       =   self.get_dummy_inputs(__snake_case				)
							_snake_case       :					List[str]				       =   3 * [inputs["prompt"]]

							# forward
							_snake_case       :					Dict				       =   audioldm_pipe(**__snake_case				)
							_snake_case       :					Union[str, Any]				       =   output.audios[0]

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

							_snake_case       :					int				       =   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       :					Dict				       =   audioldm_pipe.text_encoder(
							    __snake_case ,						)
							_snake_case       :					Tuple				       =   prompt_embeds.text_embeds
							# additional L_2 normalization over each hidden-state
							_snake_case       :					List[Any]				       =   F.normalize(__snake_case ,						dim=-1				)

							_snake_case       :					Union[str, Any]				       =   prompt_embeds

							# forward
							_snake_case       :					int				       =   audioldm_pipe(**__snake_case				)
							_snake_case       :					Dict				       =   output.audios[0]

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







		def        lowercase       (    self				)      ->      List[str]:


							'''simple docstring'''

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

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

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

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

							_snake_case       :					List[Any]				       =   []
							for p in [prompt, negative_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       :					List[Any]				       =   text_inputs["input_ids"].to(__snake_case				)

												_snake_case       :					List[Any]				       =   audioldm_pipe.text_encoder(
												    __snake_case ,						)
												_snake_case       :					Union[str, Any]				       =   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       , _snake_case       :					int				       =   embeds

							# forward
							_snake_case       :					Any				       =   audioldm_pipe(**__snake_case				)
							_snake_case       :					Dict				       =   output.audios[0]

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







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


							'''simple docstring'''

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

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

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

							_snake_case       :					List[str]				       =   audio[:1_0]
							_snake_case       :					Optional[int]				       =   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				)      ->      Dict:


							'''simple docstring'''

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

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

							# test num_waveforms_per_prompt=1 (default)
							_snake_case       :					Dict				       =   audioldm_pipe(__snake_case ,						num_inference_steps=2				).audios

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

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

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

							# test num_waveforms_per_prompt for batch of prompts
							_snake_case       :					Optional[int]				       =   2
							_snake_case       :					str				       =   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_5_6)







		def        lowercase       (    self				)      ->      int:


							'''simple docstring'''

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

							_snake_case       :					str				       =   self.get_dummy_inputs(__snake_case				)
							_snake_case       :					List[Any]				       =   audioldm_pipe(audio_length_in_s=0.0_16 ,						**__snake_case				)
							_snake_case       :					List[str]				       =   output.audios[0]

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

							_snake_case       :					Tuple				       =   audioldm_pipe(audio_length_in_s=0.0_32 ,						**__snake_case				)
							_snake_case       :					Union[str, Any]				       =   output.audios[0]

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







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


							'''simple docstring'''

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

							_snake_case       :					int				       =   ["hey"]

							_snake_case       :					int				       =   audioldm_pipe(__snake_case ,						num_inference_steps=1				)
							_snake_case       :					List[Any]				       =   output.audios.shape
							assert audio_shape == (1, 2_5_6)

							_snake_case       :					Optional[Any]				       =   audioldm_pipe.vocoder.config
							config.model_in_dim *= 2
							_snake_case       :					str				       =   SpeechTaHifiGan(__snake_case				).to(__snake_case				)
							_snake_case       :					Any				       =   audioldm_pipe(__snake_case ,						num_inference_steps=1				)
							_snake_case       :					Tuple				       =   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				)      ->      Any:


							'''simple docstring'''

							self._test_attention_slicing_forward_pass(test_mean_pixel_difference=__snake_case				)







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


							'''simple docstring'''

							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        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=__snake_case				)






@slow
class        _a(    unittest.TestCase    ):







		def        lowercase       (    self				)      ->      Optional[Any]:


							'''simple docstring'''

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







		def        lowercase       (    self ,						__snake_case ,						__snake_case="cpu" ,						__snake_case=torch.floataa ,						__snake_case=0				)      ->      Any:


							'''simple docstring'''

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







		def        lowercase       (    self				)      ->      Dict:


							'''simple docstring'''

							_snake_case       :					List[Any]				       =   AudioLDMPipeline.from_pretrained("cvssp/audioldm"				)
							_snake_case       :					str				       =   audioldm_pipe.to(__snake_case				)
							audioldm_pipe.set_progress_bar_config(disable=__snake_case				)

							_snake_case       :					Tuple				       =   self.get_inputs(__snake_case				)
							_snake_case       :					Optional[Any]				       =   2_5
							_snake_case       :					str				       =   audioldm_pipe(**__snake_case				).audios[0]

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

							_snake_case       :					Any				       =   audio[7_7_2_3_0:7_7_2_4_0]
							_snake_case       :					str				       =   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]				)
							_snake_case       :					List[str]				       =   np.abs(expected_slice - audio_slice				).max()
							assert max_diff < 1E-2







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


							'''simple docstring'''

							_snake_case       :					Tuple				       =   AudioLDMPipeline.from_pretrained("cvssp/audioldm"				)
							_snake_case       :					Any				       =   LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config				)
							_snake_case       :					int				       =   audioldm_pipe.to(__snake_case				)
							audioldm_pipe.set_progress_bar_config(disable=__snake_case				)

							_snake_case       :					Any				       =   self.get_inputs(__snake_case				)
							_snake_case       :					Optional[Any]				       =   audioldm_pipe(**__snake_case				).audios[0]

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

							_snake_case       :					str				       =   audio[2_7_7_8_0:2_7_7_9_0]
							_snake_case       :					Any				       =   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]				)
							_snake_case       :					Tuple				       =   np.abs(expected_slice - audio_slice				).max()
							assert max_diff < 3E-2