


"""simple docstring"""






import gc
import random
import unittest

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

from diffusers import AutoencoderKL, PNDMScheduler, StableDiffusionInpaintPipeline, UNetaDConditionModel
from diffusers.utils import floats_tensor, load_image, load_numpy, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow

from ..pipeline_params import TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS, TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class 	lowerCamelCase__  (	__UpperCAmelCase	,       __UpperCAmelCase	,       __UpperCAmelCase	,       unittest.TestCase							):







					"""simple docstring"""


					__a      			=				StableDiffusionInpaintPipeline
					__a      			=				TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
					__a      			=				TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
					__a      			=				frozenset(
					    []							)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
					__a      			=				frozenset([]							)






					def lowerCamelCase__				(			self       :					List[str]    ):
												'''simple docstring'''






												torch.manual_seed(0    )
												__UpperCAmelCase  :		Tuple 				=   UNetaDConditionModel(
												    block_out_channels=(32, 64)					,			layers_per_block=2					,			sample_size=32					,			in_channels=9					,			out_channels=4					,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")					,			up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")					,			cross_attention_dim=32					,			attention_head_dim=(2, 4)					,			use_linear_projection=lowerCAmelCase_					,			)
												__UpperCAmelCase  :		List[str] 				=   PNDMScheduler(skip_prk_steps=lowerCAmelCase_    )
												torch.manual_seed(0    )
												__UpperCAmelCase  :		Dict 				=   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					,			sample_size=128					,			)
												torch.manual_seed(0    )
												__UpperCAmelCase  :		Optional[Any] 				=   CLIPTextConfig(
												    bos_token_id=0					,			eos_token_id=2					,			hidden_size=32					,			intermediate_size=37					,			layer_norm_eps=1e-0_5					,			num_attention_heads=4					,			num_hidden_layers=5					,			pad_token_id=1					,			vocab_size=1_000					,			hidden_act="""gelu"""					,			projection_dim=512					,			)
												__UpperCAmelCase  :		Dict 				=   CLIPTextModel(lowerCAmelCase_    )
												__UpperCAmelCase  :		Tuple 				=   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

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






					def lowerCamelCase__				(			self       :					Optional[Any]					,			UpperCamelCase       :					List[Any]					,			UpperCamelCase       :					Optional[int]=0    ):
												'''simple docstring'''






												__UpperCAmelCase  :		Union[str, Any] 				=   floats_tensor((1, 3, 32, 32)					,			rng=random.Random(lowerCAmelCase_    )    ).to(lowerCAmelCase_    )
												__UpperCAmelCase  :		Tuple 				=   image.cpu().permute(0					,			2					,			3					,			1    )[0]
												__UpperCAmelCase  :		Any 				=   Image.fromarray(np.uinta(lowerCAmelCase_    )    ).convert("""RGB"""    ).resize((64, 64)    )
												__UpperCAmelCase  :		Optional[Any] 				=   Image.fromarray(np.uinta(image + 4    )    ).convert("""RGB"""    ).resize((64, 64)    )
												if str(lowerCAmelCase_    ).startswith("""mps"""    ):
																			__UpperCAmelCase  :		List[str] 				=   torch.manual_seed(lowerCAmelCase_    )
												else:
																			__UpperCAmelCase  :		Tuple 				=   torch.Generator(device=lowerCAmelCase_    ).manual_seed(lowerCAmelCase_    )
												__UpperCAmelCase  :		Optional[Any] 				=   {
												    'prompt': 'A painting of a squirrel eating a burger',
												    'image': init_image,
												    'mask_image': mask_image,
												    'generator': generator,
												    'num_inference_steps': 2,
												    'guidance_scale': 6.0,
												    'output_type': 'numpy',
												}
												return inputs






					def lowerCamelCase__				(			self       :					Dict    ):
												'''simple docstring'''






												__UpperCAmelCase  :		List[str] 				=   'cpu'  # ensure determinism for the device-dependent torch.Generator
												__UpperCAmelCase  :		str 				=   self.get_dummy_components()
												__UpperCAmelCase  :		Any 				=   StableDiffusionInpaintPipeline(**lowerCAmelCase_    )
												__UpperCAmelCase  :		List[str] 				=   sd_pipe.to(lowerCAmelCase_    )
												sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_    )

												__UpperCAmelCase  :		Optional[int] 				=   self.get_dummy_inputs(lowerCAmelCase_    )
												__UpperCAmelCase  :		Optional[Any] 				=   sd_pipe(**lowerCAmelCase_    ).images
												__UpperCAmelCase  :		Optional[int] 				=   image[0, -3:, -3:, -1]

												assert image.shape == (1, 64, 64, 3)
												__UpperCAmelCase  :		List[str] 				=   np.array([0.4727, 0.5735, 0.3941, 0.5446, 0.5926, 0.4394, 0.5062, 0.4654, 0.4476]    )

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






					def lowerCamelCase__				(			self       :					Optional[Any]    ):
												'''simple docstring'''






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






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







					"""simple docstring"""


					def lowerCamelCase__				(			self       :					int    ):
												'''simple docstring'''






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






					def lowerCamelCase__				(			self       :					str    ):
												'''simple docstring'''






												__UpperCAmelCase  :		List[str] 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
												    """/sd2-inpaint/init_image.png"""    )
												__UpperCAmelCase  :		Any 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""    )
												__UpperCAmelCase  :		Dict 				=   load_numpy(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"""
												    """/yellow_cat_sitting_on_a_park_bench.npy"""    )

												__UpperCAmelCase  :		Optional[Any] 				=   'stabilityai/stable-diffusion-2-inpainting'
												__UpperCAmelCase  :		Optional[int] 				=   StableDiffusionInpaintPipeline.from_pretrained(lowerCAmelCase_					,			safety_checker=lowerCAmelCase_    )
												pipe.to(lowerCAmelCase_    )
												pipe.set_progress_bar_config(disable=lowerCAmelCase_    )
												pipe.enable_attention_slicing()

												__UpperCAmelCase  :		List[str] 				=   'Face of a yellow cat, high resolution, sitting on a park bench'

												__UpperCAmelCase  :		Optional[Any] 				=   torch.manual_seed(0    )
												__UpperCAmelCase  :		List[Any] 				=   pipe(
												    prompt=lowerCAmelCase_					,			image=lowerCAmelCase_					,			mask_image=lowerCAmelCase_					,			generator=lowerCAmelCase_					,			output_type="""np"""					,			)
												__UpperCAmelCase  :		int 				=   output.images[0]

												assert image.shape == (512, 512, 3)
												assert np.abs(expected_image - image    ).max() < 9e-3






					def lowerCamelCase__				(			self       :					int    ):
												'''simple docstring'''






												__UpperCAmelCase  :		Dict 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
												    """/sd2-inpaint/init_image.png"""    )
												__UpperCAmelCase  :		str 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""    )
												__UpperCAmelCase  :		int 				=   load_numpy(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"""
												    """/yellow_cat_sitting_on_a_park_bench_fp16.npy"""    )

												__UpperCAmelCase  :		Optional[Any] 				=   'stabilityai/stable-diffusion-2-inpainting'
												__UpperCAmelCase  :		str 				=   StableDiffusionInpaintPipeline.from_pretrained(
												    lowerCAmelCase_					,			torch_dtype=torch.floataa					,			safety_checker=lowerCAmelCase_					,			)
												pipe.to(lowerCAmelCase_    )
												pipe.set_progress_bar_config(disable=lowerCAmelCase_    )
												pipe.enable_attention_slicing()

												__UpperCAmelCase  :		str 				=   'Face of a yellow cat, high resolution, sitting on a park bench'

												__UpperCAmelCase  :		Tuple 				=   torch.manual_seed(0    )
												__UpperCAmelCase  :		List[Any] 				=   pipe(
												    prompt=lowerCAmelCase_					,			image=lowerCAmelCase_					,			mask_image=lowerCAmelCase_					,			generator=lowerCAmelCase_					,			output_type="""np"""					,			)
												__UpperCAmelCase  :		Optional[int] 				=   output.images[0]

												assert image.shape == (512, 512, 3)
												assert np.abs(expected_image - image    ).max() < 5e-1






					def lowerCamelCase__				(			self       :					str    ):
												'''simple docstring'''






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

												__UpperCAmelCase  :		str 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
												    """/sd2-inpaint/init_image.png"""    )
												__UpperCAmelCase  :		Optional[Any] 				=   load_image(
												    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""    )

												__UpperCAmelCase  :		str 				=   'stabilityai/stable-diffusion-2-inpainting'
												__UpperCAmelCase  :		List[Any] 				=   PNDMScheduler.from_pretrained(lowerCAmelCase_					,			subfolder="""scheduler"""    )
												__UpperCAmelCase  :		Optional[Any] 				=   StableDiffusionInpaintPipeline.from_pretrained(
												    lowerCAmelCase_					,			safety_checker=lowerCAmelCase_					,			scheduler=lowerCAmelCase_					,			torch_dtype=torch.floataa					,			)
												pipe.to(lowerCAmelCase_    )
												pipe.set_progress_bar_config(disable=lowerCAmelCase_    )
												pipe.enable_attention_slicing(1    )
												pipe.enable_sequential_cpu_offload()

												__UpperCAmelCase  :		str 				=   'Face of a yellow cat, high resolution, sitting on a park bench'

												__UpperCAmelCase  :		Union[str, Any] 				=   torch.manual_seed(0    )
												__UpperCAmelCase  :		Dict 				=   pipe(
												    prompt=lowerCAmelCase_					,			image=lowerCAmelCase_					,			mask_image=lowerCAmelCase_					,			generator=lowerCAmelCase_					,			num_inference_steps=2					,			output_type="""np"""					,			)

												__UpperCAmelCase  :		Union[str, Any] 				=   torch.cuda.max_memory_allocated()
												# make sure that less than 2.65 GB is allocated
												assert mem_bytes < 2.65 * 10**9








