




import logging
import pathlib
import re
import sys
from dataclasses import dataclass, field
from typing import Any, Callable, Dict, List, Optional, Set, Union

import datasets
import librosa
import numpy as np
import torch
from lang_trans import arabic
from packaging import version
from torch import nn

from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaForCTC,
    WavaVecaProcessor,
    is_apex_available,
    trainer_utils,
)


if is_apex_available():
		from apex import amp

if version.parse(version.parse(torch.__version__).base_version) >= version.parse("""1.6"""):
		UpperCAmelCase_    =True
		from torch.cuda.amp import autocast


UpperCAmelCase_    =logging.getLogger(__name__)


@dataclass
class 				__UpperCamelCase      :





				'''simple docstring'''



				__a				:			Optional[int]					    =field(
				    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
				__a				:			str					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}  ,       )
				__a				:			Optional[int]					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""}	)
				__a				:			str					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Whether to log verbose messages or not."""}  ,       )




def    UpperCAmelCase							(  _snake_case    ,			_snake_case				):
							logging.basicConfig(
							    format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s'''    ,			datefmt='''%m/%d/%Y %H:%M:%S'''    ,			handlers=[logging.StreamHandler(sys.stdout				)]    ,			)
							lowerCAmelCase =		logging.WARNING
							if model_args.verbose_logging:
														lowerCAmelCase =		logging.DEBUG
							elif trainer_utils.is_main_process(training_args.local_rank				):
														lowerCAmelCase =		logging.INFO
							logger.setLevel(_snake_case				)


@dataclass
class 				__UpperCamelCase      :





				'''simple docstring'''



				__a				:			str					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """The name of the dataset to use (via the datasets library)."""}	)
				__a				:			str					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""}	)
				__a				:			List[str]					    =field(
				    default="""train"""  ,       metadata={
				        """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'"""
				    }  ,       )
				__a				:			Any					    =field(
				    default="""validation"""  ,       metadata={
				        """help""": (
				            """The name of the validation data set split to use (via the datasets library). Defaults to 'validation'"""
				        )
				    }  ,       )
				__a				:			int					    =field(
				    default="""text"""  ,       metadata={"""help""": """Column in the dataset that contains label (target text). Defaults to 'text'"""}  ,       )
				__a				:			int					    =field(
				    default="""file"""  ,       metadata={"""help""": """Column in the dataset that contains speech file path. Defaults to 'file'"""}  ,       )
				__a				:			Union[str, Any]					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Resample loaded audio to target feature extractor's sampling rate or not."""}  ,       )
				__a				:			List[str]					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Filters out examples longer than specified. Defaults to no filtering."""}  ,       )
				__a				:			Dict					    =field(
				    default="""librispeech"""  ,       metadata={
				        """help""": (
				            """Orthography used for normalization and tokenization: 'librispeech' (default), 'timit', or"""
				            """ 'buckwalter'."""
				        )
				    }  ,       )
				__a				:			List[Any]					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""}	)
				__a				:			Tuple					    =field(
				    default=_lowerCamelCase  ,       metadata={"""help""": """The number of processes to use for the preprocessing."""}  ,       )




@dataclass
class 				__UpperCamelCase      :





				'''simple docstring'''



				__a				:			Tuple					    =False
				__a				:			Optional[Any]					    =None
				__a				:			Optional[Any]					    ="""|"""
				__a				:			Union[str, Any]					    =field(default_factory=_lowerCamelCase	)
				__a				:			Dict					    =field(default_factory=_lowerCamelCase	)
				__a				:			List[Any]					    =None



				@classmethod
				def    __snake_case		(			cls							,			UpperCAmelCase_    ):
											if name == "librispeech":
																		return cls()
											if name == "timit":
																		return cls(
																		    do_lower_case=_UpperCamelCase							,			translation_table=str.maketrans({'''-''': ''' '''}    )							,			)
											if name == "buckwalter":
																		lowerCAmelCase =		{
																		    '''-''': ''' ''',  # sometimes used to represent pauses
																		    '''^''': '''v''',  # fixing "tha" in arabic_speech_corpus dataset
																		}
																		return cls(
																		    vocab_file=pathlib.Path(_UpperCamelCase    ).parent.joinpath('''vocab/buckwalter.json'''    )							,			word_delimiter_token='''/'''							,			translation_table=str.maketrans(_UpperCamelCase    )							,			words_to_remove={'''sil'''}							,			untransliterator=arabic.buckwalter.untransliterate							,			)
											raise ValueError(F"""Unsupported orthography: '{name}'."""    )



				def    __snake_case		(			self							,			UpperCAmelCase_    ):
											# TODO(elgeish) return a pipeline (e.g., from jiwer) instead? Or rely on branch predictor as is
											if len(self.translation_table    ) > 0:
																		lowerCAmelCase =		text.translate(self.translation_table    )
											if len(self.words_to_remove    ) == 0:
																		lowerCAmelCase =		''' '''.join(text.split()    )  # clean up whitespaces
											else:
																		lowerCAmelCase =		''' '''.join(w for w in text.split() if w not in self.words_to_remove    )  # and clean up whilespaces
											return text






				def    __snake_case		(			self							,			UpperCAmelCase_    ):
											lowerCAmelCase =		WavaVecaFeatureExtractor.from_pretrained(
											    model_args.model_name_or_path							,			cache_dir=model_args.cache_dir    )
											if self.vocab_file:
																		lowerCAmelCase =		WavaVecaCTCTokenizer(
																		    self.vocab_file							,			cache_dir=model_args.cache_dir							,			do_lower_case=self.do_lower_case							,			word_delimiter_token=self.word_delimiter_token							,			)
											else:
																		lowerCAmelCase =		WavaVecaCTCTokenizer.from_pretrained(
																		    model_args.model_name_or_path							,			cache_dir=model_args.cache_dir							,			do_lower_case=self.do_lower_case							,			word_delimiter_token=self.word_delimiter_token							,			)
											return WavaVecaProcessor(_UpperCamelCase							,			_UpperCamelCase    )




@dataclass
class 				__UpperCamelCase      :





				'''simple docstring'''



				__a				:			Dict					    =4_2
				__a				:			Dict					    =True
				__a				:			Optional[Any]					    =None
				__a				:			str					    =None
				__a				:			str					    =None
				__a				:			Optional[Any]					    =None



				def __call__(			self							,			UpperCAmelCase_    ):
											# split inputs and labels since they have to be of different lenghts and need
											# different padding methods
											lowerCAmelCase =		[{'''input_values''': feature['''input_values''']} for feature in features]
											lowerCAmelCase =		[{'''input_ids''': feature['''labels''']} for feature in features]

											lowerCAmelCase =		self.processor.pad(
											    _UpperCamelCase							,			padding=self.padding							,			max_length=self.max_length							,			pad_to_multiple_of=self.pad_to_multiple_of							,			return_tensors='''pt'''							,			)
											lowerCAmelCase =		self.processor.pad(
											    labels=_UpperCamelCase							,			padding=self.padding							,			max_length=self.max_length_labels							,			pad_to_multiple_of=self.pad_to_multiple_of_labels							,			return_tensors='''pt'''							,			)

											# replace padding with -100 to ignore loss correctly
											lowerCAmelCase =		labels_batch['''input_ids'''].masked_fill(labels_batch.attention_mask.ne(1    )							,			-1_00    )

											lowerCAmelCase =		labels

											return batch




class 				__UpperCamelCase      (			_lowerCamelCase	):





				'''simple docstring'''



				def    __snake_case		(			self							,			UpperCAmelCase_							,			UpperCAmelCase_    ):
											model.train()
											lowerCAmelCase =		self._prepare_inputs(_UpperCamelCase    )

											if self.use_amp:
																		with autocast():
																									lowerCAmelCase =		self.compute_loss(_UpperCamelCase							,			_UpperCamelCase    )
											else:
																		lowerCAmelCase =		self.compute_loss(_UpperCamelCase							,			_UpperCamelCase    )

											if self.args.n_gpu > 1:
																		if model.module.config.ctc_loss_reduction == "mean":
																									lowerCAmelCase =		loss.mean()
																		elif model.module.config.ctc_loss_reduction == "sum":
																									lowerCAmelCase =		loss.sum() / (inputs['''labels'''] >= 0).sum()
																		else:
																									raise ValueError(F"""{model.config.ctc_loss_reduction} is not valid. Choose one of ['mean', 'sum']"""    )

											if self.args.gradient_accumulation_steps > 1:
																		lowerCAmelCase =		loss / self.args.gradient_accumulation_steps

											if self.use_amp:
																		self.scaler.scale(_UpperCamelCase    ).backward()
											elif self.use_apex:
																		with amp.scale_loss(_UpperCamelCase							,			self.optimizer    ) as scaled_loss:
																									scaled_loss.backward()
											elif self.deepspeed:
																		self.deepspeed.backward(_UpperCamelCase    )
											else:
																		loss.backward()

											return loss.detach()




def    UpperCAmelCase							(  ):
							lowerCAmelCase =		HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)				)

							lowerCAmelCase ,  lowerCAmelCase ,  lowerCAmelCase =		parser.parse_args_into_dataclasses()
							configure_logger(_snake_case    ,			_snake_case				)

							lowerCAmelCase =		Orthography.from_name(data_args.orthography.lower()				)
							lowerCAmelCase =		orthography.create_processor(_snake_case				)
							lowerCAmelCase =		WavaVecaForCTC.from_pretrained(
							    model_args.model_name_or_path    ,			cache_dir=model_args.cache_dir    ,			gradient_checkpointing=training_args.gradient_checkpointing    ,			vocab_size=len(processor.tokenizer				)    ,			)

							lowerCAmelCase =		datasets.load_dataset(
							    data_args.dataset_name    ,			data_args.dataset_config_name    ,			split=data_args.train_split_name				)
							lowerCAmelCase =		datasets.load_dataset(
							    data_args.dataset_name    ,			data_args.dataset_config_name    ,			split=data_args.validation_split_name				)

							lowerCAmelCase =		datasets.load_metric('''wer'''				)
							lowerCAmelCase =		processor.feature_extractor.sampling_rate if data_args.target_feature_extractor_sampling_rate else None
							lowerCAmelCase =		''''''.join(t for t in processor.tokenizer.get_vocab().keys() if len(_snake_case				) == 1				)
							lowerCAmelCase =		re.compile(  # remove characters not in vocabulary
							    RF"""[^\s{re.escape(_snake_case				)}]"""    ,			flags=re.IGNORECASE if processor.tokenizer.do_lower_case else 0    ,			)
							lowerCAmelCase =		[]

							def prepare_example(_snake_case				):  # TODO(elgeish) make use of multiprocessing?
														lowerCAmelCase ,  lowerCAmelCase =		librosa.load(example[data_args.speech_file_column]    ,			sr=_snake_case				)
														if data_args.max_duration_in_seconds is not None:
																					lowerCAmelCase =		len(example['''speech''']				) / example['''sampling_rate''']
														# Normalize and clean up text; order matters!
														lowerCAmelCase =		orthography.preprocess_for_training(example[data_args.target_text_column]				)
														lowerCAmelCase =		vocabulary_text_cleaner.sub(''''''    ,			_snake_case				)
														if updated_text != example[data_args.target_text_column]:
																					text_updates.append((example[data_args.target_text_column], updated_text)				)
																					lowerCAmelCase =		updated_text
														return example

							lowerCAmelCase =		train_dataset.map(_snake_case    ,			remove_columns=[data_args.speech_file_column]				)
							lowerCAmelCase =		val_dataset.map(_snake_case    ,			remove_columns=[data_args.speech_file_column]				)

							if data_args.max_duration_in_seconds is not None:

														def filter_by_max_duration(_snake_case				):
																					return example["duration_in_seconds"] <= data_args.max_duration_in_seconds

														lowerCAmelCase =		len(_snake_case				)
														lowerCAmelCase =		len(_snake_case				)
														lowerCAmelCase =		train_dataset.filter(_snake_case    ,			remove_columns=['''duration_in_seconds''']				)
														lowerCAmelCase =		val_dataset.filter(_snake_case    ,			remove_columns=['''duration_in_seconds''']				)
														if len(_snake_case				) > old_train_size:
																					logger.warning(
																					    F"""Filtered out {len(_snake_case				) - old_train_size} train example(s) longer than"""
																					    F""" {data_args.max_duration_in_seconds} second(s)."""				)
														if len(_snake_case				) > old_val_size:
																					logger.warning(
																					    F"""Filtered out {len(_snake_case				) - old_val_size} validation example(s) longer than"""
																					    F""" {data_args.max_duration_in_seconds} second(s)."""				)
							logger.info(F"""Split sizes: {len(_snake_case				)} train and {len(_snake_case				)} validation."""				)

							logger.warning(F"""Updated {len(_snake_case				)} transcript(s) using '{data_args.orthography}' orthography rules."""				)
							if logger.isEnabledFor(logging.DEBUG				):
														for original_text, updated_text in text_updates:
																					logger.debug(F"""Updated text: \"{original_text}\" -> \"{updated_text}\""""				)
							lowerCAmelCase =		None

							def prepare_dataset(_snake_case				):
														# check that all files have the correct sampling rate
														assert (
														    len(set(batch['''sampling_rate''']				)				) == 1
														), F"""Make sure all inputs have the same sampling rate of {processor.feature_extractor.sampling_rate}."""

														lowerCAmelCase =		processor(
														    audio=batch['''speech''']    ,			text=batch[data_args.target_text_column]    ,			sampling_rate=batch['''sampling_rate'''][0]				)
														batch.update(_snake_case				)
														return batch

							lowerCAmelCase =		train_dataset.map(
							    _snake_case    ,			batch_size=training_args.per_device_train_batch_size    ,			batched=_snake_case    ,			num_proc=data_args.preprocessing_num_workers    ,			)
							lowerCAmelCase =		val_dataset.map(
							    _snake_case    ,			batch_size=training_args.per_device_train_batch_size    ,			batched=_snake_case    ,			num_proc=data_args.preprocessing_num_workers    ,			)

							lowerCAmelCase =		DataCollatorCTCWithPadding(processor=_snake_case    ,			padding=_snake_case				)

							def compute_metrics(_snake_case				):
														lowerCAmelCase =		pred.predictions
														lowerCAmelCase =		np.argmax(_snake_case    ,			axis=-1				)

														lowerCAmelCase =		processor.tokenizer.pad_token_id

														lowerCAmelCase =		processor.batch_decode(_snake_case				)
														# we do not want to group tokens when computing the metrics
														lowerCAmelCase =		processor.batch_decode(pred.label_ids    ,			group_tokens=_snake_case				)
														if logger.isEnabledFor(logging.DEBUG				):
																					for reference, predicted in zip(_snake_case    ,			_snake_case				):
																												logger.debug(F"""reference: \"{reference}\""""				)
																												logger.debug(F"""predicted: \"{predicted}\""""				)
																												if orthography.untransliterator is not None:
																																			logger.debug(F"""reference (untransliterated): \"{orthography.untransliterator(_snake_case				)}\""""				)
																																			logger.debug(F"""predicted (untransliterated): \"{orthography.untransliterator(_snake_case				)}\""""				)

														lowerCAmelCase =		wer_metric.compute(predictions=_snake_case    ,			references=_snake_case				)

														return {"wer": wer}

							if model_args.freeze_feature_extractor:
														model.freeze_feature_extractor()

							lowerCAmelCase =		CTCTrainer(
							    model=_snake_case    ,			data_collator=_snake_case    ,			args=_snake_case    ,			compute_metrics=_snake_case    ,			train_dataset=_snake_case    ,			eval_dataset=_snake_case    ,			tokenizer=processor.feature_extractor    ,			)

							trainer.train()


