
"""simple docstring"""







import copy

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


lowercase__		:    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(lowercase_  )
class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""rag"""
      _SCREAMING_SNAKE_CASE      =					True



      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=None   ,      SCREAMING_SNAKE_CASE_    :				Dict=True   ,      SCREAMING_SNAKE_CASE_    :				Any=None   ,      SCREAMING_SNAKE_CASE_    :				Dict=None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=" / "   ,      SCREAMING_SNAKE_CASE_    :				int=" // "   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=5   ,      SCREAMING_SNAKE_CASE_    :				Dict=3_0_0   ,      SCREAMING_SNAKE_CASE_    :				int=7_6_8   ,      SCREAMING_SNAKE_CASE_    :				Tuple=8   ,      SCREAMING_SNAKE_CASE_    :				List[Any]="wiki_dpr"   ,      SCREAMING_SNAKE_CASE_    :				List[str]="train"   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]="compressed"   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=None   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=None   ,      SCREAMING_SNAKE_CASE_    :				Dict=False   ,      SCREAMING_SNAKE_CASE_    :				Tuple=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=0.0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True   ,      SCREAMING_SNAKE_CASE_    :				int=False   ,      SCREAMING_SNAKE_CASE_    :				int=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=None   ,      **SCREAMING_SNAKE_CASE_    :				Optional[Any]   ,      ):
        super().__init__(
            bos_token_id=__A   ,      pad_token_id=__A   ,      eos_token_id=__A   ,      decoder_start_token_id=__A   ,      forced_eos_token_id=__A   ,      is_encoder_decoder=__A   ,      prefix=__A   ,      vocab_size=__A   ,      **__A   ,      )
        assert (
            "question_encoder" in kwargs and "generator" in kwargs
        ), "Config has to be initialized with question_encoder and generator config"
        lowerCAmelCase_       :    Union[str, Any]								=			kwargs.pop('question_encoder'	)
        lowerCAmelCase_       :    Any								=			question_encoder_config.pop('model_type'	)
        lowerCAmelCase_       :    Tuple								=			kwargs.pop('generator'	)
        lowerCAmelCase_       :    Optional[int]								=			decoder_config.pop('model_type'	)

        from ..auto.configuration_auto import AutoConfig

        lowerCAmelCase_       :    Union[str, Any]								=			AutoConfig.for_model(__A   ,      **__A	)
        lowerCAmelCase_       :    Union[str, Any]								=			AutoConfig.for_model(__A   ,      **__A	)

        lowerCAmelCase_       :    int								=			reduce_loss
        lowerCAmelCase_       :    List[str]								=			label_smoothing
        lowerCAmelCase_       :    str								=			exclude_bos_score
        lowerCAmelCase_       :    int								=			do_marginalize

        lowerCAmelCase_       :    Dict								=			title_sep
        lowerCAmelCase_       :    Optional[Any]								=			doc_sep
        lowerCAmelCase_       :    Tuple								=			n_docs
        lowerCAmelCase_       :    Optional[Any]								=			max_combined_length

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

        lowerCAmelCase_       :    Union[str, Any]								=			retrieval_vector_size
        lowerCAmelCase_       :    List[Any]								=			retrieval_batch_size
        lowerCAmelCase_       :    Any								=			passages_path
        lowerCAmelCase_       :    str								=			index_path
        lowerCAmelCase_       :    Union[str, Any]								=			use_dummy_dataset

        lowerCAmelCase_       :    int								=			output_retrieved

        lowerCAmelCase_       :    Optional[Any]								=			do_deduplication

        lowerCAmelCase_       :    str								=			use_cache

        if self.forced_eos_token_id is None:
          lowerCAmelCase_       :    int								=			getattr(self.generator   ,      'forced_eos_token_id'   ,      __A	)



      @classmethod
      def 					SCREAMING_SNAKE_CASE__			(				cls    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				PretrainedConfig   ,      SCREAMING_SNAKE_CASE_    :				PretrainedConfig   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]	):
        return cls(question_encoder=question_encoder_config.to_dict()   ,      generator=generator_config.to_dict()   ,      **__A	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        lowerCAmelCase_       :    Union[str, Any]								=			copy.deepcopy(self.__dict__	)
        lowerCAmelCase_       :    Dict								=			self.question_encoder.to_dict()
        lowerCAmelCase_       :    int								=			self.generator.to_dict()
        lowerCAmelCase_       :    List[Any]								=			self.__class__.model_type
        return output










"""simple docstring"""







import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


lowercase__		:    Tuple				   =     datasets.logging.get_logger(__name__)


lowercase__		:    List[Any]				   =     """\
@inproceedings{bleurt,
  title={BLEURT: Learning Robust Metrics for Text Generation},
  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},
  booktitle={ACL},
  year={2020},
  url={https://arxiv.org/abs/2004.04696}
}
"""

lowercase__		:    Tuple				   =     """\
BLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)
and then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune
it for your specific application (the latter is expected to perform better).

