sevensense-learning 0.1.0

GNN-based learning and embedding refinement for 7sense bioacoustics platform
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
//! Attention mechanisms for GNN models.
//!
//! This module provides attention layers and mechanisms including:
//! - Single-head attention
//! - Multi-head attention
//! - Graph-level attention readout

use ndarray::{Array1, Array2, Axis};
use rand::Rng;
use rand_distr::{Distribution, Uniform};

/// Error type for attention operations
#[derive(Debug, thiserror::Error)]
pub enum AttentionError {
    /// Dimension mismatch
    #[error("Dimension mismatch: expected {expected}, got {actual}")]
    DimensionMismatch { expected: usize, actual: usize },

    /// Invalid configuration
    #[error("Invalid attention configuration: {0}")]
    InvalidConfig(String),

    /// Computation error
    #[error("Attention computation error: {0}")]
    ComputationError(String),
}

/// Result type for attention operations
pub type AttentionResult<T> = Result<T, AttentionError>;

/// Single-head attention layer
#[derive(Debug, Clone)]
pub struct AttentionLayer {
    /// Query weight matrix
    query_weights: Array2<f32>,
    /// Key weight matrix
    key_weights: Array2<f32>,
    /// Value weight matrix
    value_weights: Array2<f32>,
    /// Attention dimension
    attention_dim: usize,
    /// Input dimension
    input_dim: usize,
    /// Output dimension
    output_dim: usize,
    /// Scaling factor
    scale: f32,
}

impl AttentionLayer {
    /// Create a new attention layer
    #[must_use]
    pub fn new(input_dim: usize, attention_dim: usize) -> Self {
        let query_weights = xavier_init(input_dim, attention_dim);
        let key_weights = xavier_init(input_dim, attention_dim);
        let value_weights = xavier_init(input_dim, attention_dim);
        let scale = (attention_dim as f32).sqrt();

        Self {
            query_weights,
            key_weights,
            value_weights,
            attention_dim,
            input_dim,
            output_dim: attention_dim,
            scale,
        }
    }

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

    /// Compute attention scores
    pub fn compute_attention(
        &self,
        query: &Array2<f32>,
        key: &Array2<f32>,
        mask: Option<&Array2<f32>>,
    ) -> AttentionResult<Array2<f32>> {
        // Q * K^T / sqrt(d_k)
        let scores = query.dot(&key.t()) / self.scale;

        // Apply mask if provided (set masked positions to -inf)
        let scores = if let Some(mask) = mask {
            let mut masked = scores;
            for i in 0..masked.nrows() {
                for j in 0..masked.ncols() {
                    if mask[[i, j]] == 0.0 {
                        masked[[i, j]] = f32::NEG_INFINITY;
                    }
                }
            }
            masked
        } else {
            scores
        };

        // Softmax
        let attention_weights = softmax_2d(&scores);

        Ok(attention_weights)
    }

    /// Forward pass through the attention layer
    pub fn forward(&self, features: &Array2<f32>) -> AttentionResult<Array2<f32>> {
        if features.ncols() != self.input_dim {
            return Err(AttentionError::DimensionMismatch {
                expected: self.input_dim,
                actual: features.ncols(),
            });
        }

        // Compute Q, K, V
        let q = features.dot(&self.query_weights);
        let k = features.dot(&self.key_weights);
        let v = features.dot(&self.value_weights);

        // Compute attention weights
        let attention_weights = self.compute_attention(&q, &k, None)?;

        // Apply attention to values
        let output = attention_weights.dot(&v);

        Ok(output)
    }

    /// Forward pass with explicit Q, K, V
    pub fn forward_qkv(
        &self,
        query: &Array2<f32>,
        key: &Array2<f32>,
        value: &Array2<f32>,
        mask: Option<&Array2<f32>>,
    ) -> AttentionResult<Array2<f32>> {
        // Transform Q, K, V
        let q = query.dot(&self.query_weights);
        let k = key.dot(&self.key_weights);
        let v = value.dot(&self.value_weights);

        // Compute attention weights
        let attention_weights = self.compute_attention(&q, &k, mask)?;

        // Apply attention to values
        let output = attention_weights.dot(&v);

        Ok(output)
    }

