primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// SPDX-License-Identifier: AGPL-3.0-or-later
//! High-level Spiking Neural Network (SNN) API
//!
//! **EVOLVED**: Pure Rust spike processing - No specialized WGSL shaders!
//!
//! This module provides a production-ready interface for building and running
//! spiking neural networks using pure Rust event processing algorithms.
//!
//! # Spiking Neural Networks
//!
//! SNNs are brain-inspired neural networks that:
//! - Process information as discrete events (spikes)
//! - Maintain temporal dynamics (memory)
//! - Operate efficiently on any hardware
//! - Excel at temporal pattern recognition
//!
//! # Philosophy
//!
//! SNN operations are **event processing**, not heavy tensor math! For typical
//! neuron counts (100-10,000), pure Rust spike logic is faster and simpler
//! than GPU shader overhead.
//!
//! # Deep Debt Compliance
//!
//! - ✅ **Hardware agnostic**: No GPU/NPU assumptions
//! - ✅ **Pure Rust**: No specialized WGSL shaders
//! - ✅ **Fast**: Event processing beats GPU overhead
//! - ✅ **Safe**: Zero unsafe code
//! - ✅ **Capability-based**: Runtime configuration
//!
//! # Example
//!
//! ```rust,ignore
//! use barracuda::snn::{SpikingNetwork, SNNConfig, SNNLayer};
//!
//! // No device needed - pure Rust!
//! let mut network = SpikingNetwork::builder()
//!     .add_layer(SNNLayer::LIF {
//!         size: 100,
//!         tau: 20.0,
//!         threshold: 1.0,
//!         reset: 0.0,
//!     })
//!     .add_layer(SNNLayer::TemporalPool { window_size: 10 })
//!     .build();
//!
//! // Process temporal sequence
//! let input_sequence = vec![/* spike trains */];
//! let output = network.process_sequence(&input_sequence)?;
//! ```

use crate::error::{BarracudaError, Result as BarracudaResult};
use rand::{Rng, SeedableRng};

/// Configuration for spiking neural network.
#[derive(Debug, Clone)]
pub struct SNNConfig {
    /// Input encoding parameters
    pub input_encoding: EncodingType,

    /// Output decoding parameters
    pub output_decoding: DecodingType,

    /// Enable automatic state management
    pub auto_reset: bool,

    /// Time step for simulation (ms)
    pub dt: f32,
}

impl Default for SNNConfig {
    fn default() -> Self {
        Self {
            input_encoding: EncodingType::Rate { max_rate: 100.0 },
            output_decoding: DecodingType::Rate,
            auto_reset: true,
            dt: 1.0,
        }
    }
}

/// Input encoding strategy (capability-based).
#[derive(Debug, Clone)]
pub enum EncodingType {
    /// Rate coding: intensity → spike frequency
    Rate {
        /// Maximum spike rate (Hz)
        max_rate: f32,
    },
    /// Temporal coding: value → spike timing
    Temporal,
    /// Population coding: distributed representation
    Population {
        /// Number of neurons in population
        n_neurons: usize,
    },
}

/// Output decoding strategy (capability-based).
#[derive(Debug, Clone)]
pub enum DecodingType {
    /// Rate decoding: spike frequency → intensity
    Rate,
    /// First-to-spike: winner-take-all
    FirstSpike,
    /// Population vector: weighted combination
    PopulationVector,
}

/// SNN layer types (all capability-based).
#[derive(Debug, Clone)]
pub enum SNNLayer {
    /// Leaky Integrate-and-Fire neurons
    LIF {
        /// Number of neurons in the layer
        size: usize,
        /// Membrane time constant (ms)
        tau: f32,
        /// Spike threshold
        threshold: f32,
        /// Reset potential after spike
        reset: f32,
    },
    /// Temporal pooling (aggregation over time)
    TemporalPool {
        /// Number of time steps to aggregate
        window_size: usize,
    },
    /// Sparse linear transformation
    SparseLinear {
        /// Input dimension
        input_size: usize,
        /// Output dimension
        output_size: usize,
        /// Fraction of non-zero weights (0.0–1.0)
        sparsity: f32,
        /// Weight matrix (row-major); `None` = auto-initialize
        weights: Option<Vec<f32>>,
    },
    /// Rate encoding layer
    RateEncoder {
        /// Maximum spike rate (Hz)
        max_rate: f32,
    },
    /// Rate decoding layer
    RateDecoder,
}

