





import argparse
import ast
import logging
import os
import sys

import pandas as pd
import torch
from tqdm import tqdm

from transformers import BartForConditionalGeneration, RagRetriever, RagSequenceForGeneration, RagTokenForGeneration
from transformers import logging as transformers_logging


sys.path.append(os.path.join(os.getcwd()))  # noqa: E402 # isort:skip
from utils_rag import exact_match_score, fa_score  # noqa: E402 # isort:skip


snake_case__   :				Tuple						=	logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

transformers_logging.set_verbosity_info()



def _a							(      lowerCamelCase:  int					)      ->		List[str]:







      '''simple docstring'''






      if "token" in model_name_or_path:
            return "rag_token"
      if "sequence" in model_name_or_path:
            return "rag_sequence"
      if "bart" in model_name_or_path:
            return "bart"
      return None



def _a							(      lowerCamelCase:  List[Any]   ,							lowerCamelCase:  Tuple   ,							lowerCamelCase:  Any					)      ->		str:







      '''simple docstring'''






      return max(metric_fn(lowerCamelCase   ,							lowerCamelCase					) for gt in ground_truths					)



def _a							(      lowerCamelCase:  Any   ,							lowerCamelCase:  Optional[Any]   ,							lowerCamelCase:  List[str]					)      ->		Any:







      '''simple docstring'''






      __A			  =							[line.strip() for line in open(lowerCamelCase   ,							'''r'''					).readlines()]
      __A			  =							[]

      if args.gold_data_mode == "qa":
            __A			  =							pd.read_csv(lowerCamelCase   ,							sep='''\t'''   ,							header=lowerCamelCase					)
            for answer_list in data[1]:
                  __A			  =							ast.literal_eval(lowerCamelCase					)
                  answers.append(lowerCamelCase					)
      else:
            __A			  =							[line.strip() for line in open(lowerCamelCase   ,							'''r'''					).readlines()]
            __A			  =							[[reference] for reference in references]

      __A			  =							__A			  =							__A			  =							0
      for prediction, ground_truths in zip(lowerCamelCase   ,							lowerCamelCase					):
            total += 1
            em += metric_max_over_ground_truths(lowerCamelCase   ,							lowerCamelCase   ,							lowerCamelCase					)
            fa += metric_max_over_ground_truths(lowerCamelCase   ,							lowerCamelCase   ,							lowerCamelCase					)

      __A			  =							100.0 * em / total
      __A			  =							100.0 * fa / total

      logger.info(F"""F1: {fa:.2f}"""					)
      logger.info(F"""EM: {em:.2f}"""					)



def _a							(      lowerCamelCase:  int   ,							lowerCamelCase:  int   ,							lowerCamelCase:  Tuple					)      ->		List[str]:







      '''simple docstring'''






      __A			  =							args.k
      __A			  =							[line.strip() for line in open(lowerCamelCase   ,							'''r'''					).readlines()]
      __A			  =							[line.strip() for line in open(lowerCamelCase   ,							'''r'''					).readlines()]

      __A			  =							__A			  =							0
      for hypo, reference in zip(lowerCamelCase   ,							lowerCamelCase					):
            __A			  =							set(hypo.split('''\t'''					)[:k]					)
            __A			  =							set(reference.split('''\t'''					)					)
            total += 1
            em += len(hypo_provenance & ref_provenance					) / k

      __A			  =							100.0 * em / total
      logger.info(F"""Precision@{k}: {em: .2f}"""					)



def _a							(      lowerCamelCase:  int   ,							lowerCamelCase:  List[Any]   ,							lowerCamelCase:  int					)      ->		int:







      '''simple docstring'''






      def strip_title(lowerCamelCase:  int					):
            if title.startswith('''"'''					):
                  __A			  =							title[1:]
            if title.endswith('''"'''					):
                  __A			  =							title[:-1]
            return title

      __A			  =							rag_model.retriever.question_encoder_tokenizer.batch_encode_plus(
          lowerCamelCase   ,							return_tensors='''pt'''   ,							padding=lowerCamelCase   ,							truncation=lowerCamelCase   ,							)['''input_ids'''].to(args.device					)

      __A			  =							rag_model.rag.question_encoder(lowerCamelCase					)
      __A			  =							question_enc_outputs[0]

      __A			  =							rag_model.retriever(
          lowerCamelCase   ,							question_enc_pool_output.cpu().detach().to(torch.floataa					).numpy()   ,							prefix=rag_model.rag.generator.config.prefix   ,							n_docs=rag_model.config.n_docs   ,							return_tensors='''pt'''   ,							)
      __A			  =							rag_model.retriever.index.get_doc_dicts(result.doc_ids					)
      __A			  =							[]
      for docs in all_docs:
            __A			  =							[strip_title(lowerCamelCase					) for title in docs['''title''']]
            provenance_strings.append('''\t'''.join(lowerCamelCase					)					)
      return provenance_strings



