


"""simple docstring"""







from typing import Callable, List, Optional, Tuple, Union

import torch
from transformers import CLIPTextModel, CLIPTokenizer

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin, TransformeraDModel, VQModel
from ...schedulers import VQDiffusionScheduler
from ...utils import logging
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


a__				:							Any						=     logging.get_logger(__name__)  # pylint: disable=invalid-name




class  UpperCamelCase_	( UpperCamelCase			,  UpperCamelCase):

	"""simple docstring"""





	@register_to_config
	def __init__( self			:				Tuple			,			UpperCAmelCase__			:				bool			,			UpperCAmelCase__			:				Optional[int] = None			,			UpperCAmelCase__			:				Optional[int] = None   )       ->	Tuple:
						super().__init__()

						__SCREAMING_SNAKE_CASE						   =				learnable

						if self.learnable:
											assert hidden_size is not None, "learnable=True requires `hidden_size` to be set"
											assert length is not None, "learnable=True requires `length` to be set"

											__SCREAMING_SNAKE_CASE						   =				torch.zeros(UpperCAmelCase__			,			UpperCAmelCase__   )
						else:
											__SCREAMING_SNAKE_CASE						   =				None

						__SCREAMING_SNAKE_CASE						   =				torch.nn.Parameter(UpperCAmelCase__   )




