diff --git a/.claude/skills/zerolm-roadmap/SKILL.md b/.claude/skills/zerolm-roadmap/SKILL.md index 3213aeb..5360728 100644 --- a/.claude/skills/zerolm-roadmap/SKILL.md +++ b/.claude/skills/zerolm-roadmap/SKILL.md @@ -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. diff --git a/AGENTS.md b/AGENTS.md index 24ced40..43e04ee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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`. diff --git a/README.md b/README.md index 0d28110..4e83bbe 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/crates/zerolm-engine/src/binding.rs b/crates/zerolm-engine/src/binding.rs index 746ae10..6e5c4bb 100644 --- a/crates/zerolm-engine/src/binding.rs +++ b/crates/zerolm-engine/src/binding.rs @@ -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. @@ -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, 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 { + (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 { + (-range..=range).flat_map(|n| self.number(n).data).collect() } } @@ -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[..]); } } diff --git a/crates/zerolm-eval/src/uniform.rs b/crates/zerolm-eval/src/uniform.rs index 2e53e3d..08f252d 100644 --- a/crates/zerolm-eval/src/uniform.rs +++ b/crates/zerolm-eval/src/uniform.rs @@ -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) -> 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, String> { @@ -134,21 +133,63 @@ fn memory_parity() -> Result { *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 { 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); } diff --git a/crates/zerolm-memory/src/fast_weights.rs b/crates/zerolm-memory/src/fast_weights.rs deleted file mode 100644 index 073e04b..0000000 --- a/crates/zerolm-memory/src/fast_weights.rs +++ /dev/null @@ -1,89 +0,0 @@ -//! `W_FAST`: a short-lived associative matrix (tensor `memory.fast`). -//! -//! Keys first pass a frozen pattern-separation layer (tensor -//! `core.fast_separator`, seeded): s = kWTA(P k), the `active` largest -//! entries of P k set to 1/sqrt(active), the rest 0. Sparse codes of -//! correlated keys overlap far less than the keys themselves. -//! -//! Write (normalized delta rule with decay): -//! M <- decay * M; M <- M + lr * (v - M s) s^T / (s.s + eps) -//! Read: v_hat = M s(q). Associations are superposed in one matrix, so they -//! interfere and fade; capacity is measured, not assumed. - -use zerolm_tensor::{dot, Matrix}; - -pub const TENSOR: &str = "memory.fast"; -pub const SEPARATOR: &str = "core.fast_separator"; -pub const EPS: f32 = 1e-6; - -/// k-winners-take-all sparse code of `p * k` (ties broken by index). -pub fn separate(p: &Matrix, k: &[f32], active: usize) -> Vec { - let mut h = vec![0.0f32; p.rows]; - p.matvec(k, &mut h); - let mut idx: Vec = (0..h.len()).collect(); - idx.sort_by(|&a, &b| h[b].partial_cmp(&h[a]).unwrap_or(std::cmp::Ordering::Equal).then(a.cmp(&b))); - let mut s = vec![0.0f32; h.len()]; - let a = active.clamp(1, h.len()); - for &i in &idx[..a] { - s[i] = 1.0 / (a as f32).sqrt(); - } - s -} - -#[derive(Debug, Clone, PartialEq)] -pub struct FastWeights { - pub m: Matrix, - pub decay: f32, - pub lr: f32, -} - -impl FastWeights { - pub fn new(value_dim: usize, key_dim: usize, decay: f32, lr: f32) -> Self { - FastWeights { m: Matrix::zeros(value_dim, key_dim), decay, lr } - } - - pub fn read(&self, q: &[f32]) -> Vec { - let mut out = vec![0.0; self.m.rows]; - self.m.matvec(q, &mut out); - out - } - - /// Returns the Frobenius norm of the delta-rule increment. - pub fn write(&mut self, k: &[f32], v: &[f32]) -> f32 { - assert_eq!(v.len(), self.m.rows); - self.m.data.iter_mut().for_each(|w| *w *= self.decay); - let pred = self.read(k); - let scale = self.lr / (dot(k, k) + EPS); - let cols = self.m.cols; - let mut n2 = 0.0f32; - let k2 = dot(k, k); - for (r, (vt, pt)) in v.iter().zip(&pred).enumerate() { - let e = scale * (vt - pt); - n2 += e * e * k2; - for (w, x) in self.m.data[r * cols..(r + 1) * cols].iter_mut().zip(k) { - *w += e * x; - } - } - n2.sqrt() - } - - pub fn clear(&mut self) { - self.m.data.iter_mut().for_each(|w| *w = 0.0); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn a_single_write_is_recalled_exactly_and_fades_with_decay() { - let mut f = FastWeights::new(3, 2, 0.5, 1.0); - f.write(&[1.0, 0.0], &[1.0, 2.0, 3.0]); - let r = f.read(&[1.0, 0.0]); - assert!(r.iter().zip([1.0, 2.0, 3.0]).all(|(a, b)| (a - b).abs() < 1e-4)); - f.write(&[0.0, 1.0], &[0.0, 0.0, 1.0]); - let r = f.read(&[1.0, 0.0]); - assert!((r[2] - 1.5).abs() < 1e-4, "decayed by half: {r:?}"); - } -} diff --git a/crates/zerolm-memory/src/lib.rs b/crates/zerolm-memory/src/lib.rs index d20ca54..199a7d9 100644 --- a/crates/zerolm-memory/src/lib.rs +++ b/crates/zerolm-memory/src/lib.rs @@ -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}; diff --git a/crates/zerolm-memory/src/persistent_memory.rs b/crates/zerolm-memory/src/persistent_memory.rs index f5f89ed..0693647 100644 --- a/crates/zerolm-memory/src/persistent_memory.rs +++ b/crates/zerolm-memory/src/persistent_memory.rs @@ -14,16 +14,17 @@ use crate::checkpoint::{self, CheckpointError, Fault, LoadSource, Tensor}; use crate::encoder::{self, KeyEncoder}; -use crate::fast_weights::{self, FastWeights}; use crate::read_circuit; -use crate::retrieval::{self, Decoded}; +use crate::value_circuits::{Decoded, ValueCircuits}; use serde::{Deserialize, Serialize}; use std::fmt; use std::path::Path; use zerolm_engine::bootstrap::default_model; +use zerolm_engine::isa::Buf; use zerolm_engine::Model; use zerolm_tensor::sha256::Sha256; use zerolm_tensor::{fnv1a, shuffle, Matrix}; +use zerolm_wsp::circuits::memory_write::OUTCOMES; use zerolm_wsp::Compiled; pub const KEYS: &str = "memory.semantic.keys"; @@ -35,6 +36,10 @@ pub const EP_META: &str = "memory.episodic.meta"; pub const STATE: &str = "memory.state"; pub const EMBED: &str = "core.embed"; pub const CELL_PREFIX: &str = "core.cell."; +/// `W_FAST`: the short-lived associative matrix (written by `fast-write`). +pub const FAST: &str = "memory.fast"; +/// The frozen seeded pattern separator of `W_FAST` keys. +pub const SEPARATOR: &str = "core.fast_separator"; /// Semantic metadata columns. pub mod field { @@ -319,8 +324,10 @@ pub struct MemoryStore { meta: Matrix, ep_keys: Matrix, ep_meta: Matrix, - fast: FastWeights, + fast: Matrix, separator: Matrix, + /// The compiled value codec and `W_FAST` circuits (`W_PROG`). + codec: ValueCircuits, /// The compiled key encoder (`W_PROG`, `prog.memory-encode.*`). encode: Compiled, /// The compiled read circuit (`W_PROG`, `prog.memory-read.*`). @@ -347,6 +354,10 @@ fn prov(stream: &str) -> [f32; 3] { [(h & m) as f32, ((h >> 22) & m) as f32, (h >> 44) as f32] } +fn view(m: &Matrix) -> zerolm_engine::isa::View<'_> { + zerolm_engine::isa::View::new(m.rows, m.cols, &m.data) +} + fn slot_ref(x: f32) -> Option { if x < 0.0 { None @@ -374,8 +385,9 @@ impl MemoryStore { meta, ep_keys: Matrix::zeros(cfg.episodic_capacity, cfg.key_dim), ep_meta: Matrix::zeros(cfg.episodic_capacity, ep_field::COUNT), - fast: FastWeights::new(cfg.positions() * e, cfg.fast_expansion, cfg.fast_decay, cfg.fast_lr), - separator: Matrix::uniform(cfg.fast_expansion, cfg.key_dim, cfg.seed, fast_weights::SEPARATOR, 1.0), + fast: Matrix::zeros(cfg.positions() * e, cfg.fast_expansion), + separator: Matrix::uniform(cfg.fast_expansion, cfg.key_dim, cfg.seed, SEPARATOR, 1.0), + codec: ValueCircuits::new(&cfg, core.embed.rows, e), encode: read_circuit::build_encoder(&cfg), read: read_circuit::build(&cfg, core.embed.rows, e), core, @@ -473,20 +485,8 @@ impl MemoryStore { Ok(()) } - fn encode_value(&self, value: &str) -> Vec { - retrieval::encode_value(value.as_bytes(), &self.core.embed, self.cfg.positions()) - } - - /// Decode exactly what one slot holds (used for write-time comparison). - fn slot_value(&self, slot: usize) -> Vec { - retrieval::decode(self.values.row(slot), &self.core.embed).bytes - } - - /// Active slot whose key is the same fact key: the read circuit's best - /// slot when its score reaches `same_key_threshold`. - fn find_same(&self, k: &[f32]) -> Option { - let a = self.answer(read_circuit::key_inputs(k)); - a.slot.filter(|_| a.score >= self.cfg.same_key_threshold) + fn codes(&self) -> zerolm_engine::isa::View<'_> { + view(&self.core.embed) } fn tick(&mut self) -> u64 { @@ -545,67 +545,110 @@ impl MemoryStore { }); } - #[allow(clippy::too_many_arguments)] - fn write_slot(&mut self, slot: usize, k: &[f32], words: &[f32], n_words: usize, value: &str, source: Source, stream: &str, seq: u64) { - let v = self.encode_value(value); - let kc = self.keys.cols; - self.keys.data[slot * kc..(slot + 1) * kc].copy_from_slice(k); - let wc = self.key_words.cols; - self.key_words.data[slot * wc..(slot + 1) * wc].copy_from_slice(words); - self.set(slot, field::KEY_WORDS, n_words as f32); - let vc = self.values.cols; - self.values.data[slot * vc..(slot + 1) * vc].copy_from_slice(&v); + /// The metadata a write at `seq` is stamped with (what the host owns: + /// sequence, source, provenance, key word count); the circuit adds status, + /// confidence, and links. + fn stamp(seq: u64, source: Source, stream: &str, n_words: usize) -> Vec { let (lo, hi) = split48(seq); - let p = prov(stream); - self.set(slot, field::STATUS, ACTIVE); - self.set(slot, field::CONFIDENCE, UNCONFIRMED); - self.set(slot, field::CREATED_LO, lo); - self.set(slot, field::CREATED_HI, hi); - self.set(slot, field::UPDATED_LO, lo); - self.set(slot, field::UPDATED_HI, hi); - self.set(slot, field::SOURCE, source as u8 as f32); - for (i, x) in p.iter().enumerate() { - self.set(slot, field::PROV + i, *x); - } - self.set(slot, field::SUPERSEDES, -1.0); - self.set(slot, field::SUPERSEDED_BY, -1.0); - self.set(slot, field::CONTRADICTIONS, 0.0); - } - - fn touch(&mut self, slot: usize, seq: u64) { + let mut s = vec![0.0f32; field::COUNT]; + s[field::CREATED_LO] = lo; + s[field::CREATED_HI] = hi; + s[field::UPDATED_LO] = lo; + s[field::UPDATED_HI] = hi; + s[field::SOURCE] = source as u8 as f32; + s[field::PROV..field::PROV + 3].copy_from_slice(&prov(stream)); + s[field::KEY_WORDS] = n_words as f32; + s + } + + /// The episodic stamp of an operation at `seq`; the circuit adds the op and slot. + fn ep_stamp(seq: u64, source: Source, stream: &str) -> Vec { let (lo, hi) = split48(seq); - self.set(slot, field::UPDATED_LO, lo); - self.set(slot, field::UPDATED_HI, hi); + let mut r = vec![0.0f32; ep_field::COUNT]; + r[ep_field::SEQ_LO] = lo; + r[ep_field::SEQ_HI] = hi; + r[ep_field::SOURCE] = source as u8 as f32; + r[ep_field::PROV..ep_field::PROV + 3].copy_from_slice(&prov(stream)); + r[ep_field::VALID] = 1.0; + r } - fn free_slot(&self) -> Option { - (0..self.cfg.slots).find(|&i| self.status(i) == EMPTY) + /// Append an episode row computed by a circuit (ring buffer). + fn push_episode(&mut self, key: Option<&[f32]>, row: &[f32]) -> usize { + let r = (self.ep_count % self.cfg.episodic_capacity as u64) as usize; + self.ep_count += 1; + let c = self.ep_keys.cols; + match key { + Some(k) => self.ep_keys.data[r * c..(r + 1) * c].copy_from_slice(k), + None => self.ep_keys.data[r * c..(r + 1) * c].iter_mut().for_each(|x| *x = 0.0), + } + self.ep_meta.data[r * ep_field::COUNT..(r + 1) * ep_field::COUNT].copy_from_slice(row); + r + } + + fn put_row(m: &mut Matrix, r: usize, row: &[f32]) { + let c = m.cols; + m.data[r * c..(r + 1) * c].copy_from_slice(row); + } + + /// teach (0), correct (1), or confirm (2): the `memory-write` circuit + /// decides the outcome and computes every row that changes; the host + /// checks capacity, moves those rows, and logs. + #[allow(clippy::too_many_arguments)] + fn apply_write(&mut self, op: usize, k: &[f32], words: &[f32], n_words: usize, value: &str, src: Source, stream: &str) -> Result { + let seq = self.clock + 1; + let mut op_row = vec![0.0f32; 3]; + op_row[op] = 1.0; + let inputs = vec![ + ("key", Buf::row(k.to_vec())), + ("words", Buf::row(words.to_vec())), + ("value", self.codec.value_input(value)), + ("op", Buf::row(op_row)), + ("stamp", Buf::row(Self::stamp(seq, src, stream, n_words))), + ("ep_stamp", Buf::row(Self::ep_stamp(seq, src, stream))), + ]; + let o = self.codec.decide( + &self.codec.mem_write, + inputs, + &[("keys", view(&self.keys)), ("values", view(&self.values)), ("meta", view(&self.meta)), ("codes", view(&self.core.embed))], + ); + let outcome = OUTCOMES[o.get("outcome").data.iter().position(|&x| x == 1.0).expect("one-hot outcome")]; + if outcome == "unknown" { + return Ok(Outcome::Unknown); + } + let writes_new = matches!(outcome, "stored" | "superseded"); + if writes_new && o.scalar("full") == 1.0 { + return Err(MemoryError::CapacityFull); + } + self.tick(); + let (best, free) = (o.scalar("best") as usize, o.scalar("free") as usize); + if writes_new { + Self::put_row(&mut self.keys, free, &o.get("new_key").data); + Self::put_row(&mut self.key_words, free, &o.get("new_words").data); + Self::put_row(&mut self.values, free, &o.get("new_value").data); + Self::put_row(&mut self.meta, free, &o.get("new_meta").data); + } + if outcome != "stored" { + Self::put_row(&mut self.meta, best, &o.get("old_meta").data); + } + let r = self.push_episode(Some(k), &o.get("episode").data); + let all = vec![KEYS, KEY_WORDS, VALUES, META, EP_KEYS, EP_META]; + let (name, changed, slot, out) = match outcome { + "stored" => ("add", all, Some(free), Outcome::Stored { slot: free }), + "unchanged" => ("unchanged", vec![EP_KEYS, EP_META], None, Outcome::Unchanged { slot: best }), + "conflict" => ("contradiction", vec![META, EP_KEYS, EP_META], Some(best), Outcome::Conflict { slot: best }), + "superseded" => ("supersede", all, Some(free), Outcome::Superseded { old: best, new: free }), + _ => ("confirm", vec![META, EP_KEYS, EP_META], Some(best), Outcome::Confirmed { slot: best }), + }; + self.log(seq, name, stream, src.signal(), "memory_write", changed, slot, Some(r)); + Ok(out) } /// `teach`: store a new fact; never overwrites a different belief. pub fn teach(&mut self, key: &str, value: &str, stream: &str) -> Result { let (k, words, n_words) = self.key(key)?; self.check_value(value)?; - let src = Source::UserStatement; - if let Some(s) = self.find_same(&k) { - let seq = self.tick(); - if self.slot_value(s) == value.as_bytes() { - let r = self.episode("unchanged", seq, Some(&k), Some(s), Some(src), stream); - self.log(seq, "unchanged", stream, src.signal(), "memory_write", vec![EP_KEYS, EP_META], None, Some(r)); - return Ok(Outcome::Unchanged { slot: s }); - } - let c = self.m(s, field::CONTRADICTIONS) + 1.0; - self.set(s, field::CONTRADICTIONS, c); - let r = self.episode("contradiction", seq, Some(&k), Some(s), Some(src), stream); - self.log(seq, "contradiction", stream, src.signal(), "memory_write", vec![META, EP_KEYS, EP_META], Some(s), Some(r)); - return Ok(Outcome::Conflict { slot: s }); - } - let slot = self.free_slot().ok_or(MemoryError::CapacityFull)?; - let seq = self.tick(); - self.write_slot(slot, &k, &words, n_words, value, src, stream, seq); - let r = self.episode("add", seq, Some(&k), Some(slot), Some(src), stream); - self.log(seq, "add", stream, src.signal(), "memory_write", vec![KEYS, KEY_WORDS, VALUES, META, EP_KEYS, EP_META], Some(slot), Some(r)); - Ok(Outcome::Stored { slot }) + self.apply_write(0, &k, &words, n_words, value, Source::UserStatement, stream) } /// `correct`: the new value supersedes the current belief (kept, marked @@ -613,89 +656,50 @@ impl MemoryStore { pub fn correct(&mut self, key: &str, value: &str, stream: &str) -> Result { let (k, words, n_words) = self.key(key)?; self.check_value(value)?; - let src = Source::UserCorrection; - let old = self.find_same(&k); - if let Some(s) = old { - if self.slot_value(s) == value.as_bytes() { - let seq = self.tick(); - let r = self.episode("unchanged", seq, Some(&k), Some(s), Some(src), stream); - self.log(seq, "unchanged", stream, src.signal(), "memory_write", vec![EP_KEYS, EP_META], None, Some(r)); - return Ok(Outcome::Unchanged { slot: s }); - } - } - let slot = self.free_slot().ok_or(MemoryError::CapacityFull)?; - let seq = self.tick(); - self.write_slot(slot, &k, &words, n_words, value, src, stream, seq); - match old { - Some(s) => { - self.set(s, field::STATUS, SUPERSEDED); - self.set(s, field::SUPERSEDED_BY, slot as f32); - self.touch(s, seq); - self.set(slot, field::SUPERSEDES, s as f32); - let r = self.episode("supersede", seq, Some(&k), Some(slot), Some(src), stream); - self.log(seq, "supersede", stream, src.signal(), "memory_write", vec![KEYS, KEY_WORDS, VALUES, META, EP_KEYS, EP_META], Some(slot), Some(r)); - Ok(Outcome::Superseded { old: s, new: slot }) - } - None => { - let r = self.episode("add", seq, Some(&k), Some(slot), Some(src), stream); - self.log(seq, "add", stream, src.signal(), "memory_write", vec![KEYS, KEY_WORDS, VALUES, META, EP_KEYS, EP_META], Some(slot), Some(r)); - Ok(Outcome::Stored { slot }) - } - } + self.apply_write(1, &k, &words, n_words, value, Source::UserCorrection, stream) } /// `confirm`: raise the stored belief's confidence to 1. pub fn confirm(&mut self, key: &str, stream: &str) -> Result { let k = self.pooled(key)?; - let Some(s) = self.find_same(&k) else { return Ok(Outcome::Unknown) }; - let seq = self.tick(); - self.set(s, field::CONFIDENCE, CONFIRMED); - self.touch(s, seq); - let src = Source::UserCorrection; - let r = self.episode("confirm", seq, Some(&k), Some(s), Some(src), stream); - self.log(seq, "confirm", stream, src.signal(), "memory_write", vec![META, EP_KEYS, EP_META], Some(s), Some(r)); - Ok(Outcome::Confirmed { slot: s }) + let words = vec![0.0f32; self.key_words.cols]; + self.apply_write(2, &k, &words, 0, "", Source::UserCorrection, stream) } - /// `forget`: delete the belief and every value it superseded. Key and - /// value rows are zeroed (erased, not hidden); the episodic records that - /// carry this key lose their key vector. Metadata keeps status=deleted. + /// `forget`: delete the belief and every value it superseded (the + /// `memory-forget` circuit follows the links). Key and value rows are + /// zeroed (erased, not hidden); the episodic records that carry this key + /// lose their key vector. Metadata keeps status=deleted. pub fn forget(&mut self, key: &str, stream: &str) -> Result { let k = self.pooled(key)?; - let Some(s) = self.find_same(&k) else { return Ok(Outcome::Unknown) }; - let seq = self.tick(); - let mut chain = vec![s]; - let mut cur = s; - while let Some(p) = slot_ref(self.m(cur, field::SUPERSEDES)) { - if chain.contains(&p) { - break; - } - chain.push(p); - cur = p; - } + let seq = self.clock + 1; + let src = Source::UserCorrection; + let (lo, hi) = split48(seq); + let mut stamp = vec![0.0f32; field::COUNT]; + stamp[field::UPDATED_LO] = lo; + stamp[field::UPDATED_HI] = hi; + let inputs = vec![("key", Buf::row(k)), ("stamp", Buf::row(stamp)), ("ep_stamp", Buf::row(Self::ep_stamp(seq, src, stream)))]; + let o = self.codec.decide(&self.codec.forget, inputs, &[("keys", view(&self.keys)), ("meta", view(&self.meta)), ("ep_meta", view(&self.ep_meta))]); + if o.scalar("found") != 1.0 { + return Ok(Outcome::Unknown); + } + self.tick(); + let marked = |b: &Buf| b.data.iter().enumerate().filter(|(_, &x)| x > 0.0).map(|(i, _)| i).collect::>(); + let chain = marked(o.get("chain")); for &c in &chain { - let (kc, vc, wc) = (self.keys.cols, self.values.cols, self.key_words.cols); - self.keys.data[c * kc..(c + 1) * kc].iter_mut().for_each(|x| *x = 0.0); - self.key_words.data[c * wc..(c + 1) * wc].iter_mut().for_each(|x| *x = 0.0); - self.values.data[c * vc..(c + 1) * vc].iter_mut().for_each(|x| *x = 0.0); - self.set(c, field::STATUS, DELETED); - self.touch(c, seq); - } - for r in 0..self.cfg.episodic_capacity { - let row = r * ep_field::COUNT; - if self.ep_meta.data[row + ep_field::VALID] == 1.0 { - if let Some(es) = slot_ref(self.ep_meta.data[row + ep_field::SLOT]) { - if chain.contains(&es) { - let c = self.ep_keys.cols; - self.ep_keys.data[r * c..(r + 1) * c].iter_mut().for_each(|x| *x = 0.0); - } - } + for m in [&mut self.keys, &mut self.key_words, &mut self.values] { + let w = m.cols; + m.data[c * w..(c + 1) * w].iter_mut().for_each(|x| *x = 0.0); } } - let src = Source::UserCorrection; - let r = self.episode("forget", seq, None, Some(s), Some(src), stream); + self.meta.data.copy_from_slice(&o.get("meta_out").data); + for r in marked(o.get("erase")) { + let c = self.ep_keys.cols; + self.ep_keys.data[r * c..(r + 1) * c].iter_mut().for_each(|x| *x = 0.0); + } + let s = o.scalar("slot") as usize; + let r = self.push_episode(None, &o.get("episode").data); self.log(seq, "forget", stream, src.signal(), "memory_write", vec![KEYS, KEY_WORDS, VALUES, META, EP_KEYS, EP_META], Some(s), Some(r)); - chain.sort_unstable(); Ok(Outcome::Forgotten { slots: chain }) } @@ -760,6 +764,11 @@ impl MemoryStore { &self.encode } + /// The compiled value codec and `W_FAST` circuits. + pub fn value_circuits(&self) -> &ValueCircuits { + &self.codec + } + /// Zero one block of the read circuit (causal ablation; evaluation only). pub fn ablate_circuit(&mut self, block: &str) -> Result<(), zerolm_engine::isa::IsaError> { self.read.ablate_block(block) @@ -773,80 +782,66 @@ impl MemoryStore { /// Episodic records whose key is this fact key, oldest first. pub fn history(&self, text: &str) -> Result, MemoryError> { let q = self.pooled(text)?; - let mut out = Vec::new(); - for r in 0..self.cfg.episodic_capacity { - let m = self.ep_meta.row(r); - if m[ep_field::VALID] == 1.0 && zerolm_tensor::dot(self.ep_keys.row(r), &q) >= self.cfg.same_key_threshold { - out.push(Episode { - seq: join48(m[ep_field::SEQ_LO], m[ep_field::SEQ_HI]), - op: OPS[m[ep_field::OP] as usize], - slot: slot_ref(m[ep_field::SLOT]), - }); - } - } + let o = self.codec.decide(&self.codec.history, vec![("key", Buf::row(q))], &[("ep_keys", view(&self.ep_keys)), ("ep_meta", view(&self.ep_meta))]); + let mut out: Vec = o + .get("hits") + .data + .iter() + .enumerate() + .filter(|(_, &h)| h == 1.0) + .map(|(r, _)| { + let m = self.ep_meta.row(r); + Episode { seq: join48(m[ep_field::SEQ_LO], m[ep_field::SEQ_HI]), op: OPS[m[ep_field::OP] as usize], slot: slot_ref(m[ep_field::SLOT]) } + }) + .collect(); out.sort_by_key(|e| e.seq); Ok(out) } - /// Reclaim superseded and deleted slots. Active facts are moved to the - /// lowest slots in order; supersession links and episodic slot references - /// to reclaimed slots become -1. Returns the number of slots freed. + /// Reclaim superseded and deleted slots. The `memory-compact` circuit + /// orders active facts to the lowest slots and remaps supersession links + /// and episodic slot references (-1 for reclaimed slots); the host moves + /// the rows in that order. Returns the number of slots freed. pub fn compact(&mut self, stream: &str) -> usize { - let active = self.active_slots(); - let freed = self.cfg.slots - active.len() - self.stats().free; - let mut map = vec![-1.0f32; self.cfg.slots]; - for (new, &old) in active.iter().enumerate() { - map[old] = new as f32; - } - let remap = |x: f32| if x < 0.0 { -1.0 } else { map[x as usize] }; - let (kc, vc, mc, wc) = (self.keys.cols, self.values.cols, field::COUNT, self.key_words.cols); - let mut keys = Matrix::zeros(self.cfg.slots, kc); - let mut key_words = Matrix::zeros(self.cfg.slots, wc); - let mut values = Matrix::zeros(self.cfg.slots, vc); - let mut meta = Matrix::zeros(self.cfg.slots, mc); - for r in 0..self.cfg.slots { - meta.data[r * mc + field::SUPERSEDES] = -1.0; - meta.data[r * mc + field::SUPERSEDED_BY] = -1.0; - } - for (new, &old) in active.iter().enumerate() { - keys.data[new * kc..(new + 1) * kc].copy_from_slice(self.keys.row(old)); - key_words.data[new * wc..(new + 1) * wc].copy_from_slice(self.key_words.row(old)); - values.data[new * vc..(new + 1) * vc].copy_from_slice(self.values.row(old)); - meta.data[new * mc..(new + 1) * mc].copy_from_slice(self.meta.row(old)); - meta.data[new * mc + field::SUPERSEDES] = remap(self.m(old, field::SUPERSEDES)); - meta.data[new * mc + field::SUPERSEDED_BY] = remap(self.m(old, field::SUPERSEDED_BY)); - } - for r in 0..self.cfg.episodic_capacity { - let i = r * ep_field::COUNT + ep_field::SLOT; - self.ep_meta.data[i] = remap(self.ep_meta.data[i]); - } - self.keys = keys; - self.key_words = key_words; - self.values = values; - self.meta = meta; + let o = self.codec.decide(&self.codec.compact, Vec::new(), &[("meta", view(&self.meta)), ("ep_meta", view(&self.ep_meta))]); + let n = o.scalar("n_active") as usize; + let order: Vec = o.get("order").data[..n].iter().map(|&x| x as usize).collect(); + for m in [&mut self.keys, &mut self.key_words, &mut self.values] { + let mut moved = Matrix::zeros(m.rows, m.cols); + for (new, &old) in order.iter().enumerate() { + moved.data[new * m.cols..(new + 1) * m.cols].copy_from_slice(m.row(old)); + } + *m = moved; + } + self.meta.data.copy_from_slice(&o.get("meta_out").data); + for (r, &s) in o.get("ep_slot").data.iter().enumerate() { + self.ep_meta.data[r * ep_field::COUNT + ep_field::SLOT] = s; + } let seq = self.tick(); let r = self.episode("compact", seq, None, None, None, stream); self.log(seq, "compact", stream, "memory_maintenance", "compaction", vec![KEYS, KEY_WORDS, VALUES, META, EP_META], None, Some(r)); - freed + o.scalar("freed") as usize } /// `W_FAST` write: a short-lived association (not a semantic belief). pub fn associate_fast(&mut self, key: &str, value: &str, stream: &str) -> Result { let k = self.pooled(key)?; self.check_value(value)?; - let v = self.encode_value(value); - let sk = fast_weights::separate(&self.separator, &k, self.cfg.fast_active); - let n = self.fast.write(&sk, &v); + let before = Buf::new(self.fast.rows, self.fast.cols, std::mem::take(&mut self.fast.data)); + let after = self.codec.write_fast(view(&self.core.embed), view(&self.separator), before.clone(), &k, value); + // Frobenius norm of the change (logging only). + let n = before.data.iter().zip(&after.data).map(|(a, b)| (b - a) * (b - a)).sum::().sqrt(); + self.fast.data = after.data; let seq = self.tick(); let mut h = Sha256::new(); - h.update_f32(&self.fast.m.data); + h.update_f32(&self.fast.data); self.events.push(MemoryEvent { seq, op: "fast_associate", input_stream: stream.into(), signal: Source::UserStatement.signal(), rule: "fast_delta", - changed_tensors: vec![fast_weights::TENSOR], + changed_tensors: vec![FAST], slot: None, rows_sha256: h.finish_hex(), checkpoint_sha256: None, @@ -856,8 +851,7 @@ impl MemoryStore { pub fn recall_fast(&self, key: &str) -> Result { let k = self.pooled(key)?; - let sk = fast_weights::separate(&self.separator, &k, self.cfg.fast_active); - Ok(retrieval::decode(&self.fast.read(&sk), &self.core.embed)) + Ok(self.codec.recall_fast(self.codes(), view(&self.separator), view(&self.fast), &k)) } /// Damage memory tensors for a causal ablation (evaluation only). @@ -888,14 +882,14 @@ impl MemoryStore { // ---- Checkpoint ---- - fn core_tensors(core: &Model, encoder: &KeyEncoder, separator: &Matrix, circuits: [&Compiled; 2]) -> Vec { + fn core_tensors(core: &Model, encoder: &KeyEncoder, separator: &Matrix, circuits: &[&Compiled]) -> Vec { let mut t = vec![Tensor::new(EMBED, "byte_embed", core.embed.rows, core.embed.cols, core.embed.data.clone())]; for (name, data) in core.cell.groups() { t.push(Tensor::new(&format!("{CELL_PREFIX}{name}"), "recurrent_core", 1, data.len(), data.to_vec())); } let e = &encoder.table; t.push(Tensor::new(encoder::TENSOR, "memory_encoder", e.rows, e.cols, e.data.clone())); - t.push(Tensor::new(fast_weights::SEPARATOR, "memory_encoder", separator.rows, separator.cols, separator.data.clone())); + t.push(Tensor::new(SEPARATOR, "memory_encoder", separator.rows, separator.cols, separator.data.clone())); for prog in circuits { for c in &prog.tensors { t.push(Tensor::new(&prog.qualified(&c.name), zerolm_wsp::compiler::GROUP, c.rows, c.cols, c.data.clone())); @@ -906,7 +900,7 @@ impl MemoryStore { /// Every tensor of the checkpoint, with its weight group. pub fn tensors(&self) -> Vec { - let mut t = Self::core_tensors(&self.core, &self.encoder, &self.separator, [&self.encode, &self.read]); + let mut t = Self::core_tensors(&self.core, &self.encoder, &self.separator, &[&[&self.encode, &self.read][..], &self.codec.all()[..]].concat()); let mat = |name: &str, group: &str, m: &Matrix| Tensor::new(name, group, m.rows, m.cols, m.data.clone()); t.push(mat(KEYS, "memory_keys", &self.keys)); t.push(mat(KEY_WORDS, "memory_keys", &self.key_words)); @@ -914,7 +908,7 @@ impl MemoryStore { t.push(mat(META, "memory_meta", &self.meta)); t.push(mat(EP_KEYS, "episodic_memory", &self.ep_keys)); t.push(mat(EP_META, "episodic_memory", &self.ep_meta)); - t.push(mat(fast_weights::TENSOR, "fast_weights", &self.fast.m)); + t.push(mat(FAST, "fast_weights", &self.fast)); let (c0, c1) = split48(self.clock); let (e0, e1) = split48(self.ep_count); t.push(Tensor::new(STATE, "memory_meta", 1, 4, vec![c0, c1, e0, e1])); @@ -992,7 +986,7 @@ impl MemoryStore { META => &mut s.meta.data, EP_KEYS => &mut s.ep_keys.data, EP_META => &mut s.ep_meta.data, - fast_weights::TENSOR => &mut s.fast.m.data, + FAST => &mut s.fast.data, STATE => { s.clock = join48(got.data[0], got.data[1]); s.ep_count = join48(got.data[2], got.data[3]); diff --git a/crates/zerolm-memory/src/read_circuit.rs b/crates/zerolm-memory/src/read_circuit.rs index 61212ac..cc533f4 100644 --- a/crates/zerolm-memory/src/read_circuit.rs +++ b/crates/zerolm-memory/src/read_circuit.rs @@ -17,6 +17,9 @@ use zerolm_wsp::{compile, Circuit, Compiled, Spec}; pub const ID: &str = "memory-read"; pub const ENCODE_ID: &str = "memory-encode"; +/// Read weights below this are dropped from the value read (a circuit parameter). +pub const MIN_WEIGHT: f32 = 1e-7; + /// Input width of the encoder: texts up to `TEXT_BYTES - 2` bytes. pub const TEXT_BYTES: usize = 256; @@ -104,7 +107,7 @@ pub fn spec(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> Spec { beta: cfg.beta, read_threshold: cfg.read_threshold, coverage_threshold: cfg.coverage_threshold, - min_weight: crate::retrieval::MIN_WEIGHT, + min_weight: MIN_WEIGHT, }), } } diff --git a/crates/zerolm-memory/src/retrieval.rs b/crates/zerolm-memory/src/retrieval.rs deleted file mode 100644 index 04dc0da..0000000 --- a/crates/zerolm-memory/src/retrieval.rs +++ /dev/null @@ -1,71 +0,0 @@ -//! Value encoding into byte-code rows and nearest-code decoding. -//! -//! Semantic queries are answered by the compiled read circuit -//! (`read_circuit`), which does its own cleanup. `decode` remains a host -//! helper for write-time value comparison and `W_FAST` recall; moving those -//! onto the engine is still open uniform-engine work. - -use zerolm_tensor::Matrix; - -/// Read weights below this are dropped from the value read (a circuit parameter). -pub const MIN_WEIGHT: f32 = 1e-7; - -/// Value bytes -> concatenated code rows, NUL-padded to `positions`. -/// The caller guarantees `bytes.len() < positions` and no NUL bytes. -pub fn encode_value(bytes: &[u8], codes: &Matrix, positions: usize) -> Vec { - debug_assert!(bytes.len() < positions && !bytes.contains(&0)); - let mut v = Vec::with_capacity(positions * codes.cols); - for p in 0..positions { - v.extend_from_slice(codes.row(bytes.get(p).copied().unwrap_or(0) as usize)); - } - v -} - -#[derive(Debug, Clone, PartialEq)] -pub struct Decoded { - pub bytes: Vec, - /// Smallest (second-nearest - nearest) squared distance over decoded - /// positions: how far the read is from flipping to another byte. - pub margin: f32, -} - -/// Nearest-code cleanup per position until the NUL code. -pub fn decode(read: &[f32], codes: &Matrix) -> Decoded { - let e = codes.cols; - let mut bytes = Vec::new(); - let mut margin = f32::INFINITY; - for pos in read.chunks_exact(e) { - let (mut b1, mut d1, mut d2) = (0usize, f32::INFINITY, f32::INFINITY); - for b in 0..codes.rows { - let d: f32 = codes.row(b).iter().zip(pos).map(|(c, x)| (c - x) * (c - x)).sum(); - if d < d1 { - d2 = d1; - d1 = d; - b1 = b; - } else if d < d2 { - d2 = d; - } - } - margin = margin.min(d2 - d1); - if b1 == 0 { - break; - } - bytes.push(b1 as u8); - } - Decoded { bytes, margin: if margin.is_finite() { margin } else { 0.0 } } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn exact_codes_decode_exactly_and_superposition_follows_the_dominant_slot() { - let codes = Matrix::uniform(256, 16, 1, "embed", 1.0); - let a = encode_value(b"blue", &codes, 8); - let b = encode_value(b"green", &codes, 8); - assert_eq!(decode(&a, &codes).bytes, b"blue"); - let r: Vec = a.iter().zip(&b).map(|(x, y)| 0.9 * x + 0.1 * y).collect(); - assert_eq!(decode(&r, &codes).bytes, b"blue"); - } -} diff --git a/crates/zerolm-memory/src/value_circuits.rs b/crates/zerolm-memory/src/value_circuits.rs new file mode 100644 index 0000000..1026325 --- /dev/null +++ b/crates/zerolm-memory/src/value_circuits.rs @@ -0,0 +1,237 @@ +//! The compiled value codec, `W_FAST`, and write circuits bound to a store +//! (`zerolm_wsp::circuits::{value_codec, memory_write}`): `value-encode` turns +//! value bytes into code rows, `fast-recall` and `fast-write` read and update +//! the short-lived associative matrix `memory.fast`, and `memory-write`, +//! `memory-forget`, `memory-compact`, and `memory-history` decide every change +//! to the semantic and episodic memory (the host moves the rows they specify). The host copies bytes into +//! input rows, binds model tensors, and reads outputs; it inspects no byte. + +use crate::persistent_memory::MemoryConfig; +use zerolm_engine::isa::{self, bundle, Bindings, Buf, Outputs, View}; +use zerolm_wsp::circuits::memory_write::WriteParams; +use zerolm_wsp::circuits::value_codec::{CodecParams, FastParams}; +use zerolm_wsp::{compile, Circuit, Compiled, Spec}; + +pub const ENCODE_ID: &str = "value-encode"; +pub const RECALL_ID: &str = "fast-recall"; +pub const WRITE_ID: &str = "fast-write"; +pub const MEM_WRITE_ID: &str = "memory-write"; +pub const FORGET_ID: &str = "memory-forget"; +pub const COMPACT_ID: &str = "memory-compact"; +pub const HISTORY_ID: &str = "memory-history"; +/// Normalization epsilon of the fast-weight delta rule. +pub const FAST_EPS: f32 = 1e-6; + +fn codec(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> CodecParams { + CodecParams { positions: cfg.positions(), code_rows, code_dim } +} + +fn fast(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> FastParams { + FastParams { + codec: codec(cfg, code_rows, code_dim), + key_dim: cfg.key_dim, + expansion: cfg.fast_expansion, + active: cfg.fast_active, + lr: cfg.fast_lr, + decay: cfg.fast_decay, + eps: FAST_EPS, + } +} + +fn write(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> WriteParams { + WriteParams { + codec: codec(cfg, code_rows, code_dim), + slots: cfg.slots, + key_dim: cfg.key_dim, + key_words: cfg.max_key_words * cfg.key_dim, + episodes: cfg.episodic_capacity, + same_key_threshold: cfg.same_key_threshold, + unconfirmed: crate::persistent_memory::UNCONFIRMED, + confirmed: crate::persistent_memory::CONFIRMED, + } +} + +fn spec(id: &str, circuit: Circuit) -> Spec { + let seed_entry = match id { + MEM_WRITE_ID | FORGET_ID | COMPACT_ID | HISTORY_ID => "memory-write-circuits", + _ => "value-codec-circuits", + }; + Spec { id: id.into(), version: 1, seed_entry: seed_entry.into(), description: String::new(), circuit } +} + +/// The seven specs for a configuration and code table shape (equal to the +/// shipped specs for the frozen default configuration). +pub fn specs(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> [Spec; 7] { + [ + spec(ENCODE_ID, Circuit::ValueEncode(codec(cfg, code_rows, code_dim))), + spec(RECALL_ID, Circuit::FastRecall(fast(cfg, code_rows, code_dim))), + spec(WRITE_ID, Circuit::FastWrite(fast(cfg, code_rows, code_dim))), + spec(MEM_WRITE_ID, Circuit::MemoryWrite(write(cfg, code_rows, code_dim))), + spec(FORGET_ID, Circuit::MemoryForget(write(cfg, code_rows, code_dim))), + spec(COMPACT_ID, Circuit::MemoryCompact(write(cfg, code_rows, code_dim))), + spec(HISTORY_ID, Circuit::MemoryHistory(write(cfg, code_rows, code_dim))), + ] +} + +/// A value decoded by a circuit. +#[derive(Debug, Clone, PartialEq)] +pub struct Decoded { + pub bytes: Vec, + /// Smallest top-1 minus top-2 code score over the decoded positions (up + /// to the first NUL): how far the read is from flipping to another byte. + pub margin: f32, +} + +/// One recorded circuit run (evaluation only): the exported bundle (program +/// and every weight as bound), the inputs (including initial state), and the +/// engine's outputs and final state. +#[derive(Debug, Clone)] +pub struct Recorded { + pub program: String, + pub bundle: Vec, + pub inputs: Vec<(String, Buf)>, + pub outputs: Outputs, +} + +pub struct ValueCircuits { + pub encode: Compiled, + pub recall: Compiled, + pub write: Compiled, + pub mem_write: Compiled, + pub forget: Compiled, + pub compact: Compiled, + pub history: Compiled, + positions: usize, + recorder: std::cell::RefCell>>, +} + +impl ValueCircuits { + pub fn new(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> Self { + let [encode, recall, write, mem_write, forget, compact, history] = specs(cfg, code_rows, code_dim).map(|s| compile(&s).expect("valid memory configuration compiles")); + ValueCircuits { encode, recall, write, mem_write, forget, compact, history, positions: cfg.positions(), recorder: Default::default() } + } + + pub fn all(&self) -> [&Compiled; 7] { + [&self.encode, &self.recall, &self.write, &self.mem_write, &self.forget, &self.compact, &self.history] + } + + /// Value bytes copied into a NUL-padded row (data movement only). The + /// caller has checked the length and that no byte is NUL. + pub fn value_input(&self, value: &str) -> Buf { + let mut row = vec![0.0f32; self.positions]; + for (slot, b) in row.iter_mut().zip(value.bytes()) { + *slot = b as f32; + } + Buf::row(row) + } + + pub fn record(&self, on: bool) { + *self.recorder.borrow_mut() = if on { Some(Vec::new()) } else { None }; + } + + pub fn take_recorded(&self) -> Vec { + self.recorder.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default() + } + + fn run(&self, c: &Compiled, inputs: Vec<(&str, Buf)>, weights: &[(&str, View<'_>)], state: Option<(&str, Buf)>) -> Outputs { + let mut w: Vec<(&str, View<'_>)> = weights.to_vec(); + w.extend(c.tensors.iter().map(|t| (t.name.as_str(), t.view()))); + let mut b = Bindings::new(); + for (n, t) in &inputs { + b = b.input(n, t.clone()); + } + if let Some((n, t)) = &state { + b = b.state(n, t.clone()); + } + for (n, v) in &w { + b = b.weight(n, *v); + } + let out = isa::run(&c.program, b).unwrap_or_else(|e| panic!("{} runs on its own store: {e}", c.id)); + if let Some(rec) = self.recorder.borrow_mut().as_mut() { + let mut all: Vec<(String, Buf)> = inputs.into_iter().map(|(n, t)| (n.to_string(), t)).collect(); + all.extend(state.map(|(n, t)| (n.to_string(), t))); + rec.push(Recorded { program: c.id.clone(), bundle: bundle::encode(&c.program, &w), inputs: all, outputs: out.clone() }); + } + out + } + + /// Code row of a value (`value-encode`). + pub fn encode_value(&self, codes: View<'_>, value: &str) -> Vec { + let o = self.run(&self.encode, vec![("value", self.value_input(value))], &[("codes", codes)], None); + o.get("row").data.clone() + } + + /// Run one of the write circuits with its inputs and bound tensors. + pub fn decide(&self, c: &Compiled, inputs: Vec<(&str, Buf)>, weights: &[(&str, View<'_>)]) -> Outputs { + self.run(c, inputs, weights, None) + } + + /// Recall from `W_FAST` (`fast-recall`). + pub fn recall_fast(&self, codes: View<'_>, separator: View<'_>, fast: View<'_>, key: &[f32]) -> Decoded { + let o = self.run(&self.recall, vec![("key", Buf::row(key.to_vec()))], &[("separator", separator), ("fast", fast), ("codes", codes)], None); + decoded(&o) + } + + /// Write one association into `W_FAST` (`fast-write`); returns the new matrix. + pub fn write_fast(&self, codes: View<'_>, separator: View<'_>, fast: Buf, key: &[f32], value: &str) -> Buf { + let mut o = self.run( + &self.write, + vec![("key", Buf::row(key.to_vec())), ("value", self.value_input(value))], + &[("separator", separator), ("codes", codes)], + Some(("fast", fast)), + ); + o.state.remove("fast").expect("fast-write returns its state") + } +} + +fn decoded(o: &Outputs) -> Decoded { + Decoded { bytes: crate::read_circuit::bytes(o.get("bytes")), margin: o.scalar("margin") } +} + +#[cfg(test)] +mod tests { + use super::*; + use zerolm_tensor::Matrix; + + fn v(m: &Matrix) -> View<'_> { + View::new(m.rows, m.cols, &m.data) + } + + fn setup() -> (MemoryConfig, Matrix, Matrix, ValueCircuits) { + let cfg = MemoryConfig::default(); + let codes = zerolm_engine::bootstrap::default_model(cfg.seed).embed; + let sep = Matrix::uniform(cfg.fast_expansion, cfg.key_dim, cfg.seed, crate::persistent_memory::SEPARATOR, 1.0); + let c = ValueCircuits::new(&cfg, codes.rows, codes.cols); + (cfg, codes, sep, c) + } + + #[test] + fn encoded_rows_are_the_code_rows() { + let (cfg, codes, _, c) = setup(); + let row = c.encode_value(v(&codes), "blue"); + assert_eq!(row.len(), cfg.positions() * codes.cols); + for (p, b) in b"blue\0\0".iter().enumerate() { + assert_eq!(&row[p * codes.cols..(p + 1) * codes.cols], codes.row(*b as usize), "position {p}"); + } + } + + #[test] + fn fast_writes_are_recalled_and_fade_with_decay() { + let (cfg, codes, sep, c) = setup(); + let key = |i: u64| { + let mut r = zerolm_tensor::Rng::from_tag(i, "test-key"); + (0..cfg.key_dim).map(|_| r.uniform(1.0)).collect::>() + }; + let mut m = Buf::zeros(cfg.positions() * codes.cols, cfg.fast_expansion); + m = c.write_fast(v(&codes), v(&sep), m, &key(1), "amber"); + let read = |m: &Buf, i| c.recall_fast(v(&codes), v(&sep), View::new(m.rows, m.cols, &m.data), &key(i)).bytes; + assert_eq!(read(&m, 1), b"amber"); + let once = m.clone(); + for i in 2..400 { + m = c.write_fast(v(&codes), v(&sep), m, &key(i), &format!("v{i}")); + } + assert_ne!(read(&m, 1), b"amber", "decayed after 400 later writes"); + assert_eq!(read(&m, 399), b"v399", "the newest association is recalled"); + assert_ne!(once.data, m.data); + } +} diff --git a/crates/zerolm-memory/tests/memory.rs b/crates/zerolm-memory/tests/memory.rs index e6f8050..a7b13dd 100644 --- a/crates/zerolm-memory/tests/memory.rs +++ b/crates/zerolm-memory/tests/memory.rs @@ -405,3 +405,40 @@ fn over_long_text_is_refused_and_changes_nothing() { assert!(matches!(m.query(&long), Err(zerolm_memory::MemoryError::TextTooLong { .. }))); assert_eq!(m.checkpoint_bytes(), before); } + +#[test] +fn the_default_value_circuits_are_the_shipped_pinned_specs() { + use zerolm_memory::value_circuits; + let cfg = zerolm_memory::MemoryConfig::default(); + let store = zerolm_memory::MemoryStore::new(cfg).unwrap(); + let tensors = store.tensors(); + for (spec, built) in value_circuits::specs(&cfg, 256, zerolm_engine::bootstrap::EMBED_DIM).iter().zip(store.value_circuits().all()) { + let shipped = zerolm_wsp::shipped(&spec.id).unwrap(); + assert_eq!(zerolm_wsp::compile(spec).unwrap().sha256(), shipped.sha256(), "MemoryConfig::default() must compile to model/prog/{}.json", spec.id); + assert_eq!(built.sha256(), shipped.sha256()); + let prog: Vec<_> = tensors.iter().filter(|t| t.name.starts_with(&format!("prog.{}.", spec.id))).collect(); + assert_eq!(prog.len(), shipped.tensors.len()); + assert!(prog.iter().all(|t| t.group == "w_prog")); + } +} + +#[test] +fn the_write_circuits_use_the_store_metadata_layout() { + use zerolm_memory::persistent_memory::{ep_field, field, ACTIVE, CONFIRMED, DELETED, EMPTY, OPS, SUPERSEDED, UNCONFIRMED}; + use zerolm_wsp::circuits::memory_write as w; + assert_eq!((w::META_COLS, w::EP_COLS), (field::COUNT, ep_field::COUNT)); + assert_eq!( + [w::field::STATUS, w::field::CONFIDENCE, w::field::UPDATED_LO, w::field::UPDATED_HI, w::field::SUPERSEDES, w::field::SUPERSEDED_BY, w::field::CONTRADICTIONS], + [field::STATUS, field::CONFIDENCE, field::UPDATED_LO, field::UPDATED_HI, field::SUPERSEDES, field::SUPERSEDED_BY, field::CONTRADICTIONS] + ); + assert_eq!([w::ep_field::OP, w::ep_field::SLOT, w::ep_field::VALID], [ep_field::OP, ep_field::SLOT, ep_field::VALID]); + assert_eq!([w::EMPTY, w::ACTIVE, w::SUPERSEDED, w::DELETED], [EMPTY, ACTIVE, SUPERSEDED, DELETED]); + let code = |op: &str| OPS.iter().position(|o| *o == op).unwrap() as f32; + assert_eq!( + [w::OP_ADD, w::OP_UNCHANGED, w::OP_CONTRADICTION, w::OP_SUPERSEDE, w::OP_CONFIRM, w::OP_FORGET], + [code("add"), code("unchanged"), code("contradiction"), code("supersede"), code("confirm"), code("forget")] + ); + let spec = zerolm_memory::value_circuits::specs(&zerolm_memory::MemoryConfig::default(), 256, zerolm_engine::bootstrap::EMBED_DIM); + let zerolm_wsp::Circuit::MemoryWrite(p) = &spec[3].circuit else { panic!("memory-write spec") }; + assert_eq!((p.unconfirmed, p.confirmed), (UNCONFIRMED, CONFIRMED)); +} diff --git a/crates/zerolm-reasoner/src/circuits.rs b/crates/zerolm-reasoner/src/circuits.rs index fdb7898..226f2c3 100644 --- a/crates/zerolm-reasoner/src/circuits.rs +++ b/crates/zerolm-reasoner/src/circuits.rs @@ -9,9 +9,12 @@ use crate::{field, Reasoner, ReasonerConfig, ROLES}; use std::collections::BTreeMap; -use zerolm_engine::binding::Hv; +use zerolm_engine::binding::{Hv, Space}; use zerolm_engine::isa::{self, bundle, Bindings, Buf, Outputs, View}; use zerolm_wsp::circuits::atom_encode::Params as AtomParams; +use zerolm_wsp::circuits::decide::CountParams; +use zerolm_wsp::circuits::probe::{CleanupParams, ProbeParams}; +use zerolm_wsp::circuits::number::{DecodeParams as NumberDecodeParams, EncodeParams as NumberEncodeParams}; use zerolm_wsp::circuits::relational::{Params, OPERATORS}; use zerolm_wsp::{compile, Circuit, Compiled, Spec}; @@ -37,6 +40,26 @@ pub fn reach_spec(cfg: &ReasonerConfig) -> Spec { Spec { id: REACH_ID.into(), version: 1, seed_entry: "relation-reach-circuit".into(), description: String::new(), circuit: Circuit::RelationReach(params(cfg)) } } +pub const JOIN_ID: &str = "relation-join"; +pub const LEVELS_ID: &str = "relation-levels"; +pub const RULES_ID: &str = "relation-rules"; +pub const COUNTS_ID: &str = "relation-counts"; +pub const VERDICT_ID: &str = "relation-verdict"; + +/// The spec of one decision circuit (`zerolm_wsp::circuits::decide`). +pub fn decide_spec(cfg: &ReasonerConfig, id: &str) -> Spec { + let p = params(cfg); + let circuit = match id { + JOIN_ID => Circuit::RelationJoin(p), + LEVELS_ID => Circuit::RelationLevels(p), + RULES_ID => Circuit::RelationRules(p), + COUNTS_ID => Circuit::RelationCounts(CountParams { relational: p, range: cfg.number_range as u32 }), + VERDICT_ID => Circuit::RelationVerdict(p), + other => panic!("no decision circuit {other}"), + }; + Spec { id: id.into(), version: 1, seed_entry: "relation-decide-circuits".into(), description: String::new(), circuit } +} + pub const ATOM_ID: &str = "atom-encode"; /// Atom encoder width: names up to `ATOM_BYTES - 1` bytes (longer names are cut). @@ -67,7 +90,7 @@ pub struct Atoms { pub circuit: Compiled, pub table: zerolm_tensor::Matrix, cache: std::cell::RefCell>, - recorder: std::cell::RefCell>>, + recorder: Recorder, dim: usize, } @@ -93,34 +116,121 @@ impl Atoms { for (slot, b) in row[1..].iter_mut().zip(key.bytes()) { *slot = b as f32; } - let inputs = vec![("name", Buf::row(row))]; - let mut w: Vec<(&str, View<'_>)> = vec![("table", view(&self.table, self.table.rows))]; - w.extend(self.circuit.tensors.iter().map(|t| (t.name.as_str(), t.view()))); - let mut b = Bindings::new(); - for (n, t) in &inputs { - b = b.input(n, t.clone()); + let w = with_consts(&self.circuit, vec![("table", view(&self.table, self.table.rows))]); + let out = run(&self.circuit, vec![("name", Buf::row(row))], &w, &self.recorder); + let h = Hv::from_data(self.dim, &out.get("atom").data); + self.cache.borrow_mut().insert(key, h.clone()); + h + } +} + +pub const NUMBER_ENCODE_ID: &str = "number-encode"; +pub const NUMBER_DECODE_ID: &str = "number-decode"; +/// Encoder width: a sign and up to 7 digits (the parser accepts 0..=1,000,000). +pub const NUMBER_BYTES: usize = 8; +/// Decimal places of `core.reasoner.number_digits`. +pub const NUMBER_PLACES: usize = 7; + +pub fn number_encode_spec(cfg: &ReasonerConfig) -> Spec { + Spec { + id: NUMBER_ENCODE_ID.into(), + version: 1, + seed_entry: "number-encode-circuit".into(), + description: String::new(), + circuit: Circuit::NumberEncode(NumberEncodeParams { numeral_bytes: NUMBER_BYTES, places: NUMBER_PLACES, dim: cfg.dim }), + } +} + +pub fn number_decode_spec(cfg: &ReasonerConfig) -> Spec { + Spec { + id: NUMBER_DECODE_ID.into(), + version: 1, + seed_entry: "number-decode-circuit".into(), + description: String::new(), + circuit: Circuit::NumberDecode(NumberDecodeParams { range: cfg.number_range as u32, dim: cfg.dim }), + } +} + +/// A number decoded by `number-decode`: its value and the clean codebook row. +#[derive(Debug, Clone)] +pub struct Decoded { + pub value: i64, + pub clean: Hv, +} + +/// The compiled number encoder and decoder with their seeded tables +/// (`core.reasoner.number_digits`, `core.reasoner.number_codebook`). +pub struct Numbers { + pub encoder: Compiled, + pub decoder: Compiled, + pub digits: zerolm_tensor::Matrix, + pub codebook: zerolm_tensor::Matrix, + cache: std::cell::RefCell>, + recorder: Recorder, + dim: usize, +} + +impl Numbers { + pub fn new(cfg: &ReasonerConfig) -> Self { + let space = Space::new(cfg.seed, cfg.dim); + let d2 = 2 * cfg.dim; + let rows = (2 * cfg.number_range + 1) as usize; + Numbers { + encoder: compile(&number_encode_spec(cfg)).expect("valid reasoning config compiles"), + decoder: compile(&number_decode_spec(cfg)).expect("valid reasoning config compiles"), + digits: zerolm_tensor::Matrix { rows: 10 * NUMBER_PLACES, cols: d2, data: space.digit_table(NUMBER_PLACES) }, + codebook: zerolm_tensor::Matrix { rows, cols: d2, data: space.codebook(cfg.number_range) }, + cache: Default::default(), + recorder: Default::default(), + dim: cfg.dim, } - for (n, v) in &w { - b = b.weight(n, *v); + } + + fn run(&self, c: &Compiled, input: (&str, Buf), table: (&str, View<'_>)) -> Outputs { + run(c, vec![input], &with_consts(c, vec![table]), &self.recorder) + } + + /// The phasor of an integer from its decimal numeral bytes, computed by + /// `number-encode` (memoized by the integer). + pub fn encode(&self, n: i64) -> Hv { + if let Some(h) = self.cache.borrow().get(&n) { + return h.clone(); } - let out = isa::run(&self.circuit.program, b).expect("atom circuit runs"); - if let Some(rec) = self.recorder.borrow_mut().as_mut() { - rec.push(Recorded { - program: self.circuit.id.clone(), - bundle: bundle::encode(&self.circuit.program, &w), - inputs: inputs.into_iter().map(|(n, t)| (n.to_string(), t)).collect(), - outputs: out.clone(), - }); + // The parser hands over an integer; its canonical decimal numeral is + // the circuit input (bytes copied, none inspected). + let numeral = n.to_string(); + assert!(numeral.len() <= NUMBER_BYTES, "numeral wider than the number-encode input"); + let mut row = vec![0.0f32; NUMBER_BYTES]; + for (slot, b) in row.iter_mut().zip(numeral.bytes()) { + *slot = b as f32; } - let h = Hv::from_data(self.dim, &out.get("atom").data); - self.cache.borrow_mut().insert(key, h.clone()); + let out = self.run(&self.encoder, ("numeral", Buf::row(row)), ("digits", self.digits_view())); + let h = Hv::from_data(self.dim, &out.get("number").data); + self.cache.borrow_mut().insert(n, h.clone()); h } + + /// Clean up a noisy number vector with `number-decode`. + pub fn decode(&self, v: &Hv) -> Decoded { + let out = self.run(&self.decoder, ("v", Buf::row(v.data.clone())), ("numbers", view(&self.codebook, self.codebook.rows))); + Decoded { value: out.get("value").data[0] as i64, clean: Hv::from_data(self.dim, &out.get("clean").data) } + } + + fn digits_view(&self) -> View<'_> { + view(&self.digits, self.digits.rows) + } } pub struct Circuits { pub graph: Compiled, pub reach: Compiled, + pub probe: Compiled, + pub cleanup: Compiled, + pub join: Compiled, + pub levels: Compiled, + pub rules: Compiled, + pub counts: Compiled, + pub verdict: Compiled, } impl Circuits { @@ -128,8 +238,20 @@ impl Circuits { Circuits { graph: compile(&graph_spec(cfg)).expect("valid reasoning config compiles"), reach: compile(&reach_spec(cfg)).expect("valid reasoning config compiles"), + probe: compile(&probe_spec(cfg)).expect("valid reasoning config compiles"), + cleanup: compile(&cleanup_spec(cfg)).expect("valid reasoning config compiles"), + join: compile(&decide_spec(cfg, JOIN_ID)).expect("valid reasoning config compiles"), + levels: compile(&decide_spec(cfg, LEVELS_ID)).expect("valid reasoning config compiles"), + rules: compile(&decide_spec(cfg, RULES_ID)).expect("valid reasoning config compiles"), + counts: compile(&decide_spec(cfg, COUNTS_ID)).expect("valid reasoning config compiles"), + verdict: compile(&decide_spec(cfg, VERDICT_ID)).expect("valid reasoning config compiles"), } } + + /// Every compiled program, in checkpoint order. + pub fn all(&self) -> [&Compiled; 9] { + [&self.graph, &self.reach, &self.probe, &self.cleanup, &self.join, &self.levels, &self.rules, &self.counts, &self.verdict] + } } /// C5 output for one memory state. @@ -142,19 +264,175 @@ pub struct Graph { pub neg: Buf, } -/// Reached symbols (codebook index) -> premise slots of the shortest proof. -pub type Reach = BTreeMap>; - fn view(m: &zerolm_tensor::Matrix, rows: usize) -> View<'_> { View::new(rows, m.cols, &m.data[..rows * m.cols]) } +/// Records circuit runs for reference replay (evaluation only). +#[derive(Default)] +pub struct Recorder { + runs: std::cell::RefCell>>, + /// The last bundle of each program, reused while its weights are unchanged. + last: std::cell::RefCell>>>, +} + +impl Recorder { + pub fn set(&self, on: bool) { + *self.runs.borrow_mut() = if on { Some(Vec::new()) } else { None }; + self.last.borrow_mut().clear(); + } + + pub fn take(&self) -> Vec { + self.runs.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default() + } +} + +/// The caller's external tensors followed by the program's compiled constants. +pub(crate) fn with_consts<'a>(c: &'a Compiled, mut ext: Vec<(&'a str, View<'a>)>) -> Vec<(&'a str, View<'a>)> { + ext.extend(c.tensors.iter().map(|t| (t.name.as_str(), t.view()))); + ext +} + +/// Bind, run, and (when recording) export one program run. +pub(crate) fn run(c: &Compiled, inputs: Vec<(&str, Buf)>, weights: &[(&str, View<'_>)], rec: &Recorder) -> Outputs { + let mut b = Bindings::new(); + for (n, t) in &inputs { + b = b.input(n, t.clone()); + } + for (n, v) in weights { + b = b.weight(n, *v); + } + let out = isa::run(&c.program, b).unwrap_or_else(|e| panic!("{} runs on its own memory: {e}", c.id)); + if let Some(runs) = rec.runs.borrow_mut().as_mut() { + let encoded = bundle::encode(&c.program, weights); + let mut last = rec.last.borrow_mut(); + let shared = match last.get(&c.id) { + Some(p) if **p == encoded => p.clone(), + _ => { + let p = std::sync::Arc::new(encoded); + last.insert(c.id.clone(), p.clone()); + p + } + }; + runs.push(Recorded { program: c.id.clone(), bundle: shared, inputs: inputs.into_iter().map(|(n, t)| (n.to_string(), t)).collect(), outputs: out.clone() }); + } + out +} + +pub const PROBE_ID: &str = "relation-probe"; +pub const CLEANUP_ID: &str = "symbol-cleanup"; + +pub fn probe_spec(cfg: &ReasonerConfig) -> Spec { + Spec { + id: PROBE_ID.into(), + version: 1, + seed_entry: "relation-probe-circuits".into(), + description: String::new(), + circuit: Circuit::RelationProbe(ProbeParams { dim: cfg.dim, meta_cols: field::COUNT, n_roles_col: field::N_ROLES, match_margin: cfg.match_margin, discourse_decay: cfg.pronoun_decay }), + } +} + +pub fn cleanup_spec(cfg: &ReasonerConfig) -> Spec { + Spec { + id: CLEANUP_ID.into(), + version: 1, + seed_entry: "relation-probe-circuits".into(), + description: String::new(), + circuit: Circuit::SymbolCleanup(CleanupParams { dim: cfg.dim, cleanup_min: cfg.cleanup_min, symbol_same: cfg.symbol_same, pronoun_min: cfg.pronoun_min }), + } +} + +/// Outputs of one `relation-probe` (statement write) run. +#[derive(Debug, Clone)] +pub struct Store { + /// The circuit decided to store the statement (resolved and not a duplicate). + pub stored: bool, + /// The slot row and metadata row to append (zero when nothing is stored). + pub row: Vec, + pub meta_row: Vec, + /// The lowest duplicate slot, if the statement is already stored. + pub dup_slot: Option, + /// The discourse state after the statement. + pub context: Hv, +} + +/// Outputs of one `symbol-cleanup` run. +#[derive(Debug, Clone)] +pub struct Cleaned { + pub found: Option, + /// The circuit decided to add `row` to the codebook (a new symbol). + pub added: bool, + pub row: Vec, +} + +/// Which threshold `symbol-cleanup` applies. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CleanupMode { + Same, + Pronoun, +} + +impl Reasoner { + /// Run `relation-probe` for a statement's role-filler `parts` with the + /// metadata `record` it would get; `gate` is 1 when its subject resolved. + /// An empty memory is bound as one all-zero inactive row (it never + /// matches), so every tensor has at least one row. + pub(crate) fn store(&self, parts: &[(usize, Hv)], record: &[f32], gate: f32) -> Store { + let d2 = 2 * self.cfg.dim; + let fillers: Vec = parts.iter().flat_map(|(_, f)| f.data.iter().copied()).collect(); + let sel: Vec = parts.iter().flat_map(|(r, _)| (0..ROLES.len()).map(move |j| if j == *r { 1.0 } else { 0.0 })).collect(); + let roles = self.roles_tensor(); + let rows = self.n_slots.max(1); + let zero_meta = vec![0.0f32; rows * field::COUNT]; + let meta = if self.n_slots > 0 { view(&self.meta, rows) } else { View::new(rows, field::COUNT, &zero_meta) }; + let w = with_consts(&self.circuits.probe, vec![("slots", view(&self.slots, rows)), ("meta", meta), ("roles", View::new(ROLES.len(), d2, &roles))]); + let inputs = vec![ + ("fillers", Buf::new(parts.len(), d2, fillers)), + ("role_sel", Buf::new(parts.len(), ROLES.len(), sel)), + ("record", Buf::row(record.to_vec())), + ("gate", Buf::scalar(gate)), + ("context", Buf::row(self.state.context.data.clone())), + ]; + let o = self.exec(&self.circuits.probe, inputs, &w); + Store { + stored: o.scalar("stored") == 1.0, + row: o.get("row").data.clone(), + meta_row: o.get("meta_row").data.clone(), + dup_slot: (o.scalar("dup") == 1.0).then(|| o.scalar("dup_slot") as usize), + context: Hv::from_data(self.cfg.dim, &o.get("context_out").data), + } + } + + /// Run `symbol-cleanup`. With `gate` 1, a vector with no codebook entry + /// is returned as the row to add. An empty codebook is bound as one zero + /// row (cosine 0, never found). + pub(crate) fn symbol_cleanup_full(&self, v: &Hv, mode: CleanupMode, gate: f32) -> Cleaned { + let m = match mode { + CleanupMode::Same => [0.0, 1.0, 0.0], + CleanupMode::Pronoun => [0.0, 0.0, 1.0], + }; + let rows = self.n_sym.max(1); + let zero = vec![0.0f32; 2 * self.cfg.dim]; + let book = if self.n_sym > 0 { view(&self.codebook, rows) } else { View::new(1, 2 * self.cfg.dim, &zero) }; + let w = with_consts(&self.circuits.cleanup, vec![("codebook", book)]); + let inputs = vec![("v", Buf::row(v.data.clone())), ("mode", Buf::row(m.to_vec())), ("gate", Buf::scalar(gate))]; + let o = self.exec(&self.circuits.cleanup, inputs, &w); + Cleaned { found: (o.scalar("found") == 1.0).then(|| o.scalar("best") as usize), added: o.scalar("added") == 1.0, row: o.get("row").data.clone() } + } + + /// The codebook index nearest to `v` if its cosine reaches the mode's threshold. + pub(crate) fn symbol_cleanup(&self, v: &Hv, mode: CleanupMode) -> Option { + self.symbol_cleanup_full(v, mode, 0.0).found + } +} + /// One recorded circuit run (evaluation only): the exported bundle (program /// and every weight exactly as bound), the inputs, and the engine's outputs. #[derive(Debug, Clone)] pub struct Recorded { pub program: String, - pub bundle: Vec, + /// Shared by consecutive runs of one program with identical weights. + pub bundle: std::sync::Arc>, pub inputs: Vec<(String, Buf)>, pub outputs: Outputs, } @@ -166,36 +444,19 @@ impl Reasoner { /// Start (or stop) recording every circuit run for reference replay. pub fn record_circuits(&self, on: bool) { - *self.recorder.borrow_mut() = if on { Some(Vec::new()) } else { None }; - *self.atoms.recorder.borrow_mut() = if on { Some(Vec::new()) } else { None }; + for r in [&self.recorder, &self.atoms.recorder, &self.numbers.recorder] { + r.set(on); + } } /// Take the runs recorded so far. pub fn take_recorded(&self) -> Vec { - let mut runs = self.recorder.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default(); - runs.extend(self.atoms.recorder.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default()); - runs + [&self.recorder, &self.atoms.recorder, &self.numbers.recorder].iter().flat_map(|r| r.take()).collect() } /// Bind, run, and (when recording) export one program run. fn exec(&self, c: &Compiled, inputs: Vec<(&str, Buf)>, weights: &[(&str, View<'_>)]) -> Outputs { - let mut b = Bindings::new(); - for (n, t) in &inputs { - b = b.input(n, t.clone()); - } - for (n, v) in weights { - b = b.weight(n, *v); - } - let out = isa::run(&c.program, b).unwrap_or_else(|e| panic!("{} runs on its own memory: {e}", c.id)); - if let Some(rec) = self.recorder.borrow_mut().as_mut() { - rec.push(Recorded { - program: c.id.clone(), - bundle: bundle::encode(&c.program, weights), - inputs: inputs.into_iter().map(|(n, t)| (n.to_string(), t)).collect(), - outputs: out.clone(), - }); - } - out + run(c, inputs, weights, &self.recorder) } /// The decoded relation graph for the current memory state (cached). @@ -226,51 +487,97 @@ impl Reasoner { [o.class_inclusion, o.transitivity, o.attribute_inheritance, o.comparison, o.arithmetic, o.temporal_ordering, o.negation, o.conditionals] } - /// Run C6 from symbol `from` over positive `rel` edges for operator `op` - /// (a disabled operator's enable block is bound as zero: direct edges only). - pub(crate) fn run_reach(&self, from: usize, to: Option, rel: &Hv, op: &str) -> Option { - if self.n_slots == 0 || self.n_sym == 0 { - return None; - } + /// The program's compiled constants, with the enable constant of every + /// disabled operator bound as zero (evaluation ablations). + fn op_consts<'a>(&self, c: &'a Compiled, zero: &'a [f32; 1]) -> Vec<(&'a str, View<'a>)> { + let disabled: Vec = OPERATORS.iter().zip(self.op_flags()).filter(|(_, on)| !on).map(|(op, _)| format!("enable_{op}")).collect(); + c.tensors.iter().map(|t| (t.name.as_str(), if disabled.iter().any(|d| *d == t.name) { View::new(1, 1, zero) } else { t.view() })).collect() + } + + /// Run a graph-reading circuit (C6 or a decision circuit) with the memory, + /// the C5 outputs, and `extra` model tensors bound. `pol` is the name C6 + /// gives its polarity row; the decision circuits read `pos` and `neg`. + fn run_on_graph(&self, c: &Compiled, inputs: Vec<(&str, Buf)>, extra: Vec<(&str, View<'_>)>) -> Outputs { let g = self.graph(); let roles = self.roles_tensor(); - let opi = OPERATORS.iter().position(|o| *o == op).expect("known operator"); - let mut sel = vec![0.0f32; OPERATORS.len()]; - sel[opi] = 1.0; - let x = self.code(from); - let y = to.map(|t| self.code(t)).unwrap_or_else(|| x.clone()); - let inputs = vec![("x", Buf::row(x.data)), ("y", Buf::row(y.data)), ("rel", Buf::row(rel.data.clone())), ("operator", Buf::row(sel))]; let mut w: Vec<(&str, View<'_>)> = vec![ ("slots", view(&self.slots, self.n_slots)), ("codebook", view(&self.codebook, self.n_sym)), ("roles", View::new(ROLES.len(), 2 * self.cfg.dim, &roles)), ("subj_sim", g.subj_sim.view()), ("obj_t", g.obj_t.view()), - ("pol", g.pos.view()), ]; - let zero = [0.0f32]; - let disabled: Vec = - OPERATORS.iter().zip(self.op_flags()).filter(|(_, on)| !on).map(|(op, _)| format!("enable_{op}")).collect(); - for t in &self.circuits.reach.tensors { - let off = disabled.iter().any(|d| *d == t.name); - w.push((t.name.as_str(), if off { View::new(1, 1, &zero) } else { t.view() })); - } - Some(self.exec(&self.circuits.reach, inputs, &w)) - } - - /// Every symbol reachable from `from` with its proof premises. - pub(crate) fn reach(&self, from: usize, rel: &Hv, op: &str) -> Reach { - let Some(o) = self.run_reach(from, None, rel, op) else { return Reach::new() }; - let reached = &o.get("reached").data; - let proofs = o.get("proofs"); - let mut out = Reach::new(); - for (j, &r) in reached.iter().enumerate() { - if r == 1.0 { - let row = &proofs.data[j * proofs.cols..(j + 1) * proofs.cols]; - out.insert(j, row.iter().enumerate().filter(|(_, &v)| v > 0.0).map(|(s, _)| s).collect()); - } + if c.id == REACH_ID { + w.push(("pol", g.pos.view())); + } else { + w.push(("pos", g.pos.view())); + w.push(("neg", g.neg.view())); } - out + w.extend(extra); + let zero = [0.0f32]; + w.extend(self.op_consts(c, &zero)); + self.exec(c, inputs, &w) + } + + /// C6 from vector `x` towards `y` over positive `rel` edges for operator + /// `op` (a disabled operator keeps only direct edges). The caller has + /// checked that the memory and codebook are not empty. + pub(crate) fn run_reach(&self, x: &Hv, y: &Hv, rel: &Hv, op: &str) -> Outputs { + let opi = OPERATORS.iter().position(|o| *o == op).expect("known operator"); + let mut sel = vec![0.0f32; OPERATORS.len()]; + sel[opi] = 1.0; + let inputs = vec![("x", Buf::row(x.data.clone())), ("y", Buf::row(y.data.clone())), ("rel", Buf::row(rel.data.clone())), ("operator", Buf::row(sel))]; + self.run_on_graph(&self.circuits.reach, inputs, Vec::new()) + } + + /// `relation-join`: a negative `rel` edge from x (plus `src`'s reached + /// nodes) to y (plus `tgt`'s), with `src`/`tgt` C6 outputs or `None`. + pub(crate) fn run_join(&self, x: &Hv, y: &Hv, rel: &Hv, src: Option<&Outputs>, tgt: Option<&Outputs>) -> Outputs { + let none = |_: ()| (Buf::zeros(1, self.n_sym), Buf::zeros(self.n_sym, self.n_slots)); + let side = |o: Option<&Outputs>| o.map(|o| (o.get("reached").clone(), o.get("proofs").clone())).unwrap_or_else(|| none(())); + let ((sm, sp), (tm, tp)) = (side(src), side(tgt)); + let inputs = vec![ + ("x", Buf::row(x.data.clone())), + ("y", Buf::row(y.data.clone())), + ("rel", Buf::row(rel.data.clone())), + ("src_more", sm), + ("src_proofs", sp), + ("tgt_more", tm), + ("tgt_proofs", tp), + ]; + self.run_on_graph(&self.circuits.join, inputs, Vec::new()) + } + + /// `relation-levels` ("can"). + pub(crate) fn run_levels(&self, x: &Hv, v: &Hv, rel_can: &Hv, rel_isa: &Hv) -> Outputs { + let inputs = vec![("x", Buf::row(x.data.clone())), ("v", Buf::row(v.data.clone())), ("rel_can", Buf::row(rel_can.data.clone())), ("rel_isa", Buf::row(rel_isa.data.clone()))]; + self.run_on_graph(&self.circuits.levels, inputs, Vec::new()) + } + + /// `relation-rules` (properties and conditionals). + pub(crate) fn run_rules(&self, x: &Hv, p: &Hv, rel_prop: &Hv, rel_implies: &Hv, something: &Hv) -> Outputs { + let inputs = vec![ + ("x", Buf::row(x.data.clone())), + ("p", Buf::row(p.data.clone())), + ("rel_prop", Buf::row(rel_prop.data.clone())), + ("rel_implies", Buf::row(rel_implies.data.clone())), + ("something", Buf::row(something.data.clone())), + ]; + self.run_on_graph(&self.circuits.rules, inputs, Vec::new()) + } + + /// `relation-counts` (arithmetic). + pub(crate) fn run_counts(&self, x: &Hv, rel_has: &Hv, rel_more: &Hv) -> Outputs { + let inputs = vec![("x", Buf::row(x.data.clone())), ("rel_has", Buf::row(rel_has.data.clone())), ("rel_more", Buf::row(rel_more.data.clone()))]; + let book = &self.numbers().codebook; + self.run_on_graph(&self.circuits.counts, inputs, vec![("numbers", view(book, book.rows))]) + } + + /// `relation-verdict`: findings to a one-hot verdict and premises. + pub(crate) fn run_verdict(&self, pos_found: f32, pos_proof: &Buf, neg_found: f32, neg_proof: &Buf) -> Outputs { + let inputs = vec![("pos_found", Buf::scalar(pos_found)), ("neg_found", Buf::scalar(neg_found)), ("pos_proof", pos_proof.clone()), ("neg_proof", neg_proof.clone())]; + let w = with_consts(&self.circuits.verdict, Vec::new()); + self.exec(&self.circuits.verdict, inputs, &w) } } @@ -301,4 +608,47 @@ mod tests { assert_eq!(again.data, vs[0].data); assert!(cosine(&a.encode("role", "cat"), &vs[0]).abs() < 0.15, "namespaces differ"); } + + #[test] + fn number_circuits_encode_numerals_and_decode_by_cleanup() { + let cfg = ReasonerConfig::default(); + let nums = Numbers::new(&cfg); + let space = Space::new(cfg.seed, cfg.dim); + for n in [0, 1, 7, 10, 42, 100, 907, 1000, -1, -2, -58, -1000, 123_456, 1_000_000, -999_999] { + let v = nums.encode(n); + assert!((sim(&v, &space.number(n)) - 1.0).abs() < 1e-4, "{n}: {}", sim(&v, &space.number(n))); + if n.abs() <= cfg.number_range { + let d = nums.decode(&v); + assert_eq!(d.value, n); + assert_eq!(d.clean.data, space.number(n).data, "clean row is the codebook row"); + } + } + // Addition is binding: 38 + (-45) = -7, decoded from clean vectors. + let (a, b) = (nums.decode(&nums.encode(38)), nums.decode(&nums.encode(-45))); + assert_eq!(nums.decode(&zerolm_engine::binding::bind(&a.clean, &b.clean)).value, -7); + // A noisy superposition still decodes to its dominant number. + let mut noisy = nums.encode(321); + noisy.add_assign(&Atoms::new(&cfg).encode("sym", "noise")); + assert_eq!(nums.decode(&noisy).value, 321); + } + + #[test] + fn number_circuit_blocks_carry_what_they_claim() { + let cfg = ReasonerConfig::default(); + let space = Space::new(cfg.seed, cfg.dim); + let mut nums = Numbers::new(&cfg); + nums.encoder.ablate_block("sign").unwrap(); + // Without the sign block, the output is zeroed (the sign mask is 0). + assert!(nums.encode(-12).is_zero()); + let mut nums = Numbers::new(&cfg); + nums.encoder.ablate_block("position").unwrap(); + // Without places every digit selects all 7 of its rows: a 49-term + // superposition in which number(12) is one term (cosine about 1/7). + assert!(cosine(&nums.encode(12), &space.number(12)) < 0.5); + let mut nums = Numbers::new(&cfg); + nums.decoder.ablate_block("readout").unwrap(); + assert_eq!(nums.decode(&space.number(12)).value, 0, "no values to read out"); + let nums = Numbers::new(&cfg); + assert!((sim(&nums.encode(-12), &space.number(-12)) - 1.0).abs() < 1e-4); + } } diff --git a/crates/zerolm-reasoner/src/lib.rs b/crates/zerolm-reasoner/src/lib.rs index 8884b23..9793964 100644 --- a/crates/zerolm-reasoner/src/lib.rs +++ b/crates/zerolm-reasoner/src/lib.rs @@ -20,7 +20,7 @@ pub mod oracle; use serde::{Deserialize, Serialize}; use std::path::Path; -use zerolm_engine::binding::{bind, bundle, sim_raw, unbind, Hv, Space}; +use zerolm_engine::binding::{bind, Hv}; use zerolm_engine::semantic_state::{parse, Fact, Pred, Rule, SemanticState, Term, Utterance}; use zerolm_memory::checkpoint::{self, CheckpointError, Tensor}; use zerolm_tensor::sha256::Sha256; @@ -32,7 +32,8 @@ pub const CODEBOOK: &str = "memory.relations.codebook"; pub const STATE: &str = "memory.relations.state"; pub const ROLES_T: &str = "core.reasoner.roles"; pub const RELATIONS_T: &str = "core.reasoner.relations"; -pub const NUMBER_T: &str = "core.reasoner.number_one"; +pub const NUMBER_DIGITS_T: &str = "core.reasoner.number_digits"; +pub const NUMBER_CODEBOOK_T: &str = "core.reasoner.number_codebook"; pub const ATOM_TABLE_T: &str = "core.reasoner.atom_table"; pub const ROLES: [&str; 5] = ["subj", "rel", "obj", "quant", "scope"]; @@ -271,7 +272,7 @@ pub struct WriteEvent { pub struct Reasoner { pub cfg: ReasonerConfig, pub ops: Ops, - space: Space, + numbers: circuits::Numbers, /// Compiled atom encoder (`atom-encode`) with its table. atoms: circuits::Atoms, roles: Vec, @@ -292,7 +293,7 @@ pub struct Reasoner { /// C5 output for the current memory state (session state, recomputed after writes). graph_cache: std::cell::RefCell>, /// Evaluation only: recorded circuit runs for reference replay. - recorder: std::cell::RefCell>>, + recorder: circuits::Recorder, } pub(crate) const SUBJ: usize = 0; @@ -310,7 +311,6 @@ const P24: u64 = 1 << 24; impl Reasoner { pub fn new(cfg: ReasonerConfig) -> Result { cfg.validate().map_err(ReasonerError::Config)?; - let space = Space::new(cfg.seed, cfg.dim); let atoms = circuits::Atoms::new(&cfg); let roles = ROLES.iter().map(|r| atoms.encode("role", r)).collect(); let rels = RELATIONS.iter().map(|r| atoms.encode("rel", r)).collect(); @@ -331,9 +331,9 @@ impl Reasoner { unsaved_from: 0, circuits: circuits::Circuits::new(&cfg), graph_cache: std::cell::RefCell::new(None), - recorder: std::cell::RefCell::new(None), + recorder: Default::default(), atoms, - space, + numbers: circuits::Numbers::new(&cfg), cfg, }) } @@ -354,8 +354,9 @@ impl Reasoner { self.atoms.encode("sym", name) } - pub fn space(&self) -> &Space { - &self.space + /// The compiled number encoder and decoder. + pub fn numbers(&self) -> &circuits::Numbers { + &self.numbers } pub fn events(&self) -> &[WriteEvent] { @@ -387,42 +388,16 @@ impl Reasoner { } } - pub(crate) fn encode(&self, parts: &[(usize, Hv)]) -> Hv { - let items: Vec = parts.iter().map(|(r, f)| bind(&self.role(*r), f)).collect(); - bundle(&items) - } - // ---- Codebook ---- pub(crate) fn code(&self, i: usize) -> Hv { Hv::from_data(self.cfg.dim, self.codebook.row(i)) } - /// Codebook index of a symbol vector (cosine >= symbol_same). + /// Codebook index of a symbol vector (cosine >= symbol_same), by the + /// `symbol-cleanup` circuit. pub(crate) fn find_symbol(&self, v: &Hv) -> Option { - self.cleanup_with(v, self.cfg.symbol_same) - } - - /// Nearest codebook entry with cosine >= min. - pub(crate) fn cleanup_with(&self, v: &Hv, min: f32) -> Option { - let d = self.cfg.dim; - let vn = sim_raw(&v.data, &v.data, d).sqrt(); - if vn == 0.0 { - return None; - } - let mut best: Option<(usize, f32)> = None; - for i in 0..self.n_sym { - let row = self.codebook.row(i); - let c = sim_raw(row, &v.data, d) / vn; // rows are unit phasors: |row| = 1 - if c >= min && best.map(|b| c > b.1).unwrap_or(true) { - best = Some((i, c)); - } - } - best.map(|b| b.0) - } - - pub(crate) fn cleanup(&self, v: &Hv) -> Option { - self.cleanup_with(v, self.cfg.cleanup_min) + self.symbol_cleanup(v, circuits::CleanupMode::Same) } /// Index of a name's symbol if it was ever stored. @@ -430,38 +405,19 @@ impl Reasoner { self.find_symbol(&self.symbol(name)) } - fn intern(&mut self, name: &str) -> Result { - let v = self.symbol(name); - if let Some(i) = self.find_symbol(&v) { - return Ok(i); - } - if self.n_sym == self.cfg.symbols { - return Err(ReasonerError::Full("codebook")); - } - let i = self.n_sym; - let c = self.codebook.cols; - self.codebook.data[i * c..(i + 1) * c].copy_from_slice(&v.data); - self.n_sym += 1; - Ok(i) - } - - // ---- Probes ---- - - /// Slots matching every given role-filler pair (sim >= k - margin). - pub(crate) fn matches(&self, parts: &[(usize, Hv)]) -> Vec { - if self.retrieval_ablated { - return Vec::new(); + /// Intern a name: `symbol-cleanup` decides whether its vector is new (and + /// `gate` is 1); the host appends the circuit's row at the codebook counter. + fn intern(&mut self, name: &str, gate: f32) -> Result<(), ReasonerError> { + let c = self.symbol_cleanup_full(&self.symbol(name), circuits::CleanupMode::Same, gate); + if c.added { + if self.n_sym == self.cfg.symbols { + return Err(ReasonerError::Full("codebook")); + } + let (i, w) = (self.n_sym, self.codebook.cols); + self.codebook.data[i * w..(i + 1) * w].copy_from_slice(&c.row); + self.n_sym += 1; } - let q = self.encode(parts); - let need = parts.len() as f32 - self.cfg.match_margin; - (0..self.n_slots) - .filter(|&s| self.slot_meta(s, field::ACTIVE) == 1.0) - .filter(|&s| sim_raw(self.slots.row(s), &q.data, self.cfg.dim) >= need) - .collect() - } - - pub(crate) fn unbind_slot(&self, slot: usize, role: usize) -> Hv { - unbind(&Hv::from_data(self.cfg.dim, self.slots.row(slot)), &self.role(role)) + Ok(()) } // ---- Language interface ---- @@ -477,9 +433,9 @@ impl Reasoner { Pred::Prop(x) => p.extend([(REL, self.rel("prop").clone()), (OBJ, sym(x))]), Pred::Greater { dim, other } => p.extend([(REL, bind(self.rel("gt"), &sym(dim))), (OBJ, sym(other))]), Pred::Before(y) => p.extend([(REL, self.rel("before").clone()), (OBJ, sym(y))]), - Pred::Count { thing, n } => p.extend([(REL, bind(self.rel("has"), &sym(thing))), (QUANT, self.space.number(*n))]), + Pred::Count { thing, n } => p.extend([(REL, bind(self.rel("has"), &sym(thing))), (QUANT, self.numbers.encode(*n))]), Pred::More { thing, delta, other } => { - p.extend([(REL, bind(self.rel("more"), &sym(thing))), (OBJ, sym(other)), (QUANT, self.space.number(*delta))]) + p.extend([(REL, bind(self.rel("more"), &sym(thing))), (OBJ, sym(other)), (QUANT, self.numbers.encode(*delta))]) } } p @@ -494,49 +450,62 @@ impl Reasoner { } } - /// Resolve a subject term to a codebook vector; `None` if "it" is unresolved. - pub(crate) fn resolve(&self, t: &Term) -> Option<(Hv, bool)> { + /// Resolve a subject term to (vector, via pronoun, resolved). An + /// unresolved "it" is the zero vector with `resolved` 0. + pub(crate) fn resolve_gated(&self, t: &Term) -> (Hv, bool, f32) { match t { - Term::Name(n) => Some((self.symbol(n), false)), - Term::Pronoun => { - let cands: Vec = (0..self.n_sym).map(|i| self.code(i)).collect(); - self.state.resolve(cands.iter().enumerate(), self.cfg.pronoun_min).map(|(i, _)| (cands[i].clone(), true)) - } + Term::Name(n) => (self.symbol(n), false, 1.0), + // "it": the codebook symbol nearest the discourse state (`symbol-cleanup`, pronoun threshold). + Term::Pronoun => match self.symbol_cleanup(&self.state.context, circuits::CleanupMode::Pronoun) { + Some(i) => (self.code(i), true, 1.0), + None => (Hv::zeros(self.cfg.dim), true, 0.0), + }, } } - fn write(&mut self, parts: &[(usize, Hv)], polarity: f32, kind: f32, confidence: f32, stream: &str) -> Result { - let v = self.encode(parts); - let full = parts.len() as f32 - self.cfg.match_margin; - for s in 0..self.n_slots { - if self.slot_meta(s, field::POLARITY) == polarity - && self.slot_meta(s, field::N_ROLES) == parts.len() as f32 - && sim_raw(self.slots.row(s), &v.data, self.cfg.dim) >= full - { - return Ok(Observed::Duplicate { slot: s }); - } + /// Resolve a subject term to a codebook vector; `None` if "it" is unresolved. + pub(crate) fn resolve(&self, t: &Term) -> Option<(Hv, bool)> { + let (v, via, ok) = self.resolve_gated(t); + (ok == 1.0).then_some((v, via)) + } + + /// Store a statement: `relation-probe` decides (resolved and not a + /// duplicate) and computes the slot and metadata rows; the host appends + /// them at the slot counter and logs the write. + fn write(&mut self, parts: &[(usize, Hv)], polarity: f32, kind: f32, confidence: f32, gate: f32, stream: &str) -> Result { + let seq = self.clock + 1; + let h = fnv1a(stream.as_bytes()); + let m = (1u64 << 22) - 1; + let mut record = vec![0.0f32; field::COUNT]; + record[field::ACTIVE] = 1.0; + record[field::POLARITY] = polarity; + record[field::CONFIDENCE] = confidence; + record[field::KIND] = kind; + record[field::SEQ_LO] = (seq % P24) as f32; + record[field::SEQ_HI] = (seq / P24 % P24) as f32; + record[field::PROV] = (h & m) as f32; + record[field::PROV + 1] = ((h >> 22) & m) as f32; + record[field::PROV + 2] = (h >> 44) as f32; + record[field::N_ROLES] = parts.len() as f32; + let st = self.store(parts, &record, gate); + if self.state.enabled && kind != RULE_KIND { + self.state.context = st.context.clone(); + } + if !st.stored { + return Ok(match st.dup_slot { + Some(s) if gate == 1.0 => Observed::Duplicate { slot: s }, + _ => Observed::Unresolved, + }); } if self.n_slots == self.cfg.slots { return Err(ReasonerError::Full("relation slots")); } let s = self.n_slots; self.n_slots += 1; - self.clock += 1; + self.clock = seq; let c = self.slots.cols; - self.slots.data[s * c..(s + 1) * c].copy_from_slice(&v.data); - let h = fnv1a(stream.as_bytes()); - let m = (1u64 << 22) - 1; - let row = &mut self.meta.data[s * field::COUNT..(s + 1) * field::COUNT]; - row[field::ACTIVE] = 1.0; - row[field::POLARITY] = polarity; - row[field::CONFIDENCE] = confidence; - row[field::KIND] = kind; - row[field::SEQ_LO] = (self.clock % P24) as f32; - row[field::SEQ_HI] = (self.clock / P24 % P24) as f32; - row[field::PROV] = (h & m) as f32; - row[field::PROV + 1] = ((h >> 22) & m) as f32; - row[field::PROV + 2] = (h >> 44) as f32; - row[field::N_ROLES] = parts.len() as f32; + self.slots.data[s * c..(s + 1) * c].copy_from_slice(&st.row); + self.meta.data[s * field::COUNT..(s + 1) * field::COUNT].copy_from_slice(&st.meta_row); let mut sh = Sha256::new(); sh.update_f32(self.slots.row(s)); sh.update_f32(self.meta.row(s)); @@ -554,23 +523,21 @@ impl Reasoner { } pub fn teach_fact(&mut self, f: &Fact, stream: &str) -> Result { - let Some((s, via_pronoun)) = self.resolve(&f.subj) else { return Ok(Observed::Unresolved) }; + let (s, via_pronoun, gate) = self.resolve_gated(&f.subj); if let Term::Name(n) = &f.subj { - self.intern(n)?; + self.intern(n, gate)?; } for n in Self::pred_symbols(&f.pred) { - self.intern(n)?; + self.intern(n, gate)?; } let parts = self.pred_parts(&s, &f.pred); let conf = if via_pronoun { self.cfg.pronoun_confidence } else { 1.0 }; - let out = self.write(&parts, if f.negated { -1.0 } else { 1.0 }, pred_code(&f.pred), conf, stream)?; - self.state.observe_subject(&s); - Ok(out) + self.write(&parts, if f.negated { -1.0 } else { 1.0 }, pred_code(&f.pred), conf, gate, stream) } pub fn teach_rule(&mut self, r: &Rule, stream: &str) -> Result { for n in [&r.premise, &r.conclusion].into_iter().chain(r.scope.as_ref()) { - self.intern(n)?; + self.intern(n, 1.0)?; } let scope = match &r.scope { Some(x) => self.symbol(x), @@ -582,7 +549,7 @@ impl Reasoner { (OBJ, self.symbol(&r.conclusion)), (SCOPE, scope), ]; - self.write(&parts, 1.0, RULE_KIND, 1.0, stream) + self.write(&parts, 1.0, RULE_KIND, 1.0, 1.0, stream) } /// Process one line: statements are stored, questions are answered. @@ -612,7 +579,8 @@ impl Reasoner { let mut t = vec![ Tensor::new(ROLES_T, "binding_roles", ROLES.len(), d2, rows(&fresh.roles)), Tensor::new(RELATIONS_T, "binding_roles", RELATIONS.len(), d2, rows(&fresh.rels)), - Tensor::new(NUMBER_T, "binding_roles", 1, d2, self.space.number(1).data), + Tensor::new(NUMBER_DIGITS_T, "binding_roles", fresh.numbers.digits.rows, d2, fresh.numbers.digits.data.clone()), + Tensor::new(NUMBER_CODEBOOK_T, "binding_roles", fresh.numbers.codebook.rows, d2, fresh.numbers.codebook.data.clone()), Tensor::new(ATOM_TABLE_T, "binding_roles", self.atoms.table.rows, d2, self.atoms.table.data.clone()), Tensor::new(SLOTS, "relation_memory", self.slots.rows, d2, self.slots.data.clone()), Tensor::new(META, "relation_memory", self.meta.rows, field::COUNT, self.meta.data.clone()), @@ -626,7 +594,7 @@ impl Reasoner { ), ]; // Compiled circuit constants (W_PROG), frozen like the role phasors. - for c in [&fresh.atoms.circuit, &fresh.circuits.graph, &fresh.circuits.reach] { + for c in [&fresh.atoms.circuit, &fresh.numbers.encoder, &fresh.numbers.decoder].into_iter().chain(fresh.circuits.all()) { for p in &c.tensors { t.push(Tensor::new(&c.qualified(&p.name), zerolm_wsp::compiler::GROUP, p.rows, p.cols, p.data.clone())); } @@ -658,7 +626,7 @@ impl Reasoner { return Err(ReasonerError::Format(format!("unexpected tensor {}", got.name))); } match got.name.as_str() { - name if name == ROLES_T || name == RELATIONS_T || name == NUMBER_T || name == ATOM_TABLE_T || name.starts_with("prog.") => { + name if name == ROLES_T || name == RELATIONS_T || name == NUMBER_DIGITS_T || name == NUMBER_CODEBOOK_T || name == ATOM_TABLE_T || name.starts_with("prog.") => { if got.data.iter().zip(&w.data).any(|(a, b)| a.to_bits() != b.to_bits()) { return Err(ReasonerError::CoreMismatch(got.name)); } diff --git a/crates/zerolm-reasoner/src/operators.rs b/crates/zerolm-reasoner/src/operators.rs index 5cbe309..a7158d6 100644 --- a/crates/zerolm-reasoner/src/operators.rs +++ b/crates/zerolm-reasoner/src/operators.rs @@ -1,58 +1,56 @@ -//! Controlled inference operators over the relational tensor memory. -//! -//! Every step is a vector operation: a probe (thresholded attention over -//! slots), unbinding a role, cleanup against the codebook, or binding number -//! vectors (addition). The control flow of each operator (which probe to issue -//! next, the depth limit, how polarities combine) is programmed and documented -//! here; nothing is learned. Derived answers are never written back to memory. - -use crate::{field, Answer, Reasoner, Verdict, OBJ, QUANT, REL, SCOPE, SUBJ}; -use std::collections::{BTreeMap, BTreeSet}; -use zerolm_engine::binding::{bind, cosine, Hv}; +//! Inference operators as a fixed sequence of compiled circuits per question +//! form (`docs/neural-isa.md`): C6 `relation-reach` (transitive chains), +//! `relation-join` (negation), `relation-levels` (attribute inheritance), +//! `relation-rules` (conditionals), `relation-counts` (arithmetic), and +//! `relation-verdict` (how positive and negative findings combine). The host +//! picks the circuits from the parsed question's form, passes outputs on +//! unchanged, and labels the result (which operators a proof used, from the +//! circuits' proof lengths and levels). Nothing is learned, and derived +//! answers are never written back to memory. + +use crate::{field, Answer, Reasoner, Verdict}; +use zerolm_engine::binding::{bind, Hv}; +use zerolm_engine::isa::{Buf, Outputs}; use zerolm_engine::semantic_state::{Pred, Query}; -/// A derivation: premise slots and the operators that produced it. -#[derive(Debug, Clone, Default)] -struct Proof { - premises: Vec, - ops: Vec<&'static str>, -} - -impl Proof { - fn of(premises: Vec, op: &'static str) -> Proof { - Proof { premises, ops: vec![op] } - } +/// Columns of `relation-verdict`'s one-hot output. +const VERDICTS: [Kind; 4] = [Kind::Yes, Kind::No, Kind::Contradiction, Kind::Unknown]; - fn with(mut self, op: &'static str) -> Proof { - self.ops.push(op); - self - } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Kind { + Yes, + No, + Contradiction, + Unknown, +} - fn join(mut self, other: Proof) -> Proof { - self.premises.extend(other.premises); - self.ops.extend(other.ops); - self - } +/// One side of a yes/no question: whether a proof was found, the proof as a +/// slot mask, and the operators it used. +struct Finding { + found: f32, + proof: Buf, + ops: Vec<&'static str>, } -use crate::circuits::Reach; +fn total(b: &Buf) -> f32 { + b.data.iter().sum() +} impl Reasoner { - /// (target symbol, slot) for every stored edge `from --rel--> target` with `polarity`. - fn edges(&self, from: usize, rel: &Hv, polarity: f32) -> Vec<(usize, usize)> { - self.matches(&[(SUBJ, self.code(from)), (REL, rel.clone())]) - .into_iter() - .filter(|&s| self.slot_meta(s, field::POLARITY) == polarity) - .filter_map(|s| self.cleanup(&self.unbind_slot(s, OBJ)).map(|t| (t, s))) - .collect() + fn none(&self) -> Finding { + Finding { found: 0.0, proof: Buf::zeros(1, self.n_slots), ops: Vec::new() } + } + + /// A C6 result as a finding: `op` when the chain is longer than one edge + /// (or `always`), else a direct retrieval. + fn chain(o: &Outputs, op: &'static str, always: bool) -> Finding { + let proof = o.get("proof").clone(); + let tag = if always || total(&proof) > 1.0 { op } else { "direct" }; + Finding { found: o.scalar("hit"), proof, ops: vec![tag] } } - fn finish(&self, verdict: Verdict, proof: Option) -> Answer { - let proof = proof.unwrap_or_default(); - let mut premises = proof.premises; - premises.sort_unstable(); - premises.dedup(); - let mut ops = proof.ops; + fn finish(&self, verdict: Verdict, mask: &[f32], mut ops: Vec<&'static str>) -> Answer { + let premises: Vec = mask.iter().enumerate().filter(|(_, &v)| v > 0.0).map(|(s, _)| s).collect(); ops.sort_unstable(); ops.dedup(); let confidence = premises.iter().map(|&s| self.slot_meta(s, field::CONFIDENCE)).product(); @@ -61,32 +59,42 @@ impl Reasoner { Answer { verdict, derived, premises, confidence, operators: ops } } - fn decide(&self, pos: Option, neg: Option) -> Answer { - match (pos, neg) { - (Some(p), Some(n)) => self.finish(Verdict::Contradiction, Some(p.join(n))), - (Some(p), None) => self.finish(Verdict::Yes, Some(p)), - (None, Some(n)) => self.finish(Verdict::No, Some(n)), - (None, None) => self.finish(Verdict::Unknown, None), - } + fn unknown(&self) -> Answer { + self.finish(Verdict::Unknown, &[], Vec::new()) } - fn unknown(&self) -> Answer { - self.finish(Verdict::Unknown, None) + /// Combine findings with `relation-verdict`. + fn decide(&self, pos: Finding, neg: Finding) -> Answer { + let o = self.run_verdict(pos.found, &pos.proof, neg.found, &neg.proof); + let col = o.get("verdict").data.iter().position(|&v| v == 1.0).expect("one-hot verdict"); + let kind = VERDICTS[col]; + let mut ops = Vec::new(); + if matches!(kind, Kind::Yes | Kind::Contradiction) { + ops.extend(pos.ops); + } + if matches!(kind, Kind::No | Kind::Contradiction) { + ops.extend(neg.ops); + } + let verdict = match kind { + Kind::Yes => Verdict::Yes, + Kind::No => Verdict::No, + Kind::Contradiction => Verdict::Contradiction, + Kind::Unknown => Verdict::Unknown, + }; + self.finish(verdict, &o.get("premises").data, ops) } pub fn answer(&self, q: &Query) -> Answer { + if self.n_slots == 0 || self.n_sym == 0 { + return self.unknown(); + } + // An unresolved "it" is the zero vector, which no circuit matches. + let subject = |t| self.resolve(t).map(|(v, _)| v).unwrap_or_else(|| Hv::zeros(self.cfg.dim)); match q { - Query::Holds { subj, pred } => { - let Some(x) = self.resolve(subj).and_then(|(v, _)| self.find_symbol(&v)) else { return self.unknown() }; - self.holds(x, pred) - } - Query::Count { subj, thing } => { - let Some(x) = self.resolve(subj).and_then(|(v, _)| self.find_symbol(&v)) else { return self.unknown() }; - self.count_answer(x, thing) - } + Query::Holds { subj, pred } => self.holds(&subject(subj), pred), + Query::Count { subj, thing } => self.count_answer(&subject(subj), thing), Query::Which { dim, higher, a, b } => { - let (Some(ia), Some(_)) = (self.known(a), self.known(b)) else { return self.unknown() }; - let ans = self.holds(ia, &Pred::Greater { dim: dim.clone(), other: b.clone() }); + let ans = self.holds(&self.symbol(a), &Pred::Greater { dim: dim.clone(), other: b.clone() }); let pick = |first: bool| Verdict::Choice(if first { a.clone() } else { b.clone() }); let verdict = match ans.verdict { Verdict::Yes => pick(*higher), @@ -98,199 +106,74 @@ impl Reasoner { } } - fn holds(&self, x: usize, pred: &Pred) -> Answer { - let ops = self.ops; + fn holds(&self, x: &Hv, pred: &Pred) -> Answer { match pred { Pred::Isa(c) => { - let Some(y) = self.known(c) else { return self.unknown() }; - let isa = self.rel("isa").clone(); - let anc = self.reach(x, &isa, "class_inclusion"); - let tag = |p: &Vec| if p.len() > 1 { "class_inclusion" } else { "direct" }; - let pos = anc.get(&y).map(|p| Proof::of(p.clone(), tag(p))); - let neg = if ops.negation { - let mut sources: Vec<(usize, Vec)> = vec![(x, Vec::new())]; - let mut targets: Reach = BTreeMap::from([(y, Vec::new())]); - if ops.class_inclusion { - sources.extend(anc.iter().map(|(k, v)| (*k, v.clone()))); - targets.extend(self.reach(y, &isa, "class_inclusion")); - } - let mut best: Option = None; - for (a, pa) in &sources { - for (z, s) in self.edges(*a, &isa, -1.0) { - if let Some(pz) = targets.get(&z) { - let mut prem = pa.clone(); - prem.push(s); - prem.extend(pz); - let mut p = Proof::of(prem, "negation"); - if !pa.is_empty() || !pz.is_empty() { - p = p.with("class_inclusion"); - } - if best.as_ref().map(|b| p.premises.len() < b.premises.len()).unwrap_or(true) { - best = Some(p); - } - } - } - } - best - } else { - None - }; - self.decide(pos, neg) + let (y, isa) = (self.symbol(c), self.rel("isa").clone()); + let from_x = self.run_reach(x, &y, &isa, "class_inclusion"); + let from_y = self.run_reach(&y, &y, &isa, "class_inclusion"); + let pos = Self::chain(&from_x, "class_inclusion", false); + let j = self.run_join(x, &y, &isa, Some(&from_x), Some(&from_y)); + let mut ops = vec!["negation"]; + if j.scalar("len") > 1.0 { + ops.push("class_inclusion"); + } + self.decide(pos, Finding { found: j.scalar("found"), proof: j.get("proof").clone(), ops }) } Pred::Can(v) => { - let Some(v) = self.known(v) else { return self.unknown() }; - let (can, isa) = (self.rel("can").clone(), self.rel("isa").clone()); - let mut frontier: Vec<(usize, Vec)> = vec![(x, Vec::new())]; - let mut visited = BTreeSet::from([x]); - for level in 0..=self.cfg.max_depth { - let (mut pos, mut neg): (Option, Option) = (None, None); - for (a, pa) in &frontier { - let found = |pol: f32| { - self.edges(*a, &can, pol).into_iter().find(|(t, _)| *t == v).map(|(_, s)| { - let mut prem = pa.clone(); - prem.push(s); - Proof::of(prem, if level > 0 { "attribute_inheritance" } else { "direct" }) - }) - }; - pos = pos.or_else(|| found(1.0)); - if ops.negation { - neg = neg.or_else(|| found(-1.0).map(|p| p.with("negation"))); - } - } - if pos.is_some() || neg.is_some() { - return self.decide(pos, neg); - } - if !ops.attribute_inheritance { - break; - } - let mut next = Vec::new(); - for (a, pa) in &frontier { - for (t, s) in self.edges(*a, &isa, 1.0) { - if visited.insert(t) { - let mut p = pa.clone(); - p.push(s); - next.push((t, p)); - } - } - } - if next.is_empty() { - break; - } - frontier = next; - } - self.unknown() + let o = self.run_levels(x, &self.symbol(v), self.rel("can"), self.rel("isa")); + let tag = if o.scalar("level") > 0.0 { "attribute_inheritance" } else { "direct" }; + let pos = Finding { found: o.scalar("pos_found"), proof: o.get("pos_proof").clone(), ops: vec![tag] }; + let neg = Finding { found: o.scalar("neg_found"), proof: o.get("neg_proof").clone(), ops: vec![tag, "negation"] }; + self.decide(pos, neg) } Pred::In(y) => { - let Some(y) = self.known(y) else { return self.unknown() }; - let rel = self.rel("in").clone(); - let tag = |p: &Vec| if p.len() > 1 { "transitivity" } else { "direct" }; - let pos = self.reach(x, &rel, "transitivity").get(&y).map(|p| Proof::of(p.clone(), tag(p))); - let neg = if ops.negation { - self.edges(x, &rel, -1.0).into_iter().find(|(t, _)| *t == y).map(|(_, s)| Proof::of(vec![s], "negation")) - } else { - None - }; - self.decide(pos, neg) + let (y, rel) = (self.symbol(y), self.rel("in").clone()); + let pos = Self::chain(&self.run_reach(x, &y, &rel, "transitivity"), "transitivity", false); + self.decide(pos, self.direct_negation(x, &y, &rel)) } Pred::Prop(p) => { - let Some(p) = self.known(p) else { return self.unknown() }; - let pos = self.derive_prop(x, p, 0, &mut BTreeSet::new()); - let neg = if ops.negation { - let rel = self.rel("prop").clone(); - self.edges(x, &rel, -1.0).into_iter().find(|(t, _)| *t == p).map(|(_, s)| Proof::of(vec![s], "negation")) - } else { - None - }; - self.decide(pos, neg) + let (p, rel) = (self.symbol(p), self.rel("prop").clone()); + let o = self.run_rules(x, &p, &rel, self.rel("implies"), self.rel("something")); + let tag = if o.scalar("level") > 0.0 { "conditionals" } else { "direct" }; + let pos = Finding { found: o.scalar("found"), proof: o.get("proof").clone(), ops: vec![tag] }; + self.decide(pos, self.direct_negation(x, &p, &rel)) } Pred::Greater { dim, other } => { let rel = bind(self.rel("gt"), &self.symbol(dim)); - self.order(x, other, &rel, ops.comparison, "comparison") - } - Pred::Before(other) => { - let rel = self.rel("before").clone(); - self.order(x, other, &rel, ops.temporal_ordering, "temporal_ordering") + self.order(x, &self.symbol(other), &rel, self.ops.comparison, "comparison") } + Pred::Before(other) => self.order(x, &self.symbol(other), &self.rel("before").clone(), self.ops.temporal_ordering, "temporal_ordering"), Pred::Count { .. } | Pred::More { .. } => self.unknown(), } } - /// Strict order: yes if a reaches b, no if b reaches a (antisymmetry). - /// Disabled: only a direct a -> b edge counts. - fn order(&self, a: usize, other: &str, rel: &Hv, enabled: bool, op: &'static str) -> Answer { - let Some(b) = self.known(other) else { return self.unknown() }; - let tag = |p: &Vec| if p.len() > 1 { op } else { "direct" }; - let pos = self.reach(a, rel, op).get(&b).map(|p| Proof::of(p.clone(), tag(p))); - let neg = if enabled { self.reach(b, rel, op).get(&a).map(|p| Proof::of(p.clone(), op)) } else { None }; - self.decide(pos, neg) + /// A stored negative edge x -rel-> y (`relation-join` with no ancestors). + fn direct_negation(&self, x: &Hv, y: &Hv, rel: &Hv) -> Finding { + let j = self.run_join(x, y, rel, None, None); + Finding { found: j.scalar("found"), proof: j.get("proof").clone(), ops: vec!["negation"] } } - /// Positive derivation of property `p` for `x`: a direct fact, or (with - /// conditionals) a rule "if is q then it is p" whose premise holds. - fn derive_prop(&self, x: usize, p: usize, depth: usize, visited: &mut BTreeSet) -> Option { - let prop = self.rel("prop").clone(); - if let Some((_, s)) = self.edges(x, &prop, 1.0).into_iter().find(|(t, _)| *t == p) { - return Some(Proof::of(vec![s], if depth > 0 { "conditionals" } else { "direct" })); - } - if !self.ops.conditionals || depth >= self.cfg.max_depth || !visited.insert(p) { - return None; - } - let something = self.rel("something").clone(); - let rules = self.matches(&[(REL, self.rel("implies").clone()), (OBJ, self.code(p))]); - for r in rules { - let scope = self.unbind_slot(r, SCOPE); - let in_scope = cosine(&scope, &something) >= self.cfg.cleanup_min || cosine(&scope, &self.code(x)) >= self.cfg.cleanup_min; - if !in_scope { - continue; - } - let Some(q) = self.cleanup(&self.unbind_slot(r, SUBJ)) else { continue }; - if let Some(sub) = self.derive_prop(x, q, depth + 1, visited) { - return Some(Proof::of(vec![r], "conditionals").join(sub)); - } - } - None + /// Strict order: yes if a reaches b, no if b reaches a (antisymmetry). + /// With the operator disabled (an evaluation ablation), only a direct + /// a -> b edge counts and the reverse direction is not tried. + fn order(&self, a: &Hv, b: &Hv, rel: &Hv, enabled: bool, op: &'static str) -> Answer { + let pos = Self::chain(&self.run_reach(a, b, rel, op), op, false); + let neg = if enabled { Self::chain(&self.run_reach(b, a, rel, op), op, true) } else { self.none() }; + self.decide(pos, neg) } - /// Every count of `thing` derivable for `x`, with its proof. - fn counts(&self, x: usize, thing: &str, depth: usize, visited: &mut BTreeSet) -> Vec<(i64, Proof)> { + fn count_answer(&self, x: &Hv, thing: &str) -> Answer { let sym = self.symbol(thing); - let r = self.cfg.number_range; - let mut out: Vec<(i64, Proof)> = self - .matches(&[(SUBJ, self.code(x)), (REL, bind(self.rel("has"), &sym))]) - .into_iter() - .filter(|&s| self.slot_meta(s, field::POLARITY) == 1.0) - .map(|s| (self.space().decode_number(&self.unbind_slot(s, QUANT), -r..=r).0, Proof::of(vec![s], "direct"))) - .collect(); - if !self.ops.arithmetic || depth >= self.cfg.max_depth || !visited.insert(x) { - return out; - } - for s in self.matches(&[(SUBJ, self.code(x)), (REL, bind(self.rel("more"), &sym))]) { - let Some(other) = self.cleanup(&self.unbind_slot(s, OBJ)) else { continue }; - let delta = self.space().decode_number(&self.unbind_slot(s, QUANT), -r..=r).0; - for (c, p) in self.counts(other, thing, depth + 1, visited) { - // Addition is binding of fractional-power number vectors. - let sum = bind(&self.space().number(c), &self.space().number(delta)); - let (n, _) = self.space().decode_number(&sum, -r..=r); - out.push((n, Proof::of(vec![s], "arithmetic").join(p))); - } - } - visited.remove(&x); - out - } - - fn count_answer(&self, x: usize, thing: &str) -> Answer { - let all = self.counts(x, thing, 0, &mut BTreeSet::new()); - let values: BTreeSet = all.iter().map(|(n, _)| *n).collect(); - match values.len() { - 0 => self.unknown(), - 1 => { - let best = all.into_iter().min_by_key(|(_, p)| p.premises.len()).map(|(_, p)| p); - self.finish(Verdict::Number(*values.iter().next().expect("one")), best) - } - _ => { - let joined = all.into_iter().fold(Proof::default(), |acc, (_, p)| acc.join(p)); - self.finish(Verdict::Contradiction, Some(joined)) - } + let o = self.run_counts(x, &bind(self.rel("has"), &sym), &bind(self.rel("more"), &sym)); + let ops = if o.scalar("arith") > 0.0 { vec!["arithmetic", "direct"] } else { vec!["direct"] }; + let premises = &o.get("premises").data; + if o.scalar("found") == 0.0 { + self.unknown() + } else if o.scalar("contradiction") == 1.0 { + self.finish(Verdict::Contradiction, premises, ops) + } else { + self.finish(Verdict::Number(o.scalar("value") as i64), premises, ops) } } } diff --git a/crates/zerolm-reasoner/tests/reasoner.rs b/crates/zerolm-reasoner/tests/reasoner.rs index 4b73ef9..99011f3 100644 --- a/crates/zerolm-reasoner/tests/reasoner.rs +++ b/crates/zerolm-reasoner/tests/reasoner.rs @@ -168,9 +168,22 @@ fn every_tensor_belongs_to_a_declared_weight_group() { #[test] fn default_config_compiles_to_the_shipped_pinned_circuits() { - use zerolm_reasoner::circuits::{graph_spec, reach_spec, GRAPH_ID, REACH_ID}; + use zerolm_reasoner::circuits::*; let cfg = ReasonerConfig::default(); - for (spec, id) in [(graph_spec(&cfg), GRAPH_ID), (reach_spec(&cfg), REACH_ID)] { + for (spec, id) in [ + (graph_spec(&cfg), GRAPH_ID), + (reach_spec(&cfg), REACH_ID), + (atom_spec(&cfg), ATOM_ID), + (number_encode_spec(&cfg), NUMBER_ENCODE_ID), + (number_decode_spec(&cfg), NUMBER_DECODE_ID), + (probe_spec(&cfg), PROBE_ID), + (cleanup_spec(&cfg), CLEANUP_ID), + (decide_spec(&cfg, JOIN_ID), JOIN_ID), + (decide_spec(&cfg, LEVELS_ID), LEVELS_ID), + (decide_spec(&cfg, RULES_ID), RULES_ID), + (decide_spec(&cfg, COUNTS_ID), COUNTS_ID), + (decide_spec(&cfg, VERDICT_ID), VERDICT_ID), + ] { let built = zerolm_wsp::compile(&spec).unwrap(); assert_eq!(built.sha256(), zerolm_wsp::shipped(id).unwrap().sha256(), "{id}: ReasonerConfig::default() must compile to model/prog/{id}.json"); } diff --git a/crates/zerolm-wsp/src/circuits/decide.rs b/crates/zerolm-wsp/src/circuits/decide.rs new file mode 100644 index 0000000..824ae18 --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/decide.rs @@ -0,0 +1,529 @@ +//! The reasoning operators' decisions as circuits, on top of C5 +//! (`relation_graph`: `subj_sim`, `obj_t`, `pos`, `neg`) and C6 +//! (`relation_reach`). Each circuit is a fixed number of recurrent steps; the +//! host only chooses which circuits a question's parsed form runs, passes +//! one circuit's outputs to the next unchanged, and labels the outputs. +//! +//! Shared notation: S slots, N codebook symbols, `E_r[s, i] = [subj_sim[s, i] +//! + sim(slot_s, rel (*) r) >= 2 - margin] * pol[s]` (slot s is an r-edge +//! leaving i with polarity pol), proofs as slot masks (1 x S) or premise +//! matrices (N x S, row j = slots on the proof of j). Among candidates, the +//! shortest proof wins (score `BIG - length`), ties to the lowest slot. +//! +//! - **`relation_join`** (negation): a negative r-edge from a source set to a +//! target set. Sources are x (cleaned up from its vector) plus, when class +//! inclusion is enabled, `src_more` with premises `src_proofs` (for "is x +//! a y": x and its ancestors); targets likewise from y. Outputs `found`, `proof`, `len`. +//! - **`relation_levels`** ("can"): breadth-first levels over positive `isa` +//! edges from x; at the first level where some node has a positive or (with +//! negation) negative `can` edge to v, both are latched. Outputs +//! `pos_found`, `pos_proof`, `neg_found`, `neg_proof`, `level`. +//! - **`relation_rules`** (property, conditionals): backward levels from +//! property p over rules "if is q then it is p" (scope "something" +//! or x itself); the first level at which x has a positive property edge to +//! a frontier property is latched. Outputs `found`, `proof`, `level`. +//! - **`relation_counts`** (arithmetic): breadth-first over `more` edges from +//! x, carrying the summed difference as a number phasor (binding is +//! addition); every reached node's `has` count is cleaned up against the +//! number codebook, bound to its carried difference, and decoded. One +//! distinct value answers it (shortest proof), several are a contradiction +//! (all proofs). Outputs `found`, `contradiction`, `value`, `premises`, +//! `arith`. +//! - **`relation_verdict`**: positive and negative findings to a one-hot +//! verdict (yes, no, contradiction, unknown) and the union of the proofs +//! that support it. + +use super::relational::{check, cleanup_query, onehot, role, support, Params, N_ROLES, ROLE_OBJ, ROLE_REL, ROLE_SUBJ}; +use crate::compiler::{Builder, Compiled}; +use serde::{Deserialize, Serialize}; +use zerolm_engine::isa::{Axis, GateFn::*, Instr, IsaError, NormKind, ReduceFn}; + +pub const ROLE_QUANT: usize = 3; +pub const ROLE_SCOPE: usize = 4; +/// Proof-length scores: `BIG - length` (exact in f32). +const BIG: f32 = 1.0e6; + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CountParams { + pub relational: Params, + /// The number codebook covers [-range, range]. + pub range: u32, +} + +pub const JOIN_OUTPUTS: [&str; 3] = ["found", "proof", "len"]; +pub const LEVELS_OUTPUTS: [&str; 5] = ["pos_found", "pos_proof", "neg_found", "neg_proof", "level"]; +pub const RULES_OUTPUTS: [&str; 3] = ["found", "proof", "level"]; +pub const COUNTS_OUTPUTS: [&str; 5] = ["found", "contradiction", "value", "premises", "arith"]; +pub const VERDICT_OUTPUTS: [&str; 2] = ["verdict", "premises"]; + +fn g(f: zerolm_engine::isa::GateFn, a: &str, b: &str, out: &str) -> Instr { + Instr::Gate { f, a: a.into(), b: Some(b.into()), out: out.into() } +} + +/// Graph tensors (C5 outputs) and memory, shared by the search circuits. +fn graph_io(b: &mut Builder, d2: usize) { + b.external_dyn("slots", None, Some(d2)); + b.external_dyn("codebook", None, Some(d2)); + b.external("roles", N_ROLES, d2); + b.external_dyn("subj_sim", None, None); + b.external_dyn("obj_t", None, None); + b.external_dyn("pos", Some(1), None); + b.external_dyn("neg", Some(1), None); +} + +fn probe_block(b: &mut Builder, p: &Params) { + b.block("probe"); + b.scalar("need", 2.0 - p.match_margin); + b.scalar("symbol_same", p.symbol_same); + b.scalar("cleanup_min", p.cleanup_min); + role(b, ROLE_REL, "role_rel"); + b.gate(Mul, "codebook", "inv_sqrt_d", "codes_unit"); +} + +/// `out` (S x N): r-edges with polarity row `pol` (1 x S). +fn edges(b: &mut Builder, rel: &str, pol: &str, out: &str) { + b.op(Instr::Bind { a: rel.into(), b: "role_rel".into(), out: format!("{out}_rb"), conj: false }); + b.matvec("slots", &format!("{out}_rb"), &format!("{out}_dot"), false); + b.gate(Mul, &format!("{out}_dot"), "inv_d", &format!("{out}_row")); + b.op(Instr::Transpose { x: format!("{out}_row"), out: format!("{out}_col") }); + b.gate(Add, "subj_sim", &format!("{out}_col"), &format!("{out}_sim")); + b.gate(Ge, &format!("{out}_sim"), "need", &format!("{out}_hit")); + b.op(Instr::Transpose { x: pol.into(), out: format!("{out}_pc") }); + b.gate(Mul, &format!("{out}_hit"), &format!("{out}_pc"), out); +} + +/// Zero premise matrix (N x S) and a zero slot row, from `obj_t`. +fn zeros(b: &mut Builder, px: &str) { + b.op(Instr::Transpose { x: "obj_t".into(), out: format!("{px}_zt") }); + b.gate(Mul, &format!("{px}_zt"), "zero", &format!("{px}_proofs")); +} + +/// One breadth-first expansion (the C6 step): from `frontier` over edge +/// matrix `e` (S x N, with `e_t` = N x S) to targets `to_t` (N x S), gated +/// by the 1 x 1 `gate`. Updates `{px}_frontier`, `{px}_reached`, +/// `{px}_proofs`; with `carry`, also the phasor rows `{px}_acc` (N x 2d), +/// bound with the picked slot's row of `carry` (S x 2d). +fn expand(px: &str, e: &str, e_t: &str, to_t: &str, gate: &str, carry: Option<&str>) -> Vec { + let n = |s: &str| format!("{px}_{s}"); + let mut v = vec![ + Instr::Matvec { w: e_t.into(), x: n("frontier"), out: n("leaving"), transpose: true }, + g(Sub, "one", &n("reached"), &n("unreached_row")), + Instr::Transpose { x: n("unreached_row"), out: n("unreached") }, + g(Mul, &n("unreached"), gate, &n("open")), + g(Mul, to_t, &n("leaving"), &n("cand_any")), + g(Mul, &n("cand_any"), &n("open"), &n("cand")), + Instr::Topk { x: n("cand"), k: 1, idx: None, val: Some(n("pick_val")), mask: Some(n("pick_mask")) }, + g(Mul, &n("pick_mask"), &n("pick_val"), &n("pick")), + Instr::Matmul { a: n("pick"), b: e.into(), out: n("parent"), ab: true }, + Instr::Matmul { a: n("parent"), b: n("proofs"), out: n("inherited"), ab: true }, + ]; + if let Some(c) = carry { + v.extend([ + Instr::Matmul { a: n("parent"), b: n("acc"), out: n("acc_parent"), ab: true }, + Instr::Matmul { a: n("pick"), b: c.into(), out: n("acc_step"), ab: true }, + Instr::Bind { a: n("acc_parent"), b: n("acc_step"), out: n("acc_new"), conj: false }, + g(Add, &n("acc"), &n("acc_new"), &n("acc")), + ]); + } + v.extend([ + g(Add, &n("proofs"), &n("inherited"), &n("proofs")), + g(Add, &n("proofs"), &n("pick"), &n("proofs")), + Instr::Transpose { x: n("pick_val"), out: n("new") }, + g(Add, &n("reached"), &n("new"), &n("reached")), + Instr::Gate { f: Copy, a: n("new"), b: None, out: n("frontier") }, + ]); + v +} + +/// Latch the best (shortest) candidate slot of `cand` (1 x S) whose subject +/// path is in `paths` (N x S) via `e` (S x N): writes `{px}_val` (1 or 0), +/// `{px}_proof` (1 x S), `{px}_len` (1 x 1). +fn best_proof(px: &str, cand: &str, e: &str, paths: &str) -> Vec { + let n = |s: &str| format!("{px}_{s}"); + vec![ + // Path length of each slot's subject. + Instr::Reduce { f: ReduceFn::Sum, x: paths.into(), axis: Axis::Rows, out: n("plen_col") }, + Instr::Matmul { a: e.into(), b: n("plen_col"), out: n("slot_plen_col"), ab: true }, + Instr::Transpose { x: n("slot_plen_col"), out: n("slot_plen") }, + g(Sub, "big_minus_one", &n("slot_plen"), &n("score_raw")), + g(Mul, &n("score_raw"), cand, &n("score")), + Instr::Topk { x: n("score"), k: 1, idx: None, val: Some(n("best")), mask: Some(n("mask")) }, + g(Ge, &n("best"), "one", &n("val")), + g(Mul, &n("mask"), &n("val"), &n("slot")), + Instr::Matmul { a: n("slot"), b: e.into(), out: n("node"), ab: true }, + Instr::Matmul { a: n("node"), b: paths.into(), out: n("path"), ab: true }, + g(Add, &n("slot"), &n("path"), &n("proof")), + Instr::Reduce { f: ReduceFn::Sum, x: n("proof"), axis: Axis::All, out: n("len") }, + ] +} + +fn length_constants(b: &mut Builder) { + b.block("support"); + b.scalar("big_minus_one", BIG - 1.0); +} + +/// Negative r-edges from a source set to a target set. +pub fn compile_join(id: &str, p: &Params) -> Result { + check(id, p)?; + let d2 = 2 * p.dim; + let mut b = Builder::new(id); + b.input("x", Some(1), d2); + b.input("y", Some(1), d2); + b.input("rel", Some(1), d2); + for side in ["src", "tgt"] { + b.input_dyn(&format!("{side}_more"), Some(1), None); + b.input_dyn(&format!("{side}_proofs"), None, None); + } + graph_io(&mut b, d2); + support(&mut b, p); + length_constants(&mut b); + probe_block(&mut b, p); + b.block("negation"); + b.scalar("enable_negation", 1.0); + b.block("class_inclusion"); + b.scalar("enable_class_inclusion", 1.0); + + b.block("probe"); + edges(&mut b, "rel", "neg", "e_neg"); + cleanup_query(&mut b, "x"); + cleanup_query(&mut b, "y"); + // Effective sets: base plus (when enabled) the reached nodes and their proofs. + for (side, base) in [("src", "x_onehot"), ("tgt", "y_onehot")] { + b.gate(Mul, &format!("{side}_more"), "enable_class_inclusion", &format!("{side}_extra")); + b.gate(Add, base, &format!("{side}_extra"), &format!("{side}_set")); + b.gate(Mul, &format!("{side}_proofs"), "enable_class_inclusion", &format!("{side}_paths")); + } + // Slots leaving a source (masked edge matrix) and arriving at a target. + b.gate(Mul, "e_neg", "src_set", "e_src"); + b.gate(Mul, "obj_t", "tgt_set", "e_tgt"); + b.reduce(ReduceFn::Sum, "e_src", Axis::Rows, "from_col"); + b.reduce(ReduceFn::Sum, "e_tgt", Axis::Rows, "to_col"); + b.gate(Mul, "from_col", "to_col", "cand_col"); + b.op(Instr::Transpose { x: "cand_col".into(), out: "cand_raw".into() }); + b.gate(Mul, "cand_raw", "enable_negation", "cand"); + // Length = source path + 1 + target path; shortest wins. + b.reduce(ReduceFn::Sum, "src_paths", Axis::Rows, "src_len_col"); + b.reduce(ReduceFn::Sum, "tgt_paths", Axis::Rows, "tgt_len_col"); + b.matmul_ab("e_src", "src_len_col", "slot_src_len"); + b.matmul_ab("e_tgt", "tgt_len_col", "slot_tgt_len"); + b.gate(Add, "slot_src_len", "slot_tgt_len", "slot_len_col"); + b.op(Instr::Transpose { x: "slot_len_col".into(), out: "slot_len".into() }); + b.gate(Sub, "big_minus_one", "slot_len", "score_raw"); + b.gate(Mul, "score_raw", "cand", "score"); + b.op(Instr::Topk { x: "score".into(), k: 1, idx: None, val: Some("best".into()), mask: Some("mask".into()) }); + b.gate(Ge, "best", "one", "found"); + b.gate(Mul, "mask", "found", "slot"); + b.matmul_ab("slot", "e_src", "src_node"); + b.matmul_ab("src_node", "src_paths", "src_path"); + b.matmul_ab("slot", "e_tgt", "tgt_node"); + b.matmul_ab("tgt_node", "tgt_paths", "tgt_path"); + b.gate(Add, "slot", "src_path", "proof_a"); + b.gate(Add, "proof_a", "tgt_path", "proof"); + b.reduce(ReduceFn::Sum, "proof", Axis::All, "len"); + Ok(b.finish(&JOIN_OUTPUTS)) +} + +/// "can": breadth-first levels over isa with a latched first decision. +pub fn compile_levels(id: &str, p: &Params) -> Result { + check(id, p)?; + let d2 = 2 * p.dim; + let mut b = Builder::new(id); + b.input("x", Some(1), d2); + b.input("v", Some(1), d2); + b.input("rel_can", Some(1), d2); + b.input("rel_isa", Some(1), d2); + graph_io(&mut b, d2); + support(&mut b, p); + length_constants(&mut b); + probe_block(&mut b, p); + b.block("negation"); + b.scalar("enable_negation", 1.0); + b.block("attribute_inheritance"); + b.scalar("enable_attribute_inheritance", 1.0); + + b.block("probe"); + edges(&mut b, "rel_can", "pos", "e_can_pos"); + edges(&mut b, "rel_can", "neg", "e_can_neg"); + edges(&mut b, "rel_isa", "pos", "e_isa"); + b.op(Instr::Transpose { x: "e_isa".into(), out: "e_isa_t".into() }); + b.op(Instr::Transpose { x: "obj_t".into(), out: "obj_tt".into() }); + cleanup_query(&mut b, "x"); + cleanup_query(&mut b, "v"); + // Slots whose object is v. + b.matvec("obj_t", "v_onehot", "to_v", false); + + b.block("depth"); + b.unary(Copy, "x_onehot", "s_frontier"); + b.unary(Copy, "x_onehot", "s_reached"); + zeros(&mut b, "s"); + for r in ["done", "pos_found", "neg_found", "level", "lvl"] { + b.gate(Mul, "zero", "one", r); + } + b.gate(Mul, "x_onehot", "zero", "zero_row_n"); + b.matvec("obj_t", "zero_row_n", "zero_row_s", false); + b.unary(Copy, "zero_row_s", "pos_proof"); + b.unary(Copy, "zero_row_s", "neg_proof"); + let mut body = Vec::new(); + body.push(g(Sub, "one", "done", "open")); + for (side, e) in [("p", "e_can_pos"), ("n", "e_can_neg")] { + body.push(Instr::Matmul { a: e.into(), b: "s_frontier_col".into(), out: format!("{side}_from_col"), ab: true }); + body.push(Instr::Transpose { x: format!("{side}_from_col"), out: format!("{side}_from") }); + body.push(g(Mul, &format!("{side}_from"), "to_v", &format!("{side}_cand"))); + // Restrict paths to the frontier so the subject's proof is used. + body.extend(best_proof(side, &format!("{side}_cand"), e, "s_proofs")); + } + body.extend([ + g(Mul, "n_val", "enable_negation", "n_val_on"), + g(Mul, "p_val", "open", "p_new"), + g(Mul, "n_val_on", "open", "n_new"), + g(Add, "pos_found", "p_new", "pos_found"), + g(Add, "neg_found", "n_new", "neg_found"), + g(Mul, "p_proof", "p_new", "p_add"), + g(Mul, "n_proof", "n_new", "n_add"), + g(Add, "pos_proof", "p_add", "pos_proof"), + g(Add, "neg_proof", "n_add", "neg_proof"), + g(Max, "p_new", "n_new", "found_now"), + g(Mul, "found_now", "lvl", "lvl_hit"), + g(Add, "level", "lvl_hit", "level"), + g(Max, "done", "found_now", "done"), + ]); + body.extend(expand("s", "e_isa", "e_isa_t", "obj_tt", "enable_attribute_inheritance", None)); + body.extend([Instr::Transpose { x: "s_frontier".into(), out: "s_frontier_col".into() }, g(Add, "lvl", "one", "lvl")]); + b.op(Instr::Transpose { x: "s_frontier".into(), out: "s_frontier_col".into() }); + b.op(Instr::Step { n: p.max_depth + 1, body }); + Ok(b.finish(&LEVELS_OUTPUTS)) +} + +/// Properties: backward levels over in-scope rules, latched at the first +/// level where x has the property directly. +pub fn compile_rules(id: &str, p: &Params) -> Result { + check(id, p)?; + let d2 = 2 * p.dim; + let mut b = Builder::new(id); + b.input("x", Some(1), d2); + b.input("p", Some(1), d2); + b.input("rel_prop", Some(1), d2); + b.input("rel_implies", Some(1), d2); + b.input("something", Some(1), d2); + graph_io(&mut b, d2); + support(&mut b, p); + length_constants(&mut b); + probe_block(&mut b, p); + b.block("conditionals"); + b.scalar("enable_conditionals", 1.0); + + b.block("probe"); + role(&mut b, ROLE_SUBJ, "role_subj"); + role(&mut b, ROLE_OBJ, "role_obj"); + role(&mut b, ROLE_SCOPE, "role_scope"); + cleanup_query(&mut b, "x"); + cleanup_query(&mut b, "p"); + // x's positive property slots (as an S x N edge matrix restricted to x). + edges(&mut b, "rel_prop", "pos", "e_prop"); + b.gate(Mul, "e_prop", "x_onehot", "e_prop_x"); + // Rules concluding j: rel = implies and object similarity to j. + b.op(Instr::Bind { a: "rel_implies".into(), b: "role_rel".into(), out: "imp_rb".into(), conj: false }); + b.matvec("slots", "imp_rb", "imp_dot", false); + b.gate(Mul, "imp_dot", "inv_d", "imp_row"); + b.op(Instr::Transpose { x: "imp_row".into(), out: "imp_col".into() }); + b.op(Instr::Bind { a: "codebook".into(), b: "role_obj".into(), out: "obj_codes".into(), conj: false }); + b.matmul("slots", "obj_codes", "obj_dot"); + b.gate(Mul, "obj_dot", "inv_d", "obj_sim"); + b.gate(Add, "obj_sim", "imp_col", "rule_sim"); + b.gate(Ge, "rule_sim", "need", "rule_hit"); + // Scope: "something" or x itself (cosine >= cleanup_min). + b.op(Instr::Bind { a: "slots".into(), b: "role_scope".into(), out: "scope_noisy".into(), conj: true }); + b.op(Instr::Norm { kind: NormKind::L2, x: "scope_noisy".into(), eps: "eps".into(), out: "scope_unit".into() }); + b.matvec("scope_unit", "something", "sc_dot", false); + b.gate(Mul, "sc_dot", "inv_sqrt_d", "sc_cos"); + b.matmul_ab("x_onehot", "codebook", "x_code"); + b.matvec("scope_unit", "x_code", "sx_dot", false); + b.gate(Mul, "sx_dot", "inv_sqrt_d", "sx_cos"); + b.gate(Ge, "sc_cos", "cleanup_min", "sc_ok"); + b.gate(Ge, "sx_cos", "cleanup_min", "sx_ok"); + b.gate(Max, "sc_ok", "sx_ok", "scope_ok"); + b.gate(Mul, "scope_ok", "pos", "rule_ok"); + b.op(Instr::Transpose { x: "rule_ok".into(), out: "rule_ok_col".into() }); + b.gate(Mul, "rule_hit", "rule_ok_col", "e_rule"); + b.op(Instr::Transpose { x: "e_rule".into(), out: "e_rule_t".into() }); + // A rule's premise property: cleanup of slot (/) subj. + b.op(Instr::Bind { a: "slots".into(), b: "role_subj".into(), out: "prem_noisy".into(), conj: true }); + b.op(Instr::Norm { kind: NormKind::L2, x: "prem_noisy".into(), eps: "eps".into(), out: "prem_unit".into() }); + b.matmul("prem_unit", "codes_unit", "prem_cos"); + b.op(Instr::Topk { x: "prem_cos".into(), k: 1, idx: None, val: Some("prem_best".into()), mask: Some("prem_mask".into()) }); + b.gate(Ge, "prem_best", "cleanup_min", "prem_ok"); + b.gate(Mul, "prem_mask", "prem_ok", "prem_t"); + b.op(Instr::Transpose { x: "prem_t".into(), out: "prem_tt".into() }); + + b.block("depth"); + b.unary(Copy, "p_onehot", "r_frontier"); + b.unary(Copy, "p_onehot", "r_reached"); + zeros(&mut b, "r"); + for r in ["done", "found", "level", "lvl"] { + b.gate(Mul, "zero", "one", r); + } + b.gate(Mul, "x_onehot", "zero", "zero_row_n"); + b.matvec("obj_t", "zero_row_n", "zero_row_s", false); + b.unary(Copy, "zero_row_s", "proof"); + // x's property slot whose object is in the frontier; the path is the rule + // chain of that object (premise matrix over properties). + let mut body = vec![ + g(Sub, "one", "done", "open"), + Instr::Matvec { w: "obj_t".into(), x: "r_frontier".into(), out: "to_frontier".into(), transpose: false }, + Instr::Reduce { f: ReduceFn::Sum, x: "e_prop_x".into(), axis: Axis::Rows, out: "x_prop_col".into() }, + Instr::Transpose { x: "x_prop_col".into(), out: "x_prop".into() }, + g(Mul, "x_prop", "to_frontier", "d_cand"), + ]; + body.extend(best_proof("d", "d_cand", "obj_t", "r_proofs")); + body.extend([ + g(Mul, "d_val", "open", "d_new"), + g(Add, "found", "d_new", "found"), + g(Mul, "d_proof", "d_new", "d_add"), + g(Add, "proof", "d_add", "proof"), + g(Mul, "d_new", "lvl", "lvl_hit"), + g(Add, "level", "lvl_hit", "level"), + g(Max, "done", "d_new", "done"), + ]); + body.extend(expand("r", "e_rule_obj", "e_rule_t", "prem_tt", "enable_conditionals", None)); + body.push(g(Add, "lvl", "one", "lvl")); + // e_rule_obj: the rule matrix as "edges leaving a conclusion" (S x N). + b.unary(Copy, "e_rule", "e_rule_obj"); + b.op(Instr::Step { n: p.max_depth + 1, body }); + Ok(b.finish(&RULES_OUTPUTS)) +} + +/// Counts along `more` chains with phasor arithmetic. +pub fn compile_counts(id: &str, c: &CountParams) -> Result { + let p = &c.relational; + check(id, p)?; + if c.range == 0 || c.range > 1 << 23 { + return Err(IsaError(format!("{id}: invalid number range"))); + } + let d2 = 2 * p.dim; + let r = c.range as i64; + let m = (2 * r + 1) as usize; + let mut b = Builder::new(id); + b.input("x", Some(1), d2); + b.input("rel_has", Some(1), d2); + b.input("rel_more", Some(1), d2); + graph_io(&mut b, d2); + b.external("numbers", m, d2); + support(&mut b, p); + length_constants(&mut b); + probe_block(&mut b, p); + b.block("arithmetic"); + b.scalar("enable_arithmetic", 1.0); + b.block("readout"); + b.constant("values", m, 1, (-r..=r).map(|n| n as f32).collect()); + b.constant("identity", 1, d2, (0..d2).map(|k| if k < p.dim { 1.0 } else { 0.0 }).collect()); + + b.block("probe"); + role(&mut b, ROLE_QUANT, "role_quant"); + cleanup_query(&mut b, "x"); + b.gate(Add, "pos", "neg", "active"); + edges(&mut b, "rel_has", "pos", "e_has"); + edges(&mut b, "rel_more", "active", "e_more_raw"); + b.gate(Mul, "e_more_raw", "enable_arithmetic", "e_more"); + b.op(Instr::Transpose { x: "e_more".into(), out: "e_more_t".into() }); + b.op(Instr::Transpose { x: "obj_t".into(), out: "obj_tt".into() }); + + // Breadth-first over more edges, carrying number(sum of differences). + // Differences are cleaned up first (only rows of more slots are nonzero). + b.op(Instr::Bind { a: "slots".into(), b: "role_quant".into(), out: "quant".into(), conj: true }); + b.reduce(ReduceFn::Sum, "e_more", Axis::Rows, "more_rows"); + b.gate(Mul, "quant", "more_rows", "quant_more"); + b.matmul("quant_more", "numbers", "delta_scores"); + b.op(Instr::Topk { x: "delta_scores".into(), k: 1, idx: None, val: None, mask: Some("delta_mask".into()) }); + b.gate(Mul, "delta_mask", "more_rows", "delta_sel"); + b.matmul_ab("delta_sel", "numbers", "delta_clean"); + + b.block("depth"); + b.unary(Copy, "x_onehot", "c_frontier"); + b.unary(Copy, "x_onehot", "c_reached"); + zeros(&mut b, "c"); + b.op(Instr::Transpose { x: "x_onehot".into(), out: "x_col".into() }); + b.matmul_ab("x_col", "identity", "c_acc"); + b.op(Instr::Step { n: p.max_depth, body: expand("c", "e_more", "e_more_t", "obj_tt", "one", Some("delta_clean")) }); + + // Every reached node's positive has-count slots. + b.block("readout"); + b.gate(Mul, "e_has", "c_reached", "e_cand"); + b.reduce(ReduceFn::Sum, "e_cand", Axis::Rows, "cand_col"); + b.op(Instr::Transpose { x: "cand_col".into(), out: "cand".into() }); + b.gate(Mul, "quant", "cand_col", "quant_cand"); + b.matmul("quant_cand", "numbers", "count_scores"); + b.op(Instr::Topk { x: "count_scores".into(), k: 1, idx: None, val: None, mask: Some("count_mask".into()) }); + b.gate(Mul, "count_mask", "cand_col", "count_sel"); + b.matmul_ab("count_sel", "numbers", "count_clean"); + b.matmul_ab("e_cand", "c_acc", "slot_acc"); + b.op(Instr::Bind { a: "count_clean".into(), b: "slot_acc".into(), out: "total".into(), conj: false }); + b.matmul("total", "numbers", "total_scores"); + b.op(Instr::Topk { x: "total_scores".into(), k: 1, idx: None, val: None, mask: Some("total_mask".into()) }); + b.gate(Mul, "total_mask", "cand_col", "value_sel"); + b.reduce(ReduceFn::Max, "value_sel", Axis::Cols, "present"); + b.reduce(ReduceFn::Sum, "present", Axis::All, "distinct"); + b.gate(Ge, "distinct", "one", "found"); + b.scalar("two", 2.0); + b.gate(Ge, "distinct", "two", "contradiction"); + b.gate(Sub, "one", "contradiction", "single"); + b.gate(Mul, "found", "single", "one_value"); + b.matmul_ab("present", "values", "value_raw"); + b.gate(Mul, "value_raw", "one_value", "value"); + // Proofs: the shortest candidate, or the union of all of them. + for i in best_proof("o", "cand", "e_cand", "c_proofs") { + b.op(i); + } + b.matmul_ab("cand", "e_cand", "cand_nodes"); + b.matmul_ab("cand_nodes", "c_proofs", "all_paths"); + b.gate(Add, "all_paths", "cand", "all_raw"); + b.gate(Min, "all_raw", "one", "all_proof"); + b.gate(Mul, "o_proof", "one_value", "one_part"); + b.gate(Mul, "all_proof", "contradiction", "all_part"); + b.gate(Add, "one_part", "all_part", "premises"); + // Arithmetic was used: a path behind the chosen (or any) candidate. + b.reduce(ReduceFn::Sum, "o_path", Axis::All, "o_path_len"); + b.reduce(ReduceFn::Sum, "all_paths", Axis::All, "all_path_len"); + b.gate(Ge, "o_path_len", "one", "o_arith"); + b.gate(Ge, "all_path_len", "one", "all_arith"); + b.gate(Mul, "o_arith", "one_value", "arith_one"); + b.gate(Mul, "all_arith", "contradiction", "arith_all"); + b.gate(Add, "arith_one", "arith_all", "arith"); + Ok(b.finish(&COUNTS_OUTPUTS)) +} + +/// Positive and negative findings to a verdict and its premises. +pub fn compile_verdict(id: &str, p: &Params) -> Result { + check(id, p)?; + let mut b = Builder::new(id); + b.input("pos_found", Some(1), 1); + b.input("neg_found", Some(1), 1); + b.input_dyn("pos_proof", Some(1), None); + b.input_dyn("neg_proof", Some(1), None); + b.block("combine"); + b.scalar("one", 1.0); + b.constant("yes_sel", 1, 4, onehot(4, 0)); + b.constant("no_sel", 1, 4, onehot(4, 1)); + b.constant("contra_sel", 1, 4, onehot(4, 2)); + b.constant("unknown_sel", 1, 4, onehot(4, 3)); + b.gate(Min, "pos_found", "one", "pf"); + b.gate(Min, "neg_found", "one", "nf"); + b.gate(Sub, "one", "pf", "not_p"); + b.gate(Sub, "one", "nf", "not_n"); + b.gate(Mul, "pf", "not_n", "yes"); + b.gate(Mul, "nf", "not_p", "no"); + b.gate(Mul, "pf", "nf", "contra"); + b.gate(Mul, "not_p", "not_n", "unknown"); + b.gate(Mul, "yes", "yes_sel", "v_yes"); + b.gate(Mul, "no", "no_sel", "v_no"); + b.gate(Mul, "contra", "contra_sel", "v_contra"); + b.gate(Mul, "unknown", "unknown_sel", "v_unknown"); + b.gate(Add, "v_yes", "v_no", "v_a"); + b.gate(Add, "v_a", "v_contra", "v_b"); + b.gate(Add, "v_b", "v_unknown", "verdict"); + b.gate(Mul, "pos_proof", "pf", "pp"); + b.gate(Mul, "neg_proof", "nf", "np"); + b.gate(Add, "pp", "np", "both"); + b.gate(Min, "both", "one", "premises"); + Ok(b.finish(&VERDICT_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/circuits/memory_write.rs b/crates/zerolm-wsp/src/circuits/memory_write.rs new file mode 100644 index 0000000..f611539 --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/memory_write.rs @@ -0,0 +1,468 @@ +//! The memory store's write decisions as circuits. The host prepares a stamp +//! (sequence number, source, provenance, key word count: bookkeeping it +//! owns), runs a program, and moves the rows the outputs specify; every +//! decision about what the memory becomes is made here. +//! +//! Metadata layout (`memory.semantic.meta`, `META_COLS` columns) and +//! episodic layout (`memory.episodic.meta`, `EP_COLS`) are fixed below and +//! checked against `zerolm_memory` by its tests. +//! +//! - **`memory_write`** (teach, correct, confirm; `op` one-hot). The same-key +//! slot is the best active key (`keys . key`) with score >= the same-key +//! threshold; its value row is decoded and compared with the new value. +//! The free slot is the lowest empty one. Outcome (one-hot, `OUTCOMES`): +//! teach: unchanged (same value), conflict (different), stored (no key); +//! correct: unchanged, superseded, stored; confirm: confirmed, unknown. +//! Outputs the new slot's key, key-word, value, and metadata rows (zero +//! unless a new slot is written), the old slot's updated metadata row, the +//! episode metadata row, and the slots involved. +//! - **`memory_forget`**: the same-key slot and every slot it supersedes +//! (one link per step, `slots` steps, so any chain is followed); outputs +//! the chain mask, the episodic rows that refer to it (their keys are +//! erased), the metadata with the chain marked deleted, and the episode row. +//! - **`memory_compact`**: active slots move to the lowest slots in order +//! (`order`, from top-k with ties to the lowest index); supersession links +//! and episodic slot references are remapped (-1 when the target is not +//! active); outputs the new metadata and episodic slot column. +//! - **`memory_history`**: valid episodes whose key matches the query key +//! (dot product >= the same-key threshold). + +use super::value_codec::{codec_io, emit_decode, emit_encode, support, CodecParams}; +use crate::compiler::{Builder, Compiled}; +use serde::{Deserialize, Serialize}; +use zerolm_engine::isa::{Axis, GateFn::*, Instr, IsaError, ReduceFn}; + +/// Semantic metadata columns. +pub mod field { + pub const STATUS: usize = 0; + pub const CONFIDENCE: usize = 1; + pub const UPDATED_LO: usize = 4; + pub const UPDATED_HI: usize = 5; + pub const SUPERSEDES: usize = 10; + pub const SUPERSEDED_BY: usize = 11; + pub const CONTRADICTIONS: usize = 12; +} +pub const META_COLS: usize = 14; +/// Episodic metadata columns. +pub mod ep_field { + pub const OP: usize = 0; + pub const SLOT: usize = 3; + pub const VALID: usize = 8; +} +pub const EP_COLS: usize = 9; +/// Status codes. +pub const EMPTY: f32 = 0.0; +pub const ACTIVE: f32 = 1.0; +pub const SUPERSEDED: f32 = 2.0; +pub const DELETED: f32 = 3.0; +/// Episode op codes (index into the memory store's op names). +pub const OP_ADD: f32 = 1.0; +pub const OP_UNCHANGED: f32 = 2.0; +pub const OP_CONTRADICTION: f32 = 3.0; +pub const OP_SUPERSEDE: f32 = 4.0; +pub const OP_CONFIRM: f32 = 5.0; +pub const OP_FORGET: f32 = 6.0; +/// `memory_write` outcome columns. +pub const OUTCOMES: [&str; 6] = ["stored", "unchanged", "conflict", "superseded", "confirmed", "unknown"]; + +const BIG: f32 = 1.0e6; + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct WriteParams { + pub codec: CodecParams, + pub slots: usize, + pub key_dim: usize, + /// Width of a key-word row (max_key_words * key_dim). + pub key_words: usize, + pub episodes: usize, + pub same_key_threshold: f32, + pub unconfirmed: f32, + pub confirmed: f32, +} + +pub const WRITE_OUTPUTS: [&str; 11] = ["outcome", "best", "slot", "free", "full", "new_key", "new_words", "new_value", "new_meta", "old_meta", "episode"]; +pub const FORGET_OUTPUTS: [&str; 6] = ["found", "slot", "chain", "erase", "meta_out", "episode"]; +pub const COMPACT_OUTPUTS: [&str; 5] = ["order", "n_active", "freed", "meta_out", "ep_slot"]; +pub const HISTORY_OUTPUTS: [&str; 1] = ["hits"]; + +fn check(id: &str, p: &WriteParams) -> Result<(), IsaError> { + if p.slots < 2 || p.key_dim == 0 || p.key_words == 0 || p.episodes == 0 { + return Err(IsaError(format!("{id}: invalid memory write parameters"))); + } + Ok(()) +} + +fn onehot(n: usize, i: usize) -> Vec { + let mut v = vec![0.0; n]; + v[i] = 1.0; + v +} + +fn eq(b: &mut Builder, a: &str, x: &str, out: &str) { + b.gate(Ge, a, x, &format!("{out}_lo")); + b.gate(Ge, x, a, &format!("{out}_hi")); + b.gate(Mul, &format!("{out}_lo"), &format!("{out}_hi"), out); +} + +/// Column `col` of `meta` as a 1 x S row, into `out`. +fn column(b: &mut Builder, m: &str, cols: usize, col: usize, out: &str) { + let sel = format!("{out}_sel"); + b.constant(&sel, 1, cols, onehot(cols, col)); + b.matvec(m, &sel, out, false); +} + +/// The same-key slot: `best_mask` (1 x S), `best` (index), `same` (1 x 1), +/// and `active` (1 x S). Needs `one`, `big`, and `status`. +fn same_key(b: &mut Builder, p: &WriteParams) { + b.block("same_key"); + b.scalar("threshold", p.same_key_threshold); + b.scalar("active_code", ACTIVE); + eq(b, "status", "active_code", "active"); + b.matvec("keys", "key", "raw", false); + b.gate(Sub, "one", "active", "inactive"); + b.gate(Mul, "inactive", "big", "penalty"); + b.gate(Mul, "raw", "active", "raw_active"); + b.gate(Sub, "raw_active", "penalty", "scores"); + b.op(Instr::Topk { x: "scores".into(), k: 1, idx: Some("best".into()), val: Some("best_val".into()), mask: Some("best_mask".into()) }); + b.reduce(ReduceFn::Sum, "active", Axis::All, "n_active"); + b.gate(Ge, "n_active", "one", "have"); + b.gate(Ge, "best_val", "threshold", "close"); + b.gate(Mul, "close", "have", "same"); +} + +fn status_row(b: &mut Builder) { + b.block("layout"); + column(b, "meta", META_COLS, field::STATUS, "status"); +} + +pub fn compile_write(id: &str, p: &WriteParams) -> Result { + check(id, p)?; + let c = &p.codec; + let mut b = Builder::new(id); + b.input("key", Some(1), p.key_dim); + b.input("words", Some(1), p.key_words); + b.input("value", Some(1), c.positions); + b.input("op", Some(1), 3); + b.input("stamp", Some(1), META_COLS); + b.input("ep_stamp", Some(1), EP_COLS); + b.external("keys", p.slots, p.key_dim); + b.external("values", p.slots, c.positions * c.code_dim); + b.external("meta", p.slots, META_COLS); + codec_io(&mut b, c); + support(&mut b); + status_row(&mut b); + same_key(&mut b, p); + + // The stored value of the same-key slot, decoded and compared. + b.matmul_ab("best_mask", "values", "best_row"); + emit_decode(&mut b, c, "best_row"); + b.block("compare"); + b.scalar("tolerance", 0.5); + b.gate(Sub, "bytes", "value", "diff_pos"); + b.gate(Sub, "value", "bytes", "diff_neg"); + b.gate(Max, "diff_pos", "diff_neg", "diff_abs"); + b.reduce(ReduceFn::Max, "diff_abs", Axis::All, "diff_max"); + b.gate(Ge, "diff_max", "tolerance", "differs"); + b.gate(Sub, "one", "differs", "same_value"); + + // The lowest empty slot. + b.block("free"); + b.scalar("empty_code", EMPTY); + eq(&mut b, "status", "empty_code", "empty"); + b.op(Instr::Topk { x: "empty".into(), k: 1, idx: Some("free".into()), val: Some("has_free".into()), mask: None }); + b.gate(Sub, "one", "has_free", "full"); + + // Outcomes. + b.block("decide"); + for (k, name) in ["teach", "correct", "confirm"].iter().enumerate() { + let sel = format!("op_{name}_sel"); + b.constant(&sel, 3, 1, onehot(3, k)); + b.matmul_ab("op", &sel, &format!("is_{name}")); + } + b.gate(Add, "is_teach", "is_correct", "is_write"); + b.gate(Sub, "one", "same", "absent"); + b.gate(Sub, "one", "same_value", "changed"); + b.gate(Mul, "same", "same_value", "same_both"); + b.gate(Mul, "same", "changed", "same_changed"); + b.gate(Mul, "is_write", "same_both", "o_unchanged"); + b.gate(Mul, "is_teach", "same_changed", "o_conflict"); + b.gate(Mul, "is_correct", "same_changed", "o_superseded"); + b.gate(Mul, "is_write", "absent", "o_stored"); + b.gate(Mul, "is_confirm", "same", "o_confirmed"); + b.gate(Mul, "is_confirm", "absent", "o_unknown"); + let mut acc = String::new(); + for (k, name) in OUTCOMES.iter().enumerate() { + let sel = format!("out_{name}_sel"); + b.constant(&sel, 1, OUTCOMES.len(), onehot(OUTCOMES.len(), k)); + let term = format!("out_{name}"); + b.gate(Mul, &format!("o_{name}"), &sel, &term); + if acc.is_empty() { + acc = term; + } else { + let next = format!("outcome_{k}"); + b.gate(Add, &acc, &term, &next); + acc = next; + } + } + b.unary(Copy, &acc, "outcome"); + b.gate(Add, "o_stored", "o_superseded", "writes_new"); + + // The new slot's rows: a fresh record plus the stamp; a superseding + // record points at the old slot. + b.block("record"); + let mut fresh = vec![0.0f32; META_COLS]; + fresh[field::STATUS] = ACTIVE; + fresh[field::CONFIDENCE] = p.unconfirmed; + fresh[field::SUPERSEDES] = -1.0; + fresh[field::SUPERSEDED_BY] = -1.0; + b.constant("fresh", 1, META_COLS, fresh); + b.constant("e_supersedes", 1, META_COLS, onehot(META_COLS, field::SUPERSEDES)); + b.gate(Add, "fresh", "stamp", "record_base"); + b.gate(Add, "best", "one", "best_plus"); + b.gate(Mul, "best_plus", "o_superseded", "link_shift"); + b.gate(Mul, "e_supersedes", "link_shift", "link_row"); + b.gate(Add, "record_base", "link_row", "record"); + b.gate(Mul, "record", "writes_new", "new_meta"); + b.gate(Mul, "key", "writes_new", "new_key"); + b.gate(Mul, "words", "writes_new", "new_words"); + emit_encode(&mut b, c, "value", "value_row"); + b.block("record"); + b.gate(Mul, "value_row", "writes_new", "new_value"); + + // The same-key slot's metadata after the operation. + b.matmul_ab("best_mask", "meta", "old"); + let sel = |b: &mut Builder, col: usize, name: &str| { + b.constant(&format!("e_{name}"), 1, META_COLS, onehot(META_COLS, col)); + b.gate(Mul, "old", &format!("e_{name}"), &format!("old_{name}_row")); + b.reduce(ReduceFn::Sum, &format!("old_{name}_row"), Axis::All, &format!("old_{name}")); + }; + sel(&mut b, field::STATUS, "status"); + sel(&mut b, field::CONFIDENCE, "confidence"); + sel(&mut b, field::SUPERSEDED_BY, "superseded_by"); + b.constant("e_contradictions", 1, META_COLS, onehot(META_COLS, field::CONTRADICTIONS)); + let mut upd = vec![0.0f32; META_COLS]; + upd[field::UPDATED_LO] = 1.0; + upd[field::UPDATED_HI] = 1.0; + b.constant("e_updated", 1, META_COLS, upd); + b.scalar("superseded_code", SUPERSEDED); + b.scalar("confirmed_level", p.confirmed); + // conflict: contradictions + 1 + b.gate(Mul, "e_contradictions", "o_conflict", "d_conflict"); + // superseded: status -> SUPERSEDED, superseded_by -> free + b.gate(Sub, "superseded_code", "old_status", "status_gap"); + b.gate(Mul, "status_gap", "o_superseded", "status_shift"); + b.gate(Mul, "e_status", "status_shift", "d_status"); + b.gate(Sub, "free", "old_superseded_by", "link_gap"); + b.gate(Mul, "link_gap", "o_superseded", "link_move"); + b.gate(Mul, "e_superseded_by", "link_move", "d_link"); + // confirmed: confidence -> confirmed level + b.gate(Sub, "confirmed_level", "old_confidence", "conf_gap"); + b.gate(Mul, "conf_gap", "o_confirmed", "conf_shift"); + b.gate(Mul, "e_confidence", "conf_shift", "d_conf"); + // superseded or confirmed: updated time -> stamp + b.gate(Add, "o_superseded", "o_confirmed", "touched"); + b.gate(Mul, "stamp", "e_updated", "stamp_upd"); + b.gate(Mul, "old", "e_updated", "old_upd"); + b.gate(Sub, "stamp_upd", "old_upd", "upd_gap"); + b.gate(Mul, "upd_gap", "touched", "d_upd"); + b.gate(Add, "old", "d_conflict", "old_a"); + b.gate(Add, "old_a", "d_status", "old_b"); + b.gate(Add, "old_b", "d_link", "old_c"); + b.gate(Add, "old_c", "d_conf", "old_d"); + b.gate(Add, "old_d", "d_upd", "old_meta"); + + // The episode row: op code and the slot it concerns. + b.block("episode"); + b.constant("op_codes", OUTCOMES.len(), 1, vec![OP_ADD, OP_UNCHANGED, OP_CONTRADICTION, OP_SUPERSEDE, OP_CONFIRM, 0.0]); + b.matmul_ab("outcome", "op_codes", "op_code"); + b.constant("e_op", 1, EP_COLS, onehot(EP_COLS, ep_field::OP)); + b.constant("e_slot", 1, EP_COLS, onehot(EP_COLS, ep_field::SLOT)); + b.gate(Sub, "free", "best", "slot_gap"); + b.gate(Mul, "slot_gap", "writes_new", "slot_shift"); + b.gate(Add, "best", "slot_shift", "slot"); + b.gate(Mul, "e_op", "op_code", "ep_op"); + b.gate(Mul, "e_slot", "slot", "ep_slot"); + b.gate(Add, "ep_stamp", "ep_op", "ep_a"); + b.gate(Add, "ep_a", "ep_slot", "episode"); + Ok(b.finish(&WRITE_OUTPUTS)) +} + +pub fn compile_forget(id: &str, p: &WriteParams) -> Result { + check(id, p)?; + let s = p.slots; + let mut b = Builder::new(id); + b.input("key", Some(1), p.key_dim); + b.input("stamp", Some(1), META_COLS); + b.input("ep_stamp", Some(1), EP_COLS); + b.external("keys", s, p.key_dim); + b.external("meta", s, META_COLS); + b.external("ep_meta", p.episodes, EP_COLS); + support(&mut b); + status_row(&mut b); + same_key(&mut b, p); + + // Follow supersedes links from the same-key slot. + b.block("chain"); + b.scalar("zero", 0.0); + b.constant("index", 1, s, (0..s).map(|i| i as f32).collect()); + column(&mut b, "meta", META_COLS, field::SUPERSEDES, "links"); + b.gate(Mul, "best_mask", "same", "chain"); + b.unary(Copy, "chain", "cur"); + let g = |f, a: &str, x: &str, out: &str| Instr::Gate { f, a: a.into(), b: Some(x.into()), out: out.into() }; + b.op(Instr::Step { + n: s, + body: vec![ + g(Mul, "cur", "links", "cur_link_row"), + Instr::Reduce { f: ReduceFn::Sum, x: "cur_link_row".into(), axis: Axis::All, out: "target".into() }, + Instr::Reduce { f: ReduceFn::Sum, x: "cur".into(), axis: Axis::All, out: "moving".into() }, + g(Ge, "target", "zero", "linked"), + g(Ge, "index", "target", "t_lo"), + g(Ge, "target", "index", "t_hi"), + g(Mul, "t_lo", "t_hi", "t_hit"), + g(Mul, "t_hit", "linked", "t_linked"), + g(Mul, "t_linked", "moving", "t_live"), + g(Sub, "one", "chain", "unvisited"), + g(Mul, "t_live", "unvisited", "cur"), + g(Max, "chain", "cur", "chain"), + ], + }); + + // Episodes that refer to a chain slot (valid ones) lose their key. + b.block("erase"); + column(&mut b, "ep_meta", EP_COLS, ep_field::SLOT, "ep_slot_row"); + column(&mut b, "ep_meta", EP_COLS, ep_field::VALID, "ep_valid"); + b.op(Instr::Transpose { x: "ep_slot_row".into(), out: "ep_slot_col".into() }); + eq(&mut b, "ep_slot_col", "index", "ep_onehot"); + b.matvec("ep_onehot", "chain", "ep_in_chain", false); + b.gate(Mul, "ep_in_chain", "ep_valid", "erase"); + + // Chain slots become deleted, updated at the stamp. + b.block("mark"); + let mut cols = vec![0.0f32; META_COLS]; + cols[field::STATUS] = 1.0; + cols[field::UPDATED_LO] = 1.0; + cols[field::UPDATED_HI] = 1.0; + b.constant("marked_cols", 1, META_COLS, cols); + b.constant("deleted_row", 1, META_COLS, onehot(META_COLS, field::STATUS).iter().map(|x| x * DELETED).collect()); + b.gate(Mul, "stamp", "marked_cols", "stamp_cols"); + b.gate(Add, "deleted_row", "stamp_cols", "marked_vals"); + b.op(Instr::Transpose { x: "chain".into(), out: "chain_col".into() }); + b.matmul_ab("chain_col", "marked_cols", "replace"); + b.gate(Sub, "one", "replace", "keep"); + b.gate(Mul, "meta", "keep", "kept"); + b.matmul_ab("chain_col", "marked_vals", "written"); + b.gate(Add, "kept", "written", "meta_out"); + + b.block("episode"); + b.scalar("forget_code", OP_FORGET); + b.constant("e_op", 1, EP_COLS, onehot(EP_COLS, ep_field::OP)); + b.constant("e_slot", 1, EP_COLS, onehot(EP_COLS, ep_field::SLOT)); + b.gate(Mul, "e_op", "forget_code", "ep_op"); + b.gate(Mul, "e_slot", "best", "ep_slot"); + b.gate(Add, "ep_stamp", "ep_op", "ep_a"); + b.gate(Add, "ep_a", "ep_slot", "episode"); + b.unary(Copy, "same", "found"); + b.unary(Copy, "best", "slot"); + Ok(b.finish(&FORGET_OUTPUTS)) +} + +pub fn compile_compact(id: &str, p: &WriteParams) -> Result { + check(id, p)?; + let s = p.slots; + let mut b = Builder::new(id); + b.external("meta", s, META_COLS); + b.external("ep_meta", p.episodes, EP_COLS); + support(&mut b); + status_row(&mut b); + + b.block("order"); + b.scalar("active_code", ACTIVE); + b.scalar("empty_code", EMPTY); + b.scalar("big_score", BIG * 10.0); + b.constant("index", 1, s, (0..s).map(|i| i as f32).collect()); + b.op(Instr::Transpose { x: "index".into(), out: "index_col".into() }); + eq(&mut b, "status", "active_code", "active"); + eq(&mut b, "status", "empty_code", "empty"); + b.reduce(ReduceFn::Sum, "active", Axis::All, "n_active"); + b.reduce(ReduceFn::Sum, "empty", Axis::All, "n_empty"); + b.scalar("n_slots", s as f32); + b.gate(Sub, "n_slots", "n_active", "not_active"); + b.gate(Sub, "not_active", "n_empty", "freed"); + // Active slots first, in index order (score big - index), then the rest. + b.gate(Sub, "big_score", "index", "rank_score"); + b.gate(Mul, "rank_score", "active", "score"); + b.op(Instr::Topk { x: "score".into(), k: s, idx: Some("order".into()), val: None, mask: None }); + // P[j, s] = 1 when new row j holds old slot s (j < n_active). + b.op(Instr::Transpose { x: "order".into(), out: "order_col".into() }); + eq(&mut b, "order_col", "index", "perm_all"); + b.gate(Add, "index_col", "one", "position"); + b.gate(Ge, "n_active", "position", "keep_col"); + b.gate(Mul, "perm_all", "keep_col", "perm"); + b.matmul_ab("perm", "meta", "moved"); + // map[s]: the new index of an active slot, else -1. + b.matmul_ab("index", "perm", "rank"); + b.gate(Mul, "rank", "active", "rank_active"); + b.gate(Sub, "active", "one", "minus_inactive"); + b.gate(Add, "rank_active", "minus_inactive", "map"); + b.op(Instr::Transpose { x: "map".into(), out: "map_col".into() }); + + // Remap a column of slot references: map[v] for v >= 0, else -1. + b.block("remap"); + let remap = |b: &mut Builder, col_reg: &str, out: &str| { + eq(b, col_reg, "index", &format!("{out}_onehot")); + b.matmul_ab(&format!("{out}_onehot"), "map_col", &format!("{out}_lookup")); + b.reduce(ReduceFn::Sum, &format!("{out}_onehot"), Axis::Rows, &format!("{out}_any")); + b.gate(Sub, &format!("{out}_any"), "one", &format!("{out}_none")); + b.gate(Add, &format!("{out}_lookup"), &format!("{out}_none"), out); + }; + for (col, name) in [(field::SUPERSEDES, "sup"), (field::SUPERSEDED_BY, "sup_by")] { + let sel = format!("{name}_sel"); + b.constant(&sel, META_COLS, 1, onehot(META_COLS, col)); + b.matmul_ab("moved", &sel, &format!("{name}_col")); + remap(&mut b, &format!("{name}_col"), &format!("{name}_new")); + } + let mut links = vec![0.0f32; META_COLS]; + links[field::SUPERSEDES] = 1.0; + links[field::SUPERSEDED_BY] = 1.0; + b.constant("link_cols", 1, META_COLS, links); + b.gate(Sub, "one", "link_cols", "other_cols"); + b.gate(Mul, "moved", "other_cols", "moved_rest"); + b.constant("e_sup", 1, META_COLS, onehot(META_COLS, field::SUPERSEDES)); + b.constant("e_sup_by", 1, META_COLS, onehot(META_COLS, field::SUPERSEDED_BY)); + b.matmul_ab("sup_new", "e_sup", "sup_part"); + b.matmul_ab("sup_by_new", "e_sup_by", "sup_by_part"); + b.gate(Add, "moved_rest", "sup_part", "remapped_a"); + b.gate(Add, "remapped_a", "sup_by_part", "remapped"); + // Rows past the active ones are empty records (links -1). + let mut empty_row = vec![0.0f32; META_COLS]; + empty_row[field::SUPERSEDES] = -1.0; + empty_row[field::SUPERSEDED_BY] = -1.0; + b.constant("empty_row", 1, META_COLS, empty_row); + b.gate(Mul, "remapped", "keep_col", "kept_rows"); + b.gate(Sub, "one", "keep_col", "drop_col"); + b.matmul_ab("drop_col", "empty_row", "empty_rows"); + b.gate(Add, "kept_rows", "empty_rows", "meta_out"); + // Episodic slot references. + column(&mut b, "ep_meta", EP_COLS, ep_field::SLOT, "ep_slot_row"); + b.op(Instr::Transpose { x: "ep_slot_row".into(), out: "ep_slot_col".into() }); + remap(&mut b, "ep_slot_col", "ep_slot"); + Ok(b.finish(&COMPACT_OUTPUTS)) +} + +pub fn compile_history(id: &str, p: &WriteParams) -> Result { + check(id, p)?; + let mut b = Builder::new(id); + b.input("key", Some(1), p.key_dim); + b.external("ep_keys", p.episodes, p.key_dim); + b.external("ep_meta", p.episodes, EP_COLS); + b.block("match"); + b.scalar("threshold", p.same_key_threshold); + b.scalar("one", 1.0); + column(&mut b, "ep_meta", EP_COLS, ep_field::VALID, "valid"); + eq(&mut b, "valid", "one", "is_valid"); + b.matvec("ep_keys", "key", "dot", false); + b.gate(Ge, "dot", "threshold", "close"); + b.gate(Mul, "close", "is_valid", "hits"); + Ok(b.finish(&HISTORY_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/circuits/mod.rs b/crates/zerolm-wsp/src/circuits/mod.rs index 14bf0f0..deeed37 100644 --- a/crates/zerolm-wsp/src/circuits/mod.rs +++ b/crates/zerolm-wsp/src/circuits/mod.rs @@ -2,6 +2,11 @@ //! constants; none may contain language data (constitution invariant 11). pub mod atom_encode; +pub mod decide; pub mod attention_read; pub mod key_encode; +pub mod memory_write; +pub mod number; +pub mod probe; pub mod relational; +pub mod value_codec; diff --git a/crates/zerolm-wsp/src/circuits/number.rs b/crates/zerolm-wsp/src/circuits/number.rs new file mode 100644 index 0000000..beb1829 --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/number.rs @@ -0,0 +1,173 @@ +//! Fractional-power number circuits: decimal bytes to a number phasor, and a +//! noisy number phasor back to an integer by cleanup. +//! +//! A number is `number(n)_k = exp(i n theta_k)` (seeded base `theta`), so +//! `number(a) * number(b) = number(a + b)`. Because the encoding is a +//! homomorphism, a decimal numeral is the product of its digit phasors: +//! `number(sum_q v_q 10^q) = prod_q number(v_q 10^q)`. The encoder therefore +//! needs no trigonometry: it selects one row per digit from a seeded table +//! (`core.reasoner.number_digits`, row `10 q + v` = `number(v 10^q)`) and binds +//! them. +//! +//! `number-encode`: input `numeral` is 1 x L byte values, the decimal numeral +//! (optionally with a leading `-`) at positions 0..len and zero elsewhere. The +//! host copies bytes and inspects none. +//! 1. Classify each byte against the ten digit codes (one-hot `L x 10`). +//! 2. A digit's place is the number of digit bytes after it (a strict lower +//! triangular matrix), one-hot over `places`. +//! 3. The outer product of place and digit one-hots selects one table row per +//! byte; non-digit bytes take the identity phasor (number 0). +//! 4. Bind the L rows. A leading `-` (byte 45) conjugates the result. +//! A digit beyond `places` selects no row, which zeroes the output. +//! +//! `number-decode`: input `v` (1 x 2d). Scores against the seeded codebook +//! (`core.reasoner.number_codebook`, row j = `number(j - range)`), top-1, +//! then the selected row's value (`value`, exact integer) and the codebook row +//! itself (`clean`), so arithmetic can continue on clean vectors. +//! +//! Blocks (units of ablation): encode `classify`, `position`, `select`, +//! `fill`, `product`, `sign`; decode `readout`. + +use crate::compiler::{Builder, Compiled}; +use serde::{Deserialize, Serialize}; +use zerolm_engine::isa::{Axis, GateFn, Instr, IsaError, ReduceFn}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct EncodeParams { + /// Input width L in bytes. + pub numeral_bytes: usize, + /// Decimal places in the digit table (largest magnitude 10^places - 1). + pub places: usize, + /// Complex dimension of the output vector. + pub dim: usize, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct DecodeParams { + /// The codebook covers [-range, range]. + pub range: u32, + pub dim: usize, +} + +pub const ENCODE_OUTPUTS: [&str; 1] = ["number"]; +pub const DECODE_OUTPUTS: [&str; 2] = ["value", "clean"]; + +/// Byte value of the digit 0 and of the minus sign. +const ZERO: u8 = b'0'; +const MINUS: u8 = b'-'; + +fn onehot(n: usize, i: usize) -> Vec { + let mut v = vec![0.0; n]; + v[i] = 1.0; + v +} + +pub fn compile_encode(id: &str, p: &EncodeParams) -> Result { + if p.numeral_bytes < 2 || p.places == 0 || p.places > 9 || p.dim == 0 { + return Err(IsaError(format!("{id}: invalid number_encode parameters"))); + } + let (l, q, d) = (p.numeral_bytes, p.places, p.dim); + let mut b = Builder::new(id); + b.input("numeral", Some(1), l); + b.external("digits", 10 * q, 2 * d); + + // Digit one-hot per byte: codes[v] == byte. + b.block("classify"); + b.constant("codes", 1, 10, (0..10).map(|v| (ZERO + v) as f32).collect()); + b.op(Instr::Transpose { x: "numeral".into(), out: "col".into() }); + b.gate(GateFn::Ge, "col", "codes", "ge_lo"); + b.gate(GateFn::Ge, "codes", "col", "ge_hi"); + b.gate(GateFn::Mul, "ge_lo", "ge_hi", "digit_oh"); + b.reduce(ReduceFn::Sum, "digit_oh", Axis::Rows, "is_digit"); + + // Place of each byte: digits after it, one-hot over the table's places. + b.block("position"); + let mut after = vec![0.0f32; l * l]; + for i in 0..l { + for j in 0..i { + after[i * l + j] = 1.0; + } + } + b.constant("after", l, l, after); + b.op(Instr::Transpose { x: "is_digit".into(), out: "digit_row".into() }); + b.matmul_ab("digit_row", "after", "place_row"); + b.op(Instr::Transpose { x: "place_row".into(), out: "place".into() }); + b.constant("places", 1, q, (0..q).map(|x| x as f32).collect()); + b.gate(GateFn::Ge, "place", "places", "pl_lo"); + b.gate(GateFn::Ge, "places", "place", "pl_hi"); + b.gate(GateFn::Mul, "pl_lo", "pl_hi", "place_oh"); + + // Row 10 q + v of the table for a digit v at place q. + b.block("select"); + let mut spread = vec![0.0f32; q * 10 * q]; + let mut tile = vec![0.0f32; 10 * 10 * q]; + for pq in 0..q { + for v in 0..10 { + spread[pq * 10 * q + pq * 10 + v] = 1.0; + tile[v * 10 * q + pq * 10 + v] = 1.0; + } + } + b.constant("spread", q, 10 * q, spread); + b.constant("tile", 10, 10 * q, tile); + b.matmul_ab("place_oh", "spread", "by_place"); + b.matmul_ab("digit_oh", "tile", "by_digit"); + b.gate(GateFn::Mul, "by_place", "by_digit", "sel"); + b.matmul_ab("sel", "digits", "rows"); + + // Non-digit bytes are the identity phasor (number 0). + b.block("fill"); + b.constant("ones_col", l, 1, vec![1.0; l]); + b.constant("identity", 1, 2 * d, (0..2 * d).map(|k| if k < d { 1.0 } else { 0.0 }).collect()); + b.gate(GateFn::Sub, "ones_col", "is_digit", "non_digit"); + b.matmul_ab("non_digit", "identity", "fill"); + b.gate(GateFn::Add, "rows", "fill", "factors"); + + // Product of the L factors. + b.block("product"); + for j in 0..l { + b.constant(&format!("pick{j}"), 1, l, onehot(l, j)); + b.matmul_ab(&format!("pick{j}"), "factors", &format!("f{j}")); + } + let mut acc = "f0".to_string(); + for j in 1..l { + let out = format!("p{j}"); + b.op(Instr::Bind { a: acc, b: format!("f{j}"), out: out.clone(), conj: false }); + acc = out; + } + + // A leading minus conjugates: im *= 1 - 2 neg. + b.block("sign"); + b.constant("first", l, 1, onehot(l, 0)); + b.scalar("minus", MINUS as f32); + b.constant("flip", 1, 2 * d, (0..2 * d).map(|k| if k < d { 0.0 } else { -2.0 }).collect()); + b.constant("ones_row", 1, 2 * d, vec![1.0; 2 * d]); + b.matmul_ab("numeral", "first", "lead"); + b.gate(GateFn::Ge, "lead", "minus", "m_lo"); + b.gate(GateFn::Ge, "minus", "lead", "m_hi"); + b.gate(GateFn::Mul, "m_lo", "m_hi", "neg"); + b.gate(GateFn::Mul, "neg", "flip", "flip_neg"); + b.gate(GateFn::Add, "ones_row", "flip_neg", "sign_mask"); + b.gate(GateFn::Mul, &acc, "sign_mask", "number"); + + Ok(b.finish(&ENCODE_OUTPUTS)) +} + +pub fn compile_decode(id: &str, p: &DecodeParams) -> Result { + if p.range == 0 || p.range > 1 << 23 || p.dim == 0 { + return Err(IsaError(format!("{id}: invalid number_decode parameters"))); + } + let r = p.range as i64; + let m = (2 * r + 1) as usize; + let mut b = Builder::new(id); + b.input("v", Some(1), 2 * p.dim); + b.external("numbers", m, 2 * p.dim); + b.block("readout"); + b.constant("values", m, 1, (-r..=r).map(|n| n as f32).collect()); + b.matvec("numbers", "v", "scores", false); + b.op(Instr::Topk { x: "scores".into(), k: 1, idx: None, val: None, mask: Some("best".into()) }); + b.matmul_ab("best", "values", "value"); + b.matmul_ab("best", "numbers", "clean"); + Ok(b.finish(&DECODE_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/circuits/probe.rs b/crates/zerolm-wsp/src/circuits/probe.rs new file mode 100644 index 0000000..a6386c3 --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/probe.rs @@ -0,0 +1,171 @@ +//! The reasoner's write decisions as circuits: storing a statement and +//! adding a symbol to the codebook. +//! +//! **`relation_probe`** (statement write): inputs `fillers` (K x 2d), +//! `role_sel` (K x 5, one-hot role per filler), `record` (1 x meta_cols, the +//! metadata the statement would get: active 1, polarity, confidence, kind, +//! sequence, provenance, role count), `gate` (1 x 1: 1 when the subject +//! resolved), and `context` (1 x 2d, the discourse state). The statement is +//! the bundle `q = sum_k role_k (*) filler_k`. It duplicates a slot when the +//! slot is active, has the record's polarity and K stored pairs, and +//! `sim(slot, q) >= K - margin` (`sim` = Re / d). Outputs: +//! - `stored` = gate * (1 - duplicate); `row` = stored * q and `meta_row` = +//! stored * record (all zero when nothing is stored); +//! - `dup` and `dup_slot` (the lowest duplicate slot); +//! - `context_out`: the discourse state after the statement, decay * context +//! + subject when `gate` is 1 and the unchanged context otherwise (the +//! subject is the filler in the subject role). +//! +//! **`symbol_cleanup`**: inputs `v` (1 x 2d), `mode` (1 x 3 one-hot: cleanup, +//! same symbol, pronoun), and `gate` (1 x 1). Cosine of `v` with every +//! codebook row (rows are unit phasors, so cos = row . v / (sqrt(d) |v|)); +//! the best row, and `found` when its cosine reaches the mode's threshold. A +//! zero `v` has cosine 0 everywhere. `added` = gate * (1 - found) and `row` = +//! added * v: the codebook row to append when interning a new symbol. +//! +//! Blocks (units of ablation): probe `query` (role selection), `match` +//! (margin, 1/d), `filter` (metadata selectors), `discourse` (decay); +//! cleanup `cosine` (1/sqrt(d), norm epsilon), `threshold` (the three +//! thresholds). + +use crate::circuits::relational::{META_ACTIVE, META_POLARITY, N_ROLES, ROLE_SUBJ}; +use crate::compiler::{Builder, Compiled}; +use serde::{Deserialize, Serialize}; +use zerolm_engine::isa::{Axis, GateFn::*, Instr, IsaError, NormKind, ReduceFn}; + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct ProbeParams { + pub dim: usize, + pub meta_cols: usize, + /// Metadata column holding the number of stored role-filler pairs. + pub n_roles_col: usize, + pub match_margin: f32, + /// Discourse-state decay per statement. + pub discourse_decay: f32, +} + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CleanupParams { + pub dim: usize, + pub cleanup_min: f32, + pub symbol_same: f32, + pub pronoun_min: f32, +} + +pub const PROBE_OUTPUTS: [&str; 6] = ["stored", "row", "meta_row", "dup", "dup_slot", "context_out"]; +pub const CLEANUP_OUTPUTS: [&str; 5] = ["best", "cos", "found", "added", "row"]; +/// `mode` columns of `symbol_cleanup`. +pub const MODE_CLEANUP: usize = 0; +pub const MODE_SAME: usize = 1; +pub const MODE_PRONOUN: usize = 2; + +fn onehot(n: usize, i: usize) -> Vec { + let mut v = vec![0.0; n]; + v[i] = 1.0; + v +} + +fn eq(b: &mut Builder, a: &str, x: &str, out: &str) { + b.gate(Ge, a, x, &format!("{out}_lo")); + b.gate(Ge, x, a, &format!("{out}_hi")); + b.gate(Mul, &format!("{out}_lo"), &format!("{out}_hi"), out); +} + +pub fn compile_probe(id: &str, p: &ProbeParams) -> Result { + if p.dim == 0 || p.meta_cols <= META_POLARITY || p.n_roles_col >= p.meta_cols { + return Err(IsaError(format!("{id}: invalid relation_probe parameters"))); + } + let d2 = 2 * p.dim; + let mut b = Builder::new(id); + b.input_dyn("fillers", None, Some(d2)); + b.input_dyn("role_sel", None, Some(N_ROLES)); + b.input("record", Some(1), p.meta_cols); + b.input("gate", Some(1), 1); + b.input("context", Some(1), d2); + b.external_dyn("slots", None, Some(d2)); + b.external_dyn("meta", None, Some(p.meta_cols)); + b.external("roles", N_ROLES, d2); + + b.block("query"); + b.matmul_ab("role_sel", "roles", "role_rows"); + b.op(Instr::Bind { a: "role_rows".into(), b: "fillers".into(), out: "pairs".into(), conj: false }); + b.reduce(ReduceFn::Sum, "pairs", Axis::Cols, "q"); + b.reduce(ReduceFn::Sum, "role_sel", Axis::All, "k"); + + b.block("match"); + b.scalar("margin", p.match_margin); + b.scalar("inv_d", 1.0 / p.dim as f32); + b.gate(Sub, "k", "margin", "need"); + b.matvec("slots", "q", "dot", false); + b.gate(Mul, "dot", "inv_d", "sim"); + b.gate(Ge, "sim", "need", "close"); + + b.block("filter"); + b.scalar("one", 1.0); + b.scalar("half", 0.5); + b.constant("active_sel", 1, p.meta_cols, onehot(p.meta_cols, META_ACTIVE)); + b.constant("polarity_sel", 1, p.meta_cols, onehot(p.meta_cols, META_POLARITY)); + b.constant("n_roles_sel", 1, p.meta_cols, onehot(p.meta_cols, p.n_roles_col)); + b.matvec("meta", "active_sel", "active", false); + b.gate(Ge, "active", "half", "is_active"); + b.gate(Mul, "close", "is_active", "hits"); + b.gate(Mul, "record", "polarity_sel", "polarity_row"); + b.reduce(ReduceFn::Sum, "polarity_row", Axis::All, "polarity"); + b.matvec("meta", "polarity_sel", "pol", false); + eq(&mut b, "pol", "polarity", "pol_eq"); + b.gate(Mul, "hits", "pol_eq", "hits_pol"); + b.matvec("meta", "n_roles_sel", "n_roles", false); + eq(&mut b, "n_roles", "k", "roles_eq"); + b.gate(Mul, "hits_pol", "roles_eq", "dups"); + b.op(Instr::Topk { x: "dups".into(), k: 1, idx: Some("dup_slot".into()), val: Some("dup".into()), mask: None }); + b.gate(Sub, "one", "dup", "fresh"); + b.gate(Mul, "gate", "fresh", "stored"); + b.gate(Mul, "q", "stored", "row"); + b.gate(Mul, "record", "stored", "meta_row"); + + b.block("discourse"); + b.scalar("decay", p.discourse_decay); + b.constant("subj_sel", N_ROLES, 1, onehot(N_ROLES, ROLE_SUBJ)); + b.matmul_ab("role_sel", "subj_sel", "subj_col"); + b.op(Instr::Transpose { x: "subj_col".into(), out: "subj_row".into() }); + b.matmul_ab("subj_row", "fillers", "subject"); + b.gate(Mul, "context", "decay", "faded"); + b.gate(Add, "faded", "subject", "updated"); + b.gate(Mul, "updated", "gate", "taken"); + b.gate(Sub, "one", "gate", "ungated"); + b.gate(Mul, "context", "ungated", "kept"); + b.gate(Add, "taken", "kept", "context_out"); + Ok(b.finish(&PROBE_OUTPUTS)) +} + +pub fn compile_cleanup(id: &str, p: &CleanupParams) -> Result { + if p.dim == 0 { + return Err(IsaError(format!("{id}: invalid symbol_cleanup parameters"))); + } + let d2 = 2 * p.dim; + let mut b = Builder::new(id); + b.input("v", Some(1), d2); + b.input("mode", Some(1), 3); + b.input("gate", Some(1), 1); + b.external_dyn("codebook", None, Some(d2)); + + b.block("cosine"); + b.scalar("eps", 0.0); + b.scalar("inv_sqrt_d", 1.0 / (p.dim as f32).sqrt()); + b.op(Instr::Norm { kind: NormKind::L2, x: "v".into(), eps: "eps".into(), out: "unit".into() }); + b.matvec("codebook", "unit", "dot", false); + b.gate(Mul, "dot", "inv_sqrt_d", "cosines"); + b.op(Instr::Topk { x: "cosines".into(), k: 1, idx: Some("best".into()), val: Some("cos".into()), mask: None }); + + b.block("threshold"); + b.scalar("one", 1.0); + b.constant("thresholds", 3, 1, vec![p.cleanup_min, p.symbol_same, p.pronoun_min]); + b.matmul_ab("mode", "thresholds", "min"); + b.gate(Ge, "cos", "min", "found"); + b.gate(Sub, "one", "found", "missing"); + b.gate(Mul, "gate", "missing", "added"); + b.gate(Mul, "v", "added", "row"); + Ok(b.finish(&CLEANUP_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/circuits/relational.rs b/crates/zerolm-wsp/src/circuits/relational.rs index 5d0cef4..1f6d53e 100644 --- a/crates/zerolm-wsp/src/circuits/relational.rs +++ b/crates/zerolm-wsp/src/circuits/relational.rs @@ -54,20 +54,20 @@ pub struct Params { pub symbol_same: f32, } -fn check(id: &str, p: &Params) -> Result<(), IsaError> { +pub(crate) fn check(id: &str, p: &Params) -> Result<(), IsaError> { if p.dim == 0 || p.max_depth == 0 || p.meta_cols <= META_POLARITY { return Err(IsaError(format!("{id}: invalid relational parameters"))); } Ok(()) } -fn onehot(n: usize, i: usize) -> Vec { +pub(crate) fn onehot(n: usize, i: usize) -> Vec { let mut v = vec![0.0; n]; v[i] = 1.0; v } -fn support(b: &mut Builder, p: &Params) { +pub(crate) fn support(b: &mut Builder, p: &Params) { b.block("support"); b.scalar("one", 1.0); b.scalar("zero", 0.0); @@ -78,7 +78,7 @@ fn support(b: &mut Builder, p: &Params) { } /// Row `role` of the roles table as a 1 x 2d vector. -fn role(b: &mut Builder, role: usize, out: &str) { +pub(crate) fn role(b: &mut Builder, role: usize, out: &str) { let sel = format!("{out}_sel"); b.constant(&sel, 1, N_ROLES, onehot(N_ROLES, role)); b.matvec("roles", &sel, out, true); @@ -127,7 +127,7 @@ pub fn compile_graph(id: &str, p: &Params) -> Result { } /// Cleanup of a query vector to a one-hot codebook row (cosine >= symbol_same). -fn cleanup_query(b: &mut Builder, v: &str) { +pub(crate) fn cleanup_query(b: &mut Builder, v: &str) { b.op(Instr::Norm { kind: NormKind::L2, x: v.into(), eps: "eps".into(), out: format!("{v}_unit") }); b.matvec("codes_unit", &format!("{v}_unit"), &format!("{v}_cos"), false); b.op(Instr::Topk { x: format!("{v}_cos"), k: 1, idx: None, val: Some(format!("{v}_best")), mask: Some(format!("{v}_mask")) }); diff --git a/crates/zerolm-wsp/src/circuits/value_codec.rs b/crates/zerolm-wsp/src/circuits/value_codec.rs new file mode 100644 index 0000000..5e31fc3 --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/value_codec.rs @@ -0,0 +1,186 @@ +//! Memory value codec and the short-lived associative matrix `W_FAST` as +//! circuits. A value is a row of byte codes: position `p` holds row `b_p` of +//! the byte code table (`core.embed`), NUL-padded to `positions`. Every +//! decision below (which code row, which byte a read is nearest to, which +//! key features win, whether two values are the same) is an ISA operation; +//! the host copies bytes into input rows and binds model tensors. +//! +//! Programs: +//! - `value-encode`: value bytes (1 x positions) to the code row +//! (1 x positions * code_dim): a byte one-hot (compare with 0..code_rows) +//! times the code table. +//! - `fast-recall`: a pooled key (1 x key_dim) to the bytes recalled from +//! `W_FAST`: pattern separation s = kWTA(P k) (the `active` largest entries +//! set to 1/sqrt(active), ties to the lower index), the read M s, and +//! nearest-code decoding. +//! - `fast-write`: a pooled key and value bytes; the same separation, the +//! value encoded in-circuit, then two `delta_write`s into the state `fast`: +//! decay (learning rate 0), then the normalized delta rule on the decayed +//! matrix: M <- d M; M <- M + lr (v - M s) s^T / (s.s + eps). +//! +//! Nearest-code decoding (shared): per position, the code maximizing +//! 2 x.c - |c|^2 (the nearest code); bytes after the first NUL are zero; the +//! margin is the smallest top-1 minus top-2 score over the positions up to +//! and including the first NUL. +//! +//! Blocks (units of ablation): `onehot` (byte indices), `decode` (score +//! scale, margin selector), `separate` (the +//! kWTA amplitude), `plasticity` (learning rate, decay, epsilon), `support` +//! (shared constants 1 and a large penalty). + +use crate::compiler::{Builder, Compiled}; +use serde::{Deserialize, Serialize}; +use zerolm_engine::isa::{Axis, GateFn::*, Instr, IsaError, ReduceFn}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct CodecParams { + /// Value positions (value bytes + 1 for the NUL terminator). + pub positions: usize, + /// Rows and width of the byte code table (`core.embed`). + pub code_rows: usize, + pub code_dim: usize, +} + +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct FastParams { + pub codec: CodecParams, + pub key_dim: usize, + /// Rows of the pattern separator (`core.fast_separator`). + pub expansion: usize, + /// Winners of the kWTA code. + pub active: usize, + pub lr: f32, + pub decay: f32, + pub eps: f32, +} + +pub const ENCODE_OUTPUTS: [&str; 1] = ["row"]; +pub const RECALL_OUTPUTS: [&str; 2] = ["bytes", "margin"]; +pub const WRITE_OUTPUTS: [&str; 1] = ["sparse"]; + +/// Penalty for excluded margins (finite). +const BIG: f32 = 1e30; + +pub(crate) fn check(id: &str, p: &CodecParams) -> Result<(), IsaError> { + if p.positions == 0 || p.code_rows < 2 || p.code_rows > 256 || p.code_dim == 0 { + return Err(IsaError(format!("{id}: invalid value codec parameters"))); + } + Ok(()) +} + +fn check_fast(id: &str, p: &FastParams) -> Result<(), IsaError> { + check(id, &p.codec)?; + if p.key_dim == 0 || p.active == 0 || p.active > p.expansion || !(p.lr.is_finite() && p.decay.is_finite() && p.eps.is_finite()) { + return Err(IsaError(format!("{id}: invalid fast-weight parameters"))); + } + Ok(()) +} + +pub(crate) fn support(b: &mut Builder) { + b.block("support"); + b.scalar("one", 1.0); + b.scalar("big", BIG); +} + +/// `bytes_reg` (1 x positions byte values) to the code row `out` (1 x positions*code_dim). +pub(crate) fn emit_encode(b: &mut Builder, p: &CodecParams, bytes_reg: &str, out: &str) { + b.block("onehot"); + b.constant("byte_index", 1, p.code_rows, (0..p.code_rows).map(|i| i as f32).collect()); + b.op(Instr::Transpose { x: bytes_reg.into(), out: "enc_col".into() }); + b.gate(Ge, "enc_col", "byte_index", "enc_lo"); + b.gate(Ge, "byte_index", "enc_col", "enc_hi"); + b.gate(Mul, "enc_lo", "enc_hi", "enc_onehot"); + b.matmul_ab("enc_onehot", "codes", "enc_rows"); + b.reshape("enc_rows", 1, p.positions * p.code_dim, out); +} + +/// The code row `read` (1 x positions*code_dim) to `bytes` (1 x positions) +/// and `margin` (1 x 1). Needs `one` and `big`. +pub(crate) fn emit_decode(b: &mut Builder, p: &CodecParams, read: &str) { + let pos = p.positions; + b.block("decode"); + b.scalar("score_scale", 2.0); + b.constant("margin_sel", 1, 2, vec![1.0, -1.0]); + b.reshape(read, pos, p.code_dim, "positions"); + b.matmul("positions", "codes", "code_dots"); + b.gate(Mul, "codes", "codes", "code_sq"); + b.reduce(ReduceFn::Sum, "code_sq", Axis::Rows, "code_norm_col"); + b.reshape("code_norm_col", 1, p.code_rows, "code_norm"); + b.gate(Mul, "code_dots", "score_scale", "code_dots2"); + b.gate(Sub, "code_dots2", "code_norm", "code_scores"); + b.op(Instr::Topk { x: "code_scores".into(), k: 1, idx: Some("byte_col".into()), val: None, mask: None }); + b.op(Instr::Topk { x: "code_scores".into(), k: 2, idx: None, val: Some("code_top2".into()), mask: None }); + b.matmul("code_top2", "margin_sel", "pos_margin_col"); + b.gate(Ge, "byte_col", "one", "nonnull_col"); + b.reshape("nonnull_col", 1, pos, "nonnull"); + b.op(Instr::Scan { x: "nonnull".into(), out: "keep".into(), exclusive: false }); + b.op(Instr::Scan { x: "nonnull".into(), out: "visited".into(), exclusive: true }); + b.reshape("byte_col", 1, pos, "bytes_all"); + b.gate(Mul, "bytes_all", "keep", "bytes"); + b.reshape("pos_margin_col", 1, pos, "pos_margin"); + b.gate(Sub, "one", "visited", "unvisited"); + b.gate(Mul, "unvisited", "big", "margin_penalty"); + b.gate(Add, "pos_margin", "margin_penalty", "margin_masked"); + b.reduce(ReduceFn::Min, "margin_masked", Axis::All, "margin"); +} + +/// The kWTA code `sparse` (1 x expansion) of the input `key`. +fn emit_separate(b: &mut Builder, p: &FastParams) { + b.block("separate"); + b.scalar("amplitude", 1.0 / (p.active as f32).sqrt()); + b.matvec("separator", "key", "features", false); + b.op(Instr::Topk { x: "features".into(), k: p.active, idx: None, val: None, mask: Some("winners".into()) }); + b.gate(Mul, "winners", "amplitude", "sparse"); +} + +pub(crate) fn codec_io(b: &mut Builder, p: &CodecParams) { + b.external("codes", p.code_rows, p.code_dim); +} + +pub fn compile_encode(id: &str, p: &CodecParams) -> Result { + check(id, p)?; + let mut b = Builder::new(id); + b.input("value", Some(1), p.positions); + codec_io(&mut b, p); + emit_encode(&mut b, p, "value", "row"); + Ok(b.finish(&ENCODE_OUTPUTS)) +} + +pub fn compile_fast_recall(id: &str, p: &FastParams) -> Result { + check_fast(id, p)?; + let c = &p.codec; + let mut b = Builder::new(id); + b.input("key", Some(1), p.key_dim); + b.external("separator", p.expansion, p.key_dim); + b.external("fast", c.positions * c.code_dim, p.expansion); + codec_io(&mut b, c); + support(&mut b); + emit_separate(&mut b, p); + b.matvec("fast", "sparse", "read", false); + emit_decode(&mut b, c, "read"); + Ok(b.finish(&RECALL_OUTPUTS)) +} + +pub fn compile_fast_write(id: &str, p: &FastParams) -> Result { + check_fast(id, p)?; + let c = &p.codec; + let mut b = Builder::new(id); + b.input("key", Some(1), p.key_dim); + b.input("value", Some(1), c.positions); + b.external("separator", p.expansion, p.key_dim); + codec_io(&mut b, c); + b.state("fast", c.positions * c.code_dim, p.expansion); + emit_separate(&mut b, p); + emit_encode(&mut b, c, "value", "target"); + b.block("plasticity"); + b.scalar("lr", p.lr); + b.scalar("decay", p.decay); + b.scalar("eps", p.eps); + b.scalar("no_lr", 0.0); + b.scalar("no_decay", 1.0); + b.op(Instr::DeltaWrite { m: "fast".into(), k: "sparse".into(), v: "target".into(), lr: "no_lr".into(), decay: "decay".into(), eps: "eps".into() }); + b.op(Instr::DeltaWrite { m: "fast".into(), k: "sparse".into(), v: "target".into(), lr: "lr".into(), decay: "no_decay".into(), eps: "eps".into() }); + Ok(b.finish(&WRITE_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/compiler.rs b/crates/zerolm-wsp/src/compiler.rs index ebe78a4..6934693 100644 --- a/crates/zerolm-wsp/src/compiler.rs +++ b/crates/zerolm-wsp/src/compiler.rs @@ -33,6 +33,22 @@ pub enum Circuit { AtomEncode(circuits::atom_encode::Params), AttentionRead(circuits::attention_read::Params), KeyEncode(circuits::key_encode::Params), + NumberEncode(circuits::number::EncodeParams), + NumberDecode(circuits::number::DecodeParams), + ValueEncode(circuits::value_codec::CodecParams), + FastRecall(circuits::value_codec::FastParams), + FastWrite(circuits::value_codec::FastParams), + RelationProbe(circuits::probe::ProbeParams), + SymbolCleanup(circuits::probe::CleanupParams), + RelationJoin(circuits::relational::Params), + RelationLevels(circuits::relational::Params), + RelationRules(circuits::relational::Params), + RelationCounts(circuits::decide::CountParams), + RelationVerdict(circuits::relational::Params), + MemoryWrite(circuits::memory_write::WriteParams), + MemoryForget(circuits::memory_write::WriteParams), + MemoryCompact(circuits::memory_write::WriteParams), + MemoryHistory(circuits::memory_write::WriteParams), RelationGraph(circuits::relational::Params), RelationReach(circuits::relational::Params), } @@ -119,6 +135,22 @@ pub fn compile(spec: &Spec) -> Result { Circuit::AtomEncode(p) => circuits::atom_encode::compile(&spec.id, p)?, Circuit::AttentionRead(p) => circuits::attention_read::compile(&spec.id, p)?, Circuit::KeyEncode(p) => circuits::key_encode::compile(&spec.id, p)?, + Circuit::NumberEncode(p) => circuits::number::compile_encode(&spec.id, p)?, + Circuit::NumberDecode(p) => circuits::number::compile_decode(&spec.id, p)?, + Circuit::ValueEncode(p) => circuits::value_codec::compile_encode(&spec.id, p)?, + Circuit::FastRecall(p) => circuits::value_codec::compile_fast_recall(&spec.id, p)?, + Circuit::FastWrite(p) => circuits::value_codec::compile_fast_write(&spec.id, p)?, + Circuit::RelationProbe(p) => circuits::probe::compile_probe(&spec.id, p)?, + Circuit::SymbolCleanup(p) => circuits::probe::compile_cleanup(&spec.id, p)?, + Circuit::RelationJoin(p) => circuits::decide::compile_join(&spec.id, p)?, + Circuit::RelationLevels(p) => circuits::decide::compile_levels(&spec.id, p)?, + Circuit::RelationRules(p) => circuits::decide::compile_rules(&spec.id, p)?, + Circuit::RelationCounts(p) => circuits::decide::compile_counts(&spec.id, p)?, + Circuit::RelationVerdict(p) => circuits::decide::compile_verdict(&spec.id, p)?, + Circuit::MemoryWrite(p) => circuits::memory_write::compile_write(&spec.id, p)?, + Circuit::MemoryForget(p) => circuits::memory_write::compile_forget(&spec.id, p)?, + Circuit::MemoryCompact(p) => circuits::memory_write::compile_compact(&spec.id, p)?, + Circuit::MemoryHistory(p) => circuits::memory_write::compile_history(&spec.id, p)?, Circuit::RelationGraph(p) => circuits::relational::compile_graph(&spec.id, p)?, Circuit::RelationReach(p) => circuits::relational::compile_reach(&spec.id, p)?, }; @@ -139,6 +171,7 @@ pub struct Builder { body: Vec, tensors: Vec, external: Vec, + state: Vec, block: String, } @@ -151,6 +184,7 @@ impl Builder { body: Vec::new(), tensors: Vec::new(), external: Vec::new(), + state: Vec::new(), block: "main".into(), } } @@ -181,6 +215,12 @@ impl Builder { self.external.push(n.into()); } + /// A mutable model tensor (written only by `delta_write`), bound and + /// returned by the caller. + pub fn state(&mut self, n: &str, rows: usize, cols: usize) { + self.state.push(Decl::new(n, Some(rows), cols)); + } + /// A compiled `W_PROG` constant in the current block. pub fn constant(&mut self, n: &str, rows: usize, cols: usize, data: Vec) { assert_eq!(rows * cols, data.len(), "constant {n}"); @@ -232,7 +272,7 @@ impl Builder { name: self.name, inputs: self.inputs, weights: self.weights, - state: Vec::new(), + state: self.state, body: self.body, outputs: outputs.iter().map(|s| s.to_string()).collect(), }, diff --git a/crates/zerolm-wsp/src/lib.rs b/crates/zerolm-wsp/src/lib.rs index b7ebf9a..067cced 100644 --- a/crates/zerolm-wsp/src/lib.rs +++ b/crates/zerolm-wsp/src/lib.rs @@ -12,12 +12,28 @@ pub mod patch; pub use compiler::{compile, parse_spec, Circuit, Compiled, ProgTensor, Spec}; /// Pinned circuit specifications shipped with the model, as (file name, JSON). -pub const SPECS: [(&str, &str); 5] = [ +pub const SPECS: [(&str, &str); 21] = [ ("memory-read.json", include_str!("../../../model/prog/memory-read.json")), ("memory-encode.json", include_str!("../../../model/prog/memory-encode.json")), ("relation-graph.json", include_str!("../../../model/prog/relation-graph.json")), ("atom-encode.json", include_str!("../../../model/prog/atom-encode.json")), ("relation-reach.json", include_str!("../../../model/prog/relation-reach.json")), + ("number-encode.json", include_str!("../../../model/prog/number-encode.json")), + ("number-decode.json", include_str!("../../../model/prog/number-decode.json")), + ("value-encode.json", include_str!("../../../model/prog/value-encode.json")), + ("fast-recall.json", include_str!("../../../model/prog/fast-recall.json")), + ("fast-write.json", include_str!("../../../model/prog/fast-write.json")), + ("relation-probe.json", include_str!("../../../model/prog/relation-probe.json")), + ("symbol-cleanup.json", include_str!("../../../model/prog/symbol-cleanup.json")), + ("relation-join.json", include_str!("../../../model/prog/relation-join.json")), + ("relation-levels.json", include_str!("../../../model/prog/relation-levels.json")), + ("relation-rules.json", include_str!("../../../model/prog/relation-rules.json")), + ("relation-counts.json", include_str!("../../../model/prog/relation-counts.json")), + ("relation-verdict.json", include_str!("../../../model/prog/relation-verdict.json")), + ("memory-write.json", include_str!("../../../model/prog/memory-write.json")), + ("memory-forget.json", include_str!("../../../model/prog/memory-forget.json")), + ("memory-compact.json", include_str!("../../../model/prog/memory-compact.json")), + ("memory-history.json", include_str!("../../../model/prog/memory-history.json")), ]; /// `model/prog/pinned.json`: circuit id -> SHA-256 of the compiled output. diff --git a/docs/claims-and-limitations.md b/docs/claims-and-limitations.md index 4f118d4..e4db530 100644 --- a/docs/claims-and-limitations.md +++ b/docs/claims-and-limitations.md @@ -10,7 +10,7 @@ - Memory: facts written through the live API persist across process restarts only as tensors in the model checkpoint, and are answered by an attention read over those tensors. On 3 seeds x 1,200 generated facts (with contradictions, supersessions, and deletions), after a restart: exact-key recall and precision 1.000; non-trivial paraphrase recall 0.973 and precision 0.993; 0.6% of negative queries answered. Memory writes leave every core tensor byte-identical; corrupt or interrupted checkpoints fall back to the last valid one; zeroing or permuting memory tensors drops recall to 0. Gates were frozen from pilot seeds. See `docs/memory-format.md`. - Reasoning: statements of a bounded language are stored as bound role-filler phasor vectors in a tensor memory, and new questions are answered by eight controlled operators (class inclusion, transitivity, inheritance with exceptions, comparison, arithmetic by number binding, temporal order, negation, conditionals) built from vector probes, unbinding, and cleanup. On 3 seeds plus a hidden seed (about 525 questions each, 20 categories), answers agree with a symbolic reference on every question; a verbatim-lookup baseline gets 0% of the derived questions. Removing one proof premise changes answers exactly as the reference says (40/40 per seed), the reported premises alone reproduce each answer, and disabling any operator or the binding, discourse-state, or retrieval pathway removes exactly the inferences that depend on it. See `docs/reasoning.md`. -- Uniform engine (in progress, not complete): a neural ISA (instruction set architecture: the project's fixed list of tensor operations, see `docs/neural-isa.md`: matvec, gate, norm, bind, top-k, hash_rows, delta_write, step) runs programs compiled by `zerolm-wsp` (WSP = weight-space programming) from typed circuit specs in `model/prog/` (output hashes pinned; patches apply and revert to the original hash). Memory now runs only as compiled programs: `memory-encode` turns raw key and query bytes into the key and word vectors (bit-identical to the former host encoder, by unit test), and `memory-read` answers. The frozen memory gate passes unchanged through both on 3 seeds, an independent reference interpreter reproduces all 8,700 gate queries per seed bit for bit from the two exported bundles, and zeroing a circuit block removes exactly what it carries (read verification: exact answers kept, every negative accepted; attention, selection, cleanup, or the encoder's byte classes, windows, or segmentation: recall 0; the encoder's case folding: upper-cased queries recall 0, while lower-case recall stays about 0.83 because interior trigrams need no folding). Reasoning's graph search also runs as compiled circuits (C5 decodes the relation memory, C6 computes reachability and shortest proofs), and the uniform-engine gate replays every circuit run behind a generated world's questions on the reference interpreter, including runs with operators ablated: 970 of 970 bit-exact. Symbol, role, and relation vectors are now computed by a fifth compiled circuit (`atom-encode`: salted hashes of the name bytes into a seeded table, normalized to unit phasors and bound), no longer by a host random generator; number encoding, the bounded-language parser, the operators' decision logic, and memory write bookkeeping are still host code (listed as pending by the gate). See `docs/neural-isa.md`. +- Uniform engine (in progress, not complete): a neural ISA (instruction set architecture: the project's fixed list of tensor operations, see `docs/neural-isa.md`: matvec, gate, norm, bind, top-k, hash_rows, delta_write, step) runs programs compiled by `zerolm-wsp` (WSP = weight-space programming) from typed circuit specs in `model/prog/` (output hashes pinned; patches apply and revert to the original hash). Memory now runs only as compiled programs: `memory-encode` turns raw key and query bytes into the key and word vectors (bit-identical to the former host encoder, by unit test), and `memory-read` answers. The frozen memory gate passes unchanged through both on 3 seeds, an independent reference interpreter reproduces all 8,700 gate queries per seed bit for bit from the two exported bundles, and zeroing a circuit block removes exactly what it carries (read verification: exact answers kept, every negative accepted; attention, selection, cleanup, or the encoder's byte classes, windows, or segmentation: recall 0; the encoder's case folding: upper-cased queries recall 0, while lower-case recall stays about 0.83 because interior trigrams need no folding). Reasoning's graph search also runs as compiled circuits (C5 decodes the relation memory, C6 computes reachability and shortest proofs), and the uniform-engine gate replays every circuit run behind a generated world's questions on the reference interpreter, including runs with operators ablated: 3,520 of 3,520 bit-exact (also the encoder runs made while the world is taught). The reasoner's one-step probes, symbol cleanup, pronoun resolution, and the bundling of a new statement run in two more circuits (`relation-probe`, `symbol-cleanup`). Symbol, role, and relation vectors are now computed by a fifth compiled circuit (`atom-encode`: salted hashes of the name bytes into a seeded table, normalized to unit phasors and bound), no longer by a host random generator. Memory values are encoded and written to and recalled from fast weights by compiled circuits (`value-encode`, `fast-write`, `fast-recall`), so the host value decoder and fast-weight code are gone. Numbers are encoded and decoded by two more compiled circuits (`number-encode`: the bytes of a decimal numeral to a product of seeded digit phasors; `number-decode`: cleanup by a codebook `matvec` and top-1), no longer by host trigonometry and a host scan. The eight operators' decisions are compiled circuits too (negation joins, inheritance levels, rule chaining, counts by phasor arithmetic, and the verdict), and the reasoning gate still agrees with the reference on every question. The reasoner's writes are decided by circuits as well: `relation-probe` decides whether a statement is stored (subject resolved, not a duplicate) and computes its slot and metadata rows and the next discourse state, and `symbol-cleanup` decides whether a name is new and gives its codebook row; the host only appends those rows at its counters. The memory store's writes are decided by four more circuits (`memory-write` for teach, correct, and confirm; `memory-forget`, which follows supersession links; `memory-compact`, which orders and remaps slots; `memory-history`), and the host only stamps, checks capacity, and moves the rows they specify. The bounded-language parser is still host code (listed as pending by the gate). See `docs/neural-isa.md`. ## What is not claimed - The online learner is weak: a byte trigram baseline beats it by about 1 BPB. Only the readout learns (on frozen random GRU features), on a tiny generated language of about 50 words. It shows online acquisition, not competitive language modeling and not English understanding. diff --git a/docs/memory-format.md b/docs/memory-format.md index 060e843..3da17fe 100644 --- a/docs/memory-format.md +++ b/docs/memory-format.md @@ -101,7 +101,7 @@ A value of bytes $v_1..v_m$ ($m \le 47$) is stored as 48 code rows, $V = [E[v_1] $$\hat b = \arg\min_b \lVert x - E_b\rVert^2 = \arg\max_b \big(2\langle x, E_b\rangle - \lVert E_b\rVert^2\big)$$ -The two forms are equal because $\lVert x\rVert^2$ does not depend on $b$; the circuit uses the second, a matmul plus a bias. Decoding stops at the first $\hat b = 0$ (NUL). The **margin** is the smallest gap, over decoded positions, between the best and second-best score: how far the read is from flipping a byte. +The two forms are equal because $\lVert x\rVert^2$ does not depend on $b$; the circuit uses the second, a matmul plus a bias. Decoding stops at the first $\hat b = 0$ (NUL). The **margin** is the smallest gap, over decoded positions, between the best and second-best score: how far the read is from flipping a byte. Values are encoded in-circuit (byte one-hot times $E$), and at write time the `memory-write` circuit decodes the same-key slot's row this way and compares it byte by byte with the new value, which tells an unchanged fact from a contradiction. ### Accept rule @@ -109,9 +109,9 @@ $$\text{answer} \iff s_{\text{best}} \ge 0.3 \;\wedge\; \text{cov} \ge 0.5 \;\we In the circuit this is the product of four $\{0, 1\}$ gates, multiplied into the decoded bytes, so a rejected read outputs an empty value. -**Writes** address an existing slot when $s_{\text{best}} \ge 0.95$ (same fact key). That uses the circuit's `best` and `score` outputs, queried with the write key as its only word. +**Writes** address an existing slot when $s_{\text{best}} = \max_{s\ \text{active}} k \cdot K_s \ge 0.95$ (same fact key). Every write decision (teach, correct, confirm, forget, compaction) is made by the compiled write circuits (`memory-write`, `memory-forget`, `memory-compact`; see `docs/neural-isa.md`); the host stamps sequence and provenance, refuses writes past capacity, and moves the rows the circuits specify. -### Fast weights: sparse keys and the delta rule (`fast_weights.rs`) +### Fast weights: sparse keys and the delta rule (`fast-write`, `fast-recall`) The pooled key $k$ is expanded and sparsified by a frozen projection $P \in \mathbb{R}^{1024 \times 128}$, $P_{ij} \sim \mathcal{U}(-1,1)$: @@ -123,6 +123,8 @@ $$M \leftarrow \gamma M, \qquad M \leftarrow M + \eta\, \frac{(v - M\sigma)\,\si Since $\sigma^\top\sigma = 1$, right after the write $M\sigma = v$ up to $\epsilon$: the newest association is stored exactly. An older association $j$ is read back as $M\sigma_j$. Its own contribution has decayed by $\gamma$ per later write, and every later write $t$ corrupted it in proportion to the code overlap $\langle\sigma_t, \sigma_j\rangle$. That is why recall falls with load (measured below). +Both run as compiled circuits (`model/prog/fast-write.json`, `model/prog/fast-recall.json`, template `zerolm_wsp::circuits::value_codec`): the separation is a `matvec` with $P$, `topk` with $k = a$ (ties to the lower index), and a constant amplitude $1/\sqrt a$; the value is encoded in-circuit by a byte one-hot times $E$; the write is two `delta_write` instructions on the state `memory.fast`, the first with learning rate 0 and decay $\gamma$ (only the decay), the second with $\eta$, $\epsilon$ and decay 1, so the error is computed on the decayed matrix exactly as above. Recall reads $M\sigma(k)$ and decodes it with the same nearest-code cleanup as the semantic read. + ### Integers in `f32` tensors An `f32` has a 24-bit significand, so every integer $0 \le x < 2^{24}$ is exact. Clocks are stored as $x = \text{lo} + 2^{24}\, \text{hi}$, two exact pieces covering 48 bits. The 64-bit provenance hash is split into 22-, 22-, and 20-bit pieces. No metadata value is ever rounded. diff --git a/docs/neural-isa.md b/docs/neural-isa.md index c127764..a60c44e 100644 --- a/docs/neural-isa.md +++ b/docs/neural-isa.md @@ -70,14 +70,14 @@ The header is `{"program": , "tensors": [{"name", "rows", "cols"}, ...] ## The uniform-engine gate -`cargo run --release -p zerolm-eval --bin zerolm-uniform` (about 2 minutes; report schema `research/schemas/uniform-engine-report.schema.json`): +`cargo run --release -p zerolm-eval --bin zerolm-uniform` (about 4 minutes; report schema `research/schemas/uniform-engine-report.schema.json`): | Check | What it shows | |---|---| | circuits pinned and reproducible | every spec in `model/prog/` compiles twice to the same SHA-256, equal to `model/prog/pinned.json` | | patch round trip | for every circuit: a one-value patch applies, a second apply is refused, and revert restores the original hash | -| memory reference parity | 400 memory queries (taught keys and unknown keys) reproduced bit for bit from the exported bundle | -| reasoning reference parity | every circuit run behind a generated world's questions, intact and with each operator ablated, replayed on the reference interpreter (970 runs, all bit-exact) | +| memory reference parity | 400 memory queries (taught keys and unknown keys) through `memory-encode` and `memory-read`, plus a short session of new, repeated, contradicting, corrected, confirmed, and forgotten facts, history, compaction, and fast associations through the write circuits, `fast-write` (including its final `memory.fast` state), and `fast-recall`: every run reproduced bit for bit from the exported bundles | +| reasoning reference parity | every circuit run while a generated world is taught and its questions answered, intact and with each operator ablated, replayed on the reference interpreter (3,520 runs, all bit-exact; every decision circuit included) | | host audit | the model-path files listed in `model/prog/host-audit.json` contain no text processing; host code that still makes content decisions is listed as **pending** | The gate passes when every check holds. The milestone counts as complete only when the pending list is also empty, which means all model behavior is in weights. @@ -93,6 +93,67 @@ The gate passes when every check holds. The milestone counts as complete only wh Two different names collide only if all three hashes collide (probability about $1024^{-3}$), and the phases of unrelated names are independent, so their cosine is about 0 with standard deviation $1/\sqrt{2d}$. The host copies bytes and memoizes the circuit's output by the same key; it inspects no byte. +## The value codec and W_FAST circuits + +`model/prog/value-encode.json`, `fast-recall.json`, `fast-write.json`, compiled by `zerolm_wsp::circuits::value_codec`; they replace the host value encoder, the host nearest-code decoder, and the host fast-weight code (`retrieval.rs` and `fast_weights.rs` are deleted). Bound tensors: the byte code table $E$ (`core.embed`, 256 x 16), the separator $P$ (`core.fast_separator`), and `memory.fast` (a weight for recall, the `delta_write` state for writes). + +- `value-encode`: value bytes (NUL-padded to 48) compared with the indices 0..255 (`ge` both ways) give a one-hot $O$; the row is $OE$, reshaped to 1 x 768. +- `fast-recall`: $\sigma(k) = \mathrm{topk}_{32}(Pk) / \sqrt{32}$, the read $M\sigma$, and the same decoding. +- `fast-write`: the same $\sigma$, the value encoded as in `value-encode`, then `delta_write` with learning rate 0 and decay 0.99 (decay only), and `delta_write` with learning rate 1, epsilon $10^{-6}$ and decay 1 (the delta rule on the decayed matrix). + +`value-encode` remains a standalone pinned program (the write circuits encode values in-circuit with the same block). The nearest-code decoding and the byte comparison that tell an unchanged fact from a contradiction are part of `memory-write` (below). + +## The memory write circuits + +`model/prog/memory-{write,forget,compact,history}.json`, compiled by `zerolm_wsp::circuits::memory_write`. They decide every change to the semantic and episodic memory; the host stamps a write with what it owns (sequence number, source, provenance, key word count), refuses a write past capacity, and moves the rows the outputs specify. + +- `memory-write` (teach, correct, confirm; a one-hot `op`): the same-key slot is the best active key with $k \cdot K_s \ge 0.95$; its value row is decoded (nearest code) and compared byte by byte with the new value; the free slot is the lowest empty one (`topk`, ties to the lowest index). The outcome is one-hot: teach gives unchanged, conflict, or stored; correct gives unchanged, superseded, or stored; confirm gives confirmed or unknown. Outputs: the new slot's key, key-word, value (encoded in-circuit), and metadata rows (zero unless a slot is written; a superseding record links to the old slot), the old slot's metadata after the operation (contradictions + 1, or status superseded with a forward link, or confidence confirmed, and the updated time), and the episode row (op code and slot). +- `memory-forget`: the same-key slot, then one supersession link per step for as many steps as there are slots (any chain is followed; visited slots are not revisited), the valid episodic records that refer to the chain (their keys are erased), and the metadata with the chain marked deleted. +- `memory-compact`: active slots first in index order (`topk` with $k = S$), a permutation matrix $P$ (row $j$ holds old slot $\mathrm{order}_j$), the map old $\to$ new ($-1$ for reclaimed slots), and every supersession link and episodic slot reference remapped through it; rows past the active ones become empty records. +- `memory-history`: valid episodes whose key has $k \cdot e \ge 0.95$ with the query key. + +## The reasoner write circuits + +`model/prog/relation-probe.json` and `model/prog/symbol-cleanup.json`, compiled by `zerolm_wsp::circuits::probe`. They decide every reasoner write; the host only appends their output rows at its slot and codebook counters (and refuses a write past capacity). + +- `relation-probe` (a statement): inputs `fillers` ($K \times 2d$), one-hot `role_sel` ($K \times 5$), the metadata `record` the statement would get, `gate` (1 when the subject resolved), and the discourse state. Roles are selected from `core.reasoner.roles`, bound to the fillers, and summed into $q = \sum_k \rho_{r_k} \circledast f_k$. An active slot with the record's polarity, $K$ stored pairs, and $\mathrm{sim}(\text{slot}, q) \ge K - 0.5$ is a duplicate. `stored` $=$ gate $\cdot$ (1 $-$ duplicate); the outputs `row` $= \text{stored} \cdot q$ and `meta_row` $= \text{stored} \cdot \text{record}$ are what the host appends; `context_out` is $0.5\,c + s$ (s the subject filler) when gate is 1, else $c$. With no slots yet, the host binds one all-zero inactive row, so no tensor is empty. +- `symbol-cleanup` (a name or "it"): input $v$, a one-hot `mode`, and `gate`. Cosines $c_i = C_i \cdot \hat v / \sqrt d$ with every codebook row, the best row, and `found` when $c \ge$ the mode's threshold: 0.9 (the same symbol) or 0.4 (resolving "it" from the discourse state; a zero state never resolves). `added` $=$ gate $\cdot$ (1 $-$ found) and `row` $=$ added $\cdot v$: interning appends that row. An empty codebook is bound as one zero row. + +The binding ablation binds identity role phasors; the discourse-state ablation keeps the state at zero. + +## The decision circuits + +Compiled by `zerolm_wsp::circuits::decide` (`model/prog/relation-{join,levels,rules,counts,verdict}.json`). They replace the operators' Rust control flow: each is a fixed number of steps over the C5 outputs (`subj_sim`, `obj_t`, `pos`, `neg`), with $E_r[s, i] = [\mathrm{subj\_sim}[s,i] + \mathrm{sim}(\text{slot}_s, r \circledast \rho_{\text{rel}}) \ge 1.5] \cdot \mathrm{pol}[s]$ the r-edges leaving symbol i. Proofs are slot masks or premise matrices (row j = slots on the proof of j); among candidates the shortest proof wins (score $10^6 - 1 - \text{length}$, ties to the lowest slot). + +| circuit | operator | computation | +|---|---|---| +| `relation-join` | negation | the shortest negative r-edge from $\{x\} \cup$ anc(x) to $\{y\} \cup$ anc(y) (ancestors and their premise matrices from two C6 runs; with class inclusion disabled, only x and y) | +| `relation-levels` | attribute inheritance | breadth-first levels over positive isa edges; at the first level whose frontier has a positive or negative can-edge to v, both are latched ($\mathrm{done} \leftarrow \max(\mathrm{done}, \mathrm{found})$) | +| `relation-rules` | conditionals | backward levels from property p over in-scope rules (scope "something" or x, cosine $\ge 0.3$), latched at the first level where x has a frontier property directly | +| `relation-counts` | arithmetic | breadth-first over "more" edges carrying $\mathrm{num}(\sum \delta)$ by binding cleaned differences; each reached node's count is cleaned up, bound to its carried sum, and decoded; one distinct value is the answer, several are a contradiction | +| `relation-verdict` | combination | $(p, n) \mapsto$ yes $p\bar n$, no $\bar p n$, contradiction $pn$, unknown $\bar p \bar n$, and the union of the supporting proofs | + +Each operator's enable constant is its own block, so zeroing it is the operator ablation. The host chooses which circuits a question's parsed form runs and labels the result from proof lengths and levels; it makes no content decision. + +## The number circuits + +`model/prog/number-encode.json` and `model/prog/number-decode.json`, compiled by `zerolm_wsp::circuits::number`. They replace the host's fractional-power number encoder and its cleanup scan. Numbers are $\mathrm{num}(n)_k = e^{i n \phi_k}$ with a seeded base $\phi$, and two seeded tables are initialized from it (bound model tensors, verified on load): the digit table $D$ (`core.reasoner.number_digits`, 70 rows, row $10q + v = \mathrm{num}(v\,10^q)$) and the codebook $C$ (`core.reasoner.number_codebook`, 2,001 rows, row $j = \mathrm{num}(j - 1000)$). + +**`number-encode`.** Input: a row of $L = 8$ byte values holding the decimal numeral (optional leading `-`) at positions $0..\ell$. Because $\mathrm{num}$ is a homomorphism from addition to binding, $\mathrm{num}(\sum_q v_q 10^q) = \circledast_q\, \mathrm{num}(v_q 10^q)$: + +1. `classify`: compare each byte with the codes 48..57 (`ge` both ways, `mul`): a digit one-hot $O \in \{0,1\}^{L \times 10}$ and the digit indicator $s = O\mathbf{1}$. +2. `position`: the place of byte $j$ is the number of digits after it, $p = s^\top U$ with $U_{ij} = [i > j]$; compared with the places $0..6$ it gives a one-hot $P \in \{0,1\}^{L \times 7}$. +3. `select`: $S = (P\,\mathrm{Spread}) \odot (O\,\mathrm{Tile})$ has a single 1 at column $10q + v$ in each digit row, so $SD$ holds $\mathrm{num}(v_j 10^{q_j})$ per digit byte. +4. `fill`: non-digit bytes (padding, the sign) get the identity phasor $\mathrm{num}(0)$: add $(1 - s)\,e$ with $e = [1, \dots, 1, 0, \dots, 0]$. +5. `product`: bind the $L$ rows. +6. `sign`: if byte 0 equals 45, negate the imaginary half (conjugation, $\mathrm{num}(-n) = \overline{\mathrm{num}(n)}$). + +A digit beyond the seventh place selects no row, which zeroes the output; the parser only produces integers up to $10^6$. The output equals the host formula up to `f32` rounding of the products (similarity $1 \pm 10^{-4}$, unit test). + +**`number-decode`.** Input: a vector $v$ (1 x $2d$). `matvec` scores $Cv$, `topk` keeps the best row (ties to the lower index), and the mask reads out the integer (a constant column $-1000..1000$, exact in `f32`) and the clean row of $C$. The arithmetic operator binds clean rows and decodes again, so a sum is exactly `decode(num(a) * num(b))`. + +Ablation (unit test): zeroing `sign` zeroes every output; zeroing `position` makes each digit select all of its places (cosine with the true vector about 1/7); zeroing `readout` reads every number as 0. + ## Why the zero-skip is exact The engine's transposed `matvec` computes $o_c = \sum_r w_{rc}\, x_r$ left to right from $o_c = +0$, but it skips every $r$ with $x_r = 0$. For finite $w_{rc}$, a skipped term is $w_{rc} \cdot 0 = \pm 0$. In IEEE-754 round-to-nearest arithmetic, $a + (\pm 0) = a$ for every $a \ne 0$, and $(+0) + (-0) = +0$. The sum $u + v$ is $-0$ only when both $u$ and $v$ are $-0$, so an accumulator that starts at $+0$ can never become $-0$. Skipping zero terms therefore leaves every partial sum bit-identical. The reference interpreter does not skip, and the parity tests confirm the equality on every gate query. diff --git a/docs/reasoning.md b/docs/reasoning.md index 56acd22..2883266 100644 --- a/docs/reasoning.md +++ b/docs/reasoning.md @@ -21,7 +21,7 @@ Every symbol, role, and relation is a 512-dimensional vector of complex unit pha - **Unbind:** product with the conjugate. - **Bundle:** sum. -Numbers use fractional-power encoding, `number(n)_k = exp(i n θ_k)`, so `bind(number(a), number(b)) = number(a+b)`. The arithmetic operator adds by binding number vectors and then cleans the result up to an integer. +Numbers use fractional-power encoding, `number(n)_k = exp(i n θ_k)`, so `bind(number(a), number(b)) = number(a+b)`. The arithmetic operator adds by binding number vectors and then cleans the result up to an integer. Both directions run as compiled circuits (`docs/neural-isa.md`): `number-encode` turns the bytes of a decimal numeral into `number(n)` as a product of seeded digit phasors, and `number-decode` cleans a vector up against a seeded codebook. ### Language (`crates/zerolm-engine/src/semantic_state.rs`) @@ -44,13 +44,13 @@ The discourse state updates as `c ← 0.5 c + subject` after each statement. "it | `memory.relations.slots` | `relation_memory` | 4096 x 1024: bound bundle per fact/rule, e.g. `subj(*)rex + rel(*)isa + obj(*)dog` | | `memory.relations.meta` | `relation_memory` | polarity (+1 / -1), confidence, kind, logical time, provenance hash | | `memory.relations.codebook` | `relation_memory` | symbol vectors seen so far (no names are stored) | -| `core.reasoner.*` | `binding_roles` | frozen role / relation phasors and the number base, verified on load | +| `core.reasoner.*` | `binding_roles` | frozen atom hash table, role / relation phasors, and the number digit table and cleanup codebook, verified on load | The checkpoint uses the memory component's atomic format (`docs/memory-format.md`). Questions and derived answers are never written to memory. ### Operators (`crates/zerolm-reasoner/src/operators.rs`) -A **probe** with k role-filler pairs matches a slot when `Re/d >= k - 0.5`. **Unbinding** a role from a matched slot gives a noisy filler, which is cleaned up to the nearest codebook entry (cosine at least 0.3). Every operator below is programmed control flow over those steps. The depth limit is 8. +A **probe** with k role-filler pairs matches a slot when `Re/d >= k - 0.5`. **Unbinding** a role from a matched slot gives a noisy filler, which is cleaned up to the nearest codebook entry (cosine at least 0.3). The probe and the cleanup run as compiled circuits (`relation-probe`, `symbol-cleanup`). Every operator below is a fixed-step compiled circuit over the decoded relation graph (C5): C6 `relation-reach` for chains, `relation-join` for negation, `relation-levels` for inheritance, `relation-rules` for conditionals, `relation-counts` for arithmetic, and `relation-verdict` for how positive and negative findings combine (`docs/neural-isa.md`). The operators are programmed (compiled), not learned. The depth limit is 8. | operator | semantics | |---|---| @@ -116,7 +116,7 @@ With a seeded base $\phi_k \sim \mathcal{U}(-\pi, \pi)$: $$\mathrm{num}(n)_k = e^{i n \phi_k}, \qquad \mathrm{num}(a) \circledast \mathrm{num}(b) = \mathrm{num}(a + b), \qquad \mathrm{num}(a) \oslash \mathrm{num}(b) = \mathrm{num}(a - b)$$ -So addition **is** binding. For integers $a \ne b$, $\mathbb{E}\cos((a-b)\phi_k) = 0$ over the uniform base, so distinct integers are quasi-orthogonal (sd 0.031). Decoding picks $\hat n = \arg\max_{|n| \le 1000} \mathrm{sim}(v, \mathrm{num}(n))$. The scan never calls trigonometric functions per candidate: it steps a phasor, $\mathrm{num}(n+1) = \mathrm{num}(n) \circledast \mathrm{num}(1)$, in `f64`. Among 2,001 candidates the largest spurious similarity is about 0.12 against 1 for the true value. +So addition **is** binding. For integers $a \ne b$, $\mathbb{E}\cos((a-b)\phi_k) = 0$ over the uniform base, so distinct integers are quasi-orthogonal (sd 0.031). Decoding picks $\hat n = \arg\max_{|n| \le 1000} \mathrm{sim}(v, \mathrm{num}(n))$: the `number-decode` circuit scores $v$ against the codebook rows $\mathrm{num}(-1000), \dots, \mathrm{num}(1000)$ (one `matvec`), keeps the top one, and reads out its integer and its clean row. Encoding needs no trigonometry at run time: since $\mathrm{num}$ is a homomorphism, $\mathrm{num}(\sum_q v_q 10^q) = \circledast_q\, \mathrm{num}(v_q 10^q)$, so the `number-encode` circuit selects one row of the digit table per digit and binds them. Among 2,001 candidates the largest spurious similarity is about 0.12 against 1 for the true value. ### Discourse state ("it") diff --git a/model/prog/fast-recall.json b/model/prog/fast-recall.json new file mode 100644 index 0000000..afba8f1 --- /dev/null +++ b/model/prog/fast-recall.json @@ -0,0 +1,20 @@ +{ + "id": "fast-recall", + "version": 1, + "seed_entry": "value-codec-circuits", + "description": "W_FAST recall compiled to the neural ISA: pattern separation of the pooled key by core.fast_separator and k-winners-take-all (32 of 1024, amplitude 1/sqrt(32)), the read memory.fast s, and nearest-code cleanup against core.embed. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "fast_recall", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "key_dim": 128, + "expansion": 1024, + "active": 32, + "lr": 1.0, + "decay": 0.99, + "eps": 1e-6 + } +} diff --git a/model/prog/fast-write.json b/model/prog/fast-write.json new file mode 100644 index 0000000..e716f6b --- /dev/null +++ b/model/prog/fast-write.json @@ -0,0 +1,20 @@ +{ + "id": "fast-write", + "version": 1, + "seed_entry": "value-codec-circuits", + "description": "W_FAST write compiled to the neural ISA: the same pattern separation, the value bytes encoded through core.embed in-circuit, then delta_write into the state memory.fast twice: decay 0.99 with learning rate 0, then the normalized delta rule (learning rate 1, epsilon 1e-6) on the decayed matrix. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "fast_write", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "key_dim": 128, + "expansion": 1024, + "active": 32, + "lr": 1.0, + "decay": 0.99, + "eps": 1e-6 + } +} diff --git a/model/prog/host-audit.json b/model/prog/host-audit.json index 0a03152..c9bc93a 100644 --- a/model/prog/host-audit.json +++ b/model/prog/host-audit.json @@ -8,7 +8,15 @@ "crates/zerolm-wsp/src/circuits/atom_encode.rs", "crates/zerolm-wsp/src/circuits/attention_read.rs", "crates/zerolm-wsp/src/circuits/key_encode.rs", + "crates/zerolm-wsp/src/circuits/number.rs", + "crates/zerolm-engine/src/binding.rs", "crates/zerolm-wsp/src/circuits/relational.rs", + "crates/zerolm-wsp/src/circuits/probe.rs", + "crates/zerolm-wsp/src/circuits/decide.rs", + "crates/zerolm-reasoner/src/operators.rs", + "crates/zerolm-wsp/src/circuits/value_codec.rs", + "crates/zerolm-wsp/src/circuits/memory_write.rs", + "crates/zerolm-memory/src/value_circuits.rs", "crates/zerolm-memory/src/read_circuit.rs", "crates/zerolm-reasoner/src/circuits.rs" ], @@ -29,11 +37,6 @@ ".contains(\"" ], "pending": [ - {"file": "crates/zerolm-engine/src/semantic_state.rs", "reason": "the bounded-language parser matches token templates in Rust; it must become a recurrent parser circuit"}, - {"file": "crates/zerolm-engine/src/binding.rs", "reason": "symbols, roles, and relations are now computed by the atom-encode circuit; what remains on the host is the number encoding (fractional-power phasors need trigonometry, which the ISA lacks) and its cleanup decoding, which must become a number codebook plus matvec and topk"}, - {"file": "crates/zerolm-reasoner/src/operators.rs", "reason": "each operator's decision logic (combining yes/no proofs, the inheritance level scan, negation edges, conditional chaining, count arithmetic) is Rust control flow; only the graph search runs in circuits"}, - {"file": "crates/zerolm-reasoner/src/lib.rs", "reason": "one-step probes (matches, edges), symbol interning, and pronoun resolution run on the host"}, - {"file": "crates/zerolm-memory/src/persistent_memory.rs", "reason": "write-time bookkeeping (slot choice, status, supersession, the key word-count limit) is host code; it is an update rule, but its value comparison uses the host decoder"}, - {"file": "crates/zerolm-memory/src/retrieval.rs", "reason": "nearest-code decoding for write-time comparison and fast-weight recall runs on the host"} + {"file": "crates/zerolm-engine/src/semantic_state.rs", "reason": "the bounded-language parser matches token templates in Rust; it must become a recurrent parser circuit; it also converts numerals to integers, which the host formats back to decimal bytes for the number-encode circuit, and its parsed structures are mapped to role-filler circuit inputs (pred_parts in zerolm-reasoner) and dispatched by form (statement or question kind) on the host"} ] } diff --git a/model/prog/memory-compact.json b/model/prog/memory-compact.json new file mode 100644 index 0000000..b8950fd --- /dev/null +++ b/model/prog/memory-compact.json @@ -0,0 +1,21 @@ +{ + "id": "memory-compact", + "version": 1, + "seed_entry": "memory-write-circuits", + "description": "Memory compaction compiled to the neural ISA: active slots move to the lowest slots in order (top-k), supersession links and episodic slot references are remapped through the permutation (-1 when the target is gone), and the remaining rows become empty records. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "memory_compact", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "slots": 2048, + "key_dim": 128, + "key_words": 1024, + "episodes": 4096, + "same_key_threshold": 0.95, + "unconfirmed": 0.5, + "confirmed": 1.0 + } +} diff --git a/model/prog/memory-forget.json b/model/prog/memory-forget.json new file mode 100644 index 0000000..4fd940c --- /dev/null +++ b/model/prog/memory-forget.json @@ -0,0 +1,21 @@ +{ + "id": "memory-forget", + "version": 1, + "seed_entry": "memory-write-circuits", + "description": "Memory forget compiled to the neural ISA: the same-key slot and every slot it supersedes (one link per step, one step per slot), the episodic records referring to them (keys erased), and the metadata with the chain marked deleted. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "memory_forget", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "slots": 2048, + "key_dim": 128, + "key_words": 1024, + "episodes": 4096, + "same_key_threshold": 0.95, + "unconfirmed": 0.5, + "confirmed": 1.0 + } +} diff --git a/model/prog/memory-history.json b/model/prog/memory-history.json new file mode 100644 index 0000000..7d4759b --- /dev/null +++ b/model/prog/memory-history.json @@ -0,0 +1,21 @@ +{ + "id": "memory-history", + "version": 1, + "seed_entry": "memory-write-circuits", + "description": "Episodic history compiled to the neural ISA: valid episodes whose key matches the query key (dot product >= 0.95). Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "memory_history", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "slots": 2048, + "key_dim": 128, + "key_words": 1024, + "episodes": 4096, + "same_key_threshold": 0.95, + "unconfirmed": 0.5, + "confirmed": 1.0 + } +} diff --git a/model/prog/memory-write.json b/model/prog/memory-write.json new file mode 100644 index 0000000..9e417f9 --- /dev/null +++ b/model/prog/memory-write.json @@ -0,0 +1,21 @@ +{ + "id": "memory-write", + "version": 1, + "seed_entry": "memory-write-circuits", + "description": "Memory write decisions compiled to the neural ISA (teach, correct, confirm): the same-key slot (best active key with score >= 0.95), its value decoded against core.embed and compared with the new value, the lowest empty slot, and the outcome (stored, unchanged, conflict, superseded, confirmed, unknown); outputs the new slot's rows, the old slot's updated metadata, and the episode row. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "memory_write", + "codec": { + "positions": 48, + "code_rows": 256, + "code_dim": 16 + }, + "slots": 2048, + "key_dim": 128, + "key_words": 1024, + "episodes": 4096, + "same_key_threshold": 0.95, + "unconfirmed": 0.5, + "confirmed": 1.0 + } +} diff --git a/model/prog/number-decode.json b/model/prog/number-decode.json new file mode 100644 index 0000000..2376948 --- /dev/null +++ b/model/prog/number-decode.json @@ -0,0 +1,11 @@ +{ + "id": "number-decode", + "version": 1, + "seed_entry": "number-decode-circuit", + "description": "Number cleanup compiled to the neural ISA: a noisy number phasor is scored against core.reasoner.number_codebook (row j = number(j - range)), the top-1 row is selected, and its integer value and clean row are read out. range equals number_range and dim equals dim in model/specs/reasoning.json.", + "circuit": { + "kind": "number_decode", + "range": 1000, + "dim": 512 + } +} diff --git a/model/prog/number-encode.json b/model/prog/number-encode.json new file mode 100644 index 0000000..857a33b --- /dev/null +++ b/model/prog/number-encode.json @@ -0,0 +1,12 @@ +{ + "id": "number-encode", + "version": 1, + "seed_entry": "number-encode-circuit", + "description": "Fractional-power number encoder compiled to the neural ISA: the bytes of a decimal numeral (optional leading '-', at positions 0..len of an 8-byte input) to the phasor number(n) = exp(i n theta). Each digit byte selects row 10 q + v (q = digits after it) of core.reasoner.number_digits (number(v 10^q)); the rows are bound, and a leading '-' conjugates. No trigonometry runs. dim equals model/specs/reasoning.json.", + "circuit": { + "kind": "number_encode", + "numeral_bytes": 8, + "places": 7, + "dim": 512 + } +} diff --git a/model/prog/pinned.json b/model/prog/pinned.json index 6ed8200..041b285 100644 --- a/model/prog/pinned.json +++ b/model/prog/pinned.json @@ -2,9 +2,25 @@ "version": 1, "circuits": { "atom-encode": "94a9df214e0da88cbb3b3a59bebe001073ec75fd133e4c36a9ec57ac033ded0e", + "fast-recall": "0653978c84ac08c5cfadc1e4a48cf0e73a90bf92d8df139970b36ff8afd27723", + "fast-write": "66d4e9f0c7ae3fb08cc470c033f6c3c81ac057344579864797c7bac0722b640a", + "memory-compact": "f853cde0c51409d08d33ade081dce10c437097067a677797197ee1e4a53edb64", "memory-encode": "99b405aa6bd6a3beaf5f8e87f425a24721e0817f18807408f6ffaae5a8207feb", + "memory-forget": "15f4f62ec7877278f367144bb3cf8b156efca60442dafa4db3d861f8c0bba050", + "memory-history": "867a935aaaf91dfb9d5a9645f7f9c4300698285504ec788b087c280b0334ee3f", "memory-read": "05a51725faae1cf9282815f82417a3976b5034937bdf7f7ee8449104659d475d", + "memory-write": "596eb76594039399714da7e88bf642facad19f7ebd6c36bc38245ba5e36fdc34", + "number-decode": "faf56ddfb8f262aa26784a5da5b9fd40f90f46afbd2202d7e9082dc53ce00aee", + "number-encode": "bb850fbef9db6f31b63f71ae16e304842f6d2d30ab4aa0348d1d37842ac78c0d", + "relation-counts": "6d2c70ec564839c47b9032a6848a2a7dc5590b909a376c24c1d147e26fa3b5a8", "relation-graph": "efefcd319d2f39c8366a3491c1505cbfa8e05287be3293a57fca586ad668bb51", - "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee" + "relation-join": "ccedd0ce9fbd4956617da47eea16a80d823b508dea9f2b8034960db3a6efdec7", + "relation-levels": "dc6a19dd8edd9b3f98217269832e969cc43f3dbb106ce0e48322c4558f373338", + "relation-probe": "099a1ed106d1ab5188a3e1a304c824561ef9725c246bb599c6c28cfeefad3293", + "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee", + "relation-rules": "3b6350b4642c4b8a7210f646474f4bf4998743738d9481da9dcb7650b1d4eef8", + "relation-verdict": "698ebf8ddbcc1ef9ef51d8e5932a26ea7ad1f77d97b92e5b0fa930b86a09a16c", + "symbol-cleanup": "33506da99bcea531e8eac9b21b07ec9e6d8dc7805e6cb85e3dcacb219f451fb0", + "value-encode": "e6240156a25968a4289f20d748a4cf6ed1e65fc9ca59380ed0a468661a170c01" } } diff --git a/model/prog/relation-counts.json b/model/prog/relation-counts.json new file mode 100644 index 0000000..5cc1f28 --- /dev/null +++ b/model/prog/relation-counts.json @@ -0,0 +1,18 @@ +{ + "id": "relation-counts", + "version": 1, + "seed_entry": "relation-decide-circuits", + "description": "Arithmetic as a circuit: breadth-first over more edges from x carrying number(sum of differences) as a phasor (binding is addition, differences cleaned up against core.reasoner.number_codebook), every reached has-count bound to its carried difference and decoded; one distinct value answers with the shortest proof, several are a contradiction with all proofs. Parameters equal model/specs/reasoning.json.", + "circuit": { + "kind": "relation_counts", + "relational": { + "dim": 512, + "meta_cols": 10, + "max_depth": 8, + "match_margin": 0.5, + "cleanup_min": 0.3, + "symbol_same": 0.9 + }, + "range": 1000 + } +} diff --git a/model/prog/relation-join.json b/model/prog/relation-join.json new file mode 100644 index 0000000..a488cff --- /dev/null +++ b/model/prog/relation-join.json @@ -0,0 +1,15 @@ +{ + "id": "relation-join", + "version": 1, + "seed_entry": "relation-decide-circuits", + "description": "Negation as a circuit: the shortest negative edge of a relation from a source set (x, plus its reached ancestors when class inclusion is enabled) to a target set (y, plus its ancestors), with source and target premise matrices from relation-reach. Parameters equal model/specs/reasoning.json.", + "circuit": { + "kind": "relation_join", + "dim": 512, + "meta_cols": 10, + "max_depth": 8, + "match_margin": 0.5, + "cleanup_min": 0.3, + "symbol_same": 0.9 + } +} diff --git a/model/prog/relation-levels.json b/model/prog/relation-levels.json new file mode 100644 index 0000000..775adce --- /dev/null +++ b/model/prog/relation-levels.json @@ -0,0 +1,15 @@ +{ + "id": "relation-levels", + "version": 1, + "seed_entry": "relation-decide-circuits", + "description": "Attribute inheritance ('can') as a circuit: breadth-first levels over positive isa edges from x (max_depth + 1 levels), latching positive and (with negation) negative can edges to v at the first level that has any. Parameters equal model/specs/reasoning.json.", + "circuit": { + "kind": "relation_levels", + "dim": 512, + "meta_cols": 10, + "max_depth": 8, + "match_margin": 0.5, + "cleanup_min": 0.3, + "symbol_same": 0.9 + } +} diff --git a/model/prog/relation-probe.json b/model/prog/relation-probe.json new file mode 100644 index 0000000..e021771 --- /dev/null +++ b/model/prog/relation-probe.json @@ -0,0 +1,14 @@ +{ + "id": "relation-probe", + "version": 1, + "seed_entry": "relation-probe-circuits", + "description": "Statement write decision compiled to the neural ISA: role-filler pairs (fillers and one-hot role selectors) are bound with core.reasoner.roles and bundled; an active slot of memory.relations.slots with the same polarity and role count and sim >= K - margin is a duplicate; the statement is stored (row and metadata row output, zero otherwise) only when its subject resolved and it is not a duplicate; the discourse state is updated (decay * state + subject) when the subject resolved. dim, match_margin, pronoun_decay equal model/specs/reasoning.json; meta columns as in zerolm-reasoner field.", + "circuit": { + "kind": "relation_probe", + "dim": 512, + "meta_cols": 10, + "n_roles_col": 9, + "match_margin": 0.5, + "discourse_decay": 0.5 + } +} diff --git a/model/prog/relation-rules.json b/model/prog/relation-rules.json new file mode 100644 index 0000000..38281c8 --- /dev/null +++ b/model/prog/relation-rules.json @@ -0,0 +1,15 @@ +{ + "id": "relation-rules", + "version": 1, + "seed_entry": "relation-decide-circuits", + "description": "Conditionals as a circuit: backward levels from a property over in-scope rules ('if is q then it is p'), latching the first level at which x has a frontier property directly; the proof is that fact plus the rule chain. Parameters equal model/specs/reasoning.json.", + "circuit": { + "kind": "relation_rules", + "dim": 512, + "meta_cols": 10, + "max_depth": 8, + "match_margin": 0.5, + "cleanup_min": 0.3, + "symbol_same": 0.9 + } +} diff --git a/model/prog/relation-verdict.json b/model/prog/relation-verdict.json new file mode 100644 index 0000000..41737d2 --- /dev/null +++ b/model/prog/relation-verdict.json @@ -0,0 +1,15 @@ +{ + "id": "relation-verdict", + "version": 1, + "seed_entry": "relation-decide-circuits", + "description": "How findings combine, as a circuit: positive and negative findings to a one-hot verdict (yes, no, contradiction, unknown) and the union of the supporting proofs.", + "circuit": { + "kind": "relation_verdict", + "dim": 512, + "meta_cols": 10, + "max_depth": 8, + "match_margin": 0.5, + "cleanup_min": 0.3, + "symbol_same": 0.9 + } +} diff --git a/model/prog/symbol-cleanup.json b/model/prog/symbol-cleanup.json new file mode 100644 index 0000000..49b220f --- /dev/null +++ b/model/prog/symbol-cleanup.json @@ -0,0 +1,13 @@ +{ + "id": "symbol-cleanup", + "version": 1, + "seed_entry": "relation-probe-circuits", + "description": "Symbol cleanup compiled to the neural ISA: the cosine of a vector with every row of memory.relations.codebook, the best row, and whether it reaches the threshold selected by a one-hot mode (cleanup 0.3, same symbol 0.9, pronoun 0.4). Used for symbol interning (with a gate, the row to append when a name has no codebook entry) and resolving 'it' from the discourse state. Thresholds equal model/specs/reasoning.json.", + "circuit": { + "kind": "symbol_cleanup", + "dim": 512, + "cleanup_min": 0.3, + "symbol_same": 0.9, + "pronoun_min": 0.4 + } +} diff --git a/model/prog/value-encode.json b/model/prog/value-encode.json new file mode 100644 index 0000000..6f00215 --- /dev/null +++ b/model/prog/value-encode.json @@ -0,0 +1,12 @@ +{ + "id": "value-encode", + "version": 1, + "seed_entry": "value-codec-circuits", + "description": "Memory value encoder compiled to the neural ISA: value bytes (NUL-padded to 48 positions) to the concatenated rows of core.embed, by a byte one-hot (comparison with 0..255) and a matrix product. Parameters equal model/specs/memory.json (value_bytes + 1 positions) and the core byte embedding.", + "circuit": { + "kind": "value_encode", + "positions": 48, + "code_rows": 256, + "code_dim": 16 + } +} diff --git a/model/specs/weight-groups.json b/model/specs/weight-groups.json index 98becd3..92b9c59 100644 --- a/model/specs/weight-groups.json +++ b/model/specs/weight-groups.json @@ -51,7 +51,7 @@ }, { "id": "binding_roles", - "description": "Frozen vector-symbolic constants (core.reasoner.*): the atom hash table (core.reasoner.atom_table, input of the atom-encode circuit), the role phasors (subj, rel, obj, quant, scope) and relation atoms it computes, and the fractional-power number base. Seeded; verified bit-identical on load.", + "description": "Frozen vector-symbolic constants (core.reasoner.*): the atom hash table (core.reasoner.atom_table, input of the atom-encode circuit), the role phasors (subj, rel, obj, quant, scope) and relation atoms it computes, and the two fractional-power number tables (core.reasoner.number_digits, input of the number-encode circuit; core.reasoner.number_codebook, input of the number-decode circuit). Seeded; verified bit-identical on load.", "init_source": "phasor-binding-init", "update_path": "none" }, diff --git a/research/manifest/seed-knowledge.json b/research/manifest/seed-knowledge.json index ebc81de..e976e16 100644 --- a/research/manifest/seed-knowledge.json +++ b/research/manifest/seed-knowledge.json @@ -40,7 +40,7 @@ { "id": "phasor-binding-init", "kind": "init_formula", - "description": "Symbols, roles, and relation atoms are unit complex phasors computed by the compiled atom-encode circuit from their namespaced name bytes and the seeded table core.reasoner.atom_table (random rows from a SplitMix64 stream); numbers use fractional-power encoding exp(i n theta) with a seeded base. Binding is elementwise complex multiplication.", + "description": "Symbols, roles, and relation atoms are unit complex phasors computed by the compiled atom-encode circuit from their namespaced name bytes and the seeded table core.reasoner.atom_table (random rows from a SplitMix64 stream); numbers use fractional-power encoding exp(i n theta) with a seeded base theta, from which two tables are initialized: core.reasoner.number_digits (row 10 q + v = number(v 10^q), q < 7) and core.reasoner.number_codebook (row j = number(j - number_range)); encoding and cleanup run in the compiled number-encode and number-decode circuits. Binding is elementwise complex multiplication.", "source": "crates/zerolm-engine/src/binding.rs", "derived_from_corpus": false }, @@ -79,6 +79,48 @@ "source": "crates/zerolm-wsp/src/circuits/atom_encode.rs", "derived_from_corpus": false }, + { + "id": "number-encode-circuit", + "kind": "structural_prior", + "description": "Circuit number-encode (model/prog/number-encode.json): a decimal numeral (bytes, optional leading minus) to its fractional-power phasor as a neural ISA program. Constants: the ten digit byte codes 48..57, the minus byte 45, a strict lower-triangular place counter, place indices 0..6, one-hot spread and tile selectors, the identity phasor, row pickers, and the conjugation mask. Digit rows come from core.reasoner.number_digits (seeded). Positional decimal notation is the only prior; no vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/number.rs", + "derived_from_corpus": false + }, + { + "id": "value-codec-circuits", + "kind": "structural_prior", + "description": "Circuits value-encode, fast-recall, and fast-write (model/prog/value-encode.json, fast-*.json): memory value bytes to rows of core.embed by a byte one-hot (byte indices 0..255); nearest-code cleanup (score 2 x.c - |c|^2, NUL-terminated, top-1 minus top-2 margin); W_FAST pattern separation by core.fast_separator with k-winners-take-all (amplitude 1/sqrt(active)); and the decayed normalized delta rule as two delta_write instructions (decay 0.99, learning rate 1, epsilon 1e-6, from model/specs/memory.json). No vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/value_codec.rs", + "derived_from_corpus": false + }, + { + "id": "relation-probe-circuits", + "kind": "structural_prior", + "description": "Circuits relation-probe and symbol-cleanup (model/prog/relation-probe.json, symbol-cleanup.json): a role-filler probe (role selection from core.reasoner.roles, binding, bundling, sim >= K - margin over active slots, polarity and role-count filters from metadata column selectors) and nearest-symbol cleanup (cosine against memory.relations.codebook, top-1, and a threshold chosen by a one-hot mode among cleanup_min, symbol_same, pronoun_min from model/specs/reasoning.json). No vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/probe.rs", + "derived_from_corpus": false + }, + { + "id": "memory-write-circuits", + "kind": "structural_prior", + "description": "Circuits memory-write, memory-forget, memory-compact, and memory-history (model/prog/memory-*.json): every change to the semantic and episodic memory as neural ISA programs: the same-key slot (best active key, score >= same_key_threshold), decode-and-compare of its value, the lowest empty slot, the outcome of teach/correct/confirm, new and updated metadata rows (status codes, unconfirmed/confirmed levels, supersession links), following supersession links for forget, the compaction order (top-k) and link remapping (a permutation matrix), and episodic history by key similarity. Constants: metadata column selectors, status and op codes, slot indices. No vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/memory_write.rs", + "derived_from_corpus": false + }, + { + "id": "relation-decide-circuits", + "kind": "structural_prior", + "description": "Circuits relation-join, relation-levels, relation-rules, relation-counts, and relation-verdict (model/prog/relation-*.json): the eight operators' decisions as fixed-step recurrences over the C5 graph outputs: negative edges between source and target sets with the shortest proof, level-by-level inheritance with a latched first decision, backward chaining over in-scope rules, counts along 'more' chains by phasor binding and codebook cleanup (one distinct value or a contradiction), and the yes/no/contradiction/unknown combination of findings. Constants: per-operator enable scalars, proof-length scores, readout values -range..range, one-hot verdict selectors. No vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/decide.rs", + "derived_from_corpus": false + }, + { + "id": "number-decode-circuit", + "kind": "structural_prior", + "description": "Circuit number-decode (model/prog/number-decode.json): cleanup of a noisy number phasor as a neural ISA program: scores against core.reasoner.number_codebook, top-1, and readout of the integer value (constant column -range..range) and the clean codebook row. No vocabulary or facts.", + "source": "crates/zerolm-wsp/src/circuits/number.rs", + "derived_from_corpus": false + }, { "id": "relation-graph-circuit", "kind": "structural_prior",