

from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)


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

__lowerCamelCase						    =      '''
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Img2ImgPipeline, KandinskyV22PriorPipeline
        >>> from diffusers.utils import load_image
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained(
        ...     "kandinsky-community/kandinsky-2-2-prior", torch_dtype=torch.float16
        ... )
        >>> pipe_prior.to("cuda")

        >>> prompt = "A red cartoon frog, 4k"
        >>> image_emb, zero_image_emb = pipe_prior(prompt, return_dict=False)

        >>> pipe = KandinskyV22Img2ImgPipeline.from_pretrained(
        ...     "kandinsky-community/kandinsky-2-2-decoder", torch_dtype=torch.float16
        ... )
        >>> pipe.to("cuda")

        >>> init_image = load_image(
        ...     "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
        ...     "/kandinsky/frog.png"
        ... )

        >>> image = pipe(
        ...     image=init_image,
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=100,
        ...     strength=0.2,
        ... ).images

        >>> image[0].save("red_frog.png")
        ```
'''
def  UpperCAmelCase__      ( UpperCAmelCase__,    UpperCAmelCase__,    UpperCAmelCase__=8			)     ->     Union[str, Any]:
       A_		   = height // scale_factor**2
       if height % scale_factor**2 != 0:
              new_height += 1
       A_		   = width // scale_factor**2
       if width % scale_factor**2 != 0:
              new_width += 1
       return new_height * scale_factor, new_width * scale_factor
def  UpperCAmelCase__      ( UpperCAmelCase__,    UpperCAmelCase__=5_12,    UpperCAmelCase__=5_12			)     ->     Union[str, Any]:
       A_		   = pil_image.resize((w, h),    resample=Image.BICUBIC,    reducing_gap=1			)
       A_		   = np.array(pil_image.convert("""RGB"""			)			)
       A_		   = arr.astype(np.floataa			) / 127.5 - 1
       A_		   = np.transpose(__lowerCAmelCase,    [2, 0, 1]			)
       A_		   = torch.from_numpy(__lowerCAmelCase			).unsqueeze(0			)
       return image







