

"""simple docstring"""




import gc
import random
import unittest

import torch

from diffusers import (
    IFImgaImgPipeline,
    IFImgaImgSuperResolutionPipeline,
    IFInpaintingPipeline,
    IFInpaintingSuperResolutionPipeline,
    IFPipeline,
    IFSuperResolutionPipeline,
)
from diffusers.models.attention_processor import AttnAddedKVProcessor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import floats_tensor, load_numpy, require_torch_gpu, skip_mps, slow, torch_device

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference
from . import IFPipelineTesterMixin





@skip_mps
class    lowerCamelCase						(A__			,A__			,unittest.TestCase			):
   lowerCamelCase__   :		Optional[Any]							=							IFPipeline
   lowerCamelCase__   :		Dict							=							TEXT_TO_IMAGE_PARAMS - {'width', 'height', 'latents'}
   lowerCamelCase__   :		List[str]							=							TEXT_TO_IMAGE_BATCH_PARAMS
   lowerCamelCase__   :		int							=							PipelineTesterMixin.required_optional_params - {'latents'}

   def 					SCREAMING_SNAKE_CASE				(					self				:						Dict       )   ->    Dict:
      return self._get_dummy_components()

   def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]    ,				__UpperCAmelCase				:						Any    ,				__UpperCAmelCase				:						Optional[int]=0       )   ->    Optional[int]:
      if str(__UpperCAmelCase       ).startswith("""mps"""       ):
         SCREAMING_SNAKE_CASE__								=      torch.manual_seed(__UpperCAmelCase       )
      else:
         SCREAMING_SNAKE_CASE__								=      torch.Generator(device=__UpperCAmelCase       ).manual_seed(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      {
          """prompt""": """A painting of a squirrel eating a burger""",
          """generator""": generator,
          """num_inference_steps""": 2,
          """output_type""": """numpy""",
      }

      return inputs

   def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple       )   ->    Any:
      self._test_save_load_optional_components()

   @unittest.skipIf(torch_device != """cuda"""    ,				reason="""float16 requires CUDA"""       )
   def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[int]       )   ->    Union[str, Any]:
      # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
      super().test_save_load_floataa(expected_max_diff=1e-1       )

   def 					SCREAMING_SNAKE_CASE				(					self				:						str       )   ->    Union[str, Any]:
      self._test_attention_slicing_forward_pass(expected_max_diff=1e-2       )

   def 					SCREAMING_SNAKE_CASE				(					self				:						Any       )   ->    str:
      self._test_save_load_local()

   def 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    Union[str, Any]:
      self._test_inference_batch_single_identical(
          expected_max_diff=1e-2    ,				)





   @unittest.skipIf(
       torch_device != """cuda""" or not is_xformers_available()    ,				reason="""XFormers attention is only available with CUDA and `xformers` installed"""    ,				)
   def 					SCREAMING_SNAKE_CASE				(					self				:						Any       )   ->    Tuple:
      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1e-3       )





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

   def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple       )   ->    Dict:
      # clean up the VRAM after each test
      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()

   def 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]       )   ->    Optional[Any]:
      # if

      SCREAMING_SNAKE_CASE__								=      IFPipeline.from_pretrained("""DeepFloyd/IF-I-XL-v1.0"""    ,				variant="""fp16"""    ,				torch_dtype=torch.floataa       )

      SCREAMING_SNAKE_CASE__								=      IFSuperResolutionPipeline.from_pretrained(
          """DeepFloyd/IF-II-L-v1.0"""    ,				variant="""fp16"""    ,				torch_dtype=torch.floataa    ,				text_encoder=__UpperCAmelCase    ,				tokenizer=__UpperCAmelCase       )

      # pre compute text embeddings and remove T5 to save memory

      pipe_a.text_encoder.to("""cuda"""       )

      SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      pipe_a.encode_prompt("""anime turtle"""    ,				device="""cuda"""       )

      del pipe_a.tokenizer
      del pipe_a.text_encoder
      gc.collect()

      SCREAMING_SNAKE_CASE__								=      None
      SCREAMING_SNAKE_CASE__								=      None

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )

      self._test_if(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase       )

      pipe_a.remove_all_hooks()
      pipe_a.remove_all_hooks()

      # img2img

      SCREAMING_SNAKE_CASE__								=      IFImgaImgPipeline(**pipe_a.components       )
      SCREAMING_SNAKE_CASE__								=      IFImgaImgSuperResolutionPipeline(**pipe_a.components       )

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )

      self._test_if_imgaimg(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase       )

      pipe_a.remove_all_hooks()
      pipe_a.remove_all_hooks()

      # inpainting

      SCREAMING_SNAKE_CASE__								=      IFInpaintingPipeline(**pipe_a.components       )
      SCREAMING_SNAKE_CASE__								=      IFInpaintingSuperResolutionPipeline(**pipe_a.components       )

      pipe_a.enable_model_cpu_offload()
      pipe_a.enable_model_cpu_offload()

      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )
      pipe_a.unet.set_attn_processor(AttnAddedKVProcessor()       )

      self._test_if_inpainting(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase       )

   def 					SCREAMING_SNAKE_CASE				(					self				:						Any    ,				__UpperCAmelCase				:						str    ,				__UpperCAmelCase				:						List[Any]    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						int       )   ->    Union[str, Any]:
      # pipeline 1

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )
      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				num_inference_steps=2    ,				generator=__UpperCAmelCase    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_3 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )

      # pipeline 2

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )

      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				generator=__UpperCAmelCase    ,				num_inference_steps=2    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_superresolution_stage_II.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )

   def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple    ,				__UpperCAmelCase				:						Tuple    ,				__UpperCAmelCase				:						Any    ,				__UpperCAmelCase				:						List[Any]    ,				__UpperCAmelCase				:						List[Any]       )   ->    List[Any]:
      # pipeline 1

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )

      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				num_inference_steps=2    ,				generator=__UpperCAmelCase    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_0 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )

      # pipeline 2

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )

      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 2_5_6, 2_5_6)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				original_image=__UpperCAmelCase    ,				generator=__UpperCAmelCase    ,				num_inference_steps=2    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_img2img_superresolution_stage_II.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )





   def 					SCREAMING_SNAKE_CASE				(					self				:						Tuple    ,				__UpperCAmelCase				:						List[Any]    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						List[Any]       )   ->    int:
      # pipeline 1

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(1       )       ).to(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )
      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				mask_image=__UpperCAmelCase    ,				num_inference_steps=2    ,				generator=__UpperCAmelCase    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (6_4, 6_4, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 1_0 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )

      # pipeline 2

      _start_torch_memory_measurement()

      SCREAMING_SNAKE_CASE__								=      torch.Generator(device="""cpu"""       ).manual_seed(0       )

      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 6_4, 6_4)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 2_5_6, 2_5_6)    ,				rng=random.Random(0       )       ).to(__UpperCAmelCase       )
      SCREAMING_SNAKE_CASE__								=      floats_tensor((1, 3, 2_5_6, 2_5_6)    ,				rng=random.Random(1       )       ).to(__UpperCAmelCase       )

      SCREAMING_SNAKE_CASE__								=      pipe_a(
          prompt_embeds=__UpperCAmelCase    ,				negative_prompt_embeds=__UpperCAmelCase    ,				image=__UpperCAmelCase    ,				mask_image=__UpperCAmelCase    ,				original_image=__UpperCAmelCase    ,				generator=__UpperCAmelCase    ,				num_inference_steps=2    ,				output_type="""np"""    ,				)

      SCREAMING_SNAKE_CASE__								=      output.images[0]

      assert image.shape == (2_5_6, 2_5_6, 3)

      SCREAMING_SNAKE_CASE__								=      torch.cuda.max_memory_allocated()
      assert mem_bytes < 4 * 1_0**9

      SCREAMING_SNAKE_CASE__								=      load_numpy(
          """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/if/test_if_inpainting_superresolution_stage_II.npy"""       )
      assert_mean_pixel_difference(__UpperCAmelCase    ,				__UpperCAmelCase       )

