aprender-core 0.29.1

Next-generation machine learning library in pure Rust
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
//! Graph Neural Network layers for learning on graph-structured data.
//!
//! This module provides neural network layers that operate on graphs,
//! enabling learning from node features and graph topology.
//!
//! # Architecture
//!
//! ```text
//! Node Features    Graph Structure
//!      │                 │
//!      ▼                 ▼
//! ┌────────────────────────────┐
//! │       GNN Layer            │
//! │  (aggregate + transform)   │
//! └────────────────────────────┘
//!//!//!    Updated Node Features
//! ```
//!
//! # Layers
//!
//! - [`GCNConv`] - Graph Convolutional Network (Kipf & Welling, 2017)
//! - [`GATConv`] - Graph Attention Network (Velickovic et al., 2018)
//!
//! # Example
//!
//! ```ignore
//! use aprender::gnn::{GCNConv, GNNModule};
//! use aprender::autograd::Tensor;
//!
//! // Create GCN layer: 16 input features → 32 output features
//! let gcn = GCNConv::new(16, 32);
//!
//! // Node features [num_nodes, in_features]
//! let x = Tensor::ones(&[4, 16]);
//!
//! // Adjacency matrix (COO format): edge_index[2, num_edges]
//! let edge_index = vec![(0, 1), (1, 2), (2, 3), (3, 0)];
//!
//! let out = gcn.forward_gnn(&x, &edge_index);
//! assert_eq!(out.shape(), &[4, 32]);
//! ```
//!
//! # References
//!
//! - Kipf, T. N., & Welling, M. (2017). Semi-Supervised Classification with
//!   Graph Convolutional Networks. ICLR.
//! - Velickovic, P., et al. (2018). Graph Attention Networks. ICLR.
//! - Hamilton, W. L., et al. (2017). Inductive Representation Learning on
//!   Large Graphs (`GraphSAGE`). `NeurIPS`.

use crate::autograd::Tensor;
use crate::nn::{Linear, Module};

/// Edge index type: (`source_node`, `target_node`)
pub type EdgeIndex = (usize, usize);

/// Trait for GNN modules that process graph-structured data.
///
/// Unlike regular [`Module`], GNN layers require graph structure in addition
/// to node features.
pub trait GNNModule: Module {
    /// Forward pass with graph structure.
    ///
    /// # Arguments
    ///
    /// * `x` - Node features `[num_nodes, in_features]`
    /// * `edge_index` - List of edges as (source, target) pairs
    ///
    /// # Returns
    ///
    /// Updated node features `[num_nodes, out_features]`
    fn forward_gnn(&self, x: &Tensor, edge_index: &[EdgeIndex]) -> Tensor;
}

/// Graph Convolutional Network layer (Kipf & Welling, 2017).
///
/// Aggregates neighbor features using mean aggregation with symmetric
/// normalization:
///
/// ```text
/// h_i' = σ(Σ_j (1/√(d_i * d_j)) * W * h_j)
/// ```
///
/// where `d_i` is the degree of node i.
///
/// # Example
///
/// ```ignore
/// use aprender::gnn::GCNConv;
///
/// let gcn = GCNConv::new(16, 32);  // 16 in → 32 out
/// ```
#[derive(Debug)]
#[allow(dead_code)]
pub struct GCNConv {
    /// Linear transformation
    linear: Linear,
    /// Input feature dimension
    in_features: usize,
    /// Output feature dimension
    out_features: usize,
    /// Whether to add self-loops
    add_self_loops: bool,
    /// Whether to use bias
    use_bias: bool,
}

impl GCNConv {
    /// Create a new GCN convolutional layer.
    ///
    /// # Arguments
    ///
    /// * `in_features` - Input feature dimension
    /// * `out_features` - Output feature dimension
    #[must_use]
    pub fn new(in_features: usize, out_features: usize) -> Self {
        Self {
            linear: Linear::new(in_features, out_features),
            in_features,
            out_features,
            add_self_loops: true,
            use_bias: true,
        }
    }

    /// Create GCN without self-loops.
    #[must_use]
    pub fn without_self_loops(in_features: usize, out_features: usize) -> Self {
        Self {
            linear: Linear::new(in_features, out_features),
            in_features,
            out_features,
            add_self_loops: false,
            use_bias: true,
        }
    }

    /// Get input feature dimension.
    #[must_use]
    pub fn in_features(&self) -> usize {
        self.in_features
    }

