okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0214-int 437668151 (round 0214: the new Unsloth Qwen quants, the Q5_0 dot fix, the Mac app branch) into f-0211-rc3e0210663

main
4.0 KiB87 linesraw
1"""Header-only fixtures of Unsloth's Qwen3.8-Flash-Next sets at revision d9620119 (2026-11-04).
2
3tests/fixtures/unsloth_v31/headers.json.gz holds, for every set of the repository, each shard's pinned size and SHA-256 and, for
4the shards that carry tensors, the split keys and the real tensor directory (name, ggml type id, shape, offset) read from the
5first 4 MB of the file with an HTTP range request.  No weights are in it.  `materialize` writes a shard back as a GGUF that holds
6only the header (the file ends where the data would begin), which gguf_reader.GGUFFile and tools/iq_pack.py's layout code read.
7
8  python tools/unsloth_v31_fixture.py build <folder of the 4 MB heads, named SET__file.gguf> <unsloth-head-pins.json> <out.json.gz>
9"""
10from __future__ import annotations
11
12import gzip
13import json
14import pathlib
15import struct
16import sys
17
18HERE = pathlib.Path(__file__).resolve().parent
19sys.path.insert(0, str(HERE))
20import gguf_reader as G                                       # noqa: E402
21
22FIXTURE = HERE.parent / "tests" / "fixtures" / "unsloth_v31" / "headers.json.gz"
23
24
25def load(path: pathlib.Path = FIXTURE) -> dict:
26    with gzip.open(path, "rt", encoding="utf-8") as f:
27        return json.load(f)
28
29
30def build(heads: pathlib.Path, pins: pathlib.Path, out: pathlib.Path) -> dict:
31    p = json.loads(pathlib.Path(pins).read_text(encoding="utf-8"))
32    sets: dict = {}
33    for key, v in p["files"].items():
34        folder, _, name = key.partition("/")
35        if not name or not name.endswith(".gguf") or "-of-" not in name:
36            continue
37        shard = {"name": name, "bytes": v["bytes"], "sha256": v["sha256"], "header": None}
38        head = pathlib.Path(heads) / key.replace("/", "__")
39        if head.exists():
40            try:
41                g = G.GGUFFile(head)
42            except ValueError:
43                g = None                                      # shard 1: metadata only, longer than the 4 MB read
44            if g is not None:
45                shard["header"] = {"split": [g.metadata.get("split.no"), g.metadata.get("split.count"),
46                                             g.metadata.get("split.tensors.count")],
47                                   "data_start": g.data_start,
48                                   "tensors": [[t.name, t.type_id, t.shape, t.offset] for t in g.tensors]}
49        sets.setdefault(folder, {"revision": p["revision"], "shards": []})["shards"].append(shard)
50    doc = {"repo": p["repo"], "revision": p["revision"], "sets": sets}
51    with gzip.open(out, "wt", encoding="utf-8", compresslevel=9) as f:
52        json.dump(doc, f, separators=(",", ":"), sort_keys=True)
53    return doc
54
55
56def _str(s: str) -> bytes:
57    b = s.encode()
58    return struct.pack("<Q", len(b)) + b
59
60
61def materialize(shard: dict, path: pathlib.Path, arch: str = "qwen4exp", first: bool = False) -> pathlib.Path:
62    """A GGUF v3 file with the shard's tensor directory and no data: it ends at the aligned start of the data section."""
63    h = shard["header"]
64    no, count, total = h["split"]
65    kv = []
66    if first:
67        kv.append(_str("general.architecture") + struct.pack("<I", 8) + _str(arch))
68    for k, v in (("split.no", no), ("split.count", count)):
69        kv.append(_str(k) + struct.pack("<IH", 2, v))
70    kv.append(_str("split.tensors.count") + struct.pack("<Ii", 5, total))
71    out = bytearray(struct.pack("<IIQQ", G.GGUF_MAGIC, 3, len(h["tensors"]), len(kv)))
72    for e in kv:
73        out += e
74    for name, type_id, shape, offset in h["tensors"]:
75        out += _str(name) + struct.pack("<I", len(shape)) + struct.pack(f"<{len(shape)}Q", *shape) + struct.pack("<IQ", type_id, offset)
76    align = 32
77    out += b"\0" * ((-len(out)) % align)
78    pathlib.Path(path).write_bytes(bytes(out))
79    return pathlib.Path(path)
80
81
82if __name__ == "__main__":
83    if len(sys.argv) == 5 and sys.argv[1] == "build":
84        d = build(pathlib.Path(sys.argv[2]), pathlib.Path(sys.argv[3]), pathlib.Path(sys.argv[4]))
85        print(len(d["sets"]), "sets")
86    else:
87        sys.exit(__doc__)