





"""simple docstring"""
import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
		from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor






class 					_UpperCAmelCase   :


					def __init__(					self			:List[Any]     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:Any=13     ,       __UpperCamelCase			:List[Any]=32     ,       __UpperCamelCase			:Union[str, Any]=2     ,       __UpperCamelCase			:Dict=3     ,       __UpperCamelCase			:Tuple=16     ,       __UpperCamelCase			:Any=[1, 2, 1]     ,       __UpperCamelCase			:Optional[int]=[2, 2, 4]     ,       __UpperCamelCase			:Dict=2     ,       __UpperCamelCase			:Optional[int]=2.0     ,       __UpperCamelCase			:str=True     ,       __UpperCamelCase			:Tuple=0.0     ,       __UpperCamelCase			:Optional[int]=0.0     ,       __UpperCamelCase			:str=0.1     ,       __UpperCamelCase			:str="gelu"     ,       __UpperCamelCase			:str=False     ,       __UpperCamelCase			:str=True     ,       __UpperCamelCase			:Tuple=0.02     ,       __UpperCamelCase			:List[str]=1e-5     ,       __UpperCamelCase			:Dict=True     ,       __UpperCamelCase			:Optional[Any]=None     ,       __UpperCamelCase			:Union[str, Any]=True     ,       __UpperCamelCase			:Dict=10     ,       __UpperCamelCase			:Optional[int]=8     ,       ):
												A		=  parent
												A		=  batch_size
												A		=  image_size
												A		=  patch_size
												A		=  num_channels
												A		=  embed_dim
												A		=  depths
												A		=  num_heads
												A		=  window_size
												A		=  mlp_ratio
												A		=  qkv_bias
												A		=  hidden_dropout_prob
												A		=  attention_probs_dropout_prob
												A		=  drop_path_rate
												A		=  hidden_act
												A		=  use_absolute_embeddings
												A		=  patch_norm
												A		=  layer_norm_eps
												A		=  initializer_range
												A		=  is_training
												A		=  scope
												A		=  use_labels
												A		=  type_sequence_label_size
												A		=  encoder_stride


					def 	lowerCamelCase       (					self			:str		):
												A		=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

												A		=  None
												if self.use_labels:
																			A		=  ids_tensor([self.batch_size]     ,       self.type_sequence_label_size		)

												A		=  self.get_config()

												return config, pixel_values, labels


					def 	lowerCamelCase       (					self			:Any		):
												return SwinvaConfig(
												    image_size=self.image_size     ,       patch_size=self.patch_size     ,       num_channels=self.num_channels     ,       embed_dim=self.embed_dim     ,       depths=self.depths     ,       num_heads=self.num_heads     ,       window_size=self.window_size     ,       mlp_ratio=self.mlp_ratio     ,       qkv_bias=self.qkv_bias     ,       hidden_dropout_prob=self.hidden_dropout_prob     ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,       drop_path_rate=self.drop_path_rate     ,       hidden_act=self.hidden_act     ,       use_absolute_embeddings=self.use_absolute_embeddings     ,       path_norm=self.patch_norm     ,       layer_norm_eps=self.layer_norm_eps     ,       initializer_range=self.initializer_range     ,       encoder_stride=self.encoder_stride     ,       )


					def 	lowerCamelCase       (					self			:Any     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:int		):
												A		=  SwinvaModel(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase		)

												A		=  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths		) - 1))
												A		=  int(config.embed_dim * 2 ** (len(config.depths		) - 1)		)

												self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, expected_seq_len, expected_dim)		)


					def 	lowerCamelCase       (					self			:List[str]     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:Any		):
												A		=  SwinvaForMaskedImageModeling(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase		)
												self.parent.assertEqual(
												    result.logits.shape     ,       (self.batch_size, self.num_channels, self.image_size, self.image_size)		)

												# test greyscale images
												A		=  1
												A		=  SwinvaForMaskedImageModeling(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

												A		=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
												A		=  model(__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, 1, self.image_size, self.image_size)		)


					def 	lowerCamelCase       (					self			:Optional[Any]     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  self.type_sequence_label_size
												A		=  SwinvaForImageClassification(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase     ,       labels=__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.type_sequence_label_size)		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  self.prepare_config_and_inputs()
												A,	A,	A		=  config_and_inputs
												A		=  {"pixel_values": pixel_values}
												return config, inputs_dict







@require_torch
class 					_UpperCAmelCase   (	lowercase_     ,							lowercase_     ,							unittest.TestCase ):
					UpperCamelCase      =							(
					    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
					)
					UpperCamelCase      =							(
					    {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
					    if is_torch_available()
					    else {}
					)

					UpperCamelCase      =							False
					UpperCamelCase      =							False
					UpperCamelCase      =							False
					UpperCamelCase      =							False


					def 	lowerCamelCase       (					self			:Union[str, Any]		):
												A		=  SwinvaModelTester(self		)
												A		=  ConfigTester(self     ,       config_class=__UpperCamelCase     ,       embed_dim=37		)


					def 	lowerCamelCase       (					self			:str		):
												self.config_tester.create_and_test_config_to_json_string()
												self.config_tester.create_and_test_config_to_json_file()
												self.config_tester.create_and_test_config_from_and_save_pretrained()
												self.config_tester.create_and_test_config_with_num_labels()
												self.config_tester.check_config_can_be_init_without_params()
												self.config_tester.check_config_arguments_init()


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*__UpperCamelCase		)


					@unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."		)
					def 	lowerCamelCase       (					self			:Union[str, Any]		):
												pass


					@unittest.skip(reason="Swinv2 does not use inputs_embeds"		)
					def 	lowerCamelCase       (					self			:Optional[Any]		):
												pass


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																			A		=  model_class(__UpperCamelCase		)
																			self.assertIsInstance(model.get_input_embeddings()     ,       (nn.Module)		)
																			A		=  model.get_output_embeddings()
																			self.assertTrue(x is None or isinstance(__UpperCamelCase     ,       nn.Linear		)		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

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

																			A		=  ["pixel_values"]
																			self.assertListEqual(arg_names[:1]     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()
												A		=  True

												for model_class in self.all_model_classes:
																			A		=  True
																			A		=  False
																			A		=  True
																			A		=  model_class(__UpperCamelCase		)
																			model.to(__UpperCamelCase		)
																			model.eval()
																			with torch.no_grad():
																										A		=  model(**self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)		)
																			A		=  outputs.attentions
																			A		=  len(self.model_tester.depths		)
																			self.assertEqual(len(__UpperCamelCase		)     ,       __UpperCamelCase		)

																			# check that output_attentions also work using config
																			del inputs_dict["output_attentions"]
																			A		=  True
																			A		=  config.window_size**2
																			A		=  model_class(__UpperCamelCase		)
																			model.to(__UpperCamelCase		)
																			model.eval()
																			with torch.no_grad():
																										A		=  model(**self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)		)
																			A		=  outputs.attentions
																			self.assertEqual(len(__UpperCamelCase		)     ,       __UpperCamelCase		)

																			self.assertListEqual(
																			    list(attentions[0].shape[-3:]		)     ,       [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,       )
																			A		=  len(__UpperCamelCase		)

																			# Check attention is always last and order is fine
																			A		=  True
																			A		=  True
																			A		=  model_class(__UpperCamelCase		)
																			model.to(__UpperCamelCase		)
																			model.eval()
																			with torch.no_grad():
																										A		=  model(**self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)		)

																			if hasattr(self.model_tester     ,       "num_hidden_states_types"		):
																										A		=  self.model_tester.num_hidden_states_types
																			else:
																										# also another +1 for reshaped_hidden_states
																										A		=  2
																			self.assertEqual(out_len + added_hidden_states     ,       len(__UpperCamelCase		)		)

																			A		=  outputs.attentions

																			self.assertEqual(len(__UpperCamelCase		)     ,       __UpperCamelCase		)

																			self.assertListEqual(
																			    list(self_attentions[0].shape[-3:]		)     ,       [self.model_tester.num_heads[0], window_size_squared, window_size_squared]     ,       )


					def 	lowerCamelCase       (					self			:List[str]     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:List[str]		):
												A		=  model_class(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

												with torch.no_grad():
																			A		=  model(**self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)		)

												A		=  outputs.hidden_states

												A		=  getattr(
												    self.model_tester     ,       "expected_num_hidden_layers"     ,       len(self.model_tester.depths		) + 1		)
												self.assertEqual(len(__UpperCamelCase		)     ,       __UpperCamelCase		)

												# Swinv2 has a different seq_length
												A		=  (
												    config.patch_size
												    if isinstance(config.patch_size     ,       collections.abc.Iterable		)
												    else (config.patch_size, config.patch_size)
												)

												A		=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

												self.assertListEqual(
												    list(hidden_states[0].shape[-2:]		)     ,       [num_patches, self.model_tester.embed_dim]     ,       )

												A		=  outputs.reshaped_hidden_states
												self.assertEqual(len(__UpperCamelCase		)     ,       __UpperCamelCase		)

												A,	A,	A,	A		=  reshaped_hidden_states[0].shape
												A		=  (
												    reshaped_hidden_states[0].view(__UpperCamelCase     ,       __UpperCamelCase     ,       height * width		).permute(0     ,       2     ,       1		)
												)
												self.assertListEqual(
												    list(reshaped_hidden_states.shape[-2:]		)     ,       [num_patches, self.model_tester.embed_dim]     ,       )


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

												A		=  (
												    self.model_tester.image_size
												    if isinstance(self.model_tester.image_size     ,       collections.abc.Iterable		)
												    else (self.model_tester.image_size, self.model_tester.image_size)
												)

												for model_class in self.all_model_classes:
																			A		=  True
																			self.check_hidden_states_output(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			A		=  True

																			self.check_hidden_states_output(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:List[Any]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()
												A		=  3

												A		=  (
												    self.model_tester.image_size
												    if isinstance(self.model_tester.image_size     ,       collections.abc.Iterable		)
												    else (self.model_tester.image_size, self.model_tester.image_size)
												)
												A		=  (
												    config.patch_size
												    if isinstance(config.patch_size     ,       collections.abc.Iterable		)
												    else (config.patch_size, config.patch_size)
												)

												A		=  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
												A		=  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

												for model_class in self.all_model_classes:
																			A		=  True
																			self.check_hidden_states_output(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       (padded_height, padded_width)		)

																			# check that output_hidden_states also work using config
																			del inputs_dict["output_hidden_states"]
																			A		=  True
																			self.check_hidden_states_output(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       (padded_height, padded_width)		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:List[str]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase		)


					@slow
					def 	lowerCamelCase       (					self			:Tuple		):
												for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A		=  SwinvaModel.from_pretrained(__UpperCamelCase		)
																			self.assertIsNotNone(__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()

												A		=  _config_zero_init(__UpperCamelCase		)
												for model_class in self.all_model_classes:
																			A		=  model_class(config=__UpperCamelCase		)
																			for name, param in model.named_parameters():
																										if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
																																	self.assertIn(
																																	    ((param.data.mean() * 1e9).round() / 1e9).item()     ,       [0.0, 1.0]     ,       msg=f"Parameter {name} of model {model_class} seems not properly initialized"     ,       )







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


					@cached_property
					def 	lowerCamelCase       (					self			:Union[str, Any]		):
												return (
												    AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"		)
												    if is_vision_available()
												    else None
												)


					@slow
					def 	lowerCamelCase       (					self			:List[str]		):
												A		=  SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"		).to(
												    __UpperCamelCase		)
												A		=  self.default_image_processor

												A		=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
												A		=  image_processor(images=__UpperCamelCase     ,       return_tensors="pt"		).to(__UpperCamelCase		)

												# forward pass
												with torch.no_grad():
																			A		=  model(**__UpperCamelCase		)

												# verify the logits
												A		=  torch.Size((1, 10_00)		)
												self.assertEqual(outputs.logits.shape     ,       __UpperCamelCase		)
												A		=  torch.tensor([-0.3_947, -0.4_306, 0.0_026]		).to(__UpperCamelCase		)
												self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,       __UpperCamelCase     ,       atol=1e-4		)		)









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


