docstr: exclude_bos_score (`bool`, *optional*):
      Only relevant if `labels` is passed. If `True`, the score of the BOS token is disregarded when computing
      the loss.
  reduce_loss (`bool`, *optional*):
      Only relevant if `labels` is passed. If `True`, the NLL loss is reduced using the `torch.Tensor.sum`
      operation.
  kwargs (`Dict[str, any]`, optional, defaults to *{}*):
       Legacy dictionary, which is required so that model can use *generate()* function.
  
  Returns:
  
  Example:
  
  ```python
  >>> from transformers import AutoTokenizer, RagRetriever, RagSequenceForGeneration
  >>> import torch
  
  >>> tokenizer = AutoTokenizer.from_pretrained("facebook/rag-sequence-nq")
  >>> retriever = RagRetriever.from_pretrained(
  ...     "facebook/rag-sequence-nq", index_name="exact", use_dummy_dataset=True
  ... )
  >>> # initialize with RagRetriever to do everything in one forward call
  >>> model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
  
  >>> inputs = tokenizer("How many people live in Paris?", return_tensors="pt")
  >>> targets = tokenizer(text_target="In Paris, there are 10 million people.", return_tensors="pt")
  >>> input_ids = inputs["input_ids"]
  >>> labels = targets["input_ids"]
  >>> outputs = model(input_ids=input_ids, labels=labels)
  
  >>> # or use retriever separately
  >>> model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq", use_dummy_dataset=True)
  >>> # 1. Encode
  >>> question_hidden_states = model.question_encoder(input_ids)[0]
  >>> # 2. Retrieve
  >>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
  >>> doc_scores = torch.bmm(
  ...     question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
  ... ).squeeze(1)
  >>> # 3. Forward to generator
  >>> outputs = model(
  ...     context_input_ids=docs_dict["context_input_ids"],
  ...     context_attention_mask=docs_dict["context_attention_mask"],
  ...     doc_scores=doc_scores,
  ...     decoder_input_ids=labels,
  ... )
  ```
