-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEasyQuantizationGUI.py
More file actions
354 lines (288 loc) · 13.8 KB
/
EasyQuantizationGUI.py
File metadata and controls
354 lines (288 loc) · 13.8 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
VERSION = "1.11"
import sys
import subprocess
import importlib
import os
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
try:
import torch, tqdm, safetensors, gguf, sentencepiece, yaml, numpy
except ImportError:
print("Some required packages are missing. Installing from requirements.txt...")
install("requirements.txt")
import torch, tqdm, safetensors, gguf, sentencepiece, yaml, numpy
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import os
import shutil
import winsound
import tkinter.scrolledtext as scrolledtext
def scroll_entry_to_end(entry):
entry.xview_moveto(1)
def browse_file(entry):
file_path = filedialog.askopenfilename(filetypes=[("Model files", "*.safetensors *.gguf *.sft")])
if file_path:
file_path = file_path.replace('\\', '/') # Ensure forward slashes
entry.delete(0, tk.END)
entry.insert(0, file_path)
scroll_entry_to_end(entry)
suggest_output_file() # Call this instead of update_output_file
def suggest_output_file():
input_file = input_entry.get()
quantize_level = quantize_level_var.get()
if input_file:
input_dir = os.path.dirname(input_file)
input_filename = os.path.basename(input_file)
input_name, _ = os.path.splitext(input_filename)
output_file = f"{input_dir}/{input_name}-{quantize_level}.gguf"
output_entry.delete(0, tk.END)
output_entry.insert(0, output_file)
scroll_entry_to_end(output_entry)
def browse_output_file(entry):
# Get the current input file and quantization level
input_file = input_entry.get()
quantize_level = quantize_level_var.get()
# Generate a default output filename
if input_file:
input_dir = os.path.dirname(input_file)
input_filename = os.path.basename(input_file)
input_name, _ = os.path.splitext(input_filename)
default_filename = f"{input_name}-{quantize_level}.gguf"
else:
default_filename = f"output-{quantize_level}.gguf"
input_dir = "/"
# Open the file dialog with the default filename
file_path = filedialog.asksaveasfilename(
initialdir=input_dir,
initialfile=default_filename,
defaultextension=".gguf",
filetypes=[("GGUF files", "*.gguf")]
)
if file_path:
file_path = file_path.replace('\\', '/') # Ensure forward slashes
entry.delete(0, tk.END)
entry.insert(0, file_path)
scroll_entry_to_end(entry)
def disable_ui():
global input_entry, output_entry, input_browse, output_browse, quantize_dropdown, run_button
input_entry.config(state='disabled')
output_entry.config(state='disabled')
input_browse.config(state='disabled')
output_browse.config(state='disabled')
quantize_dropdown.config(state='disabled')
run_button.config(state='disabled')
def enable_ui():
global input_entry, output_entry, input_browse, output_browse, quantize_dropdown, run_button
input_entry.config(state='normal')
output_entry.config(state='normal')
input_browse.config(state='normal')
output_browse.config(state='normal')
quantize_dropdown.config(state='readonly')
run_button.config(state='normal')
def run_llama_quantize():
input_file = input_entry.get()
output_file = output_entry.get()
quantize_level = quantize_level_var.get()
if not input_file or not output_file:
messagebox.showerror("Error", "Please select both input and output files.")
return
# Check if input and output files are the same
if os.path.abspath(input_file) == os.path.abspath(output_file):
messagebox.showerror("Error", "Input and output files cannot be the same.")
return
output_dir = os.path.dirname(output_file)
required_space = 40_000_000_000 # ~40 GB (a bit more than 36.5 GB)
available_space = shutil.disk_usage(output_dir).free
if available_space < required_space:
required_gb = required_space / (1024**3)
available_gb = available_space / (1024**3)
messagebox.showerror("Error", f"You need {required_gb:.1f} GB of drive space to continue. Only {available_gb:.1f} GB available.")
return
disable_ui()
# Clear previous log
process_text.delete('1.0', tk.END)
root.update()
is_input_gguf = input_file.lower().endswith(".gguf")
temp_gguf_file = None # Initialize temp_gguf_file
if not is_input_gguf:
process_text.insert(tk.END, "Starting conversion process (Safetensors/SFT -> GGUF)...\n")
process_text.see(tk.END)
root.update()
# Convert the input file to GGUF format
convert_py_path = resource_path("convert.py")
output_dir = os.path.dirname(output_file)
# Use a more descriptive temporary file name based on the output file
output_name, _ = os.path.splitext(os.path.basename(output_file))
temp_gguf_file = os.path.join(output_dir, f"{output_name}_temp_conversion.gguf")
# Add cleanup of existing temp file
if os.path.exists(temp_gguf_file):
try:
os.remove(temp_gguf_file)
process_text.insert(tk.END, "Cleaned up existing temporary file.\n")
process_text.see(tk.END)
root.update()
except Exception as e:
process_text.insert(tk.END, f"Error cleaning up temporary file: {e}\n")
process_text.see(tk.END)
root.update()
enable_ui()
return
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
# Get the Python executable path from the current environment
pythonpath = sys.executable
process = subprocess.Popen([pythonpath, convert_py_path, "--src", input_file, "--dst", temp_gguf_file],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
bufsize=1, universal_newlines=True, startupinfo=startupinfo)
for line in process.stdout:
process_text.insert(tk.END, line)
process_text.see(tk.END)
root.update()
process.wait()
if process.returncode != 0:
raise subprocess.CalledProcessError(process.returncode, process.args)
process_text.insert(tk.END, "Conversion completed successfully.\n")
process_text.see(tk.END)
root.update()
except subprocess.CalledProcessError as e:
process_text.insert(tk.END, f"Error converting file: {e}\n")
process_text.insert(tk.END, f"Command: {e.cmd}\n")
process_text.insert(tk.END, f"Return code: {e.returncode}\n")
process_text.see(tk.END)
root.update()
# Clean up the temporary file even if conversion fails
if temp_gguf_file and os.path.exists(temp_gguf_file):
os.remove(temp_gguf_file)
enable_ui()
return
except Exception as e: # Catch other potential errors during conversion
process_text.insert(tk.END, f"An unexpected error occurred during conversion: {e}\n")
process_text.see(tk.END)
root.update()
if temp_gguf_file and os.path.exists(temp_gguf_file):
os.remove(temp_gguf_file)
enable_ui()
return
# --- End of conversion block ---
else:
process_text.insert(tk.END, "Input is already GGUF. Skipping conversion step.\n")
process_text.see(tk.END)
root.update()
# If input is GGUF, llama-quantize will read directly from it
quantize_input_file = input_file
# Determine the input file for the quantization step
quantize_input_file = temp_gguf_file if temp_gguf_file else input_file
# Quantize the file (either the temporary one or the original GGUF)
llama_quantize_path = resource_path("llama-quantize.exe")
process_text.insert(tk.END, "Starting quantization process...\n")
process_text.see(tk.END)
root.update()
try:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
# Use quantize_input_file determined above
process = subprocess.Popen([llama_quantize_path, quantize_input_file, output_file, quantize_level],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
bufsize=1, universal_newlines=True, startupinfo=startupinfo)
for line in process.stdout:
process_text.insert(tk.END, line)
process_text.see(tk.END)
root.update()
process.wait()
if process.returncode != 0:
# If quantization failed and we used a temp file, report the temp file name
if temp_gguf_file:
process_text.insert(tk.END, f"Quantization command failed on temporary file: {temp_gguf_file}\n")
raise subprocess.CalledProcessError(process.returncode, process.args)
process_text.insert(tk.END, "Quantization completed successfully.\n")
except subprocess.CalledProcessError as e:
process_text.insert(tk.END, f"Error running llama-quantize: {e}\n")
process_text.insert(tk.END, f"Command: {e.cmd}\n")
process_text.insert(tk.END, f"Return code: {e.returncode}\n")
process_text.see(tk.END)
root.update()
except Exception as e: # Catch other potential errors during quantization
process_text.insert(tk.END, f"An unexpected error occurred during quantization: {e}\n")
process_text.see(tk.END)
root.update()
finally:
# Clean up the temporary file if it was created
if temp_gguf_file and os.path.exists(temp_gguf_file):
try:
os.remove(temp_gguf_file)
process_text.insert(tk.END, "Cleaned up temporary conversion file.\n")
process_text.see(tk.END)
root.update()
except Exception as e:
process_text.insert(tk.END, f"Error cleaning up temporary file {temp_gguf_file}: {e}\n")
process_text.see(tk.END)
root.update()
process_text.insert(tk.END, "Process finished.\n") # Changed message slightly
process_text.see(tk.END)
root.update()
enable_ui()
# Play sound effect
winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
def main():
global root, process_text, input_entry, output_entry, quantize_dropdown, run_button, quantize_level_var
global input_browse, output_browse # Add these two variables
root = tk.Tk()
root.title(f"Easy Quantization GUI v{VERSION}")
root.geometry("800x600")
# Quantize level selection
quantize_frame = tk.Frame(root)
quantize_frame.pack(pady=10, padx=10)
quantize_label = tk.Label(quantize_frame, text="Quantize Level:")
quantize_label.pack(side=tk.LEFT)
quantize_levels = ["Q2_K", "Q2_K_S", "Q3_K", "Q3_K_L", "Q3_K_M", "Q3_K_S", "Q4_0", "Q4_1", "Q4_K", "Q4_K_M", "Q4_K_S", "Q5_0", "Q5_1", "Q5_K", "Q5_K_M", "Q5_K_S", "Q6_K", "Q8_0", "F16", "BF16", "F32"]
quantize_level_var = tk.StringVar(root)
quantize_level_var.set("Q8_0") # Set default value to Q8_0
quantize_dropdown = ttk.Combobox(quantize_frame, textvariable=quantize_level_var, values=quantize_levels, state="readonly")
quantize_dropdown.pack(side=tk.LEFT)
quantize_dropdown.bind("<<ComboboxSelected>>", lambda event: suggest_output_file())
# Input file selection
input_frame = tk.Frame(root)
input_frame.pack(pady=10, padx=10, fill=tk.X)
input_label = tk.Label(input_frame, text="Input File:")
input_label.pack(side=tk.LEFT)
input_entry = tk.Entry(input_frame)
input_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
input_browse = tk.Button(input_frame, text="Browse", command=lambda: browse_file(input_entry))
input_browse.pack(side=tk.RIGHT)
# Add binding to scroll input entry when it gains focus
input_entry.bind("<FocusIn>", lambda event: scroll_entry_to_end(input_entry))
# Output file selection
output_frame = tk.Frame(root)
output_frame.pack(pady=10, padx=10, fill=tk.X)
output_label = tk.Label(output_frame, text="Output File:")
output_label.pack(side=tk.LEFT)
output_entry = tk.Entry(output_frame)
output_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
output_browse = tk.Button(output_frame, text="Browse", command=lambda: browse_output_file(output_entry))
output_browse.pack(side=tk.RIGHT)
# Add binding to scroll output entry when it gains focus
output_entry.bind("<FocusIn>", lambda event: scroll_entry_to_end(output_entry))
# Run button
run_button = tk.Button(root, text="Run Quantization", command=run_llama_quantize)
run_button.pack(pady=20)
# Add process log to bottom of main window
process_frame = tk.Frame(root)
process_frame.pack(pady=10, padx=10, fill=tk.BOTH, expand=True)
process_label = tk.Label(process_frame, text="Process Log:")
process_label.pack(side=tk.TOP, anchor='w')
process_text = scrolledtext.ScrolledText(process_frame, wrap=tk.WORD, height=15)
process_text.pack(expand=True, fill=tk.BOTH)
root.mainloop()
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
if __name__ == "__main__":
main()