






"""simple docstring"""



import copy

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


_lowerCAmelCase	:	Any				=      r"""
    [`RagConfig`] stores the configuration of a *RagModel*. Configuration objects inherit from [`PretrainedConfig`] and
    can be used to control the model outputs. Read the documentation from [`PretrainedConfig`] for more information.

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




@add_start_docstrings(__magic_name__   )
class 			lowerCAmelCase__			(    __magic_name__   ):
   SCREAMING_SNAKE_CASE_    ='''rag'''
   SCREAMING_SNAKE_CASE_    =True
   def __init__(		self      :	List[Any]							,			snake_case__      :	Dict=None							,			snake_case__      :	List[Any]=True							,			snake_case__      :	List[Any]=None							,			snake_case__      :	str=None							,			snake_case__      :	Dict=None							,			snake_case__      :	int=None							,			snake_case__      :	Optional[Any]=None							,			snake_case__      :	Union[str, Any]=" / "							,			snake_case__      :	Union[str, Any]=" // "							,			snake_case__      :	Optional[int]=5							,			snake_case__      :	List[Any]=3_0_0							,			snake_case__      :	List[str]=7_6_8							,			snake_case__      :	Dict=8							,			snake_case__      :	Optional[Any]="wiki_dpr"							,			snake_case__      :	List[Any]="train"							,			snake_case__      :	List[str]="compressed"							,			snake_case__      :	int=None							,			snake_case__      :	List[Any]=None							,			snake_case__      :	Dict=False							,			snake_case__      :	str=False							,			snake_case__      :	str=0.0							,			snake_case__      :	Any=True							,			snake_case__      :	Dict=False							,			snake_case__      :	Union[str, Any]=False							,			snake_case__      :	Optional[int]=False							,			snake_case__      :	Tuple=True							,			snake_case__      :	str=None							,			**snake_case__      :	str							,			):


        '''simple docstring'''
        super().__init__(
            bos_token_id=snake_case__							,			pad_token_id=snake_case__							,			eos_token_id=snake_case__							,			decoder_start_token_id=snake_case__							,			forced_eos_token_id=snake_case__							,			is_encoder_decoder=snake_case__							,			prefix=snake_case__							,			vocab_size=snake_case__							,			**snake_case__							,			)
        assert (
            "question_encoder" in kwargs and "generator" in kwargs
        ), "Config has to be initialized with question_encoder and generator config"
        UpperCAmelCase__						:    Tuple							=							kwargs.pop("question_encoder"							)
        UpperCAmelCase__						:    Union[str, Any]							=							question_encoder_config.pop("model_type"							)
        UpperCAmelCase__						:    List[Any]							=							kwargs.pop("generator"							)
        UpperCAmelCase__						:    List[Any]							=							decoder_config.pop("model_type"							)

        from ..auto.configuration_auto import AutoConfig

        UpperCAmelCase__						:    Optional[Any]							=							AutoConfig.for_model(snake_case__							,			**snake_case__							)
        UpperCAmelCase__						:    Dict							=							AutoConfig.for_model(snake_case__							,			**snake_case__							)

        UpperCAmelCase__						:    Union[str, Any]							=							reduce_loss
        UpperCAmelCase__						:    Any							=							label_smoothing
        UpperCAmelCase__						:    str							=							exclude_bos_score
        UpperCAmelCase__						:    List[Any]							=							do_marginalize

        UpperCAmelCase__						:    List[str]							=							title_sep
        UpperCAmelCase__						:    Optional[int]							=							doc_sep
        UpperCAmelCase__						:    str							=							n_docs
        UpperCAmelCase__						:    int							=							max_combined_length

        UpperCAmelCase__						:    Optional[Any]							=							dataset
        UpperCAmelCase__						:    Dict							=							dataset_split
        UpperCAmelCase__						:    Dict							=							index_name

        UpperCAmelCase__						:    Union[str, Any]							=							retrieval_vector_size
        UpperCAmelCase__						:    Tuple							=							retrieval_batch_size
        UpperCAmelCase__						:    Optional[int]							=							passages_path
        UpperCAmelCase__						:    str							=							index_path
        UpperCAmelCase__						:    str							=							use_dummy_dataset

        UpperCAmelCase__						:    Optional[Any]							=							output_retrieved

        UpperCAmelCase__						:    List[str]							=							do_deduplication

        UpperCAmelCase__						:    Optional[Any]							=							use_cache

        if self.forced_eos_token_id is None:
             UpperCAmelCase__						:    str							=							getattr(self.generator							,			"forced_eos_token_id"							,			snake_case__							)
   @classmethod
   def       __a							(		cls      :	Any							,			snake_case__      :	PretrainedConfig							,			snake_case__      :	PretrainedConfig							,			**snake_case__      :	Union[str, Any]							):


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




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


        '''simple docstring'''
        UpperCAmelCase__						:    List[Any]							=							copy.deepcopy(self.__dict__							)
        UpperCAmelCase__						:    Dict							=							self.question_encoder.to_dict()
        UpperCAmelCase__						:    Dict							=							self.generator.to_dict()
        UpperCAmelCase__						:    Dict							=							self.__class__.model_type
        return output










"""simple docstring"""



import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


_lowerCAmelCase	:	Union[str, Any]				=      threading.Lock()
_lowerCAmelCase	:	Optional[logging.Handler]				=      None

_lowerCAmelCase	:	Union[str, Any]				=      {
    """debug""": logging.DEBUG,
    """info""": logging.INFO,
    """warning""": logging.WARNING,
    """error""": logging.ERROR,
    """critical""": logging.CRITICAL,
}

_lowerCAmelCase	:	Dict				=      logging.WARNING

_lowerCAmelCase	:	Optional[Any]				=      True


def 	SCREAMING_SNAKE_CASE__       (  )->		List[str]:






     '''simple docstring'''



     UpperCAmelCase__						:    Union[str, Any]							=							os.getenv("TRANSFORMERS_VERBOSITY"			,					snake_case				)
     if env_level_str:
          if env_level_str in log_levels:
               return log_levels[env_level_str]
          else:
               logging.getLogger().warning(
                   f'Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, '
                   f'has to be one of: { ", ".join(log_levels.keys()				) }'				)
     return _default_log_level


def 	SCREAMING_SNAKE_CASE__       (  )->		str:






     '''simple docstring'''



     return __name__.split("."				)[0]


def 	SCREAMING_SNAKE_CASE__       (  )->		logging.Logger:






     '''simple docstring'''



     return logging.getLogger(_get_library_name()				)


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     global _default_handler

     with _lock:
          if _default_handler:
               # This library has already configured the library root logger.
               return
          UpperCAmelCase__						:    Any							=							logging.StreamHandler()  # Set sys.stderr as stream.
          UpperCAmelCase__						:    Union[str, Any]							=							sys.stderr.flush

          # Apply our default configuration to the library root logger.
          UpperCAmelCase__						:    Union[str, Any]							=							_get_library_root_logger()
          library_root_logger.addHandler(_default_handler				)
          library_root_logger.setLevel(_get_default_logging_level()				)
          UpperCAmelCase__						:    int							=							False


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     global _default_handler

     with _lock:
          if not _default_handler:
               return

          UpperCAmelCase__						:    Union[str, Any]							=							_get_library_root_logger()
          library_root_logger.removeHandler(_default_handler				)
          library_root_logger.setLevel(logging.NOTSET				)
          UpperCAmelCase__						:    Optional[int]							=							None


def 	SCREAMING_SNAKE_CASE__       (  )->		Optional[int]:






     '''simple docstring'''



     return log_levels


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : Optional[str] = None				)->		logging.Logger:






     '''simple docstring'''



     if name is None:
          UpperCAmelCase__						:    Tuple							=							_get_library_name()

     _configure_library_root_logger()
     return logging.getLogger(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		int:






     '''simple docstring'''



     _configure_library_root_logger()
     return _get_library_root_logger().getEffectiveLevel()


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : int				)->		None:






     '''simple docstring'''



     _configure_library_root_logger()
     _get_library_root_logger().setLevel(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		Optional[Any]:






     '''simple docstring'''



     return set_verbosity(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		List[str]:






     '''simple docstring'''



     return set_verbosity(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		Tuple:






     '''simple docstring'''



     return set_verbosity(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		str:






     '''simple docstring'''



     return set_verbosity(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     _configure_library_root_logger()

     assert _default_handler is not None
     _get_library_root_logger().removeHandler(_default_handler				)


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     _configure_library_root_logger()

     assert _default_handler is not None
     _get_library_root_logger().addHandler(_default_handler				)


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : logging.Handler				)->		None:






     '''simple docstring'''



     _configure_library_root_logger()

     assert handler is not None
     _get_library_root_logger().addHandler(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  snake_case       : logging.Handler				)->		None:






     '''simple docstring'''



     _configure_library_root_logger()

     assert handler is not None and handler not in _get_library_root_logger().handlers
     _get_library_root_logger().removeHandler(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     _configure_library_root_logger()
     UpperCAmelCase__						:    Dict							=							False


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     _configure_library_root_logger()
     UpperCAmelCase__						:    List[Any]							=							True


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     UpperCAmelCase__						:    Union[str, Any]							=							_get_library_root_logger().handlers

     for handler in handlers:
          UpperCAmelCase__						:    Union[str, Any]							=							logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"				)
          handler.setFormatter(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  )->		None:






     '''simple docstring'''



     UpperCAmelCase__						:    Any							=							_get_library_root_logger().handlers

     for handler in handlers:
          handler.setFormatter(snake_case				)


def 	SCREAMING_SNAKE_CASE__       (  self       : Optional[int]			,					*snake_case       : List[str]			,					**snake_case       : str				)->		Union[str, Any]:






     '''simple docstring'''



     UpperCAmelCase__						:    Union[str, Any]							=							os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS"			,					snake_case				)
     if no_advisory_warnings:
          return
     self.warning(*snake_case			,					**snake_case				)


_lowerCAmelCase	:	int				=      warning_advice


@functools.lru_cache(snake_case				)
def 	SCREAMING_SNAKE_CASE__       (  self       : Optional[Any]			,					*snake_case       : Dict			,					**snake_case       : Any				)->		Any:






     '''simple docstring'''



     self.warning(*snake_case			,					**snake_case				)


_lowerCAmelCase	:	Tuple				=      warning_once




class 			lowerCAmelCase__			:
   def __init__(		self      :	List[str]							,			*snake_case__      :	Any							,			**snake_case__      :	List[str]							):  # pylint: disable=unused-argument


        '''simple docstring'''
        UpperCAmelCase__						:    List[Any]							=							args[0] if args else None
   def __iter__(		self      :	Any							):


        '''simple docstring'''
        return iter(self._iterator							)
   def __getattr__(		self      :	List[Any]							,			snake_case__      :	Tuple							):


        '''simple docstring'''

        def empty_fn(*snake_case__      :	Dict							,			**snake_case__      :	str							):  # pylint: disable=unused-argument
             return

        return empty_fn
   def __enter__(		self      :	Optional[Any]							):


        '''simple docstring'''
        return self




   def __exit__(		self      :	Tuple							,			snake_case__      :	Tuple							,			snake_case__      :	Any							,			snake_case__      :	Optional[int]							):


        '''simple docstring'''
        return




class 			lowerCAmelCase__			:
   def __call__(		self      :	Optional[Any]							,			*snake_case__      :	Optional[Any]							,			**snake_case__      :	Tuple							):


        '''simple docstring'''
        if _tqdm_active:
             return tqdm_lib.tqdm(*snake_case__							,			**snake_case__							)
        else:
             return EmptyTqdm(*snake_case__							,			**snake_case__							)
   def       __a							(		self      :	Dict							,			*snake_case__      :	List[str]							,			**snake_case__      :	Union[str, Any]							):


        '''simple docstring'''
        UpperCAmelCase__						:    Dict							=							None
        if _tqdm_active:
             return tqdm_lib.tqdm.set_lock(*snake_case__							,			**snake_case__							)




   def       __a							(		self      :	Tuple							):


        '''simple docstring'''
        if _tqdm_active:
             return tqdm_lib.tqdm.get_lock()




_lowerCAmelCase	:	Optional[int]				=      _tqdm_cls()


def 	SCREAMING_SNAKE_CASE__       (  )->		bool:






     '''simple docstring'''



     global _tqdm_active
     return bool(_tqdm_active				)


def 	SCREAMING_SNAKE_CASE__       (  )->		List[Any]:






     '''simple docstring'''



     global _tqdm_active
     UpperCAmelCase__						:    int							=							True
     hf_hub_utils.enable_progress_bars()



def 	SCREAMING_SNAKE_CASE__       (  )->		List[Any]:






     '''simple docstring'''



     global _tqdm_active
     UpperCAmelCase__						:    Optional[Any]							=							False
     hf_hub_utils.disable_progress_bars()


