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
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
//! Graph Attention Network (GAT) layer — Veličković et al. 2018.

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

/// Configuration for a GAT layer.
#[derive(Debug, Clone)]
pub struct GatConfig {
    /// Input feature dimension.
    pub in_features: usize,
    /// Total output feature dimension (split across heads when `concat_heads = true`).
    pub out_features: usize,
    /// Number of attention heads.
    pub num_heads: usize,
    /// Dropout probability on attention coefficients (applied as mask in CPU simulation).
    pub dropout: f32,
    /// Negative slope for LeakyReLU in attention computation.
    pub leaky_relu_slope: f32,
    /// If `true`, concatenate head outputs; if `false`, average them.
    pub concat_heads: bool,
}

/// A single GAT layer.
pub struct GatLayer {
    config: GatConfig,
    head_dim: usize,
}

impl GatLayer {
    /// Construct a GAT layer from configuration.
    ///
    /// Requires `out_features % num_heads == 0`.
    pub fn new(config: GatConfig) -> GnnResult<Self> {
        if config.in_features == 0 {
            return Err(GnnError::InvalidLayerConfig(
                "in_features must be > 0".to_string(),
            ));
        }
        if config.num_heads == 0 {
            return Err(GnnError::InvalidLayerConfig(
                "num_heads must be > 0".to_string(),
            ));
        }
        if config.out_features == 0 {
            return Err(GnnError::InvalidLayerConfig(
                "out_features must be > 0".to_string(),
            ));
        }
        if config.out_features % config.num_heads != 0 {
            return Err(GnnError::InvalidAttentionHeads {
                dim: config.out_features,
                heads: config.num_heads,
            });
        }
        let head_dim = config.out_features / config.num_heads;
        Ok(Self { config, head_dim })
    }

