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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
//! GPU-accelerated transformations
//!
//! This module provides GPU-accelerated implementations of dimensionality reduction
//! and matrix operations. Currently provides basic stubs with CPU fallback.
use crate::error::{Result, TransformError};
use scirs2_core::gpu::{GpuBackend, GpuContext};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2};
use scirs2_core::validation::{check_not_empty, check_positive, checkarray_finite};
/// GPU-accelerated Principal Component Analysis
#[cfg(feature = "gpu")]
pub struct GpuPCA {
/// Number of components to compute
pub n_components: usize,
/// Whether to center the data
pub center: bool,
/// Principal components (loading vectors)
pub components: Option<Array2<f64>>,
/// Explained variance for each component
pub explained_variance: Option<Array1<f64>>,
/// Mean values for centering
pub mean: Option<Array1<f64>>,
/// GPU context for GPU operations
gpu_context: Option<GpuContext>,
}
#[cfg(feature = "gpu")]
impl GpuPCA {
/// Create a new GPU PCA instance
///
/// # Arguments
///
/// * `n_components` - Number of principal components to compute
///
/// # Returns
///
/// Returns a new GpuPCA instance with GPU context initialized
///
/// # Errors
///
/// Returns an error if GPU initialization fails or if n_components is 0
///
/// # Examples
///
/// ```
/// # use scirs2_transform::gpu::GpuPCA;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let pca = GpuPCA::new(5)?;
/// # Ok(())
/// # }
/// ```
pub fn new(n_components: usize) -> Result<Self> {
check_positive(n_components, "n_components")?;
let gpu_context = GpuContext::new(GpuBackend::preferred()).map_err(|e| {
TransformError::ComputationError(format!("Failed to initialize GPU: {}", e))
})?;
Ok(GpuPCA {
n_components,
center: true,
components: None,
explained_variance: None,
mean: None,
gpu_context: Some(gpu_context),
})
}
/// Fit the PCA model using a CPU SVD-based backend.
///
/// Currently delegates to the CPU SVD-based PCA implementation.
/// A wgpu-backed Jacobi SVD path is planned for v0.5.
///
/// # Arguments
///
/// * `x` - Input data matrix with shape (n_samples, n_features)
///
/// # Errors
///
/// Returns an error if the input is invalid or if `n_components` exceeds
/// `min(n_samples, n_features)`.
///
/// # Examples
///
/// ```
/// # use scirs2_transform::gpu::GpuPCA;
/// # use scirs2_core::ndarray::Array2;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut pca = GpuPCA::new(2)?;
/// let data = Array2::from_shape_vec((5, 4), vec![
/// 1.0, 2.0, 3.0, 4.0,
/// 2.0, 3.0, 4.0, 5.0,
/// 3.0, 4.0, 5.0, 6.0,
/// 4.0, 5.0, 6.0, 7.0,
/// 5.0, 6.0, 7.0, 8.0,
/// ])?;
/// pca.fit(&data.view())?;
/// assert!(pca.components.is_some());
/// # Ok(())
/// # }
/// ```
pub fn fit(&mut self, x: &ArrayView2<f64>) -> Result<()> {
check_not_empty(x, "x")?;
checkarray_finite(x, "x")?;
let (n_samples, n_features) = x.dim();
if self.n_components > n_features.min(n_samples) {
return Err(TransformError::InvalidInput(
"n_components cannot be larger than min(n_samples, n_features)".to_string(),
));
}
// Delegate to CPU SVD-based PCA; GPU Jacobi SVD path planned for v0.5.
let mut cpu_pca = crate::reduction::PCA::new(self.n_components, self.center, false);
cpu_pca.fit(x)?;
self.components = cpu_pca.components().cloned();
self.mean = cpu_pca.mean().cloned();
// Store the explained variance ratio directly; GpuPCA::explained_variance_ratio()
// returns this field as-is (it is already normalised to sum ≈ 1).
self.explained_variance = cpu_pca.explained_variance_ratio().cloned();
Ok(())
}
/// Transform data using the fitted PCA model.
///
/// Projects the input data onto the principal components computed during
/// [`GpuPCA::fit`]. Currently runs on the CPU; a wgpu-backed path is
/// planned for v0.5.
///
/// # Arguments
///
/// * `x` - Input data matrix with shape (n_samples, n_features)
///
/// # Returns
///
/// Transformed data matrix with shape (n_samples, n_components)
///
/// # Errors
///
/// Returns `NotFitted` if [`GpuPCA::fit`] has not been called yet, or
/// `InvalidInput` if the number of features does not match.
pub fn transform(&self, x: &ArrayView2<f64>) -> Result<Array2<f64>> {
check_not_empty(x, "x")?;
checkarray_finite(x, "x")?;
let components = self.components.as_ref().ok_or_else(|| {
TransformError::NotFitted(
"GpuPCA model has not been fitted; call fit() first".to_string(),
)
})?;
let (n_samples, n_features) = x.dim();
let n_comp_features = components.shape()[1];
if n_features != n_comp_features {
return Err(TransformError::InvalidInput(format!(
"x has {} features, but GpuPCA was fitted with {} features",
n_features, n_comp_features,
)));
}
// Center the data if the model was trained with centering.
let x_centered: Array2<f64> = if self.center {
if let Some(mean) = &self.mean {
let mut centered = Array2::zeros((n_samples, n_features));
for i in 0..n_samples {
for j in 0..n_features {
centered[[i, j]] = x[[i, j]] - mean[j];
}
}
centered
} else {
// mean not available — use raw data
x.to_owned()
}
} else {
x.to_owned()
};
// Project onto principal components: result shape (n_samples, n_components).
let transformed = x_centered.dot(&components.t());
Ok(transformed)
}
/// Fit the PCA model and project data in one step.
///
/// Equivalent to calling [`GpuPCA::fit`] followed by [`GpuPCA::transform`]
/// on the same data.
///
/// # Arguments
///
/// * `x` - Input data matrix with shape (n_samples, n_features)
///
/// # Returns
///
/// Transformed data matrix with shape (n_samples, n_components)
///
/// # Errors
///
/// Returns any error produced by [`GpuPCA::fit`] or [`GpuPCA::transform`].
pub fn fit_transform(&mut self, x: &ArrayView2<f64>) -> Result<Array2<f64>> {
self.fit(x)?;
self.transform(x)
}
/// Get the explained variance ratio for each principal component.
///
/// The returned array sums to approximately 1.0 after fitting.
///
/// # Returns
///
/// Array of explained variance ratios with length `n_components`
///
/// # Errors
///
/// Returns `NotFitted` if [`GpuPCA::fit`] has not been called yet.
pub fn explained_variance_ratio(&self) -> Result<Array1<f64>> {
self.explained_variance
.as_ref()
.cloned()
.ok_or_else(|| TransformError::NotFitted("GpuPCA model not fitted".to_string()))
}
}
/// GPU-accelerated matrix operations for transformations
#[cfg(feature = "gpu")]
pub struct GpuMatrixOps {
#[allow(dead_code)]
gpu_context: GpuContext,
}
#[cfg(feature = "gpu")]
impl GpuMatrixOps {
/// Create new GPU matrix operations instance
pub fn new() -> Result<Self> {
let gpu_context = GpuContext::new(GpuBackend::preferred()).map_err(|e| {
TransformError::ComputationError(format!("Failed to initialize GPU: {}", e))
})?;
Ok(GpuMatrixOps { gpu_context })
}
/// GPU-accelerated matrix multiplication (placeholder)
pub fn matmul(self_a: &ArrayView2<f64>, b: &ArrayView2<f64>) -> Result<Array2<f64>> {
Err(TransformError::NotImplemented(
"GPU matrix multiplication is not yet implemented. Use CPU operations instead."
.to_string(),
))
}
/// GPU-accelerated SVD decomposition (placeholder)
pub fn svd(selfa: &ArrayView2<f64>) -> Result<(Array2<f64>, Array1<f64>, Array2<f64>)> {
Err(TransformError::NotImplemented(
"GPU SVD is not yet implemented. Use CPU operations instead.".to_string(),
))
}
/// GPU-accelerated eigendecomposition (placeholder)
pub fn eigh(selfa: &ArrayView2<f64>) -> Result<(Array1<f64>, Array2<f64>)> {
Err(TransformError::NotImplemented(
"GPU eigendecomposition is not yet implemented. Use CPU operations instead."
.to_string(),
))
}
}
/// GPU-accelerated t-SNE implementation
#[cfg(feature = "gpu")]
pub struct GpuTSNE {
/// Number of dimensions for the embedding
pub n_components: usize,
/// Perplexity parameter
pub perplexity: f64,
/// Learning rate
pub learning_rate: f64,
/// Maximum number of iterations
pub max_iter: usize,
/// GPU context
#[allow(dead_code)]
gpu_context: GpuContext,
}
#[cfg(feature = "gpu")]
impl GpuTSNE {
/// Create new GPU t-SNE instance
pub fn new(n_components: usize) -> Result<Self> {
check_positive(n_components, "n_components")?;
let gpu_context = GpuContext::new(GpuBackend::preferred()).map_err(|e| {
TransformError::ComputationError(format!("Failed to initialize GPU: {}", e))
})?;
Ok(GpuTSNE {
n_components,
perplexity: 30.0,
learning_rate: 200.0,
max_iter: 1000,
gpu_context,
})
}
/// Set perplexity parameter
pub fn with_perplexity(mut self, perplexity: f64) -> Self {
self.perplexity = perplexity;
self
}
/// Set learning rate
pub fn with_learning_rate(mut self, learning_rate: f64) -> Self {
self.learning_rate = learning_rate;
self
}
/// Set maximum iterations
pub fn with_max_iter(mut self, max_iter: usize) -> Self {
self.max_iter = max_iter;
self
}
/// Fit and transform data using GPU-accelerated t-SNE (placeholder)
pub fn fit_transform(selfx: &ArrayView2<f64>) -> Result<Array2<f64>> {
Err(TransformError::NotImplemented(
"GPU t-SNE is not yet implemented. Use CPU t-SNE instead.".to_string(),
))
}
}
// Stub implementations when GPU feature is not enabled
#[cfg(not(feature = "gpu"))]
pub struct GpuPCA;
#[cfg(not(feature = "gpu"))]
pub struct GpuMatrixOps;
#[cfg(not(feature = "gpu"))]
pub struct GpuTSNE;
#[cfg(not(feature = "gpu"))]
impl GpuPCA {
pub fn new(_ncomponents: usize) -> Result<Self> {
Err(TransformError::FeatureNotEnabled(
"GPU acceleration requires the 'gpu' feature to be enabled".to_string(),
))
}
}
#[cfg(not(feature = "gpu"))]
impl GpuMatrixOps {
pub fn new() -> Result<Self> {
Err(TransformError::FeatureNotEnabled(
"GPU acceleration requires the 'gpu' feature to be enabled".to_string(),
))
}
}
#[cfg(not(feature = "gpu"))]
impl GpuTSNE {
pub fn new(_ncomponents: usize) -> Result<Self> {
Err(TransformError::FeatureNotEnabled(
"GPU acceleration requires the 'gpu' feature to be enabled".to_string(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::Array2;
// ------------------------------------------------------------------
// Helper: construct a simple (5 × 4) test matrix with known structure.
// Each row is an arithmetic progression, so the data lies close to a
// 1-dimensional subspace; PCA with 2 components should capture ≥ 99 %
// of the variance.
// ------------------------------------------------------------------
#[cfg(feature = "gpu")]
fn sample_data_5x4() -> Array2<f64> {
Array2::from_shape_vec(
(5, 4),
vec![
1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0, 4.0, 5.0, 6.0, 7.0,
5.0, 6.0, 7.0, 8.0,
],
)
.expect("shape vec must be consistent")
}
// ------------------------------------------------------------------
// Construction tests
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_creation() {
let pca = GpuPCA::new(3);
assert!(pca.is_ok());
let pca = pca.expect("GpuPCA::new should succeed");
assert_eq!(pca.n_components, 3);
assert!(pca.center);
assert!(pca.components.is_none());
assert!(pca.explained_variance.is_none());
assert!(pca.mean.is_none());
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_invalid_components() {
let result = GpuPCA::new(0);
assert!(result.is_err());
}
// ------------------------------------------------------------------
// fit() tests — verify that fit populates all model fields correctly.
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_populates_fields() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("GpuPCA::fit should succeed");
// All model fields should now be populated.
assert!(pca.components.is_some(), "components must be set after fit");
assert!(
pca.explained_variance.is_some(),
"explained_variance must be set after fit"
);
assert!(pca.mean.is_some(), "mean must be set after fit");
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_components_shape() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
// components: (n_components, n_features) = (2, 4)
let comp = pca.components.as_ref().expect("components present");
assert_eq!(comp.shape(), &[2, 4]);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_mean_shape() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let mean = pca.mean.as_ref().expect("mean present");
assert_eq!(mean.len(), 4, "mean must have one entry per feature");
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_explained_variance_length() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let ev = pca
.explained_variance
.as_ref()
.expect("explained_variance present");
assert_eq!(
ev.len(),
2,
"explained_variance must have n_components entries"
);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_explained_variance_non_increasing() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let ev = pca
.explained_variance
.as_ref()
.expect("explained_variance present");
for i in 0..ev.len() - 1 {
assert!(
ev[i] >= ev[i + 1] - 1e-10,
"explained variance must be non-increasing: ev[{}]={} < ev[{}]={}",
i,
ev[i],
i + 1,
ev[i + 1]
);
}
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_n_components_too_large() {
// n_components > min(n_samples, n_features) must fail
let mut pca = GpuPCA::new(10).expect("GpuPCA::new(10) should succeed");
let data = sample_data_5x4(); // 5 × 4 → max 4 components
let result = pca.fit(&data.view());
assert!(
result.is_err(),
"should fail when n_components > min(n_samples, n_features)"
);
}
// ------------------------------------------------------------------
// transform() tests
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_transform_shape() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let transformed = pca
.transform(&data.view())
.expect("transform must succeed after fit");
// Output shape: (n_samples, n_components) = (5, 2)
assert_eq!(transformed.shape(), &[5, 2]);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_transform_all_finite() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let transformed = pca.transform(&data.view()).expect("transform must succeed");
assert!(
transformed.iter().all(|v| v.is_finite()),
"all transformed values must be finite"
);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_transform_before_fit_returns_error() {
let pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
let result = pca.transform(&data.view());
assert!(result.is_err(), "transform before fit must return an error");
}
// ------------------------------------------------------------------
// fit_transform() tests
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_transform_shape() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
let ft = pca
.fit_transform(&data.view())
.expect("fit_transform must succeed");
assert_eq!(ft.shape(), &[5, 2]);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_fit_transform_matches_fit_then_transform() {
let data = sample_data_5x4();
let mut pca1 = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let ft = pca1
.fit_transform(&data.view())
.expect("fit_transform must succeed");
let mut pca2 = GpuPCA::new(2).expect("GpuPCA::new should succeed");
pca2.fit(&data.view()).expect("fit must succeed");
let t = pca2
.transform(&data.view())
.expect("transform must succeed");
assert_eq!(ft.shape(), t.shape());
for (a, b) in ft.iter().zip(t.iter()) {
assert!(
(a - b).abs() < 1e-10,
"fit_transform and fit+transform must agree: {} vs {}",
a,
b
);
}
}
// ------------------------------------------------------------------
// explained_variance_ratio() method
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_explained_variance_ratio_before_fit_returns_error() {
let pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
assert!(pca.explained_variance_ratio().is_err());
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_pca_explained_variance_ratio_after_fit() {
let mut pca = GpuPCA::new(2).expect("GpuPCA::new should succeed");
let data = sample_data_5x4();
pca.fit(&data.view()).expect("fit must succeed");
let ratio = pca
.explained_variance_ratio()
.expect("explained_variance_ratio must succeed after fit");
assert_eq!(ratio.len(), 2);
// All ratios must be non-negative
assert!(ratio.iter().all(|&v| v >= 0.0));
}
// ------------------------------------------------------------------
// GpuMatrixOps and GpuTSNE smoke tests (unchanged behaviour)
// ------------------------------------------------------------------
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_matrix_ops_creation() {
let ops = GpuMatrixOps::new();
assert!(ops.is_ok());
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_tsne_creation() {
let tsne = GpuTSNE::new(2);
assert!(tsne.is_ok());
let tsne = tsne.expect("GpuTSNE::new should succeed");
assert_eq!(tsne.n_components, 2);
assert_eq!(tsne.perplexity, 30.0);
assert_eq!(tsne.learning_rate, 200.0);
assert_eq!(tsne.max_iter, 1000);
}
#[test]
#[cfg(feature = "gpu")]
fn test_gpu_tsne_with_params() {
let tsne = GpuTSNE::new(3)
.expect("GpuTSNE::new should succeed")
.with_perplexity(50.0)
.with_learning_rate(100.0)
.with_max_iter(500);
assert_eq!(tsne.n_components, 3);
assert_eq!(tsne.perplexity, 50.0);
assert_eq!(tsne.learning_rate, 100.0);
assert_eq!(tsne.max_iter, 500);
}
#[test]
#[cfg(not(feature = "gpu"))]
fn test_gpu_features_disabled() {
assert!(GpuPCA::new(2).is_err());
assert!(GpuMatrixOps::new().is_err());
assert!(GpuTSNE::new(2).is_err());
}
}