






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        a__      (	lowerCAmelCase__	,       lowerCAmelCase__	,       lowerCAmelCase__	,       unittest.TestCase		):
		UpperCAmelCase_							:       Dict 					= StableDiffusionImageVariationPipeline
		UpperCAmelCase_							:       Optional[int] 					= IMAGE_VARIATION_PARAMS
		UpperCAmelCase_							:       str 					= IMAGE_VARIATION_BATCH_PARAMS
		UpperCAmelCase_							:       str 					= frozenset([]		)
		# TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
		UpperCAmelCase_							:       Dict 					= frozenset([]		)






		def 					_lowerCamelCase					(      self						)				->				str:
				torch.manual_seed(0						)
				__A    =							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							, )
				__A    =							PNDMScheduler(skip_prk_steps=lowercase__						)
				torch.manual_seed(0						)
				__A    =							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						)
				__A    =							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							, )
				__A    =							CLIPVisionModelWithProjection(lowercase__						)
				__A    =							CLIPImageProcessor(crop_size=32							, size=32						)

				__A    =							{
				    "unet": unet,
				    "scheduler": scheduler,
				    "vae": vae,
				    "image_encoder": image_encoder,
				    "feature_extractor": feature_extractor,
				    "safety_checker": None,
				}
				return components






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__=0						)				->				str:
				__A    =							floats_tensor((1, 3, 32, 32)							, rng=random.Random(lowercase__						)						)
				__A    =							image.cpu().permute(0							, 2							, 3							, 1						)[0]
				__A    =							Image.fromarray(np.uinta(lowercase__						)						).convert("RGB"						).resize((32, 32)						)
				if str(lowercase__						).startswith("mps"						):
						__A    =							torch.manual_seed(lowercase__						)
				else:
						__A    =							torch.Generator(device=lowercase__						).manual_seed(lowercase__						)
				__A    =							{
				    "image": image,
				    "generator": generator,
				    "num_inference_steps": 2,
				    "guidance_scale": 6.0,
				    "output_type": "numpy",
				}
				return inputs






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
				__A    =							self.get_dummy_components()
				__A    =							StableDiffusionImageVariationPipeline(**lowercase__						)
				__A    =							sd_pipe.to(lowercase__						)
				sd_pipe.set_progress_bar_config(disable=lowercase__						)

				__A    =							self.get_dummy_inputs(lowercase__						)
				__A    =							sd_pipe(**lowercase__						).images
				__A    =							image[0, -3:, -3:, -1]

				assert image.shape == (1, 64, 64, 3)
				__A    =							np.array([0.5239, 0.5723, 0.4796, 0.5049, 0.5550, 0.4685, 0.5329, 0.4891, 0.4921]						)

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






		def 					_lowerCamelCase					(      self						)				->				str:
				__A    =							"cpu"  # ensure determinism for the device-dependent torch.Generator
				__A    =							self.get_dummy_components()
				__A    =							StableDiffusionImageVariationPipeline(**lowercase__						)
				__A    =							sd_pipe.to(lowercase__						)
				sd_pipe.set_progress_bar_config(disable=lowercase__						)

				__A    =							self.get_dummy_inputs(lowercase__						)
				__A    =							2 * [inputs["image"]]
				__A    =							sd_pipe(**lowercase__						)

				__A    =							output.images

				__A    =							image[-1, -3:, -3:, -1]

				assert image.shape == (2, 64, 64, 3)
				__A    =							np.array([0.6892, 0.5637, 0.5836, 0.5771, 0.6254, 0.6409, 0.5580, 0.5569, 0.5289]						)

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






		def 					_lowerCamelCase					(      self						)				->				Optional[int]:
				super().test_inference_batch_single_identical(expected_max_diff=3e-3						)





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






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__="cpu"							, lowercase__=torch.floataa							, lowercase__=0						)				->				Dict:
				__A    =							torch.Generator(device=lowercase__						).manual_seed(lowercase__						)
				__A    =							load_image(
				    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
				    "/stable_diffusion_imgvar/input_image_vermeer.png"						)
				__A    =							np.random.RandomState(lowercase__						).standard_normal((1, 4, 64, 64)						)
				__A    =							torch.from_numpy(lowercase__						).to(device=lowercase__							, dtype=lowercase__						)
				__A    =							{
				    "image": init_image,
				    "latents": latents,
				    "generator": generator,
				    "num_inference_steps": 3,
				    "guidance_scale": 7.5,
				    "output_type": "numpy",
				}
				return inputs






		def 					_lowerCamelCase					(      self						)				->				Optional[int]:
				__A    =							StableDiffusionImageVariationPipeline.from_pretrained(
				    "lambdalabs/sd-image-variations-diffusers"							, safety_checker=lowercase__						)
				__A    =							sd_pipe.to(lowercase__						)
				sd_pipe.set_progress_bar_config(disable=lowercase__						)

				__A    =							self.get_inputs(lowercase__						)
				__A    =							sd_pipe(**lowercase__						).images
				__A    =							image[0, -3:, -3:, -1].flatten()

				assert image.shape == (1, 512, 512, 3)
				__A    =							np.array([0.8_4491, 0.9_0789, 0.7_5708, 0.7_8734, 0.8_3485, 0.7_0099, 0.6_6938, 0.6_8727, 0.6_1379]						)
				assert np.abs(image_slice - expected_slice						).max() < 6e-3






		def 					_lowerCamelCase					(      self						)				->				str:
				__A    =							0

				def callback_fn(lowercase__							, lowercase__							, lowercase__						) -> None:
						__A    =							True
						nonlocal number_of_steps
						number_of_steps += 1
						if step == 1:
								__A    =							latents.detach().cpu().numpy()
								assert latents.shape == (1, 4, 64, 64)
								__A    =							latents[0, -3:, -3:, -1]
								__A    =							np.array(
								    [-0.1621, 0.2837, -0.7979, -0.1221, -1.3057, 0.7681, -2.1191, 0.0464, 1.6309]						)

								assert np.abs(latents_slice.flatten() - expected_slice						).max() < 5e-2
						elif step == 2:
								__A    =							latents.detach().cpu().numpy()
								assert latents.shape == (1, 4, 64, 64)
								__A    =							latents[0, -3:, -3:, -1]
								__A    =							np.array([0.6299, 1.7500, 1.1992, -2.1582, -1.8994, 0.7334, -0.7090, 1.0137, 1.5273]						)

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

				__A    =							False

				__A    =							StableDiffusionImageVariationPipeline.from_pretrained(
				    "fusing/sd-image-variations-diffusers"							, safety_checker=lowercase__							, torch_dtype=torch.floataa							, )
				pipe.to(lowercase__						)
				pipe.set_progress_bar_config(disable=lowercase__						)
				pipe.enable_attention_slicing()

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






		def 					_lowerCamelCase					(      self						)				->				Optional[int]:
				torch.cuda.empty_cache()
				torch.cuda.reset_max_memory_allocated()
				torch.cuda.reset_peak_memory_stats()

				__A    =							"fusing/sd-image-variations-diffusers"
				__A    =							StableDiffusionImageVariationPipeline.from_pretrained(
				    lowercase__							, safety_checker=lowercase__							, torch_dtype=torch.floataa						)
				__A    =							pipe.to(lowercase__						)
				pipe.set_progress_bar_config(disable=lowercase__						)
				pipe.enable_attention_slicing(1						)
				pipe.enable_sequential_cpu_offload()

				__A    =							self.get_inputs(lowercase__							, dtype=torch.floataa						)
				__A    =							pipe(**lowercase__						)

				__A    =							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        a__      (	unittest.TestCase		):






		def 					_lowerCamelCase					(      self						)				->				List[Any]:
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__="cpu"							, lowercase__=torch.floataa							, lowercase__=0						)				->				Dict:
				__A    =							torch.Generator(device=lowercase__						).manual_seed(lowercase__						)
				__A    =							load_image(
				    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
				    "/stable_diffusion_imgvar/input_image_vermeer.png"						)
				__A    =							np.random.RandomState(lowercase__						).standard_normal((1, 4, 64, 64)						)
				__A    =							torch.from_numpy(lowercase__						).to(device=lowercase__							, dtype=lowercase__						)
				__A    =							{
				    "image": init_image,
				    "latents": latents,
				    "generator": generator,
				    "num_inference_steps": 50,
				    "guidance_scale": 7.5,
				    "output_type": "numpy",
				}
				return inputs






		def 					_lowerCamelCase					(      self						)				->				int:
				__A    =							StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"						)
				sd_pipe.to(lowercase__						)
				sd_pipe.set_progress_bar_config(disable=lowercase__						)

				__A    =							self.get_inputs(lowercase__						)
				__A    =							sd_pipe(**lowercase__						).images[0]

				__A    =							load_numpy(
				    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
				    "/stable_diffusion_imgvar/lambdalabs_variations_pndm.npy"						)
				__A    =							np.abs(expected_image - image						).max()
				assert max_diff < 1e-3






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A    =							StableDiffusionImageVariationPipeline.from_pretrained("fusing/sd-image-variations-diffusers"						)
				__A    =							DPMSolverMultistepScheduler.from_config(sd_pipe.scheduler.config						)
				sd_pipe.to(lowercase__						)
				sd_pipe.set_progress_bar_config(disable=lowercase__						)

				__A    =							self.get_inputs(lowercase__						)
				__A    =							25
				__A    =							sd_pipe(**lowercase__						).images[0]

				__A    =							load_numpy(
				    "https://huggingface.co/datasets/diffusers/test-arrays/resolve/main"
				    "/stable_diffusion_imgvar/lambdalabs_variations_dpm_multi.npy"						)
				__A    =							np.abs(expected_image - image						).max()
				assert max_diff < 1e-3









