
"""simple docstring"""




import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class 	lowerCAmelCase_						(a__ ,					unittest.TestCase  ):


				"""simple docstring"""



				__UpperCamelCase      :						str					       =   LDMTextToImagePipeline
				__UpperCamelCase      :						Dict					       =   TEXT_TO_IMAGE_PARAMS - {
				    '''negative_prompt''',
				    '''negative_prompt_embeds''',
				    '''cross_attention_kwargs''',
				    '''prompt_embeds''',
				}
				__UpperCamelCase      :						List[str]					       =   PipelineTesterMixin.required_optional_params - {
				    '''num_images_per_prompt''',
				    '''callback''',
				    '''callback_steps''',
				}
				__UpperCamelCase      :						Any					       =   TEXT_TO_IMAGE_BATCH_PARAMS
				__UpperCamelCase      :						Union[str, Any]					       =   False



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



											"""simple docstring"""




											torch.manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       Any           =   UNetaDConditionModel(
											    block_out_channels=(32, 64)   ,       layers_per_block=2   ,       sample_size=32   ,       in_channels=4   ,       out_channels=4   ,       down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")   ,       up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")   ,       cross_attention_dim=32   ,       )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   DDIMScheduler(
											    beta_start=0.00085   ,       beta_end=0.012   ,       beta_schedule="""scaled_linear"""   ,       clip_sample=SCREAMING_SNAKE_CASE__   ,       set_alpha_to_one=SCREAMING_SNAKE_CASE__   ,       )
											torch.manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   AutoencoderKL(
											    block_out_channels=(32, 64)   ,       in_channels=3   ,       out_channels=3   ,       down_block_types=("""DownEncoderBlock2D""", """DownEncoderBlock2D""")   ,       up_block_types=("""UpDecoderBlock2D""", """UpDecoderBlock2D""")   ,       latent_channels=4   ,       )
											torch.manual_seed(0    )
											SCREAMING_SNAKE_CASE__						:       List[str]           =   CLIPTextConfig(
											    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   ,       )
											SCREAMING_SNAKE_CASE__						:       Dict           =   CLIPTextModel(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Any           =   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   {
											    """unet""": unet,
											    """scheduler""": scheduler,
											    """vqvae""": vae,
											    """bert""": text_encoder,
											    """tokenizer""": tokenizer,
											}
											return components



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=0    )					->		Optional[int]:



											"""simple docstring"""




											if str(SCREAMING_SNAKE_CASE__    ).startswith("""mps"""    ):
																		SCREAMING_SNAKE_CASE__						:       Optional[int]           =   torch.manual_seed(SCREAMING_SNAKE_CASE__    )
											else:
																		SCREAMING_SNAKE_CASE__						:       int           =   torch.Generator(device=SCREAMING_SNAKE_CASE__    ).manual_seed(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   {
											    """prompt""": """A painting of a squirrel eating a burger""",
											    """generator""": generator,
											    """num_inference_steps""": 2,
											    """guidance_scale""": 6.0,
											    """output_type""": """numpy""",
											}
											return inputs



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[Any]           =   """cpu"""  # ensure determinism for the device-dependent torch.Generator

											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   self.get_dummy_components()
											SCREAMING_SNAKE_CASE__						:       str           =   LDMTextToImagePipeline(**SCREAMING_SNAKE_CASE__    )
											pipe.to(SCREAMING_SNAKE_CASE__    )
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       str           =   self.get_dummy_inputs(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   pipe(**SCREAMING_SNAKE_CASE__    ).images
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   image[0, -3:, -3:, -1]

											assert image.shape == (1, 16, 16, 3)
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]    )

											assert np.abs(image_slice.flatten() - expected_slice    ).max() < 1E-3






