File size: 1,554 Bytes
05e6ca1 | 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 | #!/usr/bin/env python3
"""Minimal reader for tte-cmix-residual-blocks. This is the whole API."""
import json, numpy as np
def load_block(root, bid):
M = json.load(open(f"{root}/manifest.json"))
B = next(b for b in M["blocks"] if b["id"] == bid)
n, d = M["block_bits"], f"{root}/blocks/{bid}"
X = np.fromfile(f"{d}/experts_i8.bin", np.int8).reshape(n, 25) # 25 expert stretches
X = X * np.array(M["int8_scales"], np.float32) # -> stretch (logit) domain
fp = np.fromfile(f"{d}/final_p_u16.bin", np.uint16) # cmix P(1) = fp/65536
fl = np.fromfile(f"{d}/labels_u8.bin", np.uint8)
return dict(
experts=X, # (n,25) float32 stretch
final_p=fp.astype(np.float32) / 65536.0, # (n,) cmix's own P(bit=1)
y=(fl & 1).astype(np.float32), # (n,) the coded bit <-- LABEL
lstm_override=(fl >> 1) & 1, # (n,) cmix's lstm_override flag
bitpos=np.arange(n, dtype=np.int64) % 8, # (n,) 0 = MSB ... 7 = LSB
byteoff=B["byte_start"] + np.arange(n, dtype=np.int64) // 8, # absolute corpus offset
bytes_ctx=np.fromfile(f"{d}/bytes_ctx.bin", np.uint8), # [ctx | block] raw bytes
meta=B, manifest=M,
)
if __name__ == "__main__":
import sys
b = load_block(sys.argv[1] if len(sys.argv) > 1 else ".", sys.argv[2] if len(sys.argv) > 2 else "b000")
print({k: (v.shape if hasattr(v, "shape") else "…") for k, v in b.items()})
|