FantasiaFoundry
commited on
Commit
•
6df628f
1
Parent(s):
f197dc8
Add back llama-3 script in previous state.
Browse files- gguf-imat-llama-3.py +169 -0
gguf-imat-llama-3.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import requests
|
3 |
+
import zipfile
|
4 |
+
import subprocess
|
5 |
+
import shutil
|
6 |
+
from huggingface_hub import snapshot_download
|
7 |
+
|
8 |
+
def clone_or_update_llama_cpp():
|
9 |
+
print("Preparing...")
|
10 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
11 |
+
os.chdir(base_dir)
|
12 |
+
if not os.path.exists("llama.cpp"):
|
13 |
+
subprocess.run(["git", "clone", "--depth", "1", "https://github.com/ggerganov/llama.cpp"])
|
14 |
+
else:
|
15 |
+
os.chdir("llama.cpp")
|
16 |
+
subprocess.run(["git", "pull"])
|
17 |
+
os.chdir(base_dir)
|
18 |
+
print("The 'llama.cpp' repository is ready.")
|
19 |
+
|
20 |
+
def download_llama_release():
|
21 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
22 |
+
dl_dir = os.path.join(base_dir, "bin", "dl")
|
23 |
+
if not os.path.exists(dl_dir):
|
24 |
+
os.makedirs(dl_dir)
|
25 |
+
|
26 |
+
os.chdir(dl_dir)
|
27 |
+
latest_release_url = "https://github.com/ggerganov/llama.cpp/releases/latest"
|
28 |
+
response = requests.get(latest_release_url)
|
29 |
+
if response.status_code == 200:
|
30 |
+
latest_release_tag = response.url.split("/")[-1]
|
31 |
+
download_url = f"https://github.com/ggerganov/llama.cpp/releases/download/{latest_release_tag}/llama-{latest_release_tag}-bin-win-cuda-cu12.2.0-x64.zip"
|
32 |
+
response = requests.get(download_url)
|
33 |
+
if response.status_code == 200:
|
34 |
+
with open(f"llama-{latest_release_tag}-bin-win-cuda-cu12.2.0-x64.zip", "wb") as f:
|
35 |
+
f.write(response.content)
|
36 |
+
with zipfile.ZipFile(f"llama-{latest_release_tag}-bin-win-cuda-cu12.2.0-x64.zip", "r") as zip_ref:
|
37 |
+
zip_ref.extractall(os.path.join(base_dir, "bin"))
|
38 |
+
print("Downloading latest 'llama.cpp' prebuilt Windows binaries...")
|
39 |
+
print("Download and extraction completed successfully.")
|
40 |
+
return latest_release_tag
|
41 |
+
else:
|
42 |
+
print("Failed to download the release file.")
|
43 |
+
else:
|
44 |
+
print("Failed to fetch the latest release information.")
|
45 |
+
|
46 |
+
def download_cudart_if_necessary(latest_release_tag):
|
47 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
48 |
+
cudart_dl_dir = os.path.join(base_dir, "bin", "dl")
|
49 |
+
if not os.path.exists(cudart_dl_dir):
|
50 |
+
os.makedirs(cudart_dl_dir)
|
51 |
+
|
52 |
+
cudart_zip_file = os.path.join(cudart_dl_dir, "cudart-llama-bin-win-cu12.2.0-x64.zip")
|
53 |
+
cudart_extracted_files = ["cublas64_12.dll", "cublasLt64_12.dll", "cudart64_12.dll"]
|
54 |
+
|
55 |
+
if all(os.path.exists(os.path.join(base_dir, "bin", file)) for file in cudart_extracted_files):
|
56 |
+
print("Cuda resources already exist. Skipping download.")
|
57 |
+
else:
|
58 |
+
cudart_download_url = f"https://github.com/ggerganov/llama.cpp/releases/download/{latest_release_tag}/cudart-llama-bin-win-cu12.2.0-x64.zip"
|
59 |
+
response = requests.get(cudart_download_url)
|
60 |
+
if response.status_code == 200:
|
61 |
+
with open(cudart_zip_file, "wb") as f:
|
62 |
+
f.write(response.content)
|
63 |
+
with zipfile.ZipFile(cudart_zip_file, "r") as zip_ref:
|
64 |
+
zip_ref.extractall(os.path.join(base_dir, "bin"))
|
65 |
+
print("Preparing 'cuda' resources...")
|
66 |
+
print("Download and extraction of cudart completed successfully.")
|
67 |
+
else:
|
68 |
+
print("Failed to download the cudart release file.")
|
69 |
+
|
70 |
+
def download_model_repo():
|
71 |
+
base_dir = os.path.dirname(os.path.abspath(__file__))
|
72 |
+
models_dir = os.path.join(base_dir, "models")
|
73 |
+
|
74 |
+
if not os.path.exists(models_dir):
|
75 |
+
os.makedirs(models_dir)
|
76 |
+
|
77 |
+
model_id = input("Enter the model ID to download (e.g., huggingface/transformers): ")
|
78 |
+
model_name = model_id.split("/")[-1]
|
79 |
+
model_dir = os.path.join(models_dir, model_name)
|
80 |
+
|
81 |
+
gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")
|
82 |
+
gguf_model_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")
|
83 |
+
imatrix_file_name = input("Enter the name of the imatrix.txt file (default: imatrix.txt): ").strip() or "imatrix.txt"
|
84 |
+
delete_model_dir = input("Remove HF model folder after converting original model to GGUF? (yes/no) (default: no): ").strip().lower()
|
85 |
+
|
86 |
+
if os.path.exists(gguf_model_path):
|
87 |
+
create_imatrix(base_dir, gguf_dir, gguf_model_path, model_name, imatrix_file_name)
|
88 |
+
else:
|
89 |
+
if os.path.exists(model_dir):
|
90 |
+
print("Model repository already exists. Using existing repository.")
|
91 |
+
|
92 |
+
convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name)
|
93 |
+
|
94 |
+
else:
|
95 |
+
revision = input("Enter the revision (branch, tag, or commit) to download (default: main): ") or "main"
|
96 |
+
|
97 |
+
print("Downloading model repository...")
|
98 |
+
snapshot_download(repo_id=model_id, local_dir=model_dir, revision=revision)
|
99 |
+
print("Model repository downloaded successfully.")
|
100 |
+
|
101 |
+
convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name)
|
102 |
+
|
103 |
+
def convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name):
|
104 |
+
convert_script = os.path.join(base_dir, "llama.cpp", "convert-hf-to-gguf.py")
|
105 |
+
gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")
|
106 |
+
gguf_model_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")
|
107 |
+
|
108 |
+
if not os.path.exists(gguf_dir):
|
109 |
+
os.makedirs(gguf_dir)
|
110 |
+
|
111 |
+
if not os.path.exists(gguf_model_path):
|
112 |
+
subprocess.run(["python", convert_script, model_dir, "--outfile", gguf_model_path, "--outtype", "f16"])
|
113 |
+
|
114 |
+
if delete_model_dir == 'yes' or delete_model_dir == 'y':
|
115 |
+
shutil.rmtree(model_dir)
|
116 |
+
print(f"Original model directory '{model_dir}' deleted.")
|
117 |
+
else:
|
118 |
+
print(f"Original model directory '{model_dir}' was not deleted. You can remove it manually.")
|
119 |
+
|
120 |
+
|
121 |
+
create_imatrix(base_dir, gguf_dir, gguf_model_path, model_name, imatrix_file_name)
|
122 |
+
|
123 |
+
def create_imatrix(base_dir, gguf_dir, gguf_model_path, model_name, imatrix_file_name):
|
124 |
+
imatrix_exe = os.path.join(base_dir, "bin", "imatrix.exe")
|
125 |
+
imatrix_output_src = os.path.join(gguf_dir, "imatrix.dat")
|
126 |
+
imatrix_output_dst = os.path.join(gguf_dir, "imatrix.dat")
|
127 |
+
if not os.path.exists(imatrix_output_dst):
|
128 |
+
try:
|
129 |
+
subprocess.run([imatrix_exe, "-m", gguf_model_path, "-f", os.path.join(base_dir, "imatrix", imatrix_file_name), "-ngl", "8"], cwd=gguf_dir)
|
130 |
+
shutil.move(imatrix_output_src, imatrix_output_dst)
|
131 |
+
print("imatrix.dat moved successfully.")
|
132 |
+
except Exception as e:
|
133 |
+
print("Error occurred while moving imatrix.dat:", e)
|
134 |
+
else:
|
135 |
+
print("imatrix.dat already exists in the GGUF folder.")
|
136 |
+
|
137 |
+
quantize_models(base_dir, model_name)
|
138 |
+
|
139 |
+
def quantize_models(base_dir, model_name):
|
140 |
+
gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")
|
141 |
+
f16_gguf_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")
|
142 |
+
|
143 |
+
quantization_options = [
|
144 |
+
"IQ3_M", "IQ3_XXS",
|
145 |
+
"Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS",
|
146 |
+
"Q5_K_M", "Q5_K_S",
|
147 |
+
"Q6_K",
|
148 |
+
"Q8_0"
|
149 |
+
]
|
150 |
+
|
151 |
+
for quant_option in quantization_options:
|
152 |
+
quantized_gguf_name = f"{model_name}-{quant_option}-imat.gguf"
|
153 |
+
quantized_gguf_path = os.path.join(gguf_dir, quantized_gguf_name)
|
154 |
+
quantize_command = os.path.join(base_dir, "bin", "quantize.exe")
|
155 |
+
imatrix_path = os.path.join(gguf_dir, "imatrix.dat")
|
156 |
+
|
157 |
+
subprocess.run([quantize_command, "--imatrix", imatrix_path,
|
158 |
+
f16_gguf_path, quantized_gguf_path, quant_option], cwd=gguf_dir)
|
159 |
+
print(f"Model quantized with {quant_option} option.")
|
160 |
+
|
161 |
+
def main():
|
162 |
+
clone_or_update_llama_cpp()
|
163 |
+
latest_release_tag = download_llama_release()
|
164 |
+
download_cudart_if_necessary(latest_release_tag)
|
165 |
+
download_model_repo()
|
166 |
+
print("Finished preparing resources.")
|
167 |
+
|
168 |
+
if __name__ == "__main__":
|
169 |
+
main()
|