import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import SegformerImageProcessor





class        a__      (	unittest.TestCase		):






		def __init__(      self							, lowercase__							, lowercase__=7							, lowercase__=3							, lowercase__=30							, lowercase__=400							, lowercase__=True							, lowercase__=None							, lowercase__=True							, lowercase__=[0.5, 0.5, 0.5]							, lowercase__=[0.5, 0.5, 0.5]							, lowercase__=False							, )				->				Tuple:
				__A    =							size if size is not None else {"height": 30, "width": 30}
				__A    =							parent
				__A    =							batch_size
				__A    =							num_channels
				__A    =							min_resolution
				__A    =							max_resolution
				__A    =							do_resize
				__A    =							size
				__A    =							do_normalize
				__A    =							image_mean
				__A    =							image_std
				__A    =							do_reduce_labels






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				return {
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_reduce_labels": self.do_reduce_labels,
				}



def      UpperCAmelCase						( ):


		'''simple docstring'''

		__A    =							load_dataset("hf-internal-testing/fixtures_ade20k"					,     split="test"	)

		__A    =							Image.open(dataset[0]["file"]	)
		__A    =							Image.open(dataset[1]["file"]	)

		return image, map



def      UpperCAmelCase						( ):


		'''simple docstring'''

		__A    =							load_dataset("hf-internal-testing/fixtures_ade20k"					,     split="test"	)

		__A    =							Image.open(dataset[0]["file"]	)
		__A    =							Image.open(dataset[1]["file"]	)
		__A    =							Image.open(dataset[2]["file"]	)
		__A    =							Image.open(dataset[3]["file"]	)

		return [imagea, imagea], [mapa, mapa]





