



"""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		:				List[str]											=		StableDiffusionLDMaDPipeline
						_A		:				Tuple											=		TEXT_TO_IMAGE_PARAMS
						_A		:				Optional[int]											=		TEXT_TO_IMAGE_BATCH_PARAMS
						_A		:				Dict											=		TEXT_TO_IMAGE_IMAGE_PARAMS







						def     __UpperCamelCase				(self							):
											torch.manual_seed(0							)
											snake_case_       :		int			       =       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 ,		)
											snake_case_       :		Tuple			       =       DDIMScheduler(
											    beta_start=0.00085 ,		beta_end=0.012 ,		beta_schedule="""scaled_linear""" ,		clip_sample=a_ ,		set_alpha_to_one=a_ ,		)
											torch.manual_seed(0							)
											snake_case_       :		Optional[int]			       =       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							)
											snake_case_       :		List[Any]			       =       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 ,		)
											snake_case_       :		int			       =       CLIPTextModel(a_							)
											snake_case_       :		Union[str, Any]			       =       CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""							)

											snake_case_       :		int			       =       {
											    'unet': unet,
											    'scheduler': scheduler,
											    'vae': vae,
											    'text_encoder': text_encoder,
											    'tokenizer': tokenizer,
											    'safety_checker': None,
											    'feature_extractor': None,
											}
											return components







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__=0							):
											if str(a_							).startswith("""mps"""							):
																snake_case_       :		str			       =       torch.manual_seed(a_							)
											else:
																snake_case_       :		Tuple			       =       torch.Generator(device=a_							).manual_seed(a_							)
											snake_case_       :		Dict			       =       {
											    '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     __UpperCamelCase				(self							):
											snake_case_       :		Tuple			       =       'cpu'  # ensure determinism for the device-dependent torch.Generator

											snake_case_       :		List[Any]			       =       self.get_dummy_components()
											snake_case_       :		List[Any]			       =       StableDiffusionLDMaDPipeline(**a_							)
											snake_case_       :		Optional[Any]			       =       ldmad_pipe.to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

											snake_case_       :		int			       =       self.get_dummy_inputs(a_							)
											snake_case_       :		Union[str, Any]			       =       ldmad_pipe(**a_							)
											snake_case_       :		List[str]			       =       output.rgb, output.depth

											snake_case_       :		List[str]			       =       rgb[0, -3:, -3:, -1]
											snake_case_       :		List[Any]			       =       depth[0, -3:, -1]

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

											snake_case_       :		str			       =       np.array(
											    [0.37338176, 0.70247, 0.74203193, 0.51643604, 0.58256793, 0.60932136, 0.4181095, 0.48355877, 0.46535262]							)
											snake_case_       :		Tuple			       =       np.array([1_03.4_67_27, 85.81_20_04, 87.84_92_36]							)

											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     __UpperCamelCase				(self							):
											snake_case_       :		List[str]			       =       self.get_dummy_components()
											snake_case_       :		Tuple			       =       StableDiffusionLDMaDPipeline(**a_							)
											snake_case_       :		Tuple			       =       ldmad_pipe.to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

											snake_case_       :		Dict			       =       self.get_dummy_inputs(a_							)
											snake_case_       :		Optional[int]			       =       3 * [inputs['prompt']]

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

											snake_case_       :		List[Any]			       =       self.get_dummy_inputs(a_							)
											snake_case_       :		Any			       =       3 * [inputs.pop("""prompt"""							)]

											snake_case_       :		List[Any]			       =       ldmad_pipe.tokenizer(
											    a_ ,		padding="""max_length""" ,		max_length=ldmad_pipe.tokenizer.model_max_length ,		truncation=a_ ,		return_tensors="""pt""" ,		)
											snake_case_       :		int			       =       text_inputs['input_ids'].to(a_							)

											snake_case_       :		int			       =       ldmad_pipe.text_encoder(a_							)[0]

											snake_case_       :		int			       =       prompt_embeds

											# forward
											snake_case_       :		int			       =       ldmad_pipe(**a_							)
											snake_case_       :		int			       =       output.rgb, output.depth
											snake_case_       :		Dict			       =       rgb_slice_a[0, -3:, -3:, -1]
											snake_case_       :		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     __UpperCamelCase				(self							):
											snake_case_       :		Optional[Any]			       =       'cpu'  # ensure determinism for the device-dependent torch.Generator
											snake_case_       :		Any			       =       self.get_dummy_components()
											snake_case_       :		Dict			       =       PNDMScheduler(skip_prk_steps=a_							)
											snake_case_       :		int			       =       StableDiffusionLDMaDPipeline(**a_							)
											snake_case_       :		int			       =       ldmad_pipe.to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

											snake_case_       :		Union[str, Any]			       =       self.get_dummy_inputs(a_							)
											snake_case_       :		List[Any]			       =       'french fries'
											snake_case_       :		List[str]			       =       ldmad_pipe(**a_ ,		negative_prompt=a_							)

											snake_case_       :		Optional[int]			       =       output.rgb, output.depth
											snake_case_       :		Optional[int]			       =       rgb[0, -3:, -3:, -1]
											snake_case_       :		Optional[int]			       =       depth[0, -3:, -1]

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

											snake_case_       :		str			       =       np.array(
											    [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217]							)
											snake_case_       :		str			       =       np.array([1_07.8_47_38, 84.6_28_02, 89.96_21_35]							)
											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     __UpperCamelCase				(self							):
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__="cpu" ,		lowercase__=torch.floataa ,		lowercase__=0							):
											snake_case_       :		List[Any]			       =       torch.Generator(device=a_							).manual_seed(a_							)
											snake_case_       :		Optional[Any]			       =       np.random.RandomState(a_							).standard_normal((1, 4, 64, 64)							)
											snake_case_       :		Any			       =       torch.from_numpy(a_							).to(device=a_ ,		dtype=a_							)
											snake_case_       :		Dict			       =       {
											    '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     __UpperCamelCase				(self							):
											snake_case_       :		List[Any]			       =       StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""							)
											snake_case_       :		Tuple			       =       ldmad_pipe.to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

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

											assert rgb.shape == (1, 5_12, 5_12, 3)
											assert depth.shape == (1, 5_12, 5_12)

											snake_case_       :		Tuple			       =       np.array(
											    [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706]							)
											snake_case_       :		Tuple			       =       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     __UpperCamelCase				(self							):
											super().tearDown()
											gc.collect()
											torch.cuda.empty_cache()







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__="cpu" ,		lowercase__=torch.floataa ,		lowercase__=0							):
											snake_case_       :		Tuple			       =       torch.Generator(device=a_							).manual_seed(a_							)
											snake_case_       :		int			       =       np.random.RandomState(a_							).standard_normal((1, 4, 64, 64)							)
											snake_case_       :		str			       =       torch.from_numpy(a_							).to(device=a_ ,		dtype=a_							)
											snake_case_       :		Optional[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     __UpperCamelCase				(self							):
											snake_case_       :		Optional[int]			       =       StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""							).to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

											snake_case_       :		Dict			       =       self.get_inputs(a_							)
											snake_case_       :		str			       =       ldmad_pipe(**a_							)
											snake_case_       :		Optional[int]			       =       output.rgb, output.depth

											snake_case_       :		Optional[int]			       =       0.495586
											snake_case_       :		int			       =       0.33795515
											snake_case_       :		List[Any]			       =       1_12.4_85_18
											snake_case_       :		Optional[Any]			       =       98.48_97_46
											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     __UpperCamelCase				(self							):
											snake_case_       :		Tuple			       =       StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d-4c"""							).to(a_							)
											ldmad_pipe.set_progress_bar_config(disable=a_							)

											snake_case_       :		int			       =       self.get_inputs(a_							)
											snake_case_       :		Dict			       =       ldmad_pipe(**a_							)
											snake_case_       :		str			       =       output.rgb, output.depth

											snake_case_       :		List[Any]			       =       0.4194127
											snake_case_       :		Union[str, Any]			       =       0.35375586
											snake_case_       :		int			       =       0.5638502
											snake_case_       :		Optional[int]			       =       0.34686103

											assert rgb.shape == (1, 5_12, 5_12, 3)
											assert depth.shape == (1, 5_12, 5_12, 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 os


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_doctest_list.py
a							   =				'.'


if __name__ == "__main__":
	a							   =				os.path.join(REPO_PATH, 'utils/documentation_tests.txt')
	a							   =				[]
	a							   =				[]
	with open(doctest_file_path) as fp:
		for line in fp:
			a							   =				line.strip()
			a							   =				os.path.join(REPO_PATH, line)
			if not (os.path.isfile(path) or os.path.isdir(path)):
				non_existent_paths.append(line)
			all_paths.append(path)
	if len(non_existent_paths) > 0:
		a							   =				'\n'.join(non_existent_paths)
		raise ValueError(F"""`utils/documentation_tests.txt` contains non-existent paths:\n{non_existent_paths}""")
	if all_paths != sorted(all_paths):
		raise ValueError('Files in `utils/documentation_tests.txt` are not in alphabetical order.')




