


'''simple docstring'''
import gc
import unittest

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

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

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS


enable_full_determinism()
class      lowercase			(							unittest.TestCase      ):

		"""simple docstring"""

		_a									=       StableDiffusionLDMaDPipeline
		_a									=       TEXT_TO_IMAGE_PARAMS
		_a									=       TEXT_TO_IMAGE_BATCH_PARAMS
		_a									=       TEXT_TO_IMAGE_IMAGE_PARAMS




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							torch.manual_seed(0							)
							UpperCamelCase__     :Union[str, 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					,     )
							UpperCamelCase__     :int							      =				DDIMScheduler(
							    beta_start=0.00085					,     beta_end=0.012					,     beta_schedule='''scaled_linear'''					,     clip_sample=UpperCamelCase_					,     set_alpha_to_one=UpperCamelCase_					,     )
							torch.manual_seed(0							)
							UpperCamelCase__     :Tuple							      =				AutoencoderKL(
							    block_out_channels=[32, 64]					,     in_channels=6					,     out_channels=6					,     down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']					,     up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']					,     latent_channels=4					,     )
							torch.manual_seed(0							)
							UpperCamelCase__     :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=1000					,     )
							UpperCamelCase__     :int							      =				CLIPTextModel(UpperCamelCase_							)
							UpperCamelCase__     :Tuple							      =				CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							)

							UpperCamelCase__     :List[Any]							      =				{
							    '''unet''': unet,
							    '''scheduler''': scheduler,
							    '''vae''': vae,
							    '''text_encoder''': text_encoder,
							    '''tokenizer''': tokenizer,
							    '''safety_checker''': None,
							    '''feature_extractor''': None,
							}
							return components




		def 					lowerCAmelCase__       (	self					,     UpperCamelCase_					,     UpperCamelCase_=0							):

							'''simple docstring'''


							if str(UpperCamelCase_							).startswith('''mps'''							):
												UpperCamelCase__     :Any							      =				torch.manual_seed(UpperCamelCase_							)
							else:
												UpperCamelCase__     :Tuple							      =				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				{
							    '''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 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator

							UpperCamelCase__     :str							      =				self.get_dummy_components()
							UpperCamelCase__     :Optional[Any]							      =				StableDiffusionLDMaDPipeline(**UpperCamelCase_							)
							UpperCamelCase__     :str							      =				ldmad_pipe.to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Any							      =				self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :Optional[Any]							      =				output.rgb, output.depth

							UpperCamelCase__     :str							      =				rgb[0, -3:, -3:, -1]
							UpperCamelCase__     :Dict							      =				depth[0, -3:, -1]

							assert rgb.shape == (1, 64, 64, 3)
							assert depth.shape == (1, 64, 64)

							UpperCamelCase__     :Optional[int]							      =				np.array(
							    [0.37338176, 0.70247, 0.74203193, 0.51643604, 0.58256793, 0.60932136, 0.4181095, 0.48355877, 0.46535262]							)
							UpperCamelCase__     :int							      =				np.array([103.46727, 85.812004, 87.849236]							)

							assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb							).max() < 1e-2
							assert np.abs(image_slice_depth.flatten() - expected_slice_depth							).max() < 1e-2




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[Any]							      =				self.get_dummy_components()
							UpperCamelCase__     :Tuple							      =				StableDiffusionLDMaDPipeline(**UpperCamelCase_							)
							UpperCamelCase__     :Optional[Any]							      =				ldmad_pipe.to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :str							      =				self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__     :Any							      =				3 * [inputs['''prompt''']]

							# forward
							UpperCamelCase__     :Optional[int]							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :List[Any]							      =				output.rgb, output.depth
							UpperCamelCase__     :List[Any]							      =				rgb_slice_a[0, -3:, -3:, -1]
							UpperCamelCase__     :int							      =				depth_slice_a[0, -3:, -1]

							UpperCamelCase__     :Any							      =				self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				3 * [inputs.pop('''prompt'''							)]

							UpperCamelCase__     :str							      =				ldmad_pipe.tokenizer(
							    UpperCamelCase_					,     padding='''max_length'''					,     max_length=ldmad_pipe.tokenizer.model_max_length					,     truncation=UpperCamelCase_					,     return_tensors='''pt'''					,     )
							UpperCamelCase__     :Dict							      =				text_inputs['''input_ids'''].to(UpperCamelCase_							)

							UpperCamelCase__     :str							      =				ldmad_pipe.text_encoder(UpperCamelCase_							)[0]

							UpperCamelCase__     :Any							      =				prompt_embeds

							# forward
							UpperCamelCase__     :List[Any]							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :Tuple							      =				output.rgb, output.depth
							UpperCamelCase__     :int							      =				rgb_slice_a[0, -3:, -3:, -1]
							UpperCamelCase__     :List[Any]							      =				depth_slice_a[0, -3:, -1]

							assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()							).max() < 1e-4
							assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()							).max() < 1e-4




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :int							      =				'''cpu'''  # ensure determinism for the device-dependent torch.Generator
							UpperCamelCase__     :str							      =				self.get_dummy_components()
							UpperCamelCase__     :str							      =				PNDMScheduler(skip_prk_steps=UpperCamelCase_							)
							UpperCamelCase__     :Optional[int]							      =				StableDiffusionLDMaDPipeline(**UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				ldmad_pipe.to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :List[Any]							      =				self.get_dummy_inputs(UpperCamelCase_							)
							UpperCamelCase__     :Optional[Any]							      =				'''french fries'''
							UpperCamelCase__     :List[str]							      =				ldmad_pipe(**UpperCamelCase_					,     negative_prompt=UpperCamelCase_							)

							UpperCamelCase__      ,       UpperCamelCase__     :List[str]							      =				output.rgb, output.depth
							UpperCamelCase__     :List[Any]							      =				rgb[0, -3:, -3:, -1]
							UpperCamelCase__     :List[Any]							      =				depth[0, -3:, -1]

							assert rgb.shape == (1, 64, 64, 3)
							assert depth.shape == (1, 64, 64)

							UpperCamelCase__     :int							      =				np.array(
							    [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217]							)
							UpperCamelCase__     :Union[str, Any]							      =				np.array([107.84738, 84.62802, 89.962135]							)
							assert np.abs(rgb_slice.flatten() - expected_slice_rgb							).max() < 1e-2
							assert np.abs(depth_slice.flatten() - expected_slice_depth							).max() < 1e-2



@slow
@require_torch_gpu
class      lowercase			(							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'''


							UpperCamelCase__     :List[Any]							      =				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
							UpperCamelCase__     :int							      =				np.random.RandomState(UpperCamelCase_							).standard_normal((1, 4, 64, 64)							)
							UpperCamelCase__     :Any							      =				torch.from_numpy(UpperCamelCase_							).to(device=UpperCamelCase_					,     dtype=UpperCamelCase_							)
							UpperCamelCase__     :Union[str, Any]							      =				{
							    '''prompt''': '''a photograph of an astronaut riding a horse''',
							    '''latents''': latents,
							    '''generator''': generator,
							    '''num_inference_steps''': 3,
							    '''guidance_scale''': 7.5,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Union[str, Any]							      =				StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d'''							)
							UpperCamelCase__     :List[Any]							      =				ldmad_pipe.to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Optional[Any]							      =				self.get_inputs(UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :Optional[int]							      =				output.rgb, output.depth
							UpperCamelCase__     :str							      =				rgb[0, -3:, -3:, -1].flatten()
							UpperCamelCase__     :Optional[Any]							      =				rgb[0, -3:, -1].flatten()

							assert rgb.shape == (1, 512, 512, 3)
							assert depth.shape == (1, 512, 512)

							UpperCamelCase__     :Dict							      =				np.array(
							    [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706]							)
							UpperCamelCase__     :List[str]							      =				np.array(
							    [0.9263781, 0.6678672, 0.5486515, 0.92202145, 0.67831135, 0.56253487, 0.9241694, 0.7551478, 0.6459706]							)
							assert np.abs(rgb_slice - expected_slice_rgb							).max() < 3e-3
							assert np.abs(depth_slice - expected_slice_depth							).max() < 3e-3



@nightly
@require_torch_gpu
class      lowercase			(							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'''


							UpperCamelCase__     :str							      =				torch.Generator(device=UpperCamelCase_							).manual_seed(UpperCamelCase_							)
							UpperCamelCase__     :Union[str, Any]							      =				np.random.RandomState(UpperCamelCase_							).standard_normal((1, 4, 64, 64)							)
							UpperCamelCase__     :int							      =				torch.from_numpy(UpperCamelCase_							).to(device=UpperCamelCase_					,     dtype=UpperCamelCase_							)
							UpperCamelCase__     :Union[str, Any]							      =				{
							    '''prompt''': '''a photograph of an astronaut riding a horse''',
							    '''latents''': latents,
							    '''generator''': generator,
							    '''num_inference_steps''': 50,
							    '''guidance_scale''': 7.5,
							    '''output_type''': '''numpy''',
							}
							return inputs




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :Optional[int]							      =				StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d'''							).to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Dict							      =				self.get_inputs(UpperCamelCase_							)
							UpperCamelCase__     :Optional[Any]							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :List[str]							      =				output.rgb, output.depth

							UpperCamelCase__     :Any							      =				0.495586
							UpperCamelCase__     :Optional[Any]							      =				0.33795515
							UpperCamelCase__     :Optional[Any]							      =				112.48518
							UpperCamelCase__     :Union[str, Any]							      =				98.489746
							assert np.abs(expected_rgb_mean - rgb.mean()							) < 1e-3
							assert np.abs(expected_rgb_std - rgb.std()							) < 1e-3
							assert np.abs(expected_depth_mean - depth.mean()							) < 1e-3
							assert np.abs(expected_depth_std - depth.std()							) < 1e-3




		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							UpperCamelCase__     :List[Any]							      =				StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c'''							).to(UpperCamelCase_							)
							ldmad_pipe.set_progress_bar_config(disable=UpperCamelCase_							)

							UpperCamelCase__     :Optional[int]							      =				self.get_inputs(UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				ldmad_pipe(**UpperCamelCase_							)
							UpperCamelCase__      ,       UpperCamelCase__     :Any							      =				output.rgb, output.depth

							UpperCamelCase__     :Dict							      =				0.4194127
							UpperCamelCase__     :Union[str, Any]							      =				0.35375586
							UpperCamelCase__     :int							      =				0.5638502
							UpperCamelCase__     :List[str]							      =				0.34686103

							assert rgb.shape == (1, 512, 512, 3)
							assert depth.shape == (1, 512, 512, 1)
							assert np.abs(expected_rgb_mean - rgb.mean()							) < 1e-3
							assert np.abs(expected_rgb_std - rgb.std()							) < 1e-3
							assert np.abs(expected_depth_mean - depth.mean()							) < 1e-3
							assert np.abs(expected_depth_std - depth.std()							) < 1e-3



'''simple docstring'''
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
class      lowercase			(							A__      ):

		"""simple docstring"""

		_a									=       'Speech2TextFeatureExtractor'
		_a									=       'Speech2TextTokenizer'




		def __init__(	self					,     UpperCamelCase_					,     UpperCamelCase_							):

							'''simple docstring'''


							super().__init__(UpperCamelCase_					,     UpperCamelCase_							)
							UpperCamelCase__     :List[str]							      =				self.feature_extractor
							UpperCamelCase__     :Optional[Any]							      =				False




		def __call__(	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							if self._in_target_context_manager:
												return self.current_processor(*UpperCamelCase_					,     **UpperCamelCase_							)

							if "raw_speech" in kwargs:
												warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''							)
												UpperCamelCase__     :Optional[Any]							      =				kwargs.pop('''raw_speech'''							)
							else:
												UpperCamelCase__     :List[Any]							      =				kwargs.pop('''audio'''					,     UpperCamelCase_							)
							UpperCamelCase__     :List[Any]							      =				kwargs.pop('''sampling_rate'''					,     UpperCamelCase_							)
							UpperCamelCase__     :Dict							      =				kwargs.pop('''text'''					,     UpperCamelCase_							)
							if len(UpperCamelCase_							) > 0:
												UpperCamelCase__     :Tuple							      =				args[0]
												UpperCamelCase__     :Optional[int]							      =				args[1:]

							if audio is None and text is None:
												raise ValueError('''You need to specify either an `audio` or `text` input to process.'''							)

							if audio is not None:
												UpperCamelCase__     :Any							      =				self.feature_extractor(UpperCamelCase_					,     *UpperCamelCase_					,     sampling_rate=UpperCamelCase_					,     **UpperCamelCase_							)
							if text is not None:
												UpperCamelCase__     :Dict							      =				self.tokenizer(UpperCamelCase_					,     **UpperCamelCase_							)

							if text is None:
												return inputs
							elif audio is None:
												return encodings
							else:
												UpperCamelCase__     :Dict							      =				encodings['''input_ids''']
												return inputs




		def 					lowerCAmelCase__       (	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return self.tokenizer.batch_decode(*UpperCamelCase_					,     **UpperCamelCase_							)




		def 					lowerCAmelCase__       (	self					,     *UpperCamelCase_					,     **UpperCamelCase_							):

							'''simple docstring'''


							return self.tokenizer.decode(*UpperCamelCase_					,     **UpperCamelCase_							)




		@contextmanager
		def 					lowerCAmelCase__       (	self							):

							'''simple docstring'''


							warnings.warn(
							    '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
							    '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
							    '''your audio inputs, or in a separate call.'''							)
							UpperCamelCase__     :Optional[Any]							      =				True
							UpperCamelCase__     :Any							      =				self.tokenizer
							yield
							UpperCamelCase__     :Any							      =				self.feature_extractor
							UpperCamelCase__     :Any							      =				False