File size: 7,467 Bytes
1e57684 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
import argparse
import os
import shutil
import subprocess
import time
from pathlib import Path
from git import Repo
def clone_hf_with_git(username: str, model_name: str, saved_dir: str):
full_model_name = f"{username}/{model_name}"
url = f"https://huggingface.co/{full_model_name}"
saved = f"{saved_dir}/{model_name}"
# perform `git lfs install`
subprocess.run(["git", "lfs", "install"])
print(f"[INFO] Cloning {model_name} from {url} ...")
Repo.clone_from(url, saved)
def download_hf_with_git(full_name: str, saved_dir: str):
model_name = full_name.split("/")[1]
url = f"git@hf.co:{full_name}"
saved = f"{saved_dir}/{model_name}"
# perform `git lfs install`
subprocess.run(["git", "lfs", "install"])
print(f"Cloning {model_name} from {url} ...")
subprocess.run(["git", "clone", "--progress", url, saved])
def convert_hf_to_gguf(
script_path: str,
dir_raw_model: str,
gguf_model_path: str,
pad_vocab: bool = False,
):
if pad_vocab is True:
args = [
"--outfile",
gguf_model_path,
# "--vocab-type",
# "bpe",
"--pad-vocab",
dir_raw_model,
]
else:
args = ["--outfile", gguf_model_path, dir_raw_model]
# convert.py for llama-3
# args = ["--outfile", gguf_model_path, "--vocab-type", "bpe", dir_raw_model]
res = subprocess.run(["python", script_path] + args)
print(res)
def quantize_model(
quantizer: str,
f16_gguf_model_path: str,
quantized_gguf_model_path: str,
quant_type: str = "q4_0",
):
print(f"[INFO] quantizer: {quantizer}")
print(f"[INFO] quant_type: {quant_type}")
print(f"[INFO] f16_gguf_model_path: {f16_gguf_model_path}")
print(f"[INFO] quantized_model_filename: {quantized_gguf_model_path}")
subprocess.run(
[
quantizer,
f16_gguf_model_path,
quantized_gguf_model_path,
quant_type,
]
)
def main():
parser = argparse.ArgumentParser(description="Convert and quantize gguf models.")
parser.add_argument(
"--full-name",
type=str,
help="Huggingface model full name. e.g. `username/model_name`",
)
parser.add_argument(
"-s",
"--saved-dir",
type=str,
default="models",
help="The directory to save the model.",
)
parser.add_argument(
"--enable-converter",
action="store_true",
help="Enable the converter. Notice that `--converter` must be specified.",
)
parser.add_argument(
"-c",
"--converter",
type=str,
help="The path to the converter. Notice that `--enable-converter` must be specified if use this option.",
)
parser.add_argument(
"--pad-vocab",
action="store_true",
help="Enable adding pad tokens when model vocab expects more than tokenizer metadata provides. Notice that `--enable-converter` must be specified.",
)
parser.add_argument(
"--enable-quantizer",
action="store_true",
help="Enable the quantizer. Notice that `--quantizer` must be specified.",
)
parser.add_argument(
"-q",
"--quantizer",
type=str,
help="The path to the quantizer. Notice that `--enable-quantizer` must be specified if use this option.",
)
parser.add_argument(
"-t",
"--quant-type",
type=str,
default=None,
help="The quantization type. Notice that `--enable-quantizer` must be specified if use this option.",
)
args = parser.parse_args()
print(args)
print("Download model ...")
full_name = args.full_name
username, model_name = full_name.split("/")
saved_dir = args.saved_dir
# try:
# download_hf_with_git(full_name, saved_dir)
# print(f"The raw model is saved in {saved_dir}.")
# except Exception as e:
# print(f"Failed to download model. {e}")
# return
if args.enable_converter is True:
print("[CONVERTER] Convert model ...")
converter = args.converter
raw_model_dir = f"{saved_dir}/{model_name}"
print(f"[CONVERTER] raw_model_dir: {raw_model_dir}")
gguf_model_dir = Path(raw_model_dir).parent / f"{model_name}-gguf"
if not gguf_model_dir.exists():
gguf_model_dir.mkdir()
f16_gguf_model_path = gguf_model_dir / f"{model_name}-f16.gguf"
print(f"[CONVERTER] f16_gguf_model_path: {f16_gguf_model_path}")
# try:
# convert_hf_to_gguf(
# converter,
# raw_model_dir,
# str(f16_gguf_model_path),
# args.pad_vocab,
# )
# print(f"The converted gguf model is saved in {f16_gguf_model_path}.")
# except Exception as e:
# print(f"Failed to convert model. {e}")
# return
if args.enable_quantizer is True:
print("[QUANTIZER] Quantize model ...")
quantizer = args.quantizer
print(f"[QUANTIZER] quantizer: {quantizer}")
if args.quant_type is not None:
quant_type = args.quant_type
quantized_gguf_model_path = (
gguf_model_dir / f"{model_name}-{quant_type}.gguf"
)
print(f"[QUANTIZER] quant_type: {quant_type}")
print(f"[QUANTIZER] quantized_model_filename: {quantized_gguf_model_path}")
try:
quantize_model(
quantizer,
str(f16_gguf_model_path),
str(quantized_gguf_model_path),
quant_type,
)
print(
f"The quantized gguf model is saved in {quantized_gguf_model_path}."
)
except Exception as e:
print(e)
print("Failed to quantize model.")
return
else:
for quant_type in [
# "Q2_K",
# "Q3_K_L",
# "Q3_K_M",
# "Q3_K_S",
# "Q4_0",
# "Q4_K_M",
# "Q4_K_S",
# "Q5_0",
"Q5_K_M",
# "Q5_K_S",
"Q6_K",
"Q8_0",
]:
quantized_gguf_model_path = (
gguf_model_dir / f"{model_name}-{quant_type}.gguf"
)
print(f"[QUANTIZER] quant_type: {quant_type}")
print(
f"[QUANTIZER] quantized_model_filename: {quantized_gguf_model_path}"
)
try:
quantize_model(
quantizer,
str(f16_gguf_model_path),
str(quantized_gguf_model_path),
quant_type,
)
print(
f"The quantized gguf model is saved in {quantized_gguf_model_path}."
)
except Exception as e:
print(e)
print("Failed to quantize model.")
return
# # remove the raw model dir for saving space
# print(f"The quantization is done. Remove {raw_model_dir}")
# shutil.rmtree(raw_model_dir)
print("Done.")
if __name__ == "__main__":
main()
|