


from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


A	:		Dict    =       logging.get_logger(__name__)

# General docstring
A	:		str    =       'RegNetConfig'

# Base docstring
A	:		Optional[Any]    =       'facebook/regnet-y-040'
A	:		Optional[Any]    =       [1, 1_0_8_8, 7, 7]

# Image classification docstring
A	:		Optional[Any]    =       'facebook/regnet-y-040'
A	:		Any    =       'tabby, tabby cat'

A	:		List[Any]    =       [
    'facebook/regnet-y-040',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]



class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						str				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int = 3				, _UpperCAmelCase   :						int = 1				, _UpperCAmelCase   :						int = 1				, _UpperCAmelCase   :						Optional[str] = "relu"				, **_UpperCAmelCase   :						int				, )						->       Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							# The padding and conv has been verified in
							# https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
							lowercase__					      =		tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2		)
							lowercase__					      =		tf.keras.layers.ConvaD(
							    filters=_UpperCAmelCase				, kernel_size=_UpperCAmelCase				, strides=_UpperCAmelCase				, padding="""VALID"""				, groups=_UpperCAmelCase				, use_bias=_UpperCAmelCase				, name="""convolution"""				, )
							lowercase__					      =		tf.keras.layers.BatchNormalization(epsilon=1E-5				, momentum=0.9				, name="""normalization"""		)
							lowercase__					      =		ACTaFN[activation] if activation is not None else tf.identity


					def   lowerCamelCase__					(self   :						Dict				, _UpperCAmelCase   :						List[Any]		)						->       Optional[Any]:




							"""simple docstring"""


							lowercase__					      =		self.convolution(self.padding(_UpperCAmelCase		)		)
							lowercase__					      =		self.normalization(_UpperCAmelCase		)
							lowercase__					      =		self.activation(_UpperCAmelCase		)
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						Tuple				, _UpperCAmelCase   :						RegNetConfig				, **_UpperCAmelCase   :						Optional[Any]		)						->       Union[str, Any]:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		config.num_channels
							lowercase__					      =		TFRegNetConvLayer(
							    out_channels=config.embedding_size				, kernel_size=3				, stride=2				, activation=config.hidden_act				, name="""embedder"""				, )


					def   lowerCamelCase__					(self   :						str				, _UpperCAmelCase   :						List[Any]		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		shape_list(_UpperCAmelCase		)[1]
							if tf.executing_eagerly() and num_channels != self.num_channels:
									raise ValueError(
									    """Make sure that the channel dimension of the pixel values match with the one set in the configuration."""		)

							# When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
							# So change the input format from `NCHW` to `NHWC`.
							# shape = (batch_size, in_height, in_width, in_channels=num_channels)
							lowercase__					      =		tf.transpose(_UpperCAmelCase				, perm=(0, 2, 3, 1)		)
							lowercase__					      =		self.embedder(_UpperCAmelCase		)
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						int				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int = 2				, **_UpperCAmelCase   :						Optional[int]		)						->       Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		tf.keras.layers.ConvaD(
							    filters=_UpperCAmelCase				, kernel_size=1				, strides=_UpperCAmelCase				, use_bias=_UpperCAmelCase				, name="""convolution"""		)
							lowercase__					      =		tf.keras.layers.BatchNormalization(epsilon=1E-5				, momentum=0.9				, name="""normalization"""		)


					def   lowerCamelCase__					(self   :						str				, _UpperCAmelCase   :						tf.Tensor				, _UpperCAmelCase   :						bool = False		)						->       tf.Tensor:




							"""simple docstring"""


							return self.normalization(self.convolution(_UpperCAmelCase		)				, training=_UpperCAmelCase		)






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						List[Any]				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int				, **_UpperCAmelCase   :						int		)						->       Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		tf.keras.layers.GlobalAveragePoolingaD(keepdims=_UpperCAmelCase				, name="""pooler"""		)
							lowercase__					      =		[
							    tf.keras.layers.ConvaD(filters=_UpperCAmelCase				, kernel_size=1				, activation="""relu"""				, name="""attention.0"""		),
							    tf.keras.layers.ConvaD(filters=_UpperCAmelCase				, kernel_size=1				, activation="""sigmoid"""				, name="""attention.2"""		),
							]


					def   lowerCamelCase__					(self   :						int				, _UpperCAmelCase   :						List[str]		)						->       Union[str, Any]:




							"""simple docstring"""


							lowercase__					      =		self.pooler(_UpperCAmelCase		)
							for layer_module in self.attention:
									lowercase__					      =		layer_module(_UpperCAmelCase		)
							lowercase__					      =		hidden_state * pooled
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						int				, _UpperCAmelCase   :						RegNetConfig				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int = 1				, **_UpperCAmelCase   :						List[Any]		)						->       str:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		in_channels != out_channels or stride != 1
							lowercase__					      =		max(1				, out_channels // config.groups_width		)
							lowercase__					      =		(
							    TFRegNetShortCut(_UpperCAmelCase				, stride=_UpperCAmelCase				, name="""shortcut"""		)
							    if should_apply_shortcut
							    else tf.keras.layers.Activation("""linear"""				, name="""shortcut"""		)
							)
							# `self.layers` instead of `self.layer` because that is a reserved argument.
							lowercase__					      =		[
							    TFRegNetConvLayer(_UpperCAmelCase				, kernel_size=1				, activation=config.hidden_act				, name="""layer.0"""		),
							    TFRegNetConvLayer(
							        _UpperCAmelCase				, stride=_UpperCAmelCase				, groups=_UpperCAmelCase				, activation=config.hidden_act				, name="""layer.1"""		),
							    TFRegNetConvLayer(_UpperCAmelCase				, kernel_size=1				, activation=_UpperCAmelCase				, name="""layer.2"""		),
							]
							lowercase__					      =		ACTaFN[config.hidden_act]


					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						str		)						->       List[Any]:




							"""simple docstring"""


							lowercase__					      =		hidden_state
							for layer_module in self.layers:
									lowercase__					      =		layer_module(_UpperCAmelCase		)
							lowercase__					      =		self.shortcut(_UpperCAmelCase		)
							hidden_state += residual
							lowercase__					      =		self.activation(_UpperCAmelCase		)
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						Dict				, _UpperCAmelCase   :						RegNetConfig				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int = 1				, **_UpperCAmelCase   :						str		)						->       str:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		in_channels != out_channels or stride != 1
							lowercase__					      =		max(1				, out_channels // config.groups_width		)
							lowercase__					      =		(
							    TFRegNetShortCut(_UpperCAmelCase				, stride=_UpperCAmelCase				, name="""shortcut"""		)
							    if should_apply_shortcut
							    else tf.keras.layers.Activation("""linear"""				, name="""shortcut"""		)
							)
							lowercase__					      =		[
							    TFRegNetConvLayer(_UpperCAmelCase				, kernel_size=1				, activation=config.hidden_act				, name="""layer.0"""		),
							    TFRegNetConvLayer(
							        _UpperCAmelCase				, stride=_UpperCAmelCase				, groups=_UpperCAmelCase				, activation=config.hidden_act				, name="""layer.1"""		),
							    TFRegNetSELayer(_UpperCAmelCase				, reduced_channels=int(round(in_channels / 4		)		)				, name="""layer.2"""		),
							    TFRegNetConvLayer(_UpperCAmelCase				, kernel_size=1				, activation=_UpperCAmelCase				, name="""layer.3"""		),
							]
							lowercase__					      =		ACTaFN[config.hidden_act]


					def   lowerCamelCase__					(self   :						int				, _UpperCAmelCase   :						int		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		hidden_state
							for layer_module in self.layers:
									lowercase__					      =		layer_module(_UpperCAmelCase		)
							lowercase__					      =		self.shortcut(_UpperCAmelCase		)
							hidden_state += residual
							lowercase__					      =		self.activation(_UpperCAmelCase		)
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						List[str]				, _UpperCAmelCase   :						RegNetConfig				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int				, _UpperCAmelCase   :						int = 2				, _UpperCAmelCase   :						int = 2				, **_UpperCAmelCase   :						str		)						->       Union[str, Any]:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)

							lowercase__					      =		TFRegNetXLayer if config.layer_type == """x""" else TFRegNetYLayer
							lowercase__					      =		[
							    # downsampling is done in the first layer with stride of 2
							    layer(_UpperCAmelCase				, _UpperCAmelCase				, _UpperCAmelCase				, stride=_UpperCAmelCase				, name="""layers.0"""		),
							    *[layer(_UpperCAmelCase				, _UpperCAmelCase				, _UpperCAmelCase				, name=f'''layers.{i+1}'''		) for i in range(depth - 1		)],
							]


					def   lowerCamelCase__					(self   :						Any				, _UpperCAmelCase   :						Any		)						->       Dict:




							"""simple docstring"""


							for layer_module in self.layers:
									lowercase__					      =		layer_module(_UpperCAmelCase		)
							return hidden_state






