






import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

							if is_vision_available():
														from transformers import MaskFormerImageProcessor

if is_vision_available():
							from PIL import Image
class       _UpperCAmelCase					:

				"""simple docstring"""




				def __init__(     self						:    str,  lowerCamelCase						:    Any,  lowerCamelCase						:    Any=2,  lowerCamelCase						:    int=True,  lowerCamelCase						:    Any=False,  lowerCamelCase						:    Tuple=10,  lowerCamelCase						:    List[str]=3,  lowerCamelCase						:    Optional[Any]=32 * 4,  lowerCamelCase						:    Any=32 * 6,  lowerCamelCase						:    Union[str, Any]=4,  lowerCamelCase						:    int=32,  ):

								'''simple docstring'''
								lowercase__    =			parent
								lowercase__    =			batch_size
								lowercase__    =			is_training
								lowercase__    =			use_auxiliary_loss
								lowercase__    =			num_queries
								lowercase__    =			num_channels
								lowercase__    =			min_size
								lowercase__    =			max_size
								lowercase__    =			num_labels
								lowercase__    =			mask_feature_size




				def 				lowercase__   (     self						:    Union[str, Any]  ):

								'''simple docstring'''
								lowercase__    =			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]  ).to(
								    __UpperCamelCase  )

								lowercase__    =			torch.ones([self.batch_size, self.min_size, self.max_size],  device=__UpperCamelCase  )

								lowercase__    =			(
								    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size],  device=__UpperCamelCase  ) > 0.5
								).float()
								lowercase__    =			(torch.rand((self.batch_size, self.num_labels),  device=__UpperCamelCase  ) > 0.5).long()

								lowercase__    =			self.get_config()
								return config, pixel_values, pixel_mask, mask_labels, class_labels




				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								return MaskFormerConfig.from_backbone_and_decoder_configs(
								    backbone_config=SwinConfig(
								        depths=[1, 1, 1, 1],  ),  decoder_config=DetrConfig(
								        decoder_ffn_dim=128,  num_queries=self.num_queries,  decoder_attention_heads=2,  d_model=self.mask_feature_size,  ),  mask_feature_size=self.mask_feature_size,  fpn_feature_size=self.mask_feature_size,  num_channels=self.num_channels,  num_labels=self.num_labels,  )




				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.prepare_config_and_inputs()
								lowercase__    =			{'''pixel_values''': pixel_values, '''pixel_mask''': pixel_mask}
								return config, inputs_dict




				def 				lowercase__   (     self						:    Any,  lowerCamelCase						:    Dict,  lowerCamelCase						:    Tuple  ):

								'''simple docstring'''
								lowercase__    =			output.encoder_hidden_states
								lowercase__    =			output.pixel_decoder_hidden_states
								lowercase__    =			output.transformer_decoder_hidden_states

								self.parent.assertTrue(len(__UpperCamelCase  ),  len(config.backbone_config.depths  )  )
								self.parent.assertTrue(len(__UpperCamelCase  ),  len(config.backbone_config.depths  )  )
								self.parent.assertTrue(len(__UpperCamelCase  ),  config.decoder_config.decoder_layers  )




				def 				lowercase__   (     self						:    List[str],  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    Any,  lowerCamelCase						:    int=False  ):

								'''simple docstring'''
								with torch.no_grad():
												lowercase__    =			MaskFormerModel(config=__UpperCamelCase  )
												model.to(__UpperCamelCase  )
												model.eval()

												lowercase__    =			model(pixel_values=__UpperCamelCase,  pixel_mask=__UpperCamelCase  )
												lowercase__    =			model(__UpperCamelCase,  output_hidden_states=__UpperCamelCase  )
								# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
								# encoder and pixel decoder
								self.parent.assertEqual(
								    output.transformer_decoder_last_hidden_state.shape,  (self.batch_size, self.num_queries, self.mask_feature_size),  )
								# let's ensure the other two hidden state exists
								self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None  )
								self.parent.assertTrue(output.encoder_last_hidden_state is not None  )

								if output_hidden_states:
												self.check_output_hidden_state(__UpperCamelCase,  __UpperCamelCase  )




				def 				lowercase__   (     self						:    str,  lowerCamelCase						:    int,  lowerCamelCase						:    int,  lowerCamelCase						:    List[str],  lowerCamelCase						:    Dict,  lowerCamelCase						:    str  ):

								'''simple docstring'''
								lowercase__    =			MaskFormerForInstanceSegmentation(config=__UpperCamelCase  )
								model.to(__UpperCamelCase  )
								model.eval()

								def comm_check_on_output(lowerCamelCase						:    int  ):
												# let's still check that all the required stuff is there
												self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None  )
												self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None  )
												self.parent.assertTrue(result.encoder_last_hidden_state is not None  )
												# okay, now we need to check the logits shape
												# due to the encoder compression, masks have a //4 spatial size
												self.parent.assertEqual(
												    result.masks_queries_logits.shape,  (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4),  )
												# + 1 for null class
												self.parent.assertEqual(
												    result.class_queries_logits.shape,  (self.batch_size, self.num_queries, self.num_labels + 1)  )

								with torch.no_grad():
												lowercase__    =			model(pixel_values=__UpperCamelCase,  pixel_mask=__UpperCamelCase  )
												lowercase__    =			model(__UpperCamelCase  )

												comm_check_on_output(__UpperCamelCase  )

												lowercase__    =			model(
												    pixel_values=__UpperCamelCase,  pixel_mask=__UpperCamelCase,  mask_labels=__UpperCamelCase,  class_labels=__UpperCamelCase  )

								comm_check_on_output(__UpperCamelCase  )

								self.parent.assertTrue(result.loss is not None  )
								self.parent.assertEqual(result.loss.shape,  torch.Size([1]  )  )



