File size: 2,005 Bytes
00d4f8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""NumPy 反量化器 vs ggml 自帶反量化器的逐位元組交叉檢查。

`ggml_deq <type> <gguf> <offset> <n>` 用 ggml 的 dequantize_row_* 傾印一列;
本腳本把它與 verify_epq.py 使用的 NumPy 實作比對。
"""
import importlib.util
import os
import subprocess
import sys

import numpy as np

_HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location('vl', os.path.join(_HERE, 'verify_epq.py'))
vl = importlib.util.module_from_spec(spec)
spec.loader.exec_module(vl)

GGML_DEQ = os.environ.get(
    'EPQ_GGML_DEQ', os.path.join(_HERE, '..', 'llama.cpp', 'build', 'bin', 'ggml_deq'))


def main():
    kv, ts = vl.parse_headers()
    # openPangu Q3_K_M:gate/up 為 Q3_K,down 為 Q4_K。
    cases = []
    for il in (2, 5, 20, 45):
        for name in ('ffn_gate_exps.weight', 'ffn_up_exps.weight',
                     'ffn_down_exps.weight'):
            cases.append((il, name, ts[f'blk.{il}.{name}'][3]))
    all_ok = True
    for il, name, tt in cases:
        si, off, dims, t = ts[f'blk.{il}.{name}']
        assert t == tt, (name, t, tt)
        nrow = vl.row_bytes(dims[0], tt)
        ne0 = dims[0]
        for ie in (0, 17, 255):
            eo = off + ie * nrow * dims[1]
            out = subprocess.run([GGML_DEQ, str(tt), vl.SHARDS[si], str(eo), str(ne0)],
                                 capture_output=True, check=True)
            ref = np.frombuffer(out.stdout, dtype=np.float32)
            with open(vl.SHARDS[si], 'rb') as f:
                f.seek(eo)
                mine = vl.dequant(f.read(nrow), ne0, tt)
            d = float(np.abs(ref - mine).max())
            ok = d == 0.0
            all_ok &= ok
            print(f'blk.{il}.{name} E{ie} type={vl.NAME.get(tt)} n={ne0}: '
                  f'max|ggml - numpy| = {d:g}  {"PASS" if ok else "FAIL"}')
    print('\nALL PASS' if all_ok else '\nSOME FAILED')
    return 0 if all_ok else 1


if __name__ == '__main__':
    sys.exit(main())