eyad-silx commited on
Commit
0234c79
·
verified ·
1 Parent(s): c40f32d

push artifacts/scripts/make_twin.py

Browse files
Files changed (1) hide show
  1. artifacts/scripts/make_twin.py +52 -0
artifacts/scripts/make_twin.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Build the margin-noise PoC twin v2: byte-different, functionally identical copy of the
2
+ current king. TRUE lowest-mantissa-bit flip on ONE fp16 block scale (bit-cast, not value cast).
3
+ """
4
+ import hashlib
5
+ import shutil
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ sys.path.insert(0, "/root/ingot/src")
10
+ import numpy as np
11
+
12
+ from common.gguf import read_header
13
+ from common.gguf.types import PQ2_0
14
+
15
+ KING = Path("/root/models/king-xii/Ingot-XII-27B-Q2.gguf")
16
+ OUT_DIR = Path("/root/models/chal-twin")
17
+ OUT = OUT_DIR / KING.name
18
+
19
+ g = read_header(KING)
20
+ pq = sorted(n for n, t in g.tensors.items() if t.ggml_type == PQ2_0 and n.startswith("blk.30."))
21
+ name = pq[0]
22
+ t = g.tensors[name]
23
+ n_blocks = int(np.prod(t.shape)) // 128
24
+ print(f"twin base tensor: {name} ({n_blocks} blocks)")
25
+
26
+ OUT_DIR.mkdir(parents=True, exist_ok=True)
27
+ shutil.copy(KING, OUT)
28
+ raw = np.memmap(OUT, np.uint8, "r+", g.data_start + t.offset, (n_blocks * 34,))
29
+ b = raw.reshape(-1, 34)
30
+ old_bytes = b[0, :2].copy()
31
+ old_bits = old_bytes.view("<u2")[0]
32
+ new_bits = np.uint16(old_bits ^ 0x0001) # true bit-cast flip: lowest mantissa bit
33
+ b[0, :2] = np.asarray([new_bits], "<u2").view(np.uint8)
34
+ raw.flush()
35
+
36
+ old_val = np.asarray([old_bits], "<u2").view("<f2")[0]
37
+ new_val = np.asarray([new_bits], "<u2").view("<f2")[0]
38
+
39
+
40
+ def h(p):
41
+ m = hashlib.sha256()
42
+ with open(p, "rb") as f:
43
+ for c in iter(lambda: f.read(1 << 24), b""):
44
+ m.update(c)
45
+ return m.hexdigest()[:16]
46
+
47
+
48
+ print(f"scale block0: {old_val:.8e} -> {new_val:.8e} (rel change {abs(new_val-old_val)/abs(old_val):.2e})")
49
+ print(f"king sha256 {h(KING)}")
50
+ print(f"twin sha256 {h(OUT)}")
51
+ print("byte-different:", h(KING) != h(OUT))
52
+ print("WROTE", OUT)