Upload gguf-imat-llama-3.py
#30
by
SolidSnacke
- opened
- gguf-imat-llama-3.py +180 -168
gguf-imat-llama-3.py
CHANGED
@@ -1,169 +1,181 @@
|
|
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 |
-
|
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 |
-
|
102 |
-
|
103 |
-
def
|
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}-
|
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", "
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
print("
|
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 |
main()
|
|
|
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_bf16(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_bf16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name)
|
102 |
+
|
103 |
+
def convert_model_to_gguf_bf16(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}-BF16.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", "bf16"])
|
113 |
+
|
114 |
+
convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name)
|
115 |
+
|
116 |
+
def convert_model_to_gguf_f16(base_dir, model_dir, model_name, delete_model_dir, imatrix_file_name):
|
117 |
+
convert_script = os.path.join(base_dir, "llama.cpp", "convert-hf-to-gguf.py")
|
118 |
+
gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")
|
119 |
+
gguf_model_path = os.path.join(gguf_dir, f"{model_name}-F16.gguf")
|
120 |
+
|
121 |
+
if not os.path.exists(gguf_dir):
|
122 |
+
os.makedirs(gguf_dir)
|
123 |
+
|
124 |
+
if not os.path.exists(gguf_model_path):
|
125 |
+
subprocess.run(["python", convert_script, model_dir, "--outfile", gguf_model_path, "--outtype", "f16"])
|
126 |
+
|
127 |
+
if delete_model_dir == 'yes' or delete_model_dir == 'y':
|
128 |
+
shutil.rmtree(model_dir)
|
129 |
+
print(f"Original model directory '{model_dir}' deleted.")
|
130 |
+
else:
|
131 |
+
print(f"Original model directory '{model_dir}' was not deleted. You can remove it manually.")
|
132 |
+
|
133 |
+
create_imatrix(base_dir, gguf_dir, gguf_model_path, model_name, imatrix_file_name)
|
134 |
+
|
135 |
+
def create_imatrix(base_dir, gguf_dir, gguf_model_path, model_name, imatrix_file_name):
|
136 |
+
imatrix_exe = os.path.join(base_dir, "bin", "imatrix.exe")
|
137 |
+
imatrix_output_src = os.path.join(gguf_dir, "imatrix.dat")
|
138 |
+
imatrix_output_dst = os.path.join(gguf_dir, "imatrix.dat")
|
139 |
+
if not os.path.exists(imatrix_output_dst):
|
140 |
+
try:
|
141 |
+
subprocess.run([imatrix_exe, "-m", gguf_model_path, "-f", os.path.join(base_dir, "imatrix", imatrix_file_name), "-ngl", "7"], cwd=gguf_dir)
|
142 |
+
shutil.move(imatrix_output_src, imatrix_output_dst)
|
143 |
+
print("imatrix.dat moved successfully.")
|
144 |
+
except Exception as e:
|
145 |
+
print("Error occurred while moving imatrix.dat:", e)
|
146 |
+
else:
|
147 |
+
print("imatrix.dat already exists in the GGUF folder.")
|
148 |
+
|
149 |
+
quantize_models(base_dir, model_name)
|
150 |
+
|
151 |
+
def quantize_models(base_dir, model_name):
|
152 |
+
gguf_dir = os.path.join(base_dir, "models", f"{model_name}-GGUF")
|
153 |
+
bf16_gguf_path = os.path.join(gguf_dir, f"{model_name}-BF16.gguf")
|
154 |
+
|
155 |
+
quantization_options = [
|
156 |
+
"IQ3_M", "IQ3_XXS",
|
157 |
+
"Q4_K_M", "Q4_K_S", "IQ4_NL", "IQ4_XS",
|
158 |
+
"Q5_K_M", "Q5_K_S",
|
159 |
+
"Q6_K",
|
160 |
+
"Q8_0"
|
161 |
+
]
|
162 |
+
|
163 |
+
for quant_option in quantization_options:
|
164 |
+
quantized_gguf_name = f"{model_name}-{quant_option}-imat.gguf"
|
165 |
+
quantized_gguf_path = os.path.join(gguf_dir, quantized_gguf_name)
|
166 |
+
quantize_command = os.path.join(base_dir, "bin", "quantize.exe")
|
167 |
+
imatrix_path = os.path.join(gguf_dir, "imatrix.dat")
|
168 |
+
|
169 |
+
subprocess.run([quantize_command, "--imatrix", imatrix_path,
|
170 |
+
bf16_gguf_path, quantized_gguf_path, quant_option], cwd=gguf_dir)
|
171 |
+
print(f"Model quantized with {quant_option} option.")
|
172 |
+
|
173 |
+
def main():
|
174 |
+
clone_or_update_llama_cpp()
|
175 |
+
latest_release_tag = download_llama_release()
|
176 |
+
download_cudart_if_necessary(latest_release_tag)
|
177 |
+
download_model_repo()
|
178 |
+
print("Finished preparing resources.")
|
179 |
+
|
180 |
+
if __name__ == "__main__":
|
181 |
main()
|