Skip to content

Commit 7d5f184

Browse files
examples : add easy python script to create quantized (k-bit support) GGML models from local HF Transformer models (ggml-org#2311)
* Resync my fork with new llama.cpp commits * examples : rename to use dash instead of underscore --------- Co-authored-by: Georgi Gerganov <[email protected]>
1 parent d924522 commit 7d5f184

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

examples/make-ggml.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
This script converts Hugging Face llama models to GGML and quantizes them.
3+
4+
Usage:
5+
python make-ggml.py --model {model_dir_or_hf_repo_name} [--outname {output_name} (Optional)] [--outdir {output_directory} (Optional)] [--quants {quant_types} (Optional)] [--keep_fp16 (Optional)]
6+
7+
Arguments:
8+
- --model: (Required) The directory of the downloaded Hugging Face model or the name of the Hugging Face model repository. If the model directory does not exist, it will be downloaded from the Hugging Face model hub.
9+
- --outname: (Optional) The name of the output model. If not specified, the last part of the model directory path or the Hugging Face model repo name will be used.
10+
- --outdir: (Optional) The directory where the output model(s) will be stored. If not specified, '../models/{outname}' will be used.
11+
- --quants: (Optional) The types of quantization to apply. This should be a space-separated list. The default is 'Q4_K_M Q5_K_S'.
12+
- --keep_fp16: (Optional) If specified, the FP16 model will not be deleted after the quantized models are created.
13+
14+
Quant types:
15+
- Q4_0: small, very high quality loss - legacy, prefer using Q3_K_M
16+
- Q4_1: small, substantial quality loss - legacy, prefer using Q3_K_L
17+
- Q5_0: medium, balanced quality - legacy, prefer using Q4_K_M
18+
- Q5_1: medium, low quality loss - legacy, prefer using Q5_K_M
19+
- Q2_K: smallest, extreme quality loss - not recommended
20+
- Q3_K: alias for Q3_K_M
21+
- Q3_K_S: very small, very high quality loss
22+
- Q3_K_M: very small, very high quality loss
23+
- Q3_K_L: small, substantial quality loss
24+
- Q4_K: alias for Q4_K_M
25+
- Q4_K_S: small, significant quality loss
26+
- Q4_K_M: medium, balanced quality - recommended
27+
- Q5_K: alias for Q5_K_M
28+
- Q5_K_S: large, low quality loss - recommended
29+
- Q5_K_M: large, very low quality loss - recommended
30+
- Q6_K: very large, extremely low quality loss
31+
- Q8_0: very large, extremely low quality loss - not recommended
32+
- F16: extremely large, virtually no quality loss - not recommended
33+
- F32: absolutely huge, lossless - not recommended
34+
"""
35+
import subprocess
36+
subprocess.run(f"pip install huggingface-hub==0.16.4", shell=True, check=True)
37+
38+
import argparse
39+
import os
40+
from huggingface_hub import snapshot_download
41+
42+
def main(model, outname, outdir, quants, keep_fp16):
43+
ggml_version = "v3"
44+
45+
if not os.path.isdir(model):
46+
print(f"Model not found at {model}. Downloading...")
47+
try:
48+
if outname is None:
49+
outname = model.split('/')[-1]
50+
model = snapshot_download(repo_id=model, cache_dir='../models/hf_cache')
51+
except Exception as e:
52+
raise Exception(f"Could not download the model: {e}")
53+
54+
if outdir is None:
55+
outdir = f'../models/{outname}'
56+
57+
if not os.path.isfile(f"{model}/config.json"):
58+
raise Exception(f"Could not find config.json in {model}")
59+
60+
os.makedirs(outdir, exist_ok=True)
61+
62+
print("Building llama.cpp")
63+
subprocess.run(f"cd .. && make quantize", shell=True, check=True)
64+
65+
fp16 = f"{outdir}/{outname}.ggml{ggml_version}.fp16.bin"
66+
67+
print(f"Making unquantised GGML at {fp16}")
68+
if not os.path.isfile(fp16):
69+
subprocess.run(f"python3 ../convert.py {model} --outtype f16 --outfile {fp16}", shell=True, check=True)
70+
else:
71+
print(f"Unquantised GGML already exists at: {fp16}")
72+
73+
print("Making quants")
74+
for type in quants:
75+
outfile = f"{outdir}/{outname}.ggml{ggml_version}.{type}.bin"
76+
print(f"Making {type} : {outfile}")
77+
subprocess.run(f"../quantize {fp16} {outfile} {type}", shell=True, check=True)
78+
79+
if not keep_fp16:
80+
os.remove(fp16)
81+
82+
if __name__ == "__main__":
83+
parser = argparse.ArgumentParser(description='Convert/Quantize HF to GGML. If you have the HF model downloaded already, pass the path to the model dir. Otherwise, pass the Hugging Face model repo name. You need to be in the /examples folder for it to work.')
84+
parser.add_argument('--model', required=True, help='Downloaded model dir or Hugging Face model repo name')
85+
parser.add_argument('--outname', default=None, help='Output model(s) name')
86+
parser.add_argument('--outdir', default=None, help='Output directory')
87+
parser.add_argument('--quants', nargs='*', default=["Q4_K_M", "Q5_K_S"], help='Quant types')
88+
parser.add_argument('--keep_fp16', action='store_true', help='Keep fp16 model', default=False)
89+
90+
args = parser.parse_args()
91+
92+
main(args.model, args.outname, args.outdir, args.quants, args.keep_fp16)

0 commit comments

Comments
 (0)