ruvector-cnn 2.0.6

CNN feature extraction for image embeddings with SIMD acceleration
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
//! # Triplet Loss
//!
//! Implementation of the Triplet Loss for metric learning.
//!
//! ## Mathematical Formulation
//!
//! ```text
//! L(a, p, n) = max(0, ||a - p||^2 - ||a - n||^2 + margin)
//! ```
//!
//! Where:
//! - `a` is the anchor embedding
//! - `p` is the positive (similar) embedding
//! - `n` is the negative (dissimilar) embedding
//! - `margin` is the minimum desired separation
//!
//! ## Variants
//!
//! - **Standard**: Uses Euclidean distance
//! - **Angular**: Uses angular distance for normalized embeddings
//! - **Soft**: Uses soft-margin (log-exp) for smoother gradients
//!
//! ## References
//!
//! - "FaceNet: A Unified Embedding for Face Recognition and Clustering"
//! - "Deep Metric Learning Using Triplet Network"

use crate::error::{CnnError, CnnResult};
use serde::{Deserialize, Serialize};

/// Distance metric for triplet loss computation.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum TripletDistance {
    /// Euclidean (L2) distance
    Euclidean,
    /// Squared Euclidean distance (avoids sqrt, faster)
    SquaredEuclidean,
    /// Cosine distance (1 - cosine_similarity)
    Cosine,
}

/// Result of triplet loss computation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TripletResult {
    /// The computed loss value
    pub loss: f64,
    /// Distance between anchor and positive
    pub positive_distance: f64,
    /// Distance between anchor and negative
    pub negative_distance: f64,
    /// Whether the triplet is a "hard" triplet (loss > 0)
    pub is_hard: bool,
    /// Whether the triplet violates the margin
    pub violates_margin: bool,
}

/// Triplet loss for metric learning.
///
/// # Example
///
/// ```rust
/// use ruvector_cnn::contrastive::TripletLoss;
///
/// let triplet = TripletLoss::new(1.0);
///
/// let anchor = vec![1.0, 0.0, 0.0];
/// let positive = vec![0.9, 0.1, 0.0];  // similar to anchor
/// let negative = vec![0.0, 1.0, 0.0];  // dissimilar to anchor
///
/// let loss = triplet.forward(&anchor, &positive, &negative);
/// assert!(loss >= 0.0);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TripletLoss {
    /// Margin for the triplet loss (default: 1.0)
    margin: f64,
    /// Distance metric to use
    distance: TripletDistance,
    /// Whether to use soft margin (log-exp smoothing)
    soft_margin: bool,
    /// L2 regularization weight (optional)
    l2_regularization: Option<f64>,
}

impl TripletLoss {
    /// Create a new triplet loss with the specified margin.
    ///
    /// # Arguments
    ///
    /// * `margin` - The minimum desired separation between positive and negative
    ///   distances. Typical values: 0.2-2.0
    ///
    /// # Panics
    ///
    /// Panics if margin is negative.
    pub fn new(margin: f64) -> Self {
        assert!(margin >= 0.0, "Margin must be non-negative");
        Self {
            margin,
            distance: TripletDistance::SquaredEuclidean,
            soft_margin: false,
            l2_regularization: None,
        }
    }

    /// Set the distance metric.
    pub fn with_distance(mut self, distance: TripletDistance) -> Self {
        self.distance = distance;
        self
    }

    /// Enable soft margin for smoother gradients.
    ///
    /// Instead of `max(0, x)`, uses `log(1 + exp(x))`.
    pub fn with_soft_margin(mut self) -> Self {
        self.soft_margin = true;
        self
    }

    /// Add L2 regularization on embeddings.
    pub fn with_l2_regularization(mut self, weight: f64) -> Self {
        self.l2_regularization = Some(weight);
        self
    }

    /// Get the margin.
    pub fn margin(&self) -> f64 {
        self.margin
    }

    /// Get the distance metric.
    pub fn distance_metric(&self) -> TripletDistance {
        self.distance
    }

    /// Compute triplet loss for a single triplet.
    ///
    /// # Arguments
    ///
    /// * `anchor` - The anchor embedding
    /// * `positive` - The positive (similar) embedding
    /// * `negative` - The negative (dissimilar) embedding
    ///
    /// # Returns
    ///
    /// The triplet loss value (non-negative).
    pub fn forward(&self, anchor: &[f64], positive: &[f64], negative: &[f64]) -> f64 {
        self.forward_detailed(anchor, positive, negative)
            .map(|r| r.loss)
            .unwrap_or(0.0)
    }

