


import copy
import inspect
import unittest

import numpy as np
from huggingface_hub import hf_hub_download

from transformers import VideoMAEConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import (
          MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING,
          VideoMAEForPreTraining,
          VideoMAEForVideoClassification,
          VideoMAEModel,
      )
      from transformers.models.videomae.modeling_videomae import VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from transformers import VideoMAEImageProcessor





class   SCREAMING_SNAKE_CASE_			:




       '''simple docstring'''



       def __init__(						self				:				Any		,				__a				:				Optional[int]		,				__a				:				Tuple=13		,				__a				:				Optional[int]=10		,				__a				:				Union[str, Any]=3		,				__a				:				Optional[Any]=2		,				__a				:				Optional[int]=2		,				__a				:				int=2		,				__a				:				Optional[Any]=True		,				__a				:				int=True		,				__a				:				Union[str, Any]=32		,				__a				:				Dict=5		,				__a				:				Union[str, Any]=4		,				__a				:				int=37		,				__a				:				List[Any]="gelu"		,				__a				:				List[str]=0.1		,				__a				:				int=0.1		,				__a				:				Union[str, Any]=10		,				__a				:				Dict=0.02		,				__a				:				Any=0.9		,				__a				:				Dict=None		,				)					->Dict:
          lowerCamelCase_						:   Any		= parent
          lowerCamelCase_						:   List[str]		= batch_size
          lowerCamelCase_						:   Optional[Any]		= image_size
          lowerCamelCase_						:   Optional[Any]		= num_channels
          lowerCamelCase_						:   List[str]		= patch_size
          lowerCamelCase_						:   Union[str, Any]		= tubelet_size
          lowerCamelCase_						:   Optional[int]		= num_frames
          lowerCamelCase_						:   List[Any]		= is_training
          lowerCamelCase_						:   Optional[Any]		= use_labels
          lowerCamelCase_						:   List[str]		= hidden_size
          lowerCamelCase_						:   List[str]		= num_hidden_layers
          lowerCamelCase_						:   int		= num_attention_heads
          lowerCamelCase_						:   Optional[Any]		= intermediate_size
          lowerCamelCase_						:   Union[str, Any]		= hidden_act
          lowerCamelCase_						:   List[str]		= hidden_dropout_prob
          lowerCamelCase_						:   str		= attention_probs_dropout_prob
          lowerCamelCase_						:   Tuple		= type_sequence_label_size
          lowerCamelCase_						:   Tuple		= initializer_range
          lowerCamelCase_						:   Tuple		= mask_ratio
          lowerCamelCase_						:   Union[str, Any]		= scope

          # in VideoMAE, the number of tokens equals num_frames/tubelet_size * num_patches per frame
          lowerCamelCase_						:   Any		= (image_size // patch_size) ** 2
          lowerCamelCase_						:   Union[str, Any]		= (num_frames // tubelet_size) * self.num_patches_per_frame

          # use this variable to define bool_masked_pos
          lowerCamelCase_						:   Any		= int(mask_ratio * self.seq_length							)



       def 				_lowerCAmelCase							(						self				:				List[str]							)					->int:
          lowerCamelCase_						:   int		= floats_tensor(
              [self.batch_size, self.num_frames, self.num_channels, self.image_size, self.image_size]							)

          lowerCamelCase_						:   int		= None
          if self.use_labels:
             lowerCamelCase_						:   Optional[int]		= ids_tensor([self.batch_size]		,				self.type_sequence_label_size							)

          lowerCamelCase_						:   Any		= self.get_config()

          return config, pixel_values, labels



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->List[str]:
          return VideoMAEConfig(
              image_size=self.image_size		,				patch_size=self.patch_size		,				num_channels=self.num_channels		,				num_frames=self.num_frames		,				tubelet_size=self.tubelet_size		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				intermediate_size=self.intermediate_size		,				hidden_act=self.hidden_act		,				hidden_dropout_prob=self.hidden_dropout_prob		,				attention_probs_dropout_prob=self.attention_probs_dropout_prob		,				is_decoder=UpperCamelCase__		,				initializer_range=self.initializer_range		,				)



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]		,				__a				:				Tuple		,				__a				:				List[Any]		,				__a				:				str							)					->str:
          lowerCamelCase_						:   int		= VideoMAEModel(config=UpperCamelCase__							)
          model.to(UpperCamelCase__							)
          model.eval()
          lowerCamelCase_						:   str		= model(UpperCamelCase__							)
          self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)							)



       def 				_lowerCAmelCase							(						self				:				Optional[int]		,				__a				:				str		,				__a				:				List[Any]		,				__a				:				Any							)					->Union[str, Any]:
          lowerCamelCase_						:   int		= VideoMAEForPreTraining(UpperCamelCase__							)
          model.to(UpperCamelCase__							)
          model.eval()
          # important: each video needs to have the same number of masked patches
          # hence we define a single mask, which we then repeat for each example in the batch
          lowerCamelCase_						:   Any		= torch.ones((self.num_masks,)							)
          lowerCamelCase_						:   str		= torch.cat([mask, torch.zeros(self.seq_length - mask.size(0							)							)]							)
          lowerCamelCase_						:   Any		= mask.expand(self.batch_size		,				-1							).bool()

          lowerCamelCase_						:   Dict		= model(UpperCamelCase__		,				UpperCamelCase__							)
          # model only returns predictions for masked patches
          lowerCamelCase_						:   Optional[int]		= mask.sum().item()
          lowerCamelCase_						:   Dict		= 3 * self.tubelet_size * self.patch_size**2
          self.parent.assertEqual(result.logits.shape		,				(self.batch_size, num_masked_patches, decoder_num_labels)							)




       def 				_lowerCAmelCase							(						self				:				str							)					->Tuple:
          lowerCamelCase_						:   str		= self.prepare_config_and_inputs()
          lowerCamelCase_,	lowerCamelCase_,	lowerCamelCase_						:   Union[str, Any]		= config_and_inputs
          lowerCamelCase_						:   List[str]		= {"""pixel_values""": pixel_values}
          return config, inputs_dict







