Download verify_dequant.py from HelloSun/openPangu-2.0-Flash-base-Q3_K_M_EPQ: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/HelloSun/openPangu-2.0-Flash-base-Q3_K_M_EPQ/resolve/main/verify_dequant.py
- Command line
-
hf download hf://HelloSun/openPangu-2.0-Flash-base-Q3_K_M_EPQ/verify_dequant.py
-
curl -L -o verify_dequant.py https://huggingface.co/HelloSun/openPangu-2.0-Flash-base-Q3_K_M_EPQ/resolve/main/verify_dequant.py
2.01 kB
| #!/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()) | |