def 	A       (     ):




   '''simple docstring'''


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





"""simple docstring"""




import warnings
from typing import List, Optional, Tuple, Union

import numpy as np
import PIL
import torch

from ...models import UNetaDModel
from ...schedulers import RePaintScheduler
from ...utils import PIL_INTERPOLATION, logging, randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


A_							:			Tuple											= logging.get_logger(__name__)  # pylint: disable=invalid-name

def 	A       (     snake_case__    ):




			'''simple docstring'''


			warnings.warn(
			    """The preprocess method is deprecated and will be removed in a future version. Please"""
			    """ use VaeImageProcessor.preprocess instead"""		,				snake_case__		,				)
			if isinstance(snake_case__		,				torch.Tensor    ):
						return image
			elif isinstance(snake_case__		,				PIL.Image.Image    ):
						SCREAMING_SNAKE_CASE__								=      [image]

			if isinstance(image[0]		,				PIL.Image.Image    ):
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      image[0].size
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      (x - x % 8 for x in (w, h))  # resize to integer multiple of 8

						SCREAMING_SNAKE_CASE__								=      [np.array(i.resize((w, h)		,				resample=PIL_INTERPOLATION["""lanczos"""]    )    )[None, :] for i in image]
						SCREAMING_SNAKE_CASE__								=      np.concatenate(snake_case__		,				axis=0    )
						SCREAMING_SNAKE_CASE__								=      np.array(snake_case__    ).astype(np.floataa    ) / 2_55.0
						SCREAMING_SNAKE_CASE__								=      image.transpose(0		,				3		,				1		,				2    )
						SCREAMING_SNAKE_CASE__								=      2.0 * image - 1.0
						SCREAMING_SNAKE_CASE__								=      torch.from_numpy(snake_case__    )
			elif isinstance(image[0]		,				torch.Tensor    ):
						SCREAMING_SNAKE_CASE__								=      torch.cat(snake_case__		,				dim=0    )
			return image

