class_docstr: RAG models were released with the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP
  Tasks](https://arxiv.org/abs/2005.11401) by Patrick Lewis, Ethan Perez, Aleksandra Piktus et al.
  
  RAG is a retriever augmented model and encapsulate three components: a question encoder, a dataset retriever and a
  generator, the encoder and generator are trainable while the retriever is just an indexed dataset.
class_docstr_tok: ["RAG","models","were","released","with","the","paper","[","Retrieval-Augmented","Generation","for","Knowledge-Intensive","NLP","Tasks","]","(","https",":","\/\/arxiv.org\/abs\/2005.11401",")","by","Patrick","Lewis",",","Ethan","Perez",",","Aleksandra","Piktus","et","al",".","RAG","is","a","retriever","augmented","model","and","encapsulate","three","components",":","a","question","encoder",",","a","dataset","retriever","and","a","generator",",","the","encoder","and","generator","are","trainable","while","the","retriever","is","just","an","indexed","dataset","."]
docstr: Instantiates an question encoder and a generator from one or two base classes of the library from pretrained
  model checkpoints.
  
  Params:
      question_encoder_pretrained_model_name_or_path (`str`, *optional*):
          Information necessary to initiate the question encoder. Can be either:
  
              - A string with the *shortcut name* of a pretrained model to load from cache or download, e.g.,
                `bert-base-uncased`.
              - A string with the *identifier name* of a pretrained model that was user-uploaded to our S3, e.g.,
                `dbmdz/bert-base-german-cased`.
              - A path to a *directory* containing model weights saved using
                [`~TFPreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
              - A path or url to a *pytorch index checkpoint file* (e.g, `./pt_model/`). In this case,
                `question_encoder_from_pt` should be set to `True`.
  
      generator_pretrained_model_name_or_path (`str`, *optional*, defaults to `None`):
          Information necessary to initiate the generator. Can be either:
  
              - A string with the *shortcut name* of a pretrained model to load from cache or download, e.g.,
                `t5-small`.
              - A string with the *identifier name* of a pretrained model that was user-uploaded to our S3, e.g.,
                `facebook/bart-base`.
              - A path to a *directory* containing model weights saved using
                [`~TFPreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
              - A path or url to a *pytorch checkpoint file* (e.g, `./pt_model/`). In this case,
                `generator_from_pt` should be set to `True`.
  
      model_args (remaining positional arguments, *optional*):
          All remaining positional arguments will be passed to the underlying model's `__init__` method.
      retriever ([`RagRetriever`], *optional*):
          The retriever to use.
      kwargs (remaining dictionary of keyword arguments, *optional*):
          Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
          `output_attentions=True`).
  
          - To update the question_encoder configuration, use the prefix *question_encoder_* for each
            configuration parameter.
          - To update the generator configuration, use the prefix *generator_* for each configuration parameter.
          - To update the parent model configuration, do not use a prefix for each configuration parameter.
  
          Behaves differently depending on whether a `config` is provided or automatically loaded.
  
  Example:
  
  ```python
  >>> from transformers import RagRetriever, TFRagModel
  
  >>> # initialize a RAG from two pretrained models.
  >>> model = TFRagModel.from_pretrained_question_encoder_generator(
  ...     "facebook/dpr-question_encoder-single-nq-base", "t5-small"
  ... )
  >>> # alternatively, initialize from pytorch pretrained models can also be done
  >>> model = TFRagModel.from_pretrained_question_encoder_generator(
  ...     "facebook/dpr-question_encoder-single-nq-base",
  ...     "facebook/bart-base",
  ...     generator_from_pt=True,
  ...     question_encoder_from_pt=True,
  ... )
  
  >>> # saving model after fine-tuning
  >>> model.save_pretrained("./rag")
  
  >>> # load retriever
  >>> retriever = RagRetriever.from_pretrained(
  ...     "facebook/rag-token-base", index_name="exact", use_dummy_dataset=True
  ... )
  >>> # load fine-tuned model with retriever
  >>> model = TFRagModel.from_pretrained("./rag", retriever=retriever)
  ```
docstr_tok: ["Instantiates","an","question","encoder","and","a","generator","from","one","or","two","base","classes","of","the","library","from","pretrained","model","checkpoints",".","Params",":","question_encoder_pretrained_model_name_or_path","(","`","str","`",",","*","optional","*",")",":","Information","necessary","to","initiate","the","question","encoder",".","Can","be","either",":","-","A","string","with","the","*","shortcut","name","*","of","a","pretrained","model","to","load","from","cache","or","download",",","e.g.",",","`","bert-base-uncased","`",".","-","A","string","with","the","*","identifier","name","*","of","a","pretrained","model","that","was","user-uploaded","to","our","S3",",","e.g.",",","`","dbmdz\/bert-base-german-cased","`",".","-","A","path","to","a","*","directory","*","containing","model","weights","saved","using","[","`","~TFPreTrainedModel.save_pretrained","`","]",",","e.g.",",","`",".\/my_model_directory\/","`",".","-","A","path","or","url","to","a","*","pytorch","index","checkpoint","file","*","(","e.g",",","`",".\/pt_model\/","`",")",".","In","this","case",",","`","question_encoder_from_pt","`","should","be","set","to","`","True","`",".","generator_pretrained_model_name_or_path","(","`","str","`",",","*","optional","*",",","defaults","to","`","None","`",")",":","Information","necessary","to","initiate","the","generator",".","Can","be","either",":","-","A","string","with","the","*","shortcut","name","*","of","a","pretrained","model","to","load","from","cache","or","download",",","e.g.",",","`","t5-small","`",".","-","A","string","with","the","*","identifier","name","*","of","a","pretrained","model","that","was","user-uploaded","to","our","S3",",","e.g.",",","`","facebook\/bart-base","`",".","-","A","path","to","a","*","directory","*","containing","model","weights","saved","using","[","`","~TFPreTrainedModel.save_pretrained","`","]",",","e.g.",",","`",".\/my_model_directory\/","`",".","-","A","path","or","url","to","a","*","pytorch","checkpoint","file","*","(","e.g",",","`",".\/pt_model\/","`",")",".","In","this","case",",","`","generator_from_pt","`","should","be","set","to","`","True","`",".","model_args","(","remaining","positional","arguments",",","*","optional","*",")",":","All","remaining","positional","arguments","will","be","passed","to","the","underlying","model","'s","`","__init__","`","method",".","retriever","(","[","`","RagRetriever","`","]",",","*","optional","*",")",":","The","retriever","to","use",".","kwargs","(","remaining","dictionary","of","keyword","arguments",",","*","optional","*",")",":","Can","be","used","to","update","the","configuration","object","(","after","it","being","loaded",")","and","initiate","the","model","(","e.g.",",","`","output_attentions=True","`",")",".","-","To","update","the","question_encoder","configuration",",","use","the","prefix","*","question_encoder_","*","for","each","configuration","parameter",".","-","To","update","the","generator","configuration",",","use","the","prefix","*","generator_","*","for","each","configuration","parameter",".","-","To","update","the","parent","model","configuration",",","do","not","use","a","prefix","for","each","configuration","parameter",".","Behaves","differently","depending","on","whether","a","`","config","`","is","provided","or","automatically","loaded",".","Example",":","``","`","python",">",">",">","from","transformers","import","RagRetriever",",","TFRagModel",">",">",">","#","initialize","a","RAG","from","two","pretrained","models",".",">",">",">","model","=","TFRagModel.from_pretrained_question_encoder_generator","(","...","``","facebook\/dpr-question_encoder-single-nq-base","''",",","``","t5-small","''","...",")",">",">",">","#","alternatively",",","initialize","from","pytorch","pretrained","models","can","also","be","done",">",">",">","model","=","TFRagModel.from_pretrained_question_encoder_generator","(","...","``","facebook\/dpr-question_encoder-single-nq-base","''",",","...","``","facebook\/bart-base","''",",","...","generator_from_pt=True",",","...","question_encoder_from_pt=True",",","...",")",">",">",">","#","saving","model","after","fine-tuning",">",">",">","model.save_pretrained","(","``",".\/rag","''",")",">",">",">","#","load","retriever",">",">",">","retriever","=","RagRetriever.from_pretrained","(","...","``","facebook\/rag-token-base","''",",","index_name=","''","exact","''",",","use_dummy_dataset=True","...",")",">",">",">","#","load","fine-tuned","model","with","retriever",">",">",">","model","=","TFRagModel.from_pretrained","(","``",".\/rag","''",",","retriever=retriever",")","``","`"]
code: def from_pretrained_question_encoder_generator(
      cls,
      question_encoder_pretrained_model_name_or_path: str = None,
      generator_pretrained_model_name_or_path: str = None,
      retriever: RagRetriever = None,
      *model_args,
      **kwargs,
  ) -> TFPreTrainedModel:
      r"""
      Instantiates an question encoder and a generator from one or two base classes of the library from pretrained
      model checkpoints.
  
      Params:
          question_encoder_pretrained_model_name_or_path (`str`, *optional*):
              Information necessary to initiate the question encoder. Can be either:
  
                  - A string with the *shortcut name* of a pretrained model to load from cache or download, e.g.,
                    `bert-base-uncased`.
                  - A string with the *identifier name* of a pretrained model that was user-uploaded to our S3, e.g.,
                    `dbmdz/bert-base-german-cased`.
                  - A path to a *directory* containing model weights saved using
                    [`~TFPreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
                  - A path or url to a *pytorch index checkpoint file* (e.g, `./pt_model/`). In this case,
                    `question_encoder_from_pt` should be set to `True`.
  
          generator_pretrained_model_name_or_path (`str`, *optional*, defaults to `None`):
              Information necessary to initiate the generator. Can be either:
  
                  - A string with the *shortcut name* of a pretrained model to load from cache or download, e.g.,
                    `t5-small`.
                  - A string with the *identifier name* of a pretrained model that was user-uploaded to our S3, e.g.,
                    `facebook/bart-base`.
                  - A path to a *directory* containing model weights saved using
                    [`~TFPreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
                  - A path or url to a *pytorch checkpoint file* (e.g, `./pt_model/`). In this case,
                    `generator_from_pt` should be set to `True`.
  
          model_args (remaining positional arguments, *optional*):
              All remaining positional arguments will be passed to the underlying model's `__init__` method.
          retriever ([`RagRetriever`], *optional*):
              The retriever to use.
          kwargs (remaining dictionary of keyword arguments, *optional*):
              Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
              `output_attentions=True`).
  
              - To update the question_encoder configuration, use the prefix *question_encoder_* for each
                configuration parameter.
              - To update the generator configuration, use the prefix *generator_* for each configuration parameter.
              - To update the parent model configuration, do not use a prefix for each configuration parameter.
  
              Behaves differently depending on whether a `config` is provided or automatically loaded.
  
      Example:
  
      ```python
      >>> from transformers import RagRetriever, TFRagModel
  
      >>> # initialize a RAG from two pretrained models.
      >>> model = TFRagModel.from_pretrained_question_encoder_generator(
      ...     "facebook/dpr-question_encoder-single-nq-base", "t5-small"
      ... )
      >>> # alternatively, initialize from pytorch pretrained models can also be done
      >>> model = TFRagModel.from_pretrained_question_encoder_generator(
      ...     "facebook/dpr-question_encoder-single-nq-base",
      ...     "facebook/bart-base",
      ...     generator_from_pt=True,
      ...     question_encoder_from_pt=True,
      ... )
  
      >>> # saving model after fine-tuning
      >>> model.save_pretrained("./rag")
  
      >>> # load retriever
      >>> retriever = RagRetriever.from_pretrained(
      ...     "facebook/rag-token-base", index_name="exact", use_dummy_dataset=True
      ... )
      >>> # load fine-tuned model with retriever
      >>> model = TFRagModel.from_pretrained("./rag", retriever=retriever)
      ```"""
  
      kwargs_question_encoder = {
          argument[len("question_encoder_") :]: value
          for argument, value in kwargs.items()
          if argument.startswith("question_encoder_")
      }
  
      kwargs_generator = {
          argument[len("generator_") :]: value
          for argument, value in kwargs.items()
          if argument.startswith("generator_")
      }
  
      # remove question_encoder, generator kwargs from kwargs
      for key in kwargs_question_encoder.keys():
          del kwargs["question_encoder_" + key]
      for key in kwargs_generator.keys():
          del kwargs["generator_" + key]
  
      # Load and initialize the question_encoder and generator
      # The distinction between question_encoder and generator at the model level is made
      # by the value of the flag `is_generator` that we need to set correctly.
      question_encoder = kwargs_question_encoder.pop("model", None)
      if question_encoder is None:
          assert (
              question_encoder_pretrained_model_name_or_path is not None
          ), (
              "If `model` is not defined as an argument, a `question_encoder_pretrained_model_name_or_path` has to"
              " be defined"
          )
  
          from ..auto.modeling_tf_auto import TFAutoModel
  
          if "config" not in kwargs_question_encoder:
              from ..auto.configuration_auto import AutoConfig
  
              question_encoder_config = AutoConfig.from_pretrained(
                  question_encoder_pretrained_model_name_or_path
              )
              kwargs_question_encoder[
                  "config"
              ] = question_encoder_config
  
          question_encoder = TFAutoModel.from_pretrained(
              question_encoder_pretrained_model_name_or_path,
              name="question_encoder",
              load_weight_prefix=cls.load_weight_prefix,
              *model_args,
              **kwargs_question_encoder,
          )
  
      generator = kwargs_generator.pop("generator", None)
      if generator is None:
          assert generator_pretrained_model_name_or_path is not None, (
              "If `generator_model` is not defined as an argument, a `generator_pretrained_model_name_or_path` has"
              " to be defined"
          )
  
          from ..auto.modeling_tf_auto import TFAutoModelForSeq2SeqLM
  
          if "config" not in kwargs_generator:
              from ..auto.configuration_auto import AutoConfig
  
              generator_config = AutoConfig.from_pretrained(
                  generator_pretrained_model_name_or_path
              )
              kwargs_generator["config"] = generator_config
  
          generator = TFAutoModelForSeq2SeqLM.from_pretrained(
              generator_pretrained_model_name_or_path,
              name="generator",
              load_weight_prefix=cls.load_weight_prefix,
              **kwargs_generator,
          )
  
      # instantiate config with corresponding kwargs
      config = kwargs.get("config", None)
      if config is None:
          config = RagConfig.from_question_encoder_generator_configs(
              question_encoder.config, generator.config, **kwargs
          )
  
      return cls(
          question_encoder=question_encoder,
          generator=generator,
          config=config,
          retriever=retriever,
      )
  
imports: from __future__ import annotations
  import copy
  from dataclasses import dataclass
  from typing import List, Optional, Tuple, Union
  import numpy
  import tensorflow
  from ...configuration_utils import PretrainedConfig
  from ...generation import TFLogitsProcessorList
  from ...modeling_tf_utils import TFCausalLanguageModelingLoss, TFModelInputType, TFPreTrainedModel, shape_list, unpack_inputs
  from ...utils import ModelOutput, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
  from .configuration_rag import RagConfig
  from .retrieval_rag import RagRetriever
label_desc: Use image node_id 1 for calling the TFRagPreTrainedModel obj's underlying member method code with example usage: obj.from_pretrained_question_encoder_generator(cls, question_encoder_pretrained_model_name_or_path, generator_pretrained_model_name_or_path, retriever) and returns: cls