def _a							(      lowerCamelCase:  Tuple   ,							lowerCamelCase:  List[Any]   ,							lowerCamelCase:  Union[str, Any]					)      ->		Dict:







      '''simple docstring'''






      with torch.no_grad():
            __A			  =							rag_model.retriever.question_encoder_tokenizer.batch_encode_plus(
                lowerCamelCase   ,							return_tensors='''pt'''   ,							padding=lowerCamelCase   ,							truncation=lowerCamelCase					)

            __A			  =							inputs_dict.input_ids.to(args.device					)
            __A			  =							inputs_dict.attention_mask.to(args.device					)
            __A			  =							rag_model.generate(  # rag_model overwrites generate
                lowerCamelCase   ,							attention_mask=lowerCamelCase   ,							num_beams=args.num_beams   ,							min_length=args.min_length   ,							max_length=args.max_length   ,							early_stopping=lowerCamelCase   ,							num_return_sequences=1   ,							bad_words_ids=[[0, 0]]   ,							)
            __A			  =							rag_model.retriever.generator_tokenizer.batch_decode(lowerCamelCase   ,							skip_special_tokens=lowerCamelCase					)

            if args.print_predictions:
                  for q, a in zip(lowerCamelCase   ,							lowerCamelCase					):
                        logger.info('''Q: {} - A: {}'''.format(lowerCamelCase   ,							lowerCamelCase					)					)

            return answers



def _a							(      )      ->		Optional[Any]:







      '''simple docstring'''






      __A			  =							argparse.ArgumentParser()
      parser.add_argument(
          '''--model_type'''   ,							choices=['''rag_sequence''', '''rag_token''', '''bart''']   ,							type=lowerCamelCase   ,							help=(
              '''RAG model type: rag_sequence, rag_token or bart, if none specified, the type is inferred from the'''
              ''' model_name_or_path'''
          )   ,							)
      parser.add_argument(
          '''--index_name'''   ,							default=lowerCamelCase   ,							choices=['''exact''', '''compressed''', '''legacy''']   ,							type=lowerCamelCase   ,							help='''RAG model retriever type'''   ,							)
      parser.add_argument(
          '''--index_path'''   ,							default=lowerCamelCase   ,							type=lowerCamelCase   ,							help='''Path to the retrieval index'''   ,							)
      parser.add_argument('''--n_docs'''   ,							default=5   ,							type=lowerCamelCase   ,							help='''Number of retrieved docs'''					)
      parser.add_argument(
          '''--model_name_or_path'''   ,							default=lowerCamelCase   ,							type=lowerCamelCase   ,							required=lowerCamelCase   ,							help='''Path to pretrained checkpoints or model identifier from huggingface.co/models'''   ,							)
      parser.add_argument(
          '''--eval_mode'''   ,							choices=['''e2e''', '''retrieval''']   ,							default='''e2e'''   ,							type=lowerCamelCase   ,							help=(
              '''Evaluation mode, e2e calculates exact match and F1 of the downstream task, retrieval calculates'''
              ''' precision@k.'''
          )   ,							)
      parser.add_argument('''--k'''   ,							default=1   ,							type=lowerCamelCase   ,							help='''k for the precision@k calculation'''					)
      parser.add_argument(
          '''--evaluation_set'''   ,							default=lowerCamelCase   ,							type=lowerCamelCase   ,							required=lowerCamelCase   ,							help='''Path to a file containing evaluation samples'''   ,							)
      parser.add_argument(
          '''--gold_data_path'''   ,							default=lowerCamelCase   ,							type=lowerCamelCase   ,							required=lowerCamelCase   ,							help='''Path to a tab-separated file with gold samples'''   ,							)
      parser.add_argument(
          '''--gold_data_mode'''   ,							default='''qa'''   ,							type=lowerCamelCase   ,							choices=['''qa''', '''ans''']   ,							help=(
              '''Format of the gold data file'''
              '''qa - a single line in the following format: question [tab] answer_list'''
              '''ans - a single line of the gold file contains the expected answer string'''
          )   ,							)
      parser.add_argument(
          '''--predictions_path'''   ,							type=lowerCamelCase   ,							default='''predictions.txt'''   ,							help='''Name of the predictions file, to be stored in the checkpoints directory'''   ,							)
      parser.add_argument(
          '''--eval_all_checkpoints'''   ,							action='''store_true'''   ,							help='''Evaluate all checkpoints starting with the same prefix as model_name ending and ending with step number'''   ,							)
      parser.add_argument(
          '''--eval_batch_size'''   ,							default=8   ,							type=lowerCamelCase   ,							help='''Batch size per GPU/CPU for evaluation.'''   ,							)
      parser.add_argument(
          '''--recalculate'''   ,							help='''Recalculate predictions even if the prediction file exists'''   ,							action='''store_true'''   ,							)
      parser.add_argument(
          '''--num_beams'''   ,							default=4   ,							type=lowerCamelCase   ,							help='''Number of beams to be used when generating answers'''   ,							)
      parser.add_argument('''--min_length'''   ,							default=1   ,							type=lowerCamelCase   ,							help='''Min length of the generated answers'''					)
      parser.add_argument('''--max_length'''   ,							default=50   ,							type=lowerCamelCase   ,							help='''Max length of the generated answers'''					)

      parser.add_argument(
          '''--print_predictions'''   ,							action='''store_true'''   ,							help='''If True, prints predictions while evaluating.'''   ,							)
      parser.add_argument(
          '''--print_docs'''   ,							action='''store_true'''   ,							help='''If True, prints docs retried while generating.'''   ,							)
      __A			  =							parser.parse_args()
      __A			  =							torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''					)
      return args



