CompressedGemma commited on
Commit
74aeb5c
·
verified ·
1 Parent(s): 2e643f2

Delete dump_tensor.py

Browse files
Files changed (1) hide show
  1. dump_tensor.py +0 -97
dump_tensor.py DELETED
@@ -1,97 +0,0 @@
1
- #!/usr/bin/env python3
2
- """Dump one tensor from a source GGUF (F16/BF16/F32) as raw little-endian
3
- float32, for feeding to ./bound.
4
-
5
- Usage:
6
- python3 dump_tensor.py model-f16.gguf blk.2.attn_output.weight out.f32
7
- python3 dump_tensor.py model-f16.gguf --list
8
- """
9
- import struct, sys, os
10
- import numpy as np
11
-
12
- # Reuse the parsing helpers from the requantizer so the layout stays in sync.
13
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
14
- from hexstate_requantize import (
15
- GGUF_MAGIC, GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_BF16,
16
- TYPE_BLOCK_SIZE, TYPE_BLOCK_BYTES,
17
- read_string, read_kv_value, align_offset, bf16_to_f32, f16_to_f32,
18
- )
19
-
20
-
21
- def load(path):
22
- with open(path, 'rb') as f:
23
- assert struct.unpack('<I', f.read(4))[0] == GGUF_MAGIC, "bad GGUF magic"
24
- struct.unpack('<I', f.read(4))[0] # version
25
- n_tensors = struct.unpack('<Q', f.read(8))[0]
26
- n_kv = struct.unpack('<Q', f.read(8))[0]
27
- for _ in range(n_kv):
28
- read_string(f)
29
- read_kv_value(f, struct.unpack('<I', f.read(4))[0])
30
-
31
- infos = []
32
- for _ in range(n_tensors):
33
- name = read_string(f)
34
- n_dims = struct.unpack('<I', f.read(4))[0]
35
- dims = [struct.unpack('<Q', f.read(8))[0] for _ in range(n_dims)]
36
- ttype = struct.unpack('<I', f.read(4))[0]
37
- offset = struct.unpack('<Q', f.read(8))[0]
38
- n_el = 1
39
- for d in dims:
40
- n_el *= d
41
- bs = TYPE_BLOCK_SIZE.get(ttype, 1)
42
- bb = TYPE_BLOCK_BYTES.get(ttype, 4)
43
- infos.append({'name': name, 'dims': dims, 'type': ttype,
44
- 'offset': offset, 'n_elements': n_el,
45
- 'data_size': ((n_el + bs - 1) // bs) * bb})
46
- return infos, align_offset(f.tell())
47
-
48
-
49
- def main():
50
- if len(sys.argv) < 3:
51
- print(__doc__)
52
- sys.exit(1)
53
- gguf = sys.argv[1]
54
- infos, data_start = load(gguf)
55
-
56
- if sys.argv[2] == '--list':
57
- for ti in infos:
58
- print(f" {ti['name']:<50s} type={ti['type']:<3d} "
59
- f"dims={ti['dims']} n={ti['n_elements']:,}")
60
- return
61
-
62
- want = sys.argv[2]
63
- out = sys.argv[3] if len(sys.argv) > 3 else 'tensor.f32'
64
- ti = next((t for t in infos if t['name'] == want), None)
65
- if ti is None:
66
- print(f"no tensor named {want!r}; run with --list")
67
- sys.exit(1)
68
-
69
- with open(gguf, 'rb') as f:
70
- f.seek(data_start + ti['offset'])
71
- raw = f.read(ti['data_size'])
72
-
73
- if ti['type'] == GGML_TYPE_F32:
74
- a = np.frombuffer(raw, dtype=np.float32).copy()
75
- elif ti['type'] == GGML_TYPE_F16:
76
- a = f16_to_f32(raw, ti['n_elements'])
77
- elif ti['type'] == GGML_TYPE_BF16:
78
- a = bf16_to_f32(raw, ti['n_elements'])
79
- else:
80
- print(f"tensor is type {ti['type']} (already quantized) — "
81
- f"dump from the F16/BF16 source GGUF instead")
82
- sys.exit(1)
83
-
84
- a = np.ascontiguousarray(a[:ti['n_elements']], dtype=np.float32)
85
- a.tofile(out)
86
- sigma = float(np.sqrt(np.mean(a.astype(np.float64) ** 2)))
87
- v = a.astype(np.float64) ** 2
88
- kurt = float(np.mean(v * v) / (np.mean(v) ** 2)) if np.mean(v) > 0 else 3.0
89
- print(f" wrote {out}: {a.size:,} elements, dims={ti['dims']}")
90
- print(f" sigma = {sigma:.6e}")
91
- print(f" kurtosis = {kurt:.2f} (gaussian = 3)")
92
- print()
93
- print(f" next: ./bound --file {out}")
94
-
95
-
96
- if __name__ == '__main__':
97
- main()