class_docstr: Script to convert a transformer-based fairseq model to ParlAI Model.
class_docstr_tok: ["Script","to","convert","a","transformer-based","fairseq","model","to","ParlAI","Model","."]
docstr: Load a checkpoint to CPU (with upgrading for backward compatibility).
  
  :return state:
      loaded fairseq state
docstr_tok: ["Load","a","checkpoint","to","CPU","(","with","upgrading","for","backward","compatibility",")",".",":","return","state",":","loaded","fairseq","state"]
code: def load_fairseq_checkpoint(self):
      """
      Load a checkpoint to CPU (with upgrading for backward compatibility).
  
      :return state:
          loaded fairseq state
      """
      paths: List[str] = self.opt["input"]
      if len(paths) == 1:
          # just a single checkpoint, no fancy merges necessary
          return self._load_single_fairseq_checkpoint(paths[0])
  
      # many checkpoints case
      # load all the checkpoints into memory
      pieces = [self._load_single_fairseq_checkpoint(p) for p in paths]
      # store the args
      output_sd = {"args": pieces[0]["args"]}
  
      # only need merge for the model parameters
      output_model = {}
      pieces = {
          k: [p["model"][k] for p in pieces]
          for k in pieces[0]["model"].keys()
      }
  
      for k, subpieces in pieces.items():
          if ".version" in k:
              continue
          elif "_float_tensor" in k:
              # doesn't matter for embed_positions
              output_model[k] = subpieces[0]
          elif "out_proj.weight" in k or "fc2.weight" in k:
              # row parallel weights
              output_model[k] = torch.cat(subpieces, dim=1)
          elif "out_proj.bias" in k or "fc2.bias" in k:
              # row parallel bias
              output_model[k] = subpieces[0]
          elif "_proj" in k or "fc1" in k:
              # column parallel
              output_model[k] = torch.cat(subpieces, dim=0)
          elif "_norm" in k:
              # for norms, use any worker's copy
              output_model[k] = subpieces[0]
          elif "embed_tokens" in k:
              output_model[k] = torch.cat(subpieces, dim=0)
          else:
              print(f"Could not handle {k}")
              __import__("ipdb").set_trace()  # FIXME
              print()
  
      output_sd["model"] = output_model
  
      return output_sd
  
imports: from collections import OrderedDict
  import os
  import torch
  from torch.serialization import default_restore_location
  from typing import Any, Dict, List
  from parlai.core.agents import create_agent
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.script import ParlaiScript
  from parlai.utils.io import PathManager
  from parlai.scripts.vacuum import Vacuum
label_desc: Use image node_id 6 for calling the ConversionScript obj's underlying member method code with example usage: obj.load_fairseq_checkpoint() and returns: output_sd, self