
"""simple docstring"""





import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType






class     _UpperCAmelCase(    lowerCamelCase	):
		lowercase__						=      ['image_processor', 'tokenizer']
		lowercase__						=      'LayoutLMv2ImageProcessor'
		lowercase__						=      ('LayoutXLMTokenizer', 'LayoutXLMTokenizerFast')



		def __init__(					self				,		__a=None				,		__a=None				,		**__a)       ->       Optional[Any]:






							'''simple docstring'''
							if "feature_extractor" in kwargs:
												warnings.warn(
												    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
												    ''' instead.'''				,		__a				,		)
												_UpperCamelCase							 =  kwargs.pop('''feature_extractor''')

							_UpperCamelCase							 =  image_processor if image_processor is not None else feature_extractor
							if image_processor is None:
												raise ValueError('''You need to specify an `image_processor`.''')
							if tokenizer is None:
												raise ValueError('''You need to specify a `tokenizer`.''')

							super().__init__(__a				,		__a)



		def __call__(					self				,		__a				,		__a = None				,		__a = None				,		__a = None				,		__a = None				,		__a = True				,		__a = False				,		__a = None				,		__a = None				,		__a = 0				,		__a = None				,		__a = None				,		__a = None				,		__a = False				,		__a = False				,		__a = False				,		__a = False				,		__a = True				,		__a = None				,		**__a				,		)       ->       BatchEncoding:






							'''simple docstring'''
							# verify input
							if self.image_processor.apply_ocr and (boxes is not None):
												raise ValueError(
												    '''You cannot provide bounding boxes '''
												    '''if you initialized the image processor with apply_ocr set to True.''')

							if self.image_processor.apply_ocr and (word_labels is not None):
												raise ValueError(
												    '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.''')

							if return_overflowing_tokens is True and return_offsets_mapping is False:
												raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.''')

							# first, apply the image processor
							_UpperCamelCase							 =  self.image_processor(images=__a				,		return_tensors=__a)

							# second, apply the tokenizer
							if text is not None and self.image_processor.apply_ocr and text_pair is None:
												if isinstance(__a				,		__a):
																	_UpperCamelCase							 =  [text]  # add batch dimension (as the image processor always adds a batch dimension)
												_UpperCamelCase							 =  features['''words''']

							_UpperCamelCase							 =  self.tokenizer(
							    text=text if text is not None else features['''words''']				,		text_pair=text_pair if text_pair is not None else None				,		boxes=boxes if boxes is not None else features['''boxes''']				,		word_labels=__a				,		add_special_tokens=__a				,		padding=__a				,		truncation=__a				,		max_length=__a				,		stride=__a				,		pad_to_multiple_of=__a				,		return_token_type_ids=__a				,		return_attention_mask=__a				,		return_overflowing_tokens=__a				,		return_special_tokens_mask=__a				,		return_offsets_mapping=__a				,		return_length=__a				,		verbose=__a				,		return_tensors=__a				,		**__a				,		)

							# add pixel values
							_UpperCamelCase							 =  features.pop('''pixel_values''')
							if return_overflowing_tokens is True:
												_UpperCamelCase							 =  self.get_overflowing_images(__a				,		encoded_inputs['''overflow_to_sample_mapping'''])
							_UpperCamelCase							 =  images

							return encoded_inputs



		def 						UpperCAmelCase							(					self				,		__a				,		__a)       ->       Optional[int]:






							'''simple docstring'''
							# in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
							_UpperCamelCase							 =  []
							for sample_idx in overflow_to_sample_mapping:
												images_with_overflow.append(images[sample_idx])

							if len(__a) != len(__a):
												raise ValueError(
												    '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
												    F''' {len(__a)} and {len(__a)}''')

							return images_with_overflow



		def 						UpperCAmelCase							(					self				,		*__a				,		**__a)       ->       Tuple:






							'''simple docstring'''
							return self.tokenizer.batch_decode(*__a				,		**__a)



		def 						UpperCAmelCase							(					self				,		*__a				,		**__a)       ->       Union[str, Any]:






							'''simple docstring'''
							return self.tokenizer.decode(*__a				,		**__a)



		@property
		def 						UpperCAmelCase							(					self)       ->       List[Any]:






							'''simple docstring'''
							return ["input_ids", "bbox", "attention_mask", "image"]



		@property
		def 						UpperCAmelCase							(					self)       ->       List[str]:






							'''simple docstring'''
							warnings.warn(
							    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''				,		__a				,		)
							return self.image_processor_class




		@property
		def 						UpperCAmelCase							(					self)       ->       List[Any]:






							'''simple docstring'''
							warnings.warn(
							    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''				,		__a				,		)
							return self.image_processor








"""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__		(			__snake_case,     __snake_case,     __snake_case	)							->		int:


					"""simple docstring"""





					if isinstance(__snake_case,     torch.Tensor	):
										return image
					elif isinstance(__snake_case,     PIL.Image.Image	):
										_UpperCamelCase							 =  [image]

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



def       lowerCamelCase__		(			__snake_case,     __snake_case,     __snake_case,     __snake_case=0.9995	)							->		List[Any]:


					"""simple docstring"""





					if not isinstance(__snake_case,     np.ndarray	):
										_UpperCamelCase							 =  True
										_UpperCamelCase							 =  va.device
										_UpperCamelCase							 =  va.cpu().numpy()
										_UpperCamelCase							 =  va.cpu().numpy()

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

					if inputs_are_torch:
										_UpperCamelCase							 =  torch.from_numpy(__snake_case	).to(__snake_case	)

					return va



def       lowerCamelCase__		(			__snake_case,     __snake_case	)							->		List[str]:


					"""simple docstring"""





					_UpperCamelCase							 =  F.normalize(__snake_case,     dim=-1	)
					_UpperCamelCase							 =  F.normalize(__snake_case,     dim=-1	)
					return (x - y).norm(dim=-1	).div(2	).arcsin().pow(2	).mul(2	)



def       lowerCamelCase__		(			__snake_case,     __snake_case	)							->		Optional[int]:


					"""simple docstring"""





					for param in model.parameters():
										_UpperCamelCase							 =  value






class     _UpperCAmelCase(    lowerCamelCase	):



		def __init__(					self				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a=None				,		__a=None				,		__a=None				,		)       ->       List[str]:






							'''simple docstring'''
							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							 =  (
							    feature_extractor.size
							    if isinstance(feature_extractor.size				,		__a)
							    else feature_extractor.size['''shortest_edge''']
							)
							_UpperCamelCase							 =  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 						UpperCAmelCase							(					self				,		__a = "auto")       ->       Union[str, Any]:






							'''simple docstring'''
							if slice_size == "auto":
												# half the attention head size is usually a good trade-off between
												# speed and memory
												_UpperCamelCase							 =  self.unet.config.attention_head_dim // 2
							self.unet.set_attention_slice(__a)



		def 						UpperCAmelCase							(					self)       ->       Optional[int]:






							'''simple docstring'''
							self.enable_attention_slicing(__a)



		def 						UpperCAmelCase							(					self)       ->       int:






							'''simple docstring'''
							set_requires_grad(self.vae				,		__a)



		def 						UpperCAmelCase							(					self)       ->       Union[str, Any]:






							'''simple docstring'''
							set_requires_grad(self.vae				,		__a)



		def 						UpperCAmelCase							(					self)       ->       Tuple:






							'''simple docstring'''
							set_requires_grad(self.unet				,		__a)



		def 						UpperCAmelCase							(					self)       ->       int:






							'''simple docstring'''
							set_requires_grad(self.unet				,		__a)



		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a)       ->       Any:






							'''simple docstring'''
							# get the original timestep using init_timestep
							_UpperCamelCase							 =  min(int(num_inference_steps * strength)				,		__a)

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

							return timesteps, num_inference_steps - t_start



		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a=None)       ->       Tuple:






							'''simple docstring'''
							if not isinstance(__a				,		torch.Tensor):
												raise ValueError(F'''`image` has to be of type `torch.Tensor` but is {type(__a)}''')

							_UpperCamelCase							 =  image.to(device=__a				,		dtype=__a)

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

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

							_UpperCamelCase							 =  randn_tensor(init_latents.shape				,		generator=__a				,		device=__a				,		dtype=__a)

							# get latents
							_UpperCamelCase							 =  self.scheduler.add_noise(__a				,		__a				,		__a)
							_UpperCamelCase							 =  init_latents

							return latents



		def 						UpperCAmelCase							(					self				,		__a)       ->       str:






							'''simple docstring'''
							_UpperCamelCase							 =  self.coca_transform(__a).unsqueeze(0)
							with torch.no_grad(), torch.cuda.amp.autocast():
												_UpperCamelCase							 =  self.coca_model.generate(transformed_image.to(device=self.device				,		dtype=self.coca_model.dtype))
							_UpperCamelCase							 =  self.coca_tokenizer.decode(generated[0].cpu().numpy())
							return generated.split('''<end_of_text>''')[0].replace('''<start_of_text>'''				,		'''''').rstrip(''' .,''')



		def 						UpperCAmelCase							(					self				,		__a				,		__a)       ->       Optional[int]:






							'''simple docstring'''
							_UpperCamelCase							 =  self.feature_extractor.preprocess(__a)
							_UpperCamelCase							 =  torch.from_numpy(clip_image_input['''pixel_values'''][0]).unsqueeze(0).to(self.device).half()
							_UpperCamelCase							 =  self.clip_model.get_image_features(__a)
							_UpperCamelCase							 =  image_embeddings_clip / image_embeddings_clip.norm(p=2				,		dim=-1				,		keepdim=__a)
							_UpperCamelCase							 =  image_embeddings_clip.repeat_interleave(__a				,		dim=0)
							return image_embeddings_clip



		@torch.enable_grad()
		def 						UpperCAmelCase							(					self				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a				,		__a				,		)       ->       Tuple:






							'''simple docstring'''
							_UpperCamelCase							 =  latents.detach().requires_grad_()

							_UpperCamelCase							 =  self.scheduler.scale_model_input(__a				,		__a)

							# predict the noise residual
							_UpperCamelCase							 =  self.unet(__a				,		__a				,		encoder_hidden_states=__a).sample

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

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

							_UpperCamelCase							 =  transforms.Resize(self.feature_extractor_size)(__a)
							_UpperCamelCase							 =  self.normalize(__a).to(latents.dtype)

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

							_UpperCamelCase							 =  spherical_dist_loss(__a				,		__a).mean() * clip_guidance_scale

							_UpperCamelCase							 =  -torch.autograd.grad(__a				,		__a)[0]

							if isinstance(self.scheduler				,		__a):
												_UpperCamelCase							 =  latents.detach() + grads * (sigma**2)
												_UpperCamelCase							 =  noise_pred_original
							else:
												_UpperCamelCase							 =  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_12				,		__a = 5_12				,		__a = 0.6				,		__a = 50				,		__a = 7.5				,		__a = 1				,		__a = 0.0				,		__a = 1_00				,		__a = None				,		__a = "pil"				,		__a = True				,		__a = 0.8				,		__a = 0.1				,		__a = 0.1				,		)       ->       Dict:






							'''simple docstring'''
							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							 =  [generator] + [None] * (batch_size - 1)

							_UpperCamelCase							 =  [
							    ('''model''', self.coca_model is None),
							    ('''tokenizer''', self.coca_tokenizer is None),
							    ('''transform''', self.coca_transform is None),
							]
							_UpperCamelCase							 =  [x[0] for x in coca_is_none if x[1]]
							_UpperCamelCase							 =  ''', '''.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							 =  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							 =  self.get_image_description(__a)

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

							_UpperCamelCase							 =  self.tokenizer(
							    __a				,		padding='''max_length'''				,		max_length=self.tokenizer.model_max_length				,		truncation=__a				,		return_tensors='''pt'''				,		)
							_UpperCamelCase							 =  self.text_encoder(style_text_input.input_ids.to(self.device))[0]

							_UpperCamelCase							 =  slerp(__a				,		__a				,		__a)

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

							# set timesteps
							_UpperCamelCase							 =  '''offset''' in set(inspect.signature(self.scheduler.set_timesteps).parameters.keys())
							_UpperCamelCase							 =  {}
							if accepts_offset:
												_UpperCamelCase							 =  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							 =  self.get_timesteps(__a				,		__a				,		self.device)
							_UpperCamelCase							 =  timesteps[:1].repeat(__a)

							# Preprocess image
							_UpperCamelCase							 =  preprocess(__a				,		__a				,		__a)
							_UpperCamelCase							 =  self.prepare_latents(
							    __a				,		__a				,		__a				,		text_embeddings.dtype				,		self.device				,		__a)

							_UpperCamelCase							 =  preprocess(__a				,		__a				,		__a)
							_UpperCamelCase							 =  self.prepare_latents(
							    __a				,		__a				,		__a				,		text_embeddings.dtype				,		self.device				,		__a)

							_UpperCamelCase							 =  slerp(__a				,		__a				,		__a)

							if clip_guidance_scale > 0:
												_UpperCamelCase							 =  self.get_clip_image_embeddings(__a				,		__a)
												_UpperCamelCase							 =  self.get_clip_image_embeddings(__a				,		__a)
												_UpperCamelCase							 =  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							 =  guidance_scale > 1.0
							# get unconditional embeddings for classifier free guidance
							if do_classifier_free_guidance:
												_UpperCamelCase							 =  content_text_input.input_ids.shape[-1]
												_UpperCamelCase							 =  self.tokenizer(['''''']				,		padding='''max_length'''				,		max_length=__a				,		return_tensors='''pt''')
												_UpperCamelCase							 =  self.text_encoder(uncond_input.input_ids.to(self.device))[0]
												# duplicate unconditional embeddings for each generation per prompt
												_UpperCamelCase							 =  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							 =  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							 =  (batch_size, self.unet.config.in_channels, height // 8, width // 8)
							_UpperCamelCase							 =  text_embeddings.dtype
							if latents is None:
												if self.device.type == "mps":
																	# randn does not work reproducibly on mps
																	_UpperCamelCase							 =  torch.randn(__a				,		generator=__a				,		device='''cpu'''				,		dtype=__a).to(
																	    self.device)
												else:
																	_UpperCamelCase							 =  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							 =  latents.to(self.device)

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

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

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

																	# predict the noise residual
																	_UpperCamelCase							 =  self.unet(__a				,		__a				,		encoder_hidden_states=__a).sample

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

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

																	# compute the previous noisy sample x_t -> x_t-1
																	_UpperCamelCase							 =  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							 =  1 / 0.1_8215 * latents
							_UpperCamelCase							 =  self.vae.decode(__a).sample

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

							if output_type == "pil":
												_UpperCamelCase							 =  self.numpy_to_pil(__a)

							if not return_dict:
												return (image, None)

							return StableDiffusionPipelineOutput(images=__a				,		nsfw_content_detected=__a)






