From 7caafa2e2759795d3ab9001d17166a88464adfea Mon Sep 17 00:00:00 2001 From: nima Date: Sun, 15 Nov 2026 02:38:37 +0330 Subject: [PATCH 1/6] reasoner: encode numbers with the compiled number-encode circuit (decimal numeral bytes to a product of seeded digit phasors, a leading minus conjugates) and clean them up with the compiled number-decode circuit (codebook matvec, top-1, value and clean-row readout) instead of host trigonometry and a host scan; replace core.reasoner.number_one with the number_digits and number_codebook tables; arithmetic binds clean rows; pin both circuits; eval: record reasoning circuit runs from the first taught statement; remove binding.rs from the pending host audit --- crates/zerolm-engine/src/binding.rs | 53 +++---- crates/zerolm-eval/src/uniform.rs | 7 +- crates/zerolm-reasoner/src/circuits.rs | 155 +++++++++++++++++++- crates/zerolm-reasoner/src/lib.rs | 26 ++-- crates/zerolm-reasoner/src/operators.rs | 20 +-- crates/zerolm-reasoner/tests/reasoner.rs | 10 +- crates/zerolm-wsp/src/circuits/mod.rs | 1 + crates/zerolm-wsp/src/circuits/number.rs | 173 +++++++++++++++++++++++ crates/zerolm-wsp/src/compiler.rs | 4 + crates/zerolm-wsp/src/lib.rs | 4 +- docs/claims-and-limitations.md | 2 +- docs/neural-isa.md | 21 ++- docs/reasoning.md | 6 +- model/prog/host-audit.json | 7 +- model/prog/number-decode.json | 11 ++ model/prog/number-encode.json | 12 ++ model/prog/pinned.json | 2 + model/specs/weight-groups.json | 2 +- research/manifest/seed-knowledge.json | 16 ++- 19 files changed, 462 insertions(+), 70 deletions(-) create mode 100644 crates/zerolm-wsp/src/circuits/number.rs create mode 100644 model/prog/number-decode.json create mode 100644 model/prog/number-encode.json 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..9a33689 100644 --- a/crates/zerolm-eval/src/uniform.rs +++ b/crates/zerolm-eval/src/uniform.rs @@ -137,18 +137,19 @@ fn memory_parity() -> Result { 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-reasoner/src/circuits.rs b/crates/zerolm-reasoner/src/circuits.rs index fdb7898..2d16c11 100644 --- a/crates/zerolm-reasoner/src/circuits.rs +++ b/crates/zerolm-reasoner/src/circuits.rs @@ -9,9 +9,10 @@ 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::number::{DecodeParams as NumberDecodeParams, EncodeParams as NumberEncodeParams}; use zerolm_wsp::circuits::relational::{Params, OPERATORS}; use zerolm_wsp::{compile, Circuit, Compiled, Spec}; @@ -118,6 +119,113 @@ impl Atoms { } } +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: std::cell::RefCell>>, + 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, + } + } + + fn run(&self, c: &Compiled, input: (&str, Buf), table: (&str, View<'_>)) -> Outputs { + let mut w: Vec<(&str, View<'_>)> = vec![table]; + w.extend(c.tensors.iter().map(|t| (t.name.as_str(), t.view()))); + let mut b = Bindings::new().input(input.0, input.1.clone()); + for (n, v) in &w { + b = b.weight(n, *v); + } + let out = isa::run(&c.program, b).unwrap_or_else(|e| panic!("{} runs: {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, &w), inputs: vec![(input.0.to_string(), input.1)], outputs: out.clone() }); + } + out + } + + /// 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(); + } + // 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 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, @@ -168,12 +276,14 @@ impl Reasoner { 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 }; + *self.numbers.recorder.borrow_mut() = if on { Some(Vec::new()) } else { None }; } /// 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.extend(self.numbers.recorder.borrow_mut().as_mut().map(std::mem::take).unwrap_or_default()); runs } @@ -301,4 +411,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..4797363 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, bundle, sim_raw, unbind, 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, @@ -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(); @@ -333,7 +333,7 @@ impl Reasoner { graph_cache: std::cell::RefCell::new(None), recorder: std::cell::RefCell::new(None), 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] { @@ -477,9 +478,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 @@ -612,7 +613,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 +628,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, &fresh.circuits.graph, &fresh.circuits.reach] { 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 +660,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..99587f7 100644 --- a/crates/zerolm-reasoner/src/operators.rs +++ b/crates/zerolm-reasoner/src/operators.rs @@ -6,6 +6,7 @@ //! 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::circuits::Decoded; use crate::{field, Answer, Reasoner, Verdict, OBJ, QUANT, REL, SCOPE, SUBJ}; use std::collections::{BTreeMap, BTreeSet}; use zerolm_engine::binding::{bind, cosine, Hv}; @@ -251,26 +252,25 @@ impl Reasoner { None } - /// 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)> { + /// Every count of `thing` derivable for `x` (decoded by the compiled + /// `number-decode` circuit, with its clean vector), with its proof. + fn counts(&self, x: usize, thing: &str, depth: usize, visited: &mut BTreeSet) -> Vec<(Decoded, Proof)> { let sym = self.symbol(thing); - let r = self.cfg.number_range; - let mut out: Vec<(i64, Proof)> = self + let mut out: Vec<(Decoded, 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"))) + .map(|s| (self.numbers().decode(&self.unbind_slot(s, QUANT)), 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; + let delta = self.numbers().decode(&self.unbind_slot(s, QUANT)); 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); + // Addition is binding of clean fractional-power number vectors. + let n = self.numbers().decode(&bind(&c.clean, &delta.clean)); out.push((n, Proof::of(vec![s], "arithmetic").join(p))); } } @@ -280,7 +280,7 @@ impl Reasoner { 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(); + let values: BTreeSet = all.iter().map(|(n, _)| n.value).collect(); match values.len() { 0 => self.unknown(), 1 => { diff --git a/crates/zerolm-reasoner/tests/reasoner.rs b/crates/zerolm-reasoner/tests/reasoner.rs index 4b73ef9..e93681c 100644 --- a/crates/zerolm-reasoner/tests/reasoner.rs +++ b/crates/zerolm-reasoner/tests/reasoner.rs @@ -168,9 +168,15 @@ 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), + ] { 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/mod.rs b/crates/zerolm-wsp/src/circuits/mod.rs index 14bf0f0..2d34492 100644 --- a/crates/zerolm-wsp/src/circuits/mod.rs +++ b/crates/zerolm-wsp/src/circuits/mod.rs @@ -4,4 +4,5 @@ pub mod atom_encode; pub mod attention_read; pub mod key_encode; +pub mod number; pub mod relational; 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/compiler.rs b/crates/zerolm-wsp/src/compiler.rs index ebe78a4..9f5d7a1 100644 --- a/crates/zerolm-wsp/src/compiler.rs +++ b/crates/zerolm-wsp/src/compiler.rs @@ -33,6 +33,8 @@ 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), RelationGraph(circuits::relational::Params), RelationReach(circuits::relational::Params), } @@ -119,6 +121,8 @@ 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::RelationGraph(p) => circuits::relational::compile_graph(&spec.id, p)?, Circuit::RelationReach(p) => circuits::relational::compile_reach(&spec.id, p)?, }; diff --git a/crates/zerolm-wsp/src/lib.rs b/crates/zerolm-wsp/src/lib.rs index b7ebf9a..53bb97d 100644 --- a/crates/zerolm-wsp/src/lib.rs +++ b/crates/zerolm-wsp/src/lib.rs @@ -12,12 +12,14 @@ 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); 7] = [ ("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")), ]; /// `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..fbeba8e 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: 1,343 of 1,343 bit-exact (also the encoder runs made while the world is taught). 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. 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 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`. ## 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/neural-isa.md b/docs/neural-isa.md index c127764..d90b3a7 100644 --- a/docs/neural-isa.md +++ b/docs/neural-isa.md @@ -77,7 +77,7 @@ The header is `{"program": , "tensors": [{"name", "rows", "cols"}, ...] | 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) | +| 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 (1,343 runs, all bit-exact) | | 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,25 @@ 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 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..95d6828 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,7 +44,7 @@ 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. @@ -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/host-audit.json b/model/prog/host-audit.json index 0a03152..295c5ea 100644 --- a/model/prog/host-audit.json +++ b/model/prog/host-audit.json @@ -8,6 +8,8 @@ "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-memory/src/read_circuit.rs", "crates/zerolm-reasoner/src/circuits.rs" @@ -29,9 +31,8 @@ ".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-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"}, + {"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, collecting counts) is Rust control flow; the graph search, number encoding, and number cleanup run 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"} 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..1dc7c0f 100644 --- a/model/prog/pinned.json +++ b/model/prog/pinned.json @@ -4,6 +4,8 @@ "atom-encode": "94a9df214e0da88cbb3b3a59bebe001073ec75fd133e4c36a9ec57ac033ded0e", "memory-encode": "99b405aa6bd6a3beaf5f8e87f425a24721e0817f18807408f6ffaae5a8207feb", "memory-read": "05a51725faae1cf9282815f82417a3976b5034937bdf7f7ee8449104659d475d", + "number-decode": "faf56ddfb8f262aa26784a5da5b9fd40f90f46afbd2202d7e9082dc53ce00aee", + "number-encode": "bb850fbef9db6f31b63f71ae16e304842f6d2d30ab4aa0348d1d37842ac78c0d", "relation-graph": "efefcd319d2f39c8366a3491c1505cbfa8e05287be3293a57fca586ad668bb51", "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee" } 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..64f5876 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,20 @@ "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": "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", From 02bd59bfcbf0118383124cca90f94ec5e669f0ec Mon Sep 17 00:00:00 2001 From: nima Date: Sun, 15 Nov 2026 02:55:32 +0330 Subject: [PATCH 2/6] memory: encode values, compare stored values at write time, and write and recall W_FAST only through compiled circuits (value-encode: byte one-hot times core.embed; value-match: nearest-code cleanup and exact byte comparison; fast-recall: kWTA pattern separation, read, cleanup; fast-write: the same separation and two delta_write instructions for decay then the normalized delta rule on the memory.fast state); delete the host retrieval and fast-weight code; wsp: state tensors in the builder; eval: replay the four circuits (with final state) on the reference interpreter; pin them --- .claude/skills/zerolm-roadmap/SKILL.md | 2 +- crates/zerolm-eval/src/uniform.rs | 32 ++- crates/zerolm-memory/src/fast_weights.rs | 89 ------- crates/zerolm-memory/src/lib.rs | 3 +- crates/zerolm-memory/src/persistent_memory.rs | 69 ++++-- crates/zerolm-memory/src/read_circuit.rs | 5 +- crates/zerolm-memory/src/retrieval.rs | 71 ------ crates/zerolm-memory/src/value_circuits.rs | 219 ++++++++++++++++++ crates/zerolm-memory/tests/memory.rs | 16 ++ crates/zerolm-wsp/src/circuits/mod.rs | 1 + crates/zerolm-wsp/src/circuits/value_codec.rs | 210 +++++++++++++++++ crates/zerolm-wsp/src/compiler.rs | 18 +- crates/zerolm-wsp/src/lib.rs | 6 +- docs/claims-and-limitations.md | 2 +- docs/memory-format.md | 6 +- docs/neural-isa.md | 13 +- model/prog/fast-recall.json | 20 ++ model/prog/fast-write.json | 20 ++ model/prog/host-audit.json | 5 +- model/prog/pinned.json | 6 +- model/prog/value-encode.json | 12 + model/prog/value-match.json | 12 + research/manifest/seed-knowledge.json | 7 + 23 files changed, 644 insertions(+), 200 deletions(-) delete mode 100644 crates/zerolm-memory/src/fast_weights.rs delete mode 100644 crates/zerolm-memory/src/retrieval.rs create mode 100644 crates/zerolm-memory/src/value_circuits.rs create mode 100644 crates/zerolm-wsp/src/circuits/value_codec.rs create mode 100644 model/prog/fast-recall.json create mode 100644 model/prog/fast-write.json create mode 100644 model/prog/value-encode.json create mode 100644 model/prog/value-match.json 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/crates/zerolm-eval/src/uniform.rs b/crates/zerolm-eval/src/uniform.rs index 9a33689..d88f6b7 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,6 +133,31 @@ fn memory_parity() -> Result { *p.by_program.entry(id.into()).or_default() += 1; } } + // Value codec and W_FAST circuits: a short session of new facts, repeated + // facts (unchanged), contradicting facts, 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(); + for i in 0..40 { + v.teach(&format!("item{i} colour"), &format!("shade{i}"), "uniform:value-codec").map_err(me)?; + } + for i in 0..20 { + let value = if i % 2 == 0 { format!("shade{i}") } else { format!("tint{i}") }; + v.teach(&format!("item{i} colour"), &value, "uniform:value-codec").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) } 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..a6f0ce8 100644 --- a/crates/zerolm-memory/src/persistent_memory.rs +++ b/crates/zerolm-memory/src/persistent_memory.rs @@ -14,13 +14,13 @@ 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}; @@ -35,6 +35,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 +323,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 +353,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 +384,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,13 +484,19 @@ impl MemoryStore { Ok(()) } + fn codes(&self) -> zerolm_engine::isa::View<'_> { + view(&self.core.embed) + } + + /// Code row of a value, computed by the `value-encode` circuit. fn encode_value(&self, value: &str) -> Vec { - retrieval::encode_value(value.as_bytes(), &self.core.embed, self.cfg.positions()) + self.codec.encode_value(self.codes(), value) } - /// 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 + /// Whether a slot holds exactly `value` (the `value-match` circuit decodes + /// the stored row and compares it; used for write-time comparison). + fn holds_value(&self, slot: usize, value: &str) -> bool { + self.codec.matches(self.codes(), self.values.row(slot), value).1 } /// Active slot whose key is the same fact key: the read circuit's best @@ -589,7 +606,7 @@ impl MemoryStore { let src = Source::UserStatement; if let Some(s) = self.find_same(&k) { let seq = self.tick(); - if self.slot_value(s) == value.as_bytes() { + if self.holds_value(s, value) { 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 }); @@ -616,7 +633,7 @@ impl MemoryStore { let src = Source::UserCorrection; let old = self.find_same(&k); if let Some(s) = old { - if self.slot_value(s) == value.as_bytes() { + if self.holds_value(s, value) { 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)); @@ -760,6 +777,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) @@ -834,19 +856,21 @@ impl MemoryStore { 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 +880,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 +911,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 +929,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.encode, &self.codec.matcher, &self.codec.recall, &self.codec.write]); 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 +937,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 +1015,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..b4065f3 --- /dev/null +++ b/crates/zerolm-memory/src/value_circuits.rs @@ -0,0 +1,219 @@ +//! The compiled value codec and `W_FAST` circuits bound to a store +//! (`zerolm_wsp::circuits::value_codec`): `value-encode` turns value bytes +//! into code rows, `value-match` decodes a stored row and compares it with a +//! candidate value, `fast-recall` and `fast-write` read and update the +//! short-lived associative matrix `memory.fast`. 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::value_codec::{CodecParams, FastParams}; +use zerolm_wsp::{compile, Circuit, Compiled, Spec}; + +pub const ENCODE_ID: &str = "value-encode"; +pub const MATCH_ID: &str = "value-match"; +pub const RECALL_ID: &str = "fast-recall"; +pub const WRITE_ID: &str = "fast-write"; +/// 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 spec(id: &str, circuit: Circuit) -> Spec { + Spec { id: id.into(), version: 1, seed_entry: "value-codec-circuits".into(), description: String::new(), circuit } +} + +/// The four 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; 4] { + [ + spec(ENCODE_ID, Circuit::ValueEncode(codec(cfg, code_rows, code_dim))), + spec(MATCH_ID, Circuit::ValueMatch(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))), + ] +} + +/// 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 matcher: Compiled, + pub recall: Compiled, + pub write: Compiled, + positions: usize, + recorder: std::cell::RefCell>>, +} + +impl ValueCircuits { + pub fn new(cfg: &MemoryConfig, code_rows: usize, code_dim: usize) -> Self { + let [e, m, r, w] = specs(cfg, code_rows, code_dim).map(|s| compile(&s).expect("valid memory configuration compiles")); + ValueCircuits { encode: e, matcher: m, recall: r, write: w, positions: cfg.positions(), recorder: Default::default() } + } + + pub fn all(&self) -> [&Compiled; 4] { + [&self.encode, &self.matcher, &self.recall, &self.write] + } + + /// 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() + } + + /// Decode a stored row and compare it with `value` (`value-match`). + pub fn matches(&self, codes: View<'_>, row: &[f32], value: &str) -> (Decoded, bool) { + let o = self.run(&self.matcher, vec![("row", Buf::row(row.to_vec())), ("value", self.value_input(value))], &[("codes", codes)], None); + (decoded(&o), o.scalar("same") == 1.0) + } + + /// 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_and_match_decodes_them_back() { + 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}"); + } + let (d, same) = c.matches(v(&codes), &row, "blue"); + assert!(same && d.bytes == b"blue" && d.margin > 0.0); + for other in ["blu", "blues", "bluf", "x"] { + assert!(!c.matches(v(&codes), &row, other).1, "{other}"); + } + // A superposition decodes to its dominant value. + let green = c.encode_value(v(&codes), "green"); + let mix: Vec = row.iter().zip(&green).map(|(a, b)| 0.9 * a + 0.1 * b).collect(); + assert!(c.matches(v(&codes), &mix, "blue").1); + } + + #[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..2364ab4 100644 --- a/crates/zerolm-memory/tests/memory.rs +++ b/crates/zerolm-memory/tests/memory.rs @@ -405,3 +405,19 @@ 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")); + } +} diff --git a/crates/zerolm-wsp/src/circuits/mod.rs b/crates/zerolm-wsp/src/circuits/mod.rs index 2d34492..f61096d 100644 --- a/crates/zerolm-wsp/src/circuits/mod.rs +++ b/crates/zerolm-wsp/src/circuits/mod.rs @@ -6,3 +6,4 @@ pub mod attention_read; pub mod key_encode; pub mod number; pub mod relational; +pub mod value_codec; 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..9ba13fe --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/value_codec.rs @@ -0,0 +1,210 @@ +//! 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. +//! - `value-match`: a stored code row and candidate value bytes to the +//! decoded bytes, the decode margin, and `same` (1 when the decoded bytes +//! equal the candidate at every position). +//! - `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), `compare` (match tolerance), `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 MATCH_OUTPUTS: [&str; 3] = ["bytes", "margin", "same"]; +pub const RECALL_OUTPUTS: [&str; 2] = ["bytes", "margin"]; +pub const WRITE_OUTPUTS: [&str; 1] = ["sparse"]; + +/// Penalty for excluded margins (finite). +const BIG: f32 = 1e30; + +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(()) +} + +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). +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`. +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"); +} + +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_match(id: &str, p: &CodecParams) -> Result { + check(id, p)?; + let mut b = Builder::new(id); + b.input("row", Some(1), p.positions * p.code_dim); + b.input("value", Some(1), p.positions); + codec_io(&mut b, p); + support(&mut b); + emit_decode(&mut b, p, "row"); + // same = 1 iff max |bytes - value| < 1/2 (byte values are exact integers). + 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"); + Ok(b.finish(&MATCH_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 9f5d7a1..c0fd13e 100644 --- a/crates/zerolm-wsp/src/compiler.rs +++ b/crates/zerolm-wsp/src/compiler.rs @@ -35,6 +35,10 @@ pub enum Circuit { KeyEncode(circuits::key_encode::Params), NumberEncode(circuits::number::EncodeParams), NumberDecode(circuits::number::DecodeParams), + ValueEncode(circuits::value_codec::CodecParams), + ValueMatch(circuits::value_codec::CodecParams), + FastRecall(circuits::value_codec::FastParams), + FastWrite(circuits::value_codec::FastParams), RelationGraph(circuits::relational::Params), RelationReach(circuits::relational::Params), } @@ -123,6 +127,10 @@ pub fn compile(spec: &Spec) -> Result { 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::ValueMatch(p) => circuits::value_codec::compile_match(&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::RelationGraph(p) => circuits::relational::compile_graph(&spec.id, p)?, Circuit::RelationReach(p) => circuits::relational::compile_reach(&spec.id, p)?, }; @@ -143,6 +151,7 @@ pub struct Builder { body: Vec, tensors: Vec, external: Vec, + state: Vec, block: String, } @@ -155,6 +164,7 @@ impl Builder { body: Vec::new(), tensors: Vec::new(), external: Vec::new(), + state: Vec::new(), block: "main".into(), } } @@ -185,6 +195,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}"); @@ -236,7 +252,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 53bb97d..5ca4639 100644 --- a/crates/zerolm-wsp/src/lib.rs +++ b/crates/zerolm-wsp/src/lib.rs @@ -12,7 +12,7 @@ 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); 7] = [ +pub const SPECS: [(&str, &str); 11] = [ ("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")), @@ -20,6 +20,10 @@ pub const SPECS: [(&str, &str); 7] = [ ("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")), + ("value-match.json", include_str!("../../../model/prog/value-match.json")), + ("fast-recall.json", include_str!("../../../model/prog/fast-recall.json")), + ("fast-write.json", include_str!("../../../model/prog/fast-write.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 fbeba8e..e64b770 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: 1,343 of 1,343 bit-exact (also the encoder runs made while the world is taught). 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. 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 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: 1,343 of 1,343 bit-exact (also the encoder runs made while the world is taught). 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, compared at write time, and written to and recalled from fast weights by four more compiled circuits (`value-encode`, `value-match`, `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 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`. ## 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..9d8059d 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 written by the `value-encode` circuit (byte one-hot times $E$), and at write time the `value-match` circuit decodes a stored row this way and compares it byte by byte with the new value, which tells an unchanged fact from a contradiction. ### Accept rule @@ -111,7 +111,7 @@ In the circuit this is the product of four $\{0, 1\}$ gates, multiplied into the **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. -### 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 d90b3a7..8fd6dc0 100644 --- a/docs/neural-isa.md +++ b/docs/neural-isa.md @@ -76,7 +76,7 @@ The header is `{"program": , "tensors": [{"name", "rows", "cols"}, ...] |---|---| | 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 | +| memory reference parity | 400 memory queries (taught keys and unknown keys) through `memory-encode` and `memory-read`, plus a short session of new, repeated, and contradicting facts and fast associations through `value-encode`, `value-match`, `fast-write` (including its final `memory.fast` state), and `fast-recall`: 868 runs, 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 (1,343 runs, all bit-exact) | | 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** | @@ -93,6 +93,17 @@ 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`, `value-match.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. +- `value-match`: nearest-code decoding of a stored row (the `memory-read` cleanup: $\arg\max_b 2x\cdot E_b - \lVert E_b\rVert^2$, bytes after the first NUL zeroed, top-1 minus top-2 margin), then `same` $= 1 - [\max_p |\hat b_p - b_p| \ge 1/2]$. +- `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). + +The host copies bytes into input rows and branches only on the circuits' outputs (`same`, the decoded bytes); those branches are memory write bookkeeping, still listed as pending. + ## 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)$). 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 295c5ea..e66aa24 100644 --- a/model/prog/host-audit.json +++ b/model/prog/host-audit.json @@ -11,6 +11,8 @@ "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/value_codec.rs", + "crates/zerolm-memory/src/value_circuits.rs", "crates/zerolm-memory/src/read_circuit.rs", "crates/zerolm-reasoner/src/circuits.rs" ], @@ -34,7 +36,6 @@ {"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"}, {"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, collecting counts) is Rust control flow; the graph search, number encoding, and number cleanup run 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-memory/src/persistent_memory.rs", "reason": "write-time bookkeeping (slot choice, status, supersession, the key word-count limit) is host code (an update rule); value encoding, the stored-value comparison, and W_FAST writes and recalls run in circuits, but the branches on their outputs and the slot bookkeeping are Rust"} ] } diff --git a/model/prog/pinned.json b/model/prog/pinned.json index 1dc7c0f..1c1ca5e 100644 --- a/model/prog/pinned.json +++ b/model/prog/pinned.json @@ -2,11 +2,15 @@ "version": 1, "circuits": { "atom-encode": "94a9df214e0da88cbb3b3a59bebe001073ec75fd133e4c36a9ec57ac033ded0e", + "fast-recall": "0653978c84ac08c5cfadc1e4a48cf0e73a90bf92d8df139970b36ff8afd27723", + "fast-write": "66d4e9f0c7ae3fb08cc470c033f6c3c81ac057344579864797c7bac0722b640a", "memory-encode": "99b405aa6bd6a3beaf5f8e87f425a24721e0817f18807408f6ffaae5a8207feb", "memory-read": "05a51725faae1cf9282815f82417a3976b5034937bdf7f7ee8449104659d475d", "number-decode": "faf56ddfb8f262aa26784a5da5b9fd40f90f46afbd2202d7e9082dc53ce00aee", "number-encode": "bb850fbef9db6f31b63f71ae16e304842f6d2d30ab4aa0348d1d37842ac78c0d", "relation-graph": "efefcd319d2f39c8366a3491c1505cbfa8e05287be3293a57fca586ad668bb51", - "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee" + "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee", + "value-encode": "e6240156a25968a4289f20d748a4cf6ed1e65fc9ca59380ed0a468661a170c01", + "value-match": "06df6685524e9f3a9d8752d6904a68caf63d6fa44f49e38cfac236cb62ac21f7" } } 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/prog/value-match.json b/model/prog/value-match.json new file mode 100644 index 0000000..3f2932e --- /dev/null +++ b/model/prog/value-match.json @@ -0,0 +1,12 @@ +{ + "id": "value-match", + "version": 1, + "seed_entry": "value-codec-circuits", + "description": "Memory value comparison compiled to the neural ISA: a stored value row is decoded by nearest-code cleanup against core.embed (bytes after the first NUL zeroed, top-1 minus top-2 margin), and same = 1 when every decoded byte equals the candidate value byte. Used at write time to tell an unchanged fact from a contradiction. Parameters equal model/specs/memory.json.", + "circuit": { + "kind": "value_match", + "positions": 48, + "code_rows": 256, + "code_dim": 16 + } +} diff --git a/research/manifest/seed-knowledge.json b/research/manifest/seed-knowledge.json index 64f5876..6bfe0ea 100644 --- a/research/manifest/seed-knowledge.json +++ b/research/manifest/seed-knowledge.json @@ -86,6 +86,13 @@ "source": "crates/zerolm-wsp/src/circuits/number.rs", "derived_from_corpus": false }, + { + "id": "value-codec-circuits", + "kind": "structural_prior", + "description": "Circuits value-encode, value-match, fast-recall, and fast-write (model/prog/value-*.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) and an exact byte comparison (tolerance 1/2); 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": "number-decode-circuit", "kind": "structural_prior", From f5b792921e3675a8d3bda93fdc7f672d5f1b97c9 Mon Sep 17 00:00:00 2001 From: nima Date: Sun, 15 Nov 2026 03:15:35 +0330 Subject: [PATCH 3/6] reasoner: run one-step probes, symbol cleanup, and pronoun resolution through compiled circuits (relation-probe: role selection, binding, bundling, sim >= K - margin over active slots, polarity and role-count filters, and the bundled query stored as a new slot row; symbol-cleanup: codebook cosine, top-1, and a mode-selected threshold for cleanup, same symbol, or pronoun); delete the host probe loop, cleanup scan, and statement bundling; share identical bundles across recorded runs; pin both circuits --- crates/zerolm-reasoner/src/circuits.rs | 211 +++++++++++++++++------ crates/zerolm-reasoner/src/lib.rs | 73 ++------ crates/zerolm-reasoner/src/operators.rs | 8 +- crates/zerolm-reasoner/tests/reasoner.rs | 2 + crates/zerolm-wsp/src/circuits/mod.rs | 1 + crates/zerolm-wsp/src/circuits/probe.rs | 132 ++++++++++++++ crates/zerolm-wsp/src/compiler.rs | 4 + crates/zerolm-wsp/src/lib.rs | 4 +- docs/claims-and-limitations.md | 2 +- docs/neural-isa.md | 11 +- docs/reasoning.md | 2 +- model/prog/host-audit.json | 3 +- model/prog/pinned.json | 2 + model/prog/relation-probe.json | 13 ++ model/prog/symbol-cleanup.json | 13 ++ research/manifest/seed-knowledge.json | 7 + 16 files changed, 368 insertions(+), 120 deletions(-) create mode 100644 crates/zerolm-wsp/src/circuits/probe.rs create mode 100644 model/prog/relation-probe.json create mode 100644 model/prog/symbol-cleanup.json diff --git a/crates/zerolm-reasoner/src/circuits.rs b/crates/zerolm-reasoner/src/circuits.rs index 2d16c11..1039ae5 100644 --- a/crates/zerolm-reasoner/src/circuits.rs +++ b/crates/zerolm-reasoner/src/circuits.rs @@ -12,6 +12,7 @@ use std::collections::BTreeMap; 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::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}; @@ -68,7 +69,7 @@ pub struct Atoms { pub circuit: Compiled, pub table: zerolm_tensor::Matrix, cache: std::cell::RefCell>, - recorder: std::cell::RefCell>>, + recorder: Recorder, dim: usize, } @@ -94,25 +95,8 @@ 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()); - } - for (n, v) in &w { - b = b.weight(n, *v); - } - 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(), - }); - } + 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 @@ -161,7 +145,7 @@ pub struct Numbers { pub digits: zerolm_tensor::Matrix, pub codebook: zerolm_tensor::Matrix, cache: std::cell::RefCell>, - recorder: std::cell::RefCell>>, + recorder: Recorder, dim: usize, } @@ -182,17 +166,7 @@ impl Numbers { } fn run(&self, c: &Compiled, input: (&str, Buf), table: (&str, View<'_>)) -> Outputs { - let mut w: Vec<(&str, View<'_>)> = vec![table]; - w.extend(c.tensors.iter().map(|t| (t.name.as_str(), t.view()))); - let mut b = Bindings::new().input(input.0, input.1.clone()); - for (n, v) in &w { - b = b.weight(n, *v); - } - let out = isa::run(&c.program, b).unwrap_or_else(|e| panic!("{} runs: {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, &w), inputs: vec![(input.0.to_string(), input.1)], outputs: out.clone() }); - } - out + run(c, vec![input], &with_consts(c, vec![table]), &self.recorder) } /// The phasor of an integer from its decimal numeral bytes, computed by @@ -229,6 +203,8 @@ impl Numbers { pub struct Circuits { pub graph: Compiled, pub reach: Compiled, + pub probe: Compiled, + pub cleanup: Compiled, } impl Circuits { @@ -236,6 +212,8 @@ 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"), } } } @@ -257,12 +235,150 @@ 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 }), + } +} + +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` run, as slot lists. +#[derive(Debug, Clone, Default)] +pub struct Probe { + /// The bundled query (the row a new statement is stored as). + pub q: Vec, + pub hits: Vec, + pub hits_pol: Vec, + pub dups: Vec, +} + +/// Which threshold `symbol-cleanup` applies. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CleanupMode { + Cleanup, + Same, + Pronoun, +} + +fn nonzero(b: &Buf) -> Vec { + b.data.iter().enumerate().filter(|(_, &x)| x != 0.0).map(|(i, _)| i).collect() +} + +impl Reasoner { + /// Run `relation-probe` for role-filler `parts` (at least one) over the first `n_slots` + /// slots (`meta_live`: bind the metadata; false binds zeros, which is the + /// retrieval ablation and matches nothing). + pub(crate) fn probe(&self, parts: &[(usize, Hv)], polarity: f32, meta_live: bool) -> Probe { + 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(); + // An empty memory is bound as one all-zero inactive row (it never + // matches), so every tensor has at least one row. + let rows = self.n_slots.max(1); + let zero_meta = vec![0.0f32; rows * field::COUNT]; + let meta = if meta_live && 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)), + ("polarity", Buf::scalar(polarity)), + ]; + let o = self.exec(&self.circuits.probe, inputs, &w); + Probe { q: o.get("q").data.clone(), hits: nonzero(o.get("hits")), hits_pol: nonzero(o.get("hits_pol")), dups: nonzero(o.get("dups")) } + } + + /// Run `symbol-cleanup`: 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 { + if self.n_sym == 0 { + return None; + } + let m = match mode { + CleanupMode::Cleanup => [1.0, 0.0, 0.0], + CleanupMode::Same => [0.0, 1.0, 0.0], + CleanupMode::Pronoun => [0.0, 0.0, 1.0], + }; + let w = with_consts(&self.circuits.cleanup, vec![("codebook", view(&self.codebook, self.n_sym))]); + let o = self.exec(&self.circuits.cleanup, vec![("v", Buf::row(v.data.clone())), ("mode", Buf::row(m.to_vec()))], &w); + (o.scalar("found") == 1.0).then(|| o.scalar("best") as usize) + } +} + /// 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, } @@ -274,38 +390,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 }; - *self.numbers.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.extend(self.numbers.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). diff --git a/crates/zerolm-reasoner/src/lib.rs b/crates/zerolm-reasoner/src/lib.rs index 4797363..8a28127 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}; +use zerolm_engine::binding::{bind, unbind, 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; @@ -293,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; @@ -331,7 +331,7 @@ 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, numbers: circuits::Numbers::new(&cfg), cfg, @@ -388,42 +388,21 @@ 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) + self.symbol_cleanup(v, circuits::CleanupMode::Same) } + /// Nearest codebook entry with cosine >= cleanup_min (`symbol-cleanup`). pub(crate) fn cleanup(&self, v: &Hv) -> Option { - self.cleanup_with(v, self.cfg.cleanup_min) + self.symbol_cleanup(v, circuits::CleanupMode::Cleanup) } /// Index of a name's symbol if it was ever stored. @@ -448,17 +427,10 @@ impl Reasoner { // ---- Probes ---- - /// Slots matching every given role-filler pair (sim >= k - margin). + /// Slots matching every given role-filler pair (sim >= k - margin), by the + /// `relation-probe` circuit. pub(crate) fn matches(&self, parts: &[(usize, Hv)]) -> Vec { - if self.retrieval_ablated { - return Vec::new(); - } - 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() + self.probe(parts, 0.0, !self.retrieval_ablated).hits } pub(crate) fn unbind_slot(&self, slot: usize, role: usize) -> Hv { @@ -499,23 +471,16 @@ impl Reasoner { pub(crate) fn resolve(&self, t: &Term) -> Option<(Hv, bool)> { 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)) - } + // "it": the codebook symbol nearest the discourse state (`symbol-cleanup`, pronoun threshold). + Term::Pronoun => self.symbol_cleanup(&self.state.context, circuits::CleanupMode::Pronoun).map(|i| (self.code(i), true)), } } 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 }); - } + // The probe circuit bundles the statement and finds an identical stored one. + let p = self.probe(parts, polarity, true); + if let Some(&s) = p.dups.first() { + return Ok(Observed::Duplicate { slot: s }); } if self.n_slots == self.cfg.slots { return Err(ReasonerError::Full("relation slots")); @@ -524,7 +489,7 @@ impl Reasoner { self.n_slots += 1; self.clock += 1; let c = self.slots.cols; - self.slots.data[s * c..(s + 1) * c].copy_from_slice(&v.data); + self.slots.data[s * c..(s + 1) * c].copy_from_slice(&p.q); let h = fnv1a(stream.as_bytes()); let m = (1u64 << 22) - 1; let row = &mut self.meta.data[s * field::COUNT..(s + 1) * field::COUNT]; @@ -628,7 +593,7 @@ impl Reasoner { ), ]; // Compiled circuit constants (W_PROG), frozen like the role phasors. - for c in [&fresh.atoms.circuit, &fresh.numbers.encoder, &fresh.numbers.decoder, &fresh.circuits.graph, &fresh.circuits.reach] { + for c in [&fresh.atoms.circuit, &fresh.numbers.encoder, &fresh.numbers.decoder, &fresh.circuits.graph, &fresh.circuits.reach, &fresh.circuits.probe, &fresh.circuits.cleanup] { for p in &c.tensors { t.push(Tensor::new(&c.qualified(&p.name), zerolm_wsp::compiler::GROUP, p.rows, p.cols, p.data.clone())); } diff --git a/crates/zerolm-reasoner/src/operators.rs b/crates/zerolm-reasoner/src/operators.rs index 99587f7..197c2b6 100644 --- a/crates/zerolm-reasoner/src/operators.rs +++ b/crates/zerolm-reasoner/src/operators.rs @@ -41,9 +41,9 @@ use crate::circuits::Reach; 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())]) + self.probe(&[(SUBJ, self.code(from)), (REL, rel.clone())], polarity, !self.retrieval_ablated) + .hits_pol .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() } @@ -257,9 +257,9 @@ impl Reasoner { fn counts(&self, x: usize, thing: &str, depth: usize, visited: &mut BTreeSet) -> Vec<(Decoded, Proof)> { let sym = self.symbol(thing); let mut out: Vec<(Decoded, Proof)> = self - .matches(&[(SUBJ, self.code(x)), (REL, bind(self.rel("has"), &sym))]) + .probe(&[(SUBJ, self.code(x)), (REL, bind(self.rel("has"), &sym))], 1.0, !self.retrieval_ablated) + .hits_pol .into_iter() - .filter(|&s| self.slot_meta(s, field::POLARITY) == 1.0) .map(|s| (self.numbers().decode(&self.unbind_slot(s, QUANT)), Proof::of(vec![s], "direct"))) .collect(); if !self.ops.arithmetic || depth >= self.cfg.max_depth || !visited.insert(x) { diff --git a/crates/zerolm-reasoner/tests/reasoner.rs b/crates/zerolm-reasoner/tests/reasoner.rs index e93681c..d52d3c9 100644 --- a/crates/zerolm-reasoner/tests/reasoner.rs +++ b/crates/zerolm-reasoner/tests/reasoner.rs @@ -176,6 +176,8 @@ fn default_config_compiles_to_the_shipped_pinned_circuits() { (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), ] { 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/mod.rs b/crates/zerolm-wsp/src/circuits/mod.rs index f61096d..f2cf462 100644 --- a/crates/zerolm-wsp/src/circuits/mod.rs +++ b/crates/zerolm-wsp/src/circuits/mod.rs @@ -5,5 +5,6 @@ pub mod atom_encode; pub mod attention_read; pub mod key_encode; pub mod number; +pub mod probe; pub mod relational; pub mod value_codec; diff --git a/crates/zerolm-wsp/src/circuits/probe.rs b/crates/zerolm-wsp/src/circuits/probe.rs new file mode 100644 index 0000000..0d4051a --- /dev/null +++ b/crates/zerolm-wsp/src/circuits/probe.rs @@ -0,0 +1,132 @@ +//! One-step relational probes as circuits: the vector operations the +//! reasoner's operators and its write path are built from. +//! +//! **`relation_probe`**: inputs `fillers` (K x 2d) and `role_sel` (K x 5, +//! one-hot role per filler), and `polarity` (1 x 1). The query is the bundle +//! `q = sum_k role_k (*) filler_k` (output `q`: also the row a new statement +//! is stored as); a slot matches when it is active and +//! `sim(slot, q) >= K - margin` (`sim` = Re / d). Outputs (1 x S): +//! - `hits`: matching slots; +//! - `hits_pol`: matching slots whose polarity equals `polarity`; +//! - `dups`: those whose stored role count also equals K (the same statement). +//! +//! **`symbol_cleanup`**: input `v` (1 x 2d) and `mode` (1 x 3 one-hot: cleanup, +//! same symbol, pronoun). 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. +//! +//! Blocks (units of ablation): probe `query` (role selection), `match` +//! (margin, 1/d), `filter` (metadata selectors); cleanup `cosine` +//! (1/sqrt(d), norm epsilon), `threshold` (the three thresholds). + +use crate::compiler::{Builder, Compiled}; +use crate::circuits::relational::{META_ACTIVE, META_POLARITY, N_ROLES}; +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, +} + +#[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; 4] = ["q", "hits", "hits_pol", "dups"]; +pub const CLEANUP_OUTPUTS: [&str; 3] = ["best", "cos", "found"]; +/// `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("polarity", Some(1), 1); + 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("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.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"); + 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.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.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"); + Ok(b.finish(&CLEANUP_OUTPUTS)) +} diff --git a/crates/zerolm-wsp/src/compiler.rs b/crates/zerolm-wsp/src/compiler.rs index c0fd13e..023e1d7 100644 --- a/crates/zerolm-wsp/src/compiler.rs +++ b/crates/zerolm-wsp/src/compiler.rs @@ -39,6 +39,8 @@ pub enum Circuit { ValueMatch(circuits::value_codec::CodecParams), FastRecall(circuits::value_codec::FastParams), FastWrite(circuits::value_codec::FastParams), + RelationProbe(circuits::probe::ProbeParams), + SymbolCleanup(circuits::probe::CleanupParams), RelationGraph(circuits::relational::Params), RelationReach(circuits::relational::Params), } @@ -131,6 +133,8 @@ pub fn compile(spec: &Spec) -> Result { Circuit::ValueMatch(p) => circuits::value_codec::compile_match(&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::RelationGraph(p) => circuits::relational::compile_graph(&spec.id, p)?, Circuit::RelationReach(p) => circuits::relational::compile_reach(&spec.id, p)?, }; diff --git a/crates/zerolm-wsp/src/lib.rs b/crates/zerolm-wsp/src/lib.rs index 5ca4639..b3d1577 100644 --- a/crates/zerolm-wsp/src/lib.rs +++ b/crates/zerolm-wsp/src/lib.rs @@ -12,7 +12,7 @@ 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); 11] = [ +pub const SPECS: [(&str, &str); 13] = [ ("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")), @@ -24,6 +24,8 @@ pub const SPECS: [(&str, &str); 11] = [ ("value-match.json", include_str!("../../../model/prog/value-match.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")), ]; /// `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 e64b770..459700a 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: 1,343 of 1,343 bit-exact (also the encoder runs made while the world is taught). 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, compared at write time, and written to and recalled from fast weights by four more compiled circuits (`value-encode`, `value-match`, `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 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: 7,783 of 7,783 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, compared at write time, and written to and recalled from fast weights by four more compiled circuits (`value-encode`, `value-match`, `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 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`. ## 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/neural-isa.md b/docs/neural-isa.md index 8fd6dc0..e5ec553 100644 --- a/docs/neural-isa.md +++ b/docs/neural-isa.md @@ -77,7 +77,7 @@ The header is `{"program": , "tensors": [{"name", "rows", "cols"}, ...] | 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) through `memory-encode` and `memory-read`, plus a short session of new, repeated, and contradicting facts and fast associations through `value-encode`, `value-match`, `fast-write` (including its final `memory.fast` state), and `fast-recall`: 868 runs, 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 (1,343 runs, all bit-exact) | +| 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 (7,783 runs, all bit-exact) | | 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. @@ -104,6 +104,15 @@ Two different names collide only if all three hashes collide (probability about The host copies bytes into input rows and branches only on the circuits' outputs (`same`, the decoded bytes); those branches are memory write bookkeeping, still listed as pending. +## The probe and cleanup circuits + +`model/prog/relation-probe.json` and `model/prog/symbol-cleanup.json`, compiled by `zerolm_wsp::circuits::probe`. They replace the reasoner's host probe loop, its host cleanup scan, the discourse-state pronoun scan, and the host bundling of a new statement. + +- `relation-probe`: inputs `fillers` ($K \times 2d$) and one-hot `role_sel` ($K \times 5$), and `polarity`. Roles are selected from `core.reasoner.roles` (`matmul`), bound to the fillers, and summed into $q = \sum_k \rho_{r_k} \circledast f_k$ (output `q`, also the row a new statement is stored as). A slot is a hit when it is active and $\mathrm{sim}(\text{slot}, q) \ge K - 0.5$; `hits_pol` keeps hits of the given polarity, and `dups` those that also store exactly $K$ pairs (an identical statement, so the write is refused). With no slots yet, the host binds one all-zero inactive row, so no tensor is empty. +- `symbol-cleanup`: input $v$ and a one-hot `mode`. 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.3 (cleanup of an unbound filler), 0.9 (the same symbol, for interning and lookups), or 0.4 (resolving "it" from the discourse state; a zero state has cosine 0 and never resolves). + +The retrieval ablation binds zero metadata (no slot is active); the binding ablation binds identity role phasors. + ## 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)$). diff --git a/docs/reasoning.md b/docs/reasoning.md index 95d6828..c16cb6e 100644 --- a/docs/reasoning.md +++ b/docs/reasoning.md @@ -50,7 +50,7 @@ The checkpoint uses the memory component's atomic format (`docs/memory-format.md ### 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`; see `docs/neural-isa.md`); every operator below is programmed control flow over those steps. The depth limit is 8. | operator | semantics | |---|---| diff --git a/model/prog/host-audit.json b/model/prog/host-audit.json index e66aa24..9a5a125 100644 --- a/model/prog/host-audit.json +++ b/model/prog/host-audit.json @@ -11,6 +11,7 @@ "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/value_codec.rs", "crates/zerolm-memory/src/value_circuits.rs", "crates/zerolm-memory/src/read_circuit.rs", @@ -35,7 +36,7 @@ "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; it also converts numerals to integers, which the host formats back to decimal bytes for the number-encode circuit"}, {"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, collecting counts) is Rust control flow; the graph search, number encoding, and number cleanup run 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-reasoner/src/lib.rs", "reason": "one-step probes, symbol cleanup, and pronoun resolution now run in the relation-probe and symbol-cleanup circuits, and new slot rows are the probe circuit's bundled query; what remains is write bookkeeping (adding a new symbol to the codebook, slot allocation and metadata) and the mapping from parsed structures to role-filler parts"}, {"file": "crates/zerolm-memory/src/persistent_memory.rs", "reason": "write-time bookkeeping (slot choice, status, supersession, the key word-count limit) is host code (an update rule); value encoding, the stored-value comparison, and W_FAST writes and recalls run in circuits, but the branches on their outputs and the slot bookkeeping are Rust"} ] } diff --git a/model/prog/pinned.json b/model/prog/pinned.json index 1c1ca5e..ab9a55e 100644 --- a/model/prog/pinned.json +++ b/model/prog/pinned.json @@ -9,7 +9,9 @@ "number-decode": "faf56ddfb8f262aa26784a5da5b9fd40f90f46afbd2202d7e9082dc53ce00aee", "number-encode": "bb850fbef9db6f31b63f71ae16e304842f6d2d30ab4aa0348d1d37842ac78c0d", "relation-graph": "efefcd319d2f39c8366a3491c1505cbfa8e05287be3293a57fca586ad668bb51", + "relation-probe": "bf99f34a4122e34d48f563ab8cf10110f84ab4a1a0462c9794211bf669cfd7c4", "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee", + "symbol-cleanup": "6fe3b1e4011cea3a3ec12f99238d6b9254ffacbc29de23ab2bb575f7172ca201", "value-encode": "e6240156a25968a4289f20d748a4cf6ed1e65fc9ca59380ed0a468661a170c01", "value-match": "06df6685524e9f3a9d8752d6904a68caf63d6fa44f49e38cfac236cb62ac21f7" } diff --git a/model/prog/relation-probe.json b/model/prog/relation-probe.json new file mode 100644 index 0000000..d775ed2 --- /dev/null +++ b/model/prog/relation-probe.json @@ -0,0 +1,13 @@ +{ + "id": "relation-probe", + "version": 1, + "seed_entry": "relation-probe-circuits", + "description": "One-step relational probe compiled to the neural ISA: role-filler pairs (fillers and one-hot role selectors) are bound with core.reasoner.roles and bundled into a query; active slots of memory.relations.slots with sim >= K - margin are hits, filtered by polarity and by the stored role count (write-time duplicates). dim, match_margin 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 + } +} diff --git a/model/prog/symbol-cleanup.json b/model/prog/symbol-cleanup.json new file mode 100644 index 0000000..f43a148 --- /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 unbound fillers, symbol interning, 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/research/manifest/seed-knowledge.json b/research/manifest/seed-knowledge.json index 6bfe0ea..dabbae5 100644 --- a/research/manifest/seed-knowledge.json +++ b/research/manifest/seed-knowledge.json @@ -93,6 +93,13 @@ "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": "number-decode-circuit", "kind": "structural_prior", From ed65835b43752955de129ad809a7a7a4fa5f8f40 Mon Sep 17 00:00:00 2001 From: nima Date: Sun, 15 Nov 2026 11:00:27 +0330 Subject: [PATCH 4/6] reasoner: decide every operator with compiled circuits instead of Rust control flow (relation-join: shortest negative edge between source and target sets; relation-levels: inheritance by breadth-first isa levels with a latched first decision; relation-rules: backward chaining over in-scope conditionals; relation-counts: counts along more chains by phasor binding and codebook cleanup, one value or a contradiction; relation-verdict: yes/no/contradiction/unknown and supporting premises); operators.rs only sequences circuits by question form and labels proofs; pin the five circuits; remove operators.rs from the pending host audit --- AGENTS.md | 2 +- README.md | 2 +- crates/zerolm-reasoner/src/circuits.rs | 155 ++++-- crates/zerolm-reasoner/src/lib.rs | 21 +- crates/zerolm-reasoner/src/operators.rs | 355 +++++-------- crates/zerolm-reasoner/tests/reasoner.rs | 5 + crates/zerolm-wsp/src/circuits/decide.rs | 529 +++++++++++++++++++ crates/zerolm-wsp/src/circuits/mod.rs | 1 + crates/zerolm-wsp/src/circuits/relational.rs | 10 +- crates/zerolm-wsp/src/compiler.rs | 10 + crates/zerolm-wsp/src/lib.rs | 7 +- docs/claims-and-limitations.md | 2 +- docs/neural-isa.md | 18 +- docs/reasoning.md | 2 +- model/prog/host-audit.json | 3 +- model/prog/pinned.json | 5 + model/prog/relation-counts.json | 18 + model/prog/relation-join.json | 15 + model/prog/relation-levels.json | 15 + model/prog/relation-rules.json | 15 + model/prog/relation-verdict.json | 15 + research/manifest/seed-knowledge.json | 7 + 22 files changed, 906 insertions(+), 306 deletions(-) create mode 100644 crates/zerolm-wsp/src/circuits/decide.rs create mode 100644 model/prog/relation-counts.json create mode 100644 model/prog/relation-join.json create mode 100644 model/prog/relation-levels.json create mode 100644 model/prog/relation-rules.json create mode 100644 model/prog/relation-verdict.json 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-reasoner/src/circuits.rs b/crates/zerolm-reasoner/src/circuits.rs index 1039ae5..b0ef495 100644 --- a/crates/zerolm-reasoner/src/circuits.rs +++ b/crates/zerolm-reasoner/src/circuits.rs @@ -12,6 +12,7 @@ use std::collections::BTreeMap; 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}; @@ -39,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). @@ -205,6 +226,11 @@ pub struct Circuits { 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 { @@ -214,8 +240,18 @@ impl Circuits { 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. @@ -228,9 +264,6 @@ 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]) } @@ -433,51 +466,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() })); + if c.id == REACH_ID { + w.push(("pol", g.pos.view())); + } else { + w.push(("pos", g.pos.view())); + w.push(("neg", g.neg.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()); - } - } - 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) } } diff --git a/crates/zerolm-reasoner/src/lib.rs b/crates/zerolm-reasoner/src/lib.rs index 8a28127..c7ef20b 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, unbind, Hv}; +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; @@ -400,11 +400,6 @@ impl Reasoner { self.symbol_cleanup(v, circuits::CleanupMode::Same) } - /// Nearest codebook entry with cosine >= cleanup_min (`symbol-cleanup`). - pub(crate) fn cleanup(&self, v: &Hv) -> Option { - self.symbol_cleanup(v, circuits::CleanupMode::Cleanup) - } - /// Index of a name's symbol if it was ever stored. pub fn known(&self, name: &str) -> Option { self.find_symbol(&self.symbol(name)) @@ -425,18 +420,6 @@ impl Reasoner { Ok(i) } - // ---- Probes ---- - - /// Slots matching every given role-filler pair (sim >= k - margin), by the - /// `relation-probe` circuit. - pub(crate) fn matches(&self, parts: &[(usize, Hv)]) -> Vec { - self.probe(parts, 0.0, !self.retrieval_ablated).hits - } - - 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)) - } - // ---- Language interface ---- /// Relation vector and role-filler parts of a predicate for subject `s`. @@ -593,7 +576,7 @@ impl Reasoner { ), ]; // Compiled circuit constants (W_PROG), frozen like the role phasors. - for c in [&fresh.atoms.circuit, &fresh.numbers.encoder, &fresh.numbers.decoder, &fresh.circuits.graph, &fresh.circuits.reach, &fresh.circuits.probe, &fresh.circuits.cleanup] { + 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())); } diff --git a/crates/zerolm-reasoner/src/operators.rs b/crates/zerolm-reasoner/src/operators.rs index 197c2b6..a7158d6 100644 --- a/crates/zerolm-reasoner/src/operators.rs +++ b/crates/zerolm-reasoner/src/operators.rs @@ -1,59 +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::circuits::Decoded; -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.probe(&[(SUBJ, self.code(from)), (REL, rel.clone())], polarity, !self.retrieval_ablated) - .hits_pol - .into_iter() - .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(); @@ -62,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), @@ -99,198 +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` (decoded by the compiled - /// `number-decode` circuit, with its clean vector), with its proof. - fn counts(&self, x: usize, thing: &str, depth: usize, visited: &mut BTreeSet) -> Vec<(Decoded, Proof)> { + fn count_answer(&self, x: &Hv, thing: &str) -> Answer { let sym = self.symbol(thing); - let mut out: Vec<(Decoded, Proof)> = self - .probe(&[(SUBJ, self.code(x)), (REL, bind(self.rel("has"), &sym))], 1.0, !self.retrieval_ablated) - .hits_pol - .into_iter() - .map(|s| (self.numbers().decode(&self.unbind_slot(s, QUANT)), 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.numbers().decode(&self.unbind_slot(s, QUANT)); - for (c, p) in self.counts(other, thing, depth + 1, visited) { - // Addition is binding of clean fractional-power number vectors. - let n = self.numbers().decode(&bind(&c.clean, &delta.clean)); - 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.value).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 d52d3c9..99011f3 100644 --- a/crates/zerolm-reasoner/tests/reasoner.rs +++ b/crates/zerolm-reasoner/tests/reasoner.rs @@ -178,6 +178,11 @@ fn default_config_compiles_to_the_shipped_pinned_circuits() { (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/mod.rs b/crates/zerolm-wsp/src/circuits/mod.rs index f2cf462..d6f2db1 100644 --- a/crates/zerolm-wsp/src/circuits/mod.rs +++ b/crates/zerolm-wsp/src/circuits/mod.rs @@ -2,6 +2,7 @@ //! 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 number; 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/compiler.rs b/crates/zerolm-wsp/src/compiler.rs index 023e1d7..8d1b0f2 100644 --- a/crates/zerolm-wsp/src/compiler.rs +++ b/crates/zerolm-wsp/src/compiler.rs @@ -41,6 +41,11 @@ pub enum Circuit { 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), RelationGraph(circuits::relational::Params), RelationReach(circuits::relational::Params), } @@ -135,6 +140,11 @@ pub fn compile(spec: &Spec) -> Result { 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::RelationGraph(p) => circuits::relational::compile_graph(&spec.id, p)?, Circuit::RelationReach(p) => circuits::relational::compile_reach(&spec.id, p)?, }; diff --git a/crates/zerolm-wsp/src/lib.rs b/crates/zerolm-wsp/src/lib.rs index b3d1577..c0392ff 100644 --- a/crates/zerolm-wsp/src/lib.rs +++ b/crates/zerolm-wsp/src/lib.rs @@ -12,7 +12,7 @@ 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); 13] = [ +pub const SPECS: [(&str, &str); 18] = [ ("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")), @@ -26,6 +26,11 @@ pub const SPECS: [(&str, &str); 13] = [ ("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")), ]; /// `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 459700a..d35c4f7 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: 7,783 of 7,783 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, compared at write time, and written to and recalled from fast weights by four more compiled circuits (`value-encode`, `value-match`, `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 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, compared at write time, and written to and recalled from fast weights by four more compiled circuits (`value-encode`, `value-match`, `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 bounded-language parser and the reasoner and memory write bookkeeping are 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/neural-isa.md b/docs/neural-isa.md index e5ec553..e29f38b 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) through `memory-encode` and `memory-read`, plus a short session of new, repeated, and contradicting facts and fast associations through `value-encode`, `value-match`, `fast-write` (including its final `memory.fast` state), and `fast-recall`: 868 runs, 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 (7,783 runs, all bit-exact) | +| 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. @@ -113,6 +113,20 @@ The host copies bytes into input rows and branches only on the circuits' outputs The retrieval ablation binds zero metadata (no slot is active); the binding ablation binds identity role phasors. +## 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)$). diff --git a/docs/reasoning.md b/docs/reasoning.md index c16cb6e..2883266 100644 --- a/docs/reasoning.md +++ b/docs/reasoning.md @@ -50,7 +50,7 @@ The checkpoint uses the memory component's atomic format (`docs/memory-format.md ### 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). The probe and the cleanup run as compiled circuits (`relation-probe`, `symbol-cleanup`; see `docs/neural-isa.md`); 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 | |---|---| diff --git a/model/prog/host-audit.json b/model/prog/host-audit.json index 9a5a125..cf353ab 100644 --- a/model/prog/host-audit.json +++ b/model/prog/host-audit.json @@ -12,6 +12,8 @@ "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-memory/src/value_circuits.rs", "crates/zerolm-memory/src/read_circuit.rs", @@ -35,7 +37,6 @@ ], "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; it also converts numerals to integers, which the host formats back to decimal bytes for the number-encode circuit"}, - {"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, collecting counts) is Rust control flow; the graph search, number encoding, and number cleanup run in circuits"}, {"file": "crates/zerolm-reasoner/src/lib.rs", "reason": "one-step probes, symbol cleanup, and pronoun resolution now run in the relation-probe and symbol-cleanup circuits, and new slot rows are the probe circuit's bundled query; what remains is write bookkeeping (adding a new symbol to the codebook, slot allocation and metadata) and the mapping from parsed structures to role-filler parts"}, {"file": "crates/zerolm-memory/src/persistent_memory.rs", "reason": "write-time bookkeeping (slot choice, status, supersession, the key word-count limit) is host code (an update rule); value encoding, the stored-value comparison, and W_FAST writes and recalls run in circuits, but the branches on their outputs and the slot bookkeeping are Rust"} ] diff --git a/model/prog/pinned.json b/model/prog/pinned.json index ab9a55e..3748114 100644 --- a/model/prog/pinned.json +++ b/model/prog/pinned.json @@ -8,9 +8,14 @@ "memory-read": "05a51725faae1cf9282815f82417a3976b5034937bdf7f7ee8449104659d475d", "number-decode": "faf56ddfb8f262aa26784a5da5b9fd40f90f46afbd2202d7e9082dc53ce00aee", "number-encode": "bb850fbef9db6f31b63f71ae16e304842f6d2d30ab4aa0348d1d37842ac78c0d", + "relation-counts": "6d2c70ec564839c47b9032a6848a2a7dc5590b909a376c24c1d147e26fa3b5a8", "relation-graph": "efefcd319d2f39c8366a3491c1505cbfa8e05287be3293a57fca586ad668bb51", + "relation-join": "ccedd0ce9fbd4956617da47eea16a80d823b508dea9f2b8034960db3a6efdec7", + "relation-levels": "dc6a19dd8edd9b3f98217269832e969cc43f3dbb106ce0e48322c4558f373338", "relation-probe": "bf99f34a4122e34d48f563ab8cf10110f84ab4a1a0462c9794211bf669cfd7c4", "relation-reach": "7f6ed8d82ffc0d10f9a63bb897b874d53c54eec33960a4c68122449a1438c6ee", + "relation-rules": "3b6350b4642c4b8a7210f646474f4bf4998743738d9481da9dcb7650b1d4eef8", + "relation-verdict": "698ebf8ddbcc1ef9ef51d8e5932a26ea7ad1f77d97b92e5b0fa930b86a09a16c", "symbol-cleanup": "6fe3b1e4011cea3a3ec12f99238d6b9254ffacbc29de23ab2bb575f7172ca201", "value-encode": "e6240156a25968a4289f20d748a4cf6ed1e65fc9ca59380ed0a468661a170c01", "value-match": "06df6685524e9f3a9d8752d6904a68caf63d6fa44f49e38cfac236cb62ac21f7" 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-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/research/manifest/seed-knowledge.json b/research/manifest/seed-knowledge.json index dabbae5..2254eb4 100644 --- a/research/manifest/seed-knowledge.json +++ b/research/manifest/seed-knowledge.json @@ -100,6 +100,13 @@ "source": "crates/zerolm-wsp/src/circuits/probe.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", From 370810a9aaa7e7cfb842089efc2487018012e85d Mon Sep 17 00:00:00 2001 From: nima Date: Sun, 15 Nov 2026 12:30:51 +0330 Subject: [PATCH 5/6] reasoner: decide writes with compiled circuits (relation-probe now decides whether a statement is stored: subject resolved and not a duplicate; it outputs the slot row, the metadata row, and the next discourse state; symbol-cleanup gains a gate and outputs the codebook row of a new symbol); the host only appends circuit rows at its counters; remove lib.rs from the pending host audit; re-pin both circuits --- crates/zerolm-reasoner/src/circuits.rs | 87 +++++++++++-------- crates/zerolm-reasoner/src/lib.rs | 106 ++++++++++++++---------- crates/zerolm-wsp/src/circuits/probe.rs | 79 +++++++++++++----- docs/claims-and-limitations.md | 2 +- docs/neural-isa.md | 10 +-- model/prog/host-audit.json | 3 +- model/prog/pinned.json | 4 +- model/prog/relation-probe.json | 5 +- model/prog/symbol-cleanup.json | 2 +- 9 files changed, 188 insertions(+), 110 deletions(-) diff --git a/crates/zerolm-reasoner/src/circuits.rs b/crates/zerolm-reasoner/src/circuits.rs index b0ef495..226f2c3 100644 --- a/crates/zerolm-reasoner/src/circuits.rs +++ b/crates/zerolm-reasoner/src/circuits.rs @@ -328,7 +328,7 @@ pub fn probe_spec(cfg: &ReasonerConfig) -> Spec { 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 }), + 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 }), } } @@ -342,66 +342,87 @@ pub fn cleanup_spec(cfg: &ReasonerConfig) -> Spec { } } -/// Outputs of one `relation-probe` run, as slot lists. -#[derive(Debug, Clone, Default)] -pub struct Probe { - /// The bundled query (the row a new statement is stored as). - pub q: Vec, - pub hits: Vec, - pub hits_pol: Vec, - pub dups: Vec, +/// 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 { - Cleanup, Same, Pronoun, } -fn nonzero(b: &Buf) -> Vec { - b.data.iter().enumerate().filter(|(_, &x)| x != 0.0).map(|(i, _)| i).collect() -} - impl Reasoner { - /// Run `relation-probe` for role-filler `parts` (at least one) over the first `n_slots` - /// slots (`meta_live`: bind the metadata; false binds zeros, which is the - /// retrieval ablation and matches nothing). - pub(crate) fn probe(&self, parts: &[(usize, Hv)], polarity: f32, meta_live: bool) -> Probe { + /// 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(); - // An empty memory is bound as one all-zero inactive row (it never - // matches), so every tensor has at least one row. let rows = self.n_slots.max(1); let zero_meta = vec![0.0f32; rows * field::COUNT]; - let meta = if meta_live && self.n_slots > 0 { view(&self.meta, rows) } else { View::new(rows, field::COUNT, &zero_meta) }; + 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)), - ("polarity", Buf::scalar(polarity)), + ("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); - Probe { q: o.get("q").data.clone(), hits: nonzero(o.get("hits")), hits_pol: nonzero(o.get("hits_pol")), dups: nonzero(o.get("dups")) } + 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`: 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 { - if self.n_sym == 0 { - return None; - } + /// 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::Cleanup => [1.0, 0.0, 0.0], CleanupMode::Same => [0.0, 1.0, 0.0], CleanupMode::Pronoun => [0.0, 0.0, 1.0], }; - let w = with_consts(&self.circuits.cleanup, vec![("codebook", view(&self.codebook, self.n_sym))]); - let o = self.exec(&self.circuits.cleanup, vec![("v", Buf::row(v.data.clone())), ("mode", Buf::row(m.to_vec()))], &w); - (o.scalar("found") == 1.0).then(|| o.scalar("best") as usize) + 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 } } diff --git a/crates/zerolm-reasoner/src/lib.rs b/crates/zerolm-reasoner/src/lib.rs index c7ef20b..9793964 100644 --- a/crates/zerolm-reasoner/src/lib.rs +++ b/crates/zerolm-reasoner/src/lib.rs @@ -405,19 +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")); + /// 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 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) + Ok(()) } // ---- Language interface ---- @@ -450,42 +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::Name(n) => (self.symbol(n), false, 1.0), // "it": the codebook symbol nearest the discourse state (`symbol-cleanup`, pronoun threshold). - Term::Pronoun => self.symbol_cleanup(&self.state.context, circuits::CleanupMode::Pronoun).map(|i| (self.code(i), true)), + 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 { - // The probe circuit bundles the statement and finds an identical stored one. - let p = self.probe(parts, polarity, true); - if let Some(&s) = p.dups.first() { - 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(&p.q); - 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)); @@ -503,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), @@ -531,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. diff --git a/crates/zerolm-wsp/src/circuits/probe.rs b/crates/zerolm-wsp/src/circuits/probe.rs index 0d4051a..a6386c3 100644 --- a/crates/zerolm-wsp/src/circuits/probe.rs +++ b/crates/zerolm-wsp/src/circuits/probe.rs @@ -1,26 +1,35 @@ -//! One-step relational probes as circuits: the vector operations the -//! reasoner's operators and its write path are built from. +//! The reasoner's write decisions as circuits: storing a statement and +//! adding a symbol to the codebook. //! -//! **`relation_probe`**: inputs `fillers` (K x 2d) and `role_sel` (K x 5, -//! one-hot role per filler), and `polarity` (1 x 1). The query is the bundle -//! `q = sum_k role_k (*) filler_k` (output `q`: also the row a new statement -//! is stored as); a slot matches when it is active and -//! `sim(slot, q) >= K - margin` (`sim` = Re / d). Outputs (1 x S): -//! - `hits`: matching slots; -//! - `hits_pol`: matching slots whose polarity equals `polarity`; -//! - `dups`: those whose stored role count also equals K (the same statement). +//! **`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`**: input `v` (1 x 2d) and `mode` (1 x 3 one-hot: cleanup, -//! same symbol, pronoun). 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. +//! **`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); cleanup `cosine` -//! (1/sqrt(d), norm epsilon), `threshold` (the three thresholds). +//! (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 crate::circuits::relational::{META_ACTIVE, META_POLARITY, N_ROLES}; use serde::{Deserialize, Serialize}; use zerolm_engine::isa::{Axis, GateFn::*, Instr, IsaError, NormKind, ReduceFn}; @@ -32,6 +41,8 @@ pub struct ProbeParams { /// 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)] @@ -43,8 +54,8 @@ pub struct CleanupParams { pub pronoun_min: f32, } -pub const PROBE_OUTPUTS: [&str; 4] = ["q", "hits", "hits_pol", "dups"]; -pub const CLEANUP_OUTPUTS: [&str; 3] = ["best", "cos", "found"]; +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; @@ -70,7 +81,9 @@ pub fn compile_probe(id: &str, p: &ProbeParams) -> Result { let mut b = Builder::new(id); b.input_dyn("fillers", None, Some(d2)); b.input_dyn("role_sel", None, Some(N_ROLES)); - b.input("polarity", Some(1), 1); + 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); @@ -90,6 +103,7 @@ pub fn compile_probe(id: &str, p: &ProbeParams) -> Result { 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)); @@ -97,12 +111,32 @@ pub fn compile_probe(id: &str, p: &ProbeParams) -> Result { 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)) } @@ -114,6 +148,7 @@ pub fn compile_cleanup(id: &str, p: &CleanupParams) -> Result Result