    /// Graph-level readout using attention
    pub fn graph_readout(&self, node_features: &Array2<f32>) -> AttentionResult<Array1<f32>> {
        // Compute attention-weighted mean of node features
        let attended = self.forward(node_features)?;

        // Mean over nodes
        let mean = attended.mean_axis(Axis(0)).unwrap();

        Ok(mean)
    }

    /// Update weights with gradient
    pub fn update_weights(&mut self, _lr: f32, weight_decay: f32) {
        self.query_weights -= &(&self.query_weights * weight_decay);
        self.key_weights -= &(&self.key_weights * weight_decay);
        self.value_weights -= &(&self.value_weights * weight_decay);
    }
}

/// Multi-head attention layer
#[derive(Debug, Clone)]
pub struct MultiHeadAttention {
    /// Individual attention heads
    heads: Vec<AttentionLayer>,
    /// Output projection
    output_projection: Array2<f32>,
    /// Number of heads
    num_heads: usize,
    /// Dimension per head
    head_dim: usize,
    /// Total output dimension
    output_dim: usize,
    /// Dropout probability
    dropout: f32,
}

impl MultiHeadAttention {
    /// Create a new multi-head attention layer
    #[must_use]
    pub fn new(input_dim: usize, num_heads: usize, head_dim: usize, dropout: f32) -> Self {
        let mut heads = Vec::with_capacity(num_heads);
        for _ in 0..num_heads {
            heads.push(AttentionLayer::new(input_dim, head_dim));
        }

        let total_dim = num_heads * head_dim;
        let output_projection = xavier_init(total_dim, input_dim);

        Self {
            heads,
            output_projection,
            num_heads,
            head_dim,
            output_dim: input_dim,
            dropout,
        }
    }

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

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

    /// Forward pass through multi-head attention
    pub fn forward(&self, features: &Array2<f32>) -> AttentionResult<Array2<f32>> {
        let n = features.nrows();

        // Compute attention for each head
        let mut head_outputs = Vec::with_capacity(self.num_heads);
        for head in &self.heads {
            let output = head.forward(features)?;
            head_outputs.push(output);
        }

        // Concatenate head outputs
        let mut concat = Array2::zeros((n, self.num_heads * self.head_dim));
        for (h, output) in head_outputs.iter().enumerate() {
            let start = h * self.head_dim;
            for i in 0..n {
                for j in 0..self.head_dim {
                    concat[[i, start + j]] = output[[i, j]];
                }
            }
        }

        // Apply output projection
        let output = concat.dot(&self.output_projection);

        Ok(output)
    }

    /// Forward pass with explicit Q, K, V
    pub fn forward_qkv(
        &self,
        query: &Array2<f32>,
        key: &Array2<f32>,
        value: &Array2<f32>,
        mask: Option<&Array2<f32>>,
    ) -> AttentionResult<Array2<f32>> {
        let n = query.nrows();

        // Compute attention for each head
        let mut head_outputs = Vec::with_capacity(self.num_heads);
        for head in &self.heads {
            let output = head.forward_qkv(query, key, value, mask)?;
            head_outputs.push(output);
        }

        // Concatenate head outputs
        let mut concat = Array2::zeros((n, self.num_heads * self.head_dim));
        for (h, output) in head_outputs.iter().enumerate() {
            let start = h * self.head_dim;
            for i in 0..n {
                for j in 0..self.head_dim {
                    concat[[i, start + j]] = output[[i, j]];
                }
            }
        }

        // Apply output projection
        let output = concat.dot(&self.output_projection);

        Ok(output)
    }

    /// Graph-level readout using multi-head attention
    pub fn graph_readout(&self, node_features: &Array2<f32>) -> AttentionResult<Array1<f32>> {
        let attended = self.forward(node_features)?;
        let mean = attended.mean_axis(Axis(0)).unwrap();
        Ok(mean)
    }
}

/// Cross-attention between two sequences
#[derive(Debug, Clone)]
pub struct CrossAttention {
    /// Query projection for source
    query_proj: Array2<f32>,
    /// Key projection for target
    key_proj: Array2<f32>,
    /// Value projection for target
    value_proj: Array2<f32>,
    /// Attention dimension
    attention_dim: usize,
    /// Source dimension
    source_dim: usize,
    /// Target dimension
    target_dim: usize,
}

