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

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import DiffusionPipeline
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import logging


_lowerCamelCase   :				List[str]     			=						logging.get_logger(__name__)  # pylint: disable=invalid-name


class   lowercase      (       a       ):




							def __init__(  self				:   List[Any]		,							_UpperCamelCase				:   AutoencoderKL		,							_UpperCamelCase				:   CLIPTextModel		,							_UpperCamelCase				:   CLIPTokenizer		,							_UpperCamelCase				:   UNetaDConditionModel		,							_UpperCamelCase				:   Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]		,							_UpperCamelCase				:   StableDiffusionSafetyChecker		,							_UpperCamelCase				:   CLIPImageProcessor		,							)   ->			List[Any]:



												'''simple docstring'''

												super().__init__()
												self.register_modules(
												    vae=_UpperCamelCase		,							text_encoder=_UpperCamelCase		,							tokenizer=_UpperCamelCase		,							unet=_UpperCamelCase		,							scheduler=_UpperCamelCase		,							safety_checker=_UpperCamelCase		,							feature_extractor=_UpperCamelCase		,							)




							def 		__snake_case(  self				:   Dict		,							_UpperCamelCase				:   Optional[Union[str, int]] = "auto" )   ->			int:



												'''simple docstring'''

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




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



												'''simple docstring'''

												self.enable_attention_slicing(_UpperCamelCase )





							@torch.no_grad()
							def __call__(  self				:   Tuple		,							_UpperCamelCase				:   Union[str, List[str]]		,							_UpperCamelCase				:   int = 512		,							_UpperCamelCase				:   int = 512		,							_UpperCamelCase				:   int = 50		,							_UpperCamelCase				:   float = 7.5		,							_UpperCamelCase				:   Optional[Union[str, List[str]]] = None		,							_UpperCamelCase				:   Optional[int] = 1		,							_UpperCamelCase				:   float = 0.0		,							_UpperCamelCase				:   Optional[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		,							_UpperCamelCase				:   Optional[torch.FloatTensor] = None		,							**_UpperCamelCase				:   Optional[Any]		,							)   ->			Union[str, Any]:



												'''simple docstring'''

												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 )}" )

												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 (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 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]

												if text_embeddings is None:
																	SCREAMING_SNAKE_CASE           =    self.text_encoder(text_input_ids.to(self.device ) )[0]

												# duplicate text embeddings for each generation per prompt, using mps friendly method
												SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    text_embeddings.shape
												SCREAMING_SNAKE_CASE           =    text_embeddings.repeat(1		,							_UpperCamelCase		,							1 )
												SCREAMING_SNAKE_CASE           =    text_embeddings.view(bs_embed * num_images_per_prompt		,							_UpperCamelCase		,							-1 )

												# 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.
												SCREAMING_SNAKE_CASE           =    guidance_scale > 1.0
												# get unconditional embeddings for classifier free guidance
												if do_classifier_free_guidance:
																	SCREAMING_SNAKE_CASE           =    42
																	if negative_prompt is None:
																						SCREAMING_SNAKE_CASE           =    [""]
																	elif type(_UpperCamelCase ) is not type(_UpperCamelCase ):
																						raise TypeError(
																						    F"`negative_prompt` should be the same type to `prompt`, but got {type(_UpperCamelCase )} !="
																						    F" {type(_UpperCamelCase )}." )
																	elif isinstance(_UpperCamelCase		,							_UpperCamelCase ):
																						SCREAMING_SNAKE_CASE           =    [negative_prompt]
																	elif batch_size != len(_UpperCamelCase ):
																						raise ValueError(
																						    F"`negative_prompt`: {negative_prompt} has batch size {len(_UpperCamelCase )}, but `prompt`:"
																						    F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
																						    " the batch size of `prompt`." )
																	else:
																						SCREAMING_SNAKE_CASE           =    negative_prompt

																	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]

																	# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
																	SCREAMING_SNAKE_CASE           =    uncond_embeddings.shape[1]
																	SCREAMING_SNAKE_CASE           =    uncond_embeddings.repeat(_UpperCamelCase		,							_UpperCamelCase		,							1 )
																	SCREAMING_SNAKE_CASE           =    uncond_embeddings.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([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`.
												SCREAMING_SNAKE_CASE           =    (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
												SCREAMING_SNAKE_CASE           =    (batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64)
												SCREAMING_SNAKE_CASE           =    text_embeddings.dtype
												if latents is None:
																	if self.device.type == "mps":
																						# randn does not exist on mps
																						SCREAMING_SNAKE_CASE           =    torch.randn(
																						    _UpperCamelCase		,							generator=_UpperCamelCase		,							device="cpu"		,							dtype=_UpperCamelCase ).to(self.device )
																						SCREAMING_SNAKE_CASE           =    torch.randn(_UpperCamelCase		,							generator=_UpperCamelCase		,							device="cpu"		,							dtype=_UpperCamelCase ).to(
																						    self.device )
																	else:
																						SCREAMING_SNAKE_CASE           =    torch.randn(
																						    _UpperCamelCase		,							generator=_UpperCamelCase		,							device=self.device		,							dtype=_UpperCamelCase )
																						SCREAMING_SNAKE_CASE           =    torch.randn(_UpperCamelCase		,							generator=_UpperCamelCase		,							device=self.device		,							dtype=_UpperCamelCase )
												else:
																	if latents_reference.shape != latents_shape:
																						raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {latents_shape}" )
																	SCREAMING_SNAKE_CASE           =    latents_reference.to(self.device )
																	SCREAMING_SNAKE_CASE           =    latents.to(self.device )

												# This is the key part of the pipeline where we
												# try to ensure that the generated images w/ the same seed
												# but different sizes actually result in similar images
												SCREAMING_SNAKE_CASE           =    (latents_shape[3] - latents_shape_reference[3]) // 2
												SCREAMING_SNAKE_CASE           =    (latents_shape[2] - latents_shape_reference[2]) // 2
												SCREAMING_SNAKE_CASE           =    latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx
												SCREAMING_SNAKE_CASE           =    latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy
												SCREAMING_SNAKE_CASE           =    0 if dx < 0 else dx
												SCREAMING_SNAKE_CASE           =    0 if dy < 0 else dy
												SCREAMING_SNAKE_CASE           =    max(-dx		,							0 )
												SCREAMING_SNAKE_CASE           =    max(-dy		,							0 )
												# import pdb
												# pdb.set_trace()
												SCREAMING_SNAKE_CASE           =    latents_reference[:, :, dy : dy + h, dx : dx + w]

												# set timesteps
												self.scheduler.set_timesteps(_UpperCamelCase )

												# Some schedulers like PNDM have timesteps as arrays
												# It's more optimized to move all timesteps to correct device beforehand
												SCREAMING_SNAKE_CASE           =    self.scheduler.timesteps.to(self.device )

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

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

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

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

																	# compute the previous noisy sample x_t -> x_t-1
																	SCREAMING_SNAKE_CASE           =    self.scheduler.step(_UpperCamelCase		,							_UpperCamelCase		,							_UpperCamelCase		,							**_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           =    1 / 0.1_8_2_1_5 * latents
												SCREAMING_SNAKE_CASE           =    self.vae.decode(_UpperCamelCase ).sample

												SCREAMING_SNAKE_CASE           =    (image / 2 + 0.5).clamp(0		,							1 )

												# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
												SCREAMING_SNAKE_CASE           =    image.cpu().permute(0		,							2		,							3		,							1 ).float().numpy()

												if self.safety_checker is not None:
																	SCREAMING_SNAKE_CASE           =    self.feature_extractor(self.numpy_to_pil(_UpperCamelCase )		,							return_tensors="pt" ).to(
																	    self.device )
																	SCREAMING_SNAKE_CASE     ,				SCREAMING_SNAKE_CASE           =    self.safety_checker(
																	    images=_UpperCamelCase		,							clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype ) )
												else:
																	SCREAMING_SNAKE_CASE           =    None

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

												if not return_dict:
																	return (image, has_nsfw_concept)

												return StableDiffusionPipelineOutput(images=_UpperCamelCase		,							nsfw_content_detected=_UpperCamelCase )





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase   :				int     			=						logging.get_logger(__name__)

_lowerCamelCase   :				Union[str, Any]     			=						{
    '''microsoft/unispeech-sat-base-100h-libri-ft''': (
        '''https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'''
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}


class   lowercase      (       a       ):
							lowercase__  :    Tuple									=							"""unispeech-sat"""




							def __init__(  self				:   str		,							_UpperCamelCase				:   Tuple=32		,							_UpperCamelCase				:   Union[str, Any]=768		,							_UpperCamelCase				:   Tuple=12		,							_UpperCamelCase				:   List[str]=12		,							_UpperCamelCase				:   Tuple=3_072		,							_UpperCamelCase				:   List[str]="gelu"		,							_UpperCamelCase				:   Tuple=0.1		,							_UpperCamelCase				:   Any=0.1		,							_UpperCamelCase				:   Union[str, Any]=0.1		,							_UpperCamelCase				:   Dict=0.0		,							_UpperCamelCase				:   Tuple=0.0		,							_UpperCamelCase				:   Tuple=0.1		,							_UpperCamelCase				:   Optional[Any]=0.1		,							_UpperCamelCase				:   Tuple=0.0_2		,							_UpperCamelCase				:   Optional[int]=1e-5		,							_UpperCamelCase				:   Union[str, Any]="group"		,							_UpperCamelCase				:   Optional[int]="gelu"		,							_UpperCamelCase				:   Tuple=(512, 512, 512, 512, 512, 512, 512)		,							_UpperCamelCase				:   List[str]=(5, 2, 2, 2, 2, 2, 2)		,							_UpperCamelCase				:   Optional[int]=(10, 3, 3, 3, 3, 2, 2)		,							_UpperCamelCase				:   Optional[int]=False		,							_UpperCamelCase				:   Dict=128		,							_UpperCamelCase				:   Optional[int]=16		,							_UpperCamelCase				:   Tuple=False		,							_UpperCamelCase				:   Union[str, Any]=True		,							_UpperCamelCase				:   Optional[Any]=0.0_5		,							_UpperCamelCase				:   Union[str, Any]=10		,							_UpperCamelCase				:   Union[str, Any]=2		,							_UpperCamelCase				:   str=0.0		,							_UpperCamelCase				:   List[Any]=10		,							_UpperCamelCase				:   Optional[int]=0		,							_UpperCamelCase				:   Any=320		,							_UpperCamelCase				:   List[Any]=2		,							_UpperCamelCase				:   str=0.1		,							_UpperCamelCase				:   str=100		,							_UpperCamelCase				:   int=256		,							_UpperCamelCase				:   Optional[Any]=256		,							_UpperCamelCase				:   List[Any]=0.1		,							_UpperCamelCase				:   str="mean"		,							_UpperCamelCase				:   int=False		,							_UpperCamelCase				:   Optional[Any]=False		,							_UpperCamelCase				:   Any=256		,							_UpperCamelCase				:   str=(512, 512, 512, 512, 1_500)		,							_UpperCamelCase				:   List[Any]=(5, 3, 3, 1, 1)		,							_UpperCamelCase				:   Union[str, Any]=(1, 2, 3, 1, 1)		,							_UpperCamelCase				:   Any=512		,							_UpperCamelCase				:   str=0		,							_UpperCamelCase				:   int=1		,							_UpperCamelCase				:   Any=2		,							_UpperCamelCase				:   Optional[Any]=504		,							**_UpperCamelCase				:   str		,							)   ->			int:



												'''simple docstring'''

												super().__init__(**_UpperCamelCase		,							pad_token_id=_UpperCamelCase		,							bos_token_id=_UpperCamelCase		,							eos_token_id=_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    hidden_size
												SCREAMING_SNAKE_CASE           =    feat_extract_norm
												SCREAMING_SNAKE_CASE           =    feat_extract_activation
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    conv_bias
												SCREAMING_SNAKE_CASE           =    num_conv_pos_embeddings
												SCREAMING_SNAKE_CASE           =    num_conv_pos_embedding_groups
												SCREAMING_SNAKE_CASE           =    len(self.conv_dim )
												SCREAMING_SNAKE_CASE           =    num_hidden_layers
												SCREAMING_SNAKE_CASE           =    intermediate_size
												SCREAMING_SNAKE_CASE           =    hidden_act
												SCREAMING_SNAKE_CASE           =    num_attention_heads
												SCREAMING_SNAKE_CASE           =    hidden_dropout
												SCREAMING_SNAKE_CASE           =    attention_dropout
												SCREAMING_SNAKE_CASE           =    activation_dropout
												SCREAMING_SNAKE_CASE           =    feat_proj_dropout
												SCREAMING_SNAKE_CASE           =    final_dropout
												SCREAMING_SNAKE_CASE           =    layerdrop
												SCREAMING_SNAKE_CASE           =    layer_norm_eps
												SCREAMING_SNAKE_CASE           =    initializer_range
												SCREAMING_SNAKE_CASE           =    vocab_size
												SCREAMING_SNAKE_CASE           =    num_clusters
												SCREAMING_SNAKE_CASE           =    do_stable_layer_norm
												SCREAMING_SNAKE_CASE           =    use_weighted_layer_sum

												if (
												    (len(self.conv_stride ) != self.num_feat_extract_layers)
												    or (len(self.conv_kernel ) != self.num_feat_extract_layers)
												    or (len(self.conv_dim ) != self.num_feat_extract_layers)
												):
																	raise ValueError(
																	    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
																	    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
																	    F" {len(self.conv_dim )}`, `len(config.conv_stride) = {len(self.conv_stride )}`,"
																	    F" `len(config.conv_kernel) = {len(self.conv_kernel )}`." )

												# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
												SCREAMING_SNAKE_CASE           =    apply_spec_augment
												SCREAMING_SNAKE_CASE           =    mask_time_prob
												SCREAMING_SNAKE_CASE           =    mask_time_length
												SCREAMING_SNAKE_CASE           =    mask_time_min_masks
												SCREAMING_SNAKE_CASE           =    mask_feature_prob
												SCREAMING_SNAKE_CASE           =    mask_feature_length
												SCREAMING_SNAKE_CASE           =    mask_feature_min_masks

												# parameters for pretraining with codevector quantized representations
												SCREAMING_SNAKE_CASE           =    num_codevectors_per_group
												SCREAMING_SNAKE_CASE           =    num_codevector_groups
												SCREAMING_SNAKE_CASE           =    contrastive_logits_temperature
												SCREAMING_SNAKE_CASE           =    feat_quantizer_dropout
												SCREAMING_SNAKE_CASE           =    num_negatives
												SCREAMING_SNAKE_CASE           =    codevector_dim
												SCREAMING_SNAKE_CASE           =    proj_codevector_dim
												SCREAMING_SNAKE_CASE           =    diversity_loss_weight

												# ctc loss
												SCREAMING_SNAKE_CASE           =    ctc_loss_reduction
												SCREAMING_SNAKE_CASE           =    ctc_zero_infinity

												# SequenceClassification-specific parameter. Feel free to ignore for other classes.
												SCREAMING_SNAKE_CASE           =    classifier_proj_size

												# XVector-specific parameters. Feel free to ignore for other classes.
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    list(_UpperCamelCase )
												SCREAMING_SNAKE_CASE           =    xvector_output_dim





							@property
							def 		__snake_case(  self				:   Tuple )   ->			str:



												'''simple docstring'''

												return functools.reduce(operator.mul		,							self.conv_stride		,							1 )