@require_torch
@require_vision
class        a__      (	lowerCAmelCase__	,       unittest.TestCase		):
		UpperCAmelCase_							:       Dict 					= SegformerImageProcessor if is_vision_available() else None






		def 					_lowerCamelCase					(      self						)				->				List[str]:
				__A    =							SegformerImageProcessingTester(self						)






		@property
		def 					_lowerCamelCase					(      self						)				->				str:
				return self.image_processor_tester.prepare_image_processor_dict()






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				self.assertTrue(hasattr(lowercase__							, "do_resize"						)						)
				self.assertTrue(hasattr(lowercase__							, "size"						)						)
				self.assertTrue(hasattr(lowercase__							, "do_normalize"						)						)
				self.assertTrue(hasattr(lowercase__							, "image_mean"						)						)
				self.assertTrue(hasattr(lowercase__							, "image_std"						)						)
				self.assertTrue(hasattr(lowercase__							, "do_reduce_labels"						)						)






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				__A    =							self.image_processing_class.from_dict(self.image_processor_dict						)
				self.assertEqual(image_processor.size							, {"height": 30, "width": 30}						)
				self.assertEqual(image_processor.do_reduce_labels							, lowercase__						)

				__A    =							self.image_processing_class.from_dict(self.image_processor_dict							, size=42							, reduce_labels=lowercase__						)
				self.assertEqual(image_processor.size							, {"height": 42, "width": 42}						)
				self.assertEqual(image_processor.do_reduce_labels							, lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Optional[int]:
				pass






		def 					_lowerCamelCase					(      self						)				->				Union[str, Any]:
				# Initialize image_processing
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random PIL images
				__A    =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowercase__						)
				for image in image_inputs:
						self.assertIsInstance(lowercase__							, Image.Image						)

				# Test not batched input
				__A    =							image_processing(image_inputs[0]							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )

				# Test batched
				__A    =							image_processing(lowercase__							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )






		def 					_lowerCamelCase					(      self						)				->				List[str]:
				# Initialize image_processing
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random numpy tensors
				__A    =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowercase__							, numpify=lowercase__						)
				for image in image_inputs:
						self.assertIsInstance(lowercase__							, np.ndarray						)

				# Test not batched input
				__A    =							image_processing(image_inputs[0]							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )

				# Test batched
				__A    =							image_processing(lowercase__							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )






		def 					_lowerCamelCase					(      self						)				->				List[str]:
				# Initialize image_processing
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random PyTorch tensors
				__A    =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowercase__							, torchify=lowercase__						)
				for image in image_inputs:
						self.assertIsInstance(lowercase__							, torch.Tensor						)

				# Test not batched input
				__A    =							image_processing(image_inputs[0]							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )

				# Test batched
				__A    =							image_processing(lowercase__							, return_tensors="pt"						).pixel_values
				self.assertEqual(
				    encoded_images.shape							, (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )






		def 					_lowerCamelCase					(      self						)				->				Dict:
				# Initialize image_processing
				__A    =							self.image_processing_class(**self.image_processor_dict						)
				# create random PyTorch tensors
				__A    =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowercase__							, torchify=lowercase__						)
				__A    =							[]
				for image in image_inputs:
						self.assertIsInstance(lowercase__							, torch.Tensor						)
						maps.append(torch.zeros(image.shape[-2:]						).long()						)

				# Test not batched input
				__A    =							image_processing(image_inputs[0]							, maps[0]							, return_tensors="pt"						)
				self.assertEqual(
				    encoding["pixel_values"].shape							, (
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(
				    encoding["labels"].shape							, (
				        1,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(encoding["labels"].dtype							, torch.long						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 255						)

				# Test batched
				__A    =							image_processing(lowercase__							, lowercase__							, return_tensors="pt"						)
				self.assertEqual(
				    encoding["pixel_values"].shape							, (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(
				    encoding["labels"].shape							, (
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(encoding["labels"].dtype							, torch.long						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 255						)

				# Test not batched input (PIL images)
				__A ,   __A    =							prepare_semantic_single_inputs()

				__A    =							image_processing(lowercase__							, lowercase__							, return_tensors="pt"						)
				self.assertEqual(
				    encoding["pixel_values"].shape							, (
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(
				    encoding["labels"].shape							, (
				        1,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(encoding["labels"].dtype							, torch.long						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 255						)

				# Test batched input (PIL images)
				__A ,   __A    =							prepare_semantic_batch_inputs()

				__A    =							image_processing(lowercase__							, lowercase__							, return_tensors="pt"						)
				self.assertEqual(
				    encoding["pixel_values"].shape							, (
				        2,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(
				    encoding["labels"].shape							, (
				        2,
				        self.image_processor_tester.size["height"],
				        self.image_processor_tester.size["width"],
				    )							, )
				self.assertEqual(encoding["labels"].dtype							, torch.long						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 255						)






		def 					_lowerCamelCase					(      self						)				->				Tuple:
				# Initialize image_processing
				__A    =							self.image_processing_class(**self.image_processor_dict						)

				# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
				__A ,   __A    =							prepare_semantic_single_inputs()
				__A    =							image_processing(lowercase__							, lowercase__							, return_tensors="pt"						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 150						)

				__A    =							True
				__A    =							image_processing(lowercase__							, lowercase__							, return_tensors="pt"						)
				self.assertTrue(encoding["labels"].min().item() >= 0						)
				self.assertTrue(encoding["labels"].max().item() <= 255						)