See the project's README at https://github.com/google-research/bleurt#readme for more information.
"""

lowercase__		:    List[Any]				   =     """
BLEURT score.

Args:
    `predictions` (list of str): prediction/candidate sentences
    `references` (list of str): reference sentences
    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.

Returns:
    'scores': List of scores.
Examples:

    >>> predictions = [\"hello there\", \"general kenobi\"]
    >>> references = [\"hello there\", \"general kenobi\"]
    >>> bleurt = datasets.load_metric(\"bleurt\")
    >>> results = bleurt.compute(predictions=predictions, references=references)
    >>> print([round(v, 2) for v in results[\"scores\"]])
    [1.03, 1.04]
"""

lowercase__		:    List[Any]				   =     {
    """bleurt-tiny-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip""",
    """bleurt-tiny-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip""",
    """bleurt-base-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip""",
    """bleurt-base-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip""",
    """bleurt-large-128""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip""",
    """bleurt-large-512""": """https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip""",
    """BLEURT-20-D3""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip""",
    """BLEURT-20-D6""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip""",
    """BLEURT-20-D12""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip""",
    """BLEURT-20""": """https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip""",
}







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION  )
class 				UpperCamelCase__       (     datasets.Metric  ):




      """simple docstring"""




      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        return datasets.MetricInfo(
            description=_DESCRIPTION   ,      citation=_CITATION   ,      homepage='https://github.com/google-research/bleurt'   ,      inputs_description=_KWARGS_DESCRIPTION   ,      features=datasets.Features(
                {
                    'predictions': datasets.Value('string'   ,      id='sequence'	),
                    'references': datasets.Value('string'   ,      id='sequence'	),
                }	)   ,      codebase_urls=['https://github.com/google-research/bleurt']   ,      reference_urls=['https://github.com/google-research/bleurt', 'https://arxiv.org/abs/2004.04696']   ,      )



      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				str	):
        # check that config name specifies a valid BLEURT model
        if self.config_name == "default":
          logger.warning(
              'Using default BLEURT-Base checkpoint for sequence maximum length 128. '
              'You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').'	)
          lowerCAmelCase_       :    List[Any]								=			'bleurt-base-128'

        if self.config_name.lower() in CHECKPOINT_URLS:
          lowerCAmelCase_       :    List[Any]								=			self.config_name.lower()

        elif self.config_name.upper() in CHECKPOINT_URLS:
          lowerCAmelCase_       :    Tuple								=			self.config_name.upper()

        else:
          raise KeyError(
              F"{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}"	)

        # download the model checkpoint specified by self.config_name and set up the scorer
        lowerCAmelCase_       :    List[Any]								=			dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]	)
        lowerCAmelCase_       :    List[str]								=			score.BleurtScorer(os.path.join(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				Any	):
        lowerCAmelCase_       :    Tuple								=			self.scorer.score(references=SCREAMING_SNAKE_CASE_   ,      candidates=SCREAMING_SNAKE_CASE_	)
        return {"scores": scores}





