mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
The reader (memmap tensors, fields), the writer (one file, byte-identical to gguf-py for the calls the tools make), the dequantizers of 25 types and the quantizers the tools use (BF16, Q8_0, Q4_0/1, Q5_0/1). The IQ grids come from strata-quant-common.h through a generator that a test re-runs. tools/test_strata_gguf.py proves the results against the vendored gguf-py and, without it, against recorded digests.
90 lines
3.8 KiB
Python
90 lines
3.8 KiB
Python
"""tools/gen_strata_quant_tables.py - writes tools/strata_quant_tables.py from the quant header the engines compile.
|
|
|
|
python tools/gen_strata_quant_tables.py (rewrites tools/strata_quant_tables.py)
|
|
python tools/gen_strata_quant_tables.py --check (exit 1 when the file is not what the header gives)
|
|
|
|
The IQ grids, the sign table and the 4-bit value tables are data of the GGUF format; the Python dequantizers (tools/strata_quants.py)
|
|
read them from tools/strata_quant_tables.py, which is generated from third_party/strata-ggml/strata-quant-common.h so the numbers are
|
|
the ones the kernels use. Each table is stored as little-endian bytes (zlib + base64), exactly the memory image of the C array.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import pathlib
|
|
import re
|
|
import sys
|
|
import zlib
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
HEADER = ROOT / "third_party" / "strata-ggml" / "strata-quant-common.h"
|
|
OUT = ROOT / "tools" / "strata_quant_tables.py"
|
|
|
|
#: (C name, element bytes, signed) -> the name in the module
|
|
TABLES = (("kmask_iq2xs", 1, False), ("ksigns_iq2xs", 1, False),
|
|
("iq2xxs_grid", 8, False), ("iq2xs_grid", 8, False), ("iq2s_grid", 8, False),
|
|
("iq3xxs_grid", 4, False), ("iq3s_grid", 4, False),
|
|
("kvalues_iq4nl", 1, True), ("kvalues_fp4", 1, True),
|
|
("iq1s_grid", 8, False))
|
|
EXPECT = {"kmask_iq2xs": 8, "ksigns_iq2xs": 128, "iq2xxs_grid": 256, "iq2xs_grid": 512, "iq2s_grid": 1024, "iq3xxs_grid": 256,
|
|
"iq3s_grid": 512, "kvalues_iq4nl": 16, "kvalues_fp4": 16, "iq1s_grid": 2048}
|
|
|
|
|
|
def table_bytes(text: str, name: str, width: int, signed: bool) -> bytes:
|
|
m = re.search(r"GGML_TABLE_BEGIN\(\w+, %s, [A-Za-z0-9_]+\)(.*?)GGML_TABLE_END\(\)" % re.escape(name), text, re.S)
|
|
if not m:
|
|
raise SystemExit("table %s not found in %s" % (name, HEADER))
|
|
body = re.sub(r"//[^\n]*|/\*.*?\*/", "", m.group(1), flags=re.S)
|
|
out = bytearray()
|
|
n = 0
|
|
for tok in re.findall(r"-?0x[0-9a-fA-F]+|-?\d+", body):
|
|
v = int(tok, 0)
|
|
if signed:
|
|
v &= (1 << (8 * width)) - 1
|
|
out += v.to_bytes(width, "little")
|
|
n += 1
|
|
if n != EXPECT[name]:
|
|
raise SystemExit("table %s: %d entries, expected %d" % (name, n, EXPECT[name]))
|
|
return bytes(out)
|
|
|
|
|
|
def render() -> str:
|
|
text = HEADER.read_text(encoding="utf-8")
|
|
lines = ['"""GENERATED by tools/gen_strata_quant_tables.py from third_party/strata-ggml/strata-quant-common.h. Do not edit.',
|
|
'',
|
|
'Each table is the little-endian memory image of the C array (zlib + base64); strata_quants.py reads them with numpy."""',
|
|
'import base64 as _b64',
|
|
'import zlib as _zlib',
|
|
'',
|
|
'', 'def _t(s):', ' return _zlib.decompress(_b64.b64decode(s))', '', '']
|
|
for name, width, signed in TABLES:
|
|
raw = table_bytes(text, name, width, signed)
|
|
blob = base64.b64encode(zlib.compress(raw, 9)).decode()
|
|
lines.append("%s = _t(" % name.upper())
|
|
for i in range(0, len(blob), 96):
|
|
lines.append(" %r" % blob[i:i + 96])
|
|
lines.append(")")
|
|
lines.append("")
|
|
return "\n".join(lines).rstrip() + "\n"
|
|
|
|
|
|
def main() -> int:
|
|
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
|
|
ap.add_argument("--check", action="store_true")
|
|
a = ap.parse_args()
|
|
want = render()
|
|
if a.check:
|
|
have = OUT.read_text(encoding="utf-8") if OUT.exists() else ""
|
|
if have != want:
|
|
print("tools/strata_quant_tables.py is out of date: run python tools/gen_strata_quant_tables.py")
|
|
return 1
|
|
print("strata_quant_tables.py: ok")
|
|
return 0
|
|
OUT.write_text(want, encoding="utf-8", newline="\n")
|
|
print("wrote %s (%d bytes)" % (OUT, len(want)))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|