    /// Get output feature dimension.
    #[must_use]
    pub fn out_features(&self) -> usize {
        self.out_features
    }
}

impl Module for GCNConv {
    fn forward(&self, _input: &Tensor) -> Tensor {
        panic!("GCNConv requires graph structure. Use forward_gnn() instead.");
    }

    fn parameters(&self) -> Vec<&Tensor> {
        self.linear.parameters()
    }

    fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
        self.linear.parameters_mut()
    }
}

impl GNNModule for GCNConv {
    fn forward_gnn(&self, x: &Tensor, edge_index: &[EdgeIndex]) -> Tensor {
        let num_nodes = x.shape()[0];
        let in_features = x.shape()[1];

        assert_eq!(
            in_features, self.in_features,
            "Expected {} input features, got {}",
            self.in_features, in_features
        );

        // Compute node degrees (for normalization)
        let mut degrees = vec![0.0f32; num_nodes];

        // Add self-loops to degree count if enabled
        if self.add_self_loops {
            for d in &mut degrees {
                *d += 1.0;
            }
        }

        // Count degrees from edges
        for &(src, tgt) in edge_index {
            degrees[src] += 1.0;
            degrees[tgt] += 1.0; // For undirected graphs
        }

        // Compute D^{-1/2} for symmetric normalization
        let norm: Vec<f32> = degrees.iter().map(|&d| 1.0 / d.sqrt().max(1e-6)).collect();

        // Aggregate: h' = D^{-1/2} A D^{-1/2} h
        let x_data = x.data();
        let mut aggregated = vec![0.0f32; num_nodes * in_features];

        // Add self-loop contribution
        if self.add_self_loops {
            for i in 0..num_nodes {
                let norm_ii = norm[i] * norm[i]; // Self-loop normalization
                for f in 0..in_features {
                    aggregated[i * in_features + f] += norm_ii * x_data[i * in_features + f];
                }
            }
        }

        // Add neighbor contributions
        for &(src, tgt) in edge_index {
            let norm_coeff = norm[src] * norm[tgt];

            // src -> tgt
            for f in 0..in_features {
                aggregated[tgt * in_features + f] += norm_coeff * x_data[src * in_features + f];
            }

            // tgt -> src (undirected)
            for f in 0..in_features {
                aggregated[src * in_features + f] += norm_coeff * x_data[tgt * in_features + f];
            }
        }

        // Create aggregated tensor
        let agg_tensor = Tensor::new(&aggregated, &[num_nodes, in_features]);

        // Apply linear transformation
        self.linear.forward(&agg_tensor)
    }
}

/// Graph Attention Network layer (Velickovic et al., 2018).
///
/// Uses attention mechanism to weight neighbor contributions:
///
/// ```text
/// α_ij = softmax_j(LeakyReLU(a^T [W*h_i || W*h_j]))
/// h_i' = σ(Σ_j α_ij * W * h_j)
/// ```
///
/// # Example
///
/// ```ignore
/// use aprender::gnn::GATConv;
///
/// let gat = GATConv::new(16, 32, 4);  // 16 in → 32 out, 4 attention heads
/// ```
#[derive(Debug)]
pub struct GATConv {
    /// Linear transformation for node features
    linear: Linear,
    /// Attention weights (for computing attention scores)
    attention_src: Tensor,
    /// Attention weights for target nodes
    attention_tgt: Tensor,
    /// Input feature dimension
    in_features: usize,
    /// Output feature dimension per head
    out_features: usize,
    /// Number of attention heads
    num_heads: usize,
    /// Negative slope for `LeakyReLU`
    negative_slope: f32,
    /// Whether to add self-loops
    add_self_loops: bool,
}

impl GATConv {
    /// Create a new GAT convolutional layer.
    ///
    /// # Arguments
    ///
    /// * `in_features` - Input feature dimension
    /// * `out_features` - Output feature dimension per attention head
    /// * `num_heads` - Number of attention heads
    #[must_use]
    pub fn new(in_features: usize, out_features: usize, num_heads: usize) -> Self {
        let total_out = out_features * num_heads;

        // Initialize attention vectors
        let attn_data: Vec<f32> = (0..total_out)
            .map(|i| ((i % 5) as f32 - 2.0) * 0.1)
            .collect();

        Self {
            linear: Linear::new(in_features, total_out),
            attention_src: Tensor::new(&attn_data, &[num_heads, out_features]).requires_grad(),
            attention_tgt: Tensor::new(&attn_data, &[num_heads, out_features]).requires_grad(),
            in_features,
            out_features,
            num_heads,
            negative_slope: 0.2,
            add_self_loops: true,
        }
    }

