


"""simple docstring"""



from typing import List, Optional, Union

import torch

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,
)


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

UpperCAmelCase      		=			'''
    Examples:
        ```py
        >>> from diffusers import KandinskyV22Pipeline, KandinskyV22PriorPipeline
        >>> import torch

        >>> pipe_prior = KandinskyV22PriorPipeline.from_pretrained("kandinsky-community/kandinsky-2-2-prior")
        >>> pipe_prior.to("cuda")
        >>> prompt = "red cat, 4k photo"
        >>> out = pipe_prior(prompt)
        >>> image_emb = out.image_embeds
        >>> zero_image_emb = out.negative_image_embeds
        >>> pipe = KandinskyV22Pipeline.from_pretrained("kandinsky-community/kandinsky-2-2-decoder")
        >>> pipe.to("cuda")
        >>> image = pipe(
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=zero_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=50,
        ... ).images
        >>> image[0].save("cat.png")
        ```
'''






def       lowerCamelCase					(a_						:List[Any]				,				a_						:List[Any]				,				a_						:List[Any]=8)     ->							List[Any]:
       lowercase		:Dict       							=				height // scale_factor**2
       if height % scale_factor**2 != 0:
              new_height += 1
       lowercase		:Dict       							=				width // scale_factor**2
       if width % scale_factor**2 != 0:
              new_width += 1
       return new_height * scale_factor, new_width * scale_factor




