






'''simple docstring'''





# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
 import torch
class 				lowercase       (							TensorFormatter[Mapping, "torch.Tensor", Mapping]      ):


    '''simple docstring'''

    def __init__(					self     :     Optional[int]				,     __lowerCamelCase     :     int=None				,     **__lowerCamelCase     :     List[str]     )							->							Union[str, Any]:

     '''simple docstring'''




     super().__init__(features=__lowerCamelCase     )
     lowerCamelCase__  =     torch_tensor_kwargs
     import torch  # noqa import torch at initialization

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     List[str]     )							->							Union[str, Any]:

     '''simple docstring'''




     import torch

     if isinstance(__lowerCamelCase				,     __lowerCamelCase     ) and column:
      if all(
          isinstance(__lowerCamelCase				,     torch.Tensor     ) and x.shape == column[0].shape and x.dtype == column[0].dtype
          for x in column     ):
       return torch.stack(__lowerCamelCase     )
     return column

    def    a__				(					self     :     Optional[Any]				,     __lowerCamelCase     :     Optional[Any]     )							->							Any:

     '''simple docstring'''




     import torch

     if isinstance(__lowerCamelCase				,     (str, bytes, type(__lowerCamelCase     ))     ):
      return value
     elif isinstance(__lowerCamelCase				,     (np.character, np.ndarray)     ) and np.issubdtype(value.dtype				,     np.character     ):
      return value.tolist()

     lowerCamelCase__  =     {}

     if isinstance(__lowerCamelCase				,     (np.number, np.ndarray)     ) and np.issubdtype(value.dtype				,     np.integer     ):
      lowerCamelCase__  =     {"dtype": torch.intaa}
     elif isinstance(__lowerCamelCase				,     (np.number, np.ndarray)     ) and np.issubdtype(value.dtype				,     np.floating     ):
      lowerCamelCase__  =     {"dtype": torch.floataa}
     elif config.PIL_AVAILABLE and "PIL" in sys.modules:
      import PIL.Image

      if isinstance(__lowerCamelCase				,     PIL.Image.Image     ):
       lowerCamelCase__  =     np.asarray(__lowerCamelCase     )
     return torch.tensor(__lowerCamelCase				,     **{**default_dtype, **self.torch_tensor_kwargs}     )

    def    a__				(					self     :     int				,     __lowerCamelCase     :     Dict     )							->							Any:

     '''simple docstring'''




     import torch

     # support for torch, tf, jax etc.
     if hasattr(__lowerCamelCase				,     "__array__"     ) and not isinstance(__lowerCamelCase				,     torch.Tensor     ):
      lowerCamelCase__  =     data_struct.__array__()
     # support for nested types like struct of list of struct
     if isinstance(__lowerCamelCase				,     np.ndarray     ):
      if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
       return self._consolidate([self.recursive_tensorize(__lowerCamelCase     ) for substruct in data_struct]     )
     elif isinstance(__lowerCamelCase				,     (list, tuple)     ):
      return self._consolidate([self.recursive_tensorize(__lowerCamelCase     ) for substruct in data_struct]     )
     return self._tensorize(__lowerCamelCase     )

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     dict     )							->							Tuple:

     '''simple docstring'''




     return map_nested(self._recursive_tensorize				,     __lowerCamelCase				,     map_list=__lowerCamelCase     )

    def    a__				(					self     :     Dict				,     __lowerCamelCase     :     pa.Table     )							->							Mapping:

     '''simple docstring'''




     lowerCamelCase__  =     self.numpy_arrow_extractor().extract_row(__lowerCamelCase     )
     lowerCamelCase__  =     self.python_features_decoder.decode_row(__lowerCamelCase     )
     return self.recursive_tensorize(__lowerCamelCase     )

    def    a__				(					self     :     List[Any]				,     __lowerCamelCase     :     pa.Table     )							->							"torch.Tensor":

     '''simple docstring'''




     lowerCamelCase__  =     self.numpy_arrow_extractor().extract_column(__lowerCamelCase     )
     lowerCamelCase__  =     self.python_features_decoder.decode_column(__lowerCamelCase				,     pa_table.column_names[0]     )
     lowerCamelCase__  =     self.recursive_tensorize(__lowerCamelCase     )
     lowerCamelCase__  =     self._consolidate(__lowerCamelCase     )
     return column




    def    a__				(					self     :     Optional[int]				,     __lowerCamelCase     :     pa.Table     )							->							Mapping:

     '''simple docstring'''




     lowerCamelCase__  =     self.numpy_arrow_extractor().extract_batch(__lowerCamelCase     )
     lowerCamelCase__  =     self.python_features_decoder.decode_batch(__lowerCamelCase     )
     lowerCamelCase__  =     self.recursive_tensorize(__lowerCamelCase     )
     for column_name in batch:
      lowerCamelCase__  =     self._consolidate(batch[column_name]     )
     return batch