/// Network state (runtime-managed, no mocks)
struct LayerState {
    /// Neuron membrane potentials
    membrane: Vec<f32>,

    /// Current spike state
    spikes: Vec<f32>,

    /// Temporal pool buffer (if applicable)
    temporal_buffer: Vec<Vec<f32>>,

    /// Synapttic weights (for linear layers)
    weights: Option<Vec<f32>>,
}

impl LayerState {
    fn new(size: usize) -> Self {
        Self {
            membrane: vec![0.0; size],
            spikes: vec![0.0; size],
            temporal_buffer: Vec::new(),
            weights: None,
        }
    }
}

/// Spiking Neural Network
///
/// **Pure Rust implementation** - No GPU dependencies!
pub struct SpikingNetwork {
    config: SNNConfig,
    layers: Vec<SNNLayer>,
    states: Vec<LayerState>,
}

/// Builder for constructing SNNs.
pub struct SNNBuilder {
    layers: Vec<SNNLayer>,
    config: SNNConfig,
}

impl SNNBuilder {
    fn new(config: SNNConfig) -> Self {
        Self {
            layers: Vec::new(),
            config,
        }
    }

    /// Add a layer to the network.
    #[must_use]
    pub fn add_layer(mut self, layer: SNNLayer) -> Self {
        self.layers.push(layer);
        self
    }

    /// Build the network, initializing layer states and weights.
    ///
    /// For [`SNNLayer::SparseLinear`] layers without explicit weights,
    /// auto-initializes sparse random weights based on the sparsity parameter.
    #[must_use]
    pub fn build(self) -> SpikingNetwork {
        let mut states = Vec::new();

        // Initialize states for each layer
        for layer in &self.layers {
            let size = match layer {
                SNNLayer::LIF { size, .. } => *size,
                SNNLayer::TemporalPool { .. } => 0, // Stateless passthrough
                SNNLayer::SparseLinear { output_size, .. } => *output_size,
                SNNLayer::RateEncoder { .. } => 0,
                SNNLayer::RateDecoder => 0,
            };

            let mut state = LayerState::new(size);

            // Initialize weights for linear layers
            if let SNNLayer::SparseLinear {
                input_size,
                output_size,
                sparsity,
                weights,
            } = layer
            {
                let w = if let Some(w) = weights {
                    w.clone()
                } else {
                    // Auto-initialize sparse random weights
                    let mut rng = rand::rngs::StdRng::from_os_rng();
                    let mut weights = vec![0.0; input_size * output_size];
                    for w in &mut weights {
                        if rng.random::<f32>() < *sparsity {
                            *w = rng.random_range(-1.0..1.0);
                        }
                    }
                    weights
                };
                state.weights = Some(w);
            }

            states.push(state);
        }

        SpikingNetwork {
            config: self.config,
            layers: self.layers,
            states,
        }
    }
}

impl SpikingNetwork {
    /// Create a network builder.
    #[must_use]
    pub fn builder() -> SNNBuilder {
        SNNBuilder::new(SNNConfig::default())
    }

    /// Create a network builder with config.
    #[must_use]
    pub fn builder_with_config(config: SNNConfig) -> SNNBuilder {
        SNNBuilder::new(config)
    }

    /// Process a single time step through the network
    ///
    /// **Pure Rust** - Fast event processing!
    ///
    /// # Arguments
    ///
    /// * `input` - Input spikes (0 or 1) or continuous values (for encoding layer)
    ///
    /// # Returns
    ///
    /// Output spikes or decoded values
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if any layer processes input with mismatched dimensions.
    pub fn process_step(&mut self, input: impl Into<Vec<f32>>) -> BarracudaResult<Vec<f32>> {
        let mut current = input.into();

        for i in 0..self.layers.len() {
            current = self.process_layer(i, &current)?;
        }

        Ok(current)
    }

