File size: 1,671 Bytes
0234c79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Build the margin-noise PoC twin v2: byte-different, functionally identical copy of the
current king. TRUE lowest-mantissa-bit flip on ONE fp16 block scale (bit-cast, not value cast).
"""
import hashlib
import shutil
import sys
from pathlib import Path

sys.path.insert(0, "/root/ingot/src")
import numpy as np

from common.gguf import read_header
from common.gguf.types import PQ2_0

KING = Path("/root/models/king-xii/Ingot-XII-27B-Q2.gguf")
OUT_DIR = Path("/root/models/chal-twin")
OUT = OUT_DIR / KING.name

g = read_header(KING)
pq = sorted(n for n, t in g.tensors.items() if t.ggml_type == PQ2_0 and n.startswith("blk.30."))
name = pq[0]
t = g.tensors[name]
n_blocks = int(np.prod(t.shape)) // 128
print(f"twin base tensor: {name} ({n_blocks} blocks)")

OUT_DIR.mkdir(parents=True, exist_ok=True)
shutil.copy(KING, OUT)
raw = np.memmap(OUT, np.uint8, "r+", g.data_start + t.offset, (n_blocks * 34,))
b = raw.reshape(-1, 34)
old_bytes = b[0, :2].copy()
old_bits = old_bytes.view("<u2")[0]
new_bits = np.uint16(old_bits ^ 0x0001)     # true bit-cast flip: lowest mantissa bit
b[0, :2] = np.asarray([new_bits], "<u2").view(np.uint8)
raw.flush()

old_val = np.asarray([old_bits], "<u2").view("<f2")[0]
new_val = np.asarray([new_bits], "<u2").view("<f2")[0]


def h(p):
    m = hashlib.sha256()
    with open(p, "rb") as f:
        for c in iter(lambda: f.read(1 << 24), b""):
            m.update(c)
    return m.hexdigest()[:16]


print(f"scale block0: {old_val:.8e} -> {new_val:.8e} (rel change {abs(new_val-old_val)/abs(old_val):.2e})")
print(f"king sha256 {h(KING)}")
print(f"twin sha256 {h(OUT)}")
print("byte-different:", h(KING) != h(OUT))
print("WROTE", OUT)