code: def _upgrade_pytorch_state_dict(state_dict, args, model):
      # remove lightseq keys
      remove_suffix = [
          "_amax",
          "para",
          "embeddings",
          "decoder.output_projection.weight",
      ]
      for k in list(state_dict.keys()):
          for suf in remove_suffix:
              if k.endswith(suf):
                  del state_dict[k]
  
      # update state dict of embeddings
      embed_map_dict = {
          "encoder.embed_tokens.emb_lookup.weight": "encoder.embed_tokens.weight",
          "encoder.embed_tokens.embed_positions.weight": "encoder.embed_positions.weight",
          "encoder.embed_tokens.layernorm_embedding.weight": "encoder.layernorm_embedding.weight",
          "encoder.embed_tokens.layernorm_embedding.bias": "encoder.layernorm_embedding.bias",
          "decoder.embed_tokens.emb_lookup.weight": "decoder.embed_tokens.weight",
          "decoder.embed_tokens.embed_positions.weight": "decoder.embed_positions.weight",
          "decoder.embed_tokens.layernorm_embedding.weight": "decoder.layernorm_embedding.weight",
          "decoder.embed_tokens.layernorm_embedding.bias": "decoder.layernorm_embedding.bias",
      }
      for k, v in embed_map_dict.items():
          rename_key(state_dict, k, v)
  
      # update state dict of encoder
      for lid in range(args.encoder_layers):
          prefix = f"encoder.layers.{lid}."
          w, b = model.encoder.layers[lid].params_dict()
          for k, v in w.items():
              state_dict[prefix + k + ".weight"] = v
          for k, v in b.items():
              state_dict[prefix + k + ".bias"] = v
  
      # update state dict of decoder
      skip_k = ["encoder_attn.k_proj", "encoder_attn.v_proj"]
      enc_attn_kv = {}
      for lid in range(args.decoder_layers):
          prefix = f"decoder.layers.{lid}."
          w, b = model.decoder.layers[lid].params_dict()
          for k, v in w.items():
              if k not in skip_k:
                  state_dict[prefix + k + ".weight"] = v
          for k, v in b.items():
              if k not in skip_k:
                  state_dict[prefix + k + ".bias"] = v
          if lid == 0:
              enc_attn_kv["encoder_attn.k_proj.weight"] = w[
                  "encoder_attn.k_proj"
              ]
              enc_attn_kv["encoder_attn.k_proj.bias"] = b[
                  "encoder_attn.k_proj"
              ]
              enc_attn_kv["encoder_attn.v_proj.weight"] = w[
                  "encoder_attn.v_proj"
              ]
              enc_attn_kv["encoder_attn.v_proj.bias"] = b[
                  "encoder_attn.v_proj"
              ]
          for k, v in enc_attn_kv.items():
              state_dict[prefix + k] = v[lid]
  
file_path: lightseq/examples/training/fairseq/ls_finetune_bart/convert_lightseq_to_huggingface.py
imports: import argparse
  from pathlib import Path
  import fairseq
  import torch
  from packaging import version
  from torch import nn
  from transformers import BartConfig, BartForConditionalGeneration, BartForSequenceClassification, BartModel, BartTokenizer
  from lightseq.training.cli.fs_modules import LSBARTModel
  from fairseq.models.bart import BARTModel
label_desc: Use image node_id 6 for calling a global function with example usage: _upgrade_pytorch_state_dict(state_dict, args, model) without return types