code: def upgrade_state_dict_named(self, state_dict, name):
      self.upgrade_fairseq_state_dict_named(state_dict)
      if "encoder.version" not in state_dict:
          return
  
      def truncate_emb(key):
          if key in state_dict:
              state_dict[key] = state_dict[key][:-1, :]
  
      # When finetuning on translation task, remove last row of
      # embedding matrix that corresponds to mask_idx token.
      loaded_dict_size = state_dict["encoder.embed_tokens.weight"].size(
          0
      )
      if (
          loaded_dict_size == len(self.encoder.dictionary) + 1
          and "<mask>" not in self.encoder.dictionary
      ):
          truncate_emb("encoder.embed_tokens.weight")
          truncate_emb("decoder.embed_tokens.weight")
          truncate_emb("encoder.output_projection.weight")
          truncate_emb("decoder.output_projection.weight")
  
      # convert encoder and decoder from fairseq to lightseq
      def get_weight(key):
          return state_dict.pop(key).view(-1)
  
      enc_state_list = [
          "self_attn.q_proj.weight",
          "self_attn.k_proj.weight",
          "self_attn.v_proj.weight",
          "self_attn.q_proj.bias",
          "self_attn.k_proj.bias",
          "self_attn.v_proj.bias",
          "self_attn.out_proj.weight",
          "self_attn.out_proj.bias",
          "self_attn_layer_norm.weight",
          "self_attn_layer_norm.bias",
          "fc1.weight",
          "fc1.bias",
          "fc2.weight",
          "fc2.bias",
          "final_layer_norm.weight",
          "final_layer_norm.bias",
      ]
      dec_state_list = [
          "self_attn.q_proj.weight",
          "self_attn.k_proj.weight",
          "self_attn.v_proj.weight",
          "self_attn.q_proj.bias",
          "self_attn.k_proj.bias",
          "self_attn.v_proj.bias",
          "self_attn.out_proj.weight",
          "self_attn.out_proj.bias",
          "self_attn_layer_norm.weight",
          "self_attn_layer_norm.bias",
          "encoder_attn.q_proj.weight",
          "encoder_attn.q_proj.bias",
          "encoder_attn.out_proj.weight",
          "encoder_attn.out_proj.bias",
          "encoder_attn_layer_norm.weight",
          "encoder_attn_layer_norm.bias",
          "fc1.weight",
          "fc1.bias",
          "fc2.weight",
          "fc2.bias",
          "final_layer_norm.weight",
          "final_layer_norm.bias",
      ]
  
      # enc_state_list, dec_state_list = encoder_list, decoder_list
      for lid in range(self.args.encoder_layers):
          prefix = f"encoder.layers.{lid}."
          para = torch.cat(
              [get_weight(prefix + k) for k in enc_state_list]
          )
          state_dict[prefix + "para"] = para
  
      kv = {
          ".weight": [],
          ".bias": [],
      }
      for lid in range(self.args.decoder_layers):
          prefix = f"decoder.layers.{lid}.encoder_attn."
          for suffix in [".weight", ".bias"]:
              kv[suffix].append(get_weight(prefix + "k_proj" + suffix))
              kv[suffix].append(get_weight(prefix + "v_proj" + suffix))
  
      for lid in range(1, self.args.decoder_layers):
          prefix = f"decoder.layers.{lid}."
          para = torch.cat(
              [get_weight(prefix + k) for k in dec_state_list]
          )
          state_dict[prefix + "para"] = para
  
      dec_layer0_para = []
      for k in dec_state_list:
          dec_layer0_para.append(get_weight("decoder.layers.0." + k))
      dec_layer0_para.extend(kv[".weight"])
      dec_layer0_para.extend(kv[".bias"])
      state_dict["decoder.layers.0.para"] = torch.cat(dec_layer0_para)
  
      embed_map_dict = {
          "encoder.embed_tokens.weight": "encoder.embed_tokens.emb_lookup.weight",
          "encoder.embed_positions.weight": "encoder.embed_tokens.embed_positions.weight",
          "encoder.layernorm_embedding.weight": "encoder.embed_tokens.layernorm_embedding.weight",
          "encoder.layernorm_embedding.bias": "encoder.embed_tokens.layernorm_embedding.bias",
          "decoder.embed_tokens.weight": "decoder.embed_tokens.emb_lookup.weight",
          "decoder.embed_positions.weight": "decoder.embed_tokens.embed_positions.weight",
          "decoder.layernorm_embedding.weight": "decoder.embed_tokens.layernorm_embedding.weight",
          "decoder.layernorm_embedding.bias": "decoder.embed_tokens.layernorm_embedding.bias",
      }
  
      def rename_key(old, new):
          val = state_dict.pop(old)
          state_dict[new] = val
  
      for k, v in embed_map_dict.items():
          rename_key(k, v)
  
      # remove ignore keys
      ignore_keys = [
          "encoder.version",
          "decoder.version",
          "model.encoder.version",
          "model.decoder.version",
          "_float_tensor",
      ]
      for k in ignore_keys:
          state_dict.pop(k, None)
  
      # add new state
      embedding_weight = state_dict[
          "encoder.embed_tokens.emb_lookup.weight"
      ]
      state_dict["encoder.embed_tokens.embeddings"] = embedding_weight
      state_dict["encoder.embed_tokens.emb_quant._amax"] = torch.tensor(
          1.0
      )
      state_dict["decoder.embed_tokens.embeddings"] = embedding_weight
      state_dict["decoder.embed_tokens.emb_quant._amax"] = torch.tensor(
          1.0
      )
      if "decoder.output_projection.weight" not in state_dict:
          state_dict[
              "decoder.output_projection.weight"
          ] = embedding_weight
  
imports: import logging
  from typing import Optional
  import torch
  import torch.nn
  from fairseq import utils
  from fairseq.models import register_model, register_model_architecture
  from fairseq.models.transformer import TransformerModel
  from fairseq.models.bart import BARTHubInterface, BARTModel
  from .ls_transformer import LSTransformerModel
label_desc: Use image node_id 7 for calling the LSBARTModel obj's underlying member method code with example usage: obj.upgrade_state_dict_named(state_dict, name) and returns: state_dict