@require_torch
class       _UpperCAmelCase					(       lowercase_		,lowercase_		,unittest.TestCase	):

				"""simple docstring"""




				lowercase__											=       (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
				lowercase__											=       (
				    {"""feature-extraction""": MaskFormerModel, """image-segmentation""": MaskFormerForInstanceSegmentation}
				    if is_torch_available()
				    else {}
				)

				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False




				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								lowercase__    =			MaskFormerModelTester(self  )
								lowercase__    =			ConfigTester(self,  config_class=__UpperCamelCase,  has_text_modality=__UpperCamelCase  )




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								self.config_tester.run_common_tests()




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.create_and_check_maskformer_model(__UpperCamelCase,  **__UpperCamelCase,  output_hidden_states=__UpperCamelCase  )




				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__UpperCamelCase  )




				@unittest.skip(reason='''MaskFormer does not use inputs_embeds'''  )
				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								pass




				@unittest.skip(reason='''MaskFormer does not have a get_input_embeddings method'''  )
				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								pass




				@unittest.skip(reason='''MaskFormer is not a generative model'''  )
				def 				lowercase__   (     self						:    str  ):

								'''simple docstring'''
								pass




				@unittest.skip(reason='''MaskFormer does not use token embeddings'''  )
				def 				lowercase__   (     self						:    int  ):

								'''simple docstring'''
								pass




				@require_torch_multi_gpu
				@unittest.skip(
				    reason='''MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''  )
				def 				lowercase__   (     self						:    Union[str, Any]  ):

								'''simple docstring'''
								pass




				@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''  )
				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								pass




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												lowercase__    =			model_class(__UpperCamelCase  )
												lowercase__    =			inspect.signature(model.forward  )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												lowercase__    =			[*signature.parameters.keys()]

												lowercase__    =			['''pixel_values''']
												self.assertListEqual(arg_names[:1],  __UpperCamelCase  )




				@slow
				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								for model_name in ["facebook/maskformer-swin-small-coco"]:
												lowercase__    =			MaskFormerModel.from_pretrained(__UpperCamelCase  )
												self.assertIsNotNone(__UpperCamelCase  )




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			(self.model_tester.min_size,) * 2
								lowercase__    =			{
								    '''pixel_values''': torch.randn((2, 3, *size),  device=__UpperCamelCase  ),
								    '''mask_labels''': torch.randn((2, 10, *size),  device=__UpperCamelCase  ),
								    '''class_labels''': torch.zeros(2,  10,  device=__UpperCamelCase  ).long(),
								}

								lowercase__    =			MaskFormerForInstanceSegmentation(MaskFormerConfig()  ).to(__UpperCamelCase  )
								lowercase__    =			model(**__UpperCamelCase  )
								self.assertTrue(outputs.loss is not None  )




				def 				lowercase__   (     self						:    Optional[int]  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.create_and_check_maskformer_model(__UpperCamelCase,  **__UpperCamelCase,  output_hidden_states=__UpperCamelCase  )




				def 				lowercase__   (     self						:    Dict  ):

								'''simple docstring'''
								lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												lowercase__    =			model_class(__UpperCamelCase  ).to(__UpperCamelCase  )
												lowercase__    =			model(**__UpperCamelCase,  output_attentions=__UpperCamelCase  )
												self.assertTrue(outputs.attentions is not None  )




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								if not self.model_tester.is_training:
												return
								# only MaskFormerForInstanceSegmentation has the loss
								lowercase__    =			self.all_model_classes[1]
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs()

								lowercase__    =			model_class(__UpperCamelCase  )
								model.to(__UpperCamelCase  )
								model.train()

								lowercase__    =			model(__UpperCamelCase,  mask_labels=__UpperCamelCase,  class_labels=__UpperCamelCase  ).loss
								loss.backward()




				def 				lowercase__   (     self						:    Tuple  ):

								'''simple docstring'''
								# only MaskFormerForInstanceSegmentation has the loss
								lowercase__    =			self.all_model_classes[1]
								lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__ ,						lowercase__    =			self.model_tester.prepare_config_and_inputs()
								lowercase__    =			True
								lowercase__    =			True

								lowercase__    =			model_class(__UpperCamelCase  )
								model.to(__UpperCamelCase  )
								model.train()

								lowercase__    =			model(__UpperCamelCase,  mask_labels=__UpperCamelCase,  class_labels=__UpperCamelCase  )

								lowercase__    =			outputs.encoder_hidden_states[0]
								encoder_hidden_states.retain_grad()

								lowercase__    =			outputs.pixel_decoder_hidden_states[0]
								pixel_decoder_hidden_states.retain_grad()
								# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
								lowercase__    =			outputs.transformer_decoder_hidden_states[0]
								transformer_decoder_hidden_states.retain_grad()

								lowercase__    =			outputs.attentions[0]
								attentions.retain_grad()

								outputs.loss.backward(retain_graph=__UpperCamelCase  )

								self.assertIsNotNone(encoder_hidden_states.grad  )
								self.assertIsNotNone(pixel_decoder_hidden_states.grad  )
								self.assertIsNotNone(transformer_decoder_hidden_states.grad  )
								self.assertIsNotNone(attentions.grad  )



A__	:			Tuple			   = 1e-4






def   a			(						):






				'''simple docstring'''
				lowercase__    =			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
				return image




@require_vision
@slow
class       _UpperCAmelCase					(       unittest.TestCase	):

				"""simple docstring"""




				@cached_property
				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								return (
								    MaskFormerImageProcessor.from_pretrained('''facebook/maskformer-swin-small-coco'''  )
								    if is_vision_available()
								    else None
								)




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			MaskFormerModel.from_pretrained('''facebook/maskformer-swin-small-coco'''  ).to(__UpperCamelCase  )
								lowercase__    =			self.default_image_processor
								lowercase__    =			prepare_img()
								lowercase__    =			image_processor(__UpperCamelCase,  return_tensors='''pt'''  ).to(__UpperCamelCase  )
								lowercase__    =			inputs['''pixel_values'''].shape
								# check size is divisible by 32
								self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0  )
								# check size
								self.assertEqual(__UpperCamelCase,  (1, 3, 800, 1_088)  )

								with torch.no_grad():
												lowercase__    =			model(**__UpperCamelCase  )

								lowercase__    =			torch.tensor(
								    [[-0.0482, 0.9228, 0.4951], [-0.2547, 0.8017, 0.8527], [-0.0069, 0.3385, -0.0089]]  ).to(__UpperCamelCase  )
								self.assertTrue(
								    torch.allclose(
								        outputs.encoder_last_hidden_state[0, 0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )

								lowercase__    =			torch.tensor(
								    [[-0.8422, -0.8434, -0.9718], [-1.0144, -0.5565, -0.4195], [-1.0038, -0.4484, -0.1961]]  ).to(__UpperCamelCase  )
								self.assertTrue(
								    torch.allclose(
								        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )

								lowercase__    =			torch.tensor(
								    [[0.2852, -0.0159, 0.9735], [0.6254, 0.1858, 0.8529], [-0.0680, -0.4116, 1.8413]]  ).to(__UpperCamelCase  )
								self.assertTrue(
								    torch.allclose(
								        outputs.transformer_decoder_last_hidden_state[0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )




				def 				lowercase__   (     self						:    str  ):

								'''simple docstring'''
								lowercase__    =			(
								    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''  )
								    .to(__UpperCamelCase  )
								    .eval()
								)
								lowercase__    =			self.default_image_processor
								lowercase__    =			prepare_img()
								lowercase__    =			image_processor(__UpperCamelCase,  return_tensors='''pt'''  ).to(__UpperCamelCase  )
								lowercase__    =			inputs['''pixel_values'''].shape
								# check size is divisible by 32
								self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0  )
								# check size
								self.assertEqual(__UpperCamelCase,  (1, 3, 800, 1_088)  )

								with torch.no_grad():
												lowercase__    =			model(**__UpperCamelCase  )
								# masks_queries_logits
								lowercase__    =			outputs.masks_queries_logits
								self.assertEqual(
								    masks_queries_logits.shape,  (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4),  )
								lowercase__    =			[
								    [-1.3737124, -1.7724937, -1.9364233],
								    [-1.5977281, -1.9867939, -2.1523695],
								    [-1.5795398, -1.9269832, -2.093942],
								]
								lowercase__    =			torch.tensor(__UpperCamelCase  ).to(__UpperCamelCase  )
								self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )
								# class_queries_logits
								lowercase__    =			outputs.class_queries_logits
								self.assertEqual(
								    class_queries_logits.shape,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)  )
								lowercase__    =			torch.tensor(
								    [
								        [1.6512E00, -5.2572E00, -3.3519E00],
								        [3.6169E-02, -5.9025E00, -2.9313E00],
								        [1.0766E-04, -7.7630E00, -5.1263E00],
								    ]  ).to(__UpperCamelCase  )
								self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )




				def 				lowercase__   (     self						:    int  ):

								'''simple docstring'''
								lowercase__    =			(
								    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-resnet101-coco-stuff'''  )
								    .to(__UpperCamelCase  )
								    .eval()
								)
								lowercase__    =			self.default_image_processor
								lowercase__    =			prepare_img()
								lowercase__    =			image_processor(__UpperCamelCase,  return_tensors='''pt'''  ).to(__UpperCamelCase  )
								lowercase__    =			inputs['''pixel_values'''].shape
								# check size is divisible by 32
								self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0  )
								# check size
								self.assertEqual(__UpperCamelCase,  (1, 3, 800, 1_088)  )

								with torch.no_grad():
												lowercase__    =			model(**__UpperCamelCase  )
								# masks_queries_logits
								lowercase__    =			outputs.masks_queries_logits
								self.assertEqual(
								    masks_queries_logits.shape,  (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4),  )
								lowercase__    =			[[-0.9046, -2.6366, -4.6062], [-3.4179, -5.7890, -8.8057], [-4.9179, -7.6560, -10.7711]]
								lowercase__    =			torch.tensor(__UpperCamelCase  ).to(__UpperCamelCase  )
								self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )
								# class_queries_logits
								lowercase__    =			outputs.class_queries_logits
								self.assertEqual(
								    class_queries_logits.shape,  (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)  )
								lowercase__    =			torch.tensor(
								    [[4.7188, -3.2585, -2.8857], [6.6871, -2.9181, -1.2487], [7.2449, -2.2764, -2.1874]]  ).to(__UpperCamelCase  )
								self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3],  __UpperCamelCase,  atol=__UpperCamelCase  )  )




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								lowercase__    =			(
								    MaskFormerForInstanceSegmentation.from_pretrained('''facebook/maskformer-swin-small-coco'''  )
								    .to(__UpperCamelCase  )
								    .eval()
								)
								lowercase__    =			self.default_image_processor

								lowercase__    =			image_processor(
								    [np.zeros((3, 800, 1_333)  ), np.zeros((3, 800, 1_333)  )],  segmentation_maps=[np.zeros((384, 384)  ).astype(np.floataa  ), np.zeros((384, 384)  ).astype(np.floataa  )],  return_tensors='''pt''',  )

								lowercase__    =			inputs['''pixel_values'''].to(__UpperCamelCase  )
								lowercase__    =			[el.to(__UpperCamelCase  ) for el in inputs['''mask_labels''']]
								lowercase__    =			[el.to(__UpperCamelCase  ) for el in inputs['''class_labels''']]

								with torch.no_grad():
												lowercase__    =			model(**__UpperCamelCase  )

								self.assertTrue(outputs.loss is not None  )











