"""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    lowercase_   (   _lowerCamelCase:	Optional[int]						,   _lowerCamelCase:	List[str]						,   _lowerCamelCase:	Dict      ) ->      Any:



							'''simple docstring'''

							if isinstance(_lowerCamelCase						,   torch.Tensor      ):
														return image
							elif isinstance(_lowerCamelCase						,   PIL.Image.Image      ):
														__lowerCamelCase					:		Tuple		   =     [image]

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


def    lowercase_   (   _lowerCamelCase:	Any						,   _lowerCamelCase:	Tuple						,   _lowerCamelCase:	Dict						,   _lowerCamelCase:	List[str]=0.9995      ) ->      List[Any]:



							'''simple docstring'''

							if not isinstance(_lowerCamelCase						,   np.ndarray      ):
														__lowerCamelCase					:		Optional[Any]		   =     True
														__lowerCamelCase					:		Optional[Any]		   =     va.device
														__lowerCamelCase					:		str		   =     va.cpu().numpy()
														__lowerCamelCase					:		Dict		   =     va.cpu().numpy()

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

							if inputs_are_torch:
														__lowerCamelCase					:		List[Any]		   =     torch.from_numpy(_lowerCamelCase      ).to(_lowerCamelCase      )

							return va


def    lowercase_   (   _lowerCamelCase:	Tuple						,   _lowerCamelCase:	List[Any]      ) ->      int:



							'''simple docstring'''

							__lowerCamelCase					:		Any		   =     F.normalize(_lowerCamelCase						,   dim=-1      )
							__lowerCamelCase					:		str		   =     F.normalize(_lowerCamelCase						,   dim=-1      )
							return (x - y).norm(dim=-1      ).div(2      ).arcsin().pow(2      ).mul(2      )


def    lowercase_   (   _lowerCamelCase:	Any						,   _lowerCamelCase:	List[str]      ) ->      Optional[int]:



							'''simple docstring'''

							for param in model.parameters():
														__lowerCamelCase					:		List[Any]		   =     value







class  _snake_case     (					a__		):






						def __init__(       self      :			Optional[Any]							,					UpperCAmelCase      :			AutoencoderKL							,					UpperCAmelCase      :			CLIPTextModel							,					UpperCAmelCase      :			CLIPModel							,					UpperCAmelCase      :			CLIPTokenizer							,					UpperCAmelCase      :			UNetaDConditionModel							,					UpperCAmelCase      :			Union[PNDMScheduler, LMSDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler]							,					UpperCAmelCase      :			CLIPFeatureExtractor							,					UpperCAmelCase      :			Optional[Any]=None							,					UpperCAmelCase      :			List[str]=None							,					UpperCAmelCase      :			int=None							,					):
													super().__init__()
													self.register_modules(
													    vae=UpperCAmelCase							,					text_encoder=UpperCAmelCase							,					clip_model=UpperCAmelCase							,					tokenizer=UpperCAmelCase							,					unet=UpperCAmelCase							,					scheduler=UpperCAmelCase							,					feature_extractor=UpperCAmelCase							,					coca_model=UpperCAmelCase							,					coca_tokenizer=UpperCAmelCase							,					coca_transform=UpperCAmelCase							,					)
													__lowerCamelCase					:		List[str]		   =     (
													    feature_extractor.size
													    if isinstance(feature_extractor.size							,					UpperCAmelCase    )
													    else feature_extractor.size["shortest_edge"]
													)
													__lowerCamelCase					:		Any		   =     transforms.Normalize(mean=feature_extractor.image_mean							,					std=feature_extractor.image_std    )
													set_requires_grad(self.text_encoder							,					UpperCAmelCase    )
													set_requires_grad(self.clip_model							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			List[Any]							,					UpperCAmelCase      :			Optional[Union[str, int]] = "auto"    ):
													if slice_size == "auto":
																				# half the attention head size is usually a good trade-off between
																				# speed and memory
																				__lowerCamelCase					:		List[Any]		   =     self.unet.config.attention_head_dim // 2
													self.unet.set_attention_slice(UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			List[Any]    ):
													self.enable_attention_slicing(UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			str    ):
													set_requires_grad(self.vae							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			str    ):
													set_requires_grad(self.vae							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			List[Any]    ):
													set_requires_grad(self.unet							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			int    ):
													set_requires_grad(self.unet							,					UpperCAmelCase    )






						def       lowerCamelCase__     (       self      :			List[str]							,					UpperCAmelCase      :			Dict							,					UpperCAmelCase      :			str							,					UpperCAmelCase      :			Any    ):
													# get the original timestep using init_timestep
													__lowerCamelCase					:		str		   =     min(int(num_inference_steps * strength    )							,					UpperCAmelCase    )

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

													return timesteps, num_inference_steps - t_start






						def       lowerCamelCase__     (       self      :			Union[str, Any]							,					UpperCAmelCase      :			Tuple							,					UpperCAmelCase      :			Optional[Any]							,					UpperCAmelCase      :			List[Any]							,					UpperCAmelCase      :			List[str]							,					UpperCAmelCase      :			Dict							,					UpperCAmelCase      :			Any=None    ):
													if not isinstance(UpperCAmelCase							,					torch.Tensor    ):
																				raise ValueError(F"""`image` has to be of type `torch.Tensor` but is {type(UpperCAmelCase    )}"""    )

													__lowerCamelCase					:		Tuple		   =     image.to(device=UpperCAmelCase							,					dtype=UpperCAmelCase    )

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

													# Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
													__lowerCamelCase					:		Any		   =     0.1_8_2_1_5 * init_latents
													__lowerCamelCase					:		List[Any]		   =     init_latents.repeat_interleave(UpperCAmelCase							,					dim=0    )

													__lowerCamelCase					:		Union[str, Any]		   =     randn_tensor(init_latents.shape							,					generator=UpperCAmelCase							,					device=UpperCAmelCase							,					dtype=UpperCAmelCase    )

													# get latents
													__lowerCamelCase					:		List[str]		   =     self.scheduler.add_noise(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )
													__lowerCamelCase					:		Any		   =     init_latents

													return latents






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






						def       lowerCamelCase__     (       self      :			List[Any]							,					UpperCAmelCase      :			Optional[Any]							,					UpperCAmelCase      :			Union[str, Any]    ):
													__lowerCamelCase					:		str		   =     self.feature_extractor.preprocess(UpperCAmelCase    )
													__lowerCamelCase					:		int		   =     torch.from_numpy(clip_image_input["pixel_values"][0]    ).unsqueeze(0    ).to(self.device    ).half()
													__lowerCamelCase					:		Optional[int]		   =     self.clip_model.get_image_features(UpperCAmelCase    )
													__lowerCamelCase					:		Any		   =     image_embeddings_clip / image_embeddings_clip.norm(p=2							,					dim=-1							,					keepdim=UpperCAmelCase    )
													__lowerCamelCase					:		Optional[Any]		   =     image_embeddings_clip.repeat_interleave(UpperCAmelCase							,					dim=0    )
													return image_embeddings_clip






						@torch.enable_grad()
						def       lowerCamelCase__     (       self      :			Optional[Any]							,					UpperCAmelCase      :			List[str]							,					UpperCAmelCase      :			Optional[int]							,					UpperCAmelCase      :			Optional[int]							,					UpperCAmelCase      :			Optional[Any]							,					UpperCAmelCase      :			List[Any]							,					UpperCAmelCase      :			Union[str, Any]							,					UpperCAmelCase      :			Any							,					):
													__lowerCamelCase					:		List[Any]		   =     latents.detach().requires_grad_()

													__lowerCamelCase					:		int		   =     self.scheduler.scale_model_input(UpperCAmelCase							,					UpperCAmelCase    )

													# predict the noise residual
													__lowerCamelCase					:		Any		   =     self.unet(UpperCAmelCase							,					UpperCAmelCase							,					encoder_hidden_states=UpperCAmelCase    ).sample

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

																				__lowerCamelCase					:		Union[str, Any]		   =     torch.sqrt(UpperCAmelCase    )
																				__lowerCamelCase					:		Tuple		   =     pred_original_sample * (fac) + latents * (1 - fac)
													elif isinstance(self.scheduler							,					UpperCAmelCase    ):
																				__lowerCamelCase					:		List[Any]		   =     self.scheduler.sigmas[index]
																				__lowerCamelCase					:		Optional[Any]		   =     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					:		Union[str, Any]		   =     1 / 0.1_8_2_1_5 * sample
													__lowerCamelCase					:		List[Any]		   =     self.vae.decode(UpperCAmelCase    ).sample
													__lowerCamelCase					:		Dict		   =     (image / 2 + 0.5).clamp(0							,					1    )

													__lowerCamelCase					:		Dict		   =     transforms.Resize(self.feature_extractor_size    )(UpperCAmelCase    )
													__lowerCamelCase					:		str		   =     self.normalize(UpperCAmelCase    ).to(latents.dtype    )

													__lowerCamelCase					:		str		   =     self.clip_model.get_image_features(UpperCAmelCase    )
													__lowerCamelCase					:		str		   =     image_embeddings_clip / image_embeddings_clip.norm(p=2							,					dim=-1							,					keepdim=UpperCAmelCase    )

													__lowerCamelCase					:		Dict		   =     spherical_dist_loss(UpperCAmelCase							,					UpperCAmelCase    ).mean() * clip_guidance_scale

													__lowerCamelCase					:		Union[str, Any]		   =     -torch.autograd.grad(UpperCAmelCase							,					UpperCAmelCase    )[0]

													if isinstance(self.scheduler							,					UpperCAmelCase    ):
																				__lowerCamelCase					:		Any		   =     latents.detach() + grads * (sigma**2)
																				__lowerCamelCase					:		List[str]		   =     noise_pred_original
													else:
																				__lowerCamelCase					:		Optional[Any]		   =     noise_pred_original - torch.sqrt(UpperCAmelCase    ) * grads
													return noise_pred, latents







						@torch.no_grad()
						def __call__(       self      :			Union[str, Any]							,					UpperCAmelCase      :			Union[torch.FloatTensor, PIL.Image.Image]							,					UpperCAmelCase      :			Union[torch.FloatTensor, PIL.Image.Image]							,					UpperCAmelCase      :			Optional[str] = None							,					UpperCAmelCase      :			Optional[str] = None							,					UpperCAmelCase      :			Optional[int] = 512							,					UpperCAmelCase      :			Optional[int] = 512							,					UpperCAmelCase      :			float = 0.6							,					UpperCAmelCase      :			Optional[int] = 50							,					UpperCAmelCase      :			Optional[float] = 7.5							,					UpperCAmelCase      :			Optional[int] = 1							,					UpperCAmelCase      :			float = 0.0							,					UpperCAmelCase      :			Optional[float] = 100							,					UpperCAmelCase      :			Optional[torch.Generator] = None							,					UpperCAmelCase      :			Optional[str] = "pil"							,					UpperCAmelCase      :			bool = True							,					UpperCAmelCase      :			float = 0.8							,					UpperCAmelCase      :			float = 0.1							,					UpperCAmelCase      :			float = 0.1							,					):
													if isinstance(UpperCAmelCase							,					UpperCAmelCase    ) and len(UpperCAmelCase    ) != batch_size:
																				raise ValueError(F"""You have passed {batch_size} batch_size, but only {len(UpperCAmelCase    )} 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(UpperCAmelCase							,					torch.Generator    ) and batch_size > 1:
																				__lowerCamelCase					:		List[Any]		   =     [generator] + [None] * (batch_size - 1)

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

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

													__lowerCamelCase					:		Optional[Any]		   =     self.tokenizer(
													    UpperCAmelCase							,					padding="max_length"							,					max_length=self.tokenizer.model_max_length							,					truncation=UpperCAmelCase							,					return_tensors="pt"							,					)
													__lowerCamelCase					:		List[Any]		   =     self.text_encoder(style_text_input.input_ids.to(self.device    )    )[0]

													__lowerCamelCase					:		List[str]		   =     slerp(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )

													# duplicate text embeddings for each generation per prompt
													__lowerCamelCase					:		Optional[int]		   =     text_embeddings.repeat_interleave(UpperCAmelCase							,					dim=0    )

													# set timesteps
													__lowerCamelCase					:		Tuple		   =     "offset" in set(inspect.signature(self.scheduler.set_timesteps    ).parameters.keys()    )
													__lowerCamelCase					:		Optional[int]		   =     {}
													if accepts_offset:
																				__lowerCamelCase					:		Any		   =     1

													self.scheduler.set_timesteps(UpperCAmelCase							,					**UpperCAmelCase    )
													# 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					:		Union[str, Any]		   =     self.get_timesteps(UpperCAmelCase							,					UpperCAmelCase							,					self.device    )
													__lowerCamelCase					:		Optional[int]		   =     timesteps[:1].repeat(UpperCAmelCase    )

													# Preprocess image
													__lowerCamelCase					:		int		   =     preprocess(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )
													__lowerCamelCase					:		Optional[Any]		   =     self.prepare_latents(
													    UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					text_embeddings.dtype							,					self.device							,					UpperCAmelCase    )

													__lowerCamelCase					:		Optional[int]		   =     preprocess(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )
													__lowerCamelCase					:		Optional[Any]		   =     self.prepare_latents(
													    UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					text_embeddings.dtype							,					self.device							,					UpperCAmelCase    )

													__lowerCamelCase					:		Dict		   =     slerp(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )

													if clip_guidance_scale > 0:
																				__lowerCamelCase					:		str		   =     self.get_clip_image_embeddings(UpperCAmelCase							,					UpperCAmelCase    )
																				__lowerCamelCase					:		Any		   =     self.get_clip_image_embeddings(UpperCAmelCase							,					UpperCAmelCase    )
																				__lowerCamelCase					:		str		   =     slerp(
																				    UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase    )

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

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

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

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

																											# predict the noise residual
																											__lowerCamelCase					:		Any		   =     self.unet(UpperCAmelCase							,					UpperCAmelCase							,					encoder_hidden_states=UpperCAmelCase    ).sample

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

																											# perform clip guidance
																											if clip_guidance_scale > 0:
																																		__lowerCamelCase					:		Dict		   =     (
																																		    text_embeddings.chunk(2    )[1] if do_classifier_free_guidance else text_embeddings
																																		)
																																		__lowerCamelCase   ,  __lowerCamelCase					:		List[Any]		   =     self.cond_fn(
																																		    UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					)

																											# compute the previous noisy sample x_t -> x_t-1
																											__lowerCamelCase					:		Any		   =     self.scheduler.step(UpperCAmelCase							,					UpperCAmelCase							,					UpperCAmelCase							,					**UpperCAmelCase    ).prev_sample

        # Hardcode 0.18215 because stable-diffusion-2-base has not self.vae.config.scaling_factor
													__lowerCamelCase					:		int		   =     1 / 0.1_8_2_1_5 * latents
													__lowerCamelCase					:		List[Any]		   =     self.vae.decode(UpperCAmelCase    ).sample

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

													if output_type == "pil":
																				__lowerCamelCase					:		int		   =     self.numpy_to_pil(UpperCAmelCase    )

													if not return_dict:
																				return (image, None)

													return StableDiffusionPipelineOutput(images=UpperCAmelCase							,					nsfw_content_detected=UpperCAmelCase    )