class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					def __init__(self   :						Union[str, Any]				, _UpperCAmelCase   :						RegNetConfig				, **_UpperCAmelCase   :						Dict		)						->       Union[str, Any]:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		[]
							# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
							self.stages.append(
							    TFRegNetStage(
							        _UpperCAmelCase				, config.embedding_size				, config.hidden_sizes[0]				, stride=2 if config.downsample_in_first_stage else 1				, depth=config.depths[0]				, name="""stages.0"""				, )		)
							lowercase__					      =		zip(config.hidden_sizes				, config.hidden_sizes[1:]		)
							for i, ((in_channels, out_channels), depth) in enumerate(zip(_UpperCAmelCase				, config.depths[1:]		)		):
									self.stages.append(TFRegNetStage(_UpperCAmelCase				, _UpperCAmelCase				, _UpperCAmelCase				, depth=_UpperCAmelCase				, name=f'''stages.{i+1}'''		)		)


					def   lowerCamelCase__					(self   :						List[str]				, _UpperCAmelCase   :						tf.Tensor				, _UpperCAmelCase   :						bool = False				, _UpperCAmelCase   :						bool = True		)						->       TFBaseModelOutputWithNoAttention:




							"""simple docstring"""


							lowercase__					      =		() if output_hidden_states else None

							for stage_module in self.stages:
									if output_hidden_states:
											lowercase__					      =		hidden_states + (hidden_state,)

									lowercase__					      =		stage_module(_UpperCAmelCase		)

							if output_hidden_states:
									lowercase__					      =		hidden_states + (hidden_state,)

							if not return_dict:
									return tuple(v for v in [hidden_state, hidden_states] if v is not None		)

							return TFBaseModelOutputWithNoAttention(last_hidden_state=_UpperCAmelCase				, hidden_states=_UpperCAmelCase		)






