



"""simple docstring"""
import inspect
from typing import Optional, Union

import numpy as np
import PIL
import torch
from torch.nn import functional as F
from torchvision import transforms
from transformers import CLIPFeatureExtractor, CLIPModel, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    DPMSolverMultistepScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.utils import (
    PIL_INTERPOLATION,
    randn_tensor,
)





def  lowerCamelCase_						(UpperCamelCase__		:		Optional[int]     ,       UpperCamelCase__		:		Optional[Any]     ,       UpperCamelCase__		:		Dict							):
    if isinstance(UpperCamelCase__     ,       torch.Tensor							):
        return image
    elif isinstance(UpperCamelCase__     ,       PIL.Image.Image							):
        _UpperCAmelCase							:  List[str]											=    [image]

    if isinstance(image[0]     ,       PIL.Image.Image							):
        _UpperCAmelCase							:  Union[str, Any]											=    [np.array(i.resize((w, h)     ,       resample=PIL_INTERPOLATION['''lanczos''']							)							)[None, :] for i in image]
        _UpperCAmelCase							:  Optional[int]											=    np.concatenate(UpperCamelCase__     ,       axis=0							)
        _UpperCAmelCase							:  Dict											=    np.array(UpperCamelCase__							).astype(np.floataa							) / 255.0
        _UpperCAmelCase							:  Any											=    image.transpose(0     ,       3     ,       1     ,       2							)
        _UpperCAmelCase							:  Any											=    2.0 * image - 1.0
        _UpperCAmelCase							:  Dict											=    torch.from_numpy(UpperCamelCase__							)
    elif isinstance(image[0]     ,       torch.Tensor							):
        _UpperCAmelCase							:  Dict											=    torch.cat(UpperCamelCase__     ,       dim=0							)
    return image





def  lowerCamelCase_						(UpperCamelCase__		:		Dict     ,       UpperCamelCase__		:		Dict     ,       UpperCamelCase__		:		Optional[Any]     ,       UpperCamelCase__		:		Optional[int]=0.9995							):
    if not isinstance(UpperCamelCase__     ,       np.ndarray							):
        _UpperCAmelCase							:  List[str]											=    True
        _UpperCAmelCase							:  Optional[int]											=    va.device
        _UpperCAmelCase							:  str											=    va.cpu().numpy()
        _UpperCAmelCase							:  Any											=    va.cpu().numpy()

    _UpperCAmelCase							:  Union[str, Any]											=    np.sum(va * va / (np.linalg.norm(UpperCamelCase__							) * np.linalg.norm(UpperCamelCase__							))							)
    if np.abs(UpperCamelCase__							) > DOT_THRESHOLD:
        _UpperCAmelCase							:  Optional[int]											=    (1 - t) * va + t * va
    else:
        _UpperCAmelCase							:  Union[str, Any]											=    np.arccos(UpperCamelCase__							)
        _UpperCAmelCase							:  int											=    np.sin(UpperCamelCase__							)
        _UpperCAmelCase							:  Tuple											=    theta_a * t
        _UpperCAmelCase							:  Optional[int]											=    np.sin(UpperCamelCase__							)
        _UpperCAmelCase							:  int											=    np.sin(theta_a - theta_t							) / sin_theta_a
        _UpperCAmelCase							:  Optional[Any]											=    sin_theta_t / sin_theta_a
        _UpperCAmelCase							:  List[Any]											=    sa * va + sa * va

    if inputs_are_torch:
        _UpperCAmelCase							:  Tuple											=    torch.from_numpy(UpperCamelCase__							).to(UpperCamelCase__							)

    return va





def  lowerCamelCase_						(UpperCamelCase__		:		Tuple     ,       UpperCamelCase__		:		int							):
    _UpperCAmelCase							:  Any											=    F.normalize(UpperCamelCase__     ,       dim=-1							)
    _UpperCAmelCase							:  List[Any]											=    F.normalize(UpperCamelCase__     ,       dim=-1							)
    return (x - y).norm(dim=-1							).div(2							).arcsin().pow(2							).mul(2							)





def  lowerCamelCase_						(UpperCamelCase__		:		Optional[Any]     ,       UpperCamelCase__		:		Union[str, Any]							):
    for param in model.parameters():
        _UpperCAmelCase							:  Optional[Any]											=    value


