docstr: Compute RAG Token Loss. Reimplement to get the loss tensor (reduction = 'none')
  
  This is a simple NLL Loss.
  
  :param criterion:
      presumably the NLL criterion.
  :param scores:
      model scores
  :param preds:
      model "predicions" of tokens
  :param enc_state:
      encoder states
  :param label_vec:
      target tokens
  
  :return (losses, metric_loss, correct_tokens, target_tokens):
      loss: the loss through which we backpropagate
      metric_loss: loss we use for metrics
      correct_tokens: correct predictions from the model
      target_tokens: the ground truth tokens.
docstr_tok: ["Compute","RAG","Token","Loss",".","Reimplement","to","get","the","loss","tensor","(","reduction","=","'none","'",")","This","is","a","simple","NLL","Loss",".",":","param","criterion",":","presumably","the","NLL","criterion",".",":","param","scores",":","model","scores",":","param","preds",":","model","``","predicions","''","of","tokens",":","param","enc_state",":","encoder","states",":","param","label_vec",":","target","tokens",":","return","(","losses",",","metric_loss",",","correct_tokens",",","target_tokens",")",":","loss",":","the","loss","through","which","we","backpropagate","metric_loss",":","loss","we","use","for","metrics","correct_tokens",":","correct","predictions","from","the","model","target_tokens",":","the","ground","truth","tokens","."]
code: def compute_rag_token_loss(
      self,
      criterion: torch.nn.Module,
      scores: torch.Tensor,
      preds: torch.LongTensor,
      enc_state: Tuple[Any],
      label_vec: torch.LongTensor,
  ) -> Tuple[torch.Tensor, List[int], torch.Tensor, torch.Tensor]:
      """
      Compute RAG Token Loss. Reimplement to get the loss tensor (reduction = 'none')
  
      This is a simple NLL Loss.
  
      :param criterion:
          presumably the NLL criterion.
      :param scores:
          model scores
      :param preds:
          model "predicions" of tokens
      :param enc_state:
          encoder states
      :param label_vec:
          target tokens
  
      :return (losses, metric_loss, correct_tokens, target_tokens):
          loss: the loss through which we backpropagate
          metric_loss: loss we use for metrics
          correct_tokens: correct predictions from the model
          target_tokens: the ground truth tokens.
      """
      if scores.size(1) != label_vec.size(1):
          assert self._rag_model_interface.generation_model == "bart"
          # ignore start
          scores = scores[:, 1:, :]
          preds = preds[:, 1:]  # type: ignore
  
      # compute loss
      score_view = scores.reshape(-1, scores.size(-1))
      losses = criterion(score_view, label_vec.view(-1))
      losses = losses.view(scores.shape[:-1]).sum(dim=1)
  
      # calculate metric counters
      metric_loss = losses.tolist()
      notnull = label_vec.ne(self._rag_model_interface.null_idx)
      target_tokens = notnull.long().sum(dim=-1)
      correct = ((label_vec == preds) * notnull).sum(dim=-1)
  
      # loss = losses.sum()
      # loss /= target_tokens.sum()  # average loss per token
      return losses, metric_loss, correct, target_tokens
  
imports: import torch
  import torch.nn
  import torch.nn.functional
  from typing import Optional, Any, Dict, Tuple, Union, List
  from abc import abstractmethod
  from parlai.agents.transformer.modules import TransformerDecoderLayer
  from parlai.agents.fid.fid import FidAgent, FidModel
  from parlai.core.message import Message
  from parlai.core.metrics import AverageMetric
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.torch_generator_agent import PPLMetric
  from parlai.core.torch_agent import Batch, DictionaryAgent
  import parlai.utils.logging
  from projects.blenderbot2.agents.blenderbot2 import BlenderBot2FidAgent, BlenderBot2FidModel, T5BlenderBot2FidModel
  from parlai.agents.fid.fid import WizIntGoldDocRetrieverFiDAgent
  import parlai.agents.transformer.modules.ffn
  from parlai.agents.transformer.modules import LAYER_NORM_EPS
label_desc: Use image node_id 14 for calling the DirectorFidAgent obj's underlying member method code with example usage: obj.compute_rag_token_loss(criterion, scores, preds, enc_state, label_vec) and returns: losses,  metric_loss,  correct,  target_tokens