






'''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


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







class       __lowercase					(_snake_case   ,				_snake_case    ):

	@register_to_config
	def __init__(      self   :						Optional[Any]  ,					UpperCAmelCase_   :						bool  ,					UpperCAmelCase_   :						Optional[int] = None  ,					UpperCAmelCase_   :						Optional[int] = None):
			super().__init__()

			UpperCamelCase__				:					Any					=					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"

					UpperCamelCase__				:					Tuple					=					torch.zeros(lowerCAmelCase__  ,					lowerCAmelCase__)
			else:
					UpperCamelCase__				:					Any					=					None

			UpperCamelCase__				:					Optional[Any]					=					torch.nn.Parameter(lowerCAmelCase__)







class       __lowercase					(_snake_case    ):
	_lowerCamelCase              =  42
	_lowerCamelCase              =  42
	_lowerCamelCase              =  42
	_lowerCamelCase              =  42
	_lowerCamelCase              =  42
	_lowerCamelCase              =  42

	def __init__(      self   :						str  ,					UpperCAmelCase_   :						VQModel  ,					UpperCAmelCase_   :						CLIPTextModel  ,					UpperCAmelCase_   :						CLIPTokenizer  ,					UpperCAmelCase_   :						TransformeraDModel  ,					UpperCAmelCase_   :						VQDiffusionScheduler  ,					UpperCAmelCase_   :						LearnedClassifierFreeSamplingEmbeddings  ,					):
			super().__init__()

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

	def      __UpperCamelCase							(      self   :						List[Any]  ,					UpperCAmelCase_   :						Optional[Any]  ,					UpperCAmelCase_   :						Optional[int]  ,					UpperCAmelCase_   :						int):
			UpperCamelCase__				:					List[str]					=					len(lowerCAmelCase__) if isinstance(lowerCAmelCase__  ,					lowerCAmelCase__) else 1

			# get prompt text embeddings
			UpperCamelCase__				:					List[Any]					=					self.tokenizer(
			    lowerCAmelCase__  ,					padding='max_length'  ,					max_length=self.tokenizer.model_max_length  ,					return_tensors='pt'  ,					)
			UpperCamelCase__				:					Tuple					=					text_inputs.input_ids

			if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
					UpperCamelCase__				:					Any					=					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}')
					UpperCamelCase__				:					List[str]					=					text_input_ids[:, : self.tokenizer.model_max_length]
			UpperCamelCase__				:					Union[str, Any]					=					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
			UpperCamelCase__				:					Union[str, Any]					=					prompt_embeds / prompt_embeds.norm(dim=-1  ,					keepdim=lowerCAmelCase__)

			# duplicate text embeddings for each generation per prompt
			UpperCamelCase__				:					List[str]					=					prompt_embeds.repeat_interleave(lowerCAmelCase__  ,					dim=0)

			if do_classifier_free_guidance:
					if self.learned_classifier_free_sampling_embeddings.learnable:
							UpperCamelCase__				:					List[Any]					=					self.learned_classifier_free_sampling_embeddings.embeddings
							UpperCamelCase__				:					Optional[Any]					=					negative_prompt_embeds.unsqueeze(0).repeat(lowerCAmelCase__  ,					1  ,					1)
					else:
							UpperCamelCase__				:					List[Any]					=					[''] * batch_size

							UpperCamelCase__				:					List[Any]					=					text_input_ids.shape[-1]
							UpperCamelCase__				:					Tuple					=					self.tokenizer(
							    lowerCAmelCase__  ,					padding='max_length'  ,					max_length=lowerCAmelCase__  ,					truncation=lowerCAmelCase__  ,					return_tensors='pt'  ,					)
							UpperCamelCase__				:					Optional[Any]					=					self.text_encoder(uncond_input.input_ids.to(self.device))[0]
							# See comment for normalizing text embeddings
							UpperCamelCase__				:					Dict					=					negative_prompt_embeds / negative_prompt_embeds.norm(dim=-1  ,					keepdim=lowerCAmelCase__)

					# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
					UpperCamelCase__				:					List[str]					=					negative_prompt_embeds.shape[1]
					UpperCamelCase__				:					str					=					negative_prompt_embeds.repeat(1  ,					lowerCAmelCase__  ,					1)
					UpperCamelCase__				:					int					=					negative_prompt_embeds.view(batch_size * num_images_per_prompt  ,					lowerCAmelCase__  ,					-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__				:					Union[str, Any]					=					torch.cat([negative_prompt_embeds, prompt_embeds])

			return prompt_embeds

	@torch.no_grad()
	def __call__(      self   :						Optional[Any]  ,					UpperCAmelCase_   :						Union[str, List[str]]  ,					UpperCAmelCase_   :						int = 100  ,					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  ,					):
			if isinstance(lowerCAmelCase__  ,					lowerCAmelCase__):
					UpperCamelCase__				:					Optional[Any]					=					1
			elif isinstance(lowerCAmelCase__  ,					lowerCAmelCase__):
					UpperCamelCase__				:					int					=					len(lowerCAmelCase__)
			else:
					raise ValueError(F'`prompt` has to be of type `str` or `list` but is {type(lowerCAmelCase__)}')

			UpperCamelCase__				:					List[str]					=					batch_size * num_images_per_prompt

			UpperCamelCase__				:					List[Any]					=					guidance_scale > 1.0

			UpperCamelCase__				:					Optional[Any]					=					self._encode_prompt(lowerCAmelCase__  ,					lowerCAmelCase__  ,					lowerCAmelCase__)

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

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

			UpperCamelCase__				:					List[Any]					=					(batch_size, self.transformer.num_latent_pixels)
			if latents is None:
					UpperCamelCase__				:					Optional[int]					=					self.transformer.num_vector_embeds - 1
					UpperCamelCase__				:					List[Any]					=					torch.full(lowerCAmelCase__  ,					lowerCAmelCase__).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).')
					UpperCamelCase__				:					int					=					latents.to(self.device)

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

			UpperCamelCase__				:					Optional[Any]					=					self.scheduler.timesteps.to(self.device)

			UpperCamelCase__				:					str					=					latents

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

					# predict the un-noised image
					# model_output == `log_p_x_0`
					UpperCamelCase__				:					Optional[Any]					=					self.transformer(lowerCAmelCase__  ,					encoder_hidden_states=lowerCAmelCase__  ,					timestep=lowerCAmelCase__).sample

					if do_classifier_free_guidance:
							UpperCamelCase__,	UpperCamelCase__				:					Optional[int]					=					model_output.chunk(2)
							UpperCamelCase__				:					Any					=					model_output_uncond + guidance_scale * (model_output_text - model_output_uncond)
							model_output -= torch.logsumexp(lowerCAmelCase__  ,					dim=1  ,					keepdim=lowerCAmelCase__)

					UpperCamelCase__				:					str					=					self.truncate(lowerCAmelCase__  ,					lowerCAmelCase__)

					# remove `log(0)`'s (`-inf`s)
					UpperCamelCase__				:					Tuple					=					model_output.clamp(-70)

					# compute the previous noisy sample x_t -> x_t-1
					UpperCamelCase__				:					Any					=					self.scheduler.step(lowerCAmelCase__  ,					timestep=lowerCAmelCase__  ,					sample=lowerCAmelCase__  ,					generator=lowerCAmelCase__).prev_sample

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

			UpperCamelCase__				:					Tuple					=					self.vqvae.config.vq_embed_dim
			UpperCamelCase__				:					int					=					(batch_size, self.transformer.height, self.transformer.width, embedding_channels)
			UpperCamelCase__				:					Optional[Any]					=					self.vqvae.quantize.get_codebook_entry(lowerCAmelCase__  ,					shape=lowerCAmelCase__)
			UpperCamelCase__				:					int					=					self.vqvae.decode(lowerCAmelCase__  ,					force_not_quantize=lowerCAmelCase__).sample

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

			if output_type == "pil":
					UpperCamelCase__				:					Dict					=					self.numpy_to_pil(lowerCAmelCase__)

			if not return_dict:
					return (image,)

			return ImagePipelineOutput(images=lowerCAmelCase__)





	def      __UpperCamelCase							(      self   :						Union[str, Any]  ,					UpperCAmelCase_   :						torch.FloatTensor  ,					UpperCAmelCase_   :						float):
			UpperCamelCase__,	UpperCamelCase__				:					Tuple					=					torch.sort(lowerCAmelCase__  ,					1  ,					descending=lowerCAmelCase__)
			UpperCamelCase__				:					str					=					torch.exp(lowerCAmelCase__)
			UpperCamelCase__				:					Tuple					=					sorted_p_x_0.cumsum(dim=1) < truncation_rate

			# Ensure that at least the largest probability is not zeroed out
			UpperCamelCase__				:					List[Any]					=					torch.full_like(keep_mask[:, 0:1, :]  ,					lowerCAmelCase__)
			UpperCamelCase__				:					Optional[int]					=					torch.cat((all_true, keep_mask)  ,					dim=1)
			UpperCamelCase__				:					Dict					=					keep_mask[:, :-1, :]

			UpperCamelCase__				:					List[Any]					=					keep_mask.gather(1  ,					indices.argsort(1))

			UpperCamelCase__				:					Optional[Any]					=					log_p_x_0.clone()

			UpperCamelCase__				:					Union[str, Any]					=					-torch.inf  # -inf = log(0)

			return rv












'''simple docstring'''




import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__          =						logging.get_logger(__name__)

lowerCAmelCase__          =						{
    'nvidia/segformer-b0-finetuned-ade-512-512': (
        'https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json'
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  '''segformer'''

	def __init__(      self   :						Tuple  ,					UpperCAmelCase_   :						Optional[Any]=3  ,					UpperCAmelCase_   :						Optional[int]=4  ,					UpperCAmelCase_   :						Tuple=[2, 2, 2, 2]  ,					UpperCAmelCase_   :						List[str]=[8, 4, 2, 1]  ,					UpperCAmelCase_   :						Union[str, Any]=[32, 64, 160, 256]  ,					UpperCAmelCase_   :						Any=[7, 3, 3, 3]  ,					UpperCAmelCase_   :						Any=[4, 2, 2, 2]  ,					UpperCAmelCase_   :						Union[str, Any]=[1, 2, 5, 8]  ,					UpperCAmelCase_   :						Tuple=[4, 4, 4, 4]  ,					UpperCAmelCase_   :						str="gelu"  ,					UpperCAmelCase_   :						List[Any]=0.0  ,					UpperCAmelCase_   :						int=0.0  ,					UpperCAmelCase_   :						int=0.1  ,					UpperCAmelCase_   :						List[str]=0.02  ,					UpperCAmelCase_   :						Dict=0.1  ,					UpperCAmelCase_   :						Dict=1e-6  ,					UpperCAmelCase_   :						int=256  ,					UpperCAmelCase_   :						Optional[int]=255  ,					**UpperCAmelCase_   :						Tuple  ,					):
			super().__init__(**UpperCAmelCase_)

			if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
					warnings.warn(
					    'Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'
					    ' removed, as the behaviour will default to that of reshape_last_stage = True.'  ,					UpperCAmelCase_  ,					)

			UpperCamelCase__				:					List[Any]					=					num_channels
			UpperCamelCase__				:					Any					=					num_encoder_blocks
			UpperCamelCase__				:					Dict					=					depths
			UpperCamelCase__				:					int					=					sr_ratios
			UpperCamelCase__				:					str					=					hidden_sizes
			UpperCamelCase__				:					List[str]					=					patch_sizes
			UpperCamelCase__				:					Optional[int]					=					strides
			UpperCamelCase__				:					Dict					=					mlp_ratios
			UpperCamelCase__				:					List[str]					=					num_attention_heads
			UpperCamelCase__				:					int					=					hidden_act
			UpperCamelCase__				:					Any					=					hidden_dropout_prob
			UpperCamelCase__				:					str					=					attention_probs_dropout_prob
			UpperCamelCase__				:					List[str]					=					classifier_dropout_prob
			UpperCamelCase__				:					List[Any]					=					initializer_range
			UpperCamelCase__				:					Union[str, Any]					=					drop_path_rate
			UpperCamelCase__				:					int					=					layer_norm_eps
			UpperCamelCase__				:					Dict					=					decoder_hidden_size
			UpperCamelCase__				:					List[Any]					=					kwargs.get('reshape_last_stage'  ,					UpperCAmelCase_)
			UpperCamelCase__				:					List[str]					=					semantic_loss_ignore_index







class       __lowercase					(__lowerCamelCase    ):
	_lowerCamelCase              =  version.parse('''1.11'''    )

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return OrderedDict(
			    [
			        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
			    ])

	@property
	def      __UpperCamelCase							(      self   :						Optional[Any]):
			return 1e-4





	@property
	def      __UpperCamelCase							(      self   :						Any):
			return 12




