-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgb_gguf_tensor_map.py
More file actions
executable file
·64 lines (47 loc) · 1.94 KB
/
Copy pathgb_gguf_tensor_map.py
File metadata and controls
executable file
·64 lines (47 loc) · 1.94 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
#!/usr/bin/env python3
"""
gb_gguf_tensor_map.py , ground-truth (name, n_bytes) tensor list for a .gguf file.
Deliberately reuses the upstream llama.cpp GGUFReader (vendored at
greenboost-sources/llama.cpp/gguf-py) instead of re-deriving GGUF binary
parsing by hand , this is a one-off offline inspection helper, not a
runtime dependency of the shim, so correctness from reusing a known-good
parser outweighs the minor coupling to the vendored tree's layout.
Also exposes _load_gguf_reader() / tensor_map(), imported by gb_synapse.py
for GGUF metadata summaries.
Usage:
python3 gb_gguf_tensor_map.py /path/to/model.gguf
python3 gb_gguf_tensor_map.py /path/to/model.gguf --moe-only
"""
from __future__ import annotations
import sys
from pathlib import Path
_VENDORED_GGUF_PY = Path(__file__).parent.parent / "greenboost-sources" / "llama.cpp" / "gguf-py"
def _load_gguf_reader():
if str(_VENDORED_GGUF_PY) not in sys.path:
sys.path.insert(0, str(_VENDORED_GGUF_PY))
from gguf.gguf_reader import GGUFReader # noqa: E402
return GGUFReader
def tensor_map(gguf_path: str) -> list[tuple[str, int]]:
"""Return [(tensor_name, n_bytes), ...] in on-disk declaration order."""
GGUFReader = _load_gguf_reader()
reader = GGUFReader(gguf_path, mode="r")
return [(t.name, int(t.n_bytes)) for t in reader.tensors]
def main() -> int:
if len(sys.argv) < 2:
print(__doc__)
return 2
args = sys.argv[1:]
path = args[0]
moe_only = "--moe-only" in args
entries = tensor_map(path)
total = sum(n for _, n in entries)
print(f"# {path}")
print(f"# {len(entries)} tensors, {total / (1024**3):.2f} GiB total")
print(f"#{'name':<48} {'bytes':>14} {'MiB':>10}")
for name, n_bytes in entries:
if moe_only and "_exps" not in name:
continue
print(f"{name:<48} {n_bytes:>14} {n_bytes / (1024**2):>10.2f}")
return 0
if __name__ == "__main__":
raise SystemExit(main())