@slow
@require_torch_gpu
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




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



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=torch.floataa   ,       SCREAMING_SNAKE_CASE__=0    )					->		List[str]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       List[str]           =   torch.manual_seed(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   np.random.RandomState(SCREAMING_SNAKE_CASE__    ).standard_normal((1, 4, 32, 32)    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   torch.from_numpy(SCREAMING_SNAKE_CASE__    ).to(device=SCREAMING_SNAKE_CASE__   ,       dtype=SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Tuple           =   {
											    """prompt""": """A painting of a squirrel eating a burger""",
											    """latents""": latents,
											    """generator""": generator,
											    """num_inference_steps""": 3,
											    """guidance_scale""": 6.0,
											    """output_type""": """numpy""",
											}
											return inputs



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""    ).to(SCREAMING_SNAKE_CASE__    )
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   self.get_inputs(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   pipe(**SCREAMING_SNAKE_CASE__    ).images
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   image[0, -3:, -3:, -1].flatten()

											assert image.shape == (1, 2_56, 2_56, 3)
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   np.array([0.51825, 0.52850, 0.52543, 0.54258, 0.52304, 0.52569, 0.54363, 0.55276, 0.56878]    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   np.abs(expected_slice - image_slice    ).max()
											assert max_diff < 1E-3






@nightly
@require_torch_gpu
class 	lowerCAmelCase_						(unittest.TestCase  ):


				"""simple docstring"""



				def 	__magic_name__    (self    )					->		str:



											"""simple docstring"""




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



				def 	__magic_name__    (self   ,       SCREAMING_SNAKE_CASE__   ,       SCREAMING_SNAKE_CASE__=torch.floataa   ,       SCREAMING_SNAKE_CASE__=0    )					->		Union[str, Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Dict           =   torch.manual_seed(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Any           =   np.random.RandomState(SCREAMING_SNAKE_CASE__    ).standard_normal((1, 4, 32, 32)    )
											SCREAMING_SNAKE_CASE__						:       str           =   torch.from_numpy(SCREAMING_SNAKE_CASE__    ).to(device=SCREAMING_SNAKE_CASE__   ,       dtype=SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   {
											    """prompt""": """A painting of a squirrel eating a burger""",
											    """latents""": latents,
											    """generator""": generator,
											    """num_inference_steps""": 50,
											    """guidance_scale""": 6.0,
											    """output_type""": """numpy""",
											}
											return inputs



				def 	__magic_name__    (self    )					->		Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__						:       Any           =   LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""    ).to(SCREAMING_SNAKE_CASE__    )
											pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Any           =   self.get_inputs(SCREAMING_SNAKE_CASE__    )
											SCREAMING_SNAKE_CASE__						:       Union[str, Any]           =   pipe(**SCREAMING_SNAKE_CASE__    ).images[0]

											SCREAMING_SNAKE_CASE__						:       str           =   load_numpy(
											    """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy"""    )
											SCREAMING_SNAKE_CASE__						:       Dict           =   np.abs(expected_image - image    ).max()
											assert max_diff < 1E-3










"""simple docstring"""






import datetime
import platform
import subprocess
from typing import Optional, Tuple, Union

import numpy as np







def      _snake_case			(				lowercase__							,    lowercase__		):
				_lowerCamelCase     :			Tuple    =    f'''{sampling_rate}'''
				_lowerCamelCase     :			str    =    '1'
				_lowerCamelCase     :			str    =    'f32le'
				_lowerCamelCase     :			Union[str, Any]    =    [
				    'ffmpeg',
				    '-i',
				    'pipe:0',
				    '-ac',
				    ac,
				    '-ar',
				    ar,
				    '-f',
				    format_for_conversion,
				    '-hide_banner',
				    '-loglevel',
				    'quiet',
				    'pipe:1',
				]

				try:
								with subprocess.Popen(lowercase__							,    stdin=subprocess.PIPE							,    stdout=subprocess.PIPE		) as ffmpeg_process:
												_lowerCamelCase     :			str    =    ffmpeg_process.communicate(lowercase__		)
				except FileNotFoundError as error:
								raise ValueError('ffmpeg was not found but is required to load audio files from filename'		) from error
				_lowerCamelCase     :			List[Any]    =    output_stream[0]
				_lowerCamelCase     :			Tuple    =    np.frombuffer(lowercase__							,    np.floataa		)
				if audio.shape[0] == 0:
								raise ValueError('Malformed soundfile'		)
				return audio







def      _snake_case			(				lowercase__							,    lowercase__							,    lowercase__ = "f32le"							,    ):
				_lowerCamelCase     :			Optional[Any]    =    f'''{sampling_rate}'''
				_lowerCamelCase     :			List[str]    =    '1'
				if format_for_conversion == "s16le":
								_lowerCamelCase     :			List[str]    =    2
				elif format_for_conversion == "f32le":
								_lowerCamelCase     :			List[Any]    =    4
				else:
								raise ValueError(f'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`'''		)

				_lowerCamelCase     :			Dict    =    platform.system()
				if system == "Linux":
								_lowerCamelCase     :			Optional[int]    =    'alsa'
								_lowerCamelCase     :			Optional[Any]    =    'default'
				elif system == "Darwin":
								_lowerCamelCase     :			Optional[int]    =    'avfoundation'
								_lowerCamelCase     :			Any    =    ':0'
				elif system == "Windows":
								_lowerCamelCase     :			Tuple    =    'dshow'
								_lowerCamelCase     :			Tuple    =    'default'

				_lowerCamelCase     :			Optional[int]    =    [
				    'ffmpeg',
				    '-f',
				    format_,
				    '-i',
				    input_,
				    '-ac',
				    ac,
				    '-ar',
				    ar,
				    '-f',
				    format_for_conversion,
				    '-fflags',
				    'nobuffer',
				    '-hide_banner',
				    '-loglevel',
				    'quiet',
				    'pipe:1',
				]
				_lowerCamelCase     :			Tuple    =    int(round(sampling_rate * chunk_length_s		)		) * size_of_sample
				_lowerCamelCase     :			List[Any]    =    _ffmpeg_stream(lowercase__							,    lowercase__		)
				for item in iterator:
								yield item







def      _snake_case			(				lowercase__							,    lowercase__							,    lowercase__ = None							,    lowercase__ = None							,    lowercase__ = "f32le"							,    ):
				if stream_chunk_s is not None:
								_lowerCamelCase     :			int    =    stream_chunk_s
				else:
								_lowerCamelCase     :			Optional[Any]    =    chunk_length_s

				_lowerCamelCase     :			Optional[Any]    =    ffmpeg_microphone(lowercase__							,    lowercase__							,    format_for_conversion=lowercase__		)
				if format_for_conversion == "s16le":
								_lowerCamelCase     :			List[str]    =    np.intaa
								_lowerCamelCase     :			str    =    2
				elif format_for_conversion == "f32le":
								_lowerCamelCase     :			Any    =    np.floataa
								_lowerCamelCase     :			List[Any]    =    4
				else:
								raise ValueError(f'''Unhandled format `{format_for_conversion}`. Please use `s16le` or `f32le`'''		)

				if stride_length_s is None:
								_lowerCamelCase     :			Union[str, Any]    =    chunk_length_s / 6
				_lowerCamelCase     :			Optional[int]    =    int(round(sampling_rate * chunk_length_s		)		) * size_of_sample
				if isinstance(lowercase__							,    (int, float)		):
								_lowerCamelCase     :			Any    =    [stride_length_s, stride_length_s]

				_lowerCamelCase     :			Tuple    =    int(round(sampling_rate * stride_length_s[0]		)		) * size_of_sample
				_lowerCamelCase     :			Optional[Any]    =    int(round(sampling_rate * stride_length_s[1]		)		) * size_of_sample
				_lowerCamelCase     :			List[Any]    =    datetime.datetime.now()
				_lowerCamelCase     :			Optional[int]    =    datetime.timedelta(seconds=lowercase__		)
				for item in chunk_bytes_iter(lowercase__							,    lowercase__							,    stride=(stride_left, stride_right)							,    stream=lowercase__		):
								# Put everything back in numpy scale
								_lowerCamelCase     :			List[Any]    =    np.frombuffer(item['raw']							,    dtype=lowercase__		)
								_lowerCamelCase     :			int    =    (
								    item['stride'][0] // size_of_sample,
								    item['stride'][1] // size_of_sample,
								)
								_lowerCamelCase     :			Optional[int]    =    sampling_rate
								audio_time += delta
								if datetime.datetime.now() > audio_time + 10 * delta:
												# We're late !! SKIP
												continue
								yield item







def      _snake_case			(				lowercase__							,    lowercase__							,    lowercase__							,    lowercase__ = False		):
				_lowerCamelCase     :			int    =    B''
				_lowerCamelCase, _lowerCamelCase     :			Dict    =    stride
				if stride_left + stride_right >= chunk_len:
								raise ValueError(
								    f'''Stride needs to be strictly smaller than chunk_len: ({stride_left}, {stride_right}) vs {chunk_len}'''		)
				_lowerCamelCase     :			str    =    0
				for raw in iterator:
								acc += raw
								if stream and len(lowercase__		) < chunk_len:
												_lowerCamelCase     :			Optional[int]    =    (_stride_left, 0)
												yield {"raw": acc[:chunk_len], "stride": stride, "partial": True}
								else:
												while len(lowercase__		) >= chunk_len:
																# We are flushing the accumulator
																_lowerCamelCase     :			str    =    (_stride_left, stride_right)
																_lowerCamelCase     :			str    =    {'raw': acc[:chunk_len], 'stride': stride}
																if stream:
																				_lowerCamelCase     :			List[Any]    =    False
																yield item
																_lowerCamelCase     :			Optional[Any]    =    stride_left
																_lowerCamelCase     :			str    =    acc[chunk_len - stride_left - stride_right :]
    # Last chunk
				if len(lowercase__		) > stride_left:
								_lowerCamelCase     :			Optional[Any]    =    {'raw': acc, 'stride': (_stride_left, 0)}
								if stream:
												_lowerCamelCase     :			Tuple    =    False
								yield item







def      _snake_case			(				lowercase__							,    lowercase__		):
				_lowerCamelCase     :			int    =    2**24  # 16Mo
				try:
								with subprocess.Popen(lowercase__							,    stdout=subprocess.PIPE							,    bufsize=lowercase__		) as ffmpeg_process:
												while True:
																_lowerCamelCase     :			Optional[Any]    =    ffmpeg_process.stdout.read(lowercase__		)
																if raw == b"":
																				break
																yield raw
				except FileNotFoundError as error:
								raise ValueError('ffmpeg was not found but is required to stream audio files from filename'		) from error