SPIRA / python /scripts /rebuild_am.py
thefinalboss's picture
SPIRA v2: entropy-patch tokenization + hierarchical dopamine-gated spiking cortex + hippocampal one-shot memory with replay consolidation
032a961 verified
Raw History Blame Contribute Delete
1.77 kB
#!/usr/bin/env python3
"""Rebuild the corpus hippocampal AM with the FIXED save format.
The pre-fix checkpoints packed/rebuilt AM entries at wrong addresses
(load wrote at k[lane]%size instead of the derivable primary/LOO slots),
making the loaded corpus AM invisible to queries. This script resets the AM,
re-runs the corpus write pass in-memory, and saves with the corrected
packing (dedup at derivable slots).
"""
import sys
import time
sys.path.insert(0, "/home/z/my-project/download/spira")
import numpy as np
from spira import SPIRA
from spira.hippocampus import Hippocampus
BASE = "/home/z/my-project"
CKPT = f"{BASE}/download/spira/artifacts/spira_model.npz"
m = SPIRA.load(CKPT)
print(f"loaded (old AM: {m.am.stats()})")
m.am = Hippocampus(m=m.cfg.am_m, size=m.cfg.am_size, min_lanes=m.cfg.am_min_lanes)
data = open(f"{BASE}/data/train.bin", "rb").read()
patches = m.ept.segment(data)
ids = np.fromiter((__import__("spira.hashing", fromlist=["fnv1a64"]).fnv1a64(p)
for p in patches), dtype=np.uint64, count=len(patches))
t0 = time.perf_counter()
taught = m.teach_ids(ids[:1_200_000], episodic=False)
print(f"corpus AM write: {taught['associations']} associations in "
f"{time.perf_counter()-t0:.0f}s -> {m.am.stats()}")
t0 = time.perf_counter()
m.save(CKPT)
print(f"saved (fixed format) in {time.perf_counter()-t0:.0f}s")
# roundtrip validation
m2 = SPIRA.load(CKPT)
st = m2.am.stats()
print(f"reloaded AM: {st}")
v, w = m2.am.query(m2.am.lane_keys(ids[100:104]))
print(f"query ctx[100:104] -> match={v == ids[104]} (w={w:.2f})")
hits = 0
for i in range(500, 1000):
v, w = m2.am.query(m2.am.lane_keys(ids[i : i + 4]))
hits += int(v == ids[i + 4])
print(f"roundtrip retrieval on 500 corpus contexts: {hits/5:.1f}%")