





'''simple docstring'''


import copy

from ...configuration_utils import PretrainedConfig
from ...utils import add_start_docstrings


lowerCAmelCase_   :   List[Any]       =					R"\n    [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and\n    can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.\n\n    Args:\n        title_sep (`str`, *optional*, defaults to  `\" / \"`):\n            Separator inserted between the title and the text of the retrieved document when calling [`RagRetriever`].\n        doc_sep (`str`, *optional*, defaults to  `\" // \"`):\n            Separator inserted between the text of the retrieved document and the original input when calling\n            [`RagRetriever`].\n        n_docs (`int`, *optional*, defaults to 5):\n            Number of documents to retrieve.\n        max_combined_length (`int`, *optional*, defaults to 300):\n            Max length of contextualized input returned by [`~RagRetriever.__call__`].\n        retrieval_vector_size (`int`, *optional*, defaults to 768):\n            Dimensionality of the document embeddings indexed by [`RagRetriever`].\n        retrieval_batch_size (`int`, *optional*, defaults to 8):\n            Retrieval batch size, defined as the number of queries issues concurrently to the faiss index encapsulated\n            [`RagRetriever`].\n        dataset (`str`, *optional*, defaults to `\"wiki_dpr\"`):\n            A dataset identifier of the indexed dataset in HuggingFace Datasets (list all available datasets and ids\n            using `datasets.list_datasets()`).\n        dataset_split (`str`, *optional*, defaults to `\"train\"`)\n            Which split of the `dataset` to load.\n        index_name (`str`, *optional*, defaults to `\"compressed\"`)\n            The index name of the index associated with the `dataset`. One can choose between `\"legacy\"`, `\"exact\"` and\n            `\"compressed\"`.\n        index_path (`str`, *optional*)\n            The path to the serialized faiss index on disk.\n        passages_path (`str`, *optional*):\n            A path to text passages compatible with the faiss index. Required if using\n            [`~models.rag.retrieval_rag.LegacyIndex`]\n        use_dummy_dataset (`bool`, *optional*, defaults to `False`)\n            Whether to load a \"dummy\" variant of the dataset specified by `dataset`.\n        label_smoothing (`float`, *optional*, defaults to 0.0):\n            Only relevant if `return_loss` is set to `True`. Controls the `epsilon` parameter value for label smoothing\n            in the loss calculation. If set to 0, no label smoothing is performed.\n        do_marginalize (`bool`, *optional*, defaults to `False`):\n            If `True`, the logits are marginalized over all documents by making use of\n            `torch.nn.functional.log_softmax`.\n        reduce_loss (`bool`, *optional*, defaults to `False`):\n            Whether or not to reduce the NLL loss using the `torch.Tensor.sum` operation.\n        do_deduplication (`bool`, *optional*, defaults to `True`):\n            Whether or not to deduplicate the generations from different context documents for a given input. Has to be\n            set to `False` if used while training with distributed backend.\n        exclude_bos_score (`bool`, *optional*, defaults to `False`):\n            Whether or not to disregard the BOS token when computing the loss.\n        output_retrieved(`bool`, *optional*, defaults to `False`):\n            If set to `True`, `retrieved_doc_embeds`, `retrieved_doc_ids`, `context_input_ids` and\n            `context_attention_mask` are returned. See returned tensors for more detail.\n        use_cache (`bool`, *optional*, defaults to `True`):\n            Whether or not the model should return the last key/values attentions (not used by all models).\n        forced_eos_token_id (`int`, *optional*):\n            The id of the token to force as the last generated token when `max_length` is reached. Usually set to\n            `eos_token_id`.\n"






