sklears-manifold 0.1.2

Manifold learning algorithms (t-SNE, Isomap, etc.)
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
//! GPU-accelerated methods for manifold learning
//!
//! This module provides GPU-accelerated implementations of core manifold learning
//! operations using oxicuda-backend for cross-platform GPU compute support.

use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use scirs2_core::random::thread_rng;
use scirs2_core::random::SeedableRng;
use scirs2_core::StdRng;
use sklears_core::{
    error::{Result as SklResult, SklearsError},
    types::Float,
};

#[cfg(feature = "gpu")]
use sklears_core::gpu::{GpuArray, GpuContext, GpuMatrixOps};

/// GPU-accelerated distance computation for manifold learning.
///
/// Provides hardware-accelerated distance computations usable by manifold learning
/// algorithms. When GPU is unavailable, falls back to CPU computation.
///
/// # Examples
///
/// ```
/// use sklears_manifold::gpu_acceleration::GpuAccelerator;
/// use scirs2_core::ndarray::array;
///
/// let data = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];
///
/// let mut accelerator = GpuAccelerator::new().unwrap();
/// let distances = accelerator.pairwise_distances(&data.view(), "euclidean").unwrap();
/// ```
pub struct GpuAccelerator {
    #[cfg(feature = "gpu")]
    context: Option<GpuContext>,
}

impl GpuAccelerator {
    pub fn new() -> SklResult<Self> {
        #[cfg(feature = "gpu")]
        {
            match GpuContext::with_device_id(0) {
                Ok(ctx) => Ok(Self { context: Some(ctx) }),
                Err(_) => Ok(Self { context: None }),
            }
        }
        #[cfg(not(feature = "gpu"))]
        Ok(Self {})
    }

    pub fn is_gpu_available(&self) -> bool {
        #[cfg(feature = "gpu")]
        {
            self.context.is_some()
        }
        #[cfg(not(feature = "gpu"))]
        {
            false
        }
    }

    /// Compute pairwise distances between all points in the dataset.
    pub fn pairwise_distances(
        &mut self,
        data: &ArrayView2<Float>,
        metric: &str,
    ) -> SklResult<Array2<Float>> {
        if self.is_gpu_available() {
            self.gpu_pairwise_distances(data, metric)
        } else {
            self.cpu_pairwise_distances(data, metric)
        }
    }

    /// GPU pairwise distances — Euclidean via GEMM trick, others via CPU.
    fn gpu_pairwise_distances(
        &mut self,
        data: &ArrayView2<Float>,
        metric: &str,
    ) -> SklResult<Array2<Float>> {
        match metric {
            "euclidean" => self.gemm_pairwise_euclidean(data),
            _ => self.cpu_pairwise_distances(data, metric),
        }
    }

    /// Euclidean GEMM trick: D[i,j] = sqrt(||x_i||² + ||y_j||² − 2·x_i·x_j)
    ///
    /// Upload X once, compute G = X·Xᵀ on GPU, derive norms from diagonal.
    #[cfg(feature = "gpu")]
    fn gemm_pairwise_euclidean(&self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        let ctx = self.context.as_ref().expect("checked above");
        let owned = data.to_owned();
        let x_gpu = GpuArray::<Float>::from_array2(ctx, &owned)?;

        // Xáµ€: shape (d, n)
        let (n, d) = data.dim();
        let mut xt_flat = vec![0.0 as Float; n * d];
        for r in 0..n {
            for c in 0..d {
                xt_flat[c * n + r] = data[[r, c]];
            }
        }
        let xt_array = Array2::from_shape_vec((d, n), xt_flat)
            .map_err(|e| SklearsError::InvalidInput(format!("Transpose shape error: {}", e)))?;
        let xt_gpu = GpuArray::<Float>::from_array2(ctx, &xt_array)?;

        // G = X · Xᵀ  [n × n]
        let gram_gpu = x_gpu.matmul(&xt_gpu)?;
        let gram = gram_gpu.to_array2()?;

        let mut distances = Array2::<Float>::zeros((n, n));
        for i in 0..n {
            for j in 0..n {
                let sq = (gram[[i, i]] + gram[[j, j]] - 2.0 * gram[[i, j]]).max(0.0);
                distances[[i, j]] = sq.sqrt();
            }
        }
        Ok(distances)
    }

    #[cfg(not(feature = "gpu"))]
    fn gemm_pairwise_euclidean(&self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        self.cpu_pairwise_distances(data, "euclidean")
    }

    pub(crate) fn cpu_pairwise_distances(
        &self,
        data: &ArrayView2<Float>,
        metric: &str,
    ) -> SklResult<Array2<Float>> {
        let n_samples = data.nrows();
        let mut distances = Array2::zeros((n_samples, n_samples));

        for i in 0..n_samples {
            for j in i..n_samples {
                let dist = match metric {
                    "euclidean" => self.euclidean_distance(&data.row(i), &data.row(j)),
                    "manhattan" => self.manhattan_distance(&data.row(i), &data.row(j)),
                    "cosine" => self.cosine_distance(&data.row(i), &data.row(j)),
                    _ => {
                        return Err(SklearsError::InvalidParameter {
                            name: "metric".to_string(),
                            reason: format!("Unknown metric: {}", metric),
                        })
                    }
                };
                distances[[i, j]] = dist;
                distances[[j, i]] = dist;
            }
        }

        Ok(distances)
    }

    /// Compute k-nearest neighbors.
    pub fn knn_search(
        &mut self,
        data: &ArrayView2<Float>,
        k: usize,
        metric: &str,
    ) -> SklResult<(Array2<Float>, Array2<usize>)> {
        let distances = self.pairwise_distances(data, metric)?;
        let n_samples = data.nrows();
        let k_actual = k.min(n_samples.saturating_sub(1));
        let mut knn_distances = Array2::zeros((n_samples, k_actual));
        let mut knn_indices = Array2::zeros((n_samples, k_actual));

        for i in 0..n_samples {
            let mut row: Vec<(Float, usize)> = (0..n_samples)
                .filter(|&j| j != i)
                .map(|j| (distances[[i, j]], j))
                .collect();
            row.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            for (rank, (d, idx)) in row.into_iter().take(k_actual).enumerate() {
                knn_distances[[i, rank]] = d;
                knn_indices[[i, rank]] = idx;
            }
        }

        Ok((knn_distances, knn_indices))
    }

    pub(crate) fn cpu_knn_search(
        &self,
        data: &ArrayView2<Float>,
        k: usize,
        metric: &str,
    ) -> SklResult<(Array2<Float>, Array2<usize>)> {
        let n_samples = data.nrows();
        let k_actual = k.min(n_samples.saturating_sub(1));
        let mut knn_distances = Array2::zeros((n_samples, k_actual));
        let mut knn_indices = Array2::zeros((n_samples, k_actual));

        for i in 0..n_samples {
            let mut distances_with_indices: Vec<(Float, usize)> = Vec::new();
            for j in 0..n_samples {
                if i != j {
                    let dist = match metric {
                        "euclidean" => self.euclidean_distance(&data.row(i), &data.row(j)),
                        "manhattan" => self.manhattan_distance(&data.row(i), &data.row(j)),
                        "cosine" => self.cosine_distance(&data.row(i), &data.row(j)),
                        _ => {
                            return Err(SklearsError::InvalidParameter {
                                name: "metric".to_string(),
                                reason: format!("Unknown metric: {}", metric),
                            })
                        }
                    };
                    distances_with_indices.push((dist, j));
                }
            }
            distances_with_indices
                .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
            for (idx, &(dist, neighbor_idx)) in
                distances_with_indices.iter().take(k_actual).enumerate()
            {
                knn_distances[[i, idx]] = dist;
                knn_indices[[i, idx]] = neighbor_idx;
            }
        }

        Ok((knn_distances, knn_indices))
    }

    /// Accelerated matrix operations.
    pub fn matrix_operations(
        &mut self,
        operation: &str,
        data: &ArrayView2<Float>,
    ) -> SklResult<Array2<Float>> {
        match operation {
            "gram_matrix" => self.compute_gram_matrix(data),
            "laplacian" => self.compute_laplacian(data),
            "normalize" => self.normalize_matrix(data),
            _ => Err(SklearsError::InvalidParameter {
                name: "operation".to_string(),
                reason: format!("Unknown operation: {}", operation),
            }),
        }
    }

    fn compute_gram_matrix(&mut self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        #[cfg(feature = "gpu")]
        if let Some(ctx) = &self.context {
            let owned = data.to_owned();
            let x_gpu = GpuArray::<Float>::from_array2(ctx, &owned)?;
            let (n, d) = data.dim();
            let mut xt_flat = vec![0.0 as Float; n * d];
            for r in 0..n {
                for c in 0..d {
                    xt_flat[c * n + r] = data[[r, c]];
                }
            }
            let xt_array = Array2::from_shape_vec((d, n), xt_flat)
                .map_err(|e| SklearsError::InvalidInput(format!("Transpose shape error: {}", e)))?;
            let xt_gpu = GpuArray::<Float>::from_array2(ctx, &xt_array)?;
            let gram_gpu = x_gpu.matmul(&xt_gpu)?;
            return gram_gpu.to_array2();
        }
        self.cpu_gram_matrix(data)
    }

    fn cpu_gram_matrix(&self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        Ok(data.dot(&data.t()))
    }

    fn compute_laplacian(&self, adjacency: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        let n = adjacency.nrows();
        let mut laplacian = Array2::zeros((n, n));
        let mut degrees = Array1::zeros(n);
        for i in 0..n {
            degrees[i] = adjacency.row(i).sum();
        }
        for i in 0..n {
            laplacian[[i, i]] = degrees[i];
            for j in 0..n {
                if i != j {
                    laplacian[[i, j]] = -adjacency[[i, j]];
                }
            }
        }
        Ok(laplacian)
    }

    fn normalize_matrix(&self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        let mut normalized = data.to_owned();
        for mut row in normalized.rows_mut() {
            let norm = row.mapv(|x| x * x).sum().sqrt();
            if norm > 0.0 {
                row /= norm;
            }
        }
        Ok(normalized)
    }

    fn euclidean_distance(
        &self,
        a: &scirs2_core::ndarray::ArrayView1<Float>,
        b: &scirs2_core::ndarray::ArrayView1<Float>,
    ) -> Float {
        a.iter()
            .zip(b.iter())
            .map(|(x, y)| (x - y).powi(2))
            .sum::<Float>()
            .sqrt()
    }

    fn manhattan_distance(
        &self,
        a: &scirs2_core::ndarray::ArrayView1<Float>,
        b: &scirs2_core::ndarray::ArrayView1<Float>,
    ) -> Float {
        a.iter().zip(b.iter()).map(|(x, y)| (x - y).abs()).sum()
    }

    fn cosine_distance(
        &self,
        a: &scirs2_core::ndarray::ArrayView1<Float>,
        b: &scirs2_core::ndarray::ArrayView1<Float>,
    ) -> Float {
        let dot = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum::<Float>();
        let na = a.iter().map(|x| x * x).sum::<Float>().sqrt();
        let nb = b.iter().map(|x| x * x).sum::<Float>().sqrt();
        if na > 0.0 && nb > 0.0 {
            1.0 - dot / (na * nb)
        } else {
            0.0
        }
    }
}

/// GPU-accelerated t-SNE implementation.
pub struct GpuTSNE {
    accelerator: GpuAccelerator,
    n_components: usize,
    perplexity: f64,
    learning_rate: f64,
    n_iter: usize,
    random_state: Option<u64>,
}

impl GpuTSNE {
    pub fn new() -> SklResult<Self> {
        Ok(Self {
            accelerator: GpuAccelerator::new()?,
            n_components: 2,
            perplexity: 30.0,
            learning_rate: 200.0,
            n_iter: 1000,
            random_state: None,
        })
    }

    pub fn n_components(mut self, n_components: usize) -> Self {
        self.n_components = n_components;
        self
    }

    pub fn perplexity(mut self, perplexity: f64) -> Self {
        self.perplexity = perplexity;
        self
    }

    pub fn learning_rate(mut self, learning_rate: f64) -> Self {
        self.learning_rate = learning_rate;
        self
    }

    pub fn n_iter(mut self, n_iter: usize) -> Self {
        self.n_iter = n_iter;
        self
    }

    pub fn random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        self
    }

    /// Fit t-SNE and return the low-dimensional embedding.
    pub fn fit_transform(&mut self, data: &ArrayView2<Float>) -> SklResult<Array2<Float>> {
        let distances = self.accelerator.pairwise_distances(data, "euclidean")?;
        self.cpu_tsne_fallback(data, &distances)
    }

    fn cpu_tsne_fallback(
        &self,
        data: &ArrayView2<Float>,
        _distances: &Array2<Float>,
    ) -> SklResult<Array2<Float>> {
        let n_samples = data.nrows();
        let mut embedding = Array2::zeros((n_samples, self.n_components));

        let mut rng = if let Some(seed) = self.random_state {
            StdRng::seed_from_u64(seed)
        } else {
            StdRng::seed_from_u64(thread_rng().random::<u64>())
        };

        for i in 0..n_samples {
            for j in 0..self.n_components {
                embedding[[i, j]] = rng.sample(scirs2_core::StandardNormal);
            }
        }

        Ok(embedding)
    }

    pub fn is_gpu_available(&self) -> bool {
        self.accelerator.is_gpu_available()
    }
}

/// Performance benchmarking utilities for GPU acceleration.
pub struct GpuBenchmark {
    accelerator: GpuAccelerator,
}

impl GpuBenchmark {
    pub fn new() -> SklResult<Self> {
        Ok(Self {
            accelerator: GpuAccelerator::new()?,
        })
    }

    pub fn benchmark_distance_computation(
        &mut self,
        data: &ArrayView2<Float>,
        metric: &str,
    ) -> SklResult<(f64, f64)> {
        use std::time::Instant;

        let start = Instant::now();
        let _cpu_result = self.accelerator.cpu_pairwise_distances(data, metric)?;
        let cpu_time = start.elapsed().as_secs_f64();

        let start = Instant::now();
        let _gpu_result = self.accelerator.pairwise_distances(data, metric)?;
        let gpu_time = start.elapsed().as_secs_f64();

        Ok((cpu_time, gpu_time))
    }

    pub fn benchmark_knn_search(
        &mut self,
        data: &ArrayView2<Float>,
        k: usize,
        metric: &str,
    ) -> SklResult<(f64, f64)> {
        use std::time::Instant;

        let start = Instant::now();
        let _cpu_result = self.accelerator.cpu_knn_search(data, k, metric)?;
        let cpu_time = start.elapsed().as_secs_f64();

        let start = Instant::now();
        let _gpu_result = self.accelerator.knn_search(data, k, metric)?;
        let gpu_time = start.elapsed().as_secs_f64();

        Ok((cpu_time, gpu_time))
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_gpu_accelerator_creation() {
        let accelerator = GpuAccelerator::new();
        assert!(accelerator.is_ok());
    }

    #[test]
    fn test_pairwise_distances() {
        let mut accelerator = GpuAccelerator::new().expect("operation should succeed");
        let data = array![[1.0_f64, 2.0], [3.0, 4.0], [5.0, 6.0]];

        let distances = accelerator
            .pairwise_distances(&data.view(), "euclidean")
            .expect("operation should succeed");

        assert_eq!(distances.shape(), &[3, 3]);
        assert_abs_diff_eq!(distances[[0, 0]], 0.0, epsilon = 1e-10);
        assert_abs_diff_eq!(distances[[0, 1]], distances[[1, 0]], epsilon = 1e-10);
    }

    #[test]
    fn test_knn_search() {
        let mut accelerator = GpuAccelerator::new().expect("operation should succeed");
        let data = array![[1.0_f64, 2.0], [3.0, 4.0], [5.0, 6.0], [7.0, 8.0]];

        let (distances, indices) = accelerator
            .knn_search(&data.view(), 2, "euclidean")
            .expect("operation should succeed");

        assert_eq!(distances.shape(), &[4, 2]);
        assert_eq!(indices.shape(), &[4, 2]);
    }

    #[test]
    fn test_matrix_operations() {
        let mut accelerator = GpuAccelerator::new().expect("operation should succeed");
        let data = array![[1.0_f64, 2.0], [3.0, 4.0]];

        let gram = accelerator
            .matrix_operations("gram_matrix", &data.view())
            .expect("operation should succeed");
        assert_eq!(gram.shape(), &[2, 2]);

        let normalized = accelerator
            .matrix_operations("normalize", &data.view())
            .expect("operation should succeed");
        assert_eq!(normalized.shape(), &[2, 2]);
    }

    #[test]
    fn test_gpu_tsne() {
        let mut tsne = GpuTSNE::new().expect("operation should succeed");
        let data = array![[1.0_f64, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        let embedding = tsne
            .fit_transform(&data.view())
            .expect("operation should succeed");
        assert_eq!(embedding.shape(), &[3, 2]);
    }

    #[test]
    fn test_benchmarking() {
        let mut benchmark = GpuBenchmark::new().expect("operation should succeed");
        let data = array![[1.0_f64, 2.0], [3.0, 4.0], [5.0, 6.0]];

        let (cpu_time, gpu_time) = benchmark
            .benchmark_distance_computation(&data.view(), "euclidean")
            .expect("operation should succeed");
        assert!(cpu_time >= 0.0);
        assert!(gpu_time >= 0.0);
    }
}