Upload read_blocks.py with huggingface_hub
Browse files- read_blocks.py +27 -0
read_blocks.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Minimal reader for tte-cmix-residual-blocks. This is the whole API."""
|
| 3 |
+
import json, numpy as np
|
| 4 |
+
|
| 5 |
+
def load_block(root, bid):
|
| 6 |
+
M = json.load(open(f"{root}/manifest.json"))
|
| 7 |
+
B = next(b for b in M["blocks"] if b["id"] == bid)
|
| 8 |
+
n, d = M["block_bits"], f"{root}/blocks/{bid}"
|
| 9 |
+
X = np.fromfile(f"{d}/experts_i8.bin", np.int8).reshape(n, 25) # 25 expert stretches
|
| 10 |
+
X = X * np.array(M["int8_scales"], np.float32) # -> stretch (logit) domain
|
| 11 |
+
fp = np.fromfile(f"{d}/final_p_u16.bin", np.uint16) # cmix P(1) = fp/65536
|
| 12 |
+
fl = np.fromfile(f"{d}/labels_u8.bin", np.uint8)
|
| 13 |
+
return dict(
|
| 14 |
+
experts=X, # (n,25) float32 stretch
|
| 15 |
+
final_p=fp.astype(np.float32) / 65536.0, # (n,) cmix's own P(bit=1)
|
| 16 |
+
y=(fl & 1).astype(np.float32), # (n,) the coded bit <-- LABEL
|
| 17 |
+
lstm_override=(fl >> 1) & 1, # (n,) cmix's lstm_override flag
|
| 18 |
+
bitpos=np.arange(n, dtype=np.int64) % 8, # (n,) 0 = MSB ... 7 = LSB
|
| 19 |
+
byteoff=B["byte_start"] + np.arange(n, dtype=np.int64) // 8, # absolute corpus offset
|
| 20 |
+
bytes_ctx=np.fromfile(f"{d}/bytes_ctx.bin", np.uint8), # [ctx | block] raw bytes
|
| 21 |
+
meta=B, manifest=M,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
import sys
|
| 26 |
+
b = load_block(sys.argv[1] if len(sys.argv) > 1 else ".", sys.argv[2] if len(sys.argv) > 2 else "b000")
|
| 27 |
+
print({k: (v.shape if hasattr(v, "shape") else "…") for k, v in b.items()})
|