impl CrossAttention {
    /// Create a new cross-attention layer
    #[must_use]
    pub fn new(source_dim: usize, target_dim: usize, attention_dim: usize) -> Self {
        Self {
            query_proj: xavier_init(source_dim, attention_dim),
            key_proj: xavier_init(target_dim, attention_dim),
            value_proj: xavier_init(target_dim, attention_dim),
            attention_dim,
            source_dim,
            target_dim,
        }
    }

    /// Compute cross-attention between source and target
    pub fn forward(
        &self,
        source: &Array2<f32>,
        target: &Array2<f32>,
    ) -> AttentionResult<Array2<f32>> {
        // Project source to query
        let query = source.dot(&self.query_proj);

        // Project target to key and value
        let key = target.dot(&self.key_proj);
        let value = target.dot(&self.value_proj);

        // Compute attention scores
        let scale = (self.attention_dim as f32).sqrt();
        let scores = query.dot(&key.t()) / scale;

        // Softmax
        let attention_weights = softmax_2d(&scores);

        // Apply attention to values
        let output = attention_weights.dot(&value);

        Ok(output)
    }
}

/// Set attention for set-to-set operations
#[derive(Debug, Clone)]
pub struct SetAttention {
    /// Multi-head attention
    mha: MultiHeadAttention,
    /// Layer normalization parameters
    layer_norm_weight: Array1<f32>,
    layer_norm_bias: Array1<f32>,
    /// Feed-forward network
    ffn_w1: Array2<f32>,
    ffn_w2: Array2<f32>,
    /// Dimensions
    input_dim: usize,
    hidden_dim: usize,
}

impl SetAttention {
    /// Create a new set attention layer (Set Transformer style)
    #[must_use]
    pub fn new(input_dim: usize, num_heads: usize, hidden_dim: usize) -> Self {
        let head_dim = input_dim / num_heads;
        let mha = MultiHeadAttention::new(input_dim, num_heads, head_dim, 0.0);

        Self {
            mha,
            layer_norm_weight: Array1::ones(input_dim),
            layer_norm_bias: Array1::zeros(input_dim),
            ffn_w1: xavier_init(input_dim, hidden_dim),
            ffn_w2: xavier_init(hidden_dim, input_dim),
            input_dim,
            hidden_dim,
        }
    }

    /// Forward pass with self-attention and feed-forward
    pub fn forward(&self, features: &Array2<f32>) -> AttentionResult<Array2<f32>> {
        // Self-attention with residual
        let attended = self.mha.forward(features)?;
        let residual1 = features + &attended;
        let normed1 = layer_norm(&residual1, &self.layer_norm_weight, &self.layer_norm_bias);

        // Feed-forward with residual
        let hidden = normed1.dot(&self.ffn_w1).mapv(|x| x.max(0.0)); // ReLU
        let ffn_out = hidden.dot(&self.ffn_w2);
        let residual2 = &normed1 + &ffn_out;
        let output = layer_norm(&residual2, &self.layer_norm_weight, &self.layer_norm_bias);

        Ok(output)
    }

    /// Aggregate set elements using learned attention
    pub fn aggregate(&self, features: &Array2<f32>) -> AttentionResult<Array1<f32>> {
        let transformed = self.forward(features)?;
        Ok(transformed.mean_axis(Axis(0)).unwrap())
    }
}

// =========== Helper Functions ===========

/// Xavier/Glorot initialization
fn xavier_init(fan_in: usize, fan_out: usize) -> Array2<f32> {
    let limit = (6.0 / (fan_in + fan_out) as f32).sqrt();
    let uniform = Uniform::new(-limit, limit);
    let mut rng = rand::thread_rng();

    Array2::from_shape_fn((fan_in, fan_out), |_| uniform.sample(&mut rng))
}

/// Softmax over 2D array (row-wise)
fn softmax_2d(scores: &Array2<f32>) -> Array2<f32> {
    let mut result = scores.clone();

    for mut row in result.rows_mut() {
        // Subtract max for numerical stability
        let max = row.iter().cloned().fold(f32::NEG_INFINITY, f32::max);

        let mut sum = 0.0;
        for val in row.iter_mut() {
            if val.is_finite() {
                *val = (*val - max).exp();
                sum += *val;
            } else {
                *val = 0.0;
            }
        }

        if sum > 0.0 {
            row /= sum;
        }
    }

    result
}