def 	A       (     snake_case__    ):




			'''simple docstring'''


			if isinstance(snake_case__		,				torch.Tensor    ):
						return mask
			elif isinstance(snake_case__		,				PIL.Image.Image    ):
						SCREAMING_SNAKE_CASE__								=      [mask]

			if isinstance(mask[0]		,				PIL.Image.Image    ):
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      mask[0].size
						SCREAMING_SNAKE_CASE__							,		SCREAMING_SNAKE_CASE__								=      (x - x % 32 for x in (w, h))  # resize to integer multiple of 32
						SCREAMING_SNAKE_CASE__								=      [np.array(m.convert("""L"""    ).resize((w, h)		,				resample=PIL_INTERPOLATION["""nearest"""]    )    )[None, :] for m in mask]
						SCREAMING_SNAKE_CASE__								=      np.concatenate(snake_case__		,				axis=0    )
						SCREAMING_SNAKE_CASE__								=      mask.astype(np.floataa    ) / 2_55.0
						SCREAMING_SNAKE_CASE__								=      0
						SCREAMING_SNAKE_CASE__								=      1
						SCREAMING_SNAKE_CASE__								=      torch.from_numpy(snake_case__    )
			elif isinstance(mask[0]		,				torch.Tensor    ):
						SCREAMING_SNAKE_CASE__								=      torch.cat(snake_case__		,				dim=0    )
			return mask





