


import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()



class 							A   (	lowerCamelCase__			,  lowerCamelCase__			,  unittest.TestCase							):
					'''simple docstring'''






					A__      =			AutoencoderKL
					A__      =			'''sample'''
					A__      =			1e-2

					@property
					def   lowerCamelCase__					(self   :						Dict		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		4
							lowercase__					      =		3
							lowercase__					      =		(32, 32)

							lowercase__					      =		floats_tensor((batch_size, num_channels) + sizes		).to(_UpperCAmelCase		)

							return {"sample": image}

					@property
					def   lowerCamelCase__					(self   :						int		)						->       Dict:




							"""simple docstring"""


							return (3, 32, 32)

					@property
					def   lowerCamelCase__					(self   :						Dict		)						->       List[Any]:




							"""simple docstring"""


							return (3, 32, 32)

					def   lowerCamelCase__					(self   :						str		)						->       Union[str, Any]:




							"""simple docstring"""


							lowercase__					      =		{
							    "block_out_channels": [32, 64],
							    "in_channels": 3,
							    "out_channels": 3,
							    "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],
							    "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],
							    "latent_channels": 4,
							}
							lowercase__					      =		self.dummy_input
							return init_dict, inputs_dict

					def   lowerCamelCase__					(self   :						Optional[int]		)						->       Optional[Any]:




							"""simple docstring"""


							pass

					def   lowerCamelCase__					(self   :						Optional[Any]		)						->       Any:




							"""simple docstring"""


							pass

					@unittest.skipIf(torch_device == """mps"""				, """Gradient checkpointing skipped on MPS"""		)
					def   lowerCamelCase__					(self   :						Optional[int]		)						->       str:




							"""simple docstring"""


							lowercase__					      =		self.prepare_init_args_and_inputs_for_common()
							lowercase__					      =		self.model_class(**_UpperCAmelCase		)
							model.to(_UpperCAmelCase		)

							assert not model.is_gradient_checkpointing and model.training

							lowercase__					      =		model(**_UpperCAmelCase		).sample
							# run the backwards pass on the model. For backwards pass, for simplicity purpose,
							# we won't calculate the loss and rather backprop on out.sum()
							model.zero_grad()

							lowercase__					      =		torch.randn_like(_UpperCAmelCase		)
							lowercase__					      =		(out - labels).mean()
							loss.backward()

							# re-instantiate the model now enabling gradient checkpointing
							lowercase__					      =		self.model_class(**_UpperCAmelCase		)
							# clone model
							model_a.load_state_dict(model.state_dict()		)
							model_a.to(_UpperCAmelCase		)
							model_a.enable_gradient_checkpointing()

							assert model_a.is_gradient_checkpointing and model_a.training

							lowercase__					      =		model_a(**_UpperCAmelCase		).sample
							# run the backwards pass on the model. For backwards pass, for simplicity purpose,
							# we won't calculate the loss and rather backprop on out.sum()
							model_a.zero_grad()
							lowercase__					      =		(out_a - labels).mean()
							loss_a.backward()

							# compare the output and parameters gradients
							self.assertTrue((loss - loss_a).abs() < 1E-5		)
							lowercase__					      =		dict(model.named_parameters()		)
							lowercase__					      =		dict(model_a.named_parameters()		)
							for name, param in named_params.items():
									self.assertTrue(torch_all_close(param.grad.data				, named_params_a[name].grad.data				, atol=5E-5		)		)

					def   lowerCamelCase__					(self   :						Dict		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy"""				, output_loading_info=_UpperCAmelCase		)
							self.assertIsNotNone(_UpperCAmelCase		)
							self.assertEqual(len(loading_info["""missing_keys"""]		)				, 0		)

							model.to(_UpperCAmelCase		)
							lowercase__					      =		model(**self.dummy_input		)

							assert image is not None, "Make sure output is not None"


					def   lowerCamelCase__					(self   :						Any		)						->       Optional[int]:




							"""simple docstring"""


							lowercase__					      =		AutoencoderKL.from_pretrained("""fusing/autoencoder-kl-dummy"""		)
							lowercase__					      =		model.to(_UpperCAmelCase		)
							model.eval()

							if torch_device == "mps":
									lowercase__					      =		torch.manual_seed(0		)
							else:
									lowercase__					      =		torch.Generator(device=_UpperCAmelCase		).manual_seed(0		)

							lowercase__					      =		torch.randn(
							    1				, model.config.in_channels				, model.config.sample_size				, model.config.sample_size				, generator=torch.manual_seed(0		)				, )
							lowercase__					      =		image.to(_UpperCAmelCase		)
							with torch.no_grad():
									lowercase__					      =		model(_UpperCAmelCase				, sample_posterior=_UpperCAmelCase				, generator=_UpperCAmelCase		).sample

							lowercase__					      =		output[0, -1, -3:, -3:].flatten().cpu()

							# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
							# the expected output slices are not the same for CPU and GPU.
							if torch_device == "mps":
									lowercase__					      =		torch.tensor(
									    [
									        -4.0_0_7_8E-0_1,
									        -3.8_3_2_3E-0_4,
									        -1.2_6_8_1E-0_1,
									        -1.1_4_6_2E-0_1,
									        2.0_0_9_5E-0_1,
									        1.0_8_9_3E-0_1,
									        -8.8_2_4_7E-0_2,
									        -3.0_3_6_1E-0_1,
									        -9.8_6_4_4E-0_3,
									    ]		)
							elif torch_device == "cpu":
									lowercase__					      =		torch.tensor(
									    [-0.1_352, 0.0_878, 0.0_419, -0.0_818, -0.1_069, 0.0_688, -0.1_458, -0.4_446, -0.0_026]		)
							else:
									lowercase__					      =		torch.tensor(
									    [-0.2_421, 0.4_642, 0.2_507, -0.0_438, 0.0_682, 0.3_160, -0.2_018, -0.0_727, 0.2_485]		)

							self.assertTrue(torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, rtol=1E-2		)		)






@slow
class 							A   (	unittest.TestCase							):
					'''simple docstring'''






					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int		)						->       List[Any]:




							"""simple docstring"""


							return f'''gaussian_noise_s={seed}_shape={"_".join([str(_UpperCAmelCase		) for s in shape]		)}.npy'''

					def   lowerCamelCase__					(self   :						Optional[int]		)						->       Dict:




							"""simple docstring"""


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

					def   lowerCamelCase__					(self   :						Optional[Any]				, _UpperCAmelCase   :						Any=0				, _UpperCAmelCase   :						str=(4, 3, 512, 512)				, _UpperCAmelCase   :						Optional[int]=False		)						->       Union[str, Any]:




							"""simple docstring"""


							lowercase__					      =		torch.floataa if fpaa else torch.floataa
							lowercase__					      =		torch.from_numpy(load_hf_numpy(self.get_file_format(_UpperCAmelCase				, _UpperCAmelCase		)		)		).to(_UpperCAmelCase		).to(_UpperCAmelCase		)
							return image

					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						List[str]="CompVis/stable-diffusion-v1-4"				, _UpperCAmelCase   :						str=False		)						->       int:




							"""simple docstring"""


							lowercase__					      =		"fp16" if fpaa else None
							lowercase__					      =		torch.floataa if fpaa else torch.floataa

							lowercase__					      =		AutoencoderKL.from_pretrained(
							    _UpperCAmelCase				, subfolder="""vae"""				, torch_dtype=_UpperCAmelCase				, revision=_UpperCAmelCase				, )
							model.to(_UpperCAmelCase		).eval()

							return model

					def   lowerCamelCase__					(self   :						Dict				, _UpperCAmelCase   :						Any=0		)						->       Tuple:




							"""simple docstring"""


							if torch_device == "mps":
									return torch.manual_seed(_UpperCAmelCase		)
							return torch.Generator(device=_UpperCAmelCase		).manual_seed(_UpperCAmelCase		)

					@parameterized.expand(
					    [
					        # fmt: off
					        [33, [-0.1_603, 0.9_878, -0.0_495, -0.0_790, -0.2_709, 0.8_375, -0.2_060, -0.0_824], [-0.2_395, 0.0_098, 0.0_102, -0.0_709, -0.2_840, -0.0_274, -0.0_718, -0.1_824]],
					        [47, [-0.2_376, 0.1_168, 0.1_332, -0.4_840, -0.2_508, -0.0_791, -0.0_493, -0.4_089], [0.0_350, 0.0_847, 0.0_467, 0.0_344, -0.0_842, -0.0_547, -0.0_633, -0.1_131]],
					        # fmt: on
					    ]		)
					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						Union[str, Any]		)						->       int:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model()
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase		)
							lowercase__					      =		self.get_generator(_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model(_UpperCAmelCase				, generator=_UpperCAmelCase				, sample_posterior=_UpperCAmelCase		).sample

							assert sample.shape == image.shape

							lowercase__					      =		sample[-1, -2:, -2:, :2].flatten().float().cpu()
							lowercase__					      =		torch.tensor(expected_slice_mps if torch_device == """mps""" else expected_slice		)

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=3E-3		)

					@parameterized.expand(
					    [
					        # fmt: off
					        [33, [-0.0_513, 0.0_289, 1.3_799, 0.2_166, -0.2_573, -0.0_871, 0.5_103, -0.0_999]],
					        [47, [-0.4_128, -0.1_320, -0.3_704, 0.1_965, -0.4_116, -0.2_332, -0.3_340, 0.2_247]],
					        # fmt: on
					    ]		)
					@require_torch_gpu
					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						List[Any]		)						->       str:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model(fpaa=_UpperCAmelCase		)
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase				, fpaa=_UpperCAmelCase		)
							lowercase__					      =		self.get_generator(_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model(_UpperCAmelCase				, generator=_UpperCAmelCase				, sample_posterior=_UpperCAmelCase		).sample

							assert sample.shape == image.shape

							lowercase__					      =		sample[-1, -2:, :2, -2:].flatten().float().cpu()
							lowercase__					      =		torch.tensor(_UpperCAmelCase		)

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=1E-2		)

					@parameterized.expand(
					    [
					        # fmt: off
					        [33, [-0.1_609, 0.9_866, -0.0_487, -0.0_777, -0.2_716, 0.8_368, -0.2_055, -0.0_814], [-0.2_395, 0.0_098, 0.0_102, -0.0_709, -0.2_840, -0.0_274, -0.0_718, -0.1_824]],
					        [47, [-0.2_377, 0.1_147, 0.1_333, -0.4_841, -0.2_506, -0.0_805, -0.0_491, -0.4_085], [0.0_350, 0.0_847, 0.0_467, 0.0_344, -0.0_842, -0.0_547, -0.0_633, -0.1_131]],
					        # fmt: on
					    ]		)
					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						Any				, _UpperCAmelCase   :						Tuple				, _UpperCAmelCase   :						int		)						->       int:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model()
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model(_UpperCAmelCase		).sample

							assert sample.shape == image.shape

							lowercase__					      =		sample[-1, -2:, -2:, :2].flatten().float().cpu()
							lowercase__					      =		torch.tensor(expected_slice_mps if torch_device == """mps""" else expected_slice		)

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=3E-3		)

					@parameterized.expand(
					    [
					        # fmt: off
					        [13, [-0.2_051, -0.1_803, -0.2_311, -0.2_114, -0.3_292, -0.3_574, -0.2_953, -0.3_323]],
					        [37, [-0.2_632, -0.2_625, -0.2_199, -0.2_741, -0.4_539, -0.4_990, -0.3_720, -0.4_925]],
					        # fmt: on
					    ]		)
					@require_torch_gpu
					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						Optional[Any]				, _UpperCAmelCase   :						List[Any]		)						->       Optional[int]:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model()
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase				, shape=(3, 4, 64, 64)		)

							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							assert list(sample.shape		) == [3, 3, 512, 512]

							lowercase__					      =		sample[-1, -2:, :2, -2:].flatten().cpu()
							lowercase__					      =		torch.tensor(_UpperCAmelCase		)

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=1E-3		)

					@parameterized.expand(
					    [
					        # fmt: off
					        [27, [-0.0_369, 0.0_207, -0.0_776, -0.0_682, -0.1_747, -0.1_930, -0.1_465, -0.2_039]],
					        [16, [-0.1_628, -0.2_134, -0.2_747, -0.2_642, -0.3_774, -0.4_404, -0.3_687, -0.4_277]],
					        # fmt: on
					    ]		)
					@require_torch_gpu
					def   lowerCamelCase__					(self   :						Tuple				, _UpperCAmelCase   :						Dict				, _UpperCAmelCase   :						Optional[int]		)						->       List[str]:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model(fpaa=_UpperCAmelCase		)
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase				, shape=(3, 4, 64, 64)				, fpaa=_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							assert list(sample.shape		) == [3, 3, 512, 512]

							lowercase__					      =		sample[-1, -2:, :2, -2:].flatten().float().cpu()
							lowercase__					      =		torch.tensor(_UpperCAmelCase		)

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=5E-3		)

					@parameterized.expand([(13,), (16,), (27,)]		)
					@require_torch_gpu
					@unittest.skipIf(not is_xformers_available()				, reason="""xformers is not required when using PyTorch 2.0."""		)
					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						Optional[Any]		)						->       Optional[Any]:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model(fpaa=_UpperCAmelCase		)
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase				, shape=(3, 4, 64, 64)				, fpaa=_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							model.enable_xformers_memory_efficient_attention()
							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							assert list(sample.shape		) == [3, 3, 512, 512]

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=1E-1		)

					@parameterized.expand([(13,), (16,), (37,)]		)
					@require_torch_gpu
					@unittest.skipIf(not is_xformers_available()				, reason="""xformers is not required when using PyTorch 2.0."""		)
					def   lowerCamelCase__					(self   :						Union[str, Any]				, _UpperCAmelCase   :						Optional[int]		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model()
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase				, shape=(3, 4, 64, 64)		)

							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							model.enable_xformers_memory_efficient_attention()
							with torch.no_grad():
									lowercase__					      =		model.decode(_UpperCAmelCase		).sample

							assert list(sample.shape		) == [3, 3, 512, 512]

							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=1E-2		)


					@parameterized.expand(
					    [
					        # fmt: off
					        [33, [-0.3_001, 0.0_918, -2.6_984, -3.9_720, -3.2_099, -5.0_353, 1.7_338, -0.2_065, 3.4_267]],
					        [47, [-1.5_030, -4.3_871, -6.0_355, -9.1_157, -1.6_661, -2.7_853, 2.1_607, -5.0_823, 2.5_633]],
					        # fmt: on
					    ]		)
					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						List[Any]				, _UpperCAmelCase   :						List[str]		)						->       List[Any]:




							"""simple docstring"""


							lowercase__					      =		self.get_sd_vae_model()
							lowercase__					      =		self.get_sd_image(_UpperCAmelCase		)
							lowercase__					      =		self.get_generator(_UpperCAmelCase		)

							with torch.no_grad():
									lowercase__					      =		model.encode(_UpperCAmelCase		).latent_dist
									lowercase__					      =		dist.sample(generator=_UpperCAmelCase		)

							assert list(sample.shape		) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

							lowercase__					      =		sample[0, -1, -3:, -3:].flatten().cpu()
							lowercase__					      =		torch.tensor(_UpperCAmelCase		)

							lowercase__					      =		3E-3 if torch_device != "mps" else 1E-2
							assert torch_all_close(_UpperCAmelCase				, _UpperCAmelCase				, atol=_UpperCAmelCase		)










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 							A   (	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   :						Tuple		)						->       List[Any]:




							"""simple docstring"""


							torch.manual_seed(0		)
							lowercase__					      =		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=_UpperCAmelCase				, )
							lowercase__					      =		PNDMScheduler(skip_prk_steps=_UpperCAmelCase		)
							torch.manual_seed(0		)
							lowercase__					      =		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		)
							lowercase__					      =		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=1000				, hidden_act="""gelu"""				, projection_dim=512				, )
							lowercase__					      =		CLIPTextModel(_UpperCAmelCase		)
							lowercase__					      =		CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""		)

							lowercase__					      =		{
							    """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[str]				, _UpperCAmelCase   :						Optional[Any]=0		)						->       List[str]:




							"""simple docstring"""


							lowercase__					      =		floats_tensor((1, 3, 32, 32)				, rng=random.Random(_UpperCAmelCase		)		).to(_UpperCAmelCase		)
							lowercase__					      =		image.cpu().permute(0				, 2				, 3				, 1		)[0]
							lowercase__					      =		Image.fromarray(np.uinta(_UpperCAmelCase		)		).convert("""RGB"""		).resize((64, 64)		)
							lowercase__					      =		Image.fromarray(np.uinta(image + 4		)		).convert("""RGB"""		).resize((64, 64)		)
							if str(_UpperCAmelCase		).startswith("""mps"""		):
									lowercase__					      =		torch.manual_seed(_UpperCAmelCase		)
							else:
									lowercase__					      =		torch.Generator(device=_UpperCAmelCase		).manual_seed(_UpperCAmelCase		)
							lowercase__					      =		{
							    """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   :						Optional[int]		)						->       Any:




							"""simple docstring"""


							lowercase__					      =		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
							lowercase__					      =		self.get_dummy_components()
							lowercase__					      =		StableDiffusionInpaintPipeline(**_UpperCAmelCase		)
							lowercase__					      =		sd_pipe.to(_UpperCAmelCase		)
							sd_pipe.set_progress_bar_config(disable=_UpperCAmelCase		)

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

							assert image.shape == (1, 64, 64, 3)
							lowercase__					      =		np.array([0.4_727, 0.5_735, 0.3_941, 0.5_446, 0.5_926, 0.4_394, 0.5_062, 0.4_654, 0.4_476]		)

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


					def   lowerCamelCase__					(self   :						str		)						->       int:




							"""simple docstring"""


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






@slow
@require_torch_gpu
class 							A   (	unittest.TestCase							):
					'''simple docstring'''






					def   lowerCamelCase__					(self   :						str		)						->       Optional[Any]:




							"""simple docstring"""


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

					def   lowerCamelCase__					(self   :						Union[str, Any]		)						->       Union[str, Any]:




							"""simple docstring"""


							lowercase__					      =		load_image(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
							    """/sd2-inpaint/init_image.png"""		)
							lowercase__					      =		load_image(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""		)
							lowercase__					      =		load_numpy(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint"""
							    """/yellow_cat_sitting_on_a_park_bench.npy"""		)

							lowercase__					      =		"""stabilityai/stable-diffusion-2-inpainting"""
							lowercase__					      =		StableDiffusionInpaintPipeline.from_pretrained(_UpperCAmelCase				, safety_checker=_UpperCAmelCase		)
							pipe.to(_UpperCAmelCase		)
							pipe.set_progress_bar_config(disable=_UpperCAmelCase		)
							pipe.enable_attention_slicing()

							lowercase__					      =		"""Face of a yellow cat, high resolution, sitting on a park bench"""

							lowercase__					      =		torch.manual_seed(0		)
							lowercase__					      =		pipe(
							    prompt=_UpperCAmelCase				, image=_UpperCAmelCase				, mask_image=_UpperCAmelCase				, generator=_UpperCAmelCase				, output_type="""np"""				, )
							lowercase__					      =		output.images[0]

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

					def   lowerCamelCase__					(self   :						Optional[int]		)						->       List[str]:




							"""simple docstring"""


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

							lowercase__					      =		"""stabilityai/stable-diffusion-2-inpainting"""
							lowercase__					      =		StableDiffusionInpaintPipeline.from_pretrained(
							    _UpperCAmelCase				, torch_dtype=torch.floataa				, safety_checker=_UpperCAmelCase				, )
							pipe.to(_UpperCAmelCase		)
							pipe.set_progress_bar_config(disable=_UpperCAmelCase		)
							pipe.enable_attention_slicing()

							lowercase__					      =		"""Face of a yellow cat, high resolution, sitting on a park bench"""

							lowercase__					      =		torch.manual_seed(0		)
							lowercase__					      =		pipe(
							    prompt=_UpperCAmelCase				, image=_UpperCAmelCase				, mask_image=_UpperCAmelCase				, generator=_UpperCAmelCase				, output_type="""np"""				, )
							lowercase__					      =		output.images[0]

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


					def   lowerCamelCase__					(self   :						List[str]		)						->       Optional[Any]:




							"""simple docstring"""


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

							lowercase__					      =		load_image(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
							    """/sd2-inpaint/init_image.png"""		)
							lowercase__					      =		load_image(
							    """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-inpaint/mask.png"""		)

							lowercase__					      =		"""stabilityai/stable-diffusion-2-inpainting"""
							lowercase__					      =		PNDMScheduler.from_pretrained(_UpperCAmelCase				, subfolder="""scheduler"""		)
							lowercase__					      =		StableDiffusionInpaintPipeline.from_pretrained(
							    _UpperCAmelCase				, safety_checker=_UpperCAmelCase				, scheduler=_UpperCAmelCase				, torch_dtype=torch.floataa				, )
							pipe.to(_UpperCAmelCase		)
							pipe.set_progress_bar_config(disable=_UpperCAmelCase		)
							pipe.enable_attention_slicing(1		)
							pipe.enable_sequential_cpu_offload()

							lowercase__					      =		"""Face of a yellow cat, high resolution, sitting on a park bench"""

							lowercase__					      =		torch.manual_seed(0		)
							lowercase__					      =		pipe(
							    prompt=_UpperCAmelCase				, image=_UpperCAmelCase				, mask_image=_UpperCAmelCase				, generator=_UpperCAmelCase				, num_inference_steps=2				, output_type="""np"""				, )

							lowercase__					      =		torch.cuda.max_memory_allocated()
							# make sure that less than 2.65 GB is allocated
							assert mem_bytes < 2.65 * 10**9

