Upload scripts/dcpm_index.py with huggingface_hub
Browse files- scripts/dcpm_index.py +57 -0
scripts/dcpm_index.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Parse a DCPM metadata.bin index. Stage 1: read only, write nothing.
|
| 3 |
+
|
| 4 |
+
Record layout, decoded from the bytes:
|
| 5 |
+
int64 name_len | name | int64 ndim | int64 shape[ndim]
|
| 6 |
+
int32 dtype | int32 ? | int64 ? | int32 ? | int64 file_len | file
|
| 7 |
+
int64 offset | ... trailing fields
|
| 8 |
+
We locate records by scanning for the length-prefixed names rather than
|
| 9 |
+
assuming a fixed stride, so a variable-length shape list cannot desync us.
|
| 10 |
+
"""
|
| 11 |
+
import struct, sys, re, json
|
| 12 |
+
|
| 13 |
+
def parse(path):
|
| 14 |
+
b = open(path, "rb").read()
|
| 15 |
+
assert b[:4] == b"DCPM", f"not DCPM: {b[:4]!r}"
|
| 16 |
+
recs = []
|
| 17 |
+
for m in re.finditer(rb"param_(\d{6})", b):
|
| 18 |
+
i = m.start()
|
| 19 |
+
nlen = struct.unpack_from("<q", b, i - 8)[0]
|
| 20 |
+
if nlen != m.end() - i: # name must be length-prefixed
|
| 21 |
+
continue
|
| 22 |
+
j = m.end()
|
| 23 |
+
ndim = struct.unpack_from("<q", b, j)[0]; j += 8
|
| 24 |
+
if not (0 <= ndim <= 8): continue
|
| 25 |
+
shape = list(struct.unpack_from("<" + "q" * ndim, b, j)); j += 8 * ndim
|
| 26 |
+
dtype = struct.unpack_from("<i", b, j)[0]; j += 4
|
| 27 |
+
f1 = struct.unpack_from("<i", b, j)[0]; j += 4
|
| 28 |
+
f2 = struct.unpack_from("<q", b, j)[0]; j += 8
|
| 29 |
+
f3 = struct.unpack_from("<i", b, j)[0]; j += 4
|
| 30 |
+
flen = struct.unpack_from("<q", b, j)[0]; j += 8
|
| 31 |
+
fname = b[j:j + flen].decode(errors="replace"); j += flen
|
| 32 |
+
off = struct.unpack_from("<q", b, j)[0]; j += 8
|
| 33 |
+
recs.append(dict(idx=int(m.group(1)), name=m.group(0).decode(), ndim=ndim,
|
| 34 |
+
shape=shape, dtype=dtype, f1=f1, f2=f2, f3=f3,
|
| 35 |
+
file=fname, offset=off))
|
| 36 |
+
recs.sort(key=lambda r: r["idx"])
|
| 37 |
+
return recs
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
recs = parse(sys.argv[1])
|
| 41 |
+
print(f" parsed {len(recs)} param records")
|
| 42 |
+
dts = {}
|
| 43 |
+
for r in recs: dts.setdefault(r["dtype"], 0); dts[r["dtype"]] += 1
|
| 44 |
+
print(f" dtype codes seen: {dts}")
|
| 45 |
+
files = {}
|
| 46 |
+
for r in recs: files.setdefault(r["file"], 0); files[r["file"]] += 1
|
| 47 |
+
print(f" source files: {files}")
|
| 48 |
+
print(f"\n {'idx':>4} {'shape':<20} {'dtype':>5} {'file':<16} {'offset':>14}")
|
| 49 |
+
for r in recs[:14]:
|
| 50 |
+
print(f" {r['idx']:>4} {str(r['shape']):<20} {r['dtype']:>5} {r['file']:<16} {r['offset']:>14,}")
|
| 51 |
+
print(" ...")
|
| 52 |
+
for r in recs[-6:]:
|
| 53 |
+
print(f" {r['idx']:>4} {str(r['shape']):<20} {r['dtype']:>5} {r['file']:<16} {r['offset']:>14,}")
|
| 54 |
+
tot = sum(int(__import__("math").prod(r["shape"])) for r in recs)
|
| 55 |
+
print(f"\n total elements across all params: {tot:,}")
|
| 56 |
+
json.dump(recs, open("/tmp/dcpm_index.json", "w"))
|
| 57 |
+
print(" wrote /tmp/dcpm_index.json")
|