code: def convert_gptq2ggml(model_path, output_path, tokenizer_path=None):
      input_path = find_quantized_model_file(model_path)
  
      if input_path.endswith("pt"):
          model = torch.load(input_path, map_location="cpu")
      elif input_path.endswith("safetensors"):
          from safetensors.torch import load_file
  
          model = load_file(input_path)
      else:
          invalidInputError(
              False,
              "unknown input model path, only support .safetensors or .pt file.",
          )
  
      n_vocab, n_embd = model["model.embed_tokens.weight"].shape
      layer_re = r"model\.layers\.([0-9]+)"
      n_layer = 1 + max(
          int(re.match(layer_re, name).group(1))
          for name in model
          if re.match(layer_re, name)
      )
  
      # hardcoded:
      n_mult = 256
      n_head = {32: 32, 40: 40, 60: 52, 80: 64}[n_layer]
  
      if not tokenizer_path:
          tokenizer_path = os.path.join(model_path, "tokenizer.model")
          invalidInputError(
              os.path.isfile(tokenizer_path),
              f"tokenizer.model was not found under {tokenizer_path}."
              f"Please specify the tokenizer-path",
          )
  
      tokenizer = SentencePieceProcessor(tokenizer_path)
      vocab_size = tokenizer.vocab_size()
      # TODO: Support AutoTokenizer
      # from transformers import AutoTokenizer
      # tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
      # vocab_size = tokenizer.vocab_size
  
      invalidInputError(vocab_size <= n_vocab, "vocab size not match.")
  
      fout = open(output_path, "wb")
  
      fout.write(b"ggjt"[::-1])  # magic: ggmf in hex
      values = [
          3,  # file version
          n_vocab,
          n_embd,
          n_mult,
          n_head,
          n_layer,
          n_embd // n_head,  # rot (obsolete)
          4,
      ]
      fout.write(struct.pack("i" * len(values), *values))
  
      # This loop unchanged from convert-pth-to-ggml.py:
      for i in range(vocab_size):
          if tokenizer.is_unknown(i):
              text = " \u2047 ".encode("utf-8")
          elif tokenizer.is_control(i):
              text = b""
          elif tokenizer.is_byte(i):
              piece = tokenizer.id_to_piece(i)
              if len(piece) != 6:
                  print(f"Invalid token: {piece}")
                  sys.exit(1)
              byte_value = int(piece[3:-1], 16)
              text = struct.pack("B", byte_value)
          else:
              text = (
                  tokenizer.id_to_piece(i)
                  .replace("\u2581", " ")
                  .encode("utf-8")
              )
          fout.write(struct.pack("i", len(text)))
          fout.write(text)
          fout.write(struct.pack("f", tokenizer.get_score(i)))
  
      convert_non_q4(
          "model.embed_tokens.weight",
          "tok_embeddings.weight",
          model,
          fout,
      )
      convert_non_q4("model.norm.weight", "norm.weight", model, fout)
      convert_non_q4("lm_head.weight", "output.weight", model, fout)
  
      for i in range(n_layer):
          convert_q4(
              f"model.layers.{i}.self_attn.q_proj",
              f"layers.{i}.attention.wq.weight",
              model,
              fout,
              n_head,
              permute=True,
          )
          convert_q4(
              f"model.layers.{i}.self_attn.k_proj",
              f"layers.{i}.attention.wk.weight",
              model,
              fout,
              n_head,
              permute=True,
          )
          convert_q4(
              f"model.layers.{i}.self_attn.v_proj",
              f"layers.{i}.attention.wv.weight",
              model,
              fout,
              n_head,
          )
          convert_q4(
              f"model.layers.{i}.self_attn.o_proj",
              f"layers.{i}.attention.wo.weight",
              model,
              fout,
              n_head,
          )
          convert_q4(
              f"model.layers.{i}.mlp.gate_proj",
              f"layers.{i}.feed_forward.w1.weight",
              model,
              fout,
              n_head,
          )
          convert_q4(
              f"model.layers.{i}.mlp.down_proj",
              f"layers.{i}.feed_forward.w2.weight",
              model,
              fout,
              n_head,
          )
          convert_q4(
              f"model.layers.{i}.mlp.up_proj",
              f"layers.{i}.feed_forward.w3.weight",
              model,
              fout,
              n_head,
          )
  
          convert_non_q4(
              f"model.layers.{i}.input_layernorm.weight",
              f"layers.{i}.attention_norm.weight",
              model,
              fout,
          )
          convert_non_q4(
              f"model.layers.{i}.post_attention_layernorm.weight",
              f"layers.{i}.ffn_norm.weight",
              model,
              fout,
          )
      fout.close()
      print("Done. Output file: " + output_path)
      print("")
  
file_path: BigDL/python/llm/src/bigdl/llm/gptq/convert/convert_gptq_to_ggml.py
imports: import os
  import re
  import sys
  import json
  import warnings
  import struct
  import numpy
  import torch
  from sentencepiece import SentencePieceProcessor
  from pathlib import Path
  from bigdl.llm.utils.common.log4Error import invalidInputError
label_desc: Use image node_id 8 for calling a global function with example usage: convert_gptq2ggml(model_path, output_path, tokenizer_path) without return types