






'''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     A__      (					__lowerCAmelCase		:      int ,     __lowerCAmelCase		:      List[Any] ,     __lowerCAmelCase		:      Optional[Any]    ):
						if isinstance(__lowerCAmelCase ,     torch.Tensor    ):
												return image
						elif isinstance(__lowerCAmelCase ,     PIL.Image.Image    ):
												lowerCamelCase__								=	[image]

						if isinstance(image[0] ,     PIL.Image.Image    ):
												lowerCamelCase__								=	[np.array(i.resize((w, h) ,     resample=PIL_INTERPOLATION["""lanczos"""]    )    )[None, :] for i in image]
												lowerCamelCase__								=	np.concatenate(__lowerCAmelCase ,     axis=0    )
												lowerCamelCase__								=	np.array(__lowerCAmelCase    ).astype(np.floataa    ) / 255.0
												lowerCamelCase__								=	image.transpose(0 ,     3 ,     1 ,     2    )
												lowerCamelCase__								=	2.0 * image - 1.0
												lowerCamelCase__								=	torch.from_numpy(__lowerCAmelCase    )
						elif isinstance(image[0] ,     torch.Tensor    ):
												lowerCamelCase__								=	torch.cat(__lowerCAmelCase ,     dim=0    )
						return image
def     A__      (					__lowerCAmelCase		:      List[str] ,     __lowerCAmelCase		:      Optional[Any] ,     __lowerCAmelCase		:      int ,     __lowerCAmelCase		:      Any=0.9995    ):
						if not isinstance(__lowerCAmelCase ,     np.ndarray    ):
												lowerCamelCase__								=	True
												lowerCamelCase__								=	va.device
												lowerCamelCase__								=	va.cpu().numpy()
												lowerCamelCase__								=	va.cpu().numpy()

						lowerCamelCase__								=	np.sum(va * va / (np.linalg.norm(__lowerCAmelCase    ) * np.linalg.norm(__lowerCAmelCase    ))    )
						if np.abs(__lowerCAmelCase    ) > DOT_THRESHOLD:
												lowerCamelCase__								=	(1 - t) * va + t * va
						else:
												lowerCamelCase__								=	np.arccos(__lowerCAmelCase    )
												lowerCamelCase__								=	np.sin(__lowerCAmelCase    )
												lowerCamelCase__								=	theta_a * t
												lowerCamelCase__								=	np.sin(__lowerCAmelCase    )
												lowerCamelCase__								=	np.sin(theta_a - theta_t    ) / sin_theta_a
												lowerCamelCase__								=	sin_theta_t / sin_theta_a
												lowerCamelCase__								=	sa * va + sa * va

						if inputs_are_torch:
												lowerCamelCase__								=	torch.from_numpy(__lowerCAmelCase    ).to(__lowerCAmelCase    )

						return va
def     A__      (					__lowerCAmelCase		:      Union[str, Any] ,     __lowerCAmelCase		:      List[Any]    ):
						lowerCamelCase__								=	F.normalize(__lowerCAmelCase ,     dim=-1    )
						lowerCamelCase__								=	F.normalize(__lowerCAmelCase ,     dim=-1    )
						return (x - y).norm(dim=-1    ).div(2    ).arcsin().pow(2    ).mul(2    )
def     A__      (					__lowerCAmelCase		:      Union[str, Any] ,     __lowerCAmelCase		:      str    ):
						for param in model.parameters():
												lowerCamelCase__								=	value





class 		UpperCamelCase__     (a      ):




		'''simple docstring'''





		def __init__(   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase=None ,_lowerCAmelCase=None ,_lowerCAmelCase=None ,):
								super().__init__()
								self.register_modules(
								    vae=_lowerCAmelCase ,text_encoder=_lowerCAmelCase ,clip_model=_lowerCAmelCase ,tokenizer=_lowerCAmelCase ,unet=_lowerCAmelCase ,scheduler=_lowerCAmelCase ,feature_extractor=_lowerCAmelCase ,coca_model=_lowerCAmelCase ,coca_tokenizer=_lowerCAmelCase ,coca_transform=_lowerCAmelCase ,)
								lowerCamelCase__								=	(
								    feature_extractor.size
								    if isinstance(feature_extractor.size ,_lowerCAmelCase							)
								    else feature_extractor.size["""shortest_edge"""]
								)
								lowerCamelCase__								=	transforms.Normalize(mean=feature_extractor.image_mean ,std=feature_extractor.image_std							)
								set_requires_grad(self.text_encoder ,_lowerCAmelCase							)
								set_requires_grad(self.clip_model ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase = "auto"							):
								if slice_size == "auto":
														# half the attention head size is usually a good trade-off between
														# speed and memory
														lowerCamelCase__								=	self.unet.config.attention_head_dim // 2
								self.unet.set_attention_slice(_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								self.enable_attention_slicing(_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								set_requires_grad(self.vae ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								set_requires_grad(self.vae ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								set_requires_grad(self.unet ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self							):
								set_requires_grad(self.unet ,_lowerCAmelCase							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							):
								# get the original timestep using init_timestep
								lowerCamelCase__								=	min(int(num_inference_steps * strength							) ,_lowerCAmelCase							)

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

								return timesteps, num_inference_steps - t_start






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase=None							):
								if not isinstance(_lowerCAmelCase ,torch.Tensor							):
														raise ValueError(F'''`image` has to be of type `torch.Tensor` but is {type(_lowerCAmelCase							)}'''							)

								lowerCamelCase__								=	image.to(device=_lowerCAmelCase ,dtype=_lowerCAmelCase							)

								if isinstance(_lowerCAmelCase ,_lowerCAmelCase							):
														lowerCamelCase__								=	[
														    self.vae.encode(image[i : i + 1]							).latent_dist.sample(generator[i]							) for i in range(_lowerCAmelCase							)
														]
														lowerCamelCase__								=	torch.cat(_lowerCAmelCase ,dim=0							)
								else:
														lowerCamelCase__								=	self.vae.encode(_lowerCAmelCase							).latent_dist.sample(_lowerCAmelCase							)

								# Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
								lowerCamelCase__								=	0.1_8215 * init_latents
								lowerCamelCase__								=	init_latents.repeat_interleave(_lowerCAmelCase ,dim=0							)

								lowerCamelCase__								=	randn_tensor(init_latents.shape ,generator=_lowerCAmelCase ,device=_lowerCAmelCase ,dtype=_lowerCAmelCase							)

								# get latents
								lowerCamelCase__								=	self.scheduler.add_noise(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)
								lowerCamelCase__								=	init_latents

								return latents






		def       UpperCamelCase_       (   self ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.coca_transform(_lowerCAmelCase							).unsqueeze(0							)
								with torch.no_grad(), torch.cuda.amp.autocast():
														lowerCamelCase__								=	self.coca_model.generate(transformed_image.to(device=self.device ,dtype=self.coca_model.dtype							)							)
								lowerCamelCase__								=	self.coca_tokenizer.decode(generated[0].cpu().numpy()							)
								return generated.split("""<end_of_text>"""							)[0].replace("""<start_of_text>""" ,""""""							).rstrip(""" .,"""							)






		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase							):
								lowerCamelCase__								=	self.feature_extractor.preprocess(_lowerCAmelCase							)
								lowerCamelCase__								=	torch.from_numpy(clip_image_input["""pixel_values"""][0]							).unsqueeze(0							).to(self.device							).half()
								lowerCamelCase__								=	self.clip_model.get_image_features(_lowerCAmelCase							)
								lowerCamelCase__								=	image_embeddings_clip / image_embeddings_clip.norm(p=2 ,dim=-1 ,keepdim=_lowerCAmelCase							)
								lowerCamelCase__								=	image_embeddings_clip.repeat_interleave(_lowerCAmelCase ,dim=0							)
								return image_embeddings_clip






		@torch.enable_grad()
		def       UpperCamelCase_       (   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,):
								lowerCamelCase__								=	latents.detach().requires_grad_()

								lowerCamelCase__								=	self.scheduler.scale_model_input(_lowerCAmelCase ,_lowerCAmelCase							)

								# predict the noise residual
								lowerCamelCase__								=	self.unet(_lowerCAmelCase ,_lowerCAmelCase ,encoder_hidden_states=_lowerCAmelCase							).sample

								if isinstance(self.scheduler ,(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler)							):
														lowerCamelCase__								=	self.scheduler.alphas_cumprod[timestep]
														lowerCamelCase__								=	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
														lowerCamelCase__								=	(latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5

														lowerCamelCase__								=	torch.sqrt(_lowerCAmelCase							)
														lowerCamelCase__								=	pred_original_sample * (fac) + latents * (1 - fac)
								elif isinstance(self.scheduler ,_lowerCAmelCase							):
														lowerCamelCase__								=	self.scheduler.sigmas[index]
														lowerCamelCase__								=	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
								lowerCamelCase__								=	1 / 0.1_8215 * sample
								lowerCamelCase__								=	self.vae.decode(_lowerCAmelCase							).sample
								lowerCamelCase__								=	(image / 2 + 0.5).clamp(0 ,1							)

								lowerCamelCase__								=	transforms.Resize(self.feature_extractor_size							)(_lowerCAmelCase							)
								lowerCamelCase__								=	self.normalize(_lowerCAmelCase							).to(latents.dtype							)

								lowerCamelCase__								=	self.clip_model.get_image_features(_lowerCAmelCase							)
								lowerCamelCase__								=	image_embeddings_clip / image_embeddings_clip.norm(p=2 ,dim=-1 ,keepdim=_lowerCAmelCase							)

								lowerCamelCase__								=	spherical_dist_loss(_lowerCAmelCase ,_lowerCAmelCase							).mean() * clip_guidance_scale

								lowerCamelCase__								=	-torch.autograd.grad(_lowerCAmelCase ,_lowerCAmelCase							)[0]

								if isinstance(self.scheduler ,_lowerCAmelCase							):
														lowerCamelCase__								=	latents.detach() + grads * (sigma**2)
														lowerCamelCase__								=	noise_pred_original
								else:
														lowerCamelCase__								=	noise_pred_original - torch.sqrt(_lowerCAmelCase							) * grads
								return noise_pred, latents






		@torch.no_grad()
		def __call__(   self ,_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase = None ,_lowerCAmelCase = None ,_lowerCAmelCase = 5_12 ,_lowerCAmelCase = 5_12 ,_lowerCAmelCase = 0.6 ,_lowerCAmelCase = 50 ,_lowerCAmelCase = 7.5 ,_lowerCAmelCase = 1 ,_lowerCAmelCase = 0.0 ,_lowerCAmelCase = 1_00 ,_lowerCAmelCase = None ,_lowerCAmelCase = "pil" ,_lowerCAmelCase = True ,_lowerCAmelCase = 0.8 ,_lowerCAmelCase = 0.1 ,_lowerCAmelCase = 0.1 ,):
								if isinstance(_lowerCAmelCase ,_lowerCAmelCase							) and len(_lowerCAmelCase							) != batch_size:
														raise ValueError(F'''You have passed {batch_size} batch_size, but only {len(_lowerCAmelCase							)} 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(_lowerCAmelCase ,torch.Generator							) and batch_size > 1:
														lowerCamelCase__								=	[generator] + [None] * (batch_size - 1)

								lowerCamelCase__								=	[
								    ("""model""", self.coca_model is None),
								    ("""tokenizer""", self.coca_tokenizer is None),
								    ("""transform""", self.coca_transform is None),
								]
								lowerCamelCase__								=	[x[0] for x in coca_is_none if x[1]]
								lowerCamelCase__								=	""", """.join(_lowerCAmelCase							)
								# generate prompts with coca model if prompt is None
								if content_prompt is None:
														if len(_lowerCAmelCase							):
																				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.'''							)
														lowerCamelCase__								=	self.get_image_description(_lowerCAmelCase							)
								if style_prompt is None:
														if len(_lowerCAmelCase							):
																				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.'''							)
														lowerCamelCase__								=	self.get_image_description(_lowerCAmelCase							)

								# get prompt text embeddings for content and style
								lowerCamelCase__								=	self.tokenizer(
								    _lowerCAmelCase ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,truncation=_lowerCAmelCase ,return_tensors="""pt""" ,)
								lowerCamelCase__								=	self.text_encoder(content_text_input.input_ids.to(self.device							)							)[0]

								lowerCamelCase__								=	self.tokenizer(
								    _lowerCAmelCase ,padding="""max_length""" ,max_length=self.tokenizer.model_max_length ,truncation=_lowerCAmelCase ,return_tensors="""pt""" ,)
								lowerCamelCase__								=	self.text_encoder(style_text_input.input_ids.to(self.device							)							)[0]

								lowerCamelCase__								=	slerp(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

								# duplicate text embeddings for each generation per prompt
								lowerCamelCase__								=	text_embeddings.repeat_interleave(_lowerCAmelCase ,dim=0							)

								# set timesteps
								lowerCamelCase__								=	"""offset""" in set(inspect.signature(self.scheduler.set_timesteps							).parameters.keys()							)
								lowerCamelCase__								=	{}
								if accepts_offset:
														lowerCamelCase__								=	1

								self.scheduler.set_timesteps(_lowerCAmelCase ,**_lowerCAmelCase							)
								# 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							)

								lowerCamelCase__							,      lowerCamelCase__								=	self.get_timesteps(_lowerCAmelCase ,_lowerCAmelCase ,self.device							)
								lowerCamelCase__								=	timesteps[:1].repeat(_lowerCAmelCase							)

								# Preprocess image
								lowerCamelCase__								=	preprocess(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)
								lowerCamelCase__								=	self.prepare_latents(
								    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,text_embeddings.dtype ,self.device ,_lowerCAmelCase							)

								lowerCamelCase__								=	preprocess(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)
								lowerCamelCase__								=	self.prepare_latents(
								    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,text_embeddings.dtype ,self.device ,_lowerCAmelCase							)

								lowerCamelCase__								=	slerp(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

								if clip_guidance_scale > 0:
														lowerCamelCase__								=	self.get_clip_image_embeddings(_lowerCAmelCase ,_lowerCAmelCase							)
														lowerCamelCase__								=	self.get_clip_image_embeddings(_lowerCAmelCase ,_lowerCAmelCase							)
														lowerCamelCase__								=	slerp(
														    _lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase							)

								# 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.
								lowerCamelCase__								=	guidance_scale > 1.0
								# get unconditional embeddings for classifier free guidance
								if do_classifier_free_guidance:
														lowerCamelCase__								=	content_text_input.input_ids.shape[-1]
														lowerCamelCase__								=	self.tokenizer([""""""] ,padding="""max_length""" ,max_length=_lowerCAmelCase ,return_tensors="""pt"""							)
														lowerCamelCase__								=	self.text_encoder(uncond_input.input_ids.to(self.device							)							)[0]
														# duplicate unconditional embeddings for each generation per prompt
														lowerCamelCase__								=	uncond_embeddings.repeat_interleave(_lowerCAmelCase ,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
														lowerCamelCase__								=	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`.
								lowerCamelCase__								=	(batch_size, self.unet.config.in_channels, height // 8, width // 8)
								lowerCamelCase__								=	text_embeddings.dtype
								if latents is None:
														if self.device.type == "mps":
																				# randn does not work reproducibly on mps
																				lowerCamelCase__								=	torch.randn(_lowerCAmelCase ,generator=_lowerCAmelCase ,device="""cpu""" ,dtype=_lowerCAmelCase							).to(
																				    self.device							)
														else:
																				lowerCamelCase__								=	torch.randn(_lowerCAmelCase ,generator=_lowerCAmelCase ,device=self.device ,dtype=_lowerCAmelCase							)
								else:
														if latents.shape != latents_shape:
																				raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}'''							)
														lowerCamelCase__								=	latents.to(self.device							)

								# scale the initial noise by the standard deviation required by the scheduler
								lowerCamelCase__								=	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]
								lowerCamelCase__								=	"""eta""" in set(inspect.signature(self.scheduler.step							).parameters.keys()							)
								lowerCamelCase__								=	{}
								if accepts_eta:
														lowerCamelCase__								=	eta

								# check if the scheduler accepts generator
								lowerCamelCase__								=	"""generator""" in set(inspect.signature(self.scheduler.step							).parameters.keys()							)
								if accepts_generator:
														lowerCamelCase__								=	generator

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

																				# predict the noise residual
																				lowerCamelCase__								=	self.unet(_lowerCAmelCase ,_lowerCAmelCase ,encoder_hidden_states=_lowerCAmelCase							).sample

																				# perform classifier free guidance
																				if do_classifier_free_guidance:
																										lowerCamelCase__							,      lowerCamelCase__								=	noise_pred.chunk(2							)
																										lowerCamelCase__								=	noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

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

																				# compute the previous noisy sample x_t -> x_t-1
																				lowerCamelCase__								=	self.scheduler.step(_lowerCAmelCase ,_lowerCAmelCase ,_lowerCAmelCase ,**_lowerCAmelCase							).prev_sample

        # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
								lowerCamelCase__								=	1 / 0.1_8215 * latents
								lowerCamelCase__								=	self.vae.decode(_lowerCAmelCase							).sample

								lowerCamelCase__								=	(image / 2 + 0.5).clamp(0 ,1							)
								lowerCamelCase__								=	image.cpu().permute(0 ,2 ,3 ,1							).numpy()

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

								if not return_dict:
														return (image, None)

								return StableDiffusionPipelineOutput(images=_lowerCAmelCase ,nsfw_content_detected=_lowerCAmelCase							)







'''simple docstring'''



def   lowercase__(			__UpperCamelCase:					int    ):

      """simple docstring"""






      if not isinstance(__UpperCamelCase      ,__UpperCamelCase    ):
            raise TypeError('Input value must be an \'int\' type'    )

      SCREAMING_SNAKE_CASE   :      int    					=						0
      while number:
            position += 1
            number >>= 1

      return position


if __name__ == "__main__":
      import doctest

      doctest.testmod()


