oxicuda-gnn 0.2.0

Graph Neural Network primitives for OxiCUDA: sparse graph representations (CSR/COO/heterogeneous), message passing framework, GCN/GAT/GraphSAGE/GIN layers, global and hierarchical graph pooling — pure Rust, zero CUDA SDK dependency.
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
//! RWSE — Random-Walk Structural Encoding (Dwivedi et al., "Graph Neural
//! Networks with Learnable Structural and Positional Representations",
//! ICLR 2022; also the random-walk PE of GraphGPS, Rampášek 2022).
//!
//! RWSE summarises the local structural role of every node by the diagonal of
//! the powers of the random-walk transition operator `M = D⁻¹ A` (row-stochastic
//! one-step walk on the graph). For walk length `K` the encoding of node `i` is
//! the vector of **return probabilities**:
//!
//! ```text
//!   p_i = [ (M¹)_{ii}, (M²)_{ii}, …, (M^K)_{ii} ] ∈ ℝ^K
//! ```
//!
//! i.e. the probability that a random walker starting at `i` is back at `i`
//! after `k` steps. These return probabilities encode rich local topology
//! (cycles, degrees, triangle counts) and are invariant to node relabelling in
//! the sense that permuting the node indices permutes the encoding rows. RWSE
//! is computed once **offline** and concatenated to the input node features.
//!
//! Notes:
//! * The operator uses the supplied (possibly weighted) adjacency directly:
//!   `M[i,j] = w_{ij} / Σ_k w_{ik}`. Self-loop edges are honoured if present in
//!   the graph.
//! * An isolated node (out-degree `0`) has an all-zero transition row, so its
//!   return probabilities are `0` for every `k ≥ 1`.

use crate::error::{GnnError, GnnResult};
use crate::graph::csr::CsrGraph;

// ─── Free function ────────────────────────────────────────────────────────────

/// Compute the random-walk structural encoding `[n_nodes × walk_length]`.
///
/// Entry `enc[i * walk_length + (k − 1)] = (Mᵏ)_{ii}` is the `k`-step return
/// probability of node `i` under `M = D⁻¹ A`.
///
/// The powers are formed by repeated sparse-times-dense propagation of the
/// `n × n` walk matrix, reading its diagonal after every step. This is the usual
/// offline RWSE precomputation; it costs `O(walk_length · nnz · n)` time and
/// `O(n²)` scratch memory, which is appropriate for the moderate graphs handled
/// on the CPU path.
///
/// # Arguments
///
/// * `graph` — CSR graph supplying the (weighted) adjacency.
/// * `walk_length` — number of random-walk steps `K` (`≥ 1`).
///
/// # Returns
///
/// `[n_nodes × walk_length]` row-major return-probability matrix.
///
/// # Errors
///
/// * [`GnnError::InvalidLayerConfig`] if `walk_length == 0`.
/// * [`GnnError::NonFiniteOutput`] if any computed value is non-finite.
pub fn random_walk_se(graph: &CsrGraph, walk_length: usize) -> GnnResult<Vec<f32>> {
    if walk_length == 0 {
        return Err(GnnError::InvalidLayerConfig(
            "RWSE: walk_length must be >= 1".to_string(),
        ));
    }
    let n = graph.n_nodes();

    // Per-node inverse out-degree (sum of outgoing edge weights).
    let mut inv_deg = vec![0.0_f32; n];
    for (i, slot) in inv_deg.iter_mut().enumerate() {
        let w = graph.edge_weights(i)?;
        let deg: f32 = w.iter().sum();
        *slot = if deg > 0.0 { 1.0 / deg } else { 0.0 };
    }

    // walk = M (one step), stored dense [n × n], row-major walk[i*n + j].
    let mut walk = vec![0.0_f32; n * n];
    for i in 0..n {
        let nbrs = graph.neighbors(i)?;
        let wts = graph.edge_weights(i)?;
        let inv = inv_deg[i];
        for (idx, &j) in nbrs.iter().enumerate() {
            walk[i * n + j] += wts[idx] * inv;
        }
    }

    let mut enc = vec![0.0_f32; n * walk_length];
    for i in 0..n {
        enc[i * walk_length] = walk[i * n + i];
    }

    // Iterate walk ← M · walk, recording the diagonal at each step.
    let mut next = vec![0.0_f32; n * n];
    for k in 1..walk_length {
        for slot in next.iter_mut() {
            *slot = 0.0;
        }
        for i in 0..n {
            let nbrs = graph.neighbors(i)?;
            let wts = graph.edge_weights(i)?;
            let inv = inv_deg[i];
            for (idx, &j) in nbrs.iter().enumerate() {
                let m_ij = wts[idx] * inv;
                if m_ij != 0.0 {
                    for c in 0..n {
                        next[i * n + c] += m_ij * walk[j * n + c];
                    }
                }
            }
        }
        std::mem::swap(&mut walk, &mut next);
        for i in 0..n {
            enc[i * walk_length + k] = walk[i * n + i];
        }
    }

    if enc.iter().any(|v| !v.is_finite()) {
        return Err(GnnError::NonFiniteOutput("random_walk_se"));
    }
    Ok(enc)
}

