code: def extract_vit_weights(
      output_file,
      model_dir,
      head_num,
      image_size,
      patch_size,
  ):
      # load var names
      encoder_state_dict = torch.load(model_dir, "cpu")
      enc_var_name_list = list(encoder_state_dict.keys())
  
      # initialize output file
      print("Saving model to hdf5...")
      print("Writing to {0}".format(output_file))
      hdf5_file = h5py.File(output_file, "w")
  
      ln = encoder_state_dict["vit.layernorm.weight"]
      emb_dim = ln.shape[0]
      layer_nums = 0
      for name in enc_var_name_list:
          if name.endswith("para"):
              layer_nums += 1
  
      export_ls_quant_encoder(
          hdf5_file, encoder_state_dict, emb_dim, emb_dim * 4, False
      )
  
      # fill src_embedding
      fill_quant_hdf5_layer(
          enc_var_name_list,
          encoder_state_dict,
          hdf5_file,
          "src_embedding/",
          src_emb_mapping_dict,
          layer_nums,
      )
  
      # save number of layers metadata
      hdf5_file.create_dataset(
          "model_conf/n_encoder_stack", data=layer_nums, dtype="i4"
      )
      # fill in model_conf
      hdf5_file.create_dataset(
          "model_conf/head_num", data=head_num, dtype="i4"
      )
      hdf5_file.create_dataset(
          "model_conf/use_gelu", data=True, dtype="?"
      )
      hdf5_file.create_dataset(
          "model_conf/is_post_ln", data=False, dtype="?"
      )
      hdf5_file.create_dataset(
          "model_conf/image_size", data=image_size, dtype="i4"
      )
      hdf5_file.create_dataset(
          "model_conf/patch_size", data=patch_size, dtype="i4"
      )
  
      hdf5_file.close()
      # read-in again to double check
      hdf5_file = h5py.File(output_file, "r")
  
      def _print_pair(key, value):
          value = value[()]
          print(f"{key}: {value}")
  
      list(
          map(
              lambda x: _print_pair(*x), hdf5_file["model_conf"].items()
          )
      )
  
imports: import os
  import h5py
  from collections import OrderedDict
  import torch
  from lightseq.training.ops.pytorch.export_quant import export_ls_quant_encoder, fill_quant_hdf5_layer, quantize
  from export.util import parse_args
label_desc: Use image node_id 1 for calling a global function with example usage: extract_vit_weights(output_file, model_dir, head_num, image_size, patch_size) without return types