okami.mom/strata

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

Niko1221Merge f-own-2b (cc8c29f31) into f-own-int: our own GGUF reader/writer and quantizers (gguf-py out of the pack tools)0f1e5e4cb

main
3.8 KiB79 linesraw
1"""tools/_paths.py - where the tools find a GGUF library.
2
3The preparation tools use Strata's own reader/writer and quantizers (tools/strata_gguf.py, tools/strata_quants.py): gguf_lib() returns it.
4$STRATA_GGUF_PY (a folder that holds llama.cpp's `gguf` package) still selects that package instead, for two releases (the byte-identical
5A/B of the pack tools runs both); llama.cpp's HF converter (NVFP4-local) imports gguf-py itself and always uses the vendored copy.
6
7Where gguf-py is looked for (gguf_py(), add_gguf_py()), in order: $STRATA_GGUF_PY (a folder that holds the `gguf` package, e.g. <llama.cpp>/gguf-py), then the vendored copy
8<engine>/third_party/strata-gguf-py (frozen at the pinned llama.cpp commit, no download), then the development tree's
9../../.ref/llama.cpp/gguf-py, then (#3269) the install folders: in the Control package setup.py's per-user install folder
10($STRATA_INSTALL_DIR, else the app's folder, the Terminal's remembered installs), so a tool run by hand from the package finds it there.
11"""
12from __future__ import annotations
13
14import os
15import sys
16from pathlib import Path
17
18ENGINE = Path(__file__).resolve().parents[1]
19VENDORED = ENGINE / "third_party" / "strata-gguf-py"       # holds gguf/ (the package) and converter/ (llama.cpp's HF converter)
20
21
22def _install_dirs() -> list[Path]:
23    """The per-user install folders a Control-mode setup.py works in. Best effort: never raises."""
24    out: list[Path] = []
25    raw = os.environ.get("STRATA_INSTALL_DIR")
26    if raw:
27        out.append(Path(raw).expanduser())
28    try:
29        if str(ENGINE) not in sys.path:
30            sys.path.append(str(ENGINE))
31        from strata_control import discovery
32        out.append(discovery.app_install_dir())
33        out += [pl.path for pl in discovery.places(here=ENGINE)]
34    except Exception:
35        pass
36    return out
37
38
39def gguf_py() -> str:
40    cands = [os.environ.get("STRATA_GGUF_PY"), ENGINE / "third_party" / "strata-gguf-py",
41             ENGINE.parents[min(1, len(ENGINE.parents) - 1)] / ".ref" / "llama.cpp" / "gguf-py"]   # the dev tree; a shallow install (G:\Strata) has no grandparent
42    cands += [d / "third_party" / "strata-gguf-py" for d in _install_dirs()]
43    for c in cands:
44        if c and (Path(c) / "gguf").is_dir():
45            return str(c)
46    sys.exit("gguf-py was not found. It ships with Strata in third_party/strata-gguf-py; set STRATA_GGUF_PY to the folder that holds the "
47             "`gguf` package (for example <llama.cpp>/gguf-py) to use another copy")
48
49
50def gguf_lib():
51    """The GGUF library the preparation tools use, with gguf-py's names (GGMLQuantizationType, GGUFReader, GGUFWriter, GGUFValueType, quants):
52    llama.cpp's gguf-py when STRATA_GGUF_PY names a folder that holds it (kept for two releases), else tools/strata_gguf.py."""
53    own = os.environ.get("STRATA_GGUF_PY")
54    if own:
55        if not (Path(own) / "gguf").is_dir():
56            sys.exit("STRATA_GGUF_PY=%s does not hold a `gguf` package" % own)
57        if own not in sys.path:
58            sys.path.insert(0, own)
59        import gguf
60        return gguf
61    here = str(Path(__file__).resolve().parent)
62    if here not in sys.path:
63        sys.path.insert(0, here)
64    import strata_gguf
65    return strata_gguf
66
67
68def add_gguf_py() -> None:
69    p = gguf_py()
70    if p not in sys.path:
71        sys.path.insert(0, p)
72
73
74def converter_dir() -> str:
75    """llama.cpp's Hugging Face converter (convert_hf_to_gguf.py and conversion/), vendored beside gguf-py (the NVFP4-local preparation)."""
76    for c in (ENGINE / "third_party" / "strata-gguf-py" / "converter", *[d / "third_party" / "strata-gguf-py" / "converter" for d in _install_dirs()]):
77        if (c / "convert_hf_to_gguf.py").is_file():
78            return str(c)
79    sys.exit("llama.cpp's converter was not found (third_party/strata-gguf-py/converter)")