code: def interleave_fid_combo_outputs(
      enc_out_retrieval: torch.Tensor,
      enc_out_skip_retrieval: torch.Tensor,
      mask_retrieval: torch.Tensor,
      mask_skip_retrieval: torch.Tensor,
      skip_retrieval_vec: torch.Tensor,
      top_docs: List[List[Document]],
      top_doc_scores: torch.Tensor,
      right_padded: bool = True,
  ) -> Tuple[
      torch.Tensor, torch.BoolTensor, List[List[Document]], torch.Tensor
  ]:
      """
      Interleave FiD encoder outputs.
  
      Outputs are either encodings of documents/input,
      or encodings of just input.
  
      This operation is to preserve original batch order.
  
      :param enc_out_retrieval:
          encoder output for inputs with retrieved documents
      :param enc_out_skip_retrieval:
          encoder output for inputs without retrieved documents
      :param mask_retrieval:
          mask for inputs with retrieved documents
      :param mask_skip_retrieval:
          mask for inputs without retrieved documents
      :param skip_retrieval_vec:
          1D tensor indicating which inputs skip retrieval vs. dont
      :param top_docs:
          top documents for outputs with retrieved documents
      :param top_doc_scores:
          top doc scores.
      :param right_padded:
          whether the final output is right padded
  
      :return (new_out, new_mask, new_top_docs, new_top_doc_scores):
          return interleaved concatenation of inputs
      """
      bsz = enc_out_retrieval.size(0) + enc_out_skip_retrieval.size(0)
      dim = enc_out_retrieval.size(-1)
      seqlen = max(
          enc_out_retrieval.size(1), enc_out_skip_retrieval.size(1)
      )
      n_docs = top_doc_scores.size(1)
  
      # The following logic interleaves the results in new tensors.
      new_out = enc_out_retrieval.new(bsz, seqlen, dim).fill_(0)
      new_mask = mask_retrieval.new(bsz, seqlen).fill_(False)
      new_top_doc_scores = top_doc_scores.new(bsz, n_docs)
      new_top_docs = []
      retr_offset = 0
      skip_offset = 0
      for i, skip in enumerate(skip_retrieval_vec):
          if skip:
              vec = enc_out_skip_retrieval[skip_offset]
              mask = mask_skip_retrieval[skip_offset]
              scores = torch.zeros(n_docs).to(top_doc_scores)
              docs = [BLANK_DOC] * n_docs
              skip_offset += 1
          else:
              vec = enc_out_retrieval[retr_offset]
              mask = mask_retrieval[retr_offset]
              scores = top_doc_scores[retr_offset]
              docs = top_docs[retr_offset]
              retr_offset += 1
          if right_padded:
              new_out[i, : vec.size(0), :] = vec
              new_mask[i, : mask.size(0)] = mask
          else:
              new_out[i, -vec.size(0) :, :] = vec
              new_mask[i, -mask.size(0) :] = mask
  
          new_top_doc_scores[i] = scores
          new_top_docs.append(docs)
  
      return new_out, new_mask, new_top_docs, new_top_doc_scores
  
imports: import torch
  from typing import List, Tuple, Optional, Dict, Any
  from parlai.agents.fid.fid import FidModel
  from parlai.agents.rag.args import RetrieverType
  from parlai.agents.rag.retrievers import Document, BLANK_DOC, SearchQuerySearchEngineRetriever, RagRetriever, retriever_factory
  from parlai.core.dict import DictionaryAgent
  from parlai.core.opt import Opt
  import parlai.utils.logging
label_desc: Use image node_id 1 for calling a global function with example usage: interleave_fid_combo_outputs(enc_out_retrieval, enc_out_skip_retrieval, mask_retrieval, mask_skip_retrieval, skip_retrieval_vec, top_docs, top_doc_scores, right_padded) and returns: new_out,  new_mask,  new_top_docs,  new_top_doc_scores