File size: 1,457 Bytes
778e97e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
from gguf import GGUFReader, GGUFWriter

SRC = "/home/corov/cyber/gguf/qwen38-cyber-f16.gguf"
DST = "/home/corov/cyber/gguf/qwen38-cyber-f16-patched.gguf"

r = GGUFReader(SRC)

def scalar(f):
    vt = f.types[0]          # GGUFMetadataType ordinal
    arr = f.parts[f.data[0]]
    if vt == 8:              # STRING
        return arr.tobytes().decode("utf-8", "replace")
    if vt == 7:              # BOOL (stored as uint8)
        return bool(arr[0])
    if vt in (6, 11):        # FLOAT32/64
        return float(arr[0])
    return int(arr[0])

arch = None
patched = []
for k, f in r.fields.items():
    if f.types[0] == 12 or len(f.data) > 1:   # ARRAY: types[0]=12
        # arrays: parts[data[0]] holds the raw data; parts[data[1]] holds lengths? in gguf-py arrays are
        # stored as f.data list of (part_idx) for data; replicate via add_array with the numpy array
        arr = f.parts[f.data[0]]
        # element type ordinal kept in f.types[1]
        if k == "tokenizer.chat_template" or "tokens" in k or "scores" in k or "merges" in k or "type" in k:
            continue_compat = True
        yield_later = (k, arr)
        ARRAYS.append((k, arr))
        continue
    v = scalar(f)
    if k == "general.architecture":
        arch = v
        continue
    if "nextn_predict_layers" in k:
        print(f"patch {k}: {v} -> 0")
        v = 0
        patched.append(k)
    SCALARS.append((k, v))

ARRAYS, SCALARS = [], []