    /// Process a sequence through the network
    ///
    /// # Arguments
    ///
    /// * `sequence` - Sequence of inputs over time
    ///
    /// # Returns
    ///
    /// Sequence of outputs
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if any layer processes input with mismatched dimensions.
    pub fn process_sequence(&mut self, sequence: &[Vec<f32>]) -> BarracudaResult<Vec<Vec<f32>>> {
        sequence
            .iter()
            .map(|input| self.process_step(input.as_slice()))
            .collect()
    }

    /// Process a single layer (pure Rust!)
    fn process_layer(&mut self, layer_idx: usize, input: &[f32]) -> BarracudaResult<Vec<f32>> {
        let layer = &self.layers[layer_idx];
        let state = &mut self.states[layer_idx];

        match layer {
            SNNLayer::LIF {
                size,
                tau,
                threshold,
                reset,
            } => {
                if input.len() != *size {
                    return Err(BarracudaError::InvalidInput {
                        message: format!(
                            "LIF input size mismatch: expected {}, got {}",
                            size,
                            input.len()
                        ),
                    });
                }

                // Leaky Integrate-and-Fire dynamics (pure Rust!)
                let dt = self.config.dt;
                let decay_factor = 1.0 - (dt / tau);

                for ((membrane, inp), spike) in state
                    .membrane
                    .iter_mut()
                    .zip(input.iter())
                    .zip(state.spikes.iter_mut())
                {
                    // Decay membrane potential
                    *membrane *= decay_factor;

                    // Integrate input
                    *membrane += *inp;

                    // Check for spike
                    if *membrane >= *threshold {
                        *spike = 1.0;
                        *membrane = *reset; // Reset to reset potential
                    } else {
                        *spike = 0.0;
                    }
                }

                Ok(state.spikes.clone())
            }

            SNNLayer::TemporalPool { window_size } => {
                // Temporal pooling: sum spikes over time window
                // Copy necessary: we must store the frame for later aggregation; buffer owns the data.
                state.temporal_buffer.push(input.to_vec());

                if state.temporal_buffer.len() > *window_size {
                    state.temporal_buffer.remove(0);
                }

                // Sum over time window
                let output_size = input.len();
                let mut pooled = vec![0.0; output_size];

                for frame in &state.temporal_buffer {
                    for (i, &spike) in frame.iter().enumerate() {
                        pooled[i] += spike;
                    }
                }

                Ok(pooled)
            }

            SNNLayer::SparseLinear {
                input_size,
                output_size,
                ..
            } => {
                if input.len() != *input_size {
                    return Err(BarracudaError::InvalidInput {
                        message: format!(
                            "Linear input size mismatch: expected {}, got {}",
                            input_size,
                            input.len()
                        ),
                    });
                }

                let weights = state.weights.as_ref().ok_or_else(|| {
                    crate::error::BarracudaError::InvalidOperation {
                        op: "SNN Dense layer".to_string(),
                        reason: "Dense layer weights not initialized".to_string(),
                    }
                })?;
                let mut output = vec![0.0; *output_size];

                // Matrix multiply (sparse)
                for i in 0..*output_size {
                    for j in 0..*input_size {
                        output[i] = weights[i * input_size + j].mul_add(input[j], output[i]);
                    }
                }

                Ok(output)
            }

            SNNLayer::RateEncoder { max_rate } => {
                // Rate encoding: continuous → spikes
                let mut rng = rand::rngs::StdRng::from_os_rng();
                let dt = self.config.dt;

                let spikes: Vec<f32> = input
                    .iter()
                    .map(|&value| {
                        let spike_prob = value.abs() * max_rate * dt / 1000.0; // Convert to probability
                        if rng.random::<f32>() < spike_prob {
                            1.0
                        } else {
                            0.0
                        }
                    })
                    .collect();

                Ok(spikes)
            }

            SNNLayer::RateDecoder => {
                // Rate decoding: spikes → continuous
                // Just pass through (decoding happens via temporal pooling)
                // Copy necessary: we must return owned Vec; input is borrowed.
                Ok(input.to_vec())
            }
        }
    }

    /// Reset all layer states.
    pub fn reset(&mut self) {
        for state in &mut self.states {
            state.membrane.fill(0.0);
            state.spikes.fill(0.0);
            state.temporal_buffer.clear();
        }
    }
}

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

    #[test]
    fn test_snn_builder() {
        let network = SpikingNetwork::builder()
            .add_layer(SNNLayer::LIF {
                size: 10,
                tau: 20.0,
                threshold: 1.0,
                reset: 0.0,
            })
            .build();

        assert_eq!(network.layers.len(), 1);
    }

    #[test]
    fn test_lif_spike() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::LIF {
                size: 5,
                tau: 10.0,
                threshold: 1.0,
                reset: 0.0,
            })
            .build();

        // Strong input should cause spikes
        let input = vec![2.0, 2.0, 2.0, 2.0, 2.0];
        let output = network.process_step(input.as_slice()).unwrap();

        // Should have spikes (1.0) since input > threshold
        assert!(output.contains(&1.0));
    }

    #[test]
    fn test_lif_no_spike() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::LIF {
                size: 5,
                tau: 10.0,
                threshold: 1.0,
                reset: 0.0,
            })
            .build();

        // Weak input should not cause spikes
        let input = vec![0.1, 0.1, 0.1, 0.1, 0.1];
        let output = network.process_step(input.as_slice()).unwrap();

        // Should have no spikes
        assert!(output.iter().all(|&x| x == 0.0));
    }

    #[test]
    fn test_temporal_pool() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::TemporalPool { window_size: 3 })
            .build();

        // Process sequence
        network.process_step([1.0, 0.0]).unwrap();
        network.process_step([0.0, 1.0]).unwrap();
        let output = network.process_step([1.0, 0.0]).unwrap();

        // Should sum over last 3 frames
        assert_eq!(output[0], 2.0); // Two 1.0s in first channel
        assert_eq!(output[1], 1.0); // One 1.0 in second channel
    }

    #[test]
    fn test_sparse_linear() {
        let weights = vec![
            0.5, 0.0, 0.0, 0.5, // Output 0 weights
            0.0, 0.5, 0.5, 0.0, // Output 1 weights
        ];

        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::SparseLinear {
                input_size: 4,
                output_size: 2,
                sparsity: 0.5,
                weights: Some(weights),
            })
            .build();

        let input = vec![1.0, 2.0, 3.0, 4.0];
        let output = network.process_step(input.as_slice()).unwrap();

        // output[0] = 0.5*1.0 + 0.5*4.0 = 2.5
        // output[1] = 0.5*2.0 + 0.5*3.0 = 2.5
        assert!((output[0] - 2.5).abs() < 1e-6);
        assert!((output[1] - 2.5).abs() < 1e-6);
    }

    #[test]
    fn test_rate_encoding() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::RateEncoder { max_rate: 100.0 })
            .build();

        // High values should produce more spikes (statistically)
        let high_input = vec![1.0; 10];
        let low_input = vec![0.1; 10];

        let mut high_spike_count = 0;
        let mut low_spike_count = 0;

        // Run multiple trials
        for _ in 0..1000 {
            let high_out = network.process_step(high_input.as_slice()).unwrap();
            let low_out = network.process_step(low_input.as_slice()).unwrap();

            high_spike_count += high_out.iter().filter(|&&x| x == 1.0).count();
            low_spike_count += low_out.iter().filter(|&&x| x == 1.0).count();
        }

        // High input should produce more spikes
        assert!(high_spike_count > low_spike_count * 2);
    }

    #[test]
    fn test_network_reset() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::LIF {
                size: 5,
                tau: 10.0,
                threshold: 1.0,
                reset: 0.0,
            })
            .build();

        // Process some input
        network.process_step([0.5; 5]).unwrap();

        // Reset
        network.reset();

        // State should be zero
        assert!(network.states[0].membrane.iter().all(|&x| x == 0.0));
        assert!(network.states[0].spikes.iter().all(|&x| x == 0.0));
    }

    #[test]
    fn test_multi_layer_network() {
        let mut network = SpikingNetwork::builder()
            .add_layer(SNNLayer::LIF {
                size: 10,
                tau: 20.0,
                threshold: 1.0,
                reset: 0.0,
            })
            .add_layer(SNNLayer::TemporalPool { window_size: 5 })
            .build();

        // Process sequence
        let sequence = vec![vec![1.5; 10], vec![1.5; 10], vec![0.0; 10]];

        let outputs = network.process_sequence(&sequence).unwrap();
        assert_eq!(outputs.len(), 3);
    }
}