@add_start_docstrings(__lowerCAmelCase     )
class       UpperCamelCase__  (		__lowerCAmelCase     ):
	lowerCAmelCase__					:			Optional[Any]								=						"rag"
	lowerCAmelCase__					:			Optional[Any]								=						True







	def __init__(				self			:   List[Any] , lowerCamelCase			:   int=None , lowerCamelCase			:   List[str]=True , lowerCamelCase			:   Optional[Any]=None , lowerCamelCase			:   Union[str, Any]=None , lowerCamelCase			:   Dict=None , lowerCamelCase			:   Tuple=None , lowerCamelCase			:   List[Any]=None , lowerCamelCase			:   Tuple=" / " , lowerCamelCase			:   Union[str, Any]=" // " , lowerCamelCase			:   int=5 , lowerCamelCase			:   Optional[Any]=3_0_0 , lowerCamelCase			:   Tuple=7_6_8 , lowerCamelCase			:   Union[str, Any]=8 , lowerCamelCase			:   Any="wiki_dpr" , lowerCamelCase			:   Any="train" , lowerCamelCase			:   Union[str, Any]="compressed" , lowerCamelCase			:   Optional[Any]=None , lowerCamelCase			:   str=None , lowerCamelCase			:   Any=False , lowerCamelCase			:   Optional[int]=False , lowerCamelCase			:   str=0.0 , lowerCamelCase			:   Optional[int]=True , lowerCamelCase			:   Any=False , lowerCamelCase			:   Any=False , lowerCamelCase			:   int=False , lowerCamelCase			:   Any=True , lowerCamelCase			:   Optional[Any]=None , **lowerCamelCase			:   Optional[Any] , ):




				'''simple docstring'''
				super().__init__(
				    bos_token_id=lowerCamelCase , pad_token_id=lowerCamelCase , eos_token_id=lowerCamelCase , decoder_start_token_id=lowerCamelCase , forced_eos_token_id=lowerCamelCase , is_encoder_decoder=lowerCamelCase , prefix=lowerCamelCase , vocab_size=lowerCamelCase , **lowerCamelCase , )
				assert (
				    "question_encoder" in kwargs and "generator" in kwargs
				), "Config has to be initialized with question_encoder and generator config"
				a__     = kwargs.pop("question_encoder"		)
				a__     = question_encoder_config.pop("model_type"		)
				a__     = kwargs.pop("generator"		)
				a__     = decoder_config.pop("model_type"		)

				from ..auto.configuration_auto import AutoConfig

				a__     = AutoConfig.for_model(lowerCamelCase , **lowerCamelCase		)
				a__     = AutoConfig.for_model(lowerCamelCase , **lowerCamelCase		)

				a__     = reduce_loss
				a__     = label_smoothing
				a__     = exclude_bos_score
				a__     = do_marginalize

				a__     = title_sep
				a__     = doc_sep
				a__     = n_docs
				a__     = max_combined_length

				a__     = dataset
				a__     = dataset_split
				a__     = index_name

				a__     = retrieval_vector_size
				a__     = retrieval_batch_size
				a__     = passages_path
				a__     = index_path
				a__     = use_dummy_dataset

				a__     = output_retrieved

				a__     = do_deduplication

				a__     = use_cache

				if self.forced_eos_token_id is None:
							a__     = getattr(self.generator , "forced_eos_token_id" , lowerCamelCase		)







	@classmethod
	def 						__a   (				cls			:   Union[str, Any] , lowerCamelCase			:   PretrainedConfig , lowerCamelCase			:   PretrainedConfig , **lowerCamelCase			:   Optional[Any]		):




				'''simple docstring'''
				return cls(question_encoder=question_encoder_config.to_dict() , generator=generator_config.to_dict() , **lowerCamelCase		)







	def 						__a   (				self			:   List[Any]		):




				'''simple docstring'''
				a__     = copy.deepcopy(self.__dict__		)
				a__     = self.question_encoder.to_dict()
				a__     = self.generator.to_dict()
				a__     = self.__class__.model_type
				return output










'''simple docstring'''


import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append(".")







def    _lowerCamelCase  (__lowerCamelCase  :     List[str]     )   ->    List[str]:
			a__     = test_file.split(os.path.sep     )
			if components[0:2] != ["tests", "models"]:
						raise ValueError(
						    "`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got "
						    f'''{test_file} instead.'''     )
			a__     = components[-1]
			if not test_fn.endswith("py"     ):
						raise ValueError(f'''`test_file` should be a python file. Got {test_fn} instead.'''     )
			if not test_fn.startswith("test_modeling_"     ):
						raise ValueError(
						    f'''`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead.'''     )

			a__     = components[:-1] + [test_fn.replace(".py"					,  ""     )]
			a__     = ".".join(__lowerCamelCase     )

			return test_module_path







def    _lowerCamelCase  (__lowerCamelCase  :     Union[str, Any]     )   ->    Optional[Any]:
			a__     = get_module_path(__lowerCamelCase     )
			a__     = importlib.import_module(__lowerCamelCase     )

			return test_module







def    _lowerCamelCase  (__lowerCamelCase  :     Optional[int]     )   ->    Union[str, Any]:
			a__     = []
			a__     = get_test_module(__lowerCamelCase     )
			for attr in dir(__lowerCamelCase     ):
						if attr.endswith("ModelTester"     ):
									tester_classes.append(getattr(__lowerCamelCase					,  __lowerCamelCase     )     )

    # sort with class names
			return sorted(__lowerCamelCase					,  key=lambda __lowerCamelCase     : x.__name__     )







