Skip to content

Commit 0305841

Browse files
committed
added a gguf file analyzer
1 parent 91b6e29 commit 0305841

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

koboldcpp.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import urllib.parse as urlparse
2828
from concurrent.futures import ThreadPoolExecutor
2929
from datetime import datetime, timezone
30+
from pathlib import Path
3031

3132
# constants
3233
sampler_order_max = 7
@@ -3604,6 +3605,8 @@ def kcpp_export_template():
36043605
ctk.CTkButton(extra_tab , text = "Unpack KoboldCpp To Folder", command = unpack_to_dir ).grid(row=3,column=0, stick="w", padx= 8, pady=2)
36053606
makelabel(extra_tab, "Export as launcher .kcppt template (Expert Only)", 4, 0,tooltiptxt="Creates a KoboldCpp launch template for others to use.\nEmbeds JSON files directly into exported file when saving.\nWhen loaded, forces the backend to be automatically determined.\nWarning! Not recommended for beginners!")
36063607
ctk.CTkButton(extra_tab , text = "Generate LaunchTemplate", command = kcpp_export_template ).grid(row=5,column=0, stick="w", padx= 8, pady=2)
3608+
makelabel(extra_tab, "Analyze GGUF Metadata", 6, 0,tooltiptxt="Reads the metadata, weight types and tensor names in any GGUF file.")
3609+
ctk.CTkButton(extra_tab , text = "Analyze GGUF", command = analyze_gguf_model_wrapper ).grid(row=7,column=0, stick="w", padx= 8, pady=2)
36073610

36083611
# launch
36093612
def guilaunch():
@@ -4526,6 +4529,33 @@ def download_model_from_url(url,permitted_types=[".gguf",".safetensors"]):
45264529
return dlfile
45274530
return None
45284531

4532+
def analyze_gguf_model(args,filename):
4533+
try:
4534+
stime = datetime.now()
4535+
from gguf.scripts.gguf_dump import dump_metadata
4536+
from gguf import GGUFReader
4537+
reader = GGUFReader(filename, 'r')
4538+
ns = argparse.Namespace()
4539+
ns.no_tensors = False
4540+
dump_metadata(reader, ns)
4541+
atime = (datetime.now() - stime).total_seconds()
4542+
print(f"---\nAnalyzing completed in {atime:.2f}s.\n---",flush=True)
4543+
except Exception as e:
4544+
print(f"Cannot Analyze File: {e}")
4545+
return
4546+
4547+
def analyze_gguf_model_wrapper(filename=""):
4548+
if not filename or filename=="":
4549+
from tkinter.filedialog import askopenfilename
4550+
filename = askopenfilename(title="Select GGUF to analyze")
4551+
if not filename or filename=="" or not os.path.exists(filename):
4552+
print("Selected GGUF file not found. Please select a valid GGUF file to analyze.")
4553+
return
4554+
print("---")
4555+
print(f"Analyzing {filename}, please wait...\n---",flush=True)
4556+
dumpthread = threading.Thread(target=analyze_gguf_model, args=(args,filename))
4557+
dumpthread.start()
4558+
45294559
def main(launch_args,start_server=True):
45304560
global embedded_kailite, embedded_kcpp_docs, embedded_kcpp_sdui
45314561
global libname, args, friendlymodelname, friendlysdmodelname, fullsdmodelpath, mmprojpath, password, fullwhispermodelpath
@@ -4539,6 +4569,13 @@ def main(launch_args,start_server=True):
45394569

45404570
print(f"***\nWelcome to KoboldCpp - Version {KcppVersion}") # just update version manually
45414571
# print("Python version: " + sys.version)
4572+
# connect path
4573+
try:
4574+
if (Path(__file__).parent / "gguf-py").exists():
4575+
ggufpy_path = str(Path(__file__).parent / "gguf-py")
4576+
sys.path.append(ggufpy_path)
4577+
except Exception as e:
4578+
print(f"Cannot import gguf-py path: {e}")
45424579

45434580
#perform some basic cleanup of old temporary directories
45444581
try:
@@ -4550,6 +4587,10 @@ def main(launch_args,start_server=True):
45504587
unpack_to_dir(args.unpack)
45514588
return
45524589

4590+
if args.analyze:
4591+
analyze_gguf_model_wrapper(args.analyze)
4592+
return
4593+
45534594
if args.config and len(args.config)==1:
45544595
cfgname = args.config[0]
45554596
if isinstance(cfgname, str):
@@ -5182,6 +5223,7 @@ def range_checker(arg: str):
51825223
#more advanced params
51835224
advparser = parser.add_argument_group('Advanced Commands')
51845225
advparser.add_argument("--version", help="Prints version and exits.", action='store_true')
5226+
advparser.add_argument("--analyze", metavar=('[filename]'), help="Reads the metadata, weight types and tensor names in any GGUF file.", default="")
51855227
advparser.add_argument("--ropeconfig", help="If set, uses customized RoPE scaling from configured frequency scale and frequency base (e.g. --ropeconfig 0.25 10000). Otherwise, uses NTK-Aware scaling set automatically based on context size. For linear rope, simply set the freq-scale and ignore the freq-base",metavar=('[rope-freq-scale]', '[rope-freq-base]'), default=[0.0, 10000.0], type=float, nargs='+')
51865228
advparser.add_argument("--blasbatchsize", help="Sets the batch size used in BLAS processing (default 512). Setting it to -1 disables BLAS mode, but keeps other benefits like GPU offload.", type=int,choices=[-1,32,64,128,256,512,1024,2048], default=512)
51875229
advparser.add_argument("--blasthreads", help="Use a different number of threads during BLAS if specified. Otherwise, has the same value as --threads",metavar=('[threads]'), type=int, default=0)

0 commit comments

Comments
 (0)