"""simple docstring"""
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


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






class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Dict     ,       __UpperCamelCase			:WhisperForConditionalGeneration     ,       __UpperCamelCase			:WhisperProcessor     ,       __UpperCamelCase			:AutoencoderKL     ,       __UpperCamelCase			:CLIPTextModel     ,       __UpperCamelCase			:CLIPTokenizer     ,       __UpperCamelCase			:UNetaDConditionModel     ,       __UpperCamelCase			:Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]     ,       __UpperCamelCase			:StableDiffusionSafetyChecker     ,       __UpperCamelCase			:CLIPImageProcessor     ,       ):
												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 	lowerCamelCase       (					self			:Any     ,       __UpperCamelCase			:Optional[Union[str, int]] = "auto"		):
												if slice_size == "auto":
																			A		=  self.unet.config.attention_head_dim // 2
												self.unet.set_attention_slice(__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Tuple		):
												self.enable_attention_slicing(__UpperCamelCase		)


					@torch.no_grad()
					def __call__(					self			:Optional[Any]     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:Dict=1_60_00     ,       __UpperCamelCase			:int = 5_12     ,       __UpperCamelCase			:int = 5_12     ,       __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			:Dict     ,       ):
												A		=  self.speech_processor.feature_extractor(
												    __UpperCamelCase     ,       return_tensors="pt"     ,       sampling_rate=__UpperCamelCase		).input_features.to(self.device		)
												A		=  self.speech_model.generate(__UpperCamelCase     ,       max_length=48_00_00		)

												A		=  self.speech_processor.tokenizer.batch_decode(__UpperCamelCase     ,       skip_special_tokens=__UpperCamelCase     ,       normalize=__UpperCamelCase		)[
												    0
												]

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

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

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

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

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

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

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

																			# predict the noise residual
																			A		=  self.unet(__UpperCamelCase     ,       __UpperCamelCase     ,       encoder_hidden_states=__UpperCamelCase		).sample

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

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

												A		=  1 / 0.18_215 * latents
												A		=  self.vae.decode(__UpperCamelCase		).sample

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

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

												if output_type == "pil":
																			A		=  self.numpy_to_pil(__UpperCamelCase		)

												if not return_dict:
																			return image

												return StableDiffusionPipelineOutput(images=__UpperCamelCase     ,       nsfw_content_detected=__UpperCamelCase		)


