



'''simple docstring'''





import tempfile
import unittest

import numpy as np

from diffusers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    OnnxStableDiffusionPipeline,
    PNDMScheduler,
)
from diffusers.utils.testing_utils import is_onnx_available, nightly, require_onnxruntime, require_torch_gpu

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
 import onnxruntime as ort






class        lowerCAmelCase_(				SCREAMING_SNAKE_CASE_						, unittest.TestCase     ):
  '''simple docstring'''




  __lowercase							:      Tuple				=      '''hf-internal-testing/tiny-random-OnnxStableDiffusionPipeline'''




  def 							UpperCAmelCase_	(    self  ,__UpperCAmelCase=0      )    ->		int:
      lowerCAmelCase__ :  Any							      =					np.random.RandomState(__UpperCAmelCase      )
      lowerCAmelCase__ :  Optional[int]							      =					{
          """prompt""": """A painting of a squirrel eating a burger""",
          """generator""": generator,
          """num_inference_steps""": 2,
          """guidance_scale""": 7.5,
          """output_type""": """numpy""",
      }
      return inputs




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  Optional[int]							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  str							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Optional[Any]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  Any							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  Optional[int]							      =					np.array([0.6_5_0_7_2, 0.5_8_4_9_2, 0.4_8_2_1_9, 0.5_5_5_2_1, 0.5_3_1_8_0, 0.5_5_9_3_9, 0.5_0_6_9_7, 0.3_9_8_0_0, 0.4_6_4_5_5]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      lowerCAmelCase__ :  List[str]							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      lowerCAmelCase__ :  int							      =					PNDMScheduler.from_config(pipe.scheduler.config  ,skip_prk_steps=__UpperCAmelCase      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  List[Any]							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Optional[int]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  List[str]							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  str							      =					np.array([0.6_5_8_6_3, 0.5_9_4_2_5, 0.4_9_3_2_6, 0.5_6_3_1_3, 0.5_3_8_7_5, 0.5_6_6_2_7, 0.5_1_0_6_5, 0.3_9_7_7_7, 0.4_6_3_3_0]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__ :  List[str]							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      lowerCAmelCase__ :  Optional[Any]							      =					LMSDiscreteScheduler.from_config(pipe.scheduler.config      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Union[str, Any]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  Optional[int]							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  int							      =					np.array([0.5_3_7_5_5, 0.6_0_7_8_6, 0.4_7_4_0_2, 0.4_9_4_8_8, 0.5_1_8_6_9, 0.4_9_8_1_9, 0.4_7_9_8_5, 0.3_8_9_5_7, 0.4_4_2_7_9]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      lowerCAmelCase__ :  Tuple							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      lowerCAmelCase__ :  List[Any]							      =					EulerDiscreteScheduler.from_config(pipe.scheduler.config      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Tuple							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Union[str, Any]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  str							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  Dict							      =					np.array([0.5_3_7_5_5, 0.6_0_7_8_6, 0.4_7_4_0_2, 0.4_9_4_8_8, 0.5_1_8_6_9, 0.4_9_8_1_9, 0.4_7_9_8_5, 0.3_8_9_5_7, 0.4_4_2_7_9]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  str							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      lowerCAmelCase__ :  Tuple							      =					EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Union[str, Any]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  Dict							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  Optional[Any]							      =					np.array([0.5_3_8_1_7, 0.6_0_8_1_2, 0.4_7_3_8_4, 0.4_9_5_3_0, 0.5_1_8_9_4, 0.4_9_8_1_4, 0.4_7_9_8_4, 0.3_8_9_5_8, 0.4_4_2_7_1]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		Any:
      lowerCAmelCase__ :  List[str]							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      lowerCAmelCase__ :  Any							      =					DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Union[str, Any]							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Optional[Any]							      =					pipe(**__UpperCAmelCase      ).images
      lowerCAmelCase__ :  Any							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 128, 128, 3)
      lowerCAmelCase__ :  str							      =					np.array([0.5_3_8_9_5, 0.6_0_8_0_8, 0.4_7_9_3_3, 0.4_9_6_0_8, 0.5_1_8_8_6, 0.4_9_9_5_0, 0.4_8_0_5_3, 0.3_8_9_5_7, 0.4_4_2_0_0]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-2




  def 							UpperCAmelCase_	(    self      )    ->		Optional[int]:
      lowerCAmelCase__ :  Tuple							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Optional[int]							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  str							      =					3 * [inputs["""prompt"""]]

      # forward
      lowerCAmelCase__ :  Union[str, Any]							      =					pipe(**__UpperCAmelCase      )
      lowerCAmelCase__ :  Dict							      =					output.images[0, -3:, -3:, -1]

      lowerCAmelCase__ :  Union[str, Any]							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Dict							      =					3 * [inputs.pop("""prompt"""      )]

      lowerCAmelCase__ :  str							      =					pipe.tokenizer(
          __UpperCAmelCase  ,padding="""max_length"""  ,max_length=pipe.tokenizer.model_max_length  ,truncation=__UpperCAmelCase  ,return_tensors="""np"""  ,)
      lowerCAmelCase__ :  Union[str, Any]							      =					text_inputs["""input_ids"""]

      lowerCAmelCase__ :  Any							      =					pipe.text_encoder(input_ids=text_inputs.astype(np.intaa      )      )[0]

      lowerCAmelCase__ :  Optional[int]							      =					prompt_embeds

      # forward
      lowerCAmelCase__ :  Optional[Any]							      =					pipe(**__UpperCAmelCase      )
      lowerCAmelCase__ :  int							      =					output.images[0, -3:, -3:, -1]

      assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()      ).max() < 1E-4






  def 							UpperCAmelCase_	(    self      )    ->		List[str]:
      lowerCAmelCase__ :  List[Any]							      =					OnnxStableDiffusionPipeline.from_pretrained(self.hub_checkpoint  ,provider="""CPUExecutionProvider"""      )
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  Dict							      =					3 * ["""this is a negative prompt"""]
      lowerCAmelCase__ :  List[Any]							      =					negative_prompt
      lowerCAmelCase__ :  Tuple							      =					3 * [inputs["""prompt"""]]

      # forward
      lowerCAmelCase__ :  Optional[int]							      =					pipe(**__UpperCAmelCase      )
      lowerCAmelCase__ :  Any							      =					output.images[0, -3:, -3:, -1]

      lowerCAmelCase__ :  Union[str, Any]							      =					self.get_dummy_inputs()
      lowerCAmelCase__ :  List[str]							      =					3 * [inputs.pop("""prompt"""      )]

      lowerCAmelCase__ :  Tuple							      =					[]
      for p in [prompt, negative_prompt]:
          lowerCAmelCase__ :  Any							      =					pipe.tokenizer(
              __UpperCAmelCase  ,padding="""max_length"""  ,max_length=pipe.tokenizer.model_max_length  ,truncation=__UpperCAmelCase  ,return_tensors="""np"""  ,)
          lowerCAmelCase__ :  Any							      =					text_inputs["""input_ids"""]

          embeds.append(pipe.text_encoder(input_ids=text_inputs.astype(np.intaa      )      )[0]      )

      lowerCAmelCase__      ,   lowerCAmelCase__ :  Any							      =					embeds

      # forward
      lowerCAmelCase__ :  Any							      =					pipe(**__UpperCAmelCase      )
      lowerCAmelCase__ :  Any							      =					output.images[0, -3:, -3:, -1]

      assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()      ).max() < 1E-4







@nightly
@require_onnxruntime
@require_torch_gpu
class        lowerCAmelCase_(				unittest.TestCase     ):
  '''simple docstring'''




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      return (
          "CUDAExecutionProvider",
          {
              "gpu_mem_limit": "15000000000",  # 15GB
              "arena_extend_strategy": "kSameAsRequested",
          },
      )




  @property
  def 							UpperCAmelCase_	(    self      )    ->		Union[str, Any]:
      lowerCAmelCase__ :  Dict							      =					ort.SessionOptions()
      lowerCAmelCase__ :  Dict							      =					False
      return options




  def 							UpperCAmelCase_	(    self      )    ->		str:
      # using the PNDM scheduler by default
      lowerCAmelCase__ :  Union[str, Any]							      =					OnnxStableDiffusionPipeline.from_pretrained(
          """CompVis/stable-diffusion-v1-4"""  ,revision="""onnx"""  ,safety_checker=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase  ,provider=self.gpu_provider  ,sess_options=self.gpu_options  ,)
      sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  List[str]							      =					"""A painting of a squirrel eating a burger"""
      np.random.seed(0      )
      lowerCAmelCase__ :  List[Any]							      =					sd_pipe([prompt]  ,guidance_scale=6.0  ,num_inference_steps=10  ,output_type="""np"""      )
      lowerCAmelCase__ :  Optional[int]							      =					output.images

      lowerCAmelCase__ :  Optional[Any]							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      lowerCAmelCase__ :  int							      =					np.array([0.0_4_5_2, 0.0_3_9_0, 0.0_0_8_7, 0.0_3_5_0, 0.0_6_1_7, 0.0_3_6_4, 0.0_5_4_4, 0.0_5_2_3, 0.0_7_2_0]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3




  def 							UpperCAmelCase_	(    self      )    ->		str:
      lowerCAmelCase__ :  Dict							      =					DDIMScheduler.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,subfolder="""scheduler"""  ,revision="""onnx"""      )
      lowerCAmelCase__ :  List[Any]							      =					OnnxStableDiffusionPipeline.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,revision="""onnx"""  ,scheduler=__UpperCAmelCase  ,safety_checker=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase  ,provider=self.gpu_provider  ,sess_options=self.gpu_options  ,)
      sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Optional[int]							      =					"""open neural network exchange"""
      lowerCAmelCase__ :  Optional[int]							      =					np.random.RandomState(0      )
      lowerCAmelCase__ :  str							      =					sd_pipe([prompt]  ,guidance_scale=7.5  ,num_inference_steps=10  ,generator=__UpperCAmelCase  ,output_type="""np"""      )
      lowerCAmelCase__ :  Optional[int]							      =					output.images
      lowerCAmelCase__ :  Tuple							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      lowerCAmelCase__ :  int							      =					np.array([0.2_8_6_7, 0.1_9_7_4, 0.1_4_8_1, 0.7_2_9_4, 0.7_2_5_1, 0.6_6_6_7, 0.4_1_9_4, 0.5_6_4_2, 0.6_4_8_6]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3




  def 							UpperCAmelCase_	(    self      )    ->		Tuple:
      lowerCAmelCase__ :  str							      =					LMSDiscreteScheduler.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,subfolder="""scheduler"""  ,revision="""onnx"""      )
      lowerCAmelCase__ :  Tuple							      =					OnnxStableDiffusionPipeline.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,revision="""onnx"""  ,scheduler=__UpperCAmelCase  ,safety_checker=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase  ,provider=self.gpu_provider  ,sess_options=self.gpu_options  ,)
      sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  List[Any]							      =					"""open neural network exchange"""
      lowerCAmelCase__ :  List[Any]							      =					np.random.RandomState(0      )
      lowerCAmelCase__ :  Optional[int]							      =					sd_pipe([prompt]  ,guidance_scale=7.5  ,num_inference_steps=10  ,generator=__UpperCAmelCase  ,output_type="""np"""      )
      lowerCAmelCase__ :  str							      =					output.images
      lowerCAmelCase__ :  int							      =					image[0, -3:, -3:, -1]

      assert image.shape == (1, 512, 512, 3)
      lowerCAmelCase__ :  List[Any]							      =					np.array([0.2_3_0_6, 0.1_9_5_9, 0.1_5_9_3, 0.6_5_4_9, 0.6_3_9_4, 0.5_4_0_8, 0.5_0_6_5, 0.6_0_1_0, 0.6_1_6_1]      )

      assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3




  def 							UpperCAmelCase_	(    self      )    ->		List[Any]:
      lowerCAmelCase__ :  int							      =					0

      def test_callback_fn(__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase      ) -> None:
          lowerCAmelCase__ :  int							      =					True
          nonlocal number_of_steps
          number_of_steps += 1
          if step == 0:
              assert latents.shape == (1, 4, 64, 64)
              lowerCAmelCase__ :  List[str]							      =					latents[0, -3:, -3:, -1]
              lowerCAmelCase__ :  int							      =					np.array(
                  [-0.6_7_7_2, -0.3_8_3_5, -1.2_4_5_6, 0.1_9_0_5, -1.0_9_7_4, 0.6_9_6_7, -1.9_3_5_3, 0.0_1_7_8, 1.0_1_6_7]      )

              assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 1E-3
          elif step == 5:
              assert latents.shape == (1, 4, 64, 64)
              lowerCAmelCase__ :  List[Any]							      =					latents[0, -3:, -3:, -1]
              lowerCAmelCase__ :  Optional[Any]							      =					np.array(
                  [-0.3_3_5_1, 0.2_2_4_1, -0.1_8_3_7, -0.2_3_2_5, -0.6_5_7_7, 0.3_3_9_3, -0.0_2_4_1, 0.5_8_9_9, 1.3_8_7_5]      )

              assert np.abs(latents_slice.flatten() - expected_slice      ).max() < 1E-3

      lowerCAmelCase__ :  int							      =					False

      lowerCAmelCase__ :  Any							      =					OnnxStableDiffusionPipeline.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,revision="""onnx"""  ,safety_checker=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase  ,provider=self.gpu_provider  ,sess_options=self.gpu_options  ,)
      pipe.set_progress_bar_config(disable=__UpperCAmelCase      )

      lowerCAmelCase__ :  Dict							      =					"""Andromeda galaxy in a bottle"""

      lowerCAmelCase__ :  List[str]							      =					np.random.RandomState(0      )
      pipe(
          prompt=__UpperCAmelCase  ,num_inference_steps=5  ,guidance_scale=7.5  ,generator=__UpperCAmelCase  ,callback=__UpperCAmelCase  ,callback_steps=1  ,)
      assert test_callback_fn.has_been_called
      assert number_of_steps == 6






  def 							UpperCAmelCase_	(    self      )    ->		Optional[Any]:
      lowerCAmelCase__ :  Any							      =					OnnxStableDiffusionPipeline.from_pretrained(
          """runwayml/stable-diffusion-v1-5"""  ,revision="""onnx"""  ,safety_checker=__UpperCAmelCase  ,feature_extractor=__UpperCAmelCase  ,provider=self.gpu_provider  ,sess_options=self.gpu_options  ,)
      assert isinstance(__UpperCAmelCase  ,__UpperCAmelCase      )
      assert pipe.safety_checker is None

      lowerCAmelCase__ :  Dict							      =					pipe("""example prompt"""  ,num_inference_steps=2      ).images[0]
      assert image is not None

      # check that there's no error when saving a pipeline with one of the models being None
      with tempfile.TemporaryDirectory() as tmpdirname:
          pipe.save_pretrained(__UpperCAmelCase      )
          lowerCAmelCase__ :  int							      =					OnnxStableDiffusionPipeline.from_pretrained(__UpperCAmelCase      )

      # sanity check that the pipeline still works
      assert pipe.safety_checker is None
      lowerCAmelCase__ :  Union[str, Any]							      =					pipe("""example prompt"""  ,num_inference_steps=2      ).images[0]
      assert image is not None








'''simple docstring'''

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

import numpy as np
from datasets import Dataset

from transformers import is_faiss_available
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bart.tokenization_bart import BartTokenizer
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer
from transformers.models.rag.configuration_rag import RagConfig
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_faiss, require_sentencepiece, require_tokenizers, require_torch


if is_faiss_available():
							import faiss






@require_faiss
class 						a				(   _lowerCamelCase     ):


				def   A_ (		self    :					str					):
									snake_case_									=				tempfile.mkdtemp()
									snake_case_									=				8

									# DPR tok
									snake_case_									=				[
									    '''[UNK]''',
									    '''[CLS]''',
									    '''[SEP]''',
									    '''[PAD]''',
									    '''[MASK]''',
									    '''want''',
									    '''##want''',
									    '''##ed''',
									    '''wa''',
									    '''un''',
									    '''runn''',
									    '''##ing''',
									    ''',''',
									    '''low''',
									    '''lowest''',
									]
									snake_case_									=				os.path.join(self.tmpdirname       ,				'''dpr_tokenizer'''					)
									os.makedirs(lowercase_       ,				exist_ok=lowercase_					)
									snake_case_									=				os.path.join(lowercase_       ,				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
									snake_case_									=				[
									    '''l''',
									    '''o''',
									    '''w''',
									    '''e''',
									    '''r''',
									    '''s''',
									    '''t''',
									    '''i''',
									    '''d''',
									    '''n''',
									    '''\u0120''',
									    '''\u0120l''',
									    '''\u0120n''',
									    '''\u0120lo''',
									    '''\u0120low''',
									    '''er''',
									    '''\u0120lowest''',
									    '''\u0120newer''',
									    '''\u0120wider''',
									    '''<unk>''',
									]
									snake_case_									=				dict(zip(lowercase_       ,				range(len(lowercase_					)					)					)					)
									snake_case_									=				['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
									snake_case_									=				{'''unk_token''': '''<unk>'''}

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


				def   A_ (		self    :					Union[str, Any]					):
									return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,				'''dpr_tokenizer'''					)					)


				def   A_ (		self    :					Union[str, Any]					):
									return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,				'''dpr_tokenizer'''					)					)


				def   A_ (		self    :					int					):
									return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,				'''bart_tokenizer'''					)					)


				def   A_ (		self    :					str					):
									shutil.rmtree(self.tmpdirname					)


				def   A_ (		self    :					str					):
									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   A_ (		self    :					str					):
									snake_case_									=				self.get_dummy_dataset()
									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:
														snake_case_									=				dataset
														snake_case_									=				RagRetriever(
														    lowercase_       ,				question_encoder_tokenizer=self.get_dpr_tokenizer()       ,				generator_tokenizer=self.get_bart_tokenizer()       ,				)
									return retriever


				def   A_ (		self    :					str       ,				lowercase_    :					bool					):
									snake_case_									=				self.get_dummy_dataset()
									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:
														snake_case_									=				os.path.join(self.tmpdirname       ,				'''dataset'''					)
														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
														snake_case_									=				RagRetriever(
														    lowercase_       ,				question_encoder_tokenizer=self.get_dpr_tokenizer()       ,				generator_tokenizer=self.get_bart_tokenizer()       ,				)
									else:
														snake_case_									=				RagRetriever(
														    lowercase_       ,				question_encoder_tokenizer=self.get_dpr_tokenizer()       ,				generator_tokenizer=self.get_bart_tokenizer()       ,				index=CustomHFIndex(config.retrieval_vector_size       ,				lowercase_					)       ,				)
									return retriever


				def   A_ (		self    :					Tuple					):
									snake_case_									=				Dataset.from_dict(
									    {
									        '''id''': ['''0''', '''1'''],
									        '''text''': ['''foo''', '''bar'''],
									        '''title''': ['''Foo''', '''Bar'''],
									        '''embeddings''': [np.ones(self.retrieval_vector_size + 1					), 2 * np.ones(self.retrieval_vector_size + 1					)],
									    }					)
									dataset.add_faiss_index('''embeddings'''       ,				string_factory='''Flat'''       ,				metric_type=faiss.METRIC_INNER_PRODUCT					)

									snake_case_									=				os.path.join(self.tmpdirname       ,				'''hf_bert_base.hnswSQ8_correct_phi_128.c_index'''					)
									dataset.save_faiss_index('''embeddings'''       ,				index_file_name + '''.index.dpr'''					)
									pickle.dump(dataset['''id''']       ,				open(index_file_name + '''.index_meta.dpr'''       ,				'''wb'''					)					)

									snake_case_									=				os.path.join(self.tmpdirname       ,				'''psgs_w100.tsv.pkl'''					)
									snake_case_									=				{sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset}
									pickle.dump(lowercase_       ,				open(lowercase_       ,				'''wb'''					)					)

									snake_case_									=				RagConfig(
									    retrieval_vector_size=self.retrieval_vector_size       ,				question_encoder=DPRConfig().to_dict()       ,				generator=BartConfig().to_dict()       ,				index_name='''legacy'''       ,				index_path=self.tmpdirname       ,				)
									snake_case_									=				RagRetriever(
									    lowercase_       ,				question_encoder_tokenizer=self.get_dpr_tokenizer()       ,				generator_tokenizer=self.get_bart_tokenizer()					)
									return retriever


				def   A_ (		self    :					Optional[Any]					):
									snake_case_									=				1
									snake_case_									=				self.get_dummy_canonical_hf_index_retriever()
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_			,snake_case_			,snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=lowercase_					)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertEqual(len(lowercase_					)       ,				2					)
									self.assertEqual(sorted(doc_dicts[0]					)       ,				['''embeddings''', '''id''', '''text''', '''title''']					)
									self.assertEqual(len(doc_dicts[0]['''id''']					)       ,				lowercase_					)
									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   A_ (		self    :					str					):
									snake_case_									=				self.get_dummy_canonical_hf_index_retriever()
									with tempfile.TemporaryDirectory() as tmp_dirname:
														with patch('''transformers.models.rag.retrieval_rag.load_dataset'''					) as mock_load_dataset:
																			snake_case_									=				self.get_dummy_dataset()
																			retriever.save_pretrained(lowercase_					)
																			snake_case_									=				RagRetriever.from_pretrained(lowercase_					)
																			self.assertIsInstance(lowercase_       ,				lowercase_					)
																			snake_case_									=				np.array(
																			    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
																			snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=1					)
																			self.assertTrue(out is not None					)


				def   A_ (		self    :					int					):
									snake_case_									=				1
									snake_case_									=				self.get_dummy_custom_hf_index_retriever(from_disk=lowercase_					)
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_			,snake_case_			,snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=lowercase_					)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertEqual(len(lowercase_					)       ,				2					)
									self.assertEqual(sorted(doc_dicts[0]					)       ,				['''embeddings''', '''id''', '''text''', '''title''']					)
									self.assertEqual(len(doc_dicts[0]['''id''']					)       ,				lowercase_					)
									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   A_ (		self    :					int					):
									snake_case_									=				self.get_dummy_custom_hf_index_retriever(from_disk=lowercase_					)
									with tempfile.TemporaryDirectory() as tmp_dirname:
														retriever.save_pretrained(lowercase_					)
														snake_case_									=				RagRetriever.from_pretrained(lowercase_					)
														self.assertIsInstance(lowercase_       ,				lowercase_					)
														snake_case_									=				np.array(
														    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
														snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=1					)
														self.assertTrue(out is not None					)


				def   A_ (		self    :					str					):
									snake_case_									=				1
									snake_case_									=				self.get_dummy_custom_hf_index_retriever(from_disk=lowercase_					)
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_			,snake_case_			,snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=lowercase_					)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertEqual(len(lowercase_					)       ,				2					)
									self.assertEqual(sorted(doc_dicts[0]					)       ,				['''embeddings''', '''id''', '''text''', '''title''']					)
									self.assertEqual(len(doc_dicts[0]['''id''']					)       ,				lowercase_					)
									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   A_ (		self    :					Any					):
									snake_case_									=				self.get_dummy_custom_hf_index_retriever(from_disk=lowercase_					)
									with tempfile.TemporaryDirectory() as tmp_dirname:
														retriever.save_pretrained(lowercase_					)
														snake_case_									=				RagRetriever.from_pretrained(lowercase_					)
														self.assertIsInstance(lowercase_       ,				lowercase_					)
														snake_case_									=				np.array(
														    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
														snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=1					)
														self.assertTrue(out is not None					)


				def   A_ (		self    :					Any					):
									snake_case_									=				1
									snake_case_									=				self.get_dummy_legacy_index_retriever()
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_			,snake_case_			,snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=lowercase_					)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertEqual(len(lowercase_					)       ,				2					)
									self.assertEqual(sorted(doc_dicts[0]					)       ,				['''text''', '''title''']					)
									self.assertEqual(len(doc_dicts[0]['''text''']					)       ,				lowercase_					)
									self.assertEqual(doc_dicts[0]['''text'''][0]       ,				'''bar'''					)  # max inner product is reached with second doc
									self.assertEqual(doc_dicts[1]['''text'''][0]       ,				'''foo'''					)  # max inner product is reached with first doc
									self.assertListEqual(doc_ids.tolist()       ,				[[1], [0]]					)


				def   A_ (		self    :					int					):
									snake_case_									=				self.get_dummy_legacy_index_retriever()
									with tempfile.TemporaryDirectory() as tmp_dirname:
														retriever.save_pretrained(lowercase_					)
														snake_case_									=				RagRetriever.from_pretrained(lowercase_					)
														self.assertIsInstance(lowercase_       ,				lowercase_					)
														snake_case_									=				np.array(
														    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
														snake_case_									=				retriever.retrieve(lowercase_       ,				n_docs=1					)
														self.assertTrue(out is not None					)


				@require_torch
				@require_tokenizers
				@require_sentencepiece
				def   A_ (		self    :					List[str]					):
									import torch

									snake_case_									=				1
									snake_case_									=				self.get_dummy_canonical_hf_index_retriever()
									snake_case_									=				[[5, 7], [10, 11]]
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_									=				retriever(lowercase_       ,				lowercase_       ,				prefix=retriever.config.generator.prefix       ,				n_docs=lowercase_					)
									snake_case_			,snake_case_			,snake_case_									=				(
									    out['''context_input_ids'''],
									    out['''context_attention_mask'''],
									    out['''retrieved_doc_embeds'''],
									)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertIsInstance(lowercase_       ,				lowercase_					)
									self.assertIsInstance(lowercase_       ,				lowercase_					)
									self.assertIsInstance(lowercase_       ,				np.ndarray					)

									snake_case_									=				retriever(
									    lowercase_       ,				lowercase_       ,				prefix=retriever.config.generator.prefix       ,				n_docs=lowercase_       ,				return_tensors='''pt'''       ,				)
									snake_case_			,snake_case_			,snake_case_			,snake_case_									=				(  # noqa: F841
									    out['''context_input_ids'''],
									    out['''context_attention_mask'''],
									    out['''retrieved_doc_embeds'''],
									    out['''doc_ids'''],
									)
									self.assertEqual(retrieved_doc_embeds.shape       ,				(2, n_docs, self.retrieval_vector_size)					)
									self.assertIsInstance(lowercase_       ,				torch.Tensor					)
									self.assertIsInstance(lowercase_       ,				torch.Tensor					)
									self.assertIsInstance(lowercase_       ,				torch.Tensor					)


				@require_torch
				@require_tokenizers
				@require_sentencepiece
				def   A_ (		self    :					Tuple					):
									snake_case_									=				self.get_dpr_ctx_encoder_tokenizer()
									snake_case_									=				1
									snake_case_									=				self.get_dummy_custom_hf_index_retriever(from_disk=lowercase_					)
									retriever.set_ctx_encoder_tokenizer(lowercase_					)

									snake_case_									=				[[5, 7], [10, 11]]
									snake_case_									=				np.array(
									    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]       ,				dtype=np.floataa					)
									snake_case_									=				retriever(lowercase_       ,				lowercase_       ,				prefix=retriever.config.generator.prefix       ,				n_docs=lowercase_					)

									self.assertEqual(
									    len(lowercase_					)       ,				6					)  # check whether the retriever output consist of 6 attributes including tokenized docs
									self.assertEqual(
									    all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''')					)       ,				lowercase_					)  # check for doc token related keys in dictionary.