def    _lowerCamelCase  (__lowerCamelCase  :     Tuple     )   ->    Tuple:
			a__     = []
			a__     = get_test_module(__lowerCamelCase     )
			for attr in dir(__lowerCamelCase     ):
						a__     = getattr(__lowerCamelCase					,  __lowerCamelCase     )
						# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
						# `all_model_classes` is not empty (which also excludes other special classes).
						a__     = getattr(__lowerCamelCase					,  "all_model_classes"					,  []     )
						if len(__lowerCamelCase     ) > 0:
									test_classes.append(__lowerCamelCase     )

    # sort with class names
			return sorted(__lowerCamelCase					,  key=lambda __lowerCamelCase     : x.__name__     )







def    _lowerCamelCase  (__lowerCamelCase  :     List[Any]     )   ->    List[Any]:
			a__     = get_test_classes(__lowerCamelCase     )
			a__     = set()
			for test_class in test_classes:
						model_classes.update(test_class.all_model_classes     )

			# sort with class names
			return sorted(__lowerCamelCase					,  key=lambda __lowerCamelCase     : x.__name__     )







def    _lowerCamelCase  (__lowerCamelCase  :     str     )   ->    Dict:
			a__     = test_class()
			if hasattr(__lowerCamelCase					,  "setUp"     ):
						test.setUp()

			a__     = None
			if hasattr(__lowerCamelCase					,  "model_tester"     ):
						# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
						if test.model_tester is not None:
									a__     = test.model_tester.__class__

			return model_tester







def    _lowerCamelCase  (__lowerCamelCase  :     Dict					,  __lowerCamelCase  :     Optional[int]     )   ->    List[Any]:
			a__     = get_test_classes(__lowerCamelCase     )

			a__     = []
			for test_class in test_classes:
						if model_class in test_class.all_model_classes:
									target_test_classes.append(__lowerCamelCase     )

    # sort with class names
			return sorted(__lowerCamelCase					,  key=lambda __lowerCamelCase     : x.__name__     )







def    _lowerCamelCase  (__lowerCamelCase  :     Any					,  __lowerCamelCase  :     Tuple     )   ->    Dict:
			a__     = get_test_classes_for_model(__lowerCamelCase					,  __lowerCamelCase     )

			a__     = []
			for test_class in test_classes:
						a__     = get_model_tester_from_test_class(__lowerCamelCase     )
						if tester_class is not None:
									tester_classes.append(__lowerCamelCase     )

    # sort with class names
			return sorted(__lowerCamelCase					,  key=lambda __lowerCamelCase     : x.__name__     )







def    _lowerCamelCase  (__lowerCamelCase  :     List[str]     )   ->    int:
			a__     = get_test_classes(__lowerCamelCase     )
			a__     = {test_class: get_model_tester_from_test_class(__lowerCamelCase     ) for test_class in test_classes}
			return test_tester_mapping







def    _lowerCamelCase  (__lowerCamelCase  :     Any     )   ->    List[Any]:
			a__     = get_model_classes(__lowerCamelCase     )
			a__     = {
			    model_class: get_test_classes_for_model(__lowerCamelCase					,  __lowerCamelCase     ) for model_class in model_classes
			}
			return model_test_mapping







def    _lowerCamelCase  (__lowerCamelCase  :     Tuple     )   ->    Tuple:
			a__     = get_model_classes(__lowerCamelCase     )
			a__     = {
			    model_class: get_tester_classes_for_model(__lowerCamelCase					,  __lowerCamelCase     ) for model_class in model_classes
			}
			return model_to_tester_mapping







def    _lowerCamelCase  (__lowerCamelCase  :     str     )   ->    Dict:
			if isinstance(__lowerCamelCase					,  __lowerCamelCase     ):
						return o
			elif isinstance(__lowerCamelCase					,  __lowerCamelCase     ):
						return o.__name__
			elif isinstance(__lowerCamelCase					,  (list, tuple)     ):
						return [to_json(__lowerCamelCase     ) for x in o]
			elif isinstance(__lowerCamelCase					,  __lowerCamelCase     ):
						return {to_json(__lowerCamelCase     ): to_json(__lowerCamelCase     ) for k, v in o.items()}
			else:
						return o