    /// Compute triplet loss with detailed results.
    pub fn forward_detailed(
        &self,
        anchor: &[f64],
        positive: &[f64],
        negative: &[f64],
    ) -> CnnResult<TripletResult> {
        // Validate inputs
        if anchor.is_empty() {
            return Err(CnnError::InvalidInput("anchor cannot be empty".to_string()));
        }

        let dim = anchor.len();
        if positive.len() != dim {
            return Err(CnnError::DimensionMismatch(format!(
                "positive has dimension {}, expected {}",
                positive.len(),
                dim
            )));
        }
        if negative.len() != dim {
            return Err(CnnError::DimensionMismatch(format!(
                "negative has dimension {}, expected {}",
                negative.len(),
                dim
            )));
        }

        // Check for NaN/Inf
        for (name, vec) in [("anchor", anchor), ("positive", positive), ("negative", negative)] {
            if vec.iter().any(|x| x.is_nan() || x.is_infinite()) {
                return Err(CnnError::InvalidInput(format!(
                    "{} contains NaN or Inf",
                    name
                )));
            }
        }

        // Compute distances
        let pos_dist = self.compute_distance(anchor, positive);
        let neg_dist = self.compute_distance(anchor, negative);

        // Compute loss
        let diff = pos_dist - neg_dist + self.margin;
        let loss = if self.soft_margin {
            soft_relu(diff)
        } else {
            diff.max(0.0)
        };

        // Add L2 regularization if enabled
        let loss = if let Some(weight) = self.l2_regularization {
            let anchor_norm: f64 = anchor.iter().map(|x| x * x).sum();
            let pos_norm: f64 = positive.iter().map(|x| x * x).sum();
            let neg_norm: f64 = negative.iter().map(|x| x * x).sum();
            loss + weight * (anchor_norm + pos_norm + neg_norm) / 3.0
        } else {
            loss
        };

        Ok(TripletResult {
            loss,
            positive_distance: pos_dist,
            negative_distance: neg_dist,
            is_hard: diff > 0.0,
            violates_margin: pos_dist + self.margin > neg_dist,
        })
    }

    /// Compute batch triplet loss.
    ///
    /// # Arguments
    ///
    /// * `anchors` - Batch of anchor embeddings
    /// * `positives` - Batch of positive embeddings
    /// * `negatives` - Batch of negative embeddings
    ///
    /// # Returns
    ///
    /// Mean triplet loss across the batch.
    pub fn forward_batch(
        &self,
        anchors: &[Vec<f64>],
        positives: &[Vec<f64>],
        negatives: &[Vec<f64>],
    ) -> CnnResult<f64> {
        if anchors.len() != positives.len() || anchors.len() != negatives.len() {
            return Err(CnnError::DimensionMismatch(format!(
                "Batch sizes must match: anchors={}, positives={}, negatives={}",
                anchors.len(),
                positives.len(),
                negatives.len()
            )));
        }

        if anchors.is_empty() {
            return Err(CnnError::InvalidInput("batch cannot be empty".to_string()));
        }

        let mut total_loss = 0.0;
        for ((anchor, positive), negative) in anchors.iter().zip(positives).zip(negatives) {
            total_loss += self.forward(anchor, positive, negative);
        }

        Ok(total_loss / anchors.len() as f64)
    }

    /// Mine hard triplets from a batch.
    ///
    /// Returns indices of (anchor, positive, negative) triplets where the loss is positive.
    ///
    /// # Arguments
    ///
    /// * `embeddings` - All embeddings in the batch
    /// * `labels` - Class labels for each embedding
    ///
    /// # Returns
    ///
    /// Vector of (anchor_idx, positive_idx, negative_idx) tuples.
    pub fn mine_hard_triplets(
        &self,
        embeddings: &[Vec<f64>],
        labels: &[usize],
    ) -> Vec<(usize, usize, usize)> {
        if embeddings.len() != labels.len() {
            return vec![];
        }

        let n = embeddings.len();
        let mut triplets = Vec::new();

        // Precompute distance matrix
        let distances = self.compute_distance_matrix(embeddings);

        for anchor_idx in 0..n {
            let anchor_label = labels[anchor_idx];

            // Find hardest positive (furthest with same label)
            let mut hardest_pos_idx = None;
            let mut hardest_pos_dist = f64::NEG_INFINITY;

            // Find hardest negative (closest with different label)
            let mut hardest_neg_idx = None;
            let mut hardest_neg_dist = f64::INFINITY;

            for other_idx in 0..n {
                if other_idx == anchor_idx {
                    continue;
                }

                let dist = distances[anchor_idx][other_idx];

                if labels[other_idx] == anchor_label {
                    // Same class - potential positive
                    if dist > hardest_pos_dist {
                        hardest_pos_dist = dist;
                        hardest_pos_idx = Some(other_idx);
                    }
                } else {
                    // Different class - potential negative
                    if dist < hardest_neg_dist {
                        hardest_neg_dist = dist;
                        hardest_neg_idx = Some(other_idx);
                    }
                }
            }

            // Add triplet if valid and hard
            if let (Some(pos_idx), Some(neg_idx)) = (hardest_pos_idx, hardest_neg_idx) {
                if hardest_pos_dist - hardest_neg_dist + self.margin > 0.0 {
                    triplets.push((anchor_idx, pos_idx, neg_idx));
                }
            }
        }

        triplets
    }

