code: def save_bart_proto_to_hdf5(transformer: Transformer, f: h5py.File):
      """Convert bart protobuf to hdf5 format to support larger weight."""
      MODEL_CONF_KEYS = [
          # model_conf
          "head_num",
          "beam_size",
          "extra_decode_length",
          "length_penalty",
          "src_padding_id",
          "trg_start_id",
          "diverse_lambda",
          "sampling_method",
          "topp",
          "topk",
          "trg_end_id",
          "is_post_ln",
          "no_scale_embedding",
          "use_gelu",
          "is_multilingual",
      ]
  
      EMBEDDING_KEYS = [
          # src_embedding
          # trg_embedding
          "token_embedding",
          "position_embedding",
          "norm_scale",
          "norm_bias",
          "encode_output_project_kernel_kv",
          "encode_output_project_bias_kv",
          "shared_bias",
          "lang_emb",
          "trg_vocab_mask",
      ]
  
      ENCODER_LAYER_KEYS = [
          # encoder_stack/{i}
          "multihead_norm_scale",
          "multihead_norm_bias",
          "multihead_project_kernel_qkv",
          "multihead_project_bias_qkv",
          "multihead_project_kernel_output",
          "multihead_project_bias_output",
          "ffn_norm_scale",
          "ffn_norm_bias",
          "ffn_first_kernel",
          "ffn_first_bias",
          "ffn_second_kernel",
          "ffn_second_bias",
      ]
  
      DECODER_LAYER_KEYS = [
          # decoder_stack/{i}
          "self_norm_scale",
          "self_norm_bias",
          "self_project_kernel_qkv",
          "self_project_bias_qkv",
          "self_project_kernel_output",
          "self_project_bias_output",
          "encdec_norm_scale",
          "encdec_norm_bias",
          "encdec_project_kernel_q",
          "encdec_project_bias_q",
          "encdec_project_kernel_output",
          "encdec_project_bias_output",
          "ffn_norm_scale",
          "ffn_norm_bias",
          "ffn_first_kernel",
          "ffn_first_bias",
          "ffn_second_kernel",
          "ffn_second_bias",
      ]
      base_attr_to_keys = {
          "src_embedding": EMBEDDING_KEYS,
          "trg_embedding": EMBEDDING_KEYS,
          "model_conf": MODEL_CONF_KEYS,
      }
  
      print(f"start converting protobuf to hdf5 format.")
      # load src_embedding, trg_embedding, model_conf
      for base_attr, keys in base_attr_to_keys.items():
          for key in keys:
              hdf5_key = f"{base_attr}/{key}"
              proto_attr = f"{base_attr}.{key}"
  
              if key not in dir(attrgetter(base_attr)(transformer)):
                  print(f"key {key} not found in {base_attr}, skipping")
                  continue
  
              print(f"loading transformer {proto_attr} -> {hdf5_key}")
              _data = attrgetter(proto_attr)(transformer)
              if type(_data) is str:
                  print(
                      f"find type str, explicitly convert string to ascii encoded array."
                  )
                  # explict convert to array of char (int8) to avoid issues on string reading in C
                  _data = np.array([ord(c) for c in _data]).astype(
                      np.int8
                  )
              f.create_dataset(hdf5_key, data=_data)
  
      # save number of layers metadata
      f.create_dataset(
          "model_conf/n_encoder_stack",
          data=len(transformer.encoder_stack),
      )
      f.create_dataset(
          "model_conf/n_decoder_stack",
          data=len(transformer.decoder_stack),
      )
  
      # load encoder_stack
      for layer_id, layer in enumerate(transformer.encoder_stack):
          for key in ENCODER_LAYER_KEYS:
              hdf5_key = f"encoder_stack/{layer_id}/{key}"
              proto_attr = key
              print(
                  f"loading transformer.encoder_stack {proto_attr} -> {hdf5_key}"
              )
              f.create_dataset(
                  hdf5_key, data=attrgetter(proto_attr)(layer)
              )
  
      # load decoder_stack
      for layer_id, layer in enumerate(transformer.decoder_stack):
          for key in DECODER_LAYER_KEYS:
              hdf5_key = f"decoder_stack/{layer_id}/{key}"
              proto_attr = key
              print(
                  f"loading transformer.decoder_stack {proto_attr} -> {hdf5_key}"
              )
              f.create_dataset(
                  hdf5_key, data=attrgetter(proto_attr)(layer)
              )
  
      print(f"proto to hdf5 conversion completed.")
  
file_path: lightseq/examples/inference/python/export/huggingface/hf_bart_export.py
imports: import os
  from collections import OrderedDict
  import tensorflow
  import h5py
  import numpy
  from operator import attrgetter
  from lightseq.training.ops.pytorch.export import gather_token_embedding, fill_pb_layer
  from export.proto.transformer_pb2 import Transformer
  from transformers import BartForConditionalGeneration
  from export.util import parse_args
label_desc: Use image node_id 2 for calling a global function with example usage: save_bart_proto_to_hdf5(transformer, f) without return types