code: def main(
      saved_dir,
      model: GPT2Model,
      trained_gpu_num=1,
      infer_gpu_num=1,
      processes=1,
      weight_data_type="fp32",
  ):
      assert isinstance(model, GPT2Model), "model must be GPT2Model"
      args = None
      saved_dir = saved_dir + "/%d-gpu/" % infer_gpu_num
  
      if not os.path.exists(saved_dir):
          os.makedirs(saved_dir)
      # ckpt_name = args.in_file
  
      t_gpu_num = trained_gpu_num
      i_gpu_num = infer_gpu_num
      assert i_gpu_num % t_gpu_num == 0
  
      factor = (int)(i_gpu_num / t_gpu_num)
  
      # load position_embedding from rank 0
      # torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
      # model = GPT2Model.from_pretrained(args.in_file).to(torch_device)
  
      hf_config = vars(model.config)
  
      # NOTE: save parameters to config files (loaded by triton backends)
      config = configparser.ConfigParser()
      config["gpt"] = {}
      try:
          config["gpt"]["model_name"] = (
              "gpt"
              if hf_config["_name_or_path"] == ""
              else hf_config["_name_or_path"]
          )
          config["gpt"]["head_num"] = str(hf_config["n_head"])
          n_embd = hf_config["n_embd"]
          config["gpt"]["size_per_head"] = str(
              n_embd // hf_config["n_head"]
          )
          config["gpt"]["inter_size"] = str(n_embd * 4)
          config["gpt"]["max_pos_seq_len"] = str(
              hf_config["n_positions"]
          )
          config["gpt"]["num_layer"] = str(hf_config["n_layer"])
          config["gpt"]["vocab_size"] = str(hf_config["vocab_size"])
          config["gpt"]["start_id"] = str(hf_config["bos_token_id"])
          config["gpt"]["end_id"] = str(hf_config["eos_token_id"])
          config["gpt"]["weight_data_type"] = weight_data_type
          with open(saved_dir + "/config.ini", "w") as configfile:
              config.write(configfile)
      except:  # noqa: E722
          logger.warning("Fail to save the config in config.ini.")
  
      np_weight_data_type = get_weight_data_type(weight_data_type)
  
      huggingface_model_name_pattern = [
          "ln_1.bias",
          "ln_1.weight",
          "attn.c_attn.bias",
          "attn.c_attn.weight",
          "attn.c_proj.bias",
          "attn.c_proj.weight",
          "ln_2.bias",
          "ln_2.weight",
          "mlp.c_fc.bias",
          "mlp.c_fc.weight",
          "mlp.c_proj.bias",
          "mlp.c_proj.weight",
      ]
  
      ft_model_name_pattern = [
          "input_layernorm.bias",
          "input_layernorm.weight",
          "attention.query_key_value.bias",
          "attention.query_key_value.weight",
          "attention.dense.bias",
          "attention.dense.weight",
          "post_attention_layernorm.bias",
          "post_attention_layernorm.weight",
          "mlp.dense_h_to_4h.bias",
          "mlp.dense_h_to_4h.weight",
          "mlp.dense_4h_to_h.bias",
          "mlp.dense_4h_to_h.weight",
      ]
  
      # torch.multiprocessing.set_start_method("spawn")
      # torch.multiprocessing.set_sharing_strategy("file_system")
      # pool = multiprocessing.Pool(args.processes)
      for name, param in model.named_parameters():
          if name.find("weight") == -1 and name.find("bias") == -1:
              continue
          if name == "wpe.weight":
              param.detach().cpu().numpy().astype(
                  np_weight_data_type
              ).tofile(saved_dir + "model.wpe.bin")
          elif name == "wte.weight":
              param.detach().cpu().numpy().astype(
                  np_weight_data_type
              ).tofile(saved_dir + "model.wte.bin")
          elif name == "ln_f.bias":
              param.detach().cpu().numpy().astype(
                  np_weight_data_type
              ).tofile(saved_dir + "model.final_layernorm.bias.bin")
          elif name == "ln_f.weight":
              param.detach().cpu().numpy().astype(
                  np_weight_data_type
              ).tofile(saved_dir + "model.final_layernorm.weight.bin")
          elif name == "lm_head.weight":
              param.detach().cpu().numpy().astype(
                  np_weight_data_type
              ).tofile(saved_dir + "model.lm_head.weight.bin")
          else:
              for i in range(len(huggingface_model_name_pattern)):
                  if name.find(huggingface_model_name_pattern[i]) != -1:
                      new_name = name.replace("h.", "layers.").replace(
                          huggingface_model_name_pattern[i],
                          ft_model_name_pattern[i],
                      )
                      # pool.starmap(split_and_convert_process,
                      # [(0, saved_dir, factor, new_name, args,
                      # param.detach().cpu().numpy().astype(np_weight_data_type))],
                      # )
                      split_and_convert_process(
                          0,
                          saved_dir,
                          factor,
                          new_name,
                          args,
                          param.detach()
                          .cpu()
                          .numpy()
                          .astype(np_weight_data_type),
                      )
  
file_path: nebuly/optimization/nebullvm/nebullvm/operations/optimizations/compilers/faster_transformer/gpt/utils/huggingface_gpt_convert.py
imports: import argparse
  import configparser
  import os
  import sys
  from loguru import logger
  import numpy
  from transformers import GPT2Model
  from nebullvm.optional_modules.torch import torch
label_desc: Use image node_id 4 for calling a global function with example usage: main(saved_dir, model, trained_gpu_num, infer_gpu_num, processes, weight_data_type) without return types