@require_torch
class   SCREAMING_SNAKE_CASE_			(_UpperCamelCase						,       _UpperCamelCase						,       unittest.TestCase						):




       '''simple docstring'''



       _a   							=							(
           (VideoMAEModel, VideoMAEForPreTraining, VideoMAEForVideoClassification) if is_torch_available() else ()
       )
       _a   							=							(
           {"feature-extraction": VideoMAEModel, "video-classification": VideoMAEForVideoClassification}
           if is_torch_available()
           else {}
       )

       _a   							=							False
       _a   							=							False
       _a   							=							False
       _a   							=							False



       def 				_lowerCAmelCase							(						self				:				Tuple							)					->Any:
          lowerCamelCase_						:   List[Any]		= VideoMAEModelTester(self							)
          lowerCamelCase_						:   Dict		= ConfigTester(self		,				config_class=UpperCamelCase__		,				has_text_modality=UpperCamelCase__		,				hidden_size=37							)



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]		,				__a				:				Any		,				__a				:				List[Any]		,				__a				:				str=False							)					->List[Any]:
          lowerCamelCase_						:   List[str]		= copy.deepcopy(UpperCamelCase__							)

          if model_class == VideoMAEForPreTraining:
             # important: each video needs to have the same number of masked patches
             # hence we define a single mask, which we then repeat for each example in the batch
             lowerCamelCase_						:   Tuple		= torch.ones((self.model_tester.num_masks,)							)
             lowerCamelCase_						:   List[str]		= torch.cat([mask, torch.zeros(self.model_tester.seq_length - mask.size(0							)							)]							)
             lowerCamelCase_						:   Any		= mask.expand(self.model_tester.batch_size		,				-1							).bool()
             lowerCamelCase_						:   Tuple		= bool_masked_pos.to(UpperCamelCase__							)

          if return_labels:
             if model_class in [
                 *get_values(UpperCamelCase__							),
             ]:
                lowerCamelCase_						:   int		= torch.zeros(
                    self.model_tester.batch_size		,				dtype=torch.long		,				device=UpperCamelCase__							)

          return inputs_dict



       def 				_lowerCAmelCase							(						self				:				Optional[int]							)					->int:
          self.config_tester.run_common_tests()



       @unittest.skip(reason="""VideoMAE does not use inputs_embeds"""							)
       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->Tuple:
          pass



       def 				_lowerCAmelCase							(						self				:				str							)					->str:
          lowerCamelCase_,	lowerCamelCase_						:   Tuple		= self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase_						:   Union[str, Any]		= model_class(UpperCamelCase__							)
             self.assertIsInstance(model.get_input_embeddings()		,				(nn.Module)							)
             lowerCamelCase_						:   Optional[int]		= model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase__		,				nn.Linear							)							)



       def 				_lowerCAmelCase							(						self				:				List[Any]							)					->List[Any]:
          lowerCamelCase_,	lowerCamelCase_						:   str		= self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase_						:   Union[str, Any]		= model_class(UpperCamelCase__							)
             lowerCamelCase_						:   Any		= inspect.signature(model.forward							)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowerCamelCase_						:   Dict		= [*signature.parameters.keys()]

             lowerCamelCase_						:   Optional[Any]		= ["""pixel_values"""]
             self.assertListEqual(arg_names[:1]		,				UpperCamelCase__							)



       def 				_lowerCAmelCase							(						self				:				Optional[Any]							)					->int:
          lowerCamelCase_						:   Optional[Any]		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*UpperCamelCase__							)



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->int:
          lowerCamelCase_						:   Any		= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_pretraining(*UpperCamelCase__							)



       @slow
       def 				_lowerCAmelCase							(						self				:				str							)					->Optional[Any]:
          for model_name in VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowerCamelCase_						:   Tuple		= VideoMAEModel.from_pretrained(UpperCamelCase__							)
             self.assertIsNotNone(UpperCamelCase__							)



       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Optional[Any]:
          if not self.has_attentions:
             pass

          else:
             lowerCamelCase_,	lowerCamelCase_						:   int		= self.model_tester.prepare_config_and_inputs_for_common()
             lowerCamelCase_						:   Dict		= True

             for model_class in self.all_model_classes:
                lowerCamelCase_						:   Optional[Any]		= self.model_tester.seq_length - self.model_tester.num_masks
                lowerCamelCase_						:   Dict		= (
                    num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length
                )

                lowerCamelCase_						:   Optional[Any]		= True
                lowerCamelCase_						:   Any		= False
                lowerCamelCase_						:   List[str]		= True
                lowerCamelCase_						:   str		= model_class(UpperCamelCase__							)
                model.to(UpperCamelCase__							)
                model.eval()
                with torch.no_grad():
                   lowerCamelCase_						:   Optional[Any]		= model(**self._prepare_for_class(UpperCamelCase__		,				UpperCamelCase__							)							)
                lowerCamelCase_						:   List[str]		= outputs.attentions
                self.assertEqual(len(UpperCamelCase__							)		,				self.model_tester.num_hidden_layers							)

                # check that output_attentions also work using config
                del inputs_dict["output_attentions"]
                lowerCamelCase_						:   List[str]		= True
                lowerCamelCase_						:   Optional[int]		= model_class(UpperCamelCase__							)
                model.to(UpperCamelCase__							)
                model.eval()
                with torch.no_grad():
                   lowerCamelCase_						:   Optional[Any]		= model(**self._prepare_for_class(UpperCamelCase__		,				UpperCamelCase__							)							)
                lowerCamelCase_						:   Tuple		= outputs.attentions
                self.assertEqual(len(UpperCamelCase__							)		,				self.model_tester.num_hidden_layers							)

                self.assertListEqual(
                    list(attentions[0].shape[-3:]							)		,				[self.model_tester.num_attention_heads, seq_len, seq_len]		,				)
                lowerCamelCase_						:   List[Any]		= len(UpperCamelCase__							)

                # Check attention is always last and order is fine
                lowerCamelCase_						:   Union[str, Any]		= True
                lowerCamelCase_						:   str		= True
                lowerCamelCase_						:   str		= model_class(UpperCamelCase__							)
                model.to(UpperCamelCase__							)
                model.eval()
                with torch.no_grad():
                   lowerCamelCase_						:   int		= model(**self._prepare_for_class(UpperCamelCase__		,				UpperCamelCase__							)							)

                self.assertEqual(out_len + 1		,				len(UpperCamelCase__							)							)

                lowerCamelCase_						:   Dict		= outputs.attentions

                self.assertEqual(len(UpperCamelCase__							)		,				self.model_tester.num_hidden_layers							)
                self.assertListEqual(
                    list(self_attentions[0].shape[-3:]							)		,				[self.model_tester.num_attention_heads, seq_len, seq_len]		,				)



       def 				_lowerCAmelCase							(						self				:				int							)					->Dict:
          def check_hidden_states_output(__a				:				str		,				__a				:				Optional[int]		,				__a				:				str							):
             lowerCamelCase_						:   Optional[int]		= model_class(UpperCamelCase__							)
             model.to(UpperCamelCase__							)
             model.eval()

             with torch.no_grad():
                lowerCamelCase_						:   int		= model(**self._prepare_for_class(UpperCamelCase__		,				UpperCamelCase__							)							)

             lowerCamelCase_						:   int		= outputs.hidden_states
             lowerCamelCase_						:   Union[str, Any]		= self.model_tester.num_hidden_layers + 1
             self.assertEqual(len(UpperCamelCase__							)		,				UpperCamelCase__							)

             lowerCamelCase_						:   Union[str, Any]		= self.model_tester.seq_length - self.model_tester.num_masks
             lowerCamelCase_						:   List[Any]		= num_visible_patches if model_class == VideoMAEForPreTraining else self.model_tester.seq_length

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]							)		,				[seq_length, self.model_tester.hidden_size]		,				)

          lowerCamelCase_,	lowerCamelCase_						:   List[Any]		= self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             lowerCamelCase_						:   List[Any]		= True
             check_hidden_states_output(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__							)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             lowerCamelCase_						:   int		= True

             check_hidden_states_output(UpperCamelCase__		,				UpperCamelCase__		,				UpperCamelCase__							)




       @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
       def 				_lowerCAmelCase							(						self				:				str							)					->Optional[Any]:
          pass







