aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / allen-reflex / src / features.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Observable features of one request. All three scorers (hypervector, nearest exemplar, |
| 17 | //! perceptron) read exactly this list, so a comparison between them compares only the scorer. |
| 18 | |
| 19 | use crate::action::Action; |
| 20 | use crate::dialog::{DialogState, Pending}; |
| 21 | use crate::lexicon::{ |
| 22 | is_in, longest, skip_polite, Concept, CONCEPTS, FILLER, N_CONCEPTS, TEXT_FRAMES, VACUOUS, |
| 23 | }; |
| 24 | use crate::segment::Clause; |
| 25 | use crate::text::{fnv, gap, Token}; |
| 26 | use alloc::vec::Vec; |
| 27 | |
| 28 | /// A symbol: the atom a feature is built from. |
| 29 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 30 | pub enum Sym { |
| 31 | /// word stem (hash) |
| 32 | Word(u64), |
| 33 | /// one character of a character n-gram |
| 34 | Char(u8), |
| 35 | /// concept class |
| 36 | Concept(u8), |
| 37 | /// dialog context value |
| 38 | Ctx(u8), |
| 39 | /// shape value (length bucket, question, argument content) |
| 40 | Shape(u8), |
| 41 | /// the head word of the request (hash) |
| 42 | Lead(u64), |
| 43 | } |
| 44 | |
| 45 | impl Sym { |
| 46 | /// (family tag, value) used to seed the item vector. |
| 47 | pub fn family(self) -> (u64, u64) { |
| 48 | match self { |
| 49 | Sym::Word(h) => (1, h), |
| 50 | Sym::Char(c) => (2, c as u64), |
| 51 | Sym::Concept(c) => (3, c as u64), |
| 52 | Sym::Ctx(c) => (4, c as u64), |
| 53 | Sym::Shape(c) => (5, c as u64), |
| 54 | Sym::Lead(h) => (6, h), |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Feature channels, each with its own weight. |
| 60 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 61 | #[repr(u8)] |
| 62 | pub enum Channel { |
| 63 | /// word unigram |
| 64 | Word = 0, |
| 65 | /// word bigram (ordered) |
| 66 | Bigram, |
| 67 | /// character 3- and 4-grams inside a word |
| 68 | Chars, |
| 69 | /// concept class |
| 70 | Concept, |
| 71 | /// head word and head bigram |
| 72 | Lead, |
| 73 | /// dialog context (pending state, previous decision) |
| 74 | Ctx, |
| 75 | /// context bound to concept or head word |
| 76 | CtxConj, |
| 77 | /// shape |
| 78 | Shape, |
| 79 | } |
| 80 | |
| 81 | /// Number of channels. |
| 82 | pub const N_CHANNELS: usize = 8; |
| 83 | |
| 84 | /// One feature: an ordered tuple of up to 4 symbols, bound together, with a weight. |
| 85 | #[derive(Clone, Copy, Debug)] |
| 86 | pub struct Feat { |
| 87 | /// symbols, `len` of them used |
| 88 | pub syms: [Sym; 4], |
| 89 | /// used symbols |
| 90 | pub len: u8, |
| 91 | /// channel |
| 92 | pub ch: Channel, |
| 93 | /// weight (from the config) |
| 94 | pub w: u16, |
| 95 | } |
| 96 | |
| 97 | impl Feat { |
| 98 | /// Stable 64-bit key (sparse scorers). |
| 99 | pub fn key(&self) -> u64 { |
| 100 | let mut b = [0u8; 4 * 9 + 1]; |
| 101 | b[0] = self.ch as u8; |
| 102 | for k in 0..self.len as usize { |
| 103 | let (f, v) = self.syms[k].family(); |
| 104 | b[1 + k * 9] = f as u8; |
| 105 | b[2 + k * 9..10 + k * 9].copy_from_slice(&v.to_le_bytes()); |
| 106 | } |
| 107 | fnv(&b[..1 + self.len as usize * 9]) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Bound on features per request (input is bounded, so this is never reached in practice). |
| 112 | pub const MAX_FEATS: usize = 768; |
| 113 | |
| 114 | /// Context symbol values. |
| 115 | fn pend_sym(p: Pending) -> u8 { |
| 116 | match p { |
| 117 | Pending::Unknown => 0, |
| 118 | Pending::Yes => 1, |
| 119 | Pending::No => 2, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// Count of content words after the head frame ("Add a task." has 0, "Add a task: X" has 1+). |
| 124 | pub fn rest_content(toks: &[Token], c: Clause) -> usize { |
| 125 | let lead = skip_polite(toks, c.start, c.end); |
| 126 | let f = longest(toks, lead, TEXT_FRAMES); |
| 127 | toks[core::cmp::min(lead + f, c.end)..c.end] |
| 128 | .iter() |
| 129 | .filter(|t| t.alnum && !is_in(VACUOUS, &t.word) && !is_in(FILLER, &t.word)) |
| 130 | .count() |
| 131 | } |
| 132 | |
| 133 | /// Concepts present in `toks[s..e]` (dedup, as a bitmask). |
| 134 | pub fn concepts(toks: &[Token], s: usize, e: usize) -> u32 { |
| 135 | let mut m = 0u32; |
| 136 | for i in s..e { |
| 137 | for (c, phrases) in CONCEPTS { |
| 138 | if longest(&toks[..e], i, phrases) > 0 { |
| 139 | m |= 1 << (*c as u32); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | m |
| 144 | } |
| 145 | |
| 146 | fn push(v: &mut Vec<Feat>, ch: Channel, w: &[u16; N_CHANNELS], syms: &[Sym]) { |
| 147 | let wt = w[ch as usize]; |
| 148 | if wt == 0 || v.len() >= MAX_FEATS { |
| 149 | return; |
| 150 | } |
| 151 | let mut s = [Sym::Shape(0); 4]; |
| 152 | s[..syms.len()].copy_from_slice(syms); |
| 153 | v.push(Feat { |
| 154 | syms: s, |
| 155 | len: syms.len() as u8, |
| 156 | ch, |
| 157 | w: wt, |
| 158 | }); |
| 159 | } |
| 160 | |
| 161 | /// Extract the features of request `c` with dialog state `d`. |
| 162 | pub fn extract( |
| 163 | toks: &[Token], |
| 164 | c: Clause, |
| 165 | d: &DialogState, |
| 166 | w: &[u16; N_CHANNELS], |
| 167 | ends_q: bool, |
| 168 | ) -> Vec<Feat> { |
| 169 | let mut v = Vec::new(); |
| 170 | let words: Vec<&Token> = toks[c.start..c.end].iter().filter(|t| t.alnum).collect(); |
| 171 | let mut seen: Vec<u64> = Vec::new(); |
| 172 | for t in &words { |
| 173 | let h = fnv(t.stem.as_bytes()); |
| 174 | if !seen.contains(&h) { |
| 175 | seen.push(h); |
| 176 | push(&mut v, Channel::Word, w, &[Sym::Word(h)]); |
| 177 | } |
| 178 | } |
| 179 | for p in words.windows(2) { |
| 180 | let a = fnv(p[0].stem.as_bytes()); |
| 181 | let b = fnv(p[1].stem.as_bytes()); |
| 182 | push(&mut v, Channel::Bigram, w, &[Sym::Word(a), Sym::Word(b)]); |
| 183 | } |
| 184 | for t in &words { |
| 185 | let mut buf = [0u8; 34]; |
| 186 | let wb = t.word.as_bytes(); |
| 187 | let n = core::cmp::min(wb.len(), 32); |
| 188 | buf[0] = b'^'; |
| 189 | buf[1..1 + n].copy_from_slice(&wb[..n]); |
| 190 | buf[1 + n] = b'$'; |
| 191 | let s = &buf[..n + 2]; |
| 192 | for g in [3usize, 4] { |
| 193 | if s.len() < g { |
| 194 | continue; |
| 195 | } |
| 196 | for win in s.windows(g) { |
| 197 | let mut syms = [Sym::Char(0); 4]; |
| 198 | for (k, &ch) in win.iter().enumerate() { |
| 199 | syms[k] = Sym::Char(ch); |
| 200 | } |
| 201 | push(&mut v, Channel::Chars, w, &syms[..g]); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | let cm = concepts(toks, c.start, c.end); |
| 206 | for k in 0..N_CONCEPTS as u8 { |
| 207 | if cm & (1 << k) != 0 { |
| 208 | push(&mut v, Channel::Concept, w, &[Sym::Concept(k)]); |
| 209 | push( |
| 210 | &mut v, |
| 211 | Channel::CtxConj, |
| 212 | w, |
| 213 | &[Sym::Ctx(pend_sym(d.pending)), Sym::Concept(k)], |
| 214 | ); |
| 215 | } |
| 216 | } |
| 217 | let lead = skip_polite(toks, c.start, c.end); |
| 218 | if lead < c.end { |
| 219 | let h = fnv(toks[lead].stem.as_bytes()); |
| 220 | push(&mut v, Channel::Lead, w, &[Sym::Lead(h)]); |
| 221 | push( |
| 222 | &mut v, |
| 223 | Channel::CtxConj, |
| 224 | w, |
| 225 | &[Sym::Ctx(pend_sym(d.pending)), Sym::Lead(h)], |
| 226 | ); |
| 227 | if lead + 1 < c.end { |
| 228 | let h2 = fnv(toks[lead + 1].stem.as_bytes()); |
| 229 | push(&mut v, Channel::Lead, w, &[Sym::Lead(h), Sym::Word(h2)]); |
| 230 | } |
| 231 | } |
| 232 | push(&mut v, Channel::Ctx, w, &[Sym::Ctx(pend_sym(d.pending))]); |
| 233 | let prev = d.prev.map(|a: Action| a.idx() as u8 + 1).unwrap_or(0); |
| 234 | push(&mut v, Channel::Ctx, w, &[Sym::Ctx(16 + prev)]); |
| 235 | let n = words.len(); |
| 236 | let lb = match n { |
| 237 | 0..=1 => 0, |
| 238 | 2 => 1, |
| 239 | 3..=4 => 2, |
| 240 | 5..=8 => 3, |
| 241 | _ => 4, |
| 242 | }; |
| 243 | push(&mut v, Channel::Shape, w, &[Sym::Shape(lb)]); |
| 244 | let q = ends_q |
| 245 | || toks[c.start + 1..c.end] |
| 246 | .iter() |
| 247 | .any(|t| t.gap & gap::QUESTION != 0); |
| 248 | if q { |
| 249 | push(&mut v, Channel::Shape, w, &[Sym::Shape(10)]); |
| 250 | } |
| 251 | let r = match rest_content(toks, c) { |
| 252 | 0 => 20, |
| 253 | 1..=2 => 21, |
| 254 | _ => 22, |
| 255 | }; |
| 256 | push(&mut v, Channel::Shape, w, &[Sym::Shape(r)]); |
| 257 | v |
| 258 | } |
| 259 | |
| 260 | /// Index of a concept for tests and explanations. |
| 261 | pub fn concept_bit(c: Concept) -> u32 { |
| 262 | 1 << (c as u32) |
| 263 | } |