    /// Multi-head attention forward pass.
    ///
    /// # Arguments
    ///
    /// - `graph`: CSR graph
    /// - `x`: `[n_nodes × in_features]`
    /// - `weight`: `[num_heads × head_dim × in_features]` (linearised row-major)
    ///   — one linear projection per head
    /// - `attn_weight`: `[num_heads × 2 × head_dim]` (linearised)
    ///   — attention vector `a^T = [a_src || a_dst]` per head
    ///
    /// # Returns
    ///
    /// - `concat_heads=true`: `[n_nodes × out_features]`
    /// - `concat_heads=false`: `[n_nodes × head_dim]`
    pub fn forward(
        &self,
        graph: &CsrGraph,
        x: &[f32],
        weight: &[f32],
        attn_weight: &[f32],
    ) -> GnnResult<Vec<f32>> {
        let n = graph.n_nodes();
        let in_f = self.config.in_features;
        let hd = self.head_dim;
        let nh = self.config.num_heads;
        let slope = self.config.leaky_relu_slope;

        if x.len() != n * in_f {
            return Err(GnnError::NodeFeatureMismatch(n, x.len() / in_f.max(1)));
        }
        // weight: [nh × hd × in_f]
        if weight.len() != nh * hd * in_f {
            return Err(GnnError::WeightShapeMismatch {
                r: nh * hd,
                c: in_f,
                d: in_f,
            });
        }
        // attn_weight: [nh × 2 × hd]
        if attn_weight.len() != nh * 2 * hd {
            return Err(GnnError::WeightShapeMismatch {
                r: nh * 2,
                c: hd,
                d: hd,
            });
        }

        // Pre-compute projected features for all nodes and all heads: Wx
        // wx[h][i][k] = Σ_j W[h,k,j] * x[i,j]
        // Flat layout: wx[(h*n + i)*hd + k]
        let mut wx = vec![0.0_f32; nh * n * hd];
        for h in 0..nh {
            let w_off = h * hd * in_f;
            for i in 0..n {
                for k in 0..hd {
                    let mut acc = 0.0_f32;
                    for j in 0..in_f {
                        acc += weight[w_off + k * in_f + j] * x[i * in_f + j];
                    }
                    wx[(h * n + i) * hd + k] = acc;
                }
            }
        }

        // For each head, compute attention and aggregate
        let out_per_head = hd;
        let total_out = if self.config.concat_heads {
            nh * hd
        } else {
            hd
        };
        let mut all_head_out = vec![0.0_f32; nh * n * out_per_head];

        for h in 0..nh {
            let a_off = h * 2 * hd; // offset into attn_weight for head h
            let wx_off = h * n * hd;

            // Compute attention logits for all edges
            // e_ij = LeakyReLU(a_src^T Wx_i + a_dst^T Wx_j)
            // Collect per-node: for each node i, compute unnorm attentions to neighbors
            // Then softmax within each node's neighborhood
            let mut node_out = vec![0.0_f32; n * hd];

            for i in 0..n {
                let neighbors = graph.neighbors(i)?;
                if neighbors.is_empty() {
                    // No neighbors: output is 0
                    continue;
                }

                // Compute a_src^T * Wx_i (constant per source node)
                let mut a_src_dot: f32 = 0.0;
                for k in 0..hd {
                    a_src_dot += attn_weight[a_off + k] * wx[wx_off + i * hd + k];
                }

                // Compute edge scores
                let mut scores = Vec::with_capacity(neighbors.len());
                for &j in neighbors {
                    let mut a_dst_dot: f32 = 0.0;
                    for k in 0..hd {
                        a_dst_dot += attn_weight[a_off + hd + k] * wx[wx_off + j * hd + k];
                    }
                    let raw = a_src_dot + a_dst_dot;
                    // LeakyReLU
                    let score = if raw >= 0.0 { raw } else { slope * raw };
                    scores.push(score);
                }

                // Softmax over scores
                let max_score = scores.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
                let exps: Vec<f32> = scores.iter().map(|&s| (s - max_score).exp()).collect();
                let sum_exp: f32 = exps.iter().sum();
                let alphas: Vec<f32> = if sum_exp > 0.0 {
                    exps.iter().map(|&e| e / sum_exp).collect()
                } else {
                    vec![1.0 / neighbors.len() as f32; neighbors.len()]
                };

                // Aggregate: h_i^h = Σ_j α_ij * Wx_j
                for (idx_j, (&j, &alpha)) in neighbors.iter().zip(alphas.iter()).enumerate() {
                    let _ = idx_j;
                    for k in 0..hd {
                        node_out[i * hd + k] += alpha * wx[wx_off + j * hd + k];
                    }
                }
            }

            // Copy into all_head_out
            for i in 0..n {
                for k in 0..hd {
                    all_head_out[(h * n + i) * hd + k] = node_out[i * hd + k];
                }
            }
        }

        // Combine heads
        let mut out = vec![0.0_f32; n * total_out];
        if self.config.concat_heads {
            // Interleave heads: out[i, h*hd + k] = all_head_out[h, i, k]
            for h in 0..nh {
                for i in 0..n {
                    for k in 0..hd {
                        out[i * total_out + h * hd + k] = all_head_out[(h * n + i) * hd + k];
                    }
                }
            }
        } else {
            // Average heads
            let inv_nh = 1.0 / nh as f32;
            for h in 0..nh {
                for i in 0..n {
                    for k in 0..hd {
                        out[i * total_out + k] += all_head_out[(h * n + i) * hd + k] * inv_nh;
                    }
                }
            }
        }

        // Suppress unused warning for leaky_relu import
        let _ = leaky_relu;

        Ok(out)
    }

