File size: 3,254 Bytes
f4f166e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Alter-Ego serving system.
//!
//! The production home of the dual-model system: a blank-slate student (SAN)
//! whose every experience is validated by an independent alter-ego judge before
//! it is allowed to become permanent. This crate implements inference, the
//! tri-memory system, the trust-score gate, the digestion pipeline, recall,
//! LoRA-based online learning, and the real-time ecosystem loop (the "skull").
//! The student checkpoint format is identical to the one produced by the Python
//! Kaggle training pipeline (`training/kernels/train.py`), so a model trained
//! there loads directly here.

pub mod bundle;
pub mod calibration;
pub mod bench;
pub mod checkpoint;
pub mod digestion;
pub mod ecosystem;
pub mod error;
pub mod export;
pub mod lora;
#[cfg(feature = "embedder-jina")]
pub mod jina_embedder;
pub mod memory;
pub mod model;
#[cfg(feature = "og-backend")]
pub mod og_backend;
pub mod recall;
pub mod server;
pub mod tokenizer_bpe;
pub mod trust;
pub mod vector_index;

#[cfg(needle_ffi)]
pub mod needle_ffi;
#[cfg(needle_ffi)]
pub use needle_ffi::NeedleModel;

pub use error::{Error, Result};

/// Shared builders (tests today; serving bootstrap later).
pub mod main_helpers {
    use crate::model::{ParamMap, SanConfig};
    use ndarray::Array2;
    use std::collections::HashMap;

    fn rand_arr(rows: usize, cols: usize) -> Array2<f32> {
        let n = rows * cols;
        let mut s: u64 = 0x853C49E6748FEA9B ^ (n as u64).rotate_left(17);
        let data: Vec<f32> = (0..n)
            .map(|_| {
                s ^= s << 13;
                s ^= s >> 7;
                s ^= s << 17;
                ((s >> 33) as f32 / (u32::MAX as f32) - 0.5) * 0.1
            })
            .collect();
        Array2::from_shape_vec((rows, cols), data).expect("shape")
    }

    /// Deterministic random base weights for a config (frozen-base material).
    pub fn random_student(cfg: &SanConfig) -> crate::model::San {
        let d = cfg.d_model;
        let ffn = cfg.ffn_ratio * d;
        let mut p = ParamMap::new();
        p.insert("tok_emb.weight".into(), rand_arr(cfg.vocab, d));
        p.insert("pos".into(), rand_arr(cfg.max_seq_len, d));
        p.insert("head.weight".into(), rand_arr(cfg.vocab, d));
        for i in 0..cfg.n_layers {
            p.insert(format!("blocks.{i}.ln1.weight"), rand_arr(1, d));
            p.insert(format!("blocks.{i}.ln1.bias"), rand_arr(1, d));
            p.insert(format!("blocks.{i}.attn.qkv.weight"), rand_arr(3 * d, d));
            p.insert(format!("blocks.{i}.attn.o.weight"), rand_arr(d, d));
            p.insert(format!("blocks.{i}.ln2.weight"), rand_arr(1, d));
            p.insert(format!("blocks.{i}.ln2.bias"), rand_arr(1, d));
            p.insert(format!("blocks.{i}.mlp.0.weight"), rand_arr(ffn, d));
            p.insert(format!("blocks.{i}.mlp.0.bias"), rand_arr(1, ffn));
            p.insert(format!("blocks.{i}.mlp.2.weight"), rand_arr(d, ffn));
            p.insert(format!("blocks.{i}.mlp.2.bias"), rand_arr(1, d));
        }
        p.insert("ln_f.weight".into(), rand_arr(1, d));
        p.insert("ln_f.bias".into(), rand_arr(1, d));
        let cfg2 = cfg.clone();
        crate::model::San::from_params(p, cfg2).expect("valid params")
    }
}