    /// Get number of attention heads.
    #[must_use]
    pub fn num_heads(&self) -> usize {
        self.num_heads
    }

    /// Get output dimension per head.
    #[must_use]
    pub fn out_features(&self) -> usize {
        self.out_features
    }

    /// Total output dimension (`out_features` * `num_heads`).
    #[must_use]
    pub fn total_out_features(&self) -> usize {
        self.out_features * self.num_heads
    }
}

impl Module for GATConv {
    fn forward(&self, _input: &Tensor) -> Tensor {
        panic!("GATConv requires graph structure. Use forward_gnn() instead.");
    }

    fn parameters(&self) -> Vec<&Tensor> {
        let mut params = self.linear.parameters();
        params.push(&self.attention_src);
        params.push(&self.attention_tgt);
        params
    }

    fn parameters_mut(&mut self) -> Vec<&mut Tensor> {
        let mut params = self.linear.parameters_mut();
        params.push(&mut self.attention_src);
        params.push(&mut self.attention_tgt);
        params
    }
}

impl GNNModule for GATConv {
    fn forward_gnn(&self, x: &Tensor, edge_index: &[EdgeIndex]) -> Tensor {
        let num_nodes = x.shape()[0];
        let in_features = x.shape()[1];

        assert_eq!(
            in_features, self.in_features,
            "Expected {} input features, got {}",
            self.in_features, in_features
        );

        // Transform features: [num_nodes, in_features] -> [num_nodes, num_heads * out_features]
        let h = self.linear.forward(x);
        let h_data = h.data();

        let total_out = self.num_heads * self.out_features;

        // Build edge list with self-loops
        let mut edges: Vec<EdgeIndex> = edge_index.to_vec();
        if self.add_self_loops {
            for i in 0..num_nodes {
                edges.push((i, i));
            }
        }

        // For each node, collect attention scores and aggregate
        let mut output = vec![0.0f32; num_nodes * total_out];

        // Group edges by target node
        let mut neighbors: Vec<Vec<usize>> = vec![vec![]; num_nodes];
        for &(src, tgt) in &edges {
            neighbors[tgt].push(src);
        }

        // Attention computation per head
        let attn_src_data = self.attention_src.data();
        let attn_tgt_data = self.attention_tgt.data();

        for tgt in 0..num_nodes {
            if neighbors[tgt].is_empty() {
                continue;
            }

            for head in 0..self.num_heads {
                let head_offset = head * self.out_features;

                // Compute attention scores for all neighbors
                let mut scores: Vec<f32> = Vec::with_capacity(neighbors[tgt].len());
                let mut max_score = f32::NEG_INFINITY;

                for &src in &neighbors[tgt] {
                    // e_ij = LeakyReLU(a_src^T * h_src + a_tgt^T * h_tgt)
                    let mut score = 0.0;

                    for f in 0..self.out_features {
                        let h_src_f = h_data[src * total_out + head_offset + f];
                        let h_tgt_f = h_data[tgt * total_out + head_offset + f];
                        score += attn_src_data[head * self.out_features + f] * h_src_f
                            + attn_tgt_data[head * self.out_features + f] * h_tgt_f;
                    }

                    // LeakyReLU
                    if score < 0.0 {
                        score *= self.negative_slope;
                    }

                    scores.push(score);
                    max_score = max_score.max(score);
                }

                // Softmax normalization (numerically stable)
                let mut exp_sum = 0.0;
                for s in &mut scores {
                    *s = (*s - max_score).exp();
                    exp_sum += *s;
                }
                for s in &mut scores {
                    *s /= exp_sum.max(1e-8);
                }

                // Aggregate: h'_tgt = Σ α_ij * h_src
                for (idx, &src) in neighbors[tgt].iter().enumerate() {
                    let alpha = scores[idx];
                    for f in 0..self.out_features {
                        output[tgt * total_out + head_offset + f] +=
                            alpha * h_data[src * total_out + head_offset + f];
                    }
                }
            }
        }

        Tensor::new(&output, &[num_nodes, total_out])
    }
}

mod gin_conv;
pub use gin_conv::*;

mod accumulate;
pub use accumulate::*;

#[cfg(test)]
mod tests;