    /// Output feature dimension.
    pub fn output_dim(&self) -> usize {
        if self.config.concat_heads {
            self.config.out_features
        } else {
            self.head_dim
        }
    }
}

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

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

    fn ring_graph(n: usize) -> CsrGraph {
        let edges: Vec<(usize, usize)> = (0..n).map(|i| (i, (i + 1) % n)).collect();
        CsrGraph::from_edges(n, &edges).expect("test invariant: value must be valid")
    }

    #[test]
    fn invalid_heads_not_divisible() {
        let err = GatLayer::new(GatConfig {
            in_features: 4,
            out_features: 6,
            num_heads: 4,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        });
        assert!(matches!(err, Err(GnnError::InvalidAttentionHeads { .. })));
    }

    #[test]
    fn single_head_output_shape_concat() {
        let g = ring_graph(5);
        let n = 5;
        let in_f = 4;
        let out_f = 8;
        let nh = 2;
        let hd = out_f / nh;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![0.1_f32; n * in_f];
        let w = vec![0.01_f32; nh * hd * in_f];
        let aw = vec![0.01_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        assert_eq!(out.len(), n * out_f);
    }

    #[test]
    fn mean_heads_output_shape() {
        let g = ring_graph(4);
        let n = 4;
        let in_f = 4;
        let out_f = 8;
        let nh = 4;
        let hd = out_f / nh;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: false,
        })
        .expect("test invariant: value must be valid");
        let x = vec![0.1_f32; n * in_f];
        let w = vec![0.01_f32; nh * hd * in_f];
        let aw = vec![0.01_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        // output_dim = hd (mean, not concat)
        assert_eq!(out.len(), n * hd);
    }

    #[test]
    fn output_dim_concat() {
        let layer = GatLayer::new(GatConfig {
            in_features: 4,
            out_features: 8,
            num_heads: 2,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        assert_eq!(layer.output_dim(), 8);
    }

    #[test]
    fn output_dim_mean() {
        let layer = GatLayer::new(GatConfig {
            in_features: 4,
            out_features: 8,
            num_heads: 2,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: false,
        })
        .expect("test invariant: value must be valid");
        assert_eq!(layer.output_dim(), 4); // head_dim = 8/2 = 4
    }

    #[test]
    fn attention_values_finite() {
        let g = ring_graph(5);
        let n = 5;
        let in_f = 3;
        let out_f = 3;
        let nh = 1;
        let hd = 3;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let mut x = vec![0.0_f32; n * in_f];
        for i in 0..n {
            x[i * in_f] = i as f32;
        }
        let w = vec![0.5_f32; nh * hd * in_f];
        let aw = vec![0.1_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        assert!(out.iter().all(|v| v.is_finite()), "outputs must be finite");
    }

    #[test]
    fn isolated_node_produces_zero() {
        // Node 2 has no outgoing edges
        let g = CsrGraph::from_edges(3, &[(0, 1), (1, 0)])
            .expect("test invariant: value must be valid");
        let n = 3;
        let in_f = 2;
        let out_f = 2;
        let nh = 1;
        let hd = 2;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![1.0_f32; n * in_f];
        let w = vec![0.1_f32; nh * hd * in_f];
        let aw = vec![0.1_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        // Node 2 has no outgoing edges → zero output
        assert!((out[2 * out_f]).abs() < 1e-6);
        assert!((out[2 * out_f + 1]).abs() < 1e-6);
    }

    #[test]
    fn zero_weights_zero_output() {
        let g = ring_graph(4);
        let n = 4;
        let in_f = 4;
        let out_f = 4;
        let nh = 1;
        let hd = 4;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![1.0_f32; n * in_f];
        let w = vec![0.0_f32; nh * hd * in_f]; // zero projection
        let aw = vec![0.1_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        // Wx = 0, so outputs are uniform 0
        assert!(out.iter().all(|&v| v.abs() < 1e-6));
    }

    #[test]
    fn node_feature_mismatch_error() {
        let g = ring_graph(4);
        let layer = GatLayer::new(GatConfig {
            in_features: 4,
            out_features: 4,
            num_heads: 1,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![1.0_f32; 3 * 4]; // only 3 nodes but graph has 4
        let w = vec![0.1_f32; 4 * 4];
        let aw = vec![0.1_f32; 2 * 4];
        let err = layer.forward(&g, &x, &w, &aw);
        assert!(matches!(err, Err(GnnError::NodeFeatureMismatch(..))));
    }

    #[test]
    fn uniform_features_equal_outputs() {
        // With uniform features and weights, all nodes should have equal output
        let g = ring_graph(4);
        let n = 4;
        let in_f = 2;
        let out_f = 2;
        let nh = 1;
        let hd = 2;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![1.0_f32; n * in_f];
        let w = vec![1.0_f32; nh * hd * in_f];
        let aw = vec![0.5_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        // Each node has exactly one neighbor in ring, all features uniform
        let first = out[0];
        assert!(out.iter().all(|&v| (v - first).abs() < 1e-4));
    }

    #[test]
    fn four_heads_concat_output_shape() {
        let g = ring_graph(6);
        let n = 6;
        let in_f = 8;
        let out_f = 8;
        let nh = 4;
        let hd = out_f / nh;
        let layer = GatLayer::new(GatConfig {
            in_features: in_f,
            out_features: out_f,
            num_heads: nh,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        })
        .expect("test invariant: value must be valid");
        let x = vec![0.1_f32; n * in_f];
        let w = vec![0.01_f32; nh * hd * in_f];
        let aw = vec![0.01_f32; nh * 2 * hd];
        let out = layer
            .forward(&g, &x, &w, &aw)
            .expect("test invariant: value must be valid");
        assert_eq!(out.len(), n * out_f);
    }

    #[test]
    fn invalid_zero_heads() {
        let err = GatLayer::new(GatConfig {
            in_features: 4,
            out_features: 4,
            num_heads: 0,
            dropout: 0.0,
            leaky_relu_slope: 0.2,
            concat_heads: true,
        });
        assert!(err.is_err());
    }
}