okami.mom/strata

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

Niko1221Merge f-p4-split e22f397d0 into f-p4-intc7d11f789

main
3.6 KiB81 linesraw
1#!/usr/bin/env python3
2"""P4-SPLIT: check that binaries built without llama.cpp's MMQ prompt kernels (-DSTRATA_MMQ=OFF) hold none of them.
3
4usage: check_no_mmq.py [--nm NM] [--cuobjdump CUOBJDUMP] <binary or library> ...
5
6For every file, three looks, each one where the tool for it exists:
7  - the raw bytes: the names and messages only MMQ code carries (kernel names stay readable in an uncompressed fatbin or code object, and
8    a Windows .exe keeps the strings even without a symbol table);
9  - the symbol table (nm -C; on Linux and the Mac): host functions and the device stubs of launched kernels;
10  - the device functions of a CUDA binary (cuobjdump -sass, which also opens a compressed fatbin).
11Exit 0 when nothing is found, 1 with the hits listed (at most 20 per look), 2 on a usage error.
12Strata's own prompt helpers keep the namespace strata::prefill::mmq (gather_native, swiglu, iota, ...): that name is not a marker."""
13import argparse
14import os
15import re
16import shutil
17import subprocess
18import sys
19
20# llama.cpp's MMQ: the kernels and their templates (mul_mat_q, mul_mat_q_stream_k_fixup, load_tiles_*, vec_dot_*_mma / _dp4a), the q8_1
21# quantizers (quantize_mmq_q8_1, quantize_scatter_mmq_q8_1), ggml-cuda's host side (ggml_cuda_*, ggml_backend_cuda_context) and Strata's MMQ
22# glue (ggml_cuda_host.cu's error text, the K-quant library's file name)
23RAW = [b"mul_mat_q", b"quantize_mmq_q8_1", b"quantize_scatter_mmq", b"ggml_cuda_", b"ggml_backend_cuda", b"ggml-cuda (strata mmq)",
24       b"libstrata_kq", b"strata_kq.dll", b"load_tiles_", b"mmq_get_"]
25SYM = re.compile(r"mul_mat_q|quantize_mmq_q8_1|quantize_scatter_mmq|ggml_cuda_|ggml_backend_cuda|load_tiles_|mmq_get_|vec_dot_\w+_(mma|dp4a)")
26
27
28def raw_hits(path):
29    data = open(path, "rb").read()
30    hits = []
31    for m in RAW:
32        n = data.count(m)
33        if n:
34            hits.append("%s x%d" % (m.decode(), n))
35    return hits
36
37
38def tool_lines(cmd):
39    try:
40        r = subprocess.run(cmd, capture_output=True, text=True, errors="replace", timeout=900)
41    except (OSError, subprocess.TimeoutExpired) as e:
42        return None, str(e)
43    return r.stdout.splitlines(), None
44
45
46def main():
47    ap = argparse.ArgumentParser()
48    ap.add_argument("--nm", default=shutil.which("nm"))
49    ap.add_argument("--cuobjdump", default=shutil.which("cuobjdump"))
50    ap.add_argument("files", nargs="+")
51    a = ap.parse_args()
52    bad = 0
53    for f in a.files:
54        if not os.path.isfile(f):
55            print("%s: missing" % f)
56            return 2
57        looks = []
58        h = raw_hits(f)
59        looks.append(("bytes", h))
60        if a.nm:
61            lines, err = tool_lines([a.nm, "-C", f])
62            if lines is not None:
63                looks.append(("nm", sorted({ln.split(None, 2)[-1] for ln in lines if SYM.search(ln)})))
64        if a.cuobjdump:
65            lines, err = tool_lines([a.cuobjdump, "-sass", f])
66            if lines is not None:
67                names = [m.group(1) for ln in lines for m in [re.match(r"\s*Function : (\S+)", ln)] if m]
68                if names:
69                    looks.append(("cuobjdump (%d device functions)" % len(names), sorted({n for n in names if SYM.search(n)})))
70        found = [(k, v) for k, v in looks if v]
71        print("%s: %s" % (f, "; ".join("%s %s" % (k, "CLEAN" if not v else "%d HITS" % len(v)) for k, v in looks)))
72        for k, v in found:
73            for x in v[:20]:
74                print("    %s: %s" % (k, x[:200]))
75        bad += bool(found)
76    print("no MMQ symbol in %d of %d files" % (len(a.files) - bad, len(a.files)))
77    return 1 if bad else 0
78
79
80if __name__ == "__main__":
81    sys.exit(main())