@keras_serializable
class 							A   (	tf.keras.layers.Layer							):
					'''simple docstring'''






					A__      =			RegNetConfig

					def __init__(self   :						Optional[Any]				, _UpperCAmelCase   :						str				, **_UpperCAmelCase   :						Any		)						->       List[Any]:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase		)
							lowercase__					      =		config
							lowercase__					      =		TFRegNetEmbeddings(_UpperCAmelCase				, name="""embedder"""		)
							lowercase__					      =		TFRegNetEncoder(_UpperCAmelCase				, name="""encoder"""		)
							lowercase__					      =		tf.keras.layers.GlobalAveragePoolingaD(keepdims=_UpperCAmelCase				, name="""pooler"""		)


					@unpack_inputs
					def   lowerCamelCase__					(self   :						List[Any]				, _UpperCAmelCase   :						tf.Tensor				, _UpperCAmelCase   :						Optional[bool] = None				, _UpperCAmelCase   :						Optional[bool] = None				, _UpperCAmelCase   :						bool = False				, )						->       TFBaseModelOutputWithPoolingAndNoAttention:




							"""simple docstring"""


							lowercase__					      =		(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowercase__					      =		return_dict if return_dict is not None else self.config.use_return_dict

							lowercase__					      =		self.embedder(_UpperCAmelCase				, training=_UpperCAmelCase		)

							lowercase__					      =		self.encoder(
							    _UpperCAmelCase				, output_hidden_states=_UpperCAmelCase				, return_dict=_UpperCAmelCase				, training=_UpperCAmelCase		)

							lowercase__					      =		encoder_outputs[0]
							lowercase__					      =		self.pooler(_UpperCAmelCase		)

							# Change to NCHW output format have uniformity in the modules
							lowercase__					      =		tf.transpose(_UpperCAmelCase				, perm=(0, 3, 1, 2)		)
							lowercase__					      =		tf.transpose(_UpperCAmelCase				, perm=(0, 3, 1, 2)		)

							# Change the other hidden state outputs to NCHW as well
							if output_hidden_states:
									lowercase__					      =		tuple([tf.transpose(_UpperCAmelCase				, perm=(0, 3, 1, 2)		) for h in encoder_outputs[1]]		)

							if not return_dict:
									return (last_hidden_state, pooled_output) + encoder_outputs[1:]

							return TFBaseModelOutputWithPoolingAndNoAttention(
							    last_hidden_state=_UpperCAmelCase				, pooler_output=_UpperCAmelCase				, hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states				, )






class 							A   (	UpperCAmelCase__							):
					'''simple docstring'''






					A__      =			RegNetConfig
					A__      =			'''regnet'''
					A__      =			'''pixel_values'''

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




							"""simple docstring"""


							return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)				, dtype=tf.floataa		)}






