



'''simple docstring'''




import argparse
import json
from pathlib import Path

import torch
import torchaudio
from datasets import load_dataset
from huggingface_hub import hf_hub_download

from transformers import ASTConfig, ASTFeatureExtractor, ASTForAudioClassification
from transformers.utils import logging


logging.set_verbosity_info()
a   :			Optional[int]					  =    logging.get_logger(__name__)



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		int							     =		ASTConfig()

							if "10-10" in model_name:
														pass
							elif "speech-commands" in model_name:
														UpperCAmelCase     :		List[str]							     =		1_2_8
							elif "12-12" in model_name:
														UpperCAmelCase     :		str							     =		1_2
														UpperCAmelCase     :		List[Any]							     =		1_2
							elif "14-14" in model_name:
														UpperCAmelCase     :		int							     =		1_4
														UpperCAmelCase     :		Union[str, Any]							     =		1_4
							elif "16-16" in model_name:
														UpperCAmelCase     :		Any							     =		1_6
														UpperCAmelCase     :		Tuple							     =		1_6
							else:
														raise ValueError("""Model not supported"""		)

							UpperCAmelCase     :		str							     =		"""huggingface/label-files"""
							if "speech-commands" in model_name:
														UpperCAmelCase     :		Dict							     =		3_5
														UpperCAmelCase     :		Any							     =		"""speech-commands-v2-id2label.json"""
							else:
														UpperCAmelCase     :		str							     =		5_2_7
														UpperCAmelCase     :		Optional[int]							     =		"""audioset-id2label.json"""

							UpperCAmelCase     :		List[str]							     =		json.load(open(hf_hub_download(_lowercase	,				_lowercase	,				repo_type="""dataset"""		)	,				"""r"""		)		)
							UpperCAmelCase     :		Any							     =		{int(_lowercase		): v for k, v in idalabel.items()}
							UpperCAmelCase     :		Any							     =		idalabel
							UpperCAmelCase     :		Dict							     =		{v: k for k, v in idalabel.items()}

							return config



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[Any]:
							if "module.v" in name:
														UpperCAmelCase     :		Optional[int]							     =		name.replace("""module.v"""	,				"""audio_spectrogram_transformer"""		)
							if "cls_token" in name:
														UpperCAmelCase     :		Any							     =		name.replace("""cls_token"""	,				"""embeddings.cls_token"""		)
							if "dist_token" in name:
														UpperCAmelCase     :		List[str]							     =		name.replace("""dist_token"""	,				"""embeddings.distillation_token"""		)
							if "pos_embed" in name:
														UpperCAmelCase     :		int							     =		name.replace("""pos_embed"""	,				"""embeddings.position_embeddings"""		)
							if "patch_embed.proj" in name:
														UpperCAmelCase     :		List[str]							     =		name.replace("""patch_embed.proj"""	,				"""embeddings.patch_embeddings.projection"""		)
							# transformer blocks
							if "blocks" in name:
														UpperCAmelCase     :		Optional[Any]							     =		name.replace("""blocks"""	,				"""encoder.layer"""		)
							if "attn.proj" in name:
														UpperCAmelCase     :		List[Any]							     =		name.replace("""attn.proj"""	,				"""attention.output.dense"""		)
							if "attn" in name:
														UpperCAmelCase     :		List[str]							     =		name.replace("""attn"""	,				"""attention.self"""		)
							if "norm1" in name:
														UpperCAmelCase     :		List[Any]							     =		name.replace("""norm1"""	,				"""layernorm_before"""		)
							if "norm2" in name:
														UpperCAmelCase     :		List[Any]							     =		name.replace("""norm2"""	,				"""layernorm_after"""		)
							if "mlp.fc1" in name:
														UpperCAmelCase     :		Dict							     =		name.replace("""mlp.fc1"""	,				"""intermediate.dense"""		)
							if "mlp.fc2" in name:
														UpperCAmelCase     :		List[str]							     =		name.replace("""mlp.fc2"""	,				"""output.dense"""		)
							# final layernorm
							if "audio_spectrogram_transformer.norm" in name:
														UpperCAmelCase     :		str							     =		name.replace("""audio_spectrogram_transformer.norm"""	,				"""audio_spectrogram_transformer.layernorm"""		)
							# classifier head
							if "module.mlp_head.0" in name:
														UpperCAmelCase     :		Union[str, Any]							     =		name.replace("""module.mlp_head.0"""	,				"""classifier.layernorm"""		)
							if "module.mlp_head.1" in name:
														UpperCAmelCase     :		Optional[int]							     =		name.replace("""module.mlp_head.1"""	,				"""classifier.dense"""		)

							return name



def       __lowerCamelCase  (					_lowercase	,				_lowercase		)      ->			int:
							for key in orig_state_dict.copy().keys():
														UpperCAmelCase     :		List[str]							     =		orig_state_dict.pop(_lowercase		)

														if "qkv" in key:
																					UpperCAmelCase     :		Dict							     =		key.split("""."""		)
																					UpperCAmelCase     :		Tuple							     =		int(key_split[3]		)
																					UpperCAmelCase     :		List[str]							     =		config.hidden_size
																					if "weight" in key:
																												UpperCAmelCase     :		Tuple							     =		val[:dim, :]
																												UpperCAmelCase     :		Dict							     =		val[dim : dim * 2, :]
																												UpperCAmelCase     :		Optional[int]							     =		val[-dim:, :]
																					else:
																												UpperCAmelCase     :		Optional[int]							     =		val[:dim]
																												UpperCAmelCase     :		Union[str, Any]							     =		val[dim : dim * 2]
																												UpperCAmelCase     :		Tuple							     =		val[-dim:]
														else:
																					UpperCAmelCase     :		int							     =		val

							return orig_state_dict



def       __lowerCamelCase  (					_lowercase		)      ->			str:
							UpperCAmelCase     :		Union[str, Any]							     =		[
							    """module.v.head.weight""",
							    """module.v.head.bias""",
							    """module.v.head_dist.weight""",
							    """module.v.head_dist.bias""",
							]
							for k in ignore_keys:
														state_dict.pop(_lowercase	,				_lowercase		)



@torch.no_grad()
def       __lowerCamelCase  (					_lowercase	,				_lowercase	,				_lowercase=False		)      ->			List[Any]:
							UpperCAmelCase     :		Optional[Any]							     =		get_audio_spectrogram_transformer_config(_lowercase		)

							UpperCAmelCase     :		Optional[int]							     =		{
							    """ast-finetuned-audioset-10-10-0.4593""": (
							        """https://www.dropbox.com/s/ca0b1v2nlxzyeb4/audioset_10_10_0.4593.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-10-10-0.450""": (
							        """https://www.dropbox.com/s/1tv0hovue1bxupk/audioset_10_10_0.4495.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-10-10-0.448""": (
							        """https://www.dropbox.com/s/6u5sikl4b9wo4u5/audioset_10_10_0.4483.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-10-10-0.448-v2""": (
							        """https://www.dropbox.com/s/kt6i0v9fvfm1mbq/audioset_10_10_0.4475.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-12-12-0.447""": (
							        """https://www.dropbox.com/s/snfhx3tizr4nuc8/audioset_12_12_0.4467.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-14-14-0.443""": (
							        """https://www.dropbox.com/s/z18s6pemtnxm4k7/audioset_14_14_0.4431.pth?dl=1"""
							    ),
							    """ast-finetuned-audioset-16-16-0.442""": (
							        """https://www.dropbox.com/s/mdsa4t1xmcimia6/audioset_16_16_0.4422.pth?dl=1"""
							    ),
							    """ast-finetuned-speech-commands-v2""": (
							        """https://www.dropbox.com/s/q0tbqpwv44pquwy/speechcommands_10_10_0.9812.pth?dl=1"""
							    ),
							}

							# load original state_dict
							UpperCAmelCase     :		List[Any]							     =		model_name_to_url[model_name]
							UpperCAmelCase     :		Optional[Any]							     =		torch.hub.load_state_dict_from_url(_lowercase	,				map_location="""cpu"""		)
							# remove some keys
							remove_keys(_lowercase		)
							# rename some keys
							UpperCAmelCase     :		Dict							     =		convert_state_dict(_lowercase	,				_lowercase		)

							# load 🤗 model
							UpperCAmelCase     :		Optional[int]							     =		ASTForAudioClassification(_lowercase		)
							model.eval()

							model.load_state_dict(_lowercase		)

							# verify outputs on dummy input
							# source: https://github.com/YuanGongND/ast/blob/79e873b8a54d0a3b330dd522584ff2b9926cd581/src/run.py#L62
							UpperCAmelCase     :		int							     =		-4.267_7393 if """speech-commands""" not in model_name else -6.84_5978
							UpperCAmelCase     :		Optional[Any]							     =		4.568_9974 if """speech-commands""" not in model_name else 5.565_4526
							UpperCAmelCase     :		str							     =		1_0_2_4 if """speech-commands""" not in model_name else 1_2_8
							UpperCAmelCase     :		List[str]							     =		ASTFeatureExtractor(mean=_lowercase	,				std=_lowercase	,				max_length=_lowercase		)

							if "speech-commands" in model_name:
														UpperCAmelCase     :		Any							     =		load_dataset("""speech_commands"""	,				"""v0.02"""	,				split="""validation"""		)
														UpperCAmelCase     :		List[str]							     =		dataset[0]["""audio"""]["""array"""]
							else:
														UpperCAmelCase     :		Optional[int]							     =		hf_hub_download(
														    repo_id="""nielsr/audio-spectogram-transformer-checkpoint"""	,				filename="""sample_audio.flac"""	,				repo_type="""dataset"""	,				)

														UpperCAmelCase     ,     UpperCAmelCase     :		Union[str, Any]							     =		torchaudio.load(_lowercase		)
														UpperCAmelCase     :		Union[str, Any]							     =		waveform.squeeze().numpy()

							UpperCAmelCase     :		Tuple							     =		feature_extractor(_lowercase	,				sampling_rate=1_6_0_0_0	,				return_tensors="""pt"""		)

							# forward pass
							UpperCAmelCase     :		Union[str, Any]							     =		model(**_lowercase		)
							UpperCAmelCase     :		List[str]							     =		outputs.logits

							if model_name == "ast-finetuned-audioset-10-10-0.4593":
														UpperCAmelCase     :		Optional[Any]							     =		torch.tensor([-0.8760, -7.0042, -8.6602]		)
							elif model_name == "ast-finetuned-audioset-10-10-0.450":
														UpperCAmelCase     :		Tuple							     =		torch.tensor([-1.1986, -7.0903, -8.2718]		)
							elif model_name == "ast-finetuned-audioset-10-10-0.448":
														UpperCAmelCase     :		Tuple							     =		torch.tensor([-2.6128, -8.0080, -9.4344]		)
							elif model_name == "ast-finetuned-audioset-10-10-0.448-v2":
														UpperCAmelCase     :		Union[str, Any]							     =		torch.tensor([-1.5080, -7.4534, -8.8917]		)
							elif model_name == "ast-finetuned-audioset-12-12-0.447":
														UpperCAmelCase     :		Any							     =		torch.tensor([-0.5050, -6.5833, -8.0843]		)
							elif model_name == "ast-finetuned-audioset-14-14-0.443":
														UpperCAmelCase     :		Any							     =		torch.tensor([-0.3826, -7.0336, -8.2413]		)
							elif model_name == "ast-finetuned-audioset-16-16-0.442":
														UpperCAmelCase     :		List[Any]							     =		torch.tensor([-1.2113, -6.9101, -8.3470]		)
							elif model_name == "ast-finetuned-speech-commands-v2":
														UpperCAmelCase     :		Union[str, Any]							     =		torch.tensor([6.1589, -8.0566, -8.7984]		)
							else:
														raise ValueError("""Unknown model name"""		)
							if not torch.allclose(logits[0, :3]	,				_lowercase	,				atol=1e-4		):
														raise ValueError("""Logits don't match"""		)
							print("""Looks ok!"""		)

							if pytorch_dump_folder_path is not None:
														Path(_lowercase		).mkdir(exist_ok=_lowercase		)
														print(F'''Saving model {model_name} to {pytorch_dump_folder_path}'''		)
														model.save_pretrained(_lowercase		)
														print(F'''Saving feature extractor to {pytorch_dump_folder_path}'''		)
														feature_extractor.save_pretrained(_lowercase		)

							if push_to_hub:
														print("""Pushing model and feature extractor to the hub..."""		)
														model.push_to_hub(F'''MIT/{model_name}'''		)
														feature_extractor.push_to_hub(F'''MIT/{model_name}'''		)


if __name__ == "__main__":
							a   :			Tuple					  =    argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    """--model_name""",
							    default="""ast-finetuned-audioset-10-10-0.4593""",
							    type=str,
							    help="""Name of the Audio Spectrogram Transformer model you'd like to convert.""",
							)
							parser.add_argument(
							    """--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model directory."""
							)
							parser.add_argument(
							    """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
							)

							a   :			List[str]					  =    parser.parse_args()
							convert_audio_spectrogram_transformer_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)






'''simple docstring'''




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

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

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


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

class 			UpperCamelCase_ (							__magic_name__			):





	def __init__(  self		,		A		,		A		,		A		,		A		,		A		,		A		,		A		,		)			->			Optional[Any]:
								super().__init__()
								self.register_modules(
								    vae=A		,		text_encoder=A		,		tokenizer=A		,		unet=A		,		scheduler=A		,		safety_checker=A		,		feature_extractor=A		,		)





	def  _lowercase(  self		,		A = "auto"    )			->			List[Any]:
								if slice_size == "auto":
															# half the attention head size is usually a good trade-off between
															# speed and memory
															UpperCAmelCase     :		Optional[Any]							     =		self.unet.config.attention_head_dim // 2
								self.unet.set_attention_slice(A    )





	def  _lowercase(  self    )			->			Dict:
								self.enable_attention_slicing(A    )






	@torch.no_grad()
	def __call__(  self		,		A		,		A = 512		,		A = 512		,		A = 50		,		A = 7.5		,		A = None		,		A = 1		,		A = 0.0		,		A = None		,		A = None		,		A = "pil"		,		A = True		,		A = None		,		A = 1		,		A = None		,		**A		,		)			->			List[Any]:

								if isinstance(A		,		A    ):
															UpperCAmelCase     :		List[str]							     =		1
								elif isinstance(A		,		A    ):
															UpperCAmelCase     :		Dict							     =		len(A    )
								else:
															raise ValueError(f'''`prompt` has to be of type `str` or `list` but is {type(A    )}'''    )

								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(A		,		A    ) or callback_steps <= 0)
								):
															raise ValueError(
															    f'''`callback_steps` has to be a positive integer but is {callback_steps} of type'''
															    f''' {type(A    )}.'''    )

								# get prompt text embeddings
								UpperCAmelCase     :		List[str]							     =		self.tokenizer(
								    A		,		padding="""max_length"""		,		max_length=self.tokenizer.model_max_length		,		return_tensors="""pt"""		,		)
								UpperCAmelCase     :		List[Any]							     =		text_inputs.input_ids

								if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
															UpperCAmelCase     :		int							     =		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     :		Tuple							     =		text_input_ids[:, : self.tokenizer.model_max_length]

								if text_embeddings is None:
															UpperCAmelCase     :		Union[str, Any]							     =		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     :		Union[str, Any]							     =		text_embeddings.shape
								UpperCAmelCase     :		List[str]							     =		text_embeddings.repeat(1		,		A		,		1    )
								UpperCAmelCase     :		List[Any]							     =		text_embeddings.view(bs_embed * num_images_per_prompt		,		A		,		-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     :		Optional[int]							     =		guidance_scale > 1.0
								# get unconditional embeddings for classifier free guidance
								if do_classifier_free_guidance:
															UpperCAmelCase     :		List[str]
															if negative_prompt is None:
																						UpperCAmelCase     :		Any							     =		[""""""]
															elif type(A    ) is not type(A    ):
																						raise TypeError(
																						    f'''`negative_prompt` should be the same type to `prompt`, but got {type(A    )} !='''
																						    f''' {type(A    )}.'''    )
															elif isinstance(A		,		A    ):
																						UpperCAmelCase     :		Optional[int]							     =		[negative_prompt]
															elif batch_size != len(A    ):
																						raise ValueError(
																						    f'''`negative_prompt`: {negative_prompt} has batch size {len(A    )}, but `prompt`:'''
																						    f''' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'''
																						    """ the batch size of `prompt`."""    )
															else:
																						UpperCAmelCase     :		Any							     =		negative_prompt

															UpperCAmelCase     :		Dict							     =		text_input_ids.shape[-1]
															UpperCAmelCase     :		List[Any]							     =		self.tokenizer(
															    A		,		padding="""max_length"""		,		max_length=A		,		truncation=A		,		return_tensors="""pt"""		,		)
															UpperCAmelCase     :		Tuple							     =		self.text_encoder(uncond_input.input_ids.to(self.device    )    )[0]

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

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

								# set timesteps
								self.scheduler.set_timesteps(A    )

								# Some schedulers like PNDM have timesteps as arrays
								# It's more optimized to move all timesteps to correct device beforehand
								UpperCAmelCase     :		Union[str, Any]							     =		self.scheduler.timesteps.to(self.device    )

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

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

															# predict the noise residual
															UpperCAmelCase     :		Any							     =		self.unet(A		,		A		,		encoder_hidden_states=A    ).sample

															# perform guidance
															if do_classifier_free_guidance:
																						UpperCAmelCase     ,     UpperCAmelCase     :		Any							     =		noise_pred.chunk(2    )
																						UpperCAmelCase     :		List[Any]							     =		noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

															# compute the previous noisy sample x_t -> x_t-1
															UpperCAmelCase     :		Dict							     =		self.scheduler.step(A		,		A		,		A		,		**A    ).prev_sample

															# call the callback, if provided
															if callback is not None and i % callback_steps == 0:
																						callback(A		,		A		,		A    )

								UpperCAmelCase     :		Union[str, Any]							     =		1 / 0.1_8_2_1_5 * latents
								UpperCAmelCase     :		Tuple							     =		self.vae.decode(A    ).sample

								UpperCAmelCase     :		Union[str, Any]							     =		(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     :		Union[str, Any]							     =		image.cpu().permute(0		,		2		,		3		,		1    ).float().numpy()

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

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

								if not return_dict:
															return (image, has_nsfw_concept)

								return StableDiffusionPipelineOutput(images=A		,		nsfw_content_detected=A    )