if __name__ == "__main__":
		main()










import math
from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from transformers import CLIPTextModelWithProjection, CLIPTokenizer

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


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

snake_case__   :				Any						=	'\n    Examples:\n        ```py\n        >>> import torch\n        >>> from diffusers import DiffusionPipeline\n        >>> from diffusers.utils import export_to_gif\n\n        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n\n        >>> repo = "openai/shap-e"\n        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n        >>> pipe = pipe.to(device)\n\n        >>> guidance_scale = 15.0\n        >>> prompt = "a shark"\n\n        >>> images = pipe(\n        ...     prompt,\n        ...     guidance_scale=guidance_scale,\n        ...     num_inference_steps=64,\n        ...     frame_size=256,\n        ... ).images\n\n        >>> gif_path = export_to_gif(images[0], "shark_3d.gif")\n        ```\n'





@dataclass
class A_   (     _lowerCamelCase  ):
   lowerCAmelCase__  	=					42





class A_   (     _lowerCamelCase  ):




   def __init__(self      :List[str]       ,  _UpperCamelCase      :PriorTransformer       ,  _UpperCamelCase      :CLIPTextModelWithProjection       ,  _UpperCamelCase      :CLIPTokenizer       ,  _UpperCamelCase      :HeunDiscreteScheduler       ,  _UpperCamelCase      :ShapERenderer       ,  )->   Tuple:
         super().__init__()

         self.register_modules(
             prior=_UpperCamelCase       ,  text_encoder=_UpperCamelCase       ,  tokenizer=_UpperCamelCase       ,  scheduler=_UpperCamelCase       ,  renderer=_UpperCamelCase       ,  )




   def _lowerCAmelCase (self      :Any       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :Union[str, Any]       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :Union[str, Any]					)->   Any:
         if latents is None:
               __A			  =							randn_tensor(_UpperCamelCase       ,  generator=_UpperCamelCase       ,  device=_UpperCamelCase       ,  dtype=_UpperCamelCase					)
         else:
               if latents.shape != shape:
                     raise ValueError(f"""Unexpected latents shape, got {latents.shape}, expected {shape}"""					)
               __A			  =							latents.to(_UpperCamelCase					)

         __A			  =							latents * scheduler.init_noise_sigma
         return latents




   def _lowerCAmelCase (self      :Union[str, Any]       ,  _UpperCamelCase      :Dict=0					)->   Optional[Any]:
         if is_accelerate_available():
               from accelerate import cpu_offload
         else:
               raise ImportError('''Please install accelerate via `pip install accelerate`'''					)

         __A			  =							torch.device(f"""cuda:{gpu_id}"""					)

         __A			  =							[self.text_encoder, self.prior]
         for cpu_offloaded_model in models:
               if cpu_offloaded_model is not None:
                     cpu_offload(_UpperCamelCase       ,  _UpperCamelCase					)




   def _lowerCAmelCase (self      :Tuple       ,  _UpperCamelCase      :Dict=0					)->   Tuple:
         if is_accelerate_available() and is_accelerate_version('''>='''       ,  '''0.17.0.dev0'''					):
               from accelerate import cpu_offload_with_hook
         else:
               raise ImportError('''`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher.'''					)

         __A			  =							torch.device(f"""cuda:{gpu_id}"""					)

         if self.device.type != "cpu":
               self.to('''cpu'''       ,  silence_dtype_warnings=_UpperCamelCase					)
               torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

         __A			  =							None
         for cpu_offloaded_model in [self.text_encoder, self.prior, self.renderer]:
               __A  ,		__A			  =							cpu_offload_with_hook(_UpperCamelCase       ,  _UpperCamelCase       ,  prev_module_hook=_UpperCamelCase					)

         if self.safety_checker is not None:
               __A  ,		__A			  =							cpu_offload_with_hook(self.safety_checker       ,  _UpperCamelCase       ,  prev_module_hook=_UpperCamelCase					)

         # We'll offload the last model manually.
         __A			  =							hook




   @property
   def _lowerCAmelCase (self      :int					)->   int:
         if self.device != torch.device('''meta'''					) or not hasattr(self.text_encoder       ,  '''_hf_hook'''					):
               return self.device
         for module in self.text_encoder.modules():
               if (
                   hasattr(_UpperCamelCase       ,  '''_hf_hook'''					)
                   and hasattr(module._hf_hook       ,  '''execution_device'''					)
                   and module._hf_hook.execution_device is not None
               ):
                     return torch.device(module._hf_hook.execution_device					)
         return self.device




   def _lowerCAmelCase (self      :str       ,  _UpperCamelCase      :Optional[Any]       ,  _UpperCamelCase      :Optional[int]       ,  _UpperCamelCase      :Any       ,  _UpperCamelCase      :int       ,  )->   Dict:
         len(_UpperCamelCase					) if isinstance(_UpperCamelCase       ,  _UpperCamelCase					) else 1

         # YiYi Notes: set pad_token_id to be 0, not sure why I can't set in the config file
         __A			  =							0
         # get prompt text embeddings
         __A			  =							self.tokenizer(
             _UpperCamelCase       ,  padding='''max_length'''       ,  max_length=self.tokenizer.model_max_length       ,  truncation=_UpperCamelCase       ,  return_tensors='''pt'''       ,  )
         __A			  =							text_inputs.input_ids
         __A			  =							self.tokenizer(_UpperCamelCase       ,  padding='''longest'''       ,  return_tensors='''pt'''					).input_ids

         if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(_UpperCamelCase       ,  _UpperCamelCase					):
               __A			  =							self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]					)
               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			  =							self.text_encoder(text_input_ids.to(_UpperCamelCase					)					)
         __A			  =							text_encoder_output.text_embeds

         __A			  =							prompt_embeds.repeat_interleave(_UpperCamelCase       ,  dim=0					)
         # in Shap-E it normalize the prompt_embeds and then later rescale it
         __A			  =							prompt_embeds / torch.linalg.norm(_UpperCamelCase       ,  dim=-1       ,  keepdim=_UpperCamelCase					)

         if do_classifier_free_guidance:
               __A			  =							torch.zeros_like(_UpperCamelCase					)

               # 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([negative_prompt_embeds, prompt_embeds]					)

         # Rescale the features to have unit variance
         __A			  =							math.sqrt(prompt_embeds.shape[1]					) * prompt_embeds

         return prompt_embeds






   @torch.no_grad()
   @replace_example_docstring(_UpperCamelCase					)
   def __call__(self      :Optional[Any]       ,  _UpperCamelCase      :str       ,  _UpperCamelCase      :int = 1       ,  _UpperCamelCase      :int = 25       ,  _UpperCamelCase      :Optional[Union[torch.Generator, List[torch.Generator]]] = None       ,  _UpperCamelCase      :Optional[torch.FloatTensor] = None       ,  _UpperCamelCase      :float = 4.0       ,  _UpperCamelCase      :int = 64       ,  _UpperCamelCase      :Optional[str] = "pil"       ,  _UpperCamelCase      :bool = True       ,  )->   Optional[Any]:

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

         __A			  =							self._execution_device

         __A			  =							batch_size * num_images_per_prompt

         __A			  =							guidance_scale > 1.0
         __A			  =							self._encode_prompt(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase					)

         # prior

         self.scheduler.set_timesteps(_UpperCamelCase       ,  device=_UpperCamelCase					)
         __A			  =							self.scheduler.timesteps

         __A			  =							self.prior.config.num_embeddings
         __A			  =							self.prior.config.embedding_dim

         __A			  =							self.prepare_latents(
             (batch_size, num_embeddings * embedding_dim)       ,  prompt_embeds.dtype       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase       ,  self.scheduler       ,  )

         # YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
         __A			  =							latents.reshape(latents.shape[0]       ,  _UpperCamelCase       ,  _UpperCamelCase					)

         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					)

               __A			  =							self.prior(
                   _UpperCamelCase       ,  timestep=_UpperCamelCase       ,  proj_embedding=_UpperCamelCase       ,  ).predicted_image_embedding

               # remove the variance
               __A  ,		__A			  =							noise_pred.split(
                   scaled_model_input.shape[2]       ,  dim=2					)  # batch_size, num_embeddings, embedding_dim

               if do_classifier_free_guidance is not None:
                     __A  ,		__A			  =							noise_pred.chunk(2					)
                     __A			  =							noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

               __A			  =							self.scheduler.step(
                   _UpperCamelCase       ,  timestep=_UpperCamelCase       ,  sample=_UpperCamelCase       ,  ).prev_sample

         if output_type == "latent":
               return ShapEPipelineOutput(images=_UpperCamelCase					)

         __A			  =							[]
         for i, latent in enumerate(_UpperCamelCase					):
               __A			  =							self.renderer.decode(
                   latent[None, :]       ,  _UpperCamelCase       ,  size=_UpperCamelCase       ,  ray_batch_size=4096       ,  n_coarse_samples=64       ,  n_fine_samples=128       ,  )
               images.append(_UpperCamelCase					)

         __A			  =							torch.stack(_UpperCamelCase					)

         if output_type not in ["np", "pil"]:
               raise ValueError(f"""Only the output types `pil` and `np` are supported not output_type={output_type}"""					)

         __A			  =							images.cpu().numpy()

         if output_type == "pil":
               __A			  =							[self.numpy_to_pil(_UpperCamelCase					) for image in images]

         # Offload last model to CPU
         if hasattr(self       ,  '''final_offload_hook'''					) and self.final_offload_hook is not None:
               self.final_offload_hook.offload()

         if not return_dict:
               return (images,)

         return ShapEPipelineOutput(images=_UpperCamelCase					)





