kizzasi-core 0.2.1

Core SSM (State Space Model) engine for Kizzasi AGSP
Documentation
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! RetNet: Retention Networks for Multi-Scale Sequence Modeling
//!
//! RetNet replaces traditional attention with a retention mechanism that provides:
//! - **O(1)** inference complexity (like RNNs)
//! - **Parallel training** (like Transformers)
//! - **Multi-scale temporal modeling** via multiple retention heads
//! - **Linear memory complexity**
//!
//! # Architecture
//!
//! RetNet uses Multi-Scale Retention (MSR) which applies retention at different scales:
//! ```text
//! Input → [GroupNorm] → [MSRetention] → [FFN] → Output
//!//!                       [State]
//! ```
//!
//! # Retention Mechanism
//!
//! The retention mechanism for head h:
//! ```text
//! Q = X W_Q,  K = X W_K,  V = X W_V
//! Retention = (Q K^T ⊙ D) V
//! ```
//!
//! Where D is a causal decay matrix: `D[i,j] = γ^(i-j)` for i >= j
//! γ is the decay factor (different per head for multi-scale)
//!
//! # Recurrent Form (O(1) inference)
//!
//! ```text
//! S_t = γ S_{t-1} + K_t^T V_t
//! O_t = Q_t S_t
//! ```

use crate::error::{CoreError, CoreResult};
use crate::nn::{silu, LayerNorm, NormType};
use scirs2_core::ndarray::{Array1, Array2, Array3, Axis};
use scirs2_core::random::thread_rng;

/// Multi-Scale Retention Configuration
#[derive(Debug, Clone)]
pub struct RetNetConfig {
    /// Model dimension
    pub hidden_dim: usize,
    /// Number of retention heads
    pub num_heads: usize,
    /// Head dimension (hidden_dim / num_heads)
    pub head_dim: usize,
    /// FFN expansion factor
    pub ffn_dim: usize,
    /// Number of layers
    pub num_layers: usize,
    /// Dropout rate
    pub dropout: f32,
}

impl RetNetConfig {
    /// Create a new RetNet configuration
    pub fn new(hidden_dim: usize, num_heads: usize, num_layers: usize) -> CoreResult<Self> {
        if !hidden_dim.is_multiple_of(num_heads) {
            return Err(CoreError::InvalidConfig(format!(
                "hidden_dim ({}) must be divisible by num_heads ({})",
                hidden_dim, num_heads
            )));
        }

        Ok(Self {
            hidden_dim,
            num_heads,
            head_dim: hidden_dim / num_heads,
            ffn_dim: hidden_dim * 4, // Standard 4x expansion
            num_layers,
            dropout: 0.0,
        })
    }

    /// Set FFN dimension
    pub fn ffn_dim(mut self, dim: usize) -> Self {
        self.ffn_dim = dim;
        self
    }

    /// Set dropout rate
    pub fn dropout(mut self, rate: f32) -> Self {
        self.dropout = rate;
        self
    }
}

/// Multi-Scale Retention (MSR) Module
///
/// Implements retention mechanism with multiple heads, each operating at different scales
#[derive(Debug)]
pub struct MultiScaleRetention {
    config: RetNetConfig,
    // QKV projections
    w_q: Array2<f32>,
    w_k: Array2<f32>,
    w_v: Array2<f32>,
    // Output projection
    w_o: Array2<f32>,
    // Decay factors (gamma) for each head
    gamma: Array1<f32>,
    // Group norm
    group_norm: LayerNorm,
}

impl MultiScaleRetention {
    /// Create a new multi-scale retention module
    pub fn new(config: RetNetConfig) -> CoreResult<Self> {
        let hidden_dim = config.hidden_dim;
        let num_heads = config.num_heads;
        let mut rng = thread_rng();
        let scale = (1.0 / hidden_dim as f32).sqrt();

        // Initialize QKV projections
        let w_q = Array2::from_shape_fn((hidden_dim, hidden_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale
        });
        let w_k = Array2::from_shape_fn((hidden_dim, hidden_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale
        });
        let w_v = Array2::from_shape_fn((hidden_dim, hidden_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale
        });
        let w_o = Array2::from_shape_fn((hidden_dim, hidden_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale
        });

        // Initialize decay factors (gamma) for multi-scale
        // Each head has different decay: γ_h = 1 - 2^(-5-h) for h = 0..H-1
        let gamma = Array1::from_shape_fn(num_heads, |h| {
            let exponent = -(5.0 + h as f32);
            1.0 - 2.0_f32.powf(exponent)
        });

        // Group normalization (using RMSNorm for efficiency)
        let group_norm = LayerNorm::new(hidden_dim, NormType::RMSNorm);

        Ok(Self {
            config,
            w_q,
            w_k,
            w_v,
            w_o,
            gamma,
            group_norm,
        })
    }

