




'''simple docstring'''



from logging import getLogger
from typing import Any, Callable, List, Optional, Union

import numpy as np
import PIL
import torch

from ...schedulers import DDPMScheduler
from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
from ..pipeline_utils import ImagePipelineOutput
from . import StableDiffusionUpscalePipeline


lowerCamelCase_	 =		getLogger(__name__)


lowerCamelCase_	 =		4
lowerCamelCase_	 =		7

lowerCamelCase_	 =		{
    'float16': torch.floataa,
    'float32': torch.floataa,
}
def 				SCREAMING_SNAKE_CASE_				(							__A       :				str						)				->				Optional[Any]:
   if isinstance(__A ,  torch.Tensor						):
      return image
   elif isinstance(__A ,  PIL.Image.Image						):
      _SCREAMING_SNAKE_CASE =   [image]

   if isinstance(image[0] ,  PIL.Image.Image						):
      _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   image[0].size
      _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   (x - x % 64 for x in (w, h))  # resize to integer multiple of 32

      _SCREAMING_SNAKE_CASE =   [np.array(i.resize((w, h)						)						)[None, :] for i in image]
      _SCREAMING_SNAKE_CASE =   np.concatenate(__A ,  axis=0						)
      _SCREAMING_SNAKE_CASE =   np.array(__A						).astype(np.floataa						) / 2_5_5.0
      _SCREAMING_SNAKE_CASE =   image.transpose(0 ,  3 ,  1 ,  2						)
      _SCREAMING_SNAKE_CASE =   2.0 * image - 1.0
      _SCREAMING_SNAKE_CASE =   torch.from_numpy(__A						)
   elif isinstance(image[0] ,  torch.Tensor						):
      _SCREAMING_SNAKE_CASE =   torch.cat(__A ,  dim=0						)

   return image