'''simple docstring'''





import gc
import random
import tempfile
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMInverseScheduler,
    DDIMScheduler,
    DPMSolverMultistepInverseScheduler,
    DPMSolverMultistepScheduler,
    StableDiffusionDiffEditPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_image, slow
from diffusers.utils.testing_utils import enable_full_determinism, floats_tensor, require_torch_gpu, torch_device

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


enable_full_determinism()
class 				lowercase       (							_lowerCamelCase ,       _lowerCamelCase ,       unittest.TestCase      ):


    '''simple docstring'''

    lowerCAmelCase__      				=							StableDiffusionDiffEditPipeline
    lowerCAmelCase__      				=							TEXT_GUIDED_IMAGE_INPAINTING_PARAMS - {"height", "width", "image"} | {"image_latents"}
    lowerCAmelCase__      				=							TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS - {"image"} | {"image_latents"}
    lowerCAmelCase__      				=							frozenset(
        []      )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
    lowerCAmelCase__      				=							frozenset([]      )

    def    a__				(					self     :     int     )							->							Dict:

     '''simple docstring'''




     torch.manual_seed(0     )
     lowerCamelCase__  =     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				,     attention_head_dim=(2, 4)				,     use_linear_projection=__lowerCamelCase				,     )
     lowerCamelCase__  =     DDIMScheduler(
         beta_start=0.0_0_0_8_5				,     beta_end=0.0_1_2				,     beta_schedule="scaled_linear"				,     clip_sample=__lowerCamelCase				,     set_alpha_to_one=__lowerCamelCase				,     )
     lowerCamelCase__  =     DDIMInverseScheduler(
         beta_start=0.0_0_0_8_5				,     beta_end=0.0_1_2				,     beta_schedule="scaled_linear"				,     clip_sample=__lowerCamelCase				,     set_alpha_to_zero=__lowerCamelCase				,     )
     torch.manual_seed(0     )
     lowerCamelCase__  =     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     )
     lowerCamelCase__  =     CLIPTextConfig(
         bos_token_id=0				,     eos_token_id=2				,     hidden_size=32				,     intermediate_size=37				,     layer_norm_eps=1E-05				,     num_attention_heads=4				,     num_hidden_layers=5				,     pad_token_id=1				,     vocab_size=1000				,     hidden_act="gelu"				,     projection_dim=512				,     )
     lowerCamelCase__  =     CLIPTextModel(__lowerCamelCase     )
     lowerCamelCase__  =     CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"     )

     lowerCamelCase__  =     {
         "unet": unet,
         "scheduler": scheduler,
         "inverse_scheduler": inverse_scheduler,
         "vae": vae,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
         "safety_checker": None,
         "feature_extractor": None,
     }

     return components

    def    a__				(					self     :     List[Any]				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     Any=0     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  =     floats_tensor((1, 16, 16)				,     rng=random.Random(__lowerCamelCase     )     ).to(__lowerCamelCase     )
     lowerCamelCase__  =     floats_tensor((1, 2, 4, 16, 16)				,     rng=random.Random(__lowerCamelCase     )     ).to(__lowerCamelCase     )
     if str(__lowerCamelCase     ).startswith("mps"     ):
      lowerCamelCase__  =     torch.manual_seed(__lowerCamelCase     )
     else:
      lowerCamelCase__  =     torch.Generator(device=__lowerCamelCase     ).manual_seed(__lowerCamelCase     )
     lowerCamelCase__  =     {
         "prompt": "a dog and a newt",
         "mask_image": mask,
         "image_latents": latents,
         "generator": generator,
         "num_inference_steps": 2,
         "inpaint_strength": 1.0,
         "guidance_scale": 6.0,
         "output_type": "numpy",
     }

     return inputs

    def    a__				(					self     :     str				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     Optional[int]=0     )							->							Optional[Any]:

     '''simple docstring'''




     lowerCamelCase__  =     floats_tensor((1, 3, 32, 32)				,     rng=random.Random(__lowerCamelCase     )     ).to(__lowerCamelCase     )
     lowerCamelCase__  =     image.cpu().permute(0				,     2				,     3				,     1     )[0]
     lowerCamelCase__  =     Image.fromarray(np.uinta(__lowerCamelCase     )     ).convert("RGB"     )
     if str(__lowerCamelCase     ).startswith("mps"     ):
      lowerCamelCase__  =     torch.manual_seed(__lowerCamelCase     )
     else:
      lowerCamelCase__  =     torch.Generator(device=__lowerCamelCase     ).manual_seed(__lowerCamelCase     )
     lowerCamelCase__  =     {
         "image": image,
         "source_prompt": "a cat and a frog",
         "target_prompt": "a dog and a newt",
         "generator": generator,
         "num_inference_steps": 2,
         "num_maps_per_mask": 2,
         "mask_encode_strength": 1.0,
         "guidance_scale": 6.0,
         "output_type": "numpy",
     }

     return inputs

    def    a__				(					self     :     Optional[int]				,     __lowerCamelCase     :     Optional[int]				,     __lowerCamelCase     :     List[str]=0     )							->							List[Any]:

     '''simple docstring'''




     lowerCamelCase__  =     floats_tensor((1, 3, 32, 32)				,     rng=random.Random(__lowerCamelCase     )     ).to(__lowerCamelCase     )
     lowerCamelCase__  =     image.cpu().permute(0				,     2				,     3				,     1     )[0]
     lowerCamelCase__  =     Image.fromarray(np.uinta(__lowerCamelCase     )     ).convert("RGB"     )
     if str(__lowerCamelCase     ).startswith("mps"     ):
      lowerCamelCase__  =     torch.manual_seed(__lowerCamelCase     )
     else:
      lowerCamelCase__  =     torch.Generator(device=__lowerCamelCase     ).manual_seed(__lowerCamelCase     )
     lowerCamelCase__  =     {
         "image": image,
         "prompt": "a cat and a frog",
         "generator": generator,
         "num_inference_steps": 2,
         "inpaint_strength": 1.0,
         "guidance_scale": 6.0,
         "decode_latents": True,
         "output_type": "numpy",
     }
     return inputs

    def    a__				(					self     :     Tuple     )							->							str:

     '''simple docstring'''




     if not hasattr(self.pipeline_class				,     "_optional_components"     ):
      return

     lowerCamelCase__  =     self.get_dummy_components()
     lowerCamelCase__  =     self.pipeline_class(**__lowerCamelCase     )
     pipe.to(__lowerCamelCase     )
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     # set all optional components to None and update pipeline config accordingly
     for optional_component in pipe._optional_components:
      setattr(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )
     pipe.register_modules(**{optional_component: None for optional_component in pipe._optional_components}     )

     lowerCamelCase__  =     self.get_dummy_inputs(__lowerCamelCase     )
     lowerCamelCase__  =     pipe(**__lowerCamelCase     )[0]

     with tempfile.TemporaryDirectory() as tmpdir:
      pipe.save_pretrained(__lowerCamelCase     )
      lowerCamelCase__  =     self.pipeline_class.from_pretrained(__lowerCamelCase     )
      pipe_loaded.to(__lowerCamelCase     )
      pipe_loaded.set_progress_bar_config(disable=__lowerCamelCase     )

     for optional_component in pipe._optional_components:
      self.assertTrue(
          getattr(__lowerCamelCase				,     __lowerCamelCase     ) is None				,     f'''`{optional_component}` did not stay set to None after loading.'''				,     )

     lowerCamelCase__  =     self.get_dummy_inputs(__lowerCamelCase     )
     lowerCamelCase__  =     pipe_loaded(**__lowerCamelCase     )[0]

     lowerCamelCase__  =     np.abs(output - output_loaded     ).max()
     self.assertLess(__lowerCamelCase				,     1E-4     )

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

     '''simple docstring'''




     lowerCamelCase__  =     "cpu"

     lowerCamelCase__  =     self.get_dummy_components()
     lowerCamelCase__  =     self.pipeline_class(**__lowerCamelCase     )
     pipe.to(__lowerCamelCase     )
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     lowerCamelCase__  =     self.get_dummy_mask_inputs(__lowerCamelCase     )
     lowerCamelCase__  =     pipe.generate_mask(**__lowerCamelCase     )
     lowerCamelCase__  =     mask[0, -3:, -3:]

     self.assertEqual(mask.shape				,     (1, 16, 16)     )
     lowerCamelCase__  =     np.array([0] * 9     )
     lowerCamelCase__  =     np.abs(mask_slice.flatten() - expected_slice     ).max()
     self.assertLessEqual(__lowerCamelCase				,     1E-3     )
     self.assertEqual(mask[0, -3, -4]				,     0     )

    def    a__				(					self     :     List[str]     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  =     "cpu"

     lowerCamelCase__  =     self.get_dummy_components()
     lowerCamelCase__  =     self.pipeline_class(**__lowerCamelCase     )
     pipe.to(__lowerCamelCase     )
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     lowerCamelCase__  =     self.get_dummy_inversion_inputs(__lowerCamelCase     )
     lowerCamelCase__  =     pipe.invert(**__lowerCamelCase     ).images
     lowerCamelCase__  =     image[0, -1, -3:, -3:]

     self.assertEqual(image.shape				,     (2, 32, 32, 3)     )
     lowerCamelCase__  =     np.array(
         [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9]				,     )
     lowerCamelCase__  =     np.abs(image_slice.flatten() - expected_slice     ).max()
     self.assertLessEqual(__lowerCamelCase				,     1E-3     )

    def    a__				(					self     :     Tuple     )							->							Tuple:

     '''simple docstring'''




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




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

     '''simple docstring'''




     lowerCamelCase__  =     "cpu"

     lowerCamelCase__  =     self.get_dummy_components()

     lowerCamelCase__  =     {"beta_start": 0.0_0_0_8_5, "beta_end": 0.0_1_2, "beta_schedule": "scaled_linear"}
     lowerCamelCase__  =     DPMSolverMultistepScheduler(**__lowerCamelCase     )
     lowerCamelCase__  =     DPMSolverMultistepInverseScheduler(**__lowerCamelCase     )

     lowerCamelCase__  =     self.pipeline_class(**__lowerCamelCase     )
     pipe.to(__lowerCamelCase     )
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     lowerCamelCase__  =     self.get_dummy_inversion_inputs(__lowerCamelCase     )
     lowerCamelCase__  =     pipe.invert(**__lowerCamelCase     ).images
     lowerCamelCase__  =     image[0, -1, -3:, -3:]

     self.assertEqual(image.shape				,     (2, 32, 32, 3)     )
     lowerCamelCase__  =     np.array(
         [0.5_1_5_0, 0.5_1_3_4, 0.5_0_4_3, 0.5_3_7_6, 0.4_6_9_4, 0.5_1_0_5_0, 0.5_0_1_5, 0.4_4_0_7, 0.4_7_9_9]				,     )
     lowerCamelCase__  =     np.abs(image_slice.flatten() - expected_slice     ).max()
     self.assertLessEqual(__lowerCamelCase				,     1E-3     )
@require_torch_gpu
@slow
class 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    def    a__				(					self     :     Optional[int]     )							->							int:

     '''simple docstring'''




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

    @classmethod
    def    a__				(					cls     :     int     )							->							List[Any]:

     '''simple docstring'''




     lowerCamelCase__  =     load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/diffedit/fruit.png"     )

     lowerCamelCase__  =     raw_image.convert("RGB"     ).resize((768, 768)     )

     lowerCamelCase__  =     raw_image

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

     '''simple docstring'''




     lowerCamelCase__  =     torch.manual_seed(0     )

     lowerCamelCase__  =     StableDiffusionDiffEditPipeline.from_pretrained(
         "stabilityai/stable-diffusion-2-1"				,     safety_checker=__lowerCamelCase				,     torch_dtype=torch.floataa     )
     lowerCamelCase__  =     DDIMScheduler.from_config(pipe.scheduler.config     )
     lowerCamelCase__  =     DDIMInverseScheduler.from_config(pipe.scheduler.config     )
     pipe.enable_model_cpu_offload()
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     lowerCamelCase__  =     "a bowl of fruit"
     lowerCamelCase__  =     "a bowl of pears"

     lowerCamelCase__  =     pipe.generate_mask(
         image=self.raw_image				,     source_prompt=__lowerCamelCase				,     target_prompt=__lowerCamelCase				,     generator=__lowerCamelCase				,     )

     lowerCamelCase__  =     pipe.invert(
         prompt=__lowerCamelCase				,     image=self.raw_image				,     inpaint_strength=0.7				,     generator=__lowerCamelCase     ).latents

     lowerCamelCase__  =     pipe(
         prompt=__lowerCamelCase				,     mask_image=__lowerCamelCase				,     image_latents=__lowerCamelCase				,     generator=__lowerCamelCase				,     negative_prompt=__lowerCamelCase				,     inpaint_strength=0.7				,     output_type="numpy"				,     ).images[0]

     lowerCamelCase__  =     (
         np.array(
             load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/diffedit/pears.png"     ).resize((768, 768)     )     )
         / 255
     )
     assert np.abs((expected_image - image).max()     ) < 5E-1




    def    a__				(					self     :     List[str]     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  =     torch.manual_seed(0     )

     lowerCamelCase__  =     StableDiffusionDiffEditPipeline.from_pretrained(
         "stabilityai/stable-diffusion-2-1"				,     safety_checker=__lowerCamelCase				,     torch_dtype=torch.floataa     )
     lowerCamelCase__  =     DPMSolverMultistepScheduler.from_config(pipe.scheduler.config     )
     lowerCamelCase__  =     DPMSolverMultistepInverseScheduler.from_config(pipe.scheduler.config     )
     pipe.enable_model_cpu_offload()
     pipe.set_progress_bar_config(disable=__lowerCamelCase     )

     lowerCamelCase__  =     "a bowl of fruit"
     lowerCamelCase__  =     "a bowl of pears"

     lowerCamelCase__  =     pipe.generate_mask(
         image=self.raw_image				,     source_prompt=__lowerCamelCase				,     target_prompt=__lowerCamelCase				,     generator=__lowerCamelCase				,     )

     lowerCamelCase__  =     pipe.invert(
         prompt=__lowerCamelCase				,     image=self.raw_image				,     inpaint_strength=0.7				,     generator=__lowerCamelCase				,     num_inference_steps=25				,     ).latents

     lowerCamelCase__  =     pipe(
         prompt=__lowerCamelCase				,     mask_image=__lowerCamelCase				,     image_latents=__lowerCamelCase				,     generator=__lowerCamelCase				,     negative_prompt=__lowerCamelCase				,     inpaint_strength=0.7				,     num_inference_steps=25				,     output_type="numpy"				,     ).images[0]

     lowerCamelCase__  =     (
         np.array(
             load_image(
                 "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
                 "/diffedit/pears.png"     ).resize((768, 768)     )     )
         / 255
     )
     assert np.abs((expected_image - image).max()     ) < 5E-1