    /// Recurrent forward step (O(1) inference)
    ///
    /// Updates retention state and computes output
    /// State: (num_heads, head_dim, head_dim)
    pub fn step(&self, input: &Array1<f32>, state: &mut Array3<f32>) -> CoreResult<Array1<f32>> {
        let num_heads = self.config.num_heads;
        let head_dim = self.config.head_dim;

        // Project to Q, K, V
        let q = input.dot(&self.w_q);
        let k = input.dot(&self.w_k);
        let v = input.dot(&self.w_v);

        let mut output = Array1::zeros(self.config.hidden_dim);

        // Process each head
        for h in 0..num_heads {
            let start = h * head_dim;
            let end = start + head_dim;

            let q_h = q.slice(s![start..end]);
            let k_h = k.slice(s![start..end]);
            let v_h = v.slice(s![start..end]);

            // Get state for this head
            let mut s_h = state.index_axis_mut(Axis(0), h);

            // Decay previous state: S_t = γ S_{t-1}
            let gamma_h = self.gamma[h];
            for i in 0..head_dim {
                for j in 0..head_dim {
                    s_h[[i, j]] *= gamma_h;
                }
            }

            // Add new contribution: S_t += K_t^T V_t (outer product)
            for i in 0..head_dim {
                for j in 0..head_dim {
                    s_h[[i, j]] += k_h[i] * v_h[j];
                }
            }

            // Output: O_t = Q_t S_t
            for j in 0..head_dim {
                let mut sum = 0.0;
                for i in 0..head_dim {
                    sum += q_h[i] * s_h[[i, j]];
                }
                output[start + j] = sum;
            }
        }

        // Group normalization
        let normed = self.group_norm.forward(&output);

        // Output projection with SiLU activation
        let output_proj = normed.dot(&self.w_o);
        let activated = silu(&output_proj);

        Ok(activated)
    }

    /// Parallel forward for sequence (training mode)
    ///
    /// Input shape: (seq_len, hidden_dim)
    /// Output shape: (seq_len, hidden_dim)
    pub fn forward_sequence(&self, input: &Array2<f32>) -> CoreResult<Array2<f32>> {
        let (seq_len, _) = input.dim();

        let mut output = Array2::zeros((seq_len, self.config.hidden_dim));
        let mut state = self.reset_state();

        // Process sequence step by step
        for t in 0..seq_len {
            let x_t = input.row(t).to_owned();
            let y_t = self.step(&x_t, &mut state)?;
            output.row_mut(t).assign(&y_t);
        }

        Ok(output)
    }

    /// Reset retention state
    pub fn reset_state(&self) -> Array3<f32> {
        Array3::zeros((
            self.config.num_heads,
            self.config.head_dim,
            self.config.head_dim,
        ))
    }

    /// Get number of parameters
    pub fn num_parameters(&self) -> usize {
        self.w_q.len() + self.w_k.len() + self.w_v.len() + self.w_o.len() + self.gamma.len()
    }
}

/// Feed-Forward Network for RetNet
#[derive(Debug)]
pub struct RetNetFFN {
    w1: Array2<f32>,
    w2: Array2<f32>,
    layer_norm: LayerNorm,
}

impl RetNetFFN {
    /// Create a new FFN
    pub fn new(hidden_dim: usize, ffn_dim: usize) -> Self {
        let mut rng = thread_rng();
        let scale1 = (1.0 / hidden_dim as f32).sqrt();
        let scale2 = (1.0 / ffn_dim as f32).sqrt();

        let w1 = Array2::from_shape_fn((hidden_dim, ffn_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale1
        });
        let w2 = Array2::from_shape_fn((ffn_dim, hidden_dim), |_| {
            (rng.random::<f32>() - 0.5) * 2.0 * scale2
        });

        let layer_norm = LayerNorm::new(hidden_dim, NormType::RMSNorm);

        Self { w1, w2, layer_norm }
    }

    /// Forward pass
    pub fn forward(&self, input: &Array1<f32>) -> CoreResult<Array1<f32>> {
        // Layer norm
        let normed = self.layer_norm.forward(input);

        // FFN with SiLU activation: SiLU(x W1) W2
        let hidden = normed.dot(&self.w1);
        let activated = silu(&hidden);
        let output = activated.dot(&self.w2);

        Ok(output)
    }
}

/// RetNet Layer
///
/// Combines Multi-Scale Retention and FFN with residual connections
#[derive(Debug)]
pub struct RetNetLayer {
    retention: MultiScaleRetention,
    ffn: RetNetFFN,
}

impl RetNetLayer {
    /// Create a new RetNet layer
    pub fn new(config: RetNetConfig) -> CoreResult<Self> {
        let retention = MultiScaleRetention::new(config.clone())?;
        let ffn = RetNetFFN::new(config.hidden_dim, config.ffn_dim);

        Ok(Self { retention, ffn })
    }

    /// Forward step with residual connections
    pub fn step(&self, input: &Array1<f32>, state: &mut Array3<f32>) -> CoreResult<Array1<f32>> {
        // Retention with residual
        let retention_out = self.retention.step(input, state)?;
        let after_retention = input + &retention_out;

        // FFN with residual
        let ffn_out = self.ffn.forward(&after_retention)?;
        let output = &after_retention + &ffn_out;

        Ok(output)
    }

