



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       =						logging.get_logger(__name__)

# General docstring
__A       =						'RegNetConfig'

# Base docstring
__A       =						'facebook/regnet-y-040'
__A       =						[1, 1088, 7, 7]

# Image classification docstring
__A       =						'facebook/regnet-y-040'
__A       =						'tabby, tabby cat'

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






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


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




	def   _SCREAMING_SNAKE_CASE		(     self     :						Optional[Any]						,  __UpperCAmelCase     :						List[Any]					)      ->			Tuple:
							UpperCAmelCase_=       self.convolution(self.padding(_snake_case					)					)
							UpperCAmelCase_=       self.normalization(_snake_case					)
							UpperCAmelCase_=       self.activation(_snake_case					)
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						str						,  __UpperCAmelCase     :						Union[str, Any]						,  **__UpperCAmelCase     :						Dict					)      ->			str:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       config.num_channels
							UpperCAmelCase_=       TFRegNetConvLayer(
							    out_channels=config.embedding_size						,  kernel_size=3						,  stride=2						,  activation=config.hidden_act						,  name="""embedder"""						,  )




	def   _SCREAMING_SNAKE_CASE		(     self     :						List[Any]						,  __UpperCAmelCase     :						Dict					)      ->			Optional[Any]:
							UpperCAmelCase_=       shape_list(_snake_case					)[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)
							UpperCAmelCase_=       tf.transpose(_snake_case						,  perm=(0, 2, 3, 1)					)
							UpperCAmelCase_=       self.embedder(_snake_case					)
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						Union[str, Any]						,  __UpperCAmelCase     :						Dict						,  __UpperCAmelCase     :						Optional[int] = 2						,  **__UpperCAmelCase     :						int					)      ->			List[Any]:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       tf.keras.layers.ConvaD(
							    filters=_snake_case						,  kernel_size=1						,  strides=_snake_case						,  use_bias=_snake_case						,  name="""convolution"""					)
							UpperCAmelCase_=       tf.keras.layers.BatchNormalization(epsilon=1E-5						,  momentum=0.9						,  name="""normalization"""					)




	def   _SCREAMING_SNAKE_CASE		(     self     :						List[Any]						,  __UpperCAmelCase     :						str						,  __UpperCAmelCase     :						Optional[Any] = False					)      ->			tf.Tensor:
							return self.normalization(self.convolution(_snake_case					)						,  training=_snake_case					)






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						Optional[int]						,  __UpperCAmelCase     :						Union[str, Any]						,  __UpperCAmelCase     :						Tuple						,  **__UpperCAmelCase     :						Optional[Any]					)      ->			Optional[int]:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       tf.keras.layers.GlobalAveragePoolingaD(keepdims=_snake_case						,  name="""pooler"""					)
							UpperCAmelCase_=       [
							    tf.keras.layers.ConvaD(filters=_snake_case						,  kernel_size=1						,  activation="""relu"""						,  name="""attention.0"""					),
							    tf.keras.layers.ConvaD(filters=_snake_case						,  kernel_size=1						,  activation="""sigmoid"""						,  name="""attention.2"""					),
							]




	def   _SCREAMING_SNAKE_CASE		(     self     :						Dict						,  __UpperCAmelCase     :						int					)      ->			Optional[Any]:
							UpperCAmelCase_=       self.pooler(_snake_case					)
							for layer_module in self.attention:
													UpperCAmelCase_=       layer_module(_snake_case					)
							UpperCAmelCase_=       hidden_state * pooled
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						Any						,  __UpperCAmelCase     :						Dict						,  __UpperCAmelCase     :						Dict						,  __UpperCAmelCase     :						List[str]						,  __UpperCAmelCase     :						Union[str, Any] = 1						,  **__UpperCAmelCase     :						Union[str, Any]					)      ->			List[Any]:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       in_channels != out_channels or stride != 1
							UpperCAmelCase_=       max(1						,  out_channels // config.groups_width					)
							UpperCAmelCase_=       (
							    TFRegNetShortCut(_snake_case						,  stride=_snake_case						,  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.
							UpperCAmelCase_=       [
							    TFRegNetConvLayer(_snake_case						,  kernel_size=1						,  activation=config.hidden_act						,  name="""layer.0"""					),
							    TFRegNetConvLayer(
							        _snake_case						,  stride=_snake_case						,  groups=_snake_case						,  activation=config.hidden_act						,  name="""layer.1"""					),
							    TFRegNetConvLayer(_snake_case						,  kernel_size=1						,  activation=_snake_case						,  name="""layer.2"""					),
							]
							UpperCAmelCase_=       ACTaFN[config.hidden_act]




	def   _SCREAMING_SNAKE_CASE		(     self     :						Optional[Any]						,  __UpperCAmelCase     :						Any					)      ->			str:
							UpperCAmelCase_=       hidden_state
							for layer_module in self.layers:
													UpperCAmelCase_=       layer_module(_snake_case					)
							UpperCAmelCase_=       self.shortcut(_snake_case					)
							hidden_state += residual
							UpperCAmelCase_=       self.activation(_snake_case					)
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						Union[str, Any]						,  __UpperCAmelCase     :						Union[str, Any]						,  __UpperCAmelCase     :						Union[str, Any]						,  __UpperCAmelCase     :						List[Any]						,  __UpperCAmelCase     :						str = 1						,  **__UpperCAmelCase     :						Union[str, Any]					)      ->			Any:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       in_channels != out_channels or stride != 1
							UpperCAmelCase_=       max(1						,  out_channels // config.groups_width					)
							UpperCAmelCase_=       (
							    TFRegNetShortCut(_snake_case						,  stride=_snake_case						,  name="""shortcut"""					)
							    if should_apply_shortcut
							    else tf.keras.layers.Activation("""linear"""						,  name="""shortcut"""					)
							)
							UpperCAmelCase_=       [
							    TFRegNetConvLayer(_snake_case						,  kernel_size=1						,  activation=config.hidden_act						,  name="""layer.0"""					),
							    TFRegNetConvLayer(
							        _snake_case						,  stride=_snake_case						,  groups=_snake_case						,  activation=config.hidden_act						,  name="""layer.1"""					),
							    TFRegNetSELayer(_snake_case						,  reduced_channels=int(round(in_channels / 4					)					)						,  name="""layer.2"""					),
							    TFRegNetConvLayer(_snake_case						,  kernel_size=1						,  activation=_snake_case						,  name="""layer.3"""					),
							]
							UpperCAmelCase_=       ACTaFN[config.hidden_act]




	def   _SCREAMING_SNAKE_CASE		(     self     :						List[str]						,  __UpperCAmelCase     :						Dict					)      ->			int:
							UpperCAmelCase_=       hidden_state
							for layer_module in self.layers:
													UpperCAmelCase_=       layer_module(_snake_case					)
							UpperCAmelCase_=       self.shortcut(_snake_case					)
							hidden_state += residual
							UpperCAmelCase_=       self.activation(_snake_case					)
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						Optional[Any]						,  __UpperCAmelCase     :						Dict						,  __UpperCAmelCase     :						Any						,  __UpperCAmelCase     :						List[str]						,  __UpperCAmelCase     :						List[Any] = 2						,  __UpperCAmelCase     :						int = 2						,  **__UpperCAmelCase     :						Any					)      ->			Union[str, Any]:
							super().__init__(**_snake_case					)

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




	def   _SCREAMING_SNAKE_CASE		(     self     :						Optional[Any]						,  __UpperCAmelCase     :						Union[str, Any]					)      ->			Union[str, Any]:




							for layer_module in self.layers:
													UpperCAmelCase_=       layer_module(_snake_case					)
							return hidden_state






class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	def __init__(     self     :						List[str]						,  __UpperCAmelCase     :						Union[str, Any]						,  **__UpperCAmelCase     :						List[str]					)      ->			Tuple:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       []
							# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
							self.stages.append(
							    TFRegNetStage(
							        _snake_case						,  config.embedding_size						,  config.hidden_sizes[0]						,  stride=2 if config.downsample_in_first_stage else 1						,  depth=config.depths[0]						,  name="""stages.0"""						,  )					)
							UpperCAmelCase_=       zip(config.hidden_sizes						,  config.hidden_sizes[1:]					)
							for i, ((in_channels, out_channels), depth) in enumerate(zip(_snake_case						,  config.depths[1:]					)					):
													self.stages.append(TFRegNetStage(_snake_case						,  _snake_case						,  _snake_case						,  depth=_snake_case						,  name=F"""stages.{i+1}"""					)					)




	def   _SCREAMING_SNAKE_CASE		(     self     :						Union[str, Any]						,  __UpperCAmelCase     :						Any						,  __UpperCAmelCase     :						Tuple = False						,  __UpperCAmelCase     :						Any = True					)      ->			TFBaseModelOutputWithNoAttention:
							UpperCAmelCase_=       () if output_hidden_states else None

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

													UpperCAmelCase_=       stage_module(_snake_case					)

							if output_hidden_states:
													UpperCAmelCase_=       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=_snake_case						,  hidden_states=_snake_case					)






@keras_serializable
class 		lowercase						(			tf.keras.layers.Layer):






	"""simple docstring"""


	a__				: List[str]		       =							RegNetConfig




	def __init__(     self     :						str						,  __UpperCAmelCase     :						List[str]						,  **__UpperCAmelCase     :						Optional[int]					)      ->			Union[str, Any]:
							super().__init__(**_snake_case					)
							UpperCAmelCase_=       config
							UpperCAmelCase_=       TFRegNetEmbeddings(_snake_case						,  name="""embedder"""					)
							UpperCAmelCase_=       TFRegNetEncoder(_snake_case						,  name="""encoder"""					)
							UpperCAmelCase_=       tf.keras.layers.GlobalAveragePoolingaD(keepdims=_snake_case						,  name="""pooler"""					)




	@unpack_inputs
	def   _SCREAMING_SNAKE_CASE		(     self     :						List[Any]						,  __UpperCAmelCase     :						List[str]						,  __UpperCAmelCase     :						Any = None						,  __UpperCAmelCase     :						Any = None						,  __UpperCAmelCase     :						int = False						,  )      ->			TFBaseModelOutputWithPoolingAndNoAttention:
							UpperCAmelCase_=       (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCAmelCase_=       return_dict if return_dict is not None else self.config.use_return_dict

							UpperCAmelCase_=       self.embedder(_snake_case						,  training=_snake_case					)

							UpperCAmelCase_=       self.encoder(
							    _snake_case						,  output_hidden_states=_snake_case						,  return_dict=_snake_case						,  training=_snake_case					)

							UpperCAmelCase_=       encoder_outputs[0]
							UpperCAmelCase_=       self.pooler(_snake_case					)

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

							# Change the other hidden state outputs to NCHW as well
							if output_hidden_states:
													UpperCAmelCase_=       tuple([tf.transpose(_snake_case						,  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=_snake_case						,  pooler_output=_snake_case						,  hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states						,  )






class 		lowercase						(			snake_case__):






	"""simple docstring"""


	a__				: Optional[int]		       =							RegNetConfig
	a__				: str		       =							"regnet"
	a__				: List[Any]		       =							"pixel_values"




	@property
	def   _SCREAMING_SNAKE_CASE		(     self     :						Dict					)      ->			Optional[int]:
							return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)						,  dtype=tf.floataa					)}


__A       =						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       =						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."			,   snake_case__			,   )
class 		lowercase						(			snake_case__):






	"""simple docstring"""


	def __init__(     self     :						Union[str, Any]						,  __UpperCAmelCase     :						List[Any]						,  *__UpperCAmelCase     :						Union[str, Any]						,  **__UpperCAmelCase     :						List[Any]					)      ->			str:
							super().__init__(_snake_case						,  *_snake_case						,  **_snake_case					)
							UpperCAmelCase_=       TFRegNetMainLayer(_snake_case						,  name="""regnet"""					)




	@unpack_inputs
	@add_start_docstrings_to_model_forward(_snake_case					)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC						,  output_type=_snake_case						,  config_class=_CONFIG_FOR_DOC						,  modality="""vision"""						,  expected_output=_EXPECTED_OUTPUT_SHAPE						,  )
	def   _SCREAMING_SNAKE_CASE		(     self     :						int						,  __UpperCAmelCase     :						Any						,  __UpperCAmelCase     :						List[str] = None						,  __UpperCAmelCase     :						List[str] = None						,  __UpperCAmelCase     :						List[Any]=False						,  )      ->			Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:
							UpperCAmelCase_=       (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCAmelCase_=       return_dict if return_dict is not None else self.config.use_return_dict

							UpperCAmelCase_=       self.regnet(
							    pixel_values=_snake_case						,  output_hidden_states=_snake_case						,  return_dict=_snake_case						,  training=_snake_case						,  )
							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(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "			,   snake_case__			,   )
class 		lowercase						(			snake_case__			,   snake_case__):






	"""simple docstring"""


	def __init__(     self     :						Optional[Any]						,  __UpperCAmelCase     :						Union[str, Any]						,  *__UpperCAmelCase     :						Union[str, Any]						,  **__UpperCAmelCase     :						Optional[Any]					)      ->			int:
							super().__init__(_snake_case						,  *_snake_case						,  **_snake_case					)
							UpperCAmelCase_=       config.num_labels
							UpperCAmelCase_=       TFRegNetMainLayer(_snake_case						,  name="""regnet"""					)
							# classification head
							UpperCAmelCase_=       [
							    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(_snake_case					)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT						,  output_type=_snake_case						,  config_class=_CONFIG_FOR_DOC						,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,  )
	def   _SCREAMING_SNAKE_CASE		(     self     :						Optional[int]						,  __UpperCAmelCase     :						Any = None						,  __UpperCAmelCase     :						Tuple = None						,  __UpperCAmelCase     :						Union[str, Any] = None						,  __UpperCAmelCase     :						List[str] = None						,  __UpperCAmelCase     :						str=False						,  )      ->			Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:
							UpperCAmelCase_=       (
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCAmelCase_=       return_dict if return_dict is not None else self.config.use_return_dict

							UpperCAmelCase_=       self.regnet(
							    _snake_case						,  output_hidden_states=_snake_case						,  return_dict=_snake_case						,  training=_snake_case					)

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

							UpperCAmelCase_=       self.classifier[0](_snake_case					)
							UpperCAmelCase_=       self.classifier[1](_snake_case					)

							UpperCAmelCase_=       None if labels is None else self.hf_compute_loss(labels=_snake_case						,  logits=_snake_case					)

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

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









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

import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextModel,
    CLIPTokenizer,
    WhisperForConditionalGeneration,
    WhisperProcessor,
)

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import logging


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






class 		lowercase						(			snake_case__):






	"""simple docstring"""


	def __init__(     self     :						Tuple						,  __UpperCAmelCase     :						WhisperForConditionalGeneration						,  __UpperCAmelCase     :						WhisperProcessor						,  __UpperCAmelCase     :						AutoencoderKL						,  __UpperCAmelCase     :						CLIPTextModel						,  __UpperCAmelCase     :						CLIPTokenizer						,  __UpperCAmelCase     :						UNetaDConditionModel						,  __UpperCAmelCase     :						Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]						,  __UpperCAmelCase     :						StableDiffusionSafetyChecker						,  __UpperCAmelCase     :						CLIPImageProcessor						,  )      ->			List[str]:
							super().__init__()

							if safety_checker is None:
													logger.warning(
													    F"""You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure"""
													    """ that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"""
													    """ results in services or applications open to the public. Both the diffusers team and Hugging Face"""
													    """ strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling"""
													    """ it only for use-cases that involve analyzing network behavior or auditing its results. For more"""
													    """ information, please have a look at https://github.com/huggingface/diffusers/pull/254 ."""					)

							self.register_modules(
							    speech_model=__UpperCAmelCase						,  speech_processor=__UpperCAmelCase						,  vae=__UpperCAmelCase						,  text_encoder=__UpperCAmelCase						,  tokenizer=__UpperCAmelCase						,  unet=__UpperCAmelCase						,  scheduler=__UpperCAmelCase						,  feature_extractor=__UpperCAmelCase						,  )




	def   _SCREAMING_SNAKE_CASE		(     self     :						Union[str, Any]						,  __UpperCAmelCase     :						Optional[Union[str, int]] = "auto"					)      ->			List[Any]:
							if slice_size == "auto":
													UpperCAmelCase_=       self.unet.config.attention_head_dim // 2
							self.unet.set_attention_slice(__UpperCAmelCase					)




	def   _SCREAMING_SNAKE_CASE		(     self     :						Any					)      ->			Optional[Any]:
							self.enable_attention_slicing(__UpperCAmelCase					)




	@torch.no_grad()
	def __call__(     self     :						str						,  __UpperCAmelCase     :						Optional[Any]						,  __UpperCAmelCase     :						str=16_000						,  __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     :						Union[str, Any]						,  )      ->			Any:
							UpperCAmelCase_=       self.speech_processor.feature_extractor(
							    __UpperCAmelCase						,  return_tensors="""pt"""						,  sampling_rate=__UpperCAmelCase					).input_features.to(self.device					)
							UpperCAmelCase_=       self.speech_model.generate(__UpperCAmelCase						,  max_length=480_000					)

							UpperCAmelCase_=       self.speech_processor.tokenizer.batch_decode(__UpperCAmelCase						,  skip_special_tokens=__UpperCAmelCase						,  normalize=__UpperCAmelCase					)[
							    0
							]

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

							if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
													UpperCAmelCase_=       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_=       text_input_ids[:, : self.tokenizer.model_max_length]
							UpperCAmelCase_=       self.text_encoder(text_input_ids.to(self.device					)					)[0]

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

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

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

							# 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
							UpperCAmelCase_=       self.scheduler.timesteps.to(self.device					)

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

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

													# predict the noise residual
													UpperCAmelCase_=       self.unet(__UpperCAmelCase						,  __UpperCAmelCase						,  encoder_hidden_states=__UpperCAmelCase					).sample

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

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

							UpperCAmelCase_=       1 / 0.18_215 * latents
							UpperCAmelCase_=       self.vae.decode(__UpperCAmelCase					).sample

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

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

							if output_type == "pil":
													UpperCAmelCase_=       self.numpy_to_pil(__UpperCAmelCase					)

							if not return_dict:
													return image

							return StableDiffusionPipelineOutput(images=__UpperCAmelCase						,  nsfw_content_detected=__UpperCAmelCase					)