class    A__ ( lowerCamelCase_     ):



    def __init__(   self						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			)  -> Tuple:





           '''simple docstring'''
           super().__init__()

           self.register_modules(
               unet=__snake_case						,			scheduler=__snake_case						,			movq=__snake_case						,			)
           A_		   = 2 ** (len(self.movq.config.block_out_channels    ) - 1)



    def    snake_case_     (   self						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__    )  -> Optional[int]:





           '''simple docstring'''
           # get the original timestep using init_timestep
           A_		   = min(int(num_inference_steps * strength    )						,			__snake_case    )

           A_		   = max(num_inference_steps - init_timestep						,			0    )
           A_		   = self.scheduler.timesteps[t_start:]

           return timesteps, num_inference_steps - t_start



    def    snake_case_     (   self						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__=None    )  -> Union[str, Any]:





           '''simple docstring'''
           if not isinstance(__snake_case						,			(torch.Tensor, PIL.Image.Image, list)    ):
                  raise ValueError(
                      f'''`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(__snake_case    )}'''    )

           A_		   = image.to(device=__snake_case						,			dtype=__snake_case    )

           A_		   = batch_size * num_images_per_prompt

           if image.shape[1] == 4:
                  A_		   = image

           else:
                  if isinstance(__snake_case						,			__snake_case    ) and len(__snake_case    ) != batch_size:
                         raise ValueError(
                             f'''You have passed a list of generators of length {len(__snake_case    )}, but requested an effective batch'''
                             f''' size of {batch_size}. Make sure the batch size matches the length of the generators.'''    )

                  elif isinstance(__snake_case						,			__snake_case    ):
                         A_		   = [
                             self.movq.encode(image[i : i + 1]    ).latent_dist.sample(generator[i]    ) for i in range(__snake_case    )
                         ]
                         A_		   = torch.cat(__snake_case						,			dim=0    )
                  else:
                         A_		   = self.movq.encode(__snake_case    ).latent_dist.sample(__snake_case    )

                  A_		   = self.movq.config.scaling_factor * init_latents

           A_		   = torch.cat([init_latents]						,			dim=0    )

           A_		   = init_latents.shape
           A_		   = randn_tensor(__snake_case						,			generator=__snake_case						,			device=__snake_case						,			dtype=__snake_case    )

           # get latents
           A_		   = self.scheduler.add_noise(__snake_case						,			__snake_case						,			__snake_case    )

           A_		   = init_latents

           return latents



    def    snake_case_     (   self						,			UpperCamelCase__=0    )  -> str:





           '''simple docstring'''
           if is_accelerate_available():
                  from accelerate import cpu_offload
           else:
                  raise ImportError("""Please install accelerate via `pip install accelerate`"""    )

           A_		   = torch.device(f'''cuda:{gpu_id}'''    )

           A_		   = [
               self.unet,
               self.movq,
           ]
           for cpu_offloaded_model in models:
                  if cpu_offloaded_model is not None:
                         cpu_offload(__snake_case						,			__snake_case    )



    def    snake_case_     (   self						,			UpperCamelCase__=0    )  -> int:





           '''simple docstring'''
           if is_accelerate_available() and is_accelerate_version(""">="""						,			"""0.17.0.dev0"""    ):
                  from accelerate import cpu_offload_with_hook
           else:
                  raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."""    )

           A_		   = torch.device(f'''cuda:{gpu_id}'''    )

           if self.device.type != "cpu":
                  self.to("""cpu"""						,			silence_dtype_warnings=__snake_case    )
                  torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

           A_		   = None
           for cpu_offloaded_model in [self.unet, self.movq]:
                  A_   ,     A_		   = cpu_offload_with_hook(__snake_case						,			__snake_case						,			prev_module_hook=__snake_case    )

           # We'll offload the last model manually.
           A_		   = hook



    @property
    # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
    def    snake_case_     (   self    )  -> Any:





           '''simple docstring'''
           if not hasattr(self.unet						,			"""_hf_hook"""    ):
                  return self.device
           for module in self.unet.modules():
                  if (
                      hasattr(__snake_case						,			"""_hf_hook"""    )
                      and hasattr(module._hf_hook						,			"""execution_device"""    )
                      and module._hf_hook.execution_device is not None
                  ):
                         return torch.device(module._hf_hook.execution_device    )
           return self.device







    @torch.no_grad()
    @replace_example_docstring(__snake_case    )
    def __call__(   self						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__						,			UpperCamelCase__ = 512						,			UpperCamelCase__ = 512						,			UpperCamelCase__ = 100						,			UpperCamelCase__ = 4.0						,			UpperCamelCase__ = 0.3						,			UpperCamelCase__ = 1						,			UpperCamelCase__ = None						,			UpperCamelCase__ = "pil"						,			UpperCamelCase__ = True						,			)  -> Dict:





           '''simple docstring'''
           A_		   = self._execution_device

           A_		   = guidance_scale > 1.0

           if isinstance(__snake_case						,			__snake_case    ):
                  A_		   = torch.cat(__snake_case						,			dim=0    )
           A_		   = image_embeds.shape[0]
           if isinstance(__snake_case						,			__snake_case    ):
                  A_		   = torch.cat(__snake_case						,			dim=0    )

           if do_classifier_free_guidance:
                  A_		   = image_embeds.repeat_interleave(__snake_case						,			dim=0    )
                  A_		   = negative_image_embeds.repeat_interleave(__snake_case						,			dim=0    )

           A_		   = torch.cat([negative_image_embeds, image_embeds]						,			dim=0    ).to(dtype=self.unet.dtype						,			device=__snake_case    )

           if not isinstance(__snake_case						,			__snake_case    ):
                  A_		   = [image]
           if not all(isinstance(__snake_case						,			(PIL.Image.Image, torch.Tensor)    ) for i in image    ):
                  raise ValueError(
                      f'''Input is in incorrect format: {[type(__snake_case    ) for i in image]}. Currently, we only support  PIL image and pytorch tensor'''    )

           A_		   = torch.cat([prepare_image(__snake_case						,			__snake_case						,			__snake_case    ) for i in image]						,			dim=0    )
           A_		   = image.to(dtype=image_embeds.dtype						,			device=__snake_case    )

           A_		   = self.movq.encode(__snake_case    )["""latents"""]
           A_		   = latents.repeat_interleave(__snake_case						,			dim=0    )
           self.scheduler.set_timesteps(__snake_case						,			device=__snake_case    )
           A_   ,     A_		   = self.get_timesteps(__snake_case						,			__snake_case						,			__snake_case    )
           A_		   = timesteps[:1].repeat(batch_size * num_images_per_prompt    )
           A_   ,     A_		   = downscale_height_and_width(__snake_case						,			__snake_case						,			self.movq_scale_factor    )
           A_		   = self.prepare_latents(
               __snake_case						,			__snake_case						,			__snake_case						,			__snake_case						,			image_embeds.dtype						,			__snake_case						,			__snake_case    )
           for i, t in enumerate(self.progress_bar(__snake_case    )    ):
                  # expand the latents if we are doing classifier free guidance
                  A_		   = torch.cat([latents] * 2    ) if do_classifier_free_guidance else latents

                  A_		   = {"""image_embeds""": image_embeds}
                  A_		   = self.unet(
                      sample=__snake_case						,			timestep=__snake_case						,			encoder_hidden_states=__snake_case						,			added_cond_kwargs=__snake_case						,			return_dict=__snake_case						,			)[0]

                  if do_classifier_free_guidance:
                         A_   ,     A_		   = noise_pred.split(latents.shape[1]						,			dim=1    )
                         A_   ,     A_		   = noise_pred.chunk(2    )
                         A_   ,     A_		   = variance_pred.chunk(2    )
                         A_		   = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                         A_		   = torch.cat([noise_pred, variance_pred_text]						,			dim=1    )

                  if not (
                      hasattr(self.scheduler.config						,			"""variance_type"""    )
                      and self.scheduler.config.variance_type in ["learned", "learned_range"]
                  ):
                         A_   ,     A_		   = noise_pred.split(latents.shape[1]						,			dim=1    )

                  # compute the previous noisy sample x_t -> x_t-1
                  A_		   = self.scheduler.step(
                      __snake_case						,			__snake_case						,			__snake_case						,			generator=__snake_case						,			)[0]

           # post-processing
           A_		   = self.movq.decode(__snake_case						,			force_not_quantize=__snake_case    )["""sample"""]

           if output_type not in ["pt", "np", "pil"]:
                  raise ValueError(f'''Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}'''    )

           if output_type in ["np", "pil"]:
                  A_		   = image * 0.5 + 0.5
                  A_		   = image.clamp(0						,			1    )
                  A_		   = image.cpu().permute(0						,			2						,			3						,			1    ).float().numpy()

           if output_type == "pil":
                  A_		   = self.numpy_to_pil(__snake_case    )

           if not return_dict:
                  return (image,)

           return ImagePipelineOutput(images=__snake_case    )



'''simple docstring'''







import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()


class    A__ ( unittest.TestCase     ):



    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()



    @property
    def    snake_case_     (   self    )  -> List[str]:





           '''simple docstring'''
           A_		   = 1
           A_		   = 3
           A_		   = (32, 32)

           A_		   = floats_tensor((batch_size, num_channels) + sizes						,			rng=random.Random(0    )    ).to(UpperCamelCase__    )
           return image



    @property
    def    snake_case_     (   self    )  -> Union[str, Any]:





           '''simple docstring'''
           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						,			)
           return model



    @property
    def    snake_case_     (   self    )  -> List[str]:





           '''simple docstring'''
           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						,			)
           return model



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





           '''simple docstring'''
           torch.manual_seed(0    )
           A_		   = RobertaSeriesConfig(
               hidden_size=32						,			project_dim=32						,			intermediate_size=37						,			layer_norm_eps=1e-0_5						,			num_attention_heads=4						,			num_hidden_layers=5						,			pad_token_id=1						,			vocab_size=5006						,			)
           return RobertaSeriesModelWithTransformation(UpperCamelCase__    )



    @property
    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           def extract(*UpperCamelCase__						,			**UpperCamelCase__    ):


                  class    A__ :



                      def __init__(   self    )  -> Dict:





                             '''simple docstring'''
                             A_		   = torch.ones([0]    )







                      def    snake_case_     (   self						,			UpperCamelCase__    )  -> str:





                             '''simple docstring'''
                             self.pixel_values.to(UpperCamelCase__    )
                             return self

                  return Out()

           return extract



    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = """cpu"""  # ensure determinism for the device-dependent torch.Generator
           A_		   = self.dummy_cond_unet
           A_		   = PNDMScheduler(skip_prk_steps=UpperCamelCase__    )
           A_		   = self.dummy_vae
           A_		   = self.dummy_text_encoder
           A_		   = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""    )
           A_		   = 77

           A_		   = self.dummy_image.to(UpperCamelCase__    )
           A_		   = init_image / 2 + 0.5

           # make sure here that pndm scheduler skips prk
           A_		   = AltDiffusionImgaImgPipeline(
               unet=UpperCamelCase__						,			scheduler=UpperCamelCase__						,			vae=UpperCamelCase__						,			text_encoder=UpperCamelCase__						,			tokenizer=UpperCamelCase__						,			safety_checker=UpperCamelCase__						,			feature_extractor=self.dummy_extractor						,			)
           A_		   = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor						,			do_normalize=UpperCamelCase__    )
           A_		   = alt_pipe.to(UpperCamelCase__    )
           alt_pipe.set_progress_bar_config(disable=UpperCamelCase__    )

           A_		   = """A painting of a squirrel eating a burger"""
           A_		   = torch.Generator(device=UpperCamelCase__    ).manual_seed(0    )
           A_		   = alt_pipe(
               [prompt]						,			generator=UpperCamelCase__						,			guidance_scale=6.0						,			num_inference_steps=2						,			output_type="""np"""						,			image=UpperCamelCase__						,			)

           A_		   = output.images

           A_		   = torch.Generator(device=UpperCamelCase__    ).manual_seed(0    )
           A_		   = alt_pipe(
               [prompt]						,			generator=UpperCamelCase__						,			guidance_scale=6.0						,			num_inference_steps=2						,			output_type="""np"""						,			image=UpperCamelCase__						,			return_dict=UpperCamelCase__						,			)[0]

           A_		   = image[0, -3:, -3:, -1]
           A_		   = image_from_tuple[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           A_		   = np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]    )

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



    @unittest.skipIf(torch_device != """cuda"""						,			"""This test requires a GPU"""    )
    def    snake_case_     (   self    )  -> Union[str, Any]:





           '''simple docstring'''
           A_		   = self.dummy_cond_unet
           A_		   = PNDMScheduler(skip_prk_steps=UpperCamelCase__    )
           A_		   = self.dummy_vae
           A_		   = self.dummy_text_encoder
           A_		   = XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""    )
           A_		   = 77

           A_		   = self.dummy_image.to(UpperCamelCase__    )

           # put models in fp16
           A_		   = unet.half()
           A_		   = vae.half()
           A_		   = bert.half()

           # make sure here that pndm scheduler skips prk
           A_		   = AltDiffusionImgaImgPipeline(
               unet=UpperCamelCase__						,			scheduler=UpperCamelCase__						,			vae=UpperCamelCase__						,			text_encoder=UpperCamelCase__						,			tokenizer=UpperCamelCase__						,			safety_checker=UpperCamelCase__						,			feature_extractor=self.dummy_extractor						,			)
           A_		   = VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor						,			do_normalize=UpperCamelCase__    )
           A_		   = alt_pipe.to(UpperCamelCase__    )
           alt_pipe.set_progress_bar_config(disable=UpperCamelCase__    )

           A_		   = """A painting of a squirrel eating a burger"""
           A_		   = torch.manual_seed(0    )
           A_		   = alt_pipe(
               [prompt]						,			generator=UpperCamelCase__						,			num_inference_steps=2						,			output_type="""np"""						,			image=UpperCamelCase__						,			).images

           assert image.shape == (1, 32, 32, 3)







    @unittest.skipIf(torch_device != """cuda"""						,			"""This test requires a GPU"""    )
    def    snake_case_     (   self    )  -> Any:





           '''simple docstring'''
           A_		   = load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/img2img/sketch-mountains-input.jpg"""    )
           # resize to resolution that is divisible by 8 but not 16 or 32
           A_		   = init_image.resize((760, 504)    )

           A_		   = """BAAI/AltDiffusion"""
           A_		   = AltDiffusionImgaImgPipeline.from_pretrained(
               UpperCamelCase__						,			safety_checker=UpperCamelCase__						,			)
           pipe.to(UpperCamelCase__    )
           pipe.set_progress_bar_config(disable=UpperCamelCase__    )
           pipe.enable_attention_slicing()

           A_		   = """A fantasy landscape, trending on artstation"""

           A_		   = torch.manual_seed(0    )
           A_		   = pipe(
               prompt=UpperCamelCase__						,			image=UpperCamelCase__						,			strength=0.75						,			guidance_scale=7.5						,			generator=UpperCamelCase__						,			output_type="""np"""						,			)
           A_		   = output.images[0]

           A_		   = image[255:258, 383:386, -1]

           assert image.shape == (504, 760, 3)
           A_		   = np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]    )

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


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



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





           '''simple docstring'''
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()







    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = load_image(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
               """/img2img/sketch-mountains-input.jpg"""    )
           A_		   = init_image.resize((768, 512)    )
           A_		   = load_numpy(
               """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"""    )

           A_		   = """BAAI/AltDiffusion"""
           A_		   = AltDiffusionImgaImgPipeline.from_pretrained(
               UpperCamelCase__						,			safety_checker=UpperCamelCase__						,			)
           pipe.to(UpperCamelCase__    )
           pipe.set_progress_bar_config(disable=UpperCamelCase__    )
           pipe.enable_attention_slicing()

           A_		   = """A fantasy landscape, trending on artstation"""

           A_		   = torch.manual_seed(0    )
           A_		   = pipe(
               prompt=UpperCamelCase__						,			image=UpperCamelCase__						,			strength=0.75						,			guidance_scale=7.5						,			generator=UpperCamelCase__						,			output_type="""np"""						,			)
           A_		   = output.images[0]

           assert image.shape == (512, 768, 3)
           # img2img is flaky across GPUs even in fp32, so using MAE here
           assert np.abs(expected_image - image    ).max() < 1e-2