def _a							(      lowerCamelCase:  int					)      ->		Dict:







      '''simple docstring'''






      __A			  =							{}
      if args.model_type is None:
            __A			  =							infer_model_type(args.model_name_or_path					)
            assert args.model_type is not None
      if args.model_type.startswith('''rag'''					):
            __A			  =							RagTokenForGeneration if args.model_type == '''rag_token''' else RagSequenceForGeneration
            __A			  =							args.n_docs
            if args.index_name is not None:
                  __A			  =							args.index_name
            if args.index_path is not None:
                  __A			  =							args.index_path
      else:
            __A			  =							BartForConditionalGeneration

      __A			  =							(
          [f.path for f in os.scandir(args.model_name_or_path					) if f.is_dir()]
          if args.eval_all_checkpoints
          else [args.model_name_or_path]
      )

      logger.info('''Evaluate the following checkpoints: %s'''   ,							lowerCamelCase					)

      __A			  =							get_scores if args.eval_mode == '''e2e''' else get_precision_at_k
      __A			  =							evaluate_batch_eae if args.eval_mode == '''e2e''' else evaluate_batch_retrieval

      for checkpoint in checkpoints:
            if os.path.exists(args.predictions_path					) and (not args.recalculate):
                  logger.info('''Calculating metrics based on an existing predictions file: {}'''.format(args.predictions_path					)					)
                  score_fn(lowerCamelCase   ,							args.predictions_path   ,							args.gold_data_path					)
                  continue

            logger.info('''***** Running evaluation for {} *****'''.format(lowerCamelCase					)					)
            logger.info('''  Batch size = %d'''   ,							args.eval_batch_size					)
            logger.info('''  Predictions will be stored under {}'''.format(args.predictions_path					)					)

            if args.model_type.startswith('''rag'''					):
                  __A			  =							RagRetriever.from_pretrained(lowerCamelCase   ,							**lowerCamelCase					)
                  __A			  =							model_class.from_pretrained(lowerCamelCase   ,							retriever=lowerCamelCase   ,							**lowerCamelCase					)
                  model.retriever.init_retrieval()
            else:
                  __A			  =							model_class.from_pretrained(lowerCamelCase   ,							**lowerCamelCase					)
            model.to(args.device					)

            with open(args.evaluation_set   ,							'''r'''					) as eval_file, open(args.predictions_path   ,							'''w'''					) as preds_file:
                  __A			  =							[]
                  for line in tqdm(lowerCamelCase					):
                        questions.append(line.strip()					)
                        if len(lowerCamelCase					) == args.eval_batch_size:
                              __A			  =							evaluate_batch_fn(lowerCamelCase   ,							lowerCamelCase   ,							lowerCamelCase					)
                              preds_file.write('''\n'''.join(lowerCamelCase					) + '''\n'''					)
                              preds_file.flush()
                              __A			  =							[]
                  if len(lowerCamelCase					) > 0:
                        __A			  =							evaluate_batch_fn(lowerCamelCase   ,							lowerCamelCase   ,							lowerCamelCase					)
                        preds_file.write('''\n'''.join(lowerCamelCase					)					)
                        preds_file.flush()

                  score_fn(lowerCamelCase   ,							args.predictions_path   ,							args.gold_data_path					)


if __name__ == "__main__":
       snake_case__   :				List[str]						=	get_args()
       main(args)












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					)