class    lowerCamelCase						(A__			):
			lowerCamelCase__   :		UNetaDModel
			lowerCamelCase__   :		RePaintScheduler

			def __init__(					self				:						int    ,				__UpperCAmelCase				:						List[str]    ,				__UpperCAmelCase				:						Dict       )   ->    Tuple:
						super().__init__()
						self.register_modules(unet=__UpperCAmelCase    ,				scheduler=__UpperCAmelCase       )





			@torch.no_grad()
			def __call__(					self				:						List[str]    ,				__UpperCAmelCase				:						Union[torch.Tensor, PIL.Image.Image]    ,				__UpperCAmelCase				:						Union[torch.Tensor, PIL.Image.Image]    ,				__UpperCAmelCase				:						int = 2_5_0    ,				__UpperCAmelCase				:						float = 0.0    ,				__UpperCAmelCase				:						int = 1_0    ,				__UpperCAmelCase				:						int = 1_0    ,				__UpperCAmelCase				:						Optional[Union[torch.Generator, List[torch.Generator]]] = None    ,				__UpperCAmelCase				:						Optional[str] = "pil"    ,				__UpperCAmelCase				:						bool = True    ,				)   ->    Union[ImagePipelineOutput, Tuple]:
						SCREAMING_SNAKE_CASE__								=      image

						SCREAMING_SNAKE_CASE__								=      _preprocess_image(__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      original_image.to(device=self.device    ,				dtype=self.unet.dtype       )
						SCREAMING_SNAKE_CASE__								=      _preprocess_mask(__UpperCAmelCase       )
						SCREAMING_SNAKE_CASE__								=      mask_image.to(device=self.device    ,				dtype=self.unet.dtype       )

						SCREAMING_SNAKE_CASE__								=      original_image.shape[0]

						# sample gaussian noise to begin the loop
						if isinstance(__UpperCAmelCase    ,				__UpperCAmelCase       ) and len(__UpperCAmelCase       ) != batch_size:
									raise ValueError(
									    F"""You have passed a list of generators of length {len(__UpperCAmelCase       )}, but requested an effective batch"""
									    F""" size of {batch_size}. Make sure the batch size matches the length of the generators."""       )

						SCREAMING_SNAKE_CASE__								=      original_image.shape
						SCREAMING_SNAKE_CASE__								=      randn_tensor(__UpperCAmelCase    ,				generator=__UpperCAmelCase    ,				device=self.device    ,				dtype=self.unet.dtype       )

						# set step values
						self.scheduler.set_timesteps(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				self.device       )
						SCREAMING_SNAKE_CASE__								=      eta

						SCREAMING_SNAKE_CASE__								=      self.scheduler.timesteps[0] + 1
						SCREAMING_SNAKE_CASE__								=      generator[0] if isinstance(__UpperCAmelCase    ,				__UpperCAmelCase       ) else generator
						for i, t in enumerate(self.progress_bar(self.scheduler.timesteps       )       ):
									if t < t_last:
												# predict the noise residual
												SCREAMING_SNAKE_CASE__								=      self.unet(__UpperCAmelCase    ,				__UpperCAmelCase       ).sample
												# compute previous image: x_t -> x_t-1
												SCREAMING_SNAKE_CASE__								=      self.scheduler.step(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase       ).prev_sample

									else:
												# compute the reverse: x_t-1 -> x_t
												SCREAMING_SNAKE_CASE__								=      self.scheduler.undo_step(__UpperCAmelCase    ,				__UpperCAmelCase    ,				__UpperCAmelCase       )
									SCREAMING_SNAKE_CASE__								=      t

						SCREAMING_SNAKE_CASE__								=      (image / 2 + 0.5).clamp(0    ,				1       )
						SCREAMING_SNAKE_CASE__								=      image.cpu().permute(0    ,				2    ,				3    ,				1       ).numpy()
						if output_type == "pil":
									SCREAMING_SNAKE_CASE__								=      self.numpy_to_pil(__UpperCAmelCase       )

						if not return_dict:
									return (image,)

						return ImagePipelineOutput(images=__UpperCAmelCase       )


