Download src/knowledge.rs from VoidOSS/Cosmic-v0.6-1B-MoE-Preview: direct link, hf CLI and curl.
- Browser
- Download file 3.26 kB
-
https://huggingface.co/VoidOSS/Cosmic-v0.6-1B-MoE-Preview/resolve/main/src/knowledge.rs
- Command line
-
hf download hf://VoidOSS/Cosmic-v0.6-1B-MoE-Preview/src/knowledge.rs
-
curl -L -o knowledge.rs https://huggingface.co/VoidOSS/Cosmic-v0.6-1B-MoE-Preview/resolve/main/src/knowledge.rs
3.26 kB
| //! `knowledge.rs` — Direct-Indexed Surgical Token Archive & Recall Bus. | |
| //! - ZERO weights inside knowledge.rs. | |
| //! - ZERO inverted indexes & ZERO whole-page dumps. | |
| //! - Direct O(1) array lookup `archive[token_id]`: when the model internally dials | |
| //! query parameters (e.g. "when did I last generate Token 5, and what was Token 5 for?"), | |
| //! knowledge.rs returns ONLY that exact minimal record. The model reads it, flushes its | |
| //! temporary scratchpad, and returns `@OK`! | |
| use crate::tokens::META_OK; | |
| pub struct SurgicalRecord { | |
| pub exists: bool, | |
| pub token_id: u16, | |
| pub last_step: usize, | |
| pub occurrences: u32, | |
| pub prev_token_id: u16, // Immediate predecessor (what triggered it) | |
| pub role_vec: Vec<f32>, // Compact learned role/purpose fingerprint of this token | |
| } | |
| pub struct KnowledgeArchive { | |
| pub records: Vec<SurgicalRecord>, // Direct-indexed by token_id (NO inverted index!) | |
| pub total_archived: usize, | |
| pub recall_hits: usize, | |
| pub acks_received: usize, | |
| } | |
| impl KnowledgeArchive { | |
| pub fn new(vocab_size: usize, role_dim: usize) -> Self { | |
| let records = (0..vocab_size) | |
| .map(|id| SurgicalRecord { | |
| exists: false, | |
| token_id: id as u16, | |
| last_step: 0, | |
| occurrences: 0, | |
| prev_token_id: 0, | |
| role_vec: vec![0.0; role_dim], | |
| }) | |
| .collect(); | |
| Self { | |
| records, | |
| total_archived: 0, | |
| recall_hits: 0, | |
| acks_received: 0, | |
| } | |
| } | |
| pub fn reset(&mut self) { | |
| for r in &mut self.records { | |
| r.exists = false; | |
| r.last_step = 0; | |
| r.occurrences = 0; | |
| r.prev_token_id = 0; | |
| r.role_vec.fill(0.0); | |
| } | |
| self.total_archived = 0; | |
| self.recall_hits = 0; | |
| self.acks_received = 0; | |
| } | |
| /// Archive a generated token directly into `records[token_id]` (O(1), zero allocation). | |
| pub fn store(&mut self, token_id: u16, step: usize, prev_token_id: u16, role_vec: &[f32]) { | |
| let idx = token_id as usize; | |
| if idx >= self.records.len() { return; } | |
| let rec = &mut self.records[idx]; | |
| rec.exists = true; | |
| rec.last_step = step; | |
| rec.occurrences += 1; | |
| rec.prev_token_id = prev_token_id; | |
| rec.role_vec.copy_from_slice(role_vec); | |
| self.total_archived += 1; | |
| } | |
| /// Surgical parameterized query: returns ONLY the exact minimal record for `token_id` | |
| /// (zero whole-page transfer bottleneck!). | |
| pub fn query_surgical(&mut self, token_id: u16) -> Option<&SurgicalRecord> { | |
| let idx = token_id as usize; | |
| if idx < self.records.len() && self.records[idx].exists { | |
| self.recall_hits += 1; | |
| Some(&self.records[idx]) | |
| } else { | |
| None | |
| } | |
| } | |
| /// Receive `@OK` acknowledgment after the model finishes reading and flushes working memory. | |
| pub fn ack_flush(&mut self, ack_token: u16) -> bool { | |
| if ack_token == META_OK { | |
| self.acks_received += 1; | |
| true | |
| } else { | |
| false | |
| } | |
| } | |
| } | |