



"""simple docstring"""
from logging import getLogger
from typing import Any, Callable, List, Optional, Union

import numpy as np
import PIL
import torch

from ...schedulers import DDPMScheduler
from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
from ..pipeline_utils import ImagePipelineOutput
from . import StableDiffusionUpscalePipeline


_lowerCAmelCase					:str  =      getLogger(__name__)


_lowerCAmelCase					:int  =      4
_lowerCAmelCase					:Dict  =      7

_lowerCAmelCase					:Optional[int]  =      {
    'float16': torch.floataa,
    'float32': torch.floataa,
}





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

				if isinstance(image[0]  , PIL.Image.Image							):
								_UpperCAmelCase							:						Dict       							=					image[0].size
								_UpperCAmelCase							:						Optional[int]       							=					(x - x % 64 for x in (w, h))  # resize to integer multiple of 32

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

				return image


class 							_UpperCAmelCase							(				a   ):





					'''simple docstring'''



					def __init__(      self ,			A ,			A ,			A ,			A ,			A ,			A ,			A = 3_5_0 ,			)							->      Any:
									super().__init__(
									    vae=A ,			text_encoder=A ,			tokenizer=A ,			unet=A ,			low_res_scheduler=A ,			scheduler=A ,			safety_checker=A ,			feature_extractor=A ,			watermarker=A ,			max_noise_level=A ,			)





					def __call__(      self ,			A ,			A ,			A = 7_5 ,			A = 9.0 ,			A = 2_0 ,			A = None ,			A = 1 ,			A = 0.0 ,			A = None ,			A = None ,			A = None ,			A = None ,			A = "pil" ,			A = True ,			A = None ,			A = 1 ,			)							->      Optional[Any]:
									self.check_inputs(A ,			A ,			A ,			A	)

									# 2. Define call parameters
									if prompt is not None and isinstance(A ,			A	):
													_UpperCAmelCase							:						List[str]       							=					1
									elif prompt is not None and isinstance(A ,			A	):
													_UpperCAmelCase							:						List[str]       							=					len(A	)
									else:
													_UpperCAmelCase							:						Union[str, Any]       							=					prompt_embeds.shape[0]

									_UpperCAmelCase							:						Optional[int]       							=					self._execution_device
									# 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							:						Union[str, Any]       							=					guidance_scale > 1.0

									# 3. Encode input prompt
									_UpperCAmelCase							:						Optional[Any]       							=					self._encode_prompt(
									    A ,			A ,			A ,			A ,			A ,			prompt_embeds=A ,			negative_prompt_embeds=A ,			)

									_UpperCAmelCase							:						Tuple       							=					ORT_TO_PT_TYPE[str(text_embeddings.dtype	)]

									# 4. Preprocess image
									_UpperCAmelCase							:						Dict       							=					preprocess(A	)
									_UpperCAmelCase							:						Tuple       							=					image.cpu()

									# 5. set timesteps
									self.scheduler.set_timesteps(A ,			device=A	)
									_UpperCAmelCase							:						str       							=					self.scheduler.timesteps

									# 5. Add noise to image
									_UpperCAmelCase							:						List[Any]       							=					torch.tensor([noise_level] ,			dtype=torch.long ,			device=A	)
									_UpperCAmelCase							:						int       							=					torch.randn(image.shape ,			generator=A ,			device=A ,			dtype=A	)
									_UpperCAmelCase							:						Optional[Any]       							=					self.low_res_scheduler.add_noise(A ,			A ,			A	)

									_UpperCAmelCase							:						Dict       							=					2 if do_classifier_free_guidance else 1
									_UpperCAmelCase							:						int       							=					np.concatenate([image] * batch_multiplier * num_images_per_prompt	)
									_UpperCAmelCase							:						Tuple       							=					np.concatenate([noise_level] * image.shape[0]	)

									# 6. Prepare latent variables
									_UpperCAmelCase							:						int       							=					image.shape[2:]
									_UpperCAmelCase							:						Optional[int]       							=					self.prepare_latents(
									    batch_size * num_images_per_prompt ,			A ,			A ,			A ,			A ,			A ,			A ,			A ,			)

									# 7. Check that sizes of image and latents match
									_UpperCAmelCase							:						List[str]       							=					image.shape[1]
									if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
													raise ValueError(
													    '''Incorrect configuration settings! The config of `pipeline.unet` expects'''
													    f' {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +'
													    f' `num_channels_image`: {num_channels_image} '
													    f' = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of'
													    ''' `pipeline.unet` or your `image` input.'''	)

									# 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
									_UpperCAmelCase							:						Tuple       							=					self.prepare_extra_step_kwargs(A ,			A	)

									_UpperCAmelCase							:						Optional[int]       							=					next(
									    (input.type for input in self.unet.model.get_inputs() if input.name == '''timestep''') ,			'''tensor(float)'''	)
									_UpperCAmelCase							:						int       							=					ORT_TO_NP_TYPE[timestep_dtype]

									# 9. Denoising loop
									_UpperCAmelCase							:						str       							=					len(A	) - num_inference_steps * self.scheduler.order
									with self.progress_bar(total=A	) as progress_bar:
													for i, t in enumerate(A	):
																	# expand the latents if we are doing classifier free guidance
																	_UpperCAmelCase							:						Dict       							=					np.concatenate([latents] * 2	) if do_classifier_free_guidance else latents

																	# concat latents, mask, masked_image_latents in the channel dimension
																	_UpperCAmelCase							:						int       							=					self.scheduler.scale_model_input(A ,			A	)
																	_UpperCAmelCase							:						Union[str, Any]       							=					np.concatenate([latent_model_input, image] ,			axis=1	)

																	# timestep to tensor
																	_UpperCAmelCase							:						Optional[Any]       							=					np.array([t] ,			dtype=A	)

																	# predict the noise residual
																	_UpperCAmelCase							:						Optional[Any]       							=					self.unet(
																	    sample=A ,			timestep=A ,			encoder_hidden_states=A ,			class_labels=noise_level.astype(np.intaa	) ,			)[0]

																	# perform guidance
																	if do_classifier_free_guidance:
																					_UpperCAmelCase							:						Optional[int]       							=					np.split(A ,			2	)
																	_UpperCAmelCase							:						Optional[Any]       							=					noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

																	# compute the previous noisy sample x_t -> x_t-1
																	_UpperCAmelCase							:						Optional[Any]       							=					self.scheduler.step(
																	    torch.from_numpy(A	) ,			A ,			A ,			**A	).prev_sample

																	# call the callback, if provided
																	if i == len(A	) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
																					progress_bar.update()
																					if callback is not None and i % callback_steps == 0:
																									callback(A ,			A ,			A	)

        # 10. Post-processing
									_UpperCAmelCase							:						Optional[int]       							=					self.decode_latents(latents.float()	)

									# 11. Convert to PIL
									if output_type == "pil":
													_UpperCAmelCase							:						Dict       							=					self.numpy_to_pil(A	)

									if not return_dict:
													return (image,)

									return ImagePipelineOutput(images=A	)





					def 				__lowerCAmelCase			(      self ,			A	)							->      str:
									_UpperCAmelCase							:						List[Any]       							=					1 / 0.08_333 * latents
									_UpperCAmelCase							:						Optional[Any]       							=					self.vae(latent_sample=A	)[0]
									_UpperCAmelCase							:						Union[str, Any]       							=					np.clip(image / 2 + 0.5 ,			0 ,			1	)
									_UpperCAmelCase							:						List[str]       							=					image.transpose((0, 2, 3, 1)	)
									return image





					def 				__lowerCAmelCase			(      self ,			A ,			A ,			A ,			A ,			A ,			A = None ,			A = None ,			)							->      Any:
									if prompt is not None and isinstance(A ,			A	):
													_UpperCAmelCase							:						Optional[int]       							=					1
									elif prompt is not None and isinstance(A ,			A	):
													_UpperCAmelCase							:						Union[str, Any]       							=					len(A	)
									else:
													_UpperCAmelCase							:						int       							=					prompt_embeds.shape[0]

									if prompt_embeds is None:
													_UpperCAmelCase							:						int       							=					self.tokenizer(
													    A ,			padding='''max_length''' ,			max_length=self.tokenizer.model_max_length ,			truncation=A ,			return_tensors='''pt''' ,			)
													_UpperCAmelCase							:						Any       							=					text_inputs.input_ids
													_UpperCAmelCase							:						Union[str, Any]       							=					self.tokenizer(A ,			padding='''longest''' ,			return_tensors='''pt'''	).input_ids

													if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
													    A ,			A	):
																	_UpperCAmelCase							:						Optional[int]       							=					self.tokenizer.batch_decode(
																	    untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]	)
																	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}'	)

													# no positional arguments to text_encoder
													_UpperCAmelCase							:						Any       							=					self.text_encoder(
													    input_ids=text_input_ids.int().to(A	) ,			)
													_UpperCAmelCase							:						Optional[int]       							=					prompt_embeds[0]

									_UpperCAmelCase							:						str       							=					prompt_embeds.shape
									# duplicate text embeddings for each generation per prompt, using mps friendly method
									_UpperCAmelCase							:						Union[str, Any]       							=					prompt_embeds.repeat(1 ,			A	)
									_UpperCAmelCase							:						List[Any]       							=					prompt_embeds.reshape(bs_embed * num_images_per_prompt ,			A ,			-1	)

									# get unconditional embeddings for classifier free guidance
									if do_classifier_free_guidance and negative_prompt_embeds is None:
													_UpperCAmelCase							:						List[str]
													if negative_prompt is None:
																	_UpperCAmelCase							:						Union[str, Any]       							=					[''''''] * batch_size
													elif type(A	) is not type(A	):
																	raise TypeError(
																	    f'`negative_prompt` should be the same type to `prompt`, but got {type(A	)} !='
																	    f' {type(A	)}.'	)
													elif isinstance(A ,			A	):
																	_UpperCAmelCase							:						Union[str, Any]       							=					[negative_prompt]
													elif batch_size != len(A	):
																	raise ValueError(
																	    f'`negative_prompt`: {negative_prompt} has batch size {len(A	)}, but `prompt`:'
																	    f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'
																	    ''' the batch size of `prompt`.'''	)
													else:
																	_UpperCAmelCase							:						Optional[Any]       							=					negative_prompt

													_UpperCAmelCase							:						Optional[Any]       							=					text_input_ids.shape[-1]
													_UpperCAmelCase							:						Optional[Any]       							=					self.tokenizer(
													    A ,			padding='''max_length''' ,			max_length=A ,			truncation=A ,			return_tensors='''pt''' ,			)

													# if hasattr(uncond_input, "attention_mask"):
													#     attention_mask = uncond_input.attention_mask.to(device)
													# else:
													#     attention_mask = None

													_UpperCAmelCase							:						Any       							=					self.text_encoder(
													    input_ids=uncond_input.input_ids.int().to(A	) ,			)
													_UpperCAmelCase							:						Optional[int]       							=					uncond_embeddings[0]

									if do_classifier_free_guidance:
													_UpperCAmelCase							:						Optional[int]       							=					uncond_embeddings.shape[1]
													# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
													_UpperCAmelCase							:						Optional[int]       							=					uncond_embeddings.repeat(1 ,			A	)
													_UpperCAmelCase							:						Optional[int]       							=					uncond_embeddings.reshape(batch_size * num_images_per_prompt ,			A ,			-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
													_UpperCAmelCase							:						Optional[int]       							=					np.concatenate([uncond_embeddings, prompt_embeds]	)

									return prompt_embeds







"""simple docstring"""
import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer
class 							_UpperCAmelCase							(				tf.keras.layers.Layer   ):





     '''simple docstring'''



     def __init__(      self ,			A ,			A ,			A = None ,			A = None ,			A = None ,			A = "longest" ,			A = True ,			A = 5_1_2 ,			A = None ,			A = True ,			A = True ,			A = True ,			**A ,			)							->      Tuple:
         super().__init__()
         if use_fast_bert_tokenizer:
             _UpperCAmelCase							:						Union[str, Any]       							=					FastBertTokenizer(
                 A ,			token_out_type=tf.intaa ,			lower_case_nfd_strip_accents=A ,			**A	)
         else:
             _UpperCAmelCase							:						List[Any]       							=					tf.lookup.StaticVocabularyTable(
                 tf.lookup.KeyValueTensorInitializer(
                     keys=A ,			key_dtype=tf.string ,			values=tf.range(tf.size(A ,			out_type=tf.intaa	) ,			dtype=tf.intaa	) ,			value_dtype=tf.intaa ,			) ,			num_oov_buckets=1 ,			)
             _UpperCAmelCase							:						str       							=					BertTokenizerLayer(
                 A ,			token_out_type=tf.intaa ,			lower_case=A ,			**A	)

         _UpperCAmelCase							:						int       							=					vocab_list
         _UpperCAmelCase							:						Dict       							=					do_lower_case
         _UpperCAmelCase							:						Optional[int]       							=					cls_token_id or vocab_list.index('''[CLS]'''	)
         _UpperCAmelCase							:						Any       							=					sep_token_id or vocab_list.index('''[SEP]'''	)
         _UpperCAmelCase							:						List[Any]       							=					pad_token_id or vocab_list.index('''[PAD]'''	)
         _UpperCAmelCase							:						List[str]       							=					ShrinkLongestTrimmer(max_length - 3 ,			axis=1	)  # Allow room for special tokens
         _UpperCAmelCase							:						Tuple       							=					max_length
         _UpperCAmelCase							:						List[Any]       							=					padding
         _UpperCAmelCase							:						List[Any]       							=					truncation
         _UpperCAmelCase							:						Union[str, Any]       							=					pad_to_multiple_of
         _UpperCAmelCase							:						Union[str, Any]       							=					return_token_type_ids
         _UpperCAmelCase							:						Union[str, Any]       							=					return_attention_mask





     @classmethod
     def 				__lowerCAmelCase			(      cls ,			A ,			**A	)							->      Dict:  # noqa: F821
         _UpperCAmelCase							:						Optional[int]       							=					kwargs.pop('''do_lower_case''' ,			A	)
         _UpperCAmelCase							:						int       							=					tokenizer.do_lower_case if do_lower_case is None else do_lower_case
         _UpperCAmelCase							:						Optional[Any]       							=					kwargs.pop('''cls_token_id''' ,			A	)
         _UpperCAmelCase							:						str       							=					tokenizer.cls_token_id if cls_token_id is None else cls_token_id
         _UpperCAmelCase							:						str       							=					kwargs.pop('''sep_token_id''' ,			A	)
         _UpperCAmelCase							:						int       							=					tokenizer.sep_token_id if sep_token_id is None else sep_token_id
         _UpperCAmelCase							:						List[Any]       							=					kwargs.pop('''pad_token_id''' ,			A	)
         _UpperCAmelCase							:						Optional[Any]       							=					tokenizer.pad_token_id if pad_token_id is None else pad_token_id

         _UpperCAmelCase							:						int       							=					tokenizer.get_vocab()
         _UpperCAmelCase							:						List[str]       							=					sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()] ,			key=lambda A	: x[1]	)
         _UpperCAmelCase							:						Dict       							=					[entry[0] for entry in vocab]
         return cls(
             vocab_list=A ,			do_lower_case=A ,			cls_token_id=A ,			sep_token_id=A ,			pad_token_id=A ,			**A ,			)





     @classmethod
     def 				__lowerCAmelCase			(      cls ,			A ,			*A ,			**A	)							->      int:
         try:
             _UpperCAmelCase							:						int       							=					BertTokenizer.from_pretrained(A ,			*A ,			**A	)
         except:  # noqa: E722
             from .tokenization_bert_fast import BertTokenizerFast

             _UpperCAmelCase							:						Dict       							=					BertTokenizerFast.from_pretrained(A ,			*A ,			**A	)
         return cls.from_tokenizer(A ,			**A	)





     def 				__lowerCAmelCase			(      self ,			A	)							->      Any:
         if self.do_lower_case:
             _UpperCAmelCase							:						int       							=					case_fold_utfa(A	)
         _UpperCAmelCase							:						str       							=					self.tf_tokenizer.tokenize(A	)
         return tokens.merge_dims(1 ,			-1	)





     def 				__lowerCAmelCase			(      self ,			A ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			A=None ,			)							->      Optional[Any]:
         if padding is None:
             _UpperCAmelCase							:						str       							=					self.padding
         if padding not in ("longest", "max_length"):
             raise ValueError('''Padding must be either \'longest\' or \'max_length\'!'''	)
         if max_length is not None and text_pair is not None:
             # Because we have to instantiate a Trimmer to do it properly
             raise ValueError('''max_length cannot be overridden at call time when truncating paired texts!'''	)
         if max_length is None:
             _UpperCAmelCase							:						Union[str, Any]       							=					self.max_length
         if truncation is None:
             _UpperCAmelCase							:						List[Any]       							=					self.truncation
         if pad_to_multiple_of is None:
             _UpperCAmelCase							:						List[Any]       							=					self.pad_to_multiple_of
         if return_token_type_ids is None:
             _UpperCAmelCase							:						List[str]       							=					self.return_token_type_ids
         if return_attention_mask is None:
             _UpperCAmelCase							:						Any       							=					self.return_attention_mask
         if not isinstance(A ,			tf.Tensor	):
             _UpperCAmelCase							:						Dict       							=					tf.convert_to_tensor(A	)
         if text_pair is not None and not isinstance(A ,			tf.Tensor	):
             _UpperCAmelCase							:						str       							=					tf.convert_to_tensor(A	)
         if text_pair is not None:
             if text.shape.rank > 1:
                 raise ValueError('''text argument should not be multidimensional when a text pair is supplied!'''	)
             if text_pair.shape.rank > 1:
                 raise ValueError('''text_pair should not be multidimensional!'''	)
         if text.shape.rank == 2:
             _UpperCAmelCase							,       _UpperCAmelCase							:						str       							=					text[:, 0], text[:, 1]
         _UpperCAmelCase							:						Optional[Any]       							=					self.unpaired_tokenize(A	)
         if text_pair is None:  # Unpaired text
             if truncation:
                 _UpperCAmelCase							:						Optional[int]       							=					text[:, : max_length - 2]  # Allow room for special tokens
             _UpperCAmelCase							,       _UpperCAmelCase							:						Any       							=					combine_segments(
                 (text,) ,			start_of_sequence_id=self.cls_token_id ,			end_of_segment_id=self.sep_token_id	)
         else:  # Paired text
             _UpperCAmelCase							:						Tuple       							=					self.unpaired_tokenize(A	)
             if truncation:
                 _UpperCAmelCase							,       _UpperCAmelCase							:						Optional[int]       							=					self.paired_trimmer.trim([text, text_pair]	)
             _UpperCAmelCase							,       _UpperCAmelCase							:						Union[str, Any]       							=					combine_segments(
                 (text, text_pair) ,			start_of_sequence_id=self.cls_token_id ,			end_of_segment_id=self.sep_token_id	)
         if padding == "longest":
             _UpperCAmelCase							:						List[Any]       							=					input_ids.bounding_shape(axis=1	)
             if pad_to_multiple_of is not None:
                 # No ceiling division in tensorflow, so we negate floordiv instead
                 _UpperCAmelCase							:						Tuple       							=					pad_to_multiple_of * (-tf.math.floordiv(-pad_length ,			A	))
         else:
             _UpperCAmelCase							:						int       							=					max_length

         _UpperCAmelCase							,       _UpperCAmelCase							:						Any       							=					pad_model_inputs(A ,			max_seq_length=A ,			pad_value=self.pad_token_id	)
         _UpperCAmelCase							:						Dict       							=					{'''input_ids''': input_ids}
         if return_attention_mask:
             _UpperCAmelCase							:						Optional[int]       							=					attention_mask
         if return_token_type_ids:
             _UpperCAmelCase							,       _UpperCAmelCase							:						Tuple       							=					pad_model_inputs(
                 A ,			max_seq_length=A ,			pad_value=self.pad_token_id	)
             _UpperCAmelCase							:						int       							=					token_type_ids
         return output





     def 				__lowerCAmelCase			(      self	)							->      List[str]:
         return {
             "vocab_list": self.vocab_list,
             "do_lower_case": self.do_lower_case,
             "cls_token_id": self.cls_token_id,
             "sep_token_id": self.sep_token_id,
             "pad_token_id": self.pad_token_id,
         }