"""simple docstring"""
import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
			import jax
			import jax.numpy as jnp
			from flax.jax_utils import replicate
			from flax.training.common_utils import shard


@slow
@require_flax
class _lowerCAmelCase						(    unittest.TestCase       ):







						def 			A      (    self							)		->							Any:
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()







						def 			A      (    self							)		->							int:
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    FlaxStableDiffusionPipeline.from_pretrained(
										    'stabilityai/stable-diffusion-2'   ,				revision='bf16'   ,				dtype=jnp.bfloataa   ,				)

										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    'A painting of a squirrel eating a burger'
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    jax.device_count()
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_samples * [prompt]
										_SCREAMING_SNAKE_CASE   :		Any       		=    sd_pipe.prepare_inputs(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    replicate(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    shard(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    jax.random.PRNGKey(0							)
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    jax.random.split(lowerCAmelCase_   ,				jax.device_count()							)

										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    sd_pipe(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				num_inference_steps=2_5   ,				jit=lowerCAmelCase_							)[0]
										assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

										_SCREAMING_SNAKE_CASE   :		int       		=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    jnp.asarray(jax.device_get(image_slice.flatten()							)							)
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    jnp.array([0.4_238, 0.4_414, 0.4_395, 0.4_453, 0.4_629, 0.4_590, 0.4_531, 0.45_508, 0.4_512]							)
										print(F"""output_slice: {output_slice}"""							)
										assert jnp.abs(output_slice - expected_slice							).max() < 1e-2







						def 			A      (    self							)		->							Optional[int]:
										_SCREAMING_SNAKE_CASE   :		str       		=    'stabilityai/stable-diffusion-2'
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		int       		=    FlaxDPMSolverMultistepScheduler.from_pretrained(lowerCAmelCase_   ,				subfolder='scheduler'							)
										_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE   :		List[str]       		=    FlaxStableDiffusionPipeline.from_pretrained(
										    lowerCAmelCase_   ,				scheduler=lowerCAmelCase_   ,				revision='bf16'   ,				dtype=jnp.bfloataa   ,				)
										_SCREAMING_SNAKE_CASE   :		Any       		=    scheduler_params

										_SCREAMING_SNAKE_CASE   :		int       		=    'A painting of a squirrel eating a burger'
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    jax.device_count()
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    num_samples * [prompt]
										_SCREAMING_SNAKE_CASE   :		Any       		=    sd_pipe.prepare_inputs(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Dict       		=    replicate(lowerCAmelCase_							)
										_SCREAMING_SNAKE_CASE   :		Any       		=    shard(lowerCAmelCase_							)

										_SCREAMING_SNAKE_CASE   :		Any       		=    jax.random.PRNGKey(0							)
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    jax.random.split(lowerCAmelCase_   ,				jax.device_count()							)

										_SCREAMING_SNAKE_CASE   :		int       		=    sd_pipe(lowerCAmelCase_   ,				lowerCAmelCase_   ,				lowerCAmelCase_   ,				num_inference_steps=2_5   ,				jit=lowerCAmelCase_							)[0]
										assert images.shape == (jax.device_count(), 1, 7_6_8, 7_6_8, 3)

										_SCREAMING_SNAKE_CASE   :		str       		=    images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]							)
										_SCREAMING_SNAKE_CASE   :		str       		=    images[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

										_SCREAMING_SNAKE_CASE   :		int       		=    jnp.asarray(jax.device_get(image_slice.flatten()							)							)
										_SCREAMING_SNAKE_CASE   :		Optional[int]       		=    jnp.array([0.4_336, 0.42_969, 0.4_453, 0.4_199, 0.4_297, 0.4_531, 0.4_434, 0.4_434, 0.4_297]							)
										print(F"""output_slice: {output_slice}"""							)
										assert jnp.abs(output_slice - expected_slice							).max() < 1e-2