// ─── Configuration ────────────────────────────────────────────────────────────

/// Configuration for an [`RwseEncoder`].
#[derive(Debug, Clone, Copy)]
pub struct RwseConfig {
    /// Number of random-walk steps `K` (`≥ 1`); the encoding dimension.
    pub walk_length: usize,
}

// ─── Encoder ──────────────────────────────────────────────────────────────────

/// Random-walk structural-encoding helper.
///
/// Produces a `[n × walk_length]` positional/structural encoding and can
/// concatenate it onto existing node features.
pub struct RwseEncoder {
    config: RwseConfig,
}

impl RwseEncoder {
    /// Construct an encoder from configuration.
    ///
    /// # Errors
    ///
    /// Returns [`GnnError::InvalidLayerConfig`] if `walk_length == 0`.
    pub fn new(config: RwseConfig) -> GnnResult<Self> {
        if config.walk_length == 0 {
            return Err(GnnError::InvalidLayerConfig(
                "RWSE: walk_length must be >= 1".to_string(),
            ));
        }
        Ok(Self { config })
    }

    /// Dimension of the produced encoding (`walk_length`).
    #[inline]
    pub fn encoding_dim(&self) -> usize {
        self.config.walk_length
    }

    /// Compute the `[n × walk_length]` return-probability encoding.
    ///
    /// # Errors
    ///
    /// Propagates errors from [`random_walk_se`].
    pub fn encode(&self, graph: &CsrGraph) -> GnnResult<Vec<f32>> {
        random_walk_se(graph, self.config.walk_length)
    }

