sc_neurocore_engine 3.15.0

High-performance SIMD backend for SC-NeuroCore stochastic neuromorphic computing
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
// SPDX-License-Identifier: AGPL-3.0-or-later
// Commercial license available
// © Concepts 1996–2026 Miroslav Šotek. All rights reserved.
// © Code 2020–2026 Miroslav Šotek. All rights reserved.
// ORCID: 0009-0009-3560-0851
// Contact: www.anulum.li | protoscience@anulum.li
// SC-NeuroCore — Stochastic Graph Layer

//! # Stochastic Graph Layer
//!
//! Graph message-passing layer with both rate-mode and SC-mode forward paths.
//! Supports dense O(n²) and sparse CSR O(nnz) adjacency storage.

use rand::{RngExt, SeedableRng};
use rand_chacha::ChaCha8Rng;
use rayon::prelude::*;

/// Compressed Sparse Row storage for adjacency matrices.
#[derive(Clone, Debug)]
pub struct CsrMatrix {
    /// Length n_rows + 1. row_offsets[i]..row_offsets[i+1] indexes into col_indices/values.
    pub row_offsets: Vec<usize>,
    pub col_indices: Vec<usize>,
    pub values: Vec<f64>,
    pub n_rows: usize,
    pub n_cols: usize,
}

impl CsrMatrix {
    pub fn new(
        row_offsets: Vec<usize>,
        col_indices: Vec<usize>,
        values: Vec<f64>,
        n_rows: usize,
        n_cols: usize,
    ) -> Result<Self, String> {
        if row_offsets.len() != n_rows + 1 {
            return Err(format!(
                "row_offsets length {} != n_rows + 1 = {}",
                row_offsets.len(),
                n_rows + 1
            ));
        }
        if col_indices.len() != values.len() {
            return Err(format!(
                "col_indices length {} != values length {}",
                col_indices.len(),
                values.len()
            ));
        }
        let nnz = *row_offsets.last().ok_or("row_offsets must not be empty")?;
        if col_indices.len() != nnz {
            return Err(format!(
                "col_indices length {} != nnz from row_offsets {}",
                col_indices.len(),
                nnz
            ));
        }
        Ok(Self {
            row_offsets,
            col_indices,
            values,
            n_rows,
            n_cols,
        })
    }

    pub fn nnz(&self) -> usize {
        self.values.len()
    }

    /// Convert dense row-major matrix to CSR, keeping entries with |value| > threshold.
    pub fn from_dense(dense: &[f64], n_rows: usize, n_cols: usize, threshold: f64) -> Self {
        let mut row_offsets = Vec::with_capacity(n_rows + 1);
        let mut col_indices = Vec::new();
        let mut values = Vec::new();

        row_offsets.push(0);
        for i in 0..n_rows {
            for j in 0..n_cols {
                let v = dense[i * n_cols + j];
                if v.abs() > threshold {
                    col_indices.push(j);
                    values.push(v);
                }
            }
            row_offsets.push(col_indices.len());
        }

        Self {
            row_offsets,
            col_indices,
            values,
            n_rows,
            n_cols,
        }
    }

    /// Expand back to dense row-major layout.
    pub fn to_dense(&self) -> Vec<f64> {
        let mut dense = vec![0.0_f64; self.n_rows * self.n_cols];
        for i in 0..self.n_rows {
            for idx in self.row_offsets[i]..self.row_offsets[i + 1] {
                dense[i * self.n_cols + self.col_indices[idx]] = self.values[idx];
            }
        }
        dense
    }

    /// Row-sum (degree) for row i.
    fn row_sum(&self, i: usize) -> f64 {
        let mut s = 0.0_f64;
        for idx in self.row_offsets[i]..self.row_offsets[i + 1] {
            s += self.values[idx];
        }
        s
    }
}

/// Adjacency storage: dense or sparse CSR.
pub enum AdjStorage {
    Dense { adj: Vec<f64> },
    Sparse { csr: CsrMatrix },
}

pub struct StochasticGraphLayer {
    pub n_nodes: usize,
    pub n_features: usize,
    pub storage: AdjStorage,
    pub weights: Vec<f64>,
    pub degrees: Vec<f64>,
}

fn random_weights(n_features: usize, seed: u64) -> Vec<f64> {
    let mut rng = ChaCha8Rng::seed_from_u64(seed);
    let mut weights = vec![0.0_f64; n_features * n_features];
    for w in &mut weights {
        *w = rng.random::<f64>();
    }
    weights
}

fn dense_degrees(adj: &[f64], n: usize) -> Vec<f64> {
    let mut degrees = vec![0.0_f64; n];
    for i in 0..n {
        let mut sum = 0.0_f64;
        for j in 0..n {
            sum += adj[i * n + j];
        }
        degrees[i] = sum;
    }
    degrees
}

fn csr_degrees(csr: &CsrMatrix) -> Vec<f64> {
    (0..csr.n_rows).map(|i| csr.row_sum(i)).collect()
}