A	:		List[Any]    =       r'\n    Parameters:\n    This model is a Tensorflow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n    behavior.\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n'

A	:		Optional[Any]    =       r'\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n'



@add_start_docstrings(
    '''The bare RegNet model outputting raw features without any specific head on top.'''			,  UpperCAmelCase__			,  )
class 							A   (	UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self   :						Tuple				, _UpperCAmelCase   :						RegNetConfig				, *_UpperCAmelCase   :						Dict				, **_UpperCAmelCase   :						Any		)						->       Optional[Any]:




							"""simple docstring"""


							super().__init__(_UpperCAmelCase				, *_UpperCAmelCase				, **_UpperCAmelCase		)
							lowercase__					      =		TFRegNetMainLayer(_UpperCAmelCase				, name="""regnet"""		)


					@unpack_inputs
					@add_start_docstrings_to_model_forward(_UpperCAmelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC				, output_type=_UpperCAmelCase				, config_class=_CONFIG_FOR_DOC				, modality="""vision"""				, expected_output=_EXPECTED_OUTPUT_SHAPE				, )
					def   lowerCamelCase__					(self   :						List[str]				, _UpperCAmelCase   :						tf.Tensor				, _UpperCAmelCase   :						Optional[bool] = None				, _UpperCAmelCase   :						Optional[bool] = None				, _UpperCAmelCase   :						List[Any]=False				, )						->       Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:




							"""simple docstring"""


							lowercase__					      =		(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowercase__					      =		return_dict if return_dict is not None else self.config.use_return_dict

							lowercase__					      =		self.regnet(
							    pixel_values=_UpperCAmelCase				, output_hidden_states=_UpperCAmelCase				, return_dict=_UpperCAmelCase				, training=_UpperCAmelCase				, )
							if not return_dict:
									return (outputs[0],) + outputs[1:]

							return TFBaseModelOutputWithPoolingAndNoAttention(
							    last_hidden_state=outputs.last_hidden_state				, pooler_output=outputs.pooler_output				, hidden_states=outputs.hidden_states				, )






@add_start_docstrings(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''			,  UpperCAmelCase__			,  )
class 							A   (	UpperCAmelCase__			,  UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self   :						List[str]				, _UpperCAmelCase   :						RegNetConfig				, *_UpperCAmelCase   :						Any				, **_UpperCAmelCase   :						str		)						->       str:




							"""simple docstring"""


							super().__init__(_UpperCAmelCase				, *_UpperCAmelCase				, **_UpperCAmelCase		)
							lowercase__					      =		config.num_labels
							lowercase__					      =		TFRegNetMainLayer(_UpperCAmelCase				, name="""regnet"""		)
							# classification head
							lowercase__					      =		[
							    tf.keras.layers.Flatten(),
							    tf.keras.layers.Dense(config.num_labels				, name="""classifier.1"""		) if config.num_labels > 0 else tf.identity,
							]


					@unpack_inputs
					@add_start_docstrings_to_model_forward(_UpperCAmelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT				, output_type=_UpperCAmelCase				, config_class=_CONFIG_FOR_DOC				, expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT				, )
					def   lowerCamelCase__					(self   :						Optional[int]				, _UpperCAmelCase   :						tf.Tensor = None				, _UpperCAmelCase   :						tf.Tensor = None				, _UpperCAmelCase   :						bool = None				, _UpperCAmelCase   :						bool = None				, _UpperCAmelCase   :						Optional[int]=False				, )						->       Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:




							"""simple docstring"""


							lowercase__					      =		(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							lowercase__					      =		return_dict if return_dict is not None else self.config.use_return_dict

							lowercase__					      =		self.regnet(
							    _UpperCAmelCase				, output_hidden_states=_UpperCAmelCase				, return_dict=_UpperCAmelCase				, training=_UpperCAmelCase		)

							lowercase__					      =		outputs.pooler_output if return_dict else outputs[1]

							lowercase__					      =		self.classifier[0](_UpperCAmelCase		)
							lowercase__					      =		self.classifier[1](_UpperCAmelCase		)

							lowercase__					      =		None if labels is None else self.hf_compute_loss(labels=_UpperCAmelCase				, logits=_UpperCAmelCase		)

							if not return_dict:
									lowercase__					      =		(logits,) + outputs[2:]
									return ((loss,) + output) if loss is not None else output

							return TFSequenceClassifierOutput(loss=_UpperCAmelCase				, logits=_UpperCAmelCase				, hidden_states=outputs.hidden_states		)





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


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



class 							A   (	UpperCAmelCase__							):
					'''simple docstring'''






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




							"""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   lowerCamelCase__					(self   :						Optional[Any]				, _UpperCAmelCase   :						Optional[Union[str, int]] = "auto"		)						->       Tuple:




							"""simple docstring"""


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

					def   lowerCamelCase__					(self   :						Optional[Any]		)						->       Optional[Any]:




							"""simple docstring"""


							self.enable_attention_slicing(_UpperCAmelCase		)


					@torch.no_grad()
					def __call__(self   :						Any				, _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   :						Any				, )						->       Tuple:




							"""simple docstring"""


							if isinstance(_UpperCAmelCase				, _UpperCAmelCase		):
									lowercase__					      =		1
							elif isinstance(_UpperCAmelCase				, _UpperCAmelCase		):
									lowercase__					      =		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
							lowercase__					      =		self.tokenizer(
							    _UpperCAmelCase				, padding="""max_length"""				, max_length=self.tokenizer.model_max_length				, return_tensors="""pt"""				, )
							lowercase__					      =		text_inputs.input_ids

							if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
									lowercase__					      =		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}'''		)
									lowercase__					      =		text_input_ids[:, : self.tokenizer.model_max_length]

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

							# duplicate text embeddings for each generation per prompt, using mps friendly method
							lowercase__      ,     lowercase__      ,     lowercase__					      =		text_embeddings.shape
							lowercase__					      =		text_embeddings.repeat(1				, _UpperCAmelCase				, 1		)
							lowercase__					      =		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.
							lowercase__					      =		guidance_scale > 1.0
							# get unconditional embeddings for classifier free guidance
							if do_classifier_free_guidance:
									lowercase__					      =		42
									if negative_prompt is None:
											lowercase__					      =		[""""""]
									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		):
											lowercase__					      =		[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:
											lowercase__					      =		negative_prompt

									lowercase__					      =		text_input_ids.shape[-1]
									lowercase__					      =		self.tokenizer(
									    _UpperCAmelCase				, padding="""max_length"""				, max_length=_UpperCAmelCase				, truncation=_UpperCAmelCase				, return_tensors="""pt"""				, )
									lowercase__					      =		self.text_encoder(uncond_input.input_ids.to(self.device		)		)[0]

									# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
									lowercase__					      =		uncond_embeddings.shape[1]
									lowercase__					      =		uncond_embeddings.repeat(_UpperCAmelCase				, _UpperCAmelCase				, 1		)
									lowercase__					      =		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
									lowercase__					      =		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`.
							lowercase__					      =		(batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
							lowercase__					      =		(batch_size * num_images_per_prompt, self.unet.config.in_channels, 64, 64)
							lowercase__					      =		text_embeddings.dtype
							if latents is None:
									if self.device.type == "mps":
											# randn does not exist on mps
											lowercase__					      =		torch.randn(
											    _UpperCAmelCase				, generator=_UpperCAmelCase				, device="""cpu"""				, dtype=_UpperCAmelCase		).to(self.device		)
											lowercase__					      =		torch.randn(_UpperCAmelCase				, generator=_UpperCAmelCase				, device="""cpu"""				, dtype=_UpperCAmelCase		).to(
											    self.device		)
									else:
											lowercase__					      =		torch.randn(
											    _UpperCAmelCase				, generator=_UpperCAmelCase				, device=self.device				, dtype=_UpperCAmelCase		)
											lowercase__					      =		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}'''		)
									lowercase__					      =		latents_reference.to(self.device		)
									lowercase__					      =		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
							lowercase__					      =		(latents_shape[3] - latents_shape_reference[3]) // 2
							lowercase__					      =		(latents_shape[2] - latents_shape_reference[2]) // 2
							lowercase__					      =		latents_shape_reference[3] if dx >= 0 else latents_shape_reference[3] + 2 * dx
							lowercase__					      =		latents_shape_reference[2] if dy >= 0 else latents_shape_reference[2] + 2 * dy
							lowercase__					      =		0 if dx < 0 else dx
							lowercase__					      =		0 if dy < 0 else dy
							lowercase__					      =		max(-dx				, 0		)
							lowercase__					      =		max(-dy				, 0		)
							# import pdb
							# pdb.set_trace()
							lowercase__					      =		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
							lowercase__					      =		self.scheduler.timesteps.to(self.device		)

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

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

									# predict the noise residual
									lowercase__					      =		self.unet(_UpperCAmelCase				, _UpperCAmelCase				, encoder_hidden_states=_UpperCAmelCase		).sample

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

									# compute the previous noisy sample x_t -> x_t-1
									lowercase__					      =		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		)

							lowercase__					      =		1 / 0.18_215 * latents
							lowercase__					      =		self.vae.decode(_UpperCAmelCase		).sample

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

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

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

							if output_type == "pil":
									lowercase__					      =		self.numpy_to_pil(_UpperCAmelCase		)

							if not return_dict:
									return (image, has_nsfw_concept)

							return StableDiffusionPipelineOutput(images=_UpperCAmelCase				, nsfw_content_detected=_UpperCAmelCase		)