    /// Compute distance between two vectors.
    #[inline]
    fn compute_distance(&self, a: &[f64], b: &[f64]) -> f64 {
        match self.distance {
            TripletDistance::Euclidean => {
                let sum_sq: f64 = a.iter().zip(b).map(|(x, y)| (x - y).powi(2)).sum();
                sum_sq.sqrt()
            }
            TripletDistance::SquaredEuclidean => {
                a.iter().zip(b).map(|(x, y)| (x - y).powi(2)).sum()
            }
            TripletDistance::Cosine => {
                let mut dot = 0.0;
                let mut norm_a_sq = 0.0;
                let mut norm_b_sq = 0.0;

                for (x, y) in a.iter().zip(b) {
                    dot += x * y;
                    norm_a_sq += x * x;
                    norm_b_sq += y * y;
                }

                let norm = (norm_a_sq * norm_b_sq).sqrt();
                if norm < 1e-8 {
                    1.0 // Maximum distance for zero vectors
                } else {
                    1.0 - dot / norm
                }
            }
        }
    }

    /// Compute pairwise distance matrix.
    fn compute_distance_matrix(&self, embeddings: &[Vec<f64>]) -> Vec<Vec<f64>> {
        let n = embeddings.len();
        let mut matrix = vec![vec![0.0; n]; n];

        for i in 0..n {
            for j in (i + 1)..n {
                let dist = self.compute_distance(&embeddings[i], &embeddings[j]);
                matrix[i][j] = dist;
                matrix[j][i] = dist;
            }
        }

        matrix
    }
}

impl Default for TripletLoss {
    fn default() -> Self {
        Self::new(1.0)
    }
}