    /// Forward sequence
    pub fn forward_sequence(&self, input: &Array2<f32>) -> CoreResult<Array2<f32>> {
        let (seq_len, _) = input.dim();
        let mut output = Array2::zeros(input.dim());
        let mut state = self.retention.reset_state();

        for t in 0..seq_len {
            let x_t = input.row(t).to_owned();
            let y_t = self.step(&x_t, &mut state)?;
            output.row_mut(t).assign(&y_t);
        }

        Ok(output)
    }

    /// Reset state
    pub fn reset_state(&self) -> Array3<f32> {
        self.retention.reset_state()
    }
}

/// Multi-layer RetNet Model
#[derive(Debug)]
pub struct RetNetModel {
    layers: Vec<RetNetLayer>,
    config: RetNetConfig,
}

impl RetNetModel {
    /// Create a new multi-layer RetNet model
    pub fn new(config: RetNetConfig) -> CoreResult<Self> {
        let num_layers = config.num_layers;
        let mut layers = Vec::with_capacity(num_layers);

        for _ in 0..num_layers {
            layers.push(RetNetLayer::new(config.clone())?);
        }

        Ok(Self { layers, config })
    }

    /// Single step inference
    pub fn step(&self, input: &Array1<f32>, states: &mut [Array3<f32>]) -> CoreResult<Array1<f32>> {
        if states.len() != self.config.num_layers {
            return Err(CoreError::InvalidConfig(format!(
                "Expected {} states, got {}",
                self.config.num_layers,
                states.len()
            )));
        }

        let mut x = input.clone();
        for (i, layer) in self.layers.iter().enumerate() {
            x = layer.step(&x, &mut states[i])?;
        }

        Ok(x)
    }

    /// Forward pass for sequence
    pub fn forward(&self, input: &Array2<f32>) -> CoreResult<Array2<f32>> {
        let mut x = input.clone();

        for layer in &self.layers {
            x = layer.forward_sequence(&x)?;
        }

        Ok(x)
    }

    /// Reset all states
    pub fn reset_states(&self) -> Vec<Array3<f32>> {
        self.layers
            .iter()
            .map(|layer| layer.reset_state())
            .collect()
    }

    /// Get total number of parameters
    pub fn num_parameters(&self) -> usize {
        self.layers
            .iter()
            .map(|layer| layer.retention.num_parameters() + layer.ffn.w1.len() + layer.ffn.w2.len())
            .sum()
    }
}

// Re-export slice macro
use scirs2_core::ndarray::s;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_retnet_config() {
        let config = RetNetConfig::new(256, 4, 6).unwrap();
        assert_eq!(config.hidden_dim, 256);
        assert_eq!(config.num_heads, 4);
        assert_eq!(config.head_dim, 64);
        assert_eq!(config.num_layers, 6);
    }

    #[test]
    fn test_multi_scale_retention() {
        let config = RetNetConfig::new(128, 4, 2).unwrap();
        let msr = MultiScaleRetention::new(config).unwrap();

        let input = Array1::from_vec(vec![0.1; 128]);
        let mut state = msr.reset_state();

        let output = msr.step(&input, &mut state).unwrap();
        assert_eq!(output.len(), 128);

        // Check that state has been updated
        assert!(state.iter().any(|&x| x != 0.0));
    }

    #[test]
    fn test_retnet_layer() {
        let config = RetNetConfig::new(128, 4, 2).unwrap();
        let layer = RetNetLayer::new(config).unwrap();

        let input = Array1::from_vec(vec![0.1; 128]);
        let mut state = layer.reset_state();

        let output = layer.step(&input, &mut state).unwrap();
        assert_eq!(output.len(), 128);
    }

    #[test]
    fn test_retnet_model() {
        let config = RetNetConfig::new(64, 2, 3).unwrap();
        let model = RetNetModel::new(config).unwrap();

        let seq_len = 10;
        let input = Array2::from_shape_vec((seq_len, 64), vec![0.1; seq_len * 64]).unwrap();

        let output = model.forward(&input).unwrap();
        assert_eq!(output.dim(), (seq_len, 64));
    }

    #[test]
    fn test_retnet_inference() {
        let config = RetNetConfig::new(64, 2, 2).unwrap();
        let model = RetNetModel::new(config).unwrap();

        let mut states = model.reset_states();
        let input = Array1::from_vec(vec![0.1; 64]);

        // Process multiple steps
        for _ in 0..5 {
            let output = model.step(&input, &mut states).unwrap();
            assert_eq!(output.len(), 64);
        }
    }

    #[test]
    fn test_gamma_values() {
        let config = RetNetConfig::new(128, 4, 2).unwrap();
        let msr = MultiScaleRetention::new(config).unwrap();

        // Check that gamma values are in valid range (0, 1)
        for &gamma in msr.gamma.iter() {
            assert!(gamma > 0.0 && gamma < 1.0);
        }

        // Check that gammas are decreasing (larger heads have smaller decay)
        for i in 1..msr.gamma.len() {
            assert!(msr.gamma[i] >= msr.gamma[i - 1]);
        }
    }
}