




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


lowerCamelCase__					:  Union[str, Any]      							=				logging.get_logger(__name__)  # pylint: disable=invalid-name

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

def    UpperCAmelCase_				( __UpperCAmelCase		:      Optional[int]     ,     __UpperCAmelCase		:      int     ,     __UpperCAmelCase		:      Any=8				)   ->					Dict:
      SCREAMING_SNAKE_CASE_      		=     height // scale_factor**2
      if height % scale_factor**2 != 0:
            new_height += 1
      SCREAMING_SNAKE_CASE_      		=     width // scale_factor**2
      if width % scale_factor**2 != 0:
            new_width += 1
      return new_height * scale_factor, new_width * scale_factor





class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE   ):


 '''simple docstring'''







 def __init__(				self    :						Dict							,							_lowerCAmelCase    :						UNetaDConditionModel							,							_lowerCAmelCase    :						DDPMScheduler							,							_lowerCAmelCase    :						VQModel							,							):
       super().__init__()

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



 def  lowerCAmelCase_    (				self    :						Union[str, Any]							,							_lowerCAmelCase    :						str							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						Dict							,							_lowerCAmelCase    :						Optional[int]							,							_lowerCAmelCase    :						List[str]							,							_lowerCAmelCase    :						List[Any]		):
       if latents is None:
             SCREAMING_SNAKE_CASE_      		=     randn_tensor(_lowerCAmelCase							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase							,							dtype=_lowerCAmelCase		)
       else:
             if latents.shape != shape:
                   raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {shape}"		)
             SCREAMING_SNAKE_CASE_      		=     latents.to(_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_      		=     latents * scheduler.init_noise_sigma
       return latents



 def  lowerCAmelCase_    (				self    :						Optional[Any]							,							_lowerCAmelCase    :						str=0		):
       if is_accelerate_available():
             from accelerate import cpu_offload
       else:
             raise ImportError('Please install accelerate via `pip install accelerate`'		)

       SCREAMING_SNAKE_CASE_      		=     torch.device(F"cuda:{gpu_id}"		)

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



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Any=0		):
       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.'		)

       SCREAMING_SNAKE_CASE_      		=     torch.device(F"cuda:{gpu_id}"		)

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

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

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



 @property
 # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
 def  lowerCAmelCase_    (				self    :						int		):
       if not hasattr(self.unet							,							'_hf_hook'		):
             return self.device
       for module in self.unet.modules():
             if (
                 hasattr(_lowerCAmelCase							,							'_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(_lowerCAmelCase		)
 def __call__(				self    :						Any							,							_lowerCAmelCase    :						Union[torch.FloatTensor, List[torch.FloatTensor]]							,							_lowerCAmelCase    :						Union[torch.FloatTensor, List[torch.FloatTensor]]							,							_lowerCAmelCase    :						int = 512							,							_lowerCAmelCase    :						int = 512							,							_lowerCAmelCase    :						int = 100							,							_lowerCAmelCase    :						float = 4.0							,							_lowerCAmelCase    :						int = 1							,							_lowerCAmelCase    :						Optional[Union[torch.Generator, List[torch.Generator]]] = None							,							_lowerCAmelCase    :						Optional[torch.FloatTensor] = None							,							_lowerCAmelCase    :						Optional[str] = "pil"							,							_lowerCAmelCase    :						bool = True							,							):
       SCREAMING_SNAKE_CASE_      		=     self._execution_device

       SCREAMING_SNAKE_CASE_      		=     guidance_scale > 1.0

       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     torch.cat(_lowerCAmelCase							,							dim=0		)
       SCREAMING_SNAKE_CASE_      		=     image_embeds.shape[0] * num_images_per_prompt
       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     torch.cat(_lowerCAmelCase							,							dim=0		)

       if do_classifier_free_guidance:
             SCREAMING_SNAKE_CASE_      		=     image_embeds.repeat_interleave(_lowerCAmelCase							,							dim=0		)
             SCREAMING_SNAKE_CASE_      		=     negative_image_embeds.repeat_interleave(_lowerCAmelCase							,							dim=0		)

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

       self.scheduler.set_timesteps(_lowerCAmelCase							,							device=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     self.scheduler.timesteps

       SCREAMING_SNAKE_CASE_      		=     self.unet.config.in_channels

       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     downscale_height_and_width(_lowerCAmelCase							,							_lowerCAmelCase							,							self.movq_scale_factor		)

       # create initial latent
       SCREAMING_SNAKE_CASE_      		=     self.prepare_latents(
           (batch_size, num_channels_latents, height, width)							,							image_embeds.dtype							,							_lowerCAmelCase							,							_lowerCAmelCase							,							_lowerCAmelCase							,							self.scheduler							,							)

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

             SCREAMING_SNAKE_CASE_      		=     {'image_embeds': image_embeds}
             SCREAMING_SNAKE_CASE_      		=     self.unet(
                 sample=_lowerCAmelCase							,							timestep=_lowerCAmelCase							,							encoder_hidden_states=_lowerCAmelCase							,							added_cond_kwargs=_lowerCAmelCase							,							return_dict=_lowerCAmelCase							,							)[0]

             if do_classifier_free_guidance:
                   SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     noise_pred.split(latents.shape[1]							,							dim=1		)
                   SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     noise_pred.chunk(2		)
                   SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     variance_pred.chunk(2		)
                   SCREAMING_SNAKE_CASE_      		=     noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
                   SCREAMING_SNAKE_CASE_      		=     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"]
             ):
                   SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     noise_pred.split(latents.shape[1]							,							dim=1		)

             # compute the previous noisy sample x_t -> x_t-1
             SCREAMING_SNAKE_CASE_      		=     self.scheduler.step(
                 _lowerCAmelCase							,							_lowerCAmelCase							,							_lowerCAmelCase							,							generator=_lowerCAmelCase							,							)[0]
       # post-processing
       SCREAMING_SNAKE_CASE_      		=     self.movq.decode(_lowerCAmelCase							,							force_not_quantize=_lowerCAmelCase		)['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"]:
             SCREAMING_SNAKE_CASE_      		=     image * 0.5 + 0.5
             SCREAMING_SNAKE_CASE_      		=     image.clamp(0							,							1		)
             SCREAMING_SNAKE_CASE_      		=     image.cpu().permute(0							,							2							,							3							,							1		).float().numpy()

       if output_type == "pil":
             SCREAMING_SNAKE_CASE_      		=     self.numpy_to_pil(_lowerCAmelCase		)

       if not return_dict:
             return (image,)

       return ImagePipelineOutput(images=_lowerCAmelCase		)






'''simple docstring'''

import argparse
import glob
import importlib.util
import os
import re

import black
from doc_builder.style_doc import style_docstrings_in_code


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_copies.py
__lowerCAmelCase				:      Optional[Any]						      ="src/diffusers"
__lowerCAmelCase				:      Dict						      ="."


# This is to make sure the diffusers module imported is the one in the repo.
__lowerCAmelCase				:      int						      =importlib.util.spec_from_file_location(
    "diffusers",
    os.path.join(DIFFUSERS_PATH, "__init__.py"),
    submodule_search_locations=[DIFFUSERS_PATH],
)
__lowerCAmelCase				:      Dict						      =spec.loader.load_module()





def 		UpperCamelCase							(				_lowerCamelCase   :   int		,			_lowerCamelCase   :   Optional[Any]			):
    return line.startswith(_lowerCamelCase			) or len(_lowerCamelCase			) <= 1 or re.search(r"^\s*\)(\s*->.*:|:)\s*$"		,			_lowerCamelCase			) is not None





def 		UpperCamelCase							(				_lowerCamelCase   :   str			):
    A__							=     object_name.split("."			)
    A__							=     0

    # First let's find the module where our object lives.
    A__							=     parts[i]
    while i < len(_lowerCamelCase			) and not os.path.isfile(os.path.join(_lowerCamelCase		,			F"{module}.py"			)			):
        i += 1
        if i < len(_lowerCamelCase			):
            A__							=     os.path.join(_lowerCamelCase		,			parts[i]			)
    if i >= len(_lowerCamelCase			):
        raise ValueError(F"`object_name` should begin with the name of a module of diffusers but got {object_name}."			)

    with open(os.path.join(_lowerCamelCase		,			F"{module}.py"			)		,			"r"		,			encoding="utf-8"		,			newline="\n"			) as f:
        A__							=     f.readlines()

    # Now let's find the class / func in the code!
    A__							=     ""
    A__							=     0
    for name in parts[i + 1 :]:
        while (
            line_index < len(_lowerCamelCase			) and re.search(rF"^{indent}(class|def)\s+{name}(\(|\:)"		,			lines[line_index]			) is None
        ):
            line_index += 1
        indent += "    "
        line_index += 1

    if line_index >= len(_lowerCamelCase			):
        raise ValueError(F" {object_name} does not match any function or class in {module}."			)

    # We found the beginning of the class / func, now let's find the end (when the indent diminishes).
    A__							=     line_index
    while line_index < len(_lowerCamelCase			) and _should_continue(lines[line_index]		,			_lowerCamelCase			):
        line_index += 1
    # Clean up empty lines at the end (if any).
    while len(lines[line_index - 1]			) <= 1:
        line_index -= 1

    A__							=     lines[start_index:line_index]
    return "".join(_lowerCamelCase			)


__lowerCAmelCase				:      Any						      =re.compile(R"^(\s*)#\s*Copied from\s+diffusers\.(\S+\.\S+)\s*($|\S.*$)")
__lowerCAmelCase				:      List[Any]						      =re.compile(R"^\s*(\S+)->(\S+)(\s+.*|$)")
__lowerCAmelCase				:      Any						      =re.compile(R"<FILL\s+[^>]*>")





def 		UpperCamelCase							(				_lowerCamelCase   :   List[Any]			):
    A__							=     code.split("\n"			)
    A__							=     0
    while idx < len(_lowerCamelCase			) and len(lines[idx]			) == 0:
        idx += 1
    if idx < len(_lowerCamelCase			):
        return re.search(r"^(\s*)\S"		,			lines[idx]			).groups()[0]
    return ""





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple			):
    A__							=     len(get_indent(_lowerCamelCase			)			) > 0
    if has_indent:
        A__							=     F"class Bla:\n{code}"
    A__							=     black.Mode(target_versions={black.TargetVersion.PYaa}		,			line_length=1_19		,			preview=_lowerCamelCase			)
    A__							=     black.format_str(_lowerCamelCase		,			mode=_lowerCamelCase			)
    A__,      A__							=     style_docstrings_in_code(_lowerCamelCase			)
    return result[len("class Bla:\n"			) :] if has_indent else result





def 		UpperCamelCase							(				_lowerCamelCase   :   Tuple		,			_lowerCamelCase   :   List[str]=False			):
    with open(_lowerCamelCase		,			"r"		,			encoding="utf-8"		,			newline="\n"			) as f:
        A__							=     f.readlines()
    A__							=     []
    A__							=     0
    # Not a for loop cause `lines` is going to change (if `overwrite=True`).
    while line_index < len(_lowerCamelCase			):
        A__							=     _re_copy_warning.search(lines[line_index]			)
        if search is None:
            line_index += 1
            continue

        # There is some copied code here, let's retrieve the original.
        A__,      A__,      A__							=     search.groups()
        A__							=     find_code_in_diffusers(_lowerCamelCase			)
        A__							=     get_indent(_lowerCamelCase			)

        A__							=     line_index + 1 if indent == theoretical_indent else line_index + 2
        A__							=     theoretical_indent
        A__							=     start_index

        # Loop to check the observed code, stop when indentation diminishes or if we see a End copy comment.
        A__							=     True
        while line_index < len(_lowerCamelCase			) and should_continue:
            line_index += 1
            if line_index >= len(_lowerCamelCase			):
                break
            A__							=     lines[line_index]
            A__							=     _should_continue(_lowerCamelCase		,			_lowerCamelCase			) and re.search(F"^{indent}# End copy"		,			_lowerCamelCase			) is None
        # Clean up empty lines at the end (if any).
        while len(lines[line_index - 1]			) <= 1:
            line_index -= 1

        A__							=     lines[start_index:line_index]
        A__							=     "".join(_lowerCamelCase			)

        # Remove any nested `Copied from` comments to avoid circular copies
        A__							=     [line for line in theoretical_code.split("\n"			) if _re_copy_warning.search(_lowerCamelCase			) is None]
        A__							=     "\n".join(_lowerCamelCase			)

        # Before comparing, use the `replace_pattern` on the original code.
        if len(_lowerCamelCase			) > 0:
            A__							=     replace_pattern.replace("with"		,			""			).split(","			)
            A__							=     [_re_replace_pattern.search(_lowerCamelCase			) for p in patterns]
            for pattern in patterns:
                if pattern is None:
                    continue
                A__,      A__,      A__							=     pattern.groups()
                A__							=     re.sub(_lowerCamelCase		,			_lowerCamelCase		,			_lowerCamelCase			)
                if option.strip() == "all-casing":
                    A__							=     re.sub(obja.lower()		,			obja.lower()		,			_lowerCamelCase			)
                    A__							=     re.sub(obja.upper()		,			obja.upper()		,			_lowerCamelCase			)

            # Blackify after replacement. To be able to do that, we need the header (class or function definition)
            # from the previous line
            A__							=     blackify(lines[start_index - 1] + theoretical_code			)
            A__							=     theoretical_code[len(lines[start_index - 1]			) :]

        # Test for a diff and act accordingly.
        if observed_code != theoretical_code:
            diffs.append([object_name, start_index]			)
            if overwrite:
                A__							=     lines[:start_index] + [theoretical_code] + lines[line_index:]
                A__							=     start_index + 1

    if overwrite and len(_lowerCamelCase			) > 0:
        # Warn the user a file has been modified.
        print(F"Detected changes, rewriting {filename}."			)
        with open(_lowerCamelCase		,			"w"		,			encoding="utf-8"		,			newline="\n"			) as f:
            f.writelines(_lowerCamelCase			)
    return diffs





def 		UpperCamelCase							(				_lowerCamelCase   :   bool = False			):
    A__							=     glob.glob(os.path.join(_lowerCamelCase		,			"**/*.py"			)		,			recursive=_lowerCamelCase			)
    A__							=     []
    for filename in all_files:
        A__							=     is_copy_consistent(_lowerCamelCase		,			_lowerCamelCase			)
        diffs += [F"- {filename}: copy does not match {d[0]} at line {d[1]}" for d in new_diffs]
    if not overwrite and len(_lowerCamelCase			) > 0:
        A__							=     "\n".join(_lowerCamelCase			)
        raise Exception(
            "Found the following copy inconsistencies:\n"
            + diff
            + "\nRun `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them."			)


if __name__ == "__main__":
  __lowerCAmelCase				:      Union[str, Any]						      =argparse.ArgumentParser()
  parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
  __lowerCAmelCase				:      int						      =parser.parse_args()

  check_copies(args.fix_and_overwrite)