class 					__magic_name__		(			__UpperCAmelCase     ):






       def __init__(   self  :    List[str]  ,     snake_case__  :    UNetaDConditionModel  ,     snake_case__  :    DDPMScheduler  ,     snake_case__  :    VQModel  ,     ):

              '''simple docstring'''






              super().__init__()

              self.register_modules(
                  unet=snake_case__  ,     scheduler=snake_case__  ,     movq=snake_case__  ,     )
              lowercase		:Optional[int]       							=				2 ** (len(self.movq.config.block_out_channels				) - 1)






       def  __snake_case						(   self  :    List[str]  ,     snake_case__  :    int  ,     snake_case__  :    Any  ,     snake_case__  :    Tuple  ,     snake_case__  :    List[Any]  ,     snake_case__  :    Tuple  ,     snake_case__  :    Optional[int]				):

              '''simple docstring'''






              if latents is None:
                     lowercase		:List[Any]       							=				randn_tensor(snake_case__  ,     generator=snake_case__  ,     device=snake_case__  ,     dtype=snake_case__				)
              else:
                     if latents.shape != shape:
                            raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}"""				)
                     lowercase		:int       							=				latents.to(snake_case__				)

              lowercase		:str       							=				latents * scheduler.init_noise_sigma
              return latents






       def  __snake_case						(   self  :    str  ,     snake_case__  :    Optional[int]=0				):

              '''simple docstring'''






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

              lowercase		:Dict       							=				torch.device(f"""cuda:{gpu_id}"""				)

              lowercase		:int       							=				[
                  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  :    List[str]  ,     snake_case__  :    int=0				):

              '''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.'''				)

              lowercase		:Any       							=				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)

              lowercase		:Dict       							=				None
              for cpu_offloaded_model in [self.unet, self.movq]:
                     lowercase							,  lowercase		:Union[str, Any]       							=				cpu_offload_with_hook(snake_case__  ,     snake_case__  ,     prev_module_hook=snake_case__				)

              # We'll offload the last model manually.
              lowercase		:List[Any]       							=				hook






       @property
       # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
       def  __snake_case						(   self  :    List[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  :    Optional[Any]  ,     snake_case__  :    Union[torch.FloatTensor, List[torch.FloatTensor]]  ,     snake_case__  :    Union[torch.FloatTensor, List[torch.FloatTensor]]  ,     snake_case__  :    int = 5_1_2  ,     snake_case__  :    int = 5_1_2  ,     snake_case__  :    int = 1_0_0  ,     snake_case__  :    float = 4.0  ,     snake_case__  :    int = 1  ,     snake_case__  :    Optional[Union[torch.Generator, List[torch.Generator]]] = None  ,     snake_case__  :    Optional[torch.FloatTensor] = None  ,     snake_case__  :    Optional[str] = "pil"  ,     snake_case__  :    bool = True  ,     ):

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				self._execution_device

              lowercase		:Union[str, Any]       							=				guidance_scale > 1.0

              if isinstance(snake_case__  ,     snake_case__				):
                     lowercase		:Union[str, Any]       							=				torch.cat(snake_case__  ,     dim=0				)
              lowercase		:Dict       							=				image_embeds.shape[0] * num_images_per_prompt
              if isinstance(snake_case__  ,     snake_case__				):
                     lowercase		:Tuple       							=				torch.cat(snake_case__  ,     dim=0				)

              if do_classifier_free_guidance:
                     lowercase		:List[str]       							=				image_embeds.repeat_interleave(snake_case__  ,     dim=0				)
                     lowercase		:Dict       							=				negative_image_embeds.repeat_interleave(snake_case__  ,     dim=0				)

              lowercase		:str       							=				torch.cat([negative_image_embeds, image_embeds]  ,     dim=0				).to(dtype=self.unet.dtype  ,     device=snake_case__				)

              self.scheduler.set_timesteps(snake_case__  ,     device=snake_case__				)
              lowercase		:Dict       							=				self.scheduler.timesteps

              lowercase		:str       							=				self.unet.config.in_channels

              lowercase							,  lowercase		:Any       							=				downscale_height_and_width(snake_case__  ,     snake_case__  ,     self.movq_scale_factor				)

              # create initial latent
              lowercase		:Union[str, Any]       							=				self.prepare_latents(
                  (batch_size, num_channels_latents, height, width)  ,     image_embeds.dtype  ,     snake_case__  ,     snake_case__  ,     snake_case__  ,     self.scheduler  ,     )

              for i, t in enumerate(self.progress_bar(snake_case__				)				):
                     # expand the latents if we are doing classifier free guidance
                     lowercase		:str       							=				torch.cat([latents] * 2				) if do_classifier_free_guidance else latents

                     lowercase		:Any       							=				{'''image_embeds''': image_embeds}
                     lowercase		:List[str]       							=				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:
                            lowercase							,  lowercase		:Optional[Any]       							=				noise_pred.split(latents.shape[1]  ,     dim=1				)
                            lowercase							,  lowercase		:Union[str, Any]       							=				noise_pred.chunk(2				)
                            lowercase							,  lowercase		:Union[str, Any]       							=				variance_pred.chunk(2				)
                            lowercase		:List[str]       							=				noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                            lowercase		:Dict       							=				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"]
                     ):
                            lowercase							,  lowercase		:Optional[int]       							=				noise_pred.split(latents.shape[1]  ,     dim=1				)

                     # compute the previous noisy sample x_t -> x_t-1
                     lowercase		:List[str]       							=				self.scheduler.step(
                         snake_case__  ,     snake_case__  ,     snake_case__  ,     generator=snake_case__  ,     )[0]
              # post-processing
              lowercase		:str       							=				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"]:
                     lowercase		:Optional[Any]       							=				image * 0.5 + 0.5
                     lowercase		:int       							=				image.clamp(0  ,     1				)
                     lowercase		:int       							=				image.cpu().permute(0  ,     2  ,     3  ,     1				).float().numpy()

              if output_type == "pil":
                     lowercase		:Optional[Any]       							=				self.numpy_to_pil(snake_case__				)

              if not return_dict:
                     return (image,)

              return ImagePipelineOutput(images=snake_case__				)





"""simple docstring"""



def       lowerCamelCase					(a_						:int)     ->							str:
       if number > 0:
              raise ValueError('''input must be a negative integer''')
       lowercase		:Any       							=				len(bin(a_)[3:])
       lowercase		:Any       							=				bin(abs(a_) - (1 << binary_number_length))[3:]
       lowercase		:Tuple       							=				(
           (
               '''1'''
               + '''0''' * (binary_number_length - len(a_))
               + twos_complement_number
           )
           if number < 0
           else '''0'''
       )
       return "0b" + twos_complement_number


if __name__ == "__main__":
 import doctest

 doctest.testmod()