class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     def __init__(					self       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A=None       ,	A=None       ,	A=None       ,	)							->					Any:
         super().__init__()
         self.register_modules(
             vae=A       ,	text_encoder=A       ,	clip_model=A       ,	tokenizer=A       ,	unet=A       ,	scheduler=A       ,	feature_extractor=A       ,	coca_model=A       ,	coca_tokenizer=A       ,	coca_transform=A       ,	)
         _UpperCAmelCase							:  int											=    (
             feature_extractor.size
             if isinstance(feature_extractor.size       ,	A		)
             else feature_extractor.size['''shortest_edge''']
         )
         _UpperCAmelCase							:  Union[str, Any]											=    transforms.Normalize(mean=feature_extractor.image_mean       ,	std=feature_extractor.image_std		)
         set_requires_grad(self.text_encoder       ,	A		)
         set_requires_grad(self.clip_model       ,	A		)





     def  __lowerCAmelCase	(					self       ,	A = "auto"		)							->					Tuple:
         if slice_size == "auto":
             # half the attention head size is usually a good trade-off between
             # speed and memory
             _UpperCAmelCase							:  str											=    self.unet.config.attention_head_dim // 2
         self.unet.set_attention_slice(A		)





     def  __lowerCAmelCase	(					self		)							->					Optional[int]:
         self.enable_attention_slicing(A		)





     def  __lowerCAmelCase	(					self		)							->					int:
         set_requires_grad(self.vae       ,	A		)





     def  __lowerCAmelCase	(					self		)							->					Tuple:
         set_requires_grad(self.vae       ,	A		)





     def  __lowerCAmelCase	(					self		)							->					str:
         set_requires_grad(self.unet       ,	A		)





     def  __lowerCAmelCase	(					self		)							->					int:
         set_requires_grad(self.unet       ,	A		)





     def  __lowerCAmelCase	(					self       ,	A       ,	A       ,	A		)							->					Optional[Any]:
         # get the original timestep using init_timestep
         _UpperCAmelCase							:  Any											=    min(int(num_inference_steps * strength		)       ,	A		)

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

         return timesteps, num_inference_steps - t_start





     def  __lowerCAmelCase	(					self       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A=None		)							->					str:
         if not isinstance(A       ,	torch.Tensor		):
             raise ValueError(f'`image` has to be of type `torch.Tensor` but is {type(A		)}'		)

         _UpperCAmelCase							:  Optional[int]											=    image.to(device=A       ,	dtype=A		)

         if isinstance(A       ,	A		):
             _UpperCAmelCase							:  Union[str, Any]											=    [
                 self.vae.encode(image[i : i + 1]		).latent_dist.sample(generator[i]		) for i in range(A		)
             ]
             _UpperCAmelCase							:  str											=    torch.cat(A       ,	dim=0		)
         else:
             _UpperCAmelCase							:  int											=    self.vae.encode(A		).latent_dist.sample(A		)

         # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
         _UpperCAmelCase							:  Optional[Any]											=    0.18_215 * init_latents
         _UpperCAmelCase							:  Any											=    init_latents.repeat_interleave(A       ,	dim=0		)

         _UpperCAmelCase							:  int											=    randn_tensor(init_latents.shape       ,	generator=A       ,	device=A       ,	dtype=A		)

         # get latents
         _UpperCAmelCase							:  Dict											=    self.scheduler.add_noise(A       ,	A       ,	A		)
         _UpperCAmelCase							:  int											=    init_latents

         return latents





     def  __lowerCAmelCase	(					self       ,	A		)							->					str:
         _UpperCAmelCase							:  List[str]											=    self.coca_transform(A		).unsqueeze(0		)
         with torch.no_grad(), torch.cuda.amp.autocast():
             _UpperCAmelCase							:  Optional[Any]											=    self.coca_model.generate(transformed_image.to(device=self.device       ,	dtype=self.coca_model.dtype		)		)
         _UpperCAmelCase							:  Union[str, Any]											=    self.coca_tokenizer.decode(generated[0].cpu().numpy()		)
         return generated.split('''<end_of_text>'''		)[0].replace('''<start_of_text>'''       ,	''''''		).rstrip(''' .,'''		)





     def  __lowerCAmelCase	(					self       ,	A       ,	A		)							->					Optional[Any]:
         _UpperCAmelCase							:  Optional[Any]											=    self.feature_extractor.preprocess(A		)
         _UpperCAmelCase							:  Union[str, Any]											=    torch.from_numpy(clip_image_input['''pixel_values'''][0]		).unsqueeze(0		).to(self.device		).half()
         _UpperCAmelCase							:  Optional[Any]											=    self.clip_model.get_image_features(A		)
         _UpperCAmelCase							:  Union[str, Any]											=    image_embeddings_clip / image_embeddings_clip.norm(p=2       ,	dim=-1       ,	keepdim=A		)
         _UpperCAmelCase							:  str											=    image_embeddings_clip.repeat_interleave(A       ,	dim=0		)
         return image_embeddings_clip





     @torch.enable_grad()
     def  __lowerCAmelCase	(					self       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	)							->					Tuple:
         _UpperCAmelCase							:  Optional[Any]											=    latents.detach().requires_grad_()

         _UpperCAmelCase							:  str											=    self.scheduler.scale_model_input(A       ,	A		)

         # predict the noise residual
         _UpperCAmelCase							:  List[Any]											=    self.unet(A       ,	A       ,	encoder_hidden_states=A		).sample

         if isinstance(self.scheduler       ,	(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler)		):
             _UpperCAmelCase							:  List[str]											=    self.scheduler.alphas_cumprod[timestep]
             _UpperCAmelCase							:  Union[str, Any]											=    1 - alpha_prod_t
             # compute predicted original sample from predicted noise also called
             # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
             _UpperCAmelCase							:  Optional[int]											=    (latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5

             _UpperCAmelCase							:  Optional[Any]											=    torch.sqrt(A		)
             _UpperCAmelCase							:  Any											=    pred_original_sample * (fac) + latents * (1 - fac)
         elif isinstance(self.scheduler       ,	A		):
             _UpperCAmelCase							:  List[str]											=    self.scheduler.sigmas[index]
             _UpperCAmelCase							:  Optional[int]											=    latents - sigma * noise_pred
         else:
             raise ValueError(f'scheduler type {type(self.scheduler		)} not supported'		)

         # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
         _UpperCAmelCase							:  Optional[int]											=    1 / 0.18_215 * sample
         _UpperCAmelCase							:  int											=    self.vae.decode(A		).sample
         _UpperCAmelCase							:  List[Any]											=    (image / 2 + 0.5).clamp(0       ,	1		)

         _UpperCAmelCase							:  Union[str, Any]											=    transforms.Resize(self.feature_extractor_size		)(A		)
         _UpperCAmelCase							:  Union[str, Any]											=    self.normalize(A		).to(latents.dtype		)

         _UpperCAmelCase							:  Dict											=    self.clip_model.get_image_features(A		)
         _UpperCAmelCase							:  Dict											=    image_embeddings_clip / image_embeddings_clip.norm(p=2       ,	dim=-1       ,	keepdim=A		)

         _UpperCAmelCase							:  Union[str, Any]											=    spherical_dist_loss(A       ,	A		).mean() * clip_guidance_scale

         _UpperCAmelCase							:  Dict											=    -torch.autograd.grad(A       ,	A		)[0]

         if isinstance(self.scheduler       ,	A		):
             _UpperCAmelCase							:  Dict											=    latents.detach() + grads * (sigma**2)
             _UpperCAmelCase							:  Tuple											=    noise_pred_original
         else:
             _UpperCAmelCase							:  List[Any]											=    noise_pred_original - torch.sqrt(A		) * grads
         return noise_pred, latents





     @torch.no_grad()
     def __call__(					self       ,	A       ,	A       ,	A = None       ,	A = None       ,	A = 5_1_2       ,	A = 5_1_2       ,	A = 0.6       ,	A = 5_0       ,	A = 7.5       ,	A = 1       ,	A = 0.0       ,	A = 1_0_0       ,	A = None       ,	A = "pil"       ,	A = True       ,	A = 0.8       ,	A = 0.1       ,	A = 0.1       ,	)							->					Dict:
         if isinstance(A       ,	A		) and len(A		) != batch_size:
             raise ValueError(f'You have passed {batch_size} batch_size, but only {len(A		)} generators.'		)

         if height % 8 != 0 or width % 8 != 0:
             raise ValueError(f'`height` and `width` have to be divisible by 8 but are {height} and {width}.'		)

         if isinstance(A       ,	torch.Generator		) and batch_size > 1:
             _UpperCAmelCase							:  Any											=    [generator] + [None] * (batch_size - 1)

         _UpperCAmelCase							:  Tuple											=    [
             ('''model''', self.coca_model is None),
             ('''tokenizer''', self.coca_tokenizer is None),
             ('''transform''', self.coca_transform is None),
         ]
         _UpperCAmelCase							:  Dict											=    [x[0] for x in coca_is_none if x[1]]
         _UpperCAmelCase							:  Tuple											=    ''', '''.join(A		)
         # generate prompts with coca model if prompt is None
         if content_prompt is None:
             if len(A		):
                 raise ValueError(
                     f'Content prompt is None and CoCa [{coca_is_none_str}] is None.'
                     f'Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.'		)
             _UpperCAmelCase							:  str											=    self.get_image_description(A		)
         if style_prompt is None:
             if len(A		):
                 raise ValueError(
                     f'Style prompt is None and CoCa [{coca_is_none_str}] is None.'
                     f' Set prompt or pass Coca [{coca_is_none_str}] to DiffusionPipeline.'		)
             _UpperCAmelCase							:  Any											=    self.get_image_description(A		)

         # get prompt text embeddings for content and style
         _UpperCAmelCase							:  Any											=    self.tokenizer(
             A       ,	padding='''max_length'''       ,	max_length=self.tokenizer.model_max_length       ,	truncation=A       ,	return_tensors='''pt'''       ,	)
         _UpperCAmelCase							:  List[Any]											=    self.text_encoder(content_text_input.input_ids.to(self.device		)		)[0]

         _UpperCAmelCase							:  Optional[Any]											=    self.tokenizer(
             A       ,	padding='''max_length'''       ,	max_length=self.tokenizer.model_max_length       ,	truncation=A       ,	return_tensors='''pt'''       ,	)
         _UpperCAmelCase							:  Tuple											=    self.text_encoder(style_text_input.input_ids.to(self.device		)		)[0]

         _UpperCAmelCase							:  int											=    slerp(A       ,	A       ,	A		)

         # duplicate text embeddings for each generation per prompt
         _UpperCAmelCase							:  Any											=    text_embeddings.repeat_interleave(A       ,	dim=0		)

         # set timesteps
         _UpperCAmelCase							:  Any											=    '''offset''' in set(inspect.signature(self.scheduler.set_timesteps		).parameters.keys()		)
         _UpperCAmelCase							:  List[Any]											=    {}
         if accepts_offset:
             _UpperCAmelCase							:  Tuple											=    1

         self.scheduler.set_timesteps(A       ,	**A		)
         # Some schedulers like PNDM have timesteps as arrays
         # It's more optimized to move all timesteps to correct device beforehand
         self.scheduler.timesteps.to(self.device		)

         _UpperCAmelCase							,				_UpperCAmelCase							:  Any											=    self.get_timesteps(A       ,	A       ,	self.device		)
         _UpperCAmelCase							:  Optional[Any]											=    timesteps[:1].repeat(A		)

         # Preprocess image
         _UpperCAmelCase							:  Union[str, Any]											=    preprocess(A       ,	A       ,	A		)
         _UpperCAmelCase							:  Dict											=    self.prepare_latents(
             A       ,	A       ,	A       ,	text_embeddings.dtype       ,	self.device       ,	A		)

         _UpperCAmelCase							:  Tuple											=    preprocess(A       ,	A       ,	A		)
         _UpperCAmelCase							:  List[str]											=    self.prepare_latents(
             A       ,	A       ,	A       ,	text_embeddings.dtype       ,	self.device       ,	A		)

         _UpperCAmelCase							:  Optional[int]											=    slerp(A       ,	A       ,	A		)

         if clip_guidance_scale > 0:
             _UpperCAmelCase							:  Dict											=    self.get_clip_image_embeddings(A       ,	A		)
             _UpperCAmelCase							:  List[Any]											=    self.get_clip_image_embeddings(A       ,	A		)
             _UpperCAmelCase							:  Dict											=    slerp(
                 A       ,	A       ,	A		)

         # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
         # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
         # corresponds to doing no classifier free guidance.
         _UpperCAmelCase							:  Any											=    guidance_scale > 1.0
         # get unconditional embeddings for classifier free guidance
         if do_classifier_free_guidance:
             _UpperCAmelCase							:  Dict											=    content_text_input.input_ids.shape[-1]
             _UpperCAmelCase							:  Tuple											=    self.tokenizer(['''''']       ,	padding='''max_length'''       ,	max_length=A       ,	return_tensors='''pt'''		)
             _UpperCAmelCase							:  int											=    self.text_encoder(uncond_input.input_ids.to(self.device		)		)[0]
             # duplicate unconditional embeddings for each generation per prompt
             _UpperCAmelCase							:  Optional[Any]											=    uncond_embeddings.repeat_interleave(A       ,	dim=0		)

             # For classifier free guidance, we need to do two forward passes.
             # Here we concatenate the unconditional and text embeddings into a single batch
             # to avoid doing two forward passes
             _UpperCAmelCase							:  str											=    torch.cat([uncond_embeddings, text_embeddings]		)

         # get the initial random noise unless the user supplied it

         # Unlike in other pipelines, latents need to be generated in the target device
         # for 1-to-1 results reproducibility with the CompVis implementation.
         # However this currently doesn't work in `mps`.
         _UpperCAmelCase							:  Union[str, Any]											=    (batch_size, self.unet.config.in_channels, height // 8, width // 8)
         _UpperCAmelCase							:  List[Any]											=    text_embeddings.dtype
         if latents is None:
             if self.device.type == "mps":
                 # randn does not work reproducibly on mps
                 _UpperCAmelCase							:  int											=    torch.randn(A       ,	generator=A       ,	device='''cpu'''       ,	dtype=A		).to(
                     self.device		)
             else:
                 _UpperCAmelCase							:  Optional[Any]											=    torch.randn(A       ,	generator=A       ,	device=self.device       ,	dtype=A		)
         else:
             if latents.shape != latents_shape:
                 raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {latents_shape}'		)
             _UpperCAmelCase							:  int											=    latents.to(self.device		)

         # scale the initial noise by the standard deviation required by the scheduler
         _UpperCAmelCase							:  str											=    latents * self.scheduler.init_noise_sigma

         # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
         # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
         # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
         # and should be between [0, 1]
         _UpperCAmelCase							:  Union[str, Any]											=    '''eta''' in set(inspect.signature(self.scheduler.step		).parameters.keys()		)
         _UpperCAmelCase							:  Optional[Any]											=    {}
         if accepts_eta:
             _UpperCAmelCase							:  Union[str, Any]											=    eta

         # check if the scheduler accepts generator
         _UpperCAmelCase							:  Any											=    '''generator''' in set(inspect.signature(self.scheduler.step		).parameters.keys()		)
         if accepts_generator:
             _UpperCAmelCase							:  Optional[int]											=    generator

         with self.progress_bar(total=A		):
             for i, t in enumerate(A		):
                 # expand the latents if we are doing classifier free guidance
                 _UpperCAmelCase							:  Optional[Any]											=    torch.cat([latents] * 2		) if do_classifier_free_guidance else latents
                 _UpperCAmelCase							:  str											=    self.scheduler.scale_model_input(A       ,	A		)

                 # predict the noise residual
                 _UpperCAmelCase							:  Tuple											=    self.unet(A       ,	A       ,	encoder_hidden_states=A		).sample

                 # perform classifier free guidance
                 if do_classifier_free_guidance:
                     _UpperCAmelCase							,				_UpperCAmelCase							:  int											=    noise_pred.chunk(2		)
                     _UpperCAmelCase							:  Optional[Any]											=    noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

                 # perform clip guidance
                 if clip_guidance_scale > 0:
                     _UpperCAmelCase							:  int											=    (
                         text_embeddings.chunk(2		)[1] if do_classifier_free_guidance else text_embeddings
                     )
                     _UpperCAmelCase							,				_UpperCAmelCase							:  str											=    self.cond_fn(
                         A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	A       ,	)

                 # compute the previous noisy sample x_t -> x_t-1
                 _UpperCAmelCase							:  int											=    self.scheduler.step(A       ,	A       ,	A       ,	**A		).prev_sample

        # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
         _UpperCAmelCase							:  Any											=    1 / 0.18_215 * latents
         _UpperCAmelCase							:  Any											=    self.vae.decode(A		).sample

         _UpperCAmelCase							:  Optional[Any]											=    (image / 2 + 0.5).clamp(0       ,	1		)
         _UpperCAmelCase							:  List[str]											=    image.cpu().permute(0       ,	2       ,	3       ,	1		).numpy()

         if output_type == "pil":
             _UpperCAmelCase							:  Tuple											=    self.numpy_to_pil(A		)

         if not return_dict:
             return (image, None)

         return StableDiffusionPipelineOutput(images=A       ,	nsfw_content_detected=A		)







"""simple docstring"""
def  lowerCamelCase_						(UpperCamelCase__		:		int     ,       UpperCamelCase__		:		int							):
    if a < 0 or b < 0:
        raise ValueError('''the value of both inputs must be positive'''							)
    _UpperCAmelCase							:  List[str]											=    str(bin(UpperCamelCase__							)							)[2:]  # remove the leading "0b"
    _UpperCAmelCase							:  str											=    str(bin(UpperCamelCase__							)							)[2:]
    _UpperCAmelCase							:  List[str]											=    max(len(UpperCamelCase__							)     ,       len(UpperCamelCase__							)							)
    return "0b" + "".join(
        str(int('''1''' in (char_a, char_b)							)							)
        for char_a, char_b in zip(a_binary.zfill(UpperCamelCase__							)     ,       b_binary.zfill(UpperCamelCase__							)							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()