/// Layer normalization
fn layer_norm(x: &Array2<f32>, weight: &Array1<f32>, bias: &Array1<f32>) -> Array2<f32> {
    let eps = 1e-5;
    let mut result = x.clone();

    for mut row in result.rows_mut() {
        let mean = row.mean().unwrap_or(0.0);
        let variance = row.iter().map(|&v| (v - mean).powi(2)).sum::<f32>() / row.len() as f32;
        let std = (variance + eps).sqrt();

        for (i, val) in row.iter_mut().enumerate() {
            *val = (*val - mean) / std * weight[i] + bias[i];
        }
    }

    result
}

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

    #[test]
    fn test_attention_layer() {
        let layer = AttentionLayer::new(8, 16);
        assert_eq!(layer.output_dim(), 16);

        let features = Array2::from_elem((3, 8), 0.5);
        let output = layer.forward(&features).unwrap();

        assert_eq!(output.shape(), &[3, 16]);
    }

    #[test]
    fn test_multi_head_attention() {
        let mha = MultiHeadAttention::new(8, 4, 4, 0.0);
        assert_eq!(mha.num_heads(), 4);
        assert_eq!(mha.output_dim(), 8);

        let features = Array2::from_elem((5, 8), 0.5);
        let output = mha.forward(&features).unwrap();

        assert_eq!(output.shape(), &[5, 8]);
    }

    #[test]
    fn test_cross_attention() {
        let cross = CrossAttention::new(8, 16, 32);

        let source = Array2::from_elem((3, 8), 0.5);
        let target = Array2::from_elem((5, 16), 0.5);

        let output = cross.forward(&source, &target).unwrap();
        assert_eq!(output.shape(), &[3, 32]);
    }

    #[test]
    fn test_set_attention() {
        let set_attn = SetAttention::new(16, 4, 64);

        let features = Array2::from_elem((4, 16), 0.5);
        let output = set_attn.forward(&features).unwrap();

        assert_eq!(output.shape(), &[4, 16]);

        let aggregated = set_attn.aggregate(&features).unwrap();
        assert_eq!(aggregated.len(), 16);
    }

    #[test]
    fn test_softmax() {
        let scores = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();

        let probs = softmax_2d(&scores);

        // Each row should sum to 1
        for row in probs.rows() {
            let sum: f32 = row.iter().sum();
            assert!((sum - 1.0).abs() < 1e-5);
        }

        // Higher values should have higher probabilities
        assert!(probs[[0, 2]] > probs[[0, 1]]);
        assert!(probs[[0, 1]] > probs[[0, 0]]);
    }

    #[test]
    fn test_layer_norm() {
        let x = Array2::from_shape_vec((2, 4), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).unwrap();

        let weight = Array1::ones(4);
        let bias = Array1::zeros(4);

        let normed = layer_norm(&x, &weight, &bias);

        // Each row should have mean ~0 and std ~1
        for row in normed.rows() {
            let mean: f32 = row.iter().sum::<f32>() / row.len() as f32;
            assert!(mean.abs() < 1e-5);
        }
    }

    #[test]
    fn test_graph_readout() {
        let layer = AttentionLayer::new(8, 4);
        let node_features = Array2::from_elem((5, 8), 0.5);

        let readout = layer.graph_readout(&node_features).unwrap();
        assert_eq!(readout.len(), 4);
    }

    #[test]
    fn test_attention_with_mask() {
        let layer = AttentionLayer::new(4, 4);

        let features = Array2::from_elem((3, 4), 0.5);

        // Create a mask that blocks second and third positions
        let mut mask = Array2::ones((3, 3));
        mask[[0, 1]] = 0.0;
        mask[[0, 2]] = 0.0;

        let query = features.dot(&layer.query_weights);
        let key = features.dot(&layer.key_weights);

        let attn_weights = layer.compute_attention(&query, &key, Some(&mask)).unwrap();

        // First row should only attend to itself
        assert!(attn_weights[[0, 0]] > 0.99); // Almost all attention to self
        assert!(attn_weights[[0, 1]] < 0.01);
        assert!(attn_weights[[0, 2]] < 0.01);
    }
}