class 	lowercase_					(				A     ):


   """simple docstring"""

   def __init__(					self      :			Union[str, Any]						,   __lowerCamelCase      :			OnnxRuntimeModel						,   __lowerCamelCase      :			OnnxRuntimeModel						,   __lowerCamelCase      :			Any						,   __lowerCamelCase      :			OnnxRuntimeModel						,   __lowerCamelCase      :			DDPMScheduler						,   __lowerCamelCase      :			Any						,   __lowerCamelCase      :			int = 3_5_0						,   ):

      """simple docstring"""
      super().__init__(
          vae=__lowerCamelCase						,   text_encoder=__lowerCamelCase						,   tokenizer=__lowerCamelCase						,   unet=__lowerCamelCase						,   low_res_scheduler=__lowerCamelCase						,   scheduler=__lowerCamelCase						,   safety_checker=__lowerCamelCase						,   feature_extractor=__lowerCamelCase						,   watermarker=__lowerCamelCase						,   max_noise_level=__lowerCamelCase						,   )



   def __call__(					self      :			Optional[int]						,   __lowerCamelCase      :			Union[str, List[str]]						,   __lowerCamelCase      :			Union[torch.FloatTensor, PIL.Image.Image, List[PIL.Image.Image]]						,   __lowerCamelCase      :			int = 7_5						,   __lowerCamelCase      :			float = 9.0						,   __lowerCamelCase      :			int = 2_0						,   __lowerCamelCase      :			Optional[Union[str, List[str]]] = None						,   __lowerCamelCase      :			Optional[int] = 1						,   __lowerCamelCase      :			float = 0.0						,   __lowerCamelCase      :			Optional[Union[torch.Generator, List[torch.Generator]]] = None						,   __lowerCamelCase      :			Optional[torch.FloatTensor] = None						,   __lowerCamelCase      :			Optional[np.ndarray] = None						,   __lowerCamelCase      :			Optional[np.ndarray] = None						,   __lowerCamelCase      :			Optional[str] = "pil"						,   __lowerCamelCase      :			bool = True						,   __lowerCamelCase      :			Optional[Callable[[int, int, torch.FloatTensor], None]] = None						,   __lowerCamelCase      :			Optional[int] = 1						,   ):

      """simple docstring"""
      self.check_inputs(__lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase					)

      # 2. Define call parameters
      if prompt is not None and isinstance(__lowerCamelCase						,   __lowerCamelCase					):
         _SCREAMING_SNAKE_CASE =   1
      elif prompt is not None and isinstance(__lowerCamelCase						,   __lowerCamelCase					):
         _SCREAMING_SNAKE_CASE =   len(__lowerCamelCase					)
      else:
         _SCREAMING_SNAKE_CASE =   prompt_embeds.shape[0]

      _SCREAMING_SNAKE_CASE =   self._execution_device
      # 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.
      _SCREAMING_SNAKE_CASE =   guidance_scale > 1.0

      # 3. Encode input prompt
      _SCREAMING_SNAKE_CASE =   self._encode_prompt(
          __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   prompt_embeds=__lowerCamelCase						,   negative_prompt_embeds=__lowerCamelCase						,   )

      _SCREAMING_SNAKE_CASE =   ORT_TO_PT_TYPE[str(text_embeddings.dtype					)]

      # 4. Preprocess image
      _SCREAMING_SNAKE_CASE =   preprocess(__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   image.cpu()

      # 5. set timesteps
      self.scheduler.set_timesteps(__lowerCamelCase						,   device=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   self.scheduler.timesteps

      # 5. Add noise to image
      _SCREAMING_SNAKE_CASE =   torch.tensor([noise_level]						,   dtype=torch.long						,   device=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   torch.randn(image.shape						,   generator=__lowerCamelCase						,   device=__lowerCamelCase						,   dtype=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   self.low_res_scheduler.add_noise(__lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase					)

      _SCREAMING_SNAKE_CASE =   2 if do_classifier_free_guidance else 1
      _SCREAMING_SNAKE_CASE =   np.concatenate([image] * batch_multiplier * num_images_per_prompt					)
      _SCREAMING_SNAKE_CASE =   np.concatenate([noise_level] * image.shape[0]					)

      # 6. Prepare latent variables
      _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   image.shape[2:]
      _SCREAMING_SNAKE_CASE =   self.prepare_latents(
          batch_size * num_images_per_prompt						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase						,   )

      # 7. Check that sizes of image and latents match
      _SCREAMING_SNAKE_CASE =   image.shape[1]
      if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
         raise ValueError(
             "Incorrect configuration settings! The config of `pipeline.unet` expects"
             F""" {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +"""
             F""" `num_channels_image`: {num_channels_image} """
             F""" = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of"""
             " `pipeline.unet` or your `image` input."					)

      # 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
      _SCREAMING_SNAKE_CASE =   self.prepare_extra_step_kwargs(__lowerCamelCase						,   __lowerCamelCase					)

      _SCREAMING_SNAKE_CASE =   next(
          (input.type for input in self.unet.model.get_inputs() if input.name == "timestep")						,   "tensor(float)"					)
      _SCREAMING_SNAKE_CASE =   ORT_TO_NP_TYPE[timestep_dtype]

      # 9. Denoising loop
      _SCREAMING_SNAKE_CASE =   len(__lowerCamelCase					) - num_inference_steps * self.scheduler.order
      with self.progress_bar(total=__lowerCamelCase					) as progress_bar:
         for i, t in enumerate(__lowerCamelCase					):
            # expand the latents if we are doing classifier free guidance
            _SCREAMING_SNAKE_CASE =   np.concatenate([latents] * 2					) if do_classifier_free_guidance else latents

            # concat latents, mask, masked_image_latents in the channel dimension
            _SCREAMING_SNAKE_CASE =   self.scheduler.scale_model_input(__lowerCamelCase						,   __lowerCamelCase					)
            _SCREAMING_SNAKE_CASE =   np.concatenate([latent_model_input, image]						,   axis=1					)

            # timestep to tensor
            _SCREAMING_SNAKE_CASE =   np.array([t]						,   dtype=__lowerCamelCase					)

            # predict the noise residual
            _SCREAMING_SNAKE_CASE =   self.unet(
                sample=__lowerCamelCase						,   timestep=__lowerCamelCase						,   encoder_hidden_states=__lowerCamelCase						,   class_labels=noise_level.astype(np.intaa					)						,   )[0]

            # perform guidance
            if do_classifier_free_guidance:
               _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   np.split(__lowerCamelCase						,   2					)
            _SCREAMING_SNAKE_CASE =   noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

            # compute the previous noisy sample x_t -> x_t-1
            _SCREAMING_SNAKE_CASE =   self.scheduler.step(
                torch.from_numpy(__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase						,   **__lowerCamelCase					).prev_sample

            # call the callback, if provided
            if i == len(__lowerCamelCase					) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
               progress_bar.update()
               if callback is not None and i % callback_steps == 0:
                  callback(__lowerCamelCase						,   __lowerCamelCase						,   __lowerCamelCase					)

        # 10. Post-processing
      _SCREAMING_SNAKE_CASE =   self.decode_latents(latents.float()					)

      # 11. Convert to PIL
      if output_type == "pil":
         _SCREAMING_SNAKE_CASE =   self.numpy_to_pil(__lowerCamelCase					)

      if not return_dict:
         return (image,)

      return ImagePipelineOutput(images=__lowerCamelCase					)



   def 							lowerCAmelCase_    (					self      :			Optional[Any]						,   __lowerCamelCase      :			str					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1 / 0.0_8_3_3_3 * latents
      _SCREAMING_SNAKE_CASE =   self.vae(latent_sample=__lowerCamelCase					)[0]
      _SCREAMING_SNAKE_CASE =   np.clip(image / 2 + 0.5						,   0						,   1					)
      _SCREAMING_SNAKE_CASE =   image.transpose((0, 2, 3, 1)					)
      return image



   def 							lowerCAmelCase_    (					self      :			Optional[int]						,   __lowerCamelCase      :			Union[str, List[str]]						,   __lowerCamelCase      :			int						,   __lowerCamelCase      :			Optional[int]						,   __lowerCamelCase      :			bool						,   __lowerCamelCase      :			Optional[str]						,   __lowerCamelCase      :			Optional[torch.FloatTensor] = None						,   __lowerCamelCase      :			Optional[torch.FloatTensor] = None						,   ):

      """simple docstring"""
      if prompt is not None and isinstance(__lowerCamelCase						,   __lowerCamelCase					):
         _SCREAMING_SNAKE_CASE =   1
      elif prompt is not None and isinstance(__lowerCamelCase						,   __lowerCamelCase					):
         _SCREAMING_SNAKE_CASE =   len(__lowerCamelCase					)
      else:
         _SCREAMING_SNAKE_CASE =   prompt_embeds.shape[0]

      if prompt_embeds is None:
         _SCREAMING_SNAKE_CASE =   self.tokenizer(
             __lowerCamelCase						,   padding="max_length"						,   max_length=self.tokenizer.model_max_length						,   truncation=__lowerCamelCase						,   return_tensors="pt"						,   )
         _SCREAMING_SNAKE_CASE =   text_inputs.input_ids
         _SCREAMING_SNAKE_CASE =   self.tokenizer(__lowerCamelCase						,   padding="longest"						,   return_tensors="pt"					).input_ids

         if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
             __lowerCamelCase						,   __lowerCamelCase					):
            _SCREAMING_SNAKE_CASE =   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}"""					)

         # no positional arguments to text_encoder
         _SCREAMING_SNAKE_CASE =   self.text_encoder(
             input_ids=text_input_ids.int().to(__lowerCamelCase					)						,   )
         _SCREAMING_SNAKE_CASE =   prompt_embeds[0]

      _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   prompt_embeds.shape
      # duplicate text embeddings for each generation per prompt, using mps friendly method
      _SCREAMING_SNAKE_CASE =   prompt_embeds.repeat(1						,   __lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   prompt_embeds.reshape(bs_embed * num_images_per_prompt						,   __lowerCamelCase						,   -1					)

      # get unconditional embeddings for classifier free guidance
      if do_classifier_free_guidance and negative_prompt_embeds is None:
         _SCREAMING_SNAKE_CASE =   42
         if negative_prompt is None:
            _SCREAMING_SNAKE_CASE =   [""] * batch_size
         elif type(__lowerCamelCase					) is not type(__lowerCamelCase					):
            raise TypeError(
                F"""`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCamelCase					)} !="""
                F""" {type(__lowerCamelCase					)}."""					)
         elif isinstance(__lowerCamelCase						,   __lowerCamelCase					):
            _SCREAMING_SNAKE_CASE =   [negative_prompt]
         elif batch_size != len(__lowerCamelCase					):
            raise ValueError(
                F"""`negative_prompt`: {negative_prompt} has batch size {len(__lowerCamelCase					)}, but `prompt`:"""
                F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
                " the batch size of `prompt`."					)
         else:
            _SCREAMING_SNAKE_CASE =   negative_prompt

         _SCREAMING_SNAKE_CASE =   text_input_ids.shape[-1]
         _SCREAMING_SNAKE_CASE =   self.tokenizer(
             __lowerCamelCase						,   padding="max_length"						,   max_length=__lowerCamelCase						,   truncation=__lowerCamelCase						,   return_tensors="pt"						,   )

         # if hasattr(uncond_input, "attention_mask"):
         #     attention_mask = uncond_input.attention_mask.to(device)
         # else:
         #     attention_mask = None

         _SCREAMING_SNAKE_CASE =   self.text_encoder(
             input_ids=uncond_input.input_ids.int().to(__lowerCamelCase					)						,   )
         _SCREAMING_SNAKE_CASE =   uncond_embeddings[0]

      if do_classifier_free_guidance:
         _SCREAMING_SNAKE_CASE =   uncond_embeddings.shape[1]
         # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
         _SCREAMING_SNAKE_CASE =   uncond_embeddings.repeat(1						,   __lowerCamelCase					)
         _SCREAMING_SNAKE_CASE =   uncond_embeddings.reshape(batch_size * num_images_per_prompt						,   __lowerCamelCase						,   -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
         _SCREAMING_SNAKE_CASE =   np.concatenate([uncond_embeddings, prompt_embeds]					)

      return prompt_embeds







'''simple docstring'''



import json
import os
import shutil
import sys
import tempfile
import unittest
from unittest import TestCase
from unittest.mock import patch

import faiss
import numpy as np
from datasets import Dataset

from transformers import BartConfig, BartTokenizer, DPRConfig, DPRQuestionEncoderTokenizer, RagConfig
from transformers.file_utils import is_datasets_available, is_faiss_available, is_psutil_available, is_torch_available
from transformers.integrations import is_ray_available
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_ray


sys.path.append(os.path.join(os.getcwd()))  # noqa: E402 # noqa: E402 # isort:skip

if is_torch_available():
 from distributed_pytorch_retriever import RagPyTorchDistributedRetriever  # noqa: E402 # isort:skip
else:
 lowerCamelCase_	 =		None

if is_ray_available():
 import ray  # noqa: E402 # isort:skip
 from distributed_ray_retriever import RagRayDistributedRetriever, RayRetriever  # noqa: E402 # isort:skip
else:
 lowerCamelCase_	 =		None
 lowerCamelCase_	 =		None
 lowerCamelCase_	 =		None
def 				SCREAMING_SNAKE_CASE_				(							__A       :				Any						)				->				Union[str, Any]:
   if not (is_datasets_available() and is_faiss_available() and is_psutil_available()):
      _SCREAMING_SNAKE_CASE =   unittest.skip("test requires Datasets, Faiss, psutil"						)(__A						)
   return test_case







@require_distributed_retrieval
class 	lowercase_					(				A     ):


   """simple docstring"""

   def 							lowerCAmelCase_    (					self      :			List[str]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   tempfile.mkdtemp()
      _SCREAMING_SNAKE_CASE =   8

      # DPR tok
      _SCREAMING_SNAKE_CASE =   [
          "[UNK]",
          "[CLS]",
          "[SEP]",
          "[PAD]",
          "[MASK]",
          "want",
          "##want",
          "##ed",
          "wa",
          "un",
          "runn",
          "##ing",
          ",",
          "low",
          "lowest",
      ]
      _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "dpr_tokenizer"					)
      os.makedirs(__lowerCamelCase						,   exist_ok=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   os.path.join(__lowerCamelCase						,   DPR_VOCAB_FILES_NAMES["vocab_file"]					)
      with open(self.vocab_file						,   "w"						,   encoding="utf-8"					) as vocab_writer:
         vocab_writer.write("".join([x + "\n" for x in vocab_tokens]					)					)

      # BART tok
      _SCREAMING_SNAKE_CASE =   [
          "l",
          "o",
          "w",
          "e",
          "r",
          "s",
          "t",
          "i",
          "d",
          "n",
          "\u0120",
          "\u0120l",
          "\u0120n",
          "\u0120lo",
          "\u0120low",
          "er",
          "\u0120lowest",
          "\u0120newer",
          "\u0120wider",
          "<unk>",
      ]
      _SCREAMING_SNAKE_CASE =   dict(zip(__lowerCamelCase						,   range(len(__lowerCamelCase					)					)					)					)
      _SCREAMING_SNAKE_CASE =   ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
      _SCREAMING_SNAKE_CASE =   {"unk_token": "<unk>"}

      _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "bart_tokenizer"					)
      os.makedirs(__lowerCamelCase						,   exist_ok=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   os.path.join(__lowerCamelCase						,   BART_VOCAB_FILES_NAMES["vocab_file"]					)
      _SCREAMING_SNAKE_CASE =   os.path.join(__lowerCamelCase						,   BART_VOCAB_FILES_NAMES["merges_file"]					)
      with open(self.vocab_file						,   "w"						,   encoding="utf-8"					) as fp:
         fp.write(json.dumps(__lowerCamelCase					) + "\n"					)
      with open(self.merges_file						,   "w"						,   encoding="utf-8"					) as fp:
         fp.write("\n".join(__lowerCamelCase					)					)



   def 							lowerCAmelCase_    (					self      :			List[Any]					):

      """simple docstring"""
      return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname						,   "dpr_tokenizer"					)					)



   def 							lowerCAmelCase_    (					self      :			Any					):

      """simple docstring"""
      return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname						,   "bart_tokenizer"					)					)



   def 							lowerCAmelCase_    (					self      :			List[Any]					):

      """simple docstring"""
      shutil.rmtree(self.tmpdirname					)



   def 							lowerCAmelCase_    (					self      :			Union[str, Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   Dataset.from_dict(
          {
              "id": ["0", "1"],
              "text": ["foo", "bar"],
              "title": ["Foo", "Bar"],
              "embeddings": [np.ones(self.retrieval_vector_size					), 2 * np.ones(self.retrieval_vector_size					)],
          }					)
      dataset.add_faiss_index("embeddings"						,   string_factory="Flat"						,   metric_type=faiss.METRIC_INNER_PRODUCT					)
      return dataset



   def 							lowerCAmelCase_    (					self      :			List[Any]						,   __lowerCamelCase      :			bool						,   __lowerCamelCase      :			Optional[Any]=1_2_3_4_5					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   self.get_dummy_dataset()
      _SCREAMING_SNAKE_CASE =   RagConfig(
          retrieval_vector_size=self.retrieval_vector_size						,   question_encoder=DPRConfig().to_dict()						,   generator=BartConfig().to_dict()						,   )
      with patch("transformers.models.rag.retrieval_rag.load_dataset"					) as mock_load_dataset:
         _SCREAMING_SNAKE_CASE =   dataset
         _SCREAMING_SNAKE_CASE =   RagPyTorchDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   )
         if init_retrieval:
            retriever.init_retrieval(__lowerCamelCase					)
      return retriever



   def 							lowerCAmelCase_    (					self      :			Union[str, Any]						,   __lowerCamelCase      :			bool					):

      """simple docstring"""
      # Have to run in local mode because sys.path modifications at top of
      # file are not propogated to remote workers.
      # https://stackoverflow.com/questions/54338013/parallel-import-a-python-file-from-sibling-folder
      ray.init(local_mode=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   RagConfig(
          retrieval_vector_size=self.retrieval_vector_size						,   question_encoder=DPRConfig().to_dict()						,   generator=BartConfig().to_dict()						,   )
      _SCREAMING_SNAKE_CASE =   ray.remote(__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   [remote_cls.remote() for _ in range(1					)]
      with patch("transformers.models.rag.retrieval_rag.load_dataset"					) as mock_load_dataset:
         _SCREAMING_SNAKE_CASE =   self.get_dummy_dataset()
         _SCREAMING_SNAKE_CASE =   RagRayDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   retrieval_workers=__lowerCamelCase						,   )
         if init_retrieval:
            retriever.init_retrieval()
      return retriever



   def 							lowerCAmelCase_    (					self      :			Dict						,   __lowerCamelCase      :			bool						,   __lowerCamelCase      :			bool						,   __lowerCamelCase      :			List[str]=1_2_3_4_5					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   self.get_dummy_dataset()
      _SCREAMING_SNAKE_CASE =   RagConfig(
          retrieval_vector_size=self.retrieval_vector_size						,   question_encoder=DPRConfig().to_dict()						,   generator=BartConfig().to_dict()						,   index_name="custom"						,   )
      if from_disk:
         _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "dataset"					)
         _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "index.faiss"					)
         dataset.get_index("embeddings"					).save(os.path.join(self.tmpdirname						,   "index.faiss"					)					)
         dataset.drop_index("embeddings"					)
         dataset.save_to_disk(os.path.join(self.tmpdirname						,   "dataset"					)					)
         del dataset
         _SCREAMING_SNAKE_CASE =   RagPyTorchDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   )
      else:
         _SCREAMING_SNAKE_CASE =   RagPyTorchDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   index=CustomHFIndex(config.retrieval_vector_size						,   __lowerCamelCase					)						,   )
      if init_retrieval:
         retriever.init_retrieval(__lowerCamelCase					)
      return retriever



   def 							lowerCAmelCase_    (					self      :			List[Any]						,   __lowerCamelCase      :			bool						,   __lowerCamelCase      :			bool					):

      """simple docstring"""
      # Have to run in local mode because sys.path modifications at top of
      # file are not propogated to remote workers.
      # https://stackoverflow.com/questions/54338013/parallel-import-a-python-file-from-sibling-folder
      ray.init(local_mode=__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   self.get_dummy_dataset()
      _SCREAMING_SNAKE_CASE =   RagConfig(
          retrieval_vector_size=self.retrieval_vector_size						,   question_encoder=DPRConfig().to_dict()						,   generator=BartConfig().to_dict()						,   index_name="custom"						,   )
      _SCREAMING_SNAKE_CASE =   ray.remote(__lowerCamelCase					)
      _SCREAMING_SNAKE_CASE =   [remote_cls.remote() for _ in range(1					)]
      if from_disk:
         _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "dataset"					)
         _SCREAMING_SNAKE_CASE =   os.path.join(self.tmpdirname						,   "index.faiss"					)
         dataset.get_index("embeddings"					).save(os.path.join(self.tmpdirname						,   "index.faiss"					)					)
         dataset.drop_index("embeddings"					)
         dataset.save_to_disk(os.path.join(self.tmpdirname						,   "dataset"					)					)
         del dataset
         _SCREAMING_SNAKE_CASE =   RagRayDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   retrieval_workers=__lowerCamelCase						,   index=CustomHFIndex.load_from_disk(
                 vector_size=config.retrieval_vector_size						,   dataset_path=config.passages_path						,   index_path=config.index_path						,   )						,   )
      else:
         _SCREAMING_SNAKE_CASE =   RagRayDistributedRetriever(
             __lowerCamelCase						,   question_encoder_tokenizer=self.get_dpr_tokenizer()						,   generator_tokenizer=self.get_bart_tokenizer()						,   retrieval_workers=__lowerCamelCase						,   index=CustomHFIndex(config.retrieval_vector_size						,   __lowerCamelCase					)						,   )
      if init_retrieval:
         retriever.init_retrieval()
      return retriever



   def 							lowerCAmelCase_    (					self      :			int						,   __lowerCamelCase      :			RagRetriever						,   __lowerCamelCase      :			np.array						,   __lowerCamelCase      :			int					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE, _SCREAMING_SNAKE_CASE =   retriever.retrieve(__lowerCamelCase						,   n_docs=__lowerCamelCase					)
      self.assertEqual(retrieved_doc_embeds.shape						,   (2, n_docs, self.retrieval_vector_size)					)
      self.assertEqual(len(__lowerCamelCase					)						,   2					)
      self.assertEqual(sorted(doc_dicts[0]					)						,   ["embeddings", "id", "text", "title"]					)
      self.assertEqual(len(doc_dicts[0]["id"]					)						,   __lowerCamelCase					)
      self.assertEqual(doc_dicts[0]["id"][0]						,   "1"					)  # max inner product is reached with second doc
      self.assertEqual(doc_dicts[1]["id"][0]						,   "0"					)  # max inner product is reached with first doc
      self.assertListEqual(doc_ids.tolist()						,   [[1], [0]]					)



   def 							lowerCAmelCase_    (					self      :			Optional[Any]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)

      self.distributed_retriever_check(
          self.get_dummy_pytorch_distributed_retriever(init_retrieval=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase					)



   def 							lowerCAmelCase_    (					self      :			Any					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)

      self.distributed_retriever_check(
          self.get_dummy_custom_hf_index_pytorch_retriever(init_retrieval=__lowerCamelCase						,   from_disk=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase						,   )



   def 							lowerCAmelCase_    (					self      :			int					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)

      self.distributed_retriever_check(
          self.get_dummy_custom_hf_index_pytorch_retriever(init_retrieval=__lowerCamelCase						,   from_disk=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase						,   )



   @require_ray
   def 							lowerCAmelCase_    (					self      :			Optional[int]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)

      self.distributed_retriever_check(
          self.get_dummy_ray_distributed_retriever(init_retrieval=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase					)
      ray.shutdown()



   @require_ray
   def 							lowerCAmelCase_    (					self      :			Any					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)
      with self.assertRaises(__lowerCamelCase					):
         self.distributed_retriever_check(
             self.get_dummy_custom_hf_index_ray_retriever(init_retrieval=__lowerCamelCase						,   from_disk=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase						,   )
      ray.shutdown()



   @require_ray
   def 							lowerCAmelCase_    (					self      :			Optional[int]					):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE =   1
      _SCREAMING_SNAKE_CASE =   np.array(
          [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]						,   dtype=np.floataa					)

      self.distributed_retriever_check(
          self.get_dummy_custom_hf_index_ray_retriever(init_retrieval=__lowerCamelCase						,   from_disk=__lowerCamelCase					)						,   __lowerCamelCase						,   __lowerCamelCase					)
      ray.shutdown()

