



'''simple docstring'''







import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModelWithProjection

from diffusers import (
    AutoencoderKL,
    DPMSolverMultistepScheduler,
    PNDMScheduler,
    StableDiffusionImageVariationPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import IMAGE_VARIATION_BATCH_PARAMS, IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()




class 				__lowerCAmelCase(							lowerCAmelCase__		,			lowerCAmelCase__		,			lowerCAmelCase__		,			unittest.TestCase      ):
			__snake_case  :		Tuple		=      StableDiffusionImageVariationPipeline
			__snake_case  :		Optional[int]		=      IMAGE_VARIATION_PARAMS
			__snake_case  :		List[Any]		=      IMAGE_VARIATION_BATCH_PARAMS
			__snake_case  :		Any		=      frozenset([]      )
			# TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
			__snake_case  :		List[str]		=      frozenset([]      )






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					torch.manual_seed(0	)
					SCREAMING_SNAKE_CASE_						:Dict										=      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_						:Dict										=      PNDMScheduler(skip_prk_steps=SCREAMING_SNAKE_CASE	)
					torch.manual_seed(0	)
					SCREAMING_SNAKE_CASE_						:int										=      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_						:Any										=      CLIPVisionConfig(
					    hidden_size=32      ,       projection_dim=32      ,       intermediate_size=37      ,       layer_norm_eps=1E-05      ,       num_attention_heads=4      ,       num_hidden_layers=5      ,       image_size=32      ,       patch_size=4      ,       )
					SCREAMING_SNAKE_CASE_						:str										=      CLIPVisionModelWithProjection(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:str										=      CLIPImageProcessor(crop_size=32      ,       size=32	)

					SCREAMING_SNAKE_CASE_						:List[str]										=      {
					    'unet': unet,
					    'scheduler': scheduler,
					    'vae': vae,
					    'image_encoder': image_encoder,
					    'feature_extractor': feature_extractor,
					    'safety_checker': None,
					}
					return components






			def 				_lowercase	(   self  :							Any      ,       SCREAMING_SNAKE_CASE  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]=0	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[str]										=      floats_tensor((1, 3, 32, 32)      ,       rng=random.Random(SCREAMING_SNAKE_CASE	)	)
					SCREAMING_SNAKE_CASE_						:str										=      image.cpu().permute(0      ,       2      ,       3      ,       1	)[0]
					SCREAMING_SNAKE_CASE_						:int										=      Image.fromarray(np.uinta(SCREAMING_SNAKE_CASE	)	).convert('RGB'	).resize((32, 32)	)
					if str(SCREAMING_SNAKE_CASE	).startswith('mps'	):
							SCREAMING_SNAKE_CASE_						:Dict										=      torch.manual_seed(SCREAMING_SNAKE_CASE	)
					else:
							SCREAMING_SNAKE_CASE_						:Tuple										=      torch.Generator(device=SCREAMING_SNAKE_CASE	).manual_seed(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      {
					    'image': image,
					    'generator': generator,
					    'num_inference_steps': 2,
					    'guidance_scale': 6.0,
					    'output_type': 'numpy',
					}
					return inputs






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Any										=      'cpu'  # ensure determinism for the device-dependent torch.Generator
					SCREAMING_SNAKE_CASE_						:Any										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Any										=      StableDiffusionImageVariationPipeline(**SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      sd_pipe.to(SCREAMING_SNAKE_CASE	)
					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:int										=      self.get_dummy_inputs(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:str										=      sd_pipe(**SCREAMING_SNAKE_CASE	).images
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      image[0, -3:, -3:, -1]

					assert image.shape == (1, 64, 64, 3)
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      np.array([0.52_39, 0.57_23, 0.47_96, 0.50_49, 0.55_50, 0.46_85, 0.53_29, 0.48_91, 0.49_21]	)

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






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[str]										=      'cpu'  # ensure determinism for the device-dependent torch.Generator
					SCREAMING_SNAKE_CASE_						:List[Any]										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Tuple										=      StableDiffusionImageVariationPipeline(**SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      sd_pipe.to(SCREAMING_SNAKE_CASE	)
					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:int										=      self.get_dummy_inputs(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:List[Any]										=      2 * [inputs['image']]
					SCREAMING_SNAKE_CASE_						:Dict										=      sd_pipe(**SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Tuple										=      output.images

					SCREAMING_SNAKE_CASE_						:int										=      image[-1, -3:, -3:, -1]

					assert image.shape == (2, 64, 64, 3)
					SCREAMING_SNAKE_CASE_						:List[Any]										=      np.array([0.68_92, 0.56_37, 0.58_36, 0.57_71, 0.62_54, 0.64_09, 0.55_80, 0.55_69, 0.52_89]	)

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






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					super().test_inference_batch_single_identical(expected_max_diff=3E-3	)




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






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







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






			def 				_lowercase	(   self  :							Optional[int]      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Dict="cpu"      ,       SCREAMING_SNAKE_CASE  :							Any=torch.floataa      ,       SCREAMING_SNAKE_CASE  :							Tuple=0	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.Generator(device=SCREAMING_SNAKE_CASE	).manual_seed(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:List[str]										=      load_image(
					    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
					    '/stable_diffusion_imgvar/input_image_vermeer.png'	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      np.random.RandomState(SCREAMING_SNAKE_CASE	).standard_normal((1, 4, 64, 64)	)
					SCREAMING_SNAKE_CASE_						:str										=      torch.from_numpy(SCREAMING_SNAKE_CASE	).to(device=SCREAMING_SNAKE_CASE      ,       dtype=SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:int										=      {
					    'image': init_image,
					    'latents': latents,
					    'generator': generator,
					    'num_inference_steps': 3,
					    'guidance_scale': 7.5,
					    'output_type': 'numpy',
					}
					return inputs






			def 				_lowercase	(   self  :							Dict	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      StableDiffusionImageVariationPipeline.from_pretrained(
					    'lambdalabs/sd-image-variations-diffusers'      ,       safety_checker=SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:List[str]										=      sd_pipe.to(SCREAMING_SNAKE_CASE	)
					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:str										=      self.get_inputs(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sd_pipe(**SCREAMING_SNAKE_CASE	).images
					SCREAMING_SNAKE_CASE_						:int										=      image[0, -3:, -3:, -1].flatten()

					assert image.shape == (1, 512, 512, 3)
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      np.array([0.8_44_91, 0.9_07_89, 0.7_57_08, 0.7_87_34, 0.8_34_85, 0.7_00_99, 0.6_69_38, 0.6_87_27, 0.6_13_79]	)
					assert np.abs(image_slice - expected_slice	).max() < 6E-3






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Dict										=      0

					def callback_fn(SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							torch.FloatTensor	) -> None:
							SCREAMING_SNAKE_CASE_						:List[Any]										=      True
							nonlocal number_of_steps
							number_of_steps += 1
							if step == 1:
									SCREAMING_SNAKE_CASE_						:List[Any]										=      latents.detach().cpu().numpy()
									assert latents.shape == (1, 4, 64, 64)
									SCREAMING_SNAKE_CASE_						:Tuple										=      latents[0, -3:, -3:, -1]
									SCREAMING_SNAKE_CASE_						:Tuple										=      np.array(
									    [-0.16_21, 0.28_37, -0.79_79, -0.12_21, -1.30_57, 0.76_81, -2.11_91, 0.04_64, 1.63_09]	)

									assert np.abs(latents_slice.flatten() - expected_slice	).max() < 5E-2
							elif step == 2:
									SCREAMING_SNAKE_CASE_						:List[Any]										=      latents.detach().cpu().numpy()
									assert latents.shape == (1, 4, 64, 64)
									SCREAMING_SNAKE_CASE_						:Dict										=      latents[0, -3:, -3:, -1]
									SCREAMING_SNAKE_CASE_						:Dict										=      np.array([0.62_99, 1.75_00, 1.19_92, -2.15_82, -1.89_94, 0.73_34, -0.70_90, 1.01_37, 1.52_73]	)

									assert np.abs(latents_slice.flatten() - expected_slice	).max() < 5E-2

					SCREAMING_SNAKE_CASE_						:List[Any]										=      False

					SCREAMING_SNAKE_CASE_						:Any										=      StableDiffusionImageVariationPipeline.from_pretrained(
					    'fusing/sd-image-variations-diffusers'      ,       safety_checker=SCREAMING_SNAKE_CASE      ,       torch_dtype=torch.floataa      ,       )
					pipe.to(SCREAMING_SNAKE_CASE	)
					pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)
					pipe.enable_attention_slicing()

					SCREAMING_SNAKE_CASE_						:str										=      self.get_inputs(SCREAMING_SNAKE_CASE      ,       dtype=torch.floataa	)
					pipe(**SCREAMING_SNAKE_CASE      ,       callback=SCREAMING_SNAKE_CASE      ,       callback_steps=1	)
					assert callback_fn.has_been_called
					assert number_of_steps == inputs["num_inference_steps"]






			def 				_lowercase	(   self  :							Any	):




					"""simple docstring"""







					torch.cuda.empty_cache()
					torch.cuda.reset_max_memory_allocated()
					torch.cuda.reset_peak_memory_stats()

					SCREAMING_SNAKE_CASE_						:List[Any]										=      'fusing/sd-image-variations-diffusers'
					SCREAMING_SNAKE_CASE_						:Tuple										=      StableDiffusionImageVariationPipeline.from_pretrained(
					    SCREAMING_SNAKE_CASE      ,       safety_checker=SCREAMING_SNAKE_CASE      ,       torch_dtype=torch.floataa	)
					SCREAMING_SNAKE_CASE_						:Dict										=      pipe.to(SCREAMING_SNAKE_CASE	)
					pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)
					pipe.enable_attention_slicing(1	)
					pipe.enable_sequential_cpu_offload()

					SCREAMING_SNAKE_CASE_						:List[str]										=      self.get_inputs(SCREAMING_SNAKE_CASE      ,       dtype=torch.floataa	)
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      pipe(**SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Any										=      torch.cuda.max_memory_allocated()
					# make sure that less than 2.6 GB is allocated
					assert mem_bytes < 2.6 * 10**9




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






			def 				_lowercase	(   self  :							Tuple	):




					"""simple docstring"""







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






			def 				_lowercase	(   self  :							Union[str, Any]      ,       SCREAMING_SNAKE_CASE  :							int      ,       SCREAMING_SNAKE_CASE  :							Any="cpu"      ,       SCREAMING_SNAKE_CASE  :							List[Any]=torch.floataa      ,       SCREAMING_SNAKE_CASE  :							List[str]=0	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:int										=      torch.Generator(device=SCREAMING_SNAKE_CASE	).manual_seed(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      load_image(
					    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
					    '/stable_diffusion_imgvar/input_image_vermeer.png'	)
					SCREAMING_SNAKE_CASE_						:int										=      np.random.RandomState(SCREAMING_SNAKE_CASE	).standard_normal((1, 4, 64, 64)	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      torch.from_numpy(SCREAMING_SNAKE_CASE	).to(device=SCREAMING_SNAKE_CASE      ,       dtype=SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Any										=      {
					    'image': init_image,
					    'latents': latents,
					    'generator': generator,
					    'num_inference_steps': 50,
					    'guidance_scale': 7.5,
					    'output_type': 'numpy',
					}
					return inputs






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'	)
					sd_pipe.to(SCREAMING_SNAKE_CASE	)
					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:List[str]										=      self.get_inputs(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sd_pipe(**SCREAMING_SNAKE_CASE	).images[0]

					SCREAMING_SNAKE_CASE_						:str										=      load_numpy(
					    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
					    '/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy'	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      np.abs(expected_image - image	).max()
					assert max_diff < 1E-3






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      StableDiffusionImageVariationPipeline.from_pretrained('fusing/sd-image-variations-diffusers'	)
					SCREAMING_SNAKE_CASE_						:int										=      DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config	)
					sd_pipe.to(SCREAMING_SNAKE_CASE	)
					sd_pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE	)

					SCREAMING_SNAKE_CASE_						:Any										=      self.get_inputs(SCREAMING_SNAKE_CASE	)
					SCREAMING_SNAKE_CASE_						:List[Any]										=      25
					SCREAMING_SNAKE_CASE_						:List[str]										=      sd_pipe(**SCREAMING_SNAKE_CASE	).images[0]

					SCREAMING_SNAKE_CASE_						:str										=      load_numpy(
					    'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
					    '/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy'	)
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      np.abs(expected_image - image	).max()
					assert max_diff < 1E-3




import os

from packaging import version

from .. import __version__
from .accelerate_utils import apply_forward_hook
from .constants import (
    CONFIG_NAME,
    DEPRECATED_REVISION_ARGS,
    DIFFUSERS_CACHE,
    DIFFUSERS_DYNAMIC_MODULE_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    ONNX_EXTERNAL_WEIGHTS_NAME,
    ONNX_WEIGHTS_NAME,
    SAFETENSORS_WEIGHTS_NAME,
    TEXT_ENCODER_ATTN_MODULE,
    WEIGHTS_NAME,
)
from .deprecation_utils import deprecate
from .doc_utils import replace_example_docstring
from .dynamic_modules_utils import get_class_from_dynamic_module
from .hub_utils import (
    HF_HUB_OFFLINE,
    _add_variant,
    _get_model_file,
    extract_commit_hash,
    http_user_agent,
)
from .import_utils import (
    BACKENDS_MAPPING,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    DummyObject,
    OptionalDependencyNotAvailable,
    is_accelerate_available,
    is_accelerate_version,
    is_bsa_available,
    is_flax_available,
    is_ftfy_available,
    is_inflect_available,
    is_invisible_watermark_available,
    is_k_diffusion_available,
    is_k_diffusion_version,
    is_librosa_available,
    is_note_seq_available,
    is_omegaconf_available,
    is_onnx_available,
    is_safetensors_available,
    is_scipy_available,
    is_tensorboard_available,
    is_tf_available,
    is_torch_available,
    is_torch_version,
    is_torchsde_available,
    is_transformers_available,
    is_transformers_version,
    is_unidecode_available,
    is_wandb_available,
    is_xformers_available,
    requires_backends,
)
from .logging import get_logger
from .outputs import BaseOutput
from .pil_utils import PIL_INTERPOLATION, numpy_to_pil, pt_to_pil
from .torch_utils import is_compiled_module, randn_tensor


if is_torch_available():
		from .testing_utils import (
		    floats_tensor,
		    load_hf_numpy,
		    load_image,
		    load_numpy,
		    load_pt,
		    nightly,
		    parse_flag_from_env,
		    print_tensor_test,
		    require_torch_a,
		    require_torch_gpu,
		    skip_mps,
		    slow,
		    torch_all_close,
		    torch_device,
		)
		from .torch_utils import maybe_allow_in_graph

from .testing_utils import export_to_gif, export_to_video


lowerCamelCase		=						get_logger(__name__)






def 	lowerCamelCase_       (			_a			):




		"""simple docstring"""






		if version.parse(_a			) < version.parse(_a			):
				if "dev" in min_version:
						lowerCAmelCase__  :     Tuple       	=   (
						    '''This example requires a source install from HuggingFace diffusers (see '''
						    '''`https://huggingface.co/docs/diffusers/installation#install-from-source`),'''
						)
				else:
						lowerCAmelCase__  :     Dict       	=   f'This example requires a minimum version of {min_version},'
				error_message += f' but the version found is {__version__}.\n'
				raise ImportError(_a			)