def    __lowerCamelCase   (      )     ->    Dict:
   lowerCamelCase_						:   List[str]		= hf_hub_download(
       repo_id="""hf-internal-testing/spaghetti-video"""						,						filename="""eating_spaghetti.npy"""						,						repo_type="""dataset"""					)
   lowerCamelCase_						:   Tuple		= np.load(__snake_case					)
   return list(__snake_case					)





@require_torch
@require_vision
class   SCREAMING_SNAKE_CASE_			(unittest.TestCase						):




       '''simple docstring'''



       @cached_property
       def 				_lowerCAmelCase							(						self				:				Dict							)					->int:
          # logits were tested with a different mean and std, so we use the same here
          return (
              VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5]		,				image_std=[0.5, 0.5, 0.5]							)
              if is_vision_available()
              else None
          )



       @slow
       def 				_lowerCAmelCase							(						self				:				Any							)					->Optional[int]:
          lowerCamelCase_						:   Dict		= VideoMAEForVideoClassification.from_pretrained("""MCG-NJU/videomae-base-finetuned-kinetics"""							).to(
              UpperCamelCase__							)

          lowerCamelCase_						:   List[str]		= self.default_image_processor
          lowerCamelCase_						:   Any		= prepare_video()
          lowerCamelCase_						:   Tuple		= image_processor(UpperCamelCase__		,				return_tensors="""pt"""							).to(UpperCamelCase__							)

          # forward pass
          with torch.no_grad():
             lowerCamelCase_						:   Dict		= model(**UpperCamelCase__							)

          # verify the logits
          lowerCamelCase_						:   Tuple		= torch.Size((1, 400)							)
          self.assertEqual(outputs.logits.shape		,				UpperCamelCase__							)

          lowerCamelCase_						:   Dict		= torch.tensor([0.3_669, -0.0_688, -0.2_421]							).to(UpperCamelCase__							)

          self.assertTrue(torch.allclose(outputs.logits[0, :3]		,				UpperCamelCase__		,				atol=1e-4							)							)




       @slow
       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->List[Any]:
          lowerCamelCase_						:   int		= VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""							).to(UpperCamelCase__							)

          lowerCamelCase_						:   Tuple		= self.default_image_processor
          lowerCamelCase_						:   Dict		= prepare_video()
          lowerCamelCase_						:   Union[str, Any]		= image_processor(UpperCamelCase__		,				return_tensors="""pt"""							).to(UpperCamelCase__							)

          # add boolean mask, indicating which patches to mask
          lowerCamelCase_						:   Union[str, Any]		= hf_hub_download(repo_id="""hf-internal-testing/bool-masked-pos"""		,				filename="""bool_masked_pos.pt"""							)
          lowerCamelCase_						:   str		= torch.load(UpperCamelCase__							)

          # forward pass
          with torch.no_grad():
             lowerCamelCase_						:   Tuple		= model(**UpperCamelCase__							)

          # verify the logits
          lowerCamelCase_						:   Union[str, Any]		= torch.Size([1, 1_408, 1_536]							)
          lowerCamelCase_						:   Optional[Any]		= torch.tensor(
              [[0.7_994, 0.9_612, 0.8_508], [0.7_401, 0.8_958, 0.8_302], [0.5_862, 0.7_468, 0.7_325]]		,				device=UpperCamelCase__							)
          self.assertEqual(outputs.logits.shape		,				UpperCamelCase__							)
          self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,				UpperCamelCase__		,				atol=1e-4							)							)

          # verify the loss (`config.norm_pix_loss` = `True`)
          lowerCamelCase_						:   Any		= torch.tensor([0.5_142]		,				device=UpperCamelCase__							)
          self.assertTrue(torch.allclose(outputs.loss		,				UpperCamelCase__		,				atol=1e-4							)							)

          # verify the loss (`config.norm_pix_loss` = `False`)
          lowerCamelCase_						:   Tuple		= VideoMAEForPreTraining.from_pretrained("""MCG-NJU/videomae-base-short"""		,				norm_pix_loss=UpperCamelCase__							).to(
              UpperCamelCase__							)

          with torch.no_grad():
             lowerCamelCase_						:   Optional[Any]		= model(**UpperCamelCase__							)

          lowerCamelCase_						:   List[str]		= torch.tensor(torch.tensor([0.6_469]							)		,				device=UpperCamelCase__							)
          self.assertTrue(torch.allclose(outputs.loss		,				UpperCamelCase__		,				atol=1e-4							)							)