/// Soft ReLU: log(1 + exp(x)) for smooth gradients.
#[inline]
fn soft_relu(x: f64) -> f64 {
    if x > 20.0 {
        x // Avoid overflow
    } else if x < -20.0 {
        0.0 // Underflow to 0
    } else {
        (1.0 + x.exp()).ln()
    }
}

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

    #[test]
    fn test_triplet_basic() {
        let triplet = TripletLoss::new(1.0);

        let anchor = vec![1.0, 0.0, 0.0];
        let positive = vec![0.9, 0.1, 0.0];
        let negative = vec![0.0, 1.0, 0.0];

        let loss = triplet.forward(&anchor, &positive, &negative);
        assert!(loss >= 0.0);
    }

    #[test]
    fn test_triplet_zero_loss() {
        let triplet = TripletLoss::new(0.1);

        // Negative is far, positive is close - should be zero loss
        let anchor = vec![1.0, 0.0];
        let positive = vec![1.0, 0.0]; // identical
        let negative = vec![-1.0, 0.0]; // opposite

        let result = triplet.forward_detailed(&anchor, &positive, &negative).unwrap();
        assert_eq!(result.loss, 0.0);
        assert!(!result.is_hard);
    }

    #[test]
    fn test_triplet_hard() {
        let triplet = TripletLoss::new(1.0);

        // Negative is closer than positive - hard triplet
        let anchor = vec![0.0, 0.0];
        let positive = vec![2.0, 0.0];
        let negative = vec![1.0, 0.0];

        let result = triplet.forward_detailed(&anchor, &positive, &negative).unwrap();
        assert!(result.loss > 0.0);
        assert!(result.is_hard);
        assert!(result.violates_margin);
    }

    #[test]
    fn test_triplet_distances() {
        // Test Euclidean distance
        let triplet_euclidean = TripletLoss::new(0.0).with_distance(TripletDistance::Euclidean);
        let a = vec![0.0, 0.0];
        let b = vec![3.0, 4.0];
        let c = vec![0.0, 0.0];

        let result = triplet_euclidean.forward_detailed(&a, &b, &c).unwrap();
        assert!((result.positive_distance - 5.0).abs() < 1e-6);
        assert!(result.negative_distance.abs() < 1e-6);

        // Test cosine distance
        let triplet_cosine = TripletLoss::new(0.0).with_distance(TripletDistance::Cosine);
        let x = vec![1.0, 0.0];
        let y = vec![0.0, 1.0];
        let z = vec![1.0, 0.0];

        let result = triplet_cosine.forward_detailed(&x, &y, &z).unwrap();
        assert!((result.positive_distance - 1.0).abs() < 1e-6); // orthogonal = 1
        assert!(result.negative_distance.abs() < 1e-6); // identical = 0
    }

    #[test]
    fn test_soft_margin() {
        let hard = TripletLoss::new(1.0);
        let soft = TripletLoss::new(1.0).with_soft_margin();

        let anchor = vec![0.0, 0.0];
        let positive = vec![1.0, 0.0];
        let negative = vec![0.5, 0.0];

        let hard_loss = hard.forward(&anchor, &positive, &negative);
        let soft_loss = soft.forward(&anchor, &positive, &negative);

        // Soft margin should be >= hard margin
        assert!(soft_loss >= hard_loss);
        // Both should be positive for this hard triplet
        assert!(hard_loss > 0.0);
        assert!(soft_loss > 0.0);
    }

    #[test]
    fn test_batch_triplet() {
        let triplet = TripletLoss::new(1.0);

        let anchors = vec![vec![1.0, 0.0], vec![0.0, 1.0]];
        let positives = vec![vec![0.9, 0.1], vec![0.1, 0.9]];
        let negatives = vec![vec![0.0, 1.0], vec![1.0, 0.0]];

        let loss = triplet.forward_batch(&anchors, &positives, &negatives).unwrap();
        assert!(loss >= 0.0);
    }

    #[test]
    fn test_mine_hard_triplets() {
        // Use a smaller margin so triplets are more likely to be "hard"
        let triplet = TripletLoss::new(0.01);

        // Create embeddings where hard triplets are guaranteed to exist
        // Class 0 and class 1 embeddings are close to each other
        let embeddings = vec![
            vec![1.0, 0.0],   // class 0 - anchor
            vec![0.95, 0.05], // class 0 - positive (close to anchor)
            vec![0.9, 0.1],   // class 1 - negative (also close, creating hard triplet)
            vec![0.85, 0.15], // class 1 - another negative
        ];
        let labels = vec![0, 0, 1, 1];

        let hard_triplets = triplet.mine_hard_triplets(&embeddings, &labels);

        // Verify triplet structure for any hard triplets found
        // Note: hard triplets may not always be found depending on the margin and embeddings
        for (a, p, n) in &hard_triplets {
            assert_eq!(labels[*a], labels[*p]); // anchor and positive same class
            assert_ne!(labels[*a], labels[*n]); // anchor and negative different class
        }

        // The function should at least return a valid (possibly empty) vec
        // If hard triplets are found, they should have valid structure (tested above)
    }

    #[test]
    fn test_l2_regularization() {
        let no_reg = TripletLoss::new(0.0);
        let with_reg = TripletLoss::new(0.0).with_l2_regularization(0.01);

        let anchor = vec![10.0, 0.0];
        let positive = vec![10.0, 0.0];
        let negative = vec![-10.0, 0.0];

        let loss_no_reg = no_reg.forward(&anchor, &positive, &negative);
        let loss_with_reg = with_reg.forward(&anchor, &positive, &negative);

        // With L2 regularization, loss should be higher for large embeddings
        assert!(loss_with_reg > loss_no_reg);
    }

    #[test]
    fn test_error_handling() {
        let triplet = TripletLoss::new(1.0);

        // Empty anchor
        let result = triplet.forward_detailed(&[], &[1.0], &[1.0]);
        assert!(result.is_err());

        // Dimension mismatch
        let result = triplet.forward_detailed(&[1.0, 2.0], &[1.0], &[1.0, 2.0]);
        assert!(result.is_err());
    }

    #[test]
    fn test_soft_relu() {
        // Basic cases
        assert!((soft_relu(0.0) - 2.0_f64.ln()).abs() < 1e-6);
        assert!(soft_relu(-100.0) < 1e-10);
        assert!((soft_relu(100.0) - 100.0).abs() < 1e-6);

        // Smooth transition
        let x = 1.0;
        let y = soft_relu(x);
        assert!(y > x.max(0.0)); // Always >= max(0, x) but smoother
    }
}