tchbcb commited on
Commit
e0cdbff
·
verified ·
1 Parent(s): 3ba280a

surgery v2 (public mirror for colab)

Browse files
artifacts/m15_scripts/r12_surgery.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """r12_surgery.py — f16 元数据手术 v2 路线 (坑#58: 自写 walk 定位 + GGUFReader 复验双保险)
4
+ 用法: python3 r12_surgery.py <f16_path>
5
+ 补丁: qwen35.nextn_predict_layers -> 0, qwen35.block_count -> 32
6
+ 产物标记: R12_SURGERY_V2_DONE"""
7
+ import struct, json, hashlib, sys
8
+
9
+ PATH = sys.argv[1] if len(sys.argv) > 1 else "/tmp/k8b/m13_r10_f16.gguf"
10
+ SZT = {0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8}
11
+ FMT = {0: "<B", 1: "<b", 2: "<H", 3: "<h", 4: "<I", 5: "<i", 6: "<f", 7: "<B", 10: "<Q", 11: "<q", 12: "<d"}
12
+ T_STR, T_ARR = 8, 9
13
+ out = {"path": PATH}
14
+
15
+ def walk(fh):
16
+ fh.seek(0)
17
+ assert fh.read(4) == b"GGUF"
18
+ ver, n_ten, n_kv = struct.unpack("<IQQ", fh.read(20))
19
+ idx = {}
20
+ for _ in range(n_kv):
21
+ n = struct.unpack("<Q", fh.read(8))[0]
22
+ key = fh.read(n).decode("utf-8", "replace")
23
+ vtype = struct.unpack("<I", fh.read(4))[0]
24
+ if vtype == T_STR:
25
+ n2 = struct.unpack("<Q", fh.read(8))[0]
26
+ fh.seek(n2, 1); continue
27
+ if vtype == T_ARR:
28
+ etype = struct.unpack("<I", fh.read(4))[0]
29
+ cnt_off = fh.tell()
30
+ cnt = struct.unpack("<Q", fh.read(8))[0]
31
+ if etype == T_STR:
32
+ for _ in range(cnt):
33
+ ln = struct.unpack("<Q", fh.read(8))[0]
34
+ fh.seek(ln, 1)
35
+ else:
36
+ fh.seek(cnt * SZT[etype], 1)
37
+ idx[key] = (vtype, etype, cnt, cnt_off, fh.tell() if etype != T_STR else cnt_off)
38
+ continue
39
+ fh.seek(SZT[vtype], 1)
40
+ idx[key] = (vtype, None, None, None, fh.tell() - SZT[vtype])
41
+ return idx
42
+
43
+ fh = open(PATH, "r+b")
44
+ idx = walk(fh)
45
+ out["bc_found"] = "qwen35.block_count" in idx
46
+ out["nx_found"] = "qwen35.nextn_predict_layers" in idx
47
+ missing = [k for k in ("qwen35.block_count", "qwen35.nextn_predict_layers") if k not in idx]
48
+ if missing:
49
+ out["R12_SURGERY_V2_DONE"] = False
50
+ out["error"] = "keys absent: %s" % missing
51
+ print(json.dumps(out, ensure_ascii=False)); sys.exit(1)
52
+ for key, newv in (("qwen35.nextn_predict_layers", 0), ("qwen35.block_count", 32)):
53
+ vtype, _, _, _, off = idx[key]
54
+ fh.seek(off)
55
+ old = struct.unpack(FMT[vtype], fh.read(4))[0]
56
+ fh.seek(off)
57
+ fh.write(struct.pack(FMT[vtype], newv))
58
+ out["patch_" + key] = "%s->%s" % (old, newv)
59
+ fh.flush()
60
+ import os
61
+ os.fsync(fh.fileno())
62
+ fh.close()
63
+
64
+ import gguf
65
+ r = gguf.GGUFReader(PATH)
66
+ bcv = int(r.fields["qwen35.block_count"].parts[-1][0])
67
+ nxv = int(r.fields["qwen35.nextn_predict_layers"].parts[-1][0])
68
+ out["verify"] = "bc=%d nx=%d tensors=%d" % (bcv, nxv, len(r.tensors))
69
+ ok = bcv == 32 and nxv == 0 and len(r.tensors) > 400
70
+ out["R12_SURGERY_V2_DONE"] = bool(ok)
71
+ print(json.dumps(out, ensure_ascii=False))