'''simple docstring'''






import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


_UpperCAmelCase    :     int         =       logging.getLogger(__name__)







class lowercase_       :







   """simple docstring"""

   def __init__(      self      :			Tuple     )  ->			int:
        _A    		=  False
   def 	__UpperCAmelCase   (      self      :			Optional[int], UpperCamelCase__      :			str, UpperCamelCase__      :			Optional[Any], UpperCamelCase__      :			int, UpperCamelCase__      :			Optional[Any]     )  ->			Any:
        if not self.initialized:
             _A    		=  RagRetriever(
                 UpperCamelCase__, question_encoder_tokenizer=UpperCamelCase__, generator_tokenizer=UpperCamelCase__, index=UpperCamelCase__, init_retrieval=UpperCamelCase__, )
             _A    		=  True
   def 	__UpperCAmelCase   (      self      :			Any     )  ->			List[Any]:
        self.retriever.index.init_index()

   def 	__UpperCAmelCase   (      self      :			int, UpperCamelCase__      :			Optional[Any], UpperCamelCase__      :			Dict     )  ->			Optional[Any]:
        _A		,       _A    		=  self.retriever._main_retrieve(UpperCamelCase__, UpperCamelCase__     )
        return doc_ids, retrieved_doc_embeds







class lowercase_       (		_UpperCamelCase			):







   """simple docstring"""

   def __init__(      self      :			Optional[Any], UpperCamelCase__      :			Optional[Any], UpperCamelCase__      :			Optional[int], UpperCamelCase__      :			Dict, UpperCamelCase__      :			Union[str, Any], UpperCamelCase__      :			int=None     )  ->			Optional[int]:
        if index is not None and index.is_initialized() and len(UpperCamelCase__     ) > 0:
             raise ValueError(
                 'When using Ray for distributed fine-tuning, '
                 'you\'ll need to provide the paths instead, '
                 'as the dataset and the index are loaded '
                 'separately. More info in examples/rag/use_own_knowledge_dataset.py '     )
        super().__init__(
            UpperCamelCase__, question_encoder_tokenizer=UpperCamelCase__, generator_tokenizer=UpperCamelCase__, index=UpperCamelCase__, init_retrieval=UpperCamelCase__, )
        _A    		=  retrieval_workers
        if len(self.retrieval_workers     ) > 0:
             ray.get(
                 [
                     worker.create_rag_retriever.remote(UpperCamelCase__, UpperCamelCase__, UpperCamelCase__, UpperCamelCase__     )
                     for worker in self.retrieval_workers
                 ]     )
   def 	__UpperCAmelCase   (      self      :			Dict     )  ->			int:
        logger.info('initializing retrieval'     )

        if len(self.retrieval_workers     ) > 0:
             ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers]     )
        else:
             # Non-distributed training. Load index into this same process.
             self.index.init_index()
   def 	__UpperCAmelCase   (      self      :			Optional[Any], UpperCamelCase__      :			Dict, UpperCamelCase__      :			Tuple     )  ->			Dict:
        if len(self.retrieval_workers     ) > 0:
             # Select a random retrieval actor.
             _A    		=  self.retrieval_workers[random.randint(0, len(self.retrieval_workers     ) - 1     )]
             _A		,       _A    		=  ray.get(random_worker.retrieve.remote(UpperCamelCase__, UpperCamelCase__     )     )
        else:
             _A		,       _A    		=  self._main_retrieve(UpperCamelCase__, UpperCamelCase__     )
        return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(UpperCamelCase__     )
   @classmethod
   def 	__UpperCAmelCase   (      cls      :			List[str], UpperCamelCase__      :			List[Any], UpperCamelCase__      :			List[Any]=None, **UpperCamelCase__      :			Optional[int]     )  ->			Optional[Any]:
        return super(UpperCamelCase__, cls     ).get_tokenizers(UpperCamelCase__, UpperCamelCase__, **UpperCamelCase__     )

   @classmethod
   def 	__UpperCAmelCase   (      cls      :			Any, UpperCamelCase__      :			Tuple, UpperCamelCase__      :			int, UpperCamelCase__      :			List[Any]=None, **UpperCamelCase__      :			List[str]     )  ->			Tuple:
        _A    		=  kwargs.pop('config', UpperCamelCase__     ) or RagConfig.from_pretrained(UpperCamelCase__, **UpperCamelCase__     )
        _A    		=  RagTokenizer.from_pretrained(UpperCamelCase__, config=UpperCamelCase__     )
        _A    		=  rag_tokenizer.question_encoder
        _A    		=  rag_tokenizer.generator
        if indexed_dataset is not None:
             _A    		=  'custom'
             _A    		=  CustomHFIndex(config.retrieval_vector_size, UpperCamelCase__     )
        else:
             _A    		=  cls._build_index(UpperCamelCase__     )
        return cls(
            UpperCamelCase__, question_encoder_tokenizer=UpperCamelCase__, generator_tokenizer=UpperCamelCase__, retrieval_workers=UpperCamelCase__, index=UpperCamelCase__, )