class  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





	snake_case__				:  VQModel
	snake_case__				:  CLIPTextModel
	snake_case__				:  CLIPTokenizer
	snake_case__				:  TransformeraDModel
	snake_case__				:  LearnedClassifierFreeSamplingEmbeddings
	snake_case__				:  VQDiffusionScheduler




	def __init__( self			:				Union[str, Any]			,			UpperCAmelCase__			:				VQModel			,			UpperCAmelCase__			:				CLIPTextModel			,			UpperCAmelCase__			:				CLIPTokenizer			,			UpperCAmelCase__			:				TransformeraDModel			,			UpperCAmelCase__			:				VQDiffusionScheduler			,			UpperCAmelCase__			:				LearnedClassifierFreeSamplingEmbeddings			,			)       ->	int:
						super().__init__()

						self.register_modules(
						    vqvae=UpperCAmelCase__			,			transformer=UpperCAmelCase__			,			text_encoder=UpperCAmelCase__			,			tokenizer=UpperCAmelCase__			,			scheduler=UpperCAmelCase__			,			learned_classifier_free_sampling_embeddings=UpperCAmelCase__			,			)




	def   UpperCAmelCase_      ( self			:				List[Any]			,			UpperCAmelCase__			:				List[Any]			,			UpperCAmelCase__			:				int			,			UpperCAmelCase__			:				Any   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				len(UpperCAmelCase__   ) if isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ) else 1

						# get prompt text embeddings
						__SCREAMING_SNAKE_CASE						   =				self.tokenizer(
						    UpperCAmelCase__			,			padding="max_length"			,			max_length=self.tokenizer.model_max_length			,			return_tensors="pt"			,			)
						__SCREAMING_SNAKE_CASE						   =				text_inputs.input_ids

						if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
											__SCREAMING_SNAKE_CASE						   =				self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]   )
											logger.warning(
											    "The following part of your input was truncated because CLIP can only handle sequences up to"
											    F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""   )
											__SCREAMING_SNAKE_CASE						   =				text_input_ids[:, : self.tokenizer.model_max_length]
						__SCREAMING_SNAKE_CASE						   =				self.text_encoder(text_input_ids.to(self.device   )   )[0]

						# NOTE: This additional step of normalizing the text embeddings is from VQ-Diffusion.
						# While CLIP does normalize the pooled output of the text transformer when combining
						# the image and text embeddings, CLIP does not directly normalize the last hidden state.
						#
						# CLIP normalizing the pooled output.
						# https://github.com/huggingface/transformers/blob/d92e22d1f28324f513f3080e5c47c071a3916721/src/transformers/models/clip/modeling_clip.py#L1052-L1053
						__SCREAMING_SNAKE_CASE						   =				prompt_embeds / prompt_embeds.norm(dim=-1			,			keepdim=UpperCAmelCase__   )

						# duplicate text embeddings for each generation per prompt
						__SCREAMING_SNAKE_CASE						   =				prompt_embeds.repeat_interleave(UpperCAmelCase__			,			dim=0   )

						if do_classifier_free_guidance:
											if self.learned_classifier_free_sampling_embeddings.learnable:
																__SCREAMING_SNAKE_CASE						   =				self.learned_classifier_free_sampling_embeddings.embeddings
																__SCREAMING_SNAKE_CASE						   =				negative_prompt_embeds.unsqueeze(0   ).repeat(UpperCAmelCase__			,			1			,			1   )
											else:
																__SCREAMING_SNAKE_CASE						   =				[""] * batch_size

																__SCREAMING_SNAKE_CASE						   =				text_input_ids.shape[-1]
																__SCREAMING_SNAKE_CASE						   =				self.tokenizer(
																    UpperCAmelCase__			,			padding="max_length"			,			max_length=UpperCAmelCase__			,			truncation=UpperCAmelCase__			,			return_tensors="pt"			,			)
																__SCREAMING_SNAKE_CASE						   =				self.text_encoder(uncond_input.input_ids.to(self.device   )   )[0]
																# See comment for normalizing text embeddings
																__SCREAMING_SNAKE_CASE						   =				negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1			,			keepdim=UpperCAmelCase__   )

											# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
											__SCREAMING_SNAKE_CASE						   =				negative_prompt_embeds.shape[1]
											__SCREAMING_SNAKE_CASE						   =				negative_prompt_embeds.repeat(1			,			UpperCAmelCase__			,			1   )
											__SCREAMING_SNAKE_CASE						   =				negative_prompt_embeds.view(batch_size * num_images_per_prompt			,			UpperCAmelCase__			,			-1   )

											# 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
											__SCREAMING_SNAKE_CASE						   =				torch.cat([negative_prompt_embeds, prompt_embeds]   )

						return prompt_embeds




	@torch.no_grad()
	def __call__( self			:				Tuple			,			UpperCAmelCase__			:				Union[str, List[str]]			,			UpperCAmelCase__			:				int = 1_0_0			,			UpperCAmelCase__			:				float = 5.0			,			UpperCAmelCase__			:				float = 1.0			,			UpperCAmelCase__			:				int = 1			,			UpperCAmelCase__			:				Optional[Union[torch.Generator, List[torch.Generator]]] = None			,			UpperCAmelCase__			:				Optional[torch.FloatTensor] = None			,			UpperCAmelCase__			:				Optional[str] = "pil"			,			UpperCAmelCase__			:				bool = True			,			UpperCAmelCase__			:				Optional[Callable[[int, int, torch.FloatTensor], None]] = None			,			UpperCAmelCase__			:				int = 1			,			)       ->	Union[ImagePipelineOutput, Tuple]:
						if isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ):
											__SCREAMING_SNAKE_CASE						   =				1
						elif isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ):
											__SCREAMING_SNAKE_CASE						   =				len(UpperCAmelCase__   )
						else:
											raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(UpperCAmelCase__   )}"""   )

						__SCREAMING_SNAKE_CASE						   =				batch_size * num_images_per_prompt

						__SCREAMING_SNAKE_CASE						   =				guidance_scale > 1.0

						__SCREAMING_SNAKE_CASE						   =				self._encode_prompt(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__   )

						if (callback_steps is None) or (
						    callback_steps is not None and (not isinstance(UpperCAmelCase__			,			UpperCAmelCase__   ) or callback_steps <= 0)
						):
											raise ValueError(
											    F"""`callback_steps` has to be a positive integer but is {callback_steps} of type"""
											    F""" {type(UpperCAmelCase__   )}."""   )

						# get the initial completely masked latents unless the user supplied it

						__SCREAMING_SNAKE_CASE						   =				(batch_size, self.transformer.num_latent_pixels)
						if latents is None:
											__SCREAMING_SNAKE_CASE						   =				self.transformer.num_vector_embeds - 1
											__SCREAMING_SNAKE_CASE						   =				torch.full(UpperCAmelCase__			,			UpperCAmelCase__   ).to(self.device   )
						else:
											if latents.shape != latents_shape:
																raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {latents_shape}"""   )
											if (latents < 0).any() or (latents >= self.transformer.num_vector_embeds).any():
																raise ValueError(
																    "Unexpected latents value(s). All latents be valid embedding indices i.e. in the range 0,"
																    F""" {self.transformer.num_vector_embeds - 1} (inclusive)."""   )
											__SCREAMING_SNAKE_CASE						   =				latents.to(self.device   )

						# set timesteps
						self.scheduler.set_timesteps(UpperCAmelCase__			,			device=self.device   )

						__SCREAMING_SNAKE_CASE						   =				self.scheduler.timesteps.to(self.device   )

						__SCREAMING_SNAKE_CASE						   =				latents

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

											# predict the un-noised image
											# model_output == `log_p_x_0`
											__SCREAMING_SNAKE_CASE						   =				self.transformer(UpperCAmelCase__			,			encoder_hidden_states=UpperCAmelCase__			,			timestep=UpperCAmelCase__   ).sample

											if do_classifier_free_guidance:
																__SCREAMING_SNAKE_CASE   ,  __SCREAMING_SNAKE_CASE						   =				model_output.chunk(2   )
																__SCREAMING_SNAKE_CASE						   =				model_output_uncond + guidance_scale * (model_output_text - model_output_uncond)
																model_output -= torch.logsumexp(UpperCAmelCase__			,			dim=1			,			keepdim=UpperCAmelCase__   )

											__SCREAMING_SNAKE_CASE						   =				self.truncate(UpperCAmelCase__			,			UpperCAmelCase__   )

											# remove `log(0)`'s (`-inf`s)
											__SCREAMING_SNAKE_CASE						   =				model_output.clamp(-7_0   )

											# compute the previous noisy sample x_t -> x_t-1
											__SCREAMING_SNAKE_CASE						   =				self.scheduler.step(UpperCAmelCase__			,			timestep=UpperCAmelCase__			,			sample=UpperCAmelCase__			,			generator=UpperCAmelCase__   ).prev_sample

											# call the callback, if provided
											if callback is not None and i % callback_steps == 0:
																callback(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				self.vqvae.config.vq_embed_dim
						__SCREAMING_SNAKE_CASE						   =				(batch_size, self.transformer.height, self.transformer.width, embedding_channels)
						__SCREAMING_SNAKE_CASE						   =				self.vqvae.quantize.get_codebook_entry(UpperCAmelCase__			,			shape=UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				self.vqvae.decode(UpperCAmelCase__			,			force_not_quantize=UpperCAmelCase__   ).sample

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

						if output_type == "pil":
											__SCREAMING_SNAKE_CASE						   =				self.numpy_to_pil(UpperCAmelCase__   )

						if not return_dict:
											return (image,)

						return ImagePipelineOutput(images=UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]			,			UpperCAmelCase__			:				torch.FloatTensor			,			UpperCAmelCase__			:				float   )       ->	torch.FloatTensor:
						__SCREAMING_SNAKE_CASE   ,  __SCREAMING_SNAKE_CASE						   =				torch.sort(UpperCAmelCase__			,			1			,			descending=UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				torch.exp(UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				sorted_p_x_0.cumsum(dim=1   ) < truncation_rate

						# Ensure that at least the largest probability is not zeroed out
						__SCREAMING_SNAKE_CASE						   =				torch.full_like(keep_mask[:, 0:1, :]			,			UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				torch.cat((all_true, keep_mask)			,			dim=1   )
						__SCREAMING_SNAKE_CASE						   =				keep_mask[:, :-1, :]

						__SCREAMING_SNAKE_CASE						   =				keep_mask.gather(1			,			indices.argsort(1   )   )

						__SCREAMING_SNAKE_CASE						   =				log_p_x_0.clone()

						__SCREAMING_SNAKE_CASE						   =				-torch.inf  # -inf = log(0)

						return rv




"""simple docstring"""







import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


a__				:							Tuple						=     False




class  UpperCamelCase_	( unittest.TestCase):

	"""simple docstring"""





	pass




@slow
@require_torch_gpu
class  UpperCamelCase_	( unittest.TestCase):

	"""simple docstring"""





	def   UpperCAmelCase_      ( self			:				int   )       ->	int:
						__SCREAMING_SNAKE_CASE						   =				VersatileDiffusionImageVariationPipeline.from_pretrained("shi-labs/versatile-diffusion"   )
						pipe.to(UpperCAmelCase__   )
						pipe.set_progress_bar_config(disable=UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				load_image(
						    "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"   )
						__SCREAMING_SNAKE_CASE						   =				torch.manual_seed(0   )
						__SCREAMING_SNAKE_CASE						   =				pipe(
						    image=UpperCAmelCase__			,			generator=UpperCAmelCase__			,			guidance_scale=7.5			,			num_inference_steps=5_0			,			output_type="numpy"			,			).images

						__SCREAMING_SNAKE_CASE						   =				image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

						assert image.shape == (1, 5_1_2, 5_1_2, 3)
						__SCREAMING_SNAKE_CASE						   =				np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]   )

						assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