    /// Concatenate the RWSE encoding onto node features.
    ///
    /// Given node features `x` (`[n × feat_dim]`), returns
    /// `[n × (feat_dim + walk_length)]` where each node row is
    /// `[ x_i ‖ rwse_i ]`.
    ///
    /// # Errors
    ///
    /// * [`GnnError::NodeFeatureMismatch`] if `x.len() != n * feat_dim`.
    /// * Propagates errors from [`random_walk_se`].
    pub fn augment(&self, graph: &CsrGraph, x: &[f32], feat_dim: usize) -> GnnResult<Vec<f32>> {
        let n = graph.n_nodes();
        if x.len() != n * feat_dim {
            return Err(GnnError::NodeFeatureMismatch(n, x.len() / feat_dim.max(1)));
        }
        let enc = self.encode(graph)?;
        let k = self.config.walk_length;
        let out_dim = feat_dim + k;
        let mut out = vec![0.0_f32; n * out_dim];
        for i in 0..n {
            for d in 0..feat_dim {
                out[i * out_dim + d] = x[i * feat_dim + d];
            }
            for d in 0..k {
                out[i * out_dim + feat_dim + d] = enc[i * k + d];
            }
        }
        Ok(out)
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    fn two_cycle() -> CsrGraph {
        CsrGraph::from_edges(2, &[(0, 1), (1, 0)]).expect("test invariant: value must be valid")
    }

    fn triangle() -> CsrGraph {
        CsrGraph::from_edges(3, &[(0, 1), (1, 0), (1, 2), (2, 1), (0, 2), (2, 0)])
            .expect("test invariant: value must be valid")
    }

    // ── Construction ─────────────────────────────────────────────────────────

    #[test]
    fn new_valid() {
        let enc = RwseEncoder::new(RwseConfig { walk_length: 4 })
            .expect("test invariant: value must be valid");
        assert_eq!(enc.encoding_dim(), 4);
    }

    #[test]
    fn new_invalid_zero_walk() {
        assert!(RwseEncoder::new(RwseConfig { walk_length: 0 }).is_err());
    }

    #[test]
    fn free_fn_zero_walk_errors() {
        let g = two_cycle();
        assert!(random_walk_se(&g, 0).is_err());
    }

    // ── Self-loop node returns probability 1 at every step ───────────────────

    #[test]
    fn self_loop_returns_one() {
        let g = CsrGraph::from_edges(1, &[(0, 0)]).expect("test invariant: value must be valid");
        let enc = random_walk_se(&g, 4).expect("test invariant: value must be valid");
        assert_eq!(enc.len(), 4);
        for (k, &p) in enc.iter().enumerate() {
            assert!((p - 1.0).abs() < 1e-6, "step {k}: p={p}");
        }
    }

    // ── 2-cycle: return prob alternates 0, 1, 0, 1 ───────────────────────────

    #[test]
    fn two_cycle_alternates() {
        let g = two_cycle();
        let k = 4;
        let enc = random_walk_se(&g, k).expect("test invariant: value must be valid");
        // Node 0: M¹_ii = 0, M²_ii = 1, M³_ii = 0, M⁴_ii = 1.
        let expected = [0.0_f32, 1.0, 0.0, 1.0];
        for node in 0..2 {
            for (step, &e) in expected.iter().enumerate() {
                let p = enc[node * k + step];
                assert!(
                    (p - e).abs() < 1e-6,
                    "node {node} step {step}: got {p}, want {e}"
                );
            }
        }
    }

    // ── Triangle: analytic return probabilities (1 + 2(−1/2)^k)/3 ────────────

    #[test]
    fn triangle_analytic_return_probs() {
        let g = triangle();
        let k = 3;
        let enc = random_walk_se(&g, k).expect("test invariant: value must be valid");
        // (Mᵏ)_ii = (1 + 2·(−1/2)^k) / 3 for K₃.
        let expected = [0.0_f32, 0.5, 0.25];
        for node in 0..3 {
            for (step, &e) in expected.iter().enumerate() {
                let p = enc[node * k + step];
                assert!(
                    (p - e).abs() < 1e-5,
                    "node {node} step {step}: got {p}, want {e}"
                );
            }
        }
    }

    // ── Return probabilities are valid probabilities in [0, 1] ───────────────

    #[test]
    fn return_probs_in_unit_interval() {
        let g = triangle();
        let enc = random_walk_se(&g, 6).expect("test invariant: value must be valid");
        for &p in &enc {
            assert!((-1e-6..=1.0 + 1e-6).contains(&p), "p out of range: {p}");
        }
    }

    // ── First-step return prob is zero for a simple loop-free graph ──────────

    #[test]
    fn first_step_zero_without_self_loops() {
        let g = triangle();
        let enc = random_walk_se(&g, 3).expect("test invariant: value must be valid");
        for node in 0..3 {
            assert!((enc[node * 3]).abs() < 1e-6, "node {node} step 1 not zero");
        }
    }

    // ── Permutation: relabelling nodes permutes the encoding rows ────────────

    #[test]
    fn permutation_permutes_rows() {
        // Path 0–1–2 (degrees 1,2,1) and its relabelling 2–0–1 via perm.
        let g = CsrGraph::from_edges(3, &[(0, 1), (1, 0), (1, 2), (2, 1)])
            .expect("test invariant: value must be valid");
        let k = 4;
        let enc = random_walk_se(&g, k).expect("test invariant: value must be valid");

        // Relabel: new node a = old perm[a]. perm = [2,1,0] (reverse the path).
        let perm = [2usize, 1, 0];
        let mut edges = Vec::new();
        for old in 0..3 {
            for &j in g.neighbors(old).expect("nb") {
                // inverse permutation: old node `old` becomes new index `inv[old]`.
                let inv = |x: usize| perm.iter().position(|&p| p == x).expect("inv");
                edges.push((inv(old), inv(j)));
            }
        }
        let g2 = CsrGraph::from_edges(3, &edges).expect("test invariant: value must be valid");
        let enc2 = random_walk_se(&g2, k).expect("test invariant: value must be valid");

        // enc2 row `a` must equal enc row `perm[a]`.
        for a in 0..3 {
            for step in 0..k {
                let got = enc2[a * k + step];
                let want = enc[perm[a] * k + step];
                assert!(
                    (got - want).abs() < 1e-5,
                    "row {a} step {step}: {got} vs {want}"
                );
            }
        }
    }

    // ── Isolated node has zero return probability ────────────────────────────

    #[test]
    fn isolated_node_zero() {
        // Node 0 isolated; nodes 1,2 form a 2-cycle.
        let g = CsrGraph::from_edges(3, &[(1, 2), (2, 1)])
            .expect("test invariant: value must be valid");
        let k = 3;
        let enc = random_walk_se(&g, k).expect("test invariant: value must be valid");
        // Node 0 (isolated) occupies the first `k` entries of the encoding.
        for (step, &p) in enc.iter().take(k).enumerate() {
            assert!(p.abs() < 1e-6, "isolated node step {step} nonzero");
        }
    }

    // ── Encoder encode matches the free function ─────────────────────────────

    #[test]
    fn encoder_encode_matches_free_fn() {
        let g = triangle();
        let enc = RwseEncoder::new(RwseConfig { walk_length: 5 })
            .expect("test invariant: value must be valid");
        let a = enc.encode(&g).expect("test invariant: value must be valid");
        let b = random_walk_se(&g, 5).expect("test invariant: value must be valid");
        assert_eq!(a, b);
    }

    // ── Augment concatenates features and encoding ───────────────────────────

    #[test]
    fn augment_concatenates() {
        let g = two_cycle();
        let feat_dim = 2;
        let walk_length = 3;
        let enc = RwseEncoder::new(RwseConfig { walk_length })
            .expect("test invariant: value must be valid");
        let x = vec![10.0_f32, 20.0, 30.0, 40.0]; // 2 nodes × 2 feats
        let out = enc
            .augment(&g, &x, feat_dim)
            .expect("test invariant: value must be valid");
        let out_dim = feat_dim + walk_length;
        assert_eq!(out.len(), 2 * out_dim);
        // Original features preserved in the prefix of each row.
        assert!((out[0] - 10.0).abs() < 1e-6);
        assert!((out[1] - 20.0).abs() < 1e-6);
        assert!((out[out_dim] - 30.0).abs() < 1e-6);
        assert!((out[out_dim + 1] - 40.0).abs() < 1e-6);
        // RWSE suffix for node 0: [0, 1, 0] (2-cycle).
        assert!((out[feat_dim]).abs() < 1e-6);
        assert!((out[feat_dim + 1] - 1.0).abs() < 1e-6);
        assert!((out[feat_dim + 2]).abs() < 1e-6);
    }

    #[test]
    fn augment_feature_mismatch_errors() {
        let g = two_cycle();
        let enc = RwseEncoder::new(RwseConfig { walk_length: 2 })
            .expect("test invariant: value must be valid");
        let x = vec![1.0_f32; 3]; // wrong: 2 nodes × ? does not divide evenly to 2
        let err = enc.augment(&g, &x, 2);
        assert!(matches!(err, Err(GnnError::NodeFeatureMismatch(..))));
    }

    // ── Weighted graph: row normalisation respects edge weights ──────────────

    #[test]
    fn weighted_graph_normalised() {
        // Node 0 -> 1 (w=3), 0 -> 2 (w=1); back edges weight 1.
        let g =
            CsrGraph::from_edges_weighted(3, &[(0, 1, 3.0), (0, 2, 1.0), (1, 0, 1.0), (2, 0, 1.0)])
                .expect("test invariant: value must be valid");
        let enc = random_walk_se(&g, 2).expect("test invariant: value must be valid");
        // M[0,1] = 3/4, M[0,2] = 1/4, M[1,0] = 1, M[2,0] = 1.
        // (M²)_00 = M[0,1]·M[1,0] + M[0,2]·M[2,0] = 3/4 + 1/4 = 1.
        // Node 0 row occupies enc[0..2]: step 1 then step 2.
        assert!((enc[0]).abs() < 1e-6, "step1 should be 0");
        assert!((enc[1] - 1.0).abs() < 1e-6, "step2 should be 1");
    }
}