"""simple docstring"""





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


__A  =				logging.get_logger(__name__)

__A  =				{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

__A  =				[
    '''small''',
    '''small-base''',
    '''medium''',
    '''medium-base''',
    '''intermediate''',
    '''intermediate-base''',
    '''large''',
    '''large-base''',
    '''xlarge''',
    '''xlarge-base''',
]

__A  =				{
    '''vocab_file''': {
        '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt''',
        '''funnel-transformer/small-base''': '''https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt''',
        '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt''',
        '''funnel-transformer/medium-base''': (
            '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/intermediate''': (
            '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/intermediate-base''': (
            '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt'''
        ),
        '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt''',
        '''funnel-transformer/large-base''': '''https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt''',
        '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt''',
        '''funnel-transformer/xlarge-base''': (
            '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''funnel-transformer/small''': '''https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json''',
        '''funnel-transformer/small-base''': (
            '''https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/medium''': '''https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json''',
        '''funnel-transformer/medium-base''': (
            '''https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/intermediate''': (
            '''https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/intermediate-base''': (
            '''https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/large''': '''https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json''',
        '''funnel-transformer/large-base''': (
            '''https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json'''
        ),
        '''funnel-transformer/xlarge''': '''https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json''',
        '''funnel-transformer/xlarge-base''': (
            '''https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json'''
        ),
    },
}
__A  =				{F"""funnel-transformer/{name}""": 512 for name in _model_names}
__A  =				{F"""funnel-transformer/{name}""": {'''do_lower_case''': True} for name in _model_names}
class  _snake_case     (					a__		):
						snake_case__			  =			VOCAB_FILES_NAMES
						snake_case__			  =			PRETRAINED_VOCAB_FILES_MAP
						snake_case__			  =			PRETRAINED_INIT_CONFIGURATION
						snake_case__			  =			FunnelTokenizer
						snake_case__			  =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						snake_case__			  =			2






						def __init__(       self      :			List[Any]							,					UpperCAmelCase      :			List[str]=None							,					UpperCAmelCase      :			Optional[int]=None							,					UpperCAmelCase      :			List[Any]=True							,					UpperCAmelCase      :			int="<unk>"							,					UpperCAmelCase      :			List[Any]="<sep>"							,					UpperCAmelCase      :			List[str]="<pad>"							,					UpperCAmelCase      :			Union[str, Any]="<cls>"							,					UpperCAmelCase      :			int="<mask>"							,					UpperCAmelCase      :			List[str]="<s>"							,					UpperCAmelCase      :			Union[str, Any]="</s>"							,					UpperCAmelCase      :			List[Any]=True							,					UpperCAmelCase      :			Tuple=True							,					UpperCAmelCase      :			int=None							,					UpperCAmelCase      :			int="##"							,					**UpperCAmelCase      :			Dict							,					):
													super().__init__(
													    UpperCAmelCase							,					tokenizer_file=UpperCAmelCase							,					do_lower_case=UpperCAmelCase							,					unk_token=UpperCAmelCase							,					sep_token=UpperCAmelCase							,					pad_token=UpperCAmelCase							,					cls_token=UpperCAmelCase							,					mask_token=UpperCAmelCase							,					bos_token=UpperCAmelCase							,					eos_token=UpperCAmelCase							,					clean_text=UpperCAmelCase							,					tokenize_chinese_chars=UpperCAmelCase							,					strip_accents=UpperCAmelCase							,					wordpieces_prefix=UpperCAmelCase							,					**UpperCAmelCase							,					)

													__lowerCamelCase					:		List[Any]		   =     json.loads(self.backend_tokenizer.normalizer.__getstate__()    )
													if (
													    normalizer_state.get("lowercase"							,					UpperCAmelCase    ) != do_lower_case
													    or normalizer_state.get("strip_accents"							,					UpperCAmelCase    ) != strip_accents
													    or normalizer_state.get("handle_chinese_chars"							,					UpperCAmelCase    ) != tokenize_chinese_chars
													):
																				__lowerCamelCase					:		Tuple		   =     getattr(UpperCAmelCase							,					normalizer_state.pop("type"    )    )
																				__lowerCamelCase					:		Optional[int]		   =     do_lower_case
																				__lowerCamelCase					:		Union[str, Any]		   =     strip_accents
																				__lowerCamelCase					:		Optional[Any]		   =     tokenize_chinese_chars
																				__lowerCamelCase					:		Optional[Any]		   =     normalizer_class(**UpperCAmelCase    )

													__lowerCamelCase					:		List[Any]		   =     do_lower_case






						def       lowerCamelCase__     (       self      :			Union[str, Any]							,					UpperCAmelCase      :			Union[str, Any]							,					UpperCAmelCase      :			Tuple=None    ):
													__lowerCamelCase					:		List[Any]		   =     [self.cls_token_id] + token_ids_a + [self.sep_token_id]

													if token_ids_a:
																				output += token_ids_a + [self.sep_token_id]

													return output






						def       lowerCamelCase__     (       self      :			Dict							,					UpperCAmelCase      :			List[int]							,					UpperCAmelCase      :			Optional[List[int]] = None    ):
													__lowerCamelCase					:		Tuple		   =     [self.sep_token_id]
													__lowerCamelCase					:		int		   =     [self.cls_token_id]
													if token_ids_a is None:
																				return len(cls    ) * [self.cls_token_type_id] + len(token_ids_a + sep    ) * [0]
													return len(cls    ) * [self.cls_token_type_id] + len(token_ids_a + sep    ) * [0] + len(token_ids_a + sep    ) * [1]







						def       lowerCamelCase__     (       self      :			Tuple							,					UpperCAmelCase      :			str							,					UpperCAmelCase      :			Optional[str] = None    ):
													__lowerCamelCase					:		List[Any]		   =     self._tokenizer.model.save(UpperCAmelCase							,					name=UpperCAmelCase    )
													return tuple(UpperCAmelCase    )