Download code/scripts/gguf_bundle.py from Lucasgitguy-co/Turbo-Kev-Dictate-1.4B: direct link, hf CLI and curl.
- Browser
- Download file 6.66 kB
-
https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/scripts/gguf_bundle.py
- Command line
-
hf download hf://Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/code/scripts/gguf_bundle.py
-
curl -L -o gguf_bundle.py https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/scripts/gguf_bundle.py
6.66 kB
| #!/usr/bin/env python3 | |
| """Pack several GGUF files (e.g. model + mmproj) into one GGUF "bundle", and unpack it again. | |
| The bundle is a valid GGUF v3 file. Each original file is stored verbatim as one raw | |
| int8 tensor, with its name, size and sha256 in the metadata, so unpacking gives back the | |
| exact original bytes. llama.cpp cannot run a bundle directly: unpack it first. | |
| Standard library only (Python 3.8+). | |
| python3 gguf_bundle.py unpack bundle.gguf [-o OUTDIR] | |
| python3 gguf_bundle.py list bundle.gguf | |
| python3 gguf_bundle.py pack out.gguf model.gguf mmproj.gguf [--name NAME] | |
| """ | |
| import argparse, hashlib, os, struct, sys | |
| MAGIC, VERSION, ALIGN = b"GGUF", 3, 32 | |
| T_U32, T_STRING, T_ARRAY, T_U64 = 4, 8, 9, 10 | |
| GGML_TYPE_I8 = 24 | |
| CHUNK = 16 << 20 | |
| # ---------- writing ---------- | |
| def _s(x): | |
| b = x.encode("utf-8") | |
| return struct.pack("<Q", len(b)) + b | |
| def _kv(key, typ, val): | |
| out = _s(key) + struct.pack("<I", typ) | |
| if typ == T_STRING: | |
| out += _s(val) | |
| elif typ == T_U32: | |
| out += struct.pack("<I", val) | |
| elif typ == T_ARRAY: | |
| sub, items = val | |
| out += struct.pack("<IQ", sub, len(items)) | |
| for it in items: | |
| out += _s(it) if sub == T_STRING else struct.pack("<Q", it) | |
| return out | |
| def _sha(path): | |
| h = hashlib.sha256() | |
| with open(path, "rb") as f: | |
| while True: | |
| b = f.read(CHUNK) | |
| if not b: | |
| return h.hexdigest() | |
| h.update(b) | |
| def _role(name): | |
| return "mmproj" if "mmproj" in name.lower() else "model" | |
| def pack(out, inputs, name): | |
| names = [os.path.basename(p) for p in inputs] | |
| if len(set(names)) != len(names): | |
| sys.exit("input file names must be unique") | |
| sizes = [os.path.getsize(p) for p in inputs] | |
| shas = [_sha(p) for p in inputs] | |
| kvs = [ | |
| _kv("general.architecture", T_STRING, "bundle"), | |
| _kv("general.name", T_STRING, name), | |
| _kv("general.alignment", T_U32, ALIGN), | |
| _kv("bundle.version", T_U32, 1), | |
| _kv("bundle.note", T_STRING, "Not runnable as-is. Unpack with gguf_bundle.py unpack to get the original files back."), | |
| _kv("bundle.files", T_ARRAY, (T_STRING, names)), | |
| _kv("bundle.roles", T_ARRAY, (T_STRING, [_role(n) for n in names])), | |
| _kv("bundle.sizes", T_ARRAY, (T_U64, sizes)), | |
| _kv("bundle.sha256", T_ARRAY, (T_STRING, shas)), | |
| ] | |
| infos, off = [], 0 | |
| for i, size in enumerate(sizes): | |
| infos.append(_s("bundle.file.%d" % i) + struct.pack("<IQIQ", 1, size, GGML_TYPE_I8, off)) | |
| off += size + (-size % ALIGN) | |
| header = MAGIC + struct.pack("<IQQ", VERSION, len(inputs), len(kvs)) + b"".join(kvs) + b"".join(infos) | |
| header += b"\0" * (-len(header) % ALIGN) | |
| tmp = out + ".part" | |
| with open(tmp, "wb") as f: | |
| f.write(header) | |
| for p, size in zip(inputs, sizes): | |
| with open(p, "rb") as src: | |
| while True: | |
| b = src.read(CHUNK) | |
| if not b: | |
| break | |
| f.write(b) | |
| f.write(b"\0" * (-size % ALIGN)) | |
| os.replace(tmp, out) | |
| for n, s, h in zip(names, sizes, shas): | |
| print("packed %s %d bytes sha256 %s" % (n, s, h)) | |
| print("wrote", out, os.path.getsize(out), "bytes") | |
| # ---------- reading ---------- | |
| _SCALAR = {0: "<B", 1: "<b", 2: "<H", 3: "<h", 4: "<I", 5: "<i", 6: "<f", 7: "<?", 10: "<Q", 11: "<q", 12: "<d"} | |
| def _read(f, fmt): | |
| n = struct.calcsize(fmt) | |
| return struct.unpack(fmt, f.read(n))[0] | |
| def _rs(f): | |
| return f.read(_read(f, "<Q")).decode("utf-8") | |
| def _rv(f, typ): | |
| if typ == T_STRING: | |
| return _rs(f) | |
| if typ == T_ARRAY: | |
| sub, n = _read(f, "<I"), _read(f, "<Q") | |
| return [_rv(f, sub) for _ in range(n)] | |
| return _read(f, _SCALAR[typ]) | |
| def read_header(path): | |
| with open(path, "rb") as f: | |
| if f.read(4) != MAGIC: | |
| sys.exit("%s is not a GGUF file" % path) | |
| ver, nt, nkv = struct.unpack("<IQQ", f.read(20)) | |
| kv = {} | |
| for _ in range(nkv): | |
| k = _rs(f) | |
| kv[k] = _rv(f, _read(f, "<I")) | |
| tensors = [] | |
| for _ in range(nt): | |
| name = _rs(f) | |
| nd = _read(f, "<I") | |
| ne = [_read(f, "<Q") for _ in range(nd)] | |
| typ, off = _read(f, "<I"), _read(f, "<Q") | |
| tensors.append((name, ne, typ, off)) | |
| align = kv.get("general.alignment", ALIGN) | |
| data_start = f.tell() + (-f.tell() % align) | |
| if kv.get("general.architecture") != "bundle": | |
| sys.exit("%s is a normal GGUF, not a bundle" % path) | |
| return kv, tensors, data_start | |
| def list_bundle(path): | |
| kv, _, _ = read_header(path) | |
| for n, r, s, h in zip(kv["bundle.files"], kv["bundle.roles"], kv["bundle.sizes"], kv["bundle.sha256"]): | |
| print("%-7s %-50s %12d %s" % (r, n, s, h)) | |
| def unpack(path, outdir): | |
| kv, tensors, data_start = read_header(path) | |
| os.makedirs(outdir, exist_ok=True) | |
| by_name = {t[0]: t for t in tensors} | |
| ok = True | |
| for i, (n, s, want) in enumerate(zip(kv["bundle.files"], kv["bundle.sizes"], kv["bundle.sha256"])): | |
| _, ne, typ, off = by_name["bundle.file.%d" % i] | |
| assert typ == GGML_TYPE_I8 and ne == [s], "corrupt bundle entry %d" % i | |
| dest = os.path.join(outdir, os.path.basename(n)) | |
| h = hashlib.sha256() | |
| with open(path, "rb") as src, open(dest + ".part", "wb") as dst: | |
| src.seek(data_start + off) | |
| left = s | |
| while left: | |
| b = src.read(min(CHUNK, left)) | |
| if not b: | |
| sys.exit("bundle is truncated") | |
| h.update(b) | |
| dst.write(b) | |
| left -= len(b) | |
| good = h.hexdigest() == want | |
| ok &= good | |
| if good: | |
| os.replace(dest + ".part", dest) | |
| print("%s %s" % ("ok " if good else "BAD HASH", dest)) | |
| if not ok: | |
| sys.exit("sha256 mismatch: download the bundle again") | |
| def main(): | |
| ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
| sp = ap.add_subparsers(dest="cmd", required=True) | |
| p = sp.add_parser("pack"); p.add_argument("out"); p.add_argument("inputs", nargs="+"); p.add_argument("--name", default="bundle") | |
| p = sp.add_parser("unpack"); p.add_argument("bundle"); p.add_argument("-o", "--outdir", default=".") | |
| p = sp.add_parser("list"); p.add_argument("bundle") | |
| a = ap.parse_args() | |
| if a.cmd == "pack": | |
| pack(a.out, a.inputs, a.name) | |
| elif a.cmd == "unpack": | |
| unpack(a.bundle, a.outdir) | |
| else: | |
| list_bundle(a.bundle) | |
| if __name__ == "__main__": | |
| main() | |