code: def forward(
      self,
      input_ids: Optional[torch.LongTensor] = None,
      attention_mask: Optional[torch.Tensor] = None,
      encoder_outputs: Optional[Tuple[Tuple[torch.Tensor]]] = None,
      decoder_input_ids: Optional[torch.LongTensor] = None,
      decoder_attention_mask: Optional[torch.BoolTensor] = None,
      past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
      context_input_ids: Optional[torch.LongTensor] = None,
      context_attention_mask: Optional[torch.LongTensor] = None,
      doc_scores: Optional[torch.FloatTensor] = None,
      use_cache: Optional[bool] = None,
      output_attentions: Optional[bool] = None,
      output_hidden_states: Optional[bool] = None,
      output_retrieved: Optional[bool] = None,
      exclude_bos_score: Optional[bool] = None,
      reduce_loss: Optional[bool] = None,
      labels: Optional[torch.LongTensor] = None,
      n_docs: Optional[int] = None,
      **kwargs,  # needs kwargs for generation
  ) -> RetrievAugLMMarginOutput:
      r"""
      exclude_bos_score (`bool`, *optional*):
          Only relevant if `labels` is passed. If `True`, the score of the BOS token is disregarded when computing
          the loss.
      reduce_loss (`bool`, *optional*):
          Only relevant if `labels` is passed. If `True`, the NLL loss is reduced using the `torch.Tensor.sum`
          operation.
      kwargs (`Dict[str, any]`, optional, defaults to *{}*):
           Legacy dictionary, which is required so that model can use *generate()* function.
  
      Returns:
  
      Example:
  
      ```python
      >>> from transformers import AutoTokenizer, RagRetriever, RagSequenceForGeneration
      >>> import torch
  
      >>> tokenizer = AutoTokenizer.from_pretrained("facebook/rag-sequence-nq")
      >>> retriever = RagRetriever.from_pretrained(
      ...     "facebook/rag-sequence-nq", index_name="exact", use_dummy_dataset=True
      ... )
      >>> # initialize with RagRetriever to do everything in one forward call
      >>> model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
  
      >>> inputs = tokenizer("How many people live in Paris?", return_tensors="pt")
      >>> targets = tokenizer(text_target="In Paris, there are 10 million people.", return_tensors="pt")
      >>> input_ids = inputs["input_ids"]
      >>> labels = targets["input_ids"]
      >>> outputs = model(input_ids=input_ids, labels=labels)
  
      >>> # or use retriever separately
      >>> model = RagSequenceForGeneration.from_pretrained("facebook/rag-sequence-nq", use_dummy_dataset=True)
      >>> # 1. Encode
      >>> question_hidden_states = model.question_encoder(input_ids)[0]
      >>> # 2. Retrieve
      >>> docs_dict = retriever(input_ids.numpy(), question_hidden_states.detach().numpy(), return_tensors="pt")
      >>> doc_scores = torch.bmm(
      ...     question_hidden_states.unsqueeze(1), docs_dict["retrieved_doc_embeds"].float().transpose(1, 2)
      ... ).squeeze(1)
      >>> # 3. Forward to generator
      >>> outputs = model(
      ...     context_input_ids=docs_dict["context_input_ids"],
      ...     context_attention_mask=docs_dict["context_attention_mask"],
      ...     doc_scores=doc_scores,
      ...     decoder_input_ids=labels,
      ... )
      ```"""
      n_docs = n_docs if n_docs is not None else self.config.n_docs
      exclude_bos_score = (
          exclude_bos_score
          if exclude_bos_score is not None
          else self.config.exclude_bos_score
      )
      reduce_loss = (
          reduce_loss
          if reduce_loss is not None
          else self.config.reduce_loss
      )
  
      if labels is not None:
          if decoder_input_ids is None:
              decoder_input_ids = labels
          use_cache = False
  
      outputs = self.rag(
          input_ids=input_ids,
          attention_mask=attention_mask,
          encoder_outputs=encoder_outputs,
          decoder_input_ids=decoder_input_ids,
          decoder_attention_mask=decoder_attention_mask,
          context_input_ids=context_input_ids,
          context_attention_mask=context_attention_mask,
          doc_scores=doc_scores,
          past_key_values=past_key_values,
          use_cache=use_cache,
          output_attentions=output_attentions,
          output_hidden_states=output_hidden_states,
          output_retrieved=output_retrieved,
          n_docs=n_docs,
      )
  
      loss = None
      if labels is not None:
          loss = self.get_nll(
              outputs.logits,
              outputs.doc_scores,
              decoder_input_ids,
              reduce_loss=reduce_loss,
              epsilon=self.config.label_smoothing,
              exclude_bos_score=exclude_bos_score,
              n_docs=n_docs,
          )
  
      return RetrievAugLMMarginOutput(
          loss=loss,
          logits=outputs.logits,
          doc_scores=outputs.doc_scores,
          past_key_values=outputs.past_key_values,
          context_input_ids=outputs.context_input_ids,
          context_attention_mask=outputs.context_attention_mask,
          retrieved_doc_embeds=outputs.retrieved_doc_embeds,
          retrieved_doc_ids=outputs.retrieved_doc_ids,
          question_encoder_last_hidden_state=outputs.question_encoder_last_hidden_state,
          question_enc_hidden_states=outputs.question_enc_hidden_states,
          question_enc_attentions=outputs.question_enc_attentions,
          generator_enc_last_hidden_state=outputs.generator_enc_last_hidden_state,
          generator_enc_hidden_states=outputs.generator_enc_hidden_states,
          generator_enc_attentions=outputs.generator_enc_attentions,
          generator_dec_hidden_states=outputs.generator_dec_hidden_states,
          generator_dec_attentions=outputs.generator_dec_attentions,
          generator_cross_attentions=outputs.generator_cross_attentions,
      )
  
imports: import copy
  from dataclasses import dataclass
  from typing import Callable, List, Optional, Tuple, Union
  import torch
  from torch import nn
  from ...configuration_utils import PretrainedConfig
  from ...generation import BeamSearchScorer, GenerationConfig, LogitsProcessorList, StoppingCriteriaList
  from ...modeling_outputs import ModelOutput
  from ...modeling_utils import PreTrainedModel
  from ...utils import 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 4 for calling the RagSequenceForGeneration obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, encoder_outputs, decoder_input_ids, decoder_attention_mask, past_key_values, context_input_ids, context_attention_mask, doc_scores, use_cache, output_attentions, output_hidden_states, output_retrieved, exclude_bos_score, reduce_loss, labels, n_docs) and returns: RetrievAugLMMarginOutput