code: def export_native_fs_transformer(
      model_path,
      pb_path,
      hdf5_path,
      hdf5,
      max_step=300,
      bos_id=2,
      eos_id=2,
      pad_id=1,
  ):
      transformer = Transformer()
      # load var names
      reloaded = torch.load(model_path, "cpu")
      args = reloaded["args"]
      model_dict = reloaded["model"]
  
      trg_emb_mapping_dict["shared_bias"] = (
          "expression_np.zeros(%d)"
          % model_dict["decoder.embed_tokens.weight"].numpy().shape[0]
      )
  
      encoder_state_dict = {}
      decoder_state_dict = {}
      for k in model_dict:
          if k.startswith("encoder."):
              encoder_state_dict[k] = model_dict[k]
          if k.startswith("decoder."):
              decoder_state_dict[k] = model_dict[k]
  
      dec_var_name_list = list(decoder_state_dict.keys())
      enc_var_name_list = list(encoder_state_dict.keys())
  
      enc_tensor_names = {}
      for name in enc_var_name_list:
          name_split = name.split(".")
          if len(name_split) <= 2 or not name_split[2].isdigit():
              continue
          layer_id = int(name_split[2])
          enc_tensor_names.setdefault(layer_id, []).append(name)
  
      for layer_id in sorted(enc_tensor_names.keys()):
          fill_pb_layer(
              enc_tensor_names[layer_id],
              encoder_state_dict,
              transformer.encoder_stack.add(),
              enc_layer_mapping_dict,
          )
  
      # fill each decoder layer's params
      dec_tensor_names = {}
      for name in dec_var_name_list:
          name_split = name.split(".")
          if len(name_split) <= 2 or not name.split(".")[2].isdigit():
              continue
          layer_id = int(name.split(".")[2])
          dec_tensor_names.setdefault(layer_id, []).append(name)
  
      for layer_id in sorted(dec_tensor_names.keys()):
          fill_pb_layer(
              dec_tensor_names[layer_id],
              decoder_state_dict,
              transformer.decoder_stack.add(),
              dec_layer_mapping_dict,
          )
  
      fill_pb_layer(
          enc_var_name_list,
          encoder_state_dict,
          transformer.src_embedding,
          src_emb_mapping_dict,
      )
      # encoder token embedding
      src_tb, _ = gather_token_embedding(
          enc_var_name_list, encoder_state_dict, "embed_tokens"
      )
      transformer.src_embedding.token_embedding[
          :
      ] = src_tb.flatten().tolist()
      # encoder position embedding
      pos_emb = None
      if "encoder.embed_positions.weight" in encoder_state_dict:
          pos_emb = encoder_state_dict[
              "encoder.embed_positions.weight"
          ].numpy()
          transformer.src_embedding.position_embedding[
              :
          ] = pos_emb.flatten().tolist()
      else:
          pos_emb = get_pos_embedding(
              max_step + pad_id + 1, src_tb.shape[-1]
          ).numpy()
          pos_emb_list = (
              pos_emb[pad_id + 1 : max_step + pad_id + 1, :]
              .reshape([-1])
              .tolist()
          )
          transformer.src_embedding.position_embedding[:] = pos_emb_list
  
      print(
          "encoder.embed_positions.weight -> src_embedding.position_embedding, shape: {}, conversion finished!".format(
              pos_emb.shape
          )
      )
  
      # fill trg_embedding
      encode_output_mapping_dict = _get_encode_output_mapping_dict(
          len(dec_tensor_names)
      )
      trg_emb_mapping_dict.update(encode_output_mapping_dict)
      fill_pb_layer(
          dec_var_name_list,
          decoder_state_dict,
          transformer.trg_embedding,
          trg_emb_mapping_dict,
      )
      # decoder token embedding
      trg_tb, _ = gather_token_embedding(
          dec_var_name_list, decoder_state_dict, "embed_tokens"
      )
      transformer.trg_embedding.token_embedding[:] = (
          trg_tb.transpose().flatten().tolist()
      )
      print(
          "token_embedding.weight -> trg_embedding.token_embedding, shape: {}, conversion finished!".format(
              trg_tb.transpose().shape
          )
      )
      # decoder position embedding
      pos_emb = None
      if "decoder.embed_positions.weight" in decoder_state_dict:
          pos_emb = decoder_state_dict[
              "decoder.embed_positions.weight"
          ].numpy()
          transformer.trg_embedding.position_embedding[
              :
          ] = pos_emb.flatten().tolist()
      else:
          pos_emb = get_pos_embedding(
              max_step + pad_id + 1, trg_tb.shape[-1]
          ).numpy()
          pos_emb_list = (
              pos_emb[pad_id + 1 : max_step + pad_id + 1, :]
              .reshape([-1])
              .tolist()
          )
          transformer.trg_embedding.position_embedding[:] = pos_emb_list
  
      print(
          "decoder.embed_positions.weight -> trg_embedding.position_embedding, shape: {}, conversion finished!".format(
              pos_emb.shape
          )
      )
  
      # fill in conf
      export_ls_config(
          transformer,
          args.encoder_attention_heads,
          pad_id,
          bos_id,
          eos_id,
          args.encoder_layers,
          args.decoder_layers,
          save_pb=True,
      )
  
      save_path = save_model(transformer, pb_path, hdf5_path, hdf5)
      return save_path
  
file_path: lightseq/examples/inference/python/export/fairseq/native_fs_transformer_export.py
imports: from collections import OrderedDict
  import torch
  from export.proto.transformer_pb2 import Transformer
  from lightseq.training.ops.pytorch.export import gather_token_embedding, fill_pb_layer, export_ls_config
  from lightseq.training.ops.pytorch.util import get_pos_embedding
  import lightseq.inference
  from export.util import parse_args, save_model
label_desc: Use image node_id 2 for calling a global function with example usage: export_native_fs_transformer(model_path, pb_path, hdf5_path, hdf5, max_step, bos_id, eos_id, pad_id) and returns: save_path