impl StochasticGraphLayer {
    /// Construct from dense adjacency matrix (backwards-compatible API).
    pub fn new(adj_flat: Vec<f64>, n_nodes: usize, n_features: usize, seed: u64) -> Self {
        assert_eq!(
            adj_flat.len(),
            n_nodes * n_nodes,
            "adj_flat must have length n_nodes * n_nodes",
        );
        let degrees = dense_degrees(&adj_flat, n_nodes);
        Self {
            n_nodes,
            n_features,
            storage: AdjStorage::Dense { adj: adj_flat },
            weights: random_weights(n_features, seed),
            degrees,
        }
    }

    /// Construct from pre-built CSR adjacency.
    pub fn new_sparse(csr: CsrMatrix, n_features: usize, seed: u64) -> Result<Self, String> {
        if csr.n_rows != csr.n_cols {
            return Err(format!(
                "CSR must be square, got {}x{}",
                csr.n_rows, csr.n_cols
            ));
        }
        let n_nodes = csr.n_rows;
        let degrees = csr_degrees(&csr);
        Ok(Self {
            n_nodes,
            n_features,
            storage: AdjStorage::Sparse { csr },
            weights: random_weights(n_features, seed),
            degrees,
        })
    }

    /// Convert dense adjacency to CSR if density < `density_threshold` (default 0.3).
    pub fn from_dense_auto(
        adj_flat: Vec<f64>,
        n_nodes: usize,
        n_features: usize,
        seed: u64,
        density_threshold: f64,
    ) -> Self {
        assert_eq!(adj_flat.len(), n_nodes * n_nodes);
        let total = (n_nodes * n_nodes) as f64;
        let nnz = adj_flat.iter().filter(|v| v.abs() > 1e-15).count() as f64;
        let density = nnz / total;

        if density < density_threshold {
            let csr = CsrMatrix::from_dense(&adj_flat, n_nodes, n_nodes, 1e-15);
            let degrees = csr_degrees(&csr);
            Self {
                n_nodes,
                n_features,
                storage: AdjStorage::Sparse { csr },
                weights: random_weights(n_features, seed),
                degrees,
            }
        } else {
            Self::new(adj_flat, n_nodes, n_features, seed)
        }
    }

    /// True if using sparse CSR storage.
    pub fn is_sparse(&self) -> bool {
        matches!(self.storage, AdjStorage::Sparse { .. })
    }

    fn validate_features(&self, node_features: &[f64]) -> Result<(), String> {
        if node_features.len() != self.n_nodes * self.n_features {
            return Err(format!(
                "node_features length mismatch: got {}, expected {}.",
                node_features.len(),
                self.n_nodes * self.n_features
            ));
        }
        Ok(())
    }

    /// Aggregate + weight transform + tanh, dispatched on storage.
    fn aggregate_and_transform(&self, agg_flat: &[f64]) -> Vec<f64> {
        let out_rows: Vec<Vec<f64>> = (0..self.n_nodes)
            .into_par_iter()
            .map(|i| {
                let agg = &agg_flat[i * self.n_features..(i + 1) * self.n_features];
                let mut out = vec![0.0_f64; self.n_features];
                for (f_out, out_val) in out.iter_mut().enumerate().take(self.n_features) {
                    let mut acc = 0.0_f64;
                    for (g, agg_val) in agg.iter().enumerate().take(self.n_features) {
                        acc += *agg_val * self.weights[g * self.n_features + f_out];
                    }
                    *out_val = acc.tanh();
                }
                out
            })
            .collect();
        let mut flat = Vec::with_capacity(self.n_nodes * self.n_features);
        for row in out_rows {
            flat.extend(row);
        }
        flat
    }

    /// Rate-mode graph forward pass.
    pub fn forward(&self, node_features: &[f64]) -> Result<Vec<f64>, String> {
        self.validate_features(node_features)?;

        let mut agg = vec![0.0_f64; self.n_nodes * self.n_features];

        match &self.storage {
            AdjStorage::Dense { adj } => {
                let agg_rows: Vec<Vec<f64>> = (0..self.n_nodes)
                    .into_par_iter()
                    .map(|i| {
                        let mut row = vec![0.0_f64; self.n_features];
                        for f in 0..self.n_features {
                            let mut acc = 0.0_f64;
                            for j in 0..self.n_nodes {
                                acc += adj[i * self.n_nodes + j]
                                    * node_features[j * self.n_features + f];
                            }
                            row[f] = acc;
                        }
                        if self.degrees[i] != 0.0 {
                            for x in &mut row {
                                *x /= self.degrees[i];
                            }
                        }
                        row
                    })
                    .collect();
                for (i, row) in agg_rows.into_iter().enumerate() {
                    agg[i * self.n_features..(i + 1) * self.n_features].copy_from_slice(&row);
                }
            }
            AdjStorage::Sparse { csr } => {
                let agg_rows: Vec<Vec<f64>> = (0..self.n_nodes)
                    .into_par_iter()
                    .map(|i| {
                        let mut row = vec![0.0_f64; self.n_features];
                        for idx in csr.row_offsets[i]..csr.row_offsets[i + 1] {
                            let j = csr.col_indices[idx];
                            let a_ij = csr.values[idx];
                            for f in 0..self.n_features {
                                row[f] += a_ij * node_features[j * self.n_features + f];
                            }
                        }
                        if self.degrees[i] != 0.0 {
                            for x in &mut row {
                                *x /= self.degrees[i];
                            }
                        }
                        row
                    })
                    .collect();
                for (i, row) in agg_rows.into_iter().enumerate() {
                    agg[i * self.n_features..(i + 1) * self.n_features].copy_from_slice(&row);
                }
            }
        }

        Ok(self.aggregate_and_transform(&agg))
    }

    /// SC-mode forward pass using AND+popcount message passing.
    ///
    /// Dense: encodes full adjacency matrix.
    /// Sparse: encodes only non-zero CSR entries, avoiding O(n^2) dense expansion.
    pub fn forward_sc(
        &self,
        node_features: &[f64],
        length: usize,
        seed: u64,
    ) -> Result<Vec<f64>, String> {
        self.validate_features(node_features)?;
        if length == 0 {
            return Err("length must be > 0 for SC mode.".to_string());
        }

        let mut rng = ChaCha8Rng::seed_from_u64(seed);
        let words = length.div_ceil(64);

        let feat_packed = crate::bitstream::encode_matrix_prob_to_packed(
            node_features,
            self.n_nodes,
            self.n_features,
            length,
            words,
            &mut rng,
        );

        let mut agg = vec![0.0_f64; self.n_nodes * self.n_features];

        match &self.storage {
            AdjStorage::Dense { adj } => {
                let adj_packed = crate::bitstream::encode_matrix_prob_to_packed(
                    adj,
                    self.n_nodes,
                    self.n_nodes,
                    length,
                    words,
                    &mut rng,
                );
                for i in 0..self.n_nodes {
                    for f in 0..self.n_features {
                        let mut pop_total = 0_u64;
                        for j in 0..self.n_nodes {
                            let a = &adj_packed[i * self.n_nodes + j];
                            let b = &feat_packed[j * self.n_features + f];
                            for w in 0..words {
                                pop_total += crate::bitstream::swar_popcount_word(a[w] & b[w]);
                            }
                        }
                        agg[i * self.n_features + f] = pop_total as f64 / length as f64;
                    }
                }
            }
            AdjStorage::Sparse { csr } => {
                // Encode only non-zero adjacency values (nnz entries, not n^2)
                let nnz = csr.nnz();
                let adj_vals_clamped: Vec<f64> =
                    csr.values.iter().map(|v| v.clamp(0.0, 1.0)).collect();
                let adj_packed = crate::bitstream::encode_matrix_prob_to_packed(
                    &adj_vals_clamped,
                    1,
                    nnz,
                    length,
                    words,
                    &mut rng,
                );
                for i in 0..self.n_nodes {
                    #[allow(clippy::needless_range_loop)]
                    for idx in csr.row_offsets[i]..csr.row_offsets[i + 1] {
                        let j = csr.col_indices[idx];
                        let a = &adj_packed[idx];
                        for f in 0..self.n_features {
                            let b = &feat_packed[j * self.n_features + f];
                            let mut pop = 0_u64;
                            for w in 0..words {
                                pop += crate::bitstream::swar_popcount_word(a[w] & b[w]);
                            }
                            agg[i * self.n_features + f] += pop as f64 / length as f64;
                        }
                    }
                }
            }
        }

        for i in 0..self.n_nodes {
            if self.degrees[i] != 0.0 {
                for f in 0..self.n_features {
                    agg[i * self.n_features + f] /= self.degrees[i];
                }
            }
        }

        let agg_packed = crate::bitstream::encode_matrix_prob_to_packed(
            &agg,
            self.n_nodes,
            self.n_features,
            length,
            words,
            &mut rng,
        );
        let w_clamped: Vec<f64> = self.weights.iter().map(|w| w.clamp(0.0, 1.0)).collect();
        let w_packed = crate::bitstream::encode_matrix_prob_to_packed(
            &w_clamped,
            self.n_features,
            self.n_features,
            length,
            words,
            &mut rng,
        );

        let mut out = Vec::with_capacity(self.n_nodes * self.n_features);
        for i in 0..self.n_nodes {
            for f_out in 0..self.n_features {
                let mut pop_total = 0_u64;
                for g in 0..self.n_features {
                    let a = &agg_packed[i * self.n_features + g];
                    let b = &w_packed[g * self.n_features + f_out];
                    for w in 0..words {
                        pop_total += crate::bitstream::swar_popcount_word(a[w] & b[w]);
                    }
                }
                out.push((pop_total as f64 / length as f64).tanh());
            }
        }

        Ok(out)
    }

    pub fn get_weights(&self) -> Vec<f64> {
        self.weights.clone()
    }

    pub fn set_weights(&mut self, weights: Vec<f64>) -> Result<(), String> {
        if weights.len() != self.n_features * self.n_features {
            return Err(format!(
                "weights length mismatch: got {}, expected {}.",
                weights.len(),
                self.n_features * self.n_features
            ));
        }
        self.weights = weights;
        Ok(())
    }
}