Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .claude/skills/zerolm-roadmap/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Invariants: see `AGENTS.md` and `docs/research-constitution.md`.

## Phase 5 — Weight-resident associative memory

> **Implemented:** `crates/zerolm-memory` (`checkpoint.rs` atomic format + recovery, `encoder.rs`, `retrieval.rs`, `fast_weights.rs`, `persistent_memory.rs`), frozen spec `model/specs/memory.json`, benchmark `zerolm-memory` (Rust, no Python), CLI `zerolm memory --dir`, `docs/memory-format.md`. Retrieval is programmed (pooled attention + per-word coverage verification), not learned. Tune only on pilot seeds; keep the gate green.
> **Implemented:** `crates/zerolm-memory` (`checkpoint.rs` atomic format + recovery, `encoder.rs`, `persistent_memory.rs`; the former `retrieval.rs` and `fast_weights.rs` are now the compiled value-codec and W_FAST circuits, `value_circuits.rs`), frozen spec `model/specs/memory.json`, benchmark `zerolm-memory` (Rust, no Python), CLI `zerolm memory --dir`, `docs/memory-format.md`. Retrieval is programmed (pooled attention + per-word coverage verification), not learned. Tune only on pilot seeds; keep the gate green.

**Goal:** Give the neural model persistent memory without transcript replay or an external semantic database.

Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ cargo run --release -p zerolm-eval --bin zerolm-online-lm # online-learning ga
cargo run --release -p zerolm-eval --bin zerolm-memory # memory gate, incl. reference-interpreter parity (~7 min)
cargo run --release -p zerolm-eval --bin zerolm-reasoning # reasoning gate (~2 min)
cargo run -p zerolm-wsp -- check # compiled circuits match model/prog/pinned.json
cargo run --release -p zerolm-eval --bin zerolm-uniform # uniform-engine gate: reference replay of every circuit run, host audit (~2 min)
cargo run --release -p zerolm-eval --bin zerolm-uniform # uniform-engine gate: reference replay of every circuit run, host audit (~4 min)
```
All of these must pass before every commit. In Docker: `docker compose run --rm gates` (all but baseline provenance, which needs a clean git checkout); guide in `docs/getting-started.md`.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ cargo run --release -p zerolm-eval --bin zerolm-online-lm
cargo run --release -p zerolm-eval --bin zerolm-memory # memory (~7 min)
cargo run --release -p zerolm-eval --bin zerolm-reasoning # reasoning
cargo run -p zerolm-wsp -- check # compiled circuits
cargo run --release -p zerolm-eval --bin zerolm-uniform # uniform engine (~2 min)
cargo run --release -p zerolm-eval --bin zerolm-uniform # uniform engine (~4 min)
```

## License
Expand Down
53 changes: 22 additions & 31 deletions crates/zerolm-engine/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
//! identical ones give 1, a bundle of k pairs matches each pair at ~1.
//! - Numbers use fractional-power encoding: `number(n)_k = exp(i n theta_k)`,
//! so `bind(number(a), number(b)) == number(a + b)`: addition is binding.
//! [`Space`] only initializes the two seeded number tables (digit phasors and
//! the cleanup codebook); encoding and decoding run as compiled circuits
//! (`number-encode`, `number-decode` in `zerolm-wsp`).
//!
//! Everything is programmed and seeded; nothing here is learned.

Expand Down Expand Up @@ -153,32 +156,16 @@ impl Space {
&self.number_base
}

/// Clean up a noisy number vector to the best integer in `range`.
/// Scans the range with a rotating phasor (`number(n+1) = number(n) * number(1)`
/// in f64), so no trigonometry runs per candidate.
pub fn decode_number(&self, v: &Hv, range: std::ops::RangeInclusive<i64>) -> (i64, f32) {
let (start, end) = (*range.start(), *range.end());
let d = self.d;
let mut cur: Vec<(f64, f64)> = self.number_base.iter().map(|t| ((start as f64 * t).cos(), (start as f64 * t).sin())).collect();
let step: Vec<(f64, f64)> = self.number_base.iter().map(|t| (t.cos(), t.sin())).collect();
let (re, im) = (v.re(), v.im());
let mut best = (start, f32::NEG_INFINITY);
let mut n = start;
while n <= end {
let mut s = 0.0f64;
for k in 0..d {
s += re[k] as f64 * cur[k].0 + im[k] as f64 * cur[k].1;
}
let s = (s / d as f64) as f32;
if s > best.1 {
best = (n, s);
}
for (c, st) in cur.iter_mut().zip(&step) {
*c = (c.0 * st.0 - c.1 * st.1, c.0 * st.1 + c.1 * st.0);
}
n += 1;
}
best
/// Digit table of the `number-encode` circuit (`10 places x 2d`): row
/// `10 q + v` is `number(v 10^q)`.
pub fn digit_table(&self, places: usize) -> Vec<f32> {
(0..places).flat_map(|q| (0..10).flat_map(move |v| self.number(v * 10i64.pow(q as u32)).data)).collect()
}

/// Cleanup codebook of the `number-decode` circuit (`(2 range + 1) x 2d`):
/// row `j` is `number(j - range)`.
pub fn codebook(&self, range: i64) -> Vec<f32> {
(-range..=range).flat_map(|n| self.number(n).data).collect()
}
}

Expand Down Expand Up @@ -206,11 +193,15 @@ mod tests {
let s = Space::new(1, 512);
let five = bind(&s.number(3), &s.number(2));
assert!((sim(&five, &s.number(5)) - 1.0).abs() < 1e-3);
assert_eq!(s.decode_number(&five, 0..=100).0, 5);
assert_eq!(s.decode_number(&unbind(&s.number(3), &s.number(7)), -50..=50).0, -4);
assert!((sim(&unbind(&s.number(3), &s.number(7)), &s.number(-4)) - 1.0).abs() < 1e-3);
assert!(sim(&s.number(4), &s.number(5)).abs() < 0.15);
for n in [-1000, -3, 0, 7, 999, 1000] {
assert_eq!(s.decode_number(&s.number(n), -1000..=1000).0, n);
}
// Digit products give the numeral's vector (the number-encode identity).
let digits = s.digit_table(3);
let row = |q: usize, v: usize| Hv::from_data(512, &digits[(10 * q + v) * 1024..(10 * q + v + 1) * 1024]);
let n907 = bind(&bind(&row(2, 9), &row(1, 0)), &row(0, 7));
assert!((sim(&n907, &s.number(907)) - 1.0).abs() < 1e-4);
let book = s.codebook(10);
assert_eq!(book.len(), 21 * 1024);
assert_eq!(&book[..1024], &s.number(-10).data[..]);
}
}
55 changes: 48 additions & 7 deletions crates/zerolm-eval/src/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ fn to_t(b: &Buf) -> T {
T { r: b.rows, c: b.cols, d: b.data.clone() }
}

/// Every output of the engine equals the reference output bit for bit.
/// Every output and final state tensor of the engine equals the reference bit for bit.
fn same(engine: &Outputs, reference: &std::collections::BTreeMap<String, T>) -> bool {
engine.values.iter().all(|(n, b)| {
reference.get(n).map(|t| t.r == b.rows && t.c == b.cols && t.d.iter().zip(&b.data).all(|(x, y)| x.to_bits() == y.to_bits())).unwrap_or(false)
})
let eq = |b: &Buf, t: Option<&T>| t.map(|t| t.r == b.rows && t.c == b.cols && t.d.iter().zip(&b.data).all(|(x, y)| x.to_bits() == y.to_bits())).unwrap_or(false);
engine.values.iter().all(|(n, b)| eq(b, reference.get(n))) && engine.state.iter().all(|(n, b)| eq(b, reference.get(&format!("state:{n}"))))
}

fn circuits() -> Result<Vec<CircuitRow>, String> {
Expand Down Expand Up @@ -134,21 +133,63 @@ fn memory_parity() -> Result<Parity, String> {
*p.by_program.entry(id.into()).or_default() += 1;
}
}
// Write and W_FAST circuits: a short session of new facts, repeated facts
// (unchanged), contradicting facts, corrections, confirmations, a
// forgotten chain, history, compaction, fast associations, and recalls.
let mut v = MemoryStore::new(MemoryConfig::default()).map_err(|e| e.to_string())?;
v.value_circuits().record(true);
let me = |e: zerolm_memory::MemoryError| e.to_string();
let s = "uniform:memory-writes";
for i in 0..24 {
v.teach(&format!("item{i} colour"), &format!("shade{i}"), s).map_err(me)?;
}
for i in 0..12 {
let value = if i % 2 == 0 { format!("shade{i}") } else { format!("tint{i}") };
v.teach(&format!("item{i} colour"), &value, s).map_err(me)?;
}
for i in 12..18 {
v.correct(&format!("item{i} colour"), &format!("hue{i}"), s).map_err(me)?;
}
for i in 18..22 {
v.confirm(&format!("item{i} colour"), s).map_err(me)?;
}
v.confirm("never taught key", s).map_err(me)?;
v.correct("item12 colour", "hue12b", s).map_err(me)?;
v.forget("item12 colour", s).map_err(me)?;
v.forget("item13 colour", s).map_err(me)?;
v.forget("never taught key", s).map_err(me)?;
v.history("item14 colour").map_err(me)?;
v.compact(s);
v.teach("after compaction", "fresh", s).map_err(me)?;
for i in 0..4 {
v.associate_fast(&format!("door{i} code"), &format!("code{i}"), "uniform:value-codec").map_err(me)?;
}
for i in 0..4 {
v.recall_fast(&format!("door{i} code")).map_err(me)?;
}
for rec in v.value_circuits().take_recorded() {
let inputs: Vec<(&str, T)> = rec.inputs.iter().map(|(n, b)| (n.as_str(), to_t(b))).collect();
let ok = same(&rec.outputs, &isa_ref::run(&rec.bundle, &inputs)?);
p.runs += 1;
p.mismatches += !ok as usize;
*p.by_program.entry(rec.program).or_default() += 1;
}
Ok(p)
}

/// Reasoning: answer a generated world's questions (intact, then with each
/// operator ablated in turn on a subset), record every circuit run, and
/// Reasoning: teach a generated world, answer its questions (intact, then
/// with each operator ablated in turn on a subset), record every circuit run, and
/// replay all of them on the reference interpreter in parallel.
fn reasoning_parity(root: &Path) -> Result<Parity, String> {
let spec = reasoning::Spec::load(root)?;
let seed = *spec.main_seeds.first().ok_or("no main seed")?;
let world = reasoning::generate(&spec, seed);
let mut r = Reasoner::new(spec.config).map_err(|e| e.to_string())?;
// Record from the first statement, so encoder runs while teaching are replayed too.
r.record_circuits(true);
for s in &world.statements {
r.observe(&s.text, "uniform:synthetic-world").map_err(|e| e.to_string())?;
}
r.record_circuits(true);
for q in &world.questions {
r.ask(&q.text);
}
Expand Down
89 changes: 0 additions & 89 deletions crates/zerolm-memory/src/fast_weights.rs

This file was deleted.

3 changes: 1 addition & 2 deletions crates/zerolm-memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

pub mod checkpoint;
pub mod encoder;
pub mod fast_weights;
pub mod persistent_memory;
pub mod read_circuit;
pub mod retrieval;
pub mod value_circuits;

pub use persistent_memory::{Ablation, Answer, MemoryConfig, MemoryError, MemoryEvent, MemoryStore, Outcome, Stats};
Loading
Loading