






"""simple docstring"""







import argparse

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    CLIPTokenizer,
    CLIPTokenizerFast,
    VideoMAEImageProcessor,
    XCLIPConfig,
    XCLIPModel,
    XCLIPProcessor,
    XCLIPTextConfig,
    XCLIPVisionConfig,
)





def 						lowercase__(A ,		A						)    ->Union[str, Any]:




						"""simple docstring"""



						lowercase__    :				Union[str, Any]=	XCLIPTextConfig()

						# derive patch size from model name
						lowercase__    :				List[Any]=	model_name.find("patch"						)
						lowercase__    :				List[Any]=	int(model_name[start_idx + len("patch"						) : start_idx + len("patch"						) + 2]						)
						lowercase__    :				Optional[Any]=	XCLIPVisionConfig(patch_size=A ,		num_frames=A						)

						if "large" in model_name:
												lowercase__    :				List[Any]=	768
												lowercase__    :				Any=	3_072
												lowercase__    :				Optional[int]=	12

												lowercase__    :				Optional[int]=	1_024
												lowercase__    :				Optional[int]=	4_096
												lowercase__    :				Dict=	16
												lowercase__    :				Union[str, Any]=	24
												lowercase__    :				str=	768
												lowercase__    :				Any=	3_072

						if model_name == "xclip-large-patch14-16-frames":
												lowercase__    :				str=	336

						lowercase__    :				Union[str, Any]=	XCLIPConfig.from_text_vision_configs(A ,		A						)

						if "large" in model_name:
												lowercase__    :				Optional[Any]=	768

						return config





def 						lowercase__(A						)    ->Union[str, Any]:




						"""simple docstring"""



						if name == "token_embedding.weight":
												lowercase__    :				str=	name.replace("token_embedding.weight" ,		"text_model.embeddings.token_embedding.weight"						)
						if name == "positional_embedding":
												lowercase__    :				Any=	name.replace("positional_embedding" ,		"text_model.embeddings.position_embedding.weight"						)
						if "ln_1" in name:
												lowercase__    :				Any=	name.replace("ln_1" ,		"layer_norm1"						)
						if "ln_2" in name:
												lowercase__    :				Optional[int]=	name.replace("ln_2" ,		"layer_norm2"						)
						if "c_fc" in name:
												lowercase__    :				List[str]=	name.replace("c_fc" ,		"fc1"						)
						if "c_proj" in name:
												lowercase__    :				Tuple=	name.replace("c_proj" ,		"fc2"						)
						if name.startswith("transformer.resblocks"						):
												lowercase__    :				str=	name.replace("transformer.resblocks" ,		"text_model.encoder.layers"						)
						if "attn.out_proj" in name and "message" not in name:
												lowercase__    :				int=	name.replace("attn.out_proj" ,		"self_attn.out_proj"						)
						if "ln_final" in name:
												lowercase__    :				List[str]=	name.replace("ln_final" ,		"text_model.final_layer_norm"						)
						# visual encoder
						if name == "visual.class_embedding":
												lowercase__    :				int=	name.replace("visual.class_embedding" ,		"vision_model.embeddings.class_embedding"						)
						if name == "visual.positional_embedding":
												lowercase__    :				Dict=	name.replace("visual.positional_embedding" ,		"vision_model.embeddings.position_embedding.weight"						)
						if name.startswith("visual.transformer.resblocks"						):
												lowercase__    :				List[str]=	name.replace("visual.transformer.resblocks" ,		"vision_model.encoder.layers"						)
						if "visual.conv1" in name:
												lowercase__    :				Dict=	name.replace("visual.conv1" ,		"vision_model.embeddings.patch_embedding"						)
						if "visual.ln_pre" in name:
												lowercase__    :				Any=	name.replace("visual.ln_pre" ,		"vision_model.pre_layernorm"						)
						if "visual.ln_post" in name:
												lowercase__    :				Optional[int]=	name.replace("visual.ln_post" ,		"vision_model.post_layernorm"						)
						if "visual.proj" in name:
												lowercase__    :				int=	name.replace("visual.proj" ,		"visual_projection.weight"						)
						if "text_projection" in name:
												lowercase__    :				List[str]=	name.replace("text_projection" ,		"text_projection.weight"						)
						# things on top
						if "prompts_visual_proj" in name:
												lowercase__    :				List[str]=	name.replace("prompts_visual_proj" ,		"prompts_visual_projection"						)
						if "prompts_visual_ln" in name:
												lowercase__    :				Optional[Any]=	name.replace("prompts_visual_ln" ,		"prompts_visual_layernorm"						)
						# mit
						if name == "mit.positional_embedding":
												lowercase__    :				List[Any]=	name.replace("positional" ,		"position"						)
						if name.startswith("mit.resblocks"						):
												lowercase__    :				List[str]=	name.replace("mit.resblocks" ,		"mit.encoder.layers"						)
						# prompts generator
						if name.startswith("prompts_generator.norm"						):
												lowercase__    :				int=	name.replace("prompts_generator.norm" ,		"prompts_generator.layernorm"						)

						return name





def 						lowercase__(A ,		A						)    ->Tuple:




						"""simple docstring"""



						for key in orig_state_dict.copy().keys():
												lowercase__    :				List[str]=	orig_state_dict.pop(A						)

												if "attn.in_proj" in key:
																		lowercase__    :				Optional[Any]=	key.split("."						)
																		if key.startswith("visual"						):
																								lowercase__    :				Tuple=	key_split[3]
																								lowercase__    :				Optional[Any]=	config.vision_config.hidden_size
																								if "message_attn" in key:
																														if "weight" in key:
																																				lowercase__    :				Optional[int]=	val[
																																				    :dim, :
																																				]
																																				lowercase__    :				List[str]=	val[
																																				    dim : dim * 2, :
																																				]
																																				lowercase__    :				Union[str, Any]=	val[
																																				    -dim:, :
																																				]
																														else:
																																				lowercase__    :				Tuple=	val[
																																				    :dim
																																				]
																																				lowercase__    :				Optional[int]=	val[
																																				    dim : dim * 2
																																				]
																																				lowercase__    :				int=	val[
																																				    -dim:
																																				]
																								else:
																														if "weight" in key:
																																				lowercase__    :				Any=	val[
																																				    :dim, :
																																				]
																																				lowercase__    :				Tuple=	val[
																																				    dim : dim * 2, :
																																				]
																																				lowercase__    :				List[Any]=	val[
																																				    -dim:, :
																																				]
																														else:
																																				lowercase__    :				Optional[int]=	val[:dim]
																																				lowercase__    :				Dict=	val[
																																				    dim : dim * 2
																																				]
																																				lowercase__    :				str=	val[-dim:]
																		elif key.startswith("mit"						):
																								lowercase__    :				str=	key_split[2]
																								lowercase__    :				List[Any]=	config.vision_config.mit_hidden_size
																								if "weight" in key:
																														lowercase__    :				str=	val[:dim, :]
																														lowercase__    :				Tuple=	val[dim : dim * 2, :]
																														lowercase__    :				Tuple=	val[-dim:, :]
																								else:
																														lowercase__    :				Dict=	val[:dim]
																														lowercase__    :				Any=	val[dim : dim * 2]
																														lowercase__    :				Optional[Any]=	val[-dim:]
																		else:
																								lowercase__    :				Optional[Any]=	key_split[2]
																								lowercase__    :				Union[str, Any]=	config.text_config.hidden_size
																								if "weight" in key:
																														lowercase__    :				Dict=	val[:dim, :]
																														lowercase__    :				Optional[int]=	val[
																														    dim : dim * 2, :
																														]
																														lowercase__    :				Dict=	val[-dim:, :]
																								else:
																														lowercase__    :				Dict=	val[:dim]
																														lowercase__    :				Dict=	val[
																														    dim : dim * 2
																														]
																														lowercase__    :				List[Any]=	val[-dim:]
												else:
																		lowercase__    :				Dict=	rename_key(A						)
																		if new_key_name in ["visual_projection.weight", "text_projection.weight"]:
																								lowercase__    :				Tuple=	val.T
																		lowercase__    :				Optional[Any]=	val

						return orig_state_dict





def 						lowercase__(A						)    ->Optional[int]:




						"""simple docstring"""



						if num_frames == 8:
												lowercase__    :				Optional[Any]=	"eating_spaghetti_8_frames.npy"
						elif num_frames == 16:
												lowercase__    :				Optional[Any]=	"eating_spaghetti.npy"
						elif num_frames == 32:
												lowercase__    :				Union[str, Any]=	"eating_spaghetti_32_frames.npy"
						lowercase__    :				Union[str, Any]=	hf_hub_download(
						    repo_id="hf-internal-testing/spaghetti-video" ,		filename=A ,		repo_type="dataset" ,		)
						lowercase__    :				Optional[int]=	np.load(A						)
						return list(A						)





def 						lowercase__(A ,		A=None ,		A=False						)    ->int:




						"""simple docstring"""



						lowercase__    :				str=	{
						    # fully supervised kinetics-400 checkpoints
						    "xclip-base-patch32": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_8.pth",
						    "xclip-base-patch32-16-frames": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_32_16.pth"
						    ),
						    "xclip-base-patch16": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_8.pth",
						    "xclip-base-patch16-16-frames": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k400_16_16.pth"
						    ),
						    "xclip-large-patch14": "https://drive.google.com/u/0/uc?id=1NUOImq0o5DlQTST17iIP3vG7DgmHQuCx&amp;export=download&amp;confirm=t&amp;uuid=b26caedc-88e2-473e-830a-9d158b653cdb",
						    "xclip-large-patch14-16-frames": "https://drive.google.com/u/0/uc?id=1FOYgnJc097OJ4lGwtRCCydQyVPJEOH7d&amp;export=download&amp;confirm=t&amp;uuid=538fa810-e671-4050-b385-9a623f89804f",
						    # fully supervised kinetics-600 checkpoints
						    "xclip-base-patch16-kinetics-600": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_8.pth"
						    ),
						    "xclip-base-patch16-kinetics-600-16-frames": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/k600_16_16.pth"
						    ),
						    "xclip-large-patch14-kinetics-600": "https://drive.google.com/u/0/uc?id=1FV8C1INuM91sLAN4ImjzePLIlpMSihwV&amp;export=download&amp;confirm=t&amp;uuid=141d4977-4a65-44ae-864f-4b0c19f838be",
						    # few shot
						    "xclip-base-patch16-hmdb-2-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_2.pth"
						    ),
						    "xclip-base-patch16-hmdb-4-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_4.pth"
						    ),
						    "xclip-base-patch16-hmdb-8-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_8.pth"
						    ),
						    "xclip-base-patch16-hmdb-16-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_hmdb_16.pth"
						    ),
						    "xclip-base-patch16-ucf-2-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_2.pth"
						    ),
						    "xclip-base-patch16-ucf-4-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_4.pth"
						    ),
						    "xclip-base-patch16-ucf-8-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_8.pth"
						    ),
						    "xclip-base-patch16-ucf-16-shot": (
						        "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/few_ucf_16.pth"
						    ),
						    # zero shot
						    "xclip-base-patch16-zero-shot": "https://github.com/nbl97/X-CLIP_Model_Zoo/releases/download/v1.0/zero.pth",
						}

						lowercase__    :				Any=	model_to_url[model_name]
						lowercase__    :				Dict=	8
						if "16-frames" in model_name:
												lowercase__    :				Dict=	16
						elif "shot" in model_name:
												lowercase__    :				Any=	32

						lowercase__    :				List[str]=	get_xclip_config(A ,		A						)
						lowercase__    :				Optional[Any]=	XCLIPModel(A						)
						model.eval()

						if "drive" in checkpoint_url:
												lowercase__    :				Union[str, Any]=	"pytorch_model.bin"
												gdown.cached_download(A ,		A ,		quiet=A						)
												lowercase__    :				Tuple=	torch.load(A ,		map_location="cpu"						)["model"]
						else:
												lowercase__    :				Union[str, Any]=	torch.hub.load_state_dict_from_url(A						)["model"]

						lowercase__    :				int=	convert_state_dict(A ,		A						)

						lowercase__    :				Tuple=	XCLIPModel(A						)
						lowercase__,     lowercase__    :				Any=	model.load_state_dict(A ,		strict=A						)
						assert missing_keys == ["text_model.embeddings.position_ids", "vision_model.embeddings.position_ids"]
						model.eval()

						lowercase__    :				Dict=	336 if model_name == "xclip-large-patch14-16-frames" else 224
						lowercase__    :				Tuple=	VideoMAEImageProcessor(size=A						)
						lowercase__    :				str=	CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"						)
						lowercase__    :				Tuple=	CLIPTokenizerFast.from_pretrained("openai/clip-vit-base-patch32"						)
						lowercase__    :				str=	XCLIPProcessor(image_processor=A ,		tokenizer=A						)

						lowercase__    :				Any=	prepare_video(A						)
						lowercase__    :				Any=	processor(
						    text=["playing sports", "eating spaghetti", "go shopping"] ,		videos=A ,		return_tensors="pt" ,		padding=A						)

						print("Shape of pixel values:" ,		inputs.pixel_values.shape						)

						with torch.no_grad():
												lowercase__    :				Tuple=	model(**A						)

						# Verify outputs
						lowercase__    :				Optional[Any]=	outputs.logits_per_video
						lowercase__    :				Any=	logits_per_video.softmax(dim=1						)
						print("Probs:" ,		A						)
						# kinetics-400
						if model_name == "xclip-base-patch32":
												lowercase__    :				Optional[int]=	torch.tensor([[0.0_019, 0.9_951, 0.0_030]]						)
						elif model_name == "xclip-base-patch32-16-frames":
												lowercase__    :				str=	torch.tensor([[7.0_9_9_9e-0_4, 9.9_8_8_3e-0_1, 4.5_5_8_0e-0_4]]						)
						elif model_name == "xclip-base-patch16":
												lowercase__    :				Optional[Any]=	torch.tensor([[0.0_083, 0.9_681, 0.0_236]]						)
						elif model_name == "xclip-base-patch16-16-frames":
												lowercase__    :				Optional[int]=	torch.tensor([[7.6_9_3_7e-0_4, 9.9_7_2_8e-0_1, 1.9_4_7_3e-0_3]]						)
						elif model_name == "xclip-large-patch14":
												lowercase__    :				Optional[Any]=	torch.tensor([[0.0_062, 0.9_864, 0.0_075]]						)
						elif model_name == "xclip-large-patch14-16-frames":
												lowercase__    :				Optional[Any]=	torch.tensor([[3.3_8_7_7e-0_4, 9.9_9_3_7e-0_1, 2.8_8_8_8e-0_4]]						)
						# kinetics-600
						elif model_name == "xclip-base-patch16-kinetics-600":
												lowercase__    :				Tuple=	torch.tensor([[0.0_555, 0.8_914, 0.0_531]]						)
						elif model_name == "xclip-base-patch16-kinetics-600-16-frames":
												lowercase__    :				Union[str, Any]=	torch.tensor([[3.8_5_5_4e-0_4, 9.9_9_2_9e-0_1, 3.2_7_5_4e-0_4]]						)
						elif model_name == "xclip-large-patch14-kinetics-600":
												lowercase__    :				List[str]=	torch.tensor([[0.0_036, 0.9_920, 0.0_045]]						)
						# few shot
						elif model_name == "xclip-base-patch16-hmdb-2-shot":
												lowercase__    :				List[str]=	torch.tensor([[7.1_8_9_0e-0_6, 9.9_9_9_4e-0_1, 5.6_5_5_9e-0_5]]						)
						elif model_name == "xclip-base-patch16-hmdb-4-shot":
												lowercase__    :				int=	torch.tensor([[1.0_3_2_0e-0_5, 9.9_9_9_3e-0_1, 6.2_4_3_5e-0_5]]						)
						elif model_name == "xclip-base-patch16-hmdb-8-shot":
												lowercase__    :				int=	torch.tensor([[4.1_3_7_7e-0_6, 9.9_9_9_0e-0_1, 9.8_3_8_6e-0_5]]						)
						elif model_name == "xclip-base-patch16-hmdb-16-shot":
												lowercase__    :				List[Any]=	torch.tensor([[4.1_3_4_7e-0_5, 9.9_9_6_2e-0_1, 3.3_4_1_1e-0_4]]						)
						elif model_name == "xclip-base-patch16-ucf-2-shot":
												lowercase__    :				Optional[int]=	torch.tensor([[8.5_8_5_7e-0_5, 9.9_9_2_8e-0_1, 6.3_2_9_1e-0_4]]						)
						elif model_name == "xclip-base-patch16-ucf-4-shot":
												lowercase__    :				List[str]=	torch.tensor([[8.5_8_5_7e-0_5, 9.9_9_2_8e-0_1, 6.3_2_9_1e-0_4]]						)
						elif model_name == "xclip-base-patch16-ucf-8-shot":
												lowercase__    :				Optional[Any]=	torch.tensor([[0.0_027, 0.9_904, 0.0_070]]						)
						elif model_name == "xclip-base-patch16-ucf-16-shot":
												lowercase__    :				Any=	torch.tensor([[9.8_2_1_9e-0_4, 9.9_5_9_3e-0_1, 3.0_8_6_3e-0_3]]						)
						# zero shot
						elif model_name == "xclip-base-patch16-zero-shot":
												lowercase__    :				Dict=	torch.tensor([[3.5_0_8_2e-0_4, 9.9_7_8_5e-0_1, 1.7_9_6_6e-0_3]]						)
						else:
												raise ValueError(f'''Model name {model_name} not supported'''						)
						assert torch.allclose(A ,		A ,		atol=1e-3						)
						print("Looks ok!"						)

						if pytorch_dump_folder_path is not None:
												print(f'''Saving model {model_name} to {pytorch_dump_folder_path}'''						)
												model.save_pretrained(A						)

						if push_to_hub:
												print("Pushing model, processor and slow tokenizer files to the hub..."						)
												model.push_to_hub(A ,		organization="nielsr"						)
												processor.push_to_hub(A ,		organization="nielsr"						)
												slow_tokenizer.push_to_hub(A ,		organization="nielsr"						)


if __name__ == "__main__":
			a			:    Any				 =		argparse.ArgumentParser()
			# Required parameters
			parser.add_argument(
			    """--model_name""",
			    default="""xclip-base-patch32""",
			    type=str,
			    help="""Name of the model.""",
			)
			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_xclip_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)














"""simple docstring"""







from typing import Dict, List, Optional, Tuple, Union

import torch

from ...models import AutoencoderKL, TransformeraDModel
from ...schedulers import KarrasDiffusionSchedulers
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput


class    __UpperCAmelCase(       SCREAMING_SNAKE_CASE__							):




						"""simple docstring"""




						def __init__(       self	,  snake_case__	,  snake_case__	,  snake_case__	,  snake_case__ = None	,  ):
												'''simple docstring'''
												super().__init__()
												self.register_modules(transformer=snake_case__	,  vae=snake_case__	,  scheduler=snake_case__      )

												# create a imagenet -> id dictionary for easier use
												lowercase__    :				int=	{}
												if idalabel is not None:
																		for key, value in idalabel.items():
																								for label in value.split(","      ):
																														lowercase__    :				Tuple=	int(snake_case__      )
																		lowercase__    :				Union[str, Any]=	dict(sorted(self.labels.items()      )      )




						def 		UpperCAmelCase_	(       self	,  snake_case__      ):
												'''simple docstring'''

												if not isinstance(snake_case__	,  snake_case__      ):
																		lowercase__    :				List[Any]=	list(snake_case__      )

												for l in label:
																		if l not in self.labels:
																								raise ValueError(
																								    F'''{l} does not exist. Please make sure to select one of the following labels: \n {self.labels}.'''      )

												return [self.labels[l] for l in label]




						@torch.no_grad()
						def __call__(       self	,  snake_case__	,  snake_case__ = 4.0	,  snake_case__ = None	,  snake_case__ = 50	,  snake_case__ = "pil"	,  snake_case__ = True	,  ):
												'''simple docstring'''
												lowercase__    :				List[Any]=	len(snake_case__      )
												lowercase__    :				Optional[int]=	self.transformer.config.sample_size
												lowercase__    :				List[str]=	self.transformer.config.in_channels

												lowercase__    :				Any=	randn_tensor(
												    shape=(batch_size, latent_channels, latent_size, latent_size)	,  generator=snake_case__	,  device=self.device	,  dtype=self.transformer.dtype	,  )
												lowercase__    :				Any=	torch.cat([latents] * 2      ) if guidance_scale > 1 else latents

												lowercase__    :				Tuple=	torch.tensor(snake_case__	,  device=self.device      ).reshape(-1      )
												lowercase__    :				Any=	torch.tensor([1000] * batch_size	,  device=self.device      )
												lowercase__    :				Tuple=	torch.cat([class_labels, class_null]	,  0      ) if guidance_scale > 1 else class_labels

												# set step values
												self.scheduler.set_timesteps(snake_case__      )

												for t in self.progress_bar(self.scheduler.timesteps      ):
																		if guidance_scale > 1:
																								lowercase__    :				List[str]=	latent_model_input[: len(snake_case__      ) // 2]
																								lowercase__    :				int=	torch.cat([half, half]	,  dim=0      )
																		lowercase__    :				Union[str, Any]=	self.scheduler.scale_model_input(snake_case__	,  snake_case__      )

																		lowercase__    :				Optional[int]=	t
																		if not torch.is_tensor(snake_case__      ):
																								# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
																								# This would be a good case for the `match` statement (Python 3.10+)
																								lowercase__    :				List[str]=	latent_model_input.device.type == "mps"
																								if isinstance(snake_case__	,  snake_case__      ):
																														lowercase__    :				int=	torch.floataa if is_mps else torch.floataa
																								else:
																														lowercase__    :				Dict=	torch.intaa if is_mps else torch.intaa
																								lowercase__    :				Tuple=	torch.tensor([timesteps]	,  dtype=snake_case__	,  device=latent_model_input.device      )
																		elif len(timesteps.shape      ) == 0:
																								lowercase__    :				Dict=	timesteps[None].to(latent_model_input.device      )
																		# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
																		lowercase__    :				int=	timesteps.expand(latent_model_input.shape[0]      )
																		# predict noise model_output
																		lowercase__    :				Union[str, Any]=	self.transformer(
																		    snake_case__	,  timestep=snake_case__	,  class_labels=snake_case__      ).sample

																		# perform guidance
																		if guidance_scale > 1:
																								lowercase__,     lowercase__    :				Tuple=	noise_pred[:, :latent_channels], noise_pred[:, latent_channels:]
																								lowercase__,     lowercase__    :				Union[str, Any]=	torch.split(snake_case__	,  len(snake_case__      ) // 2	,  dim=0      )

																								lowercase__    :				str=	uncond_eps + guidance_scale * (cond_eps - uncond_eps)
																								lowercase__    :				Dict=	torch.cat([half_eps, half_eps]	,  dim=0      )

																								lowercase__    :				Optional[int]=	torch.cat([eps, rest]	,  dim=1      )

																		# learned sigma
																		if self.transformer.config.out_channels // 2 == latent_channels:
																								lowercase__,     lowercase__    :				Union[str, Any]=	torch.split(snake_case__	,  snake_case__	,  dim=1      )
																		else:
																								lowercase__    :				int=	noise_pred

																		# compute previous image: x_t -> x_t-1
																		lowercase__    :				List[Any]=	self.scheduler.step(snake_case__	,  snake_case__	,  snake_case__      ).prev_sample

												if guidance_scale > 1:
																		lowercase__,     lowercase__    :				Any=	latent_model_input.chunk(2	,  dim=0      )
												else:
																		lowercase__    :				str=	latent_model_input

												lowercase__    :				Dict=	1 / self.vae.config.scaling_factor * latents
												lowercase__    :				Any=	self.vae.decode(snake_case__      ).sample

												lowercase__    :				Tuple=	(samples / 2 + 0.5).clamp(0	,  1      )

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

												if output_type == "pil":
																		lowercase__    :				Optional[Any]=	self.numpy_to_pil(snake_case__      )

												if not return_dict:
																		return (samples,)

												return ImagePipelineOutput(images=snake_case__      )






