primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Adaptive dispatch for RBF surrogate training
//!
//! Implements dual-precision training strategy:
//! - **Small N (<threshold)**: Full f64 CPU path (current default)
//! - **Large N (≥threshold)**: f32 distance computation → promote to f64 → f64 solve
//! - **GPU path**: `cdist.wgsl` shader for 10-14× speedup on large datasets
//!
//! The f32 distance path is 2-4× faster than f64 for the O(n²·d) cdist operation
//! due to SIMD vectorization (4 f32 vs 2 f64 per SSE/NEON lane). When a GPU is
//! available, the f32 path is replaced with `cdist.wgsl` for 10-14× speedup.
//!
//! # Architecture
//!
//! ```text
//! Training Data: Vec<Vec<f64>>
//!         |
//!    GPU available?
//!    ├── YES: GPU f32 cdist.wgsl → promote to f64 → f64 kernel → f64 solve
//!    └── NO:  N < threshold?
//!             ├── YES: CPU f64 cdist → f64 kernel → f64 solve (exact)
//!             └── NO:  CPU f32 cdist → promote → f64 kernel → f64 solve
//! ```
//!
//! # Cross-Domain Applications
//!
//! - **Nuclear physics**: 10D Skyrme fits with N=1000+ training points
//! - **Materials science**: DFT surrogate models with large datasets
//! - **ML**: Gaussian process regression with many observations
//!
//! # References
//!
//! - Diaw et al. (2024): Dual-precision surrogate training architecture

use super::kernels::RBFKernel;
use super::rbf::{RBFSurrogate, RbfTrainedModel, RbfTrainingData};
use crate::device::WgpuDevice;
use crate::error::{BarracudaError, Result};
use crate::linalg::solve_f64;
use crate::ops::cdist_wgsl::DistanceMetric;
use crate::tensor::Tensor;
use std::sync::Arc;

/// Configuration for adaptive dispatch during RBF training.
#[derive(Debug, Clone)]
pub struct AdaptiveConfig {
    /// Minimum N for switching to f32 distance computation.
    /// Below this threshold, full f64 is used.
    /// Default: 200
    pub f32_threshold: usize,

    /// Whether to force f64 path regardless of N.
    /// Useful for validation/comparison.
    /// Default: false
    pub force_f64: bool,

    /// Whether to enable parallelism for distance computation.
    /// Default: true
    pub parallel: bool,

    /// Whether to prefer GPU for distance computation when available.
    /// Default: true
    pub prefer_gpu: bool,
}

impl Default for AdaptiveConfig {
    fn default() -> Self {
        Self {
            f32_threshold: 200,
            force_f64: false,
            parallel: true,
            prefer_gpu: true,
        }
    }
}

impl AdaptiveConfig {
    /// Create config that always uses f64 (for validation).
    #[must_use]
    pub fn exact() -> Self {
        Self {
            force_f64: true,
            prefer_gpu: false,
            ..Default::default()
        }
    }

    /// Create config with a custom threshold.
    #[must_use]
    pub fn with_threshold(threshold: usize) -> Self {
        Self {
            f32_threshold: threshold,
            ..Default::default()
        }
    }

    /// Create config that forces CPU path (no GPU).
    #[must_use]
    pub fn cpu_only() -> Self {
        Self {
            prefer_gpu: false,
            ..Default::default()
        }
    }
}

/// Training diagnostics from adaptive dispatch.
#[derive(Debug, Clone)]
pub struct TrainingDiagnostics {
    /// Whether f32 path was used for distances
    pub used_f32_distances: bool,
    /// Whether GPU was used for distance computation
    pub used_gpu: bool,
    /// Number of training points
    pub n_train: usize,
    /// Number of dimensions
    pub n_dim: usize,
    /// Size of the linear system solved
    pub system_size: usize,
    /// Maximum absolute difference between f32 and f64 distances
    /// (only populated if both were computed for validation)
    pub max_distance_error: Option<f64>,
}

/// Train an RBF surrogate with adaptive dispatch.
///
/// Uses f32 distance computation for large datasets and f64 for small ones.
/// The kernel evaluation and linear solve always use f64 for numerical stability.
///
/// # Arguments
///
/// * `x_data` - Training points `[[x₁₁, x₁₂, ...], ...]`
/// * `y_data` - Training values `[y₁, y₂, ...]`
/// * `kernel` - RBF kernel type
/// * `smoothing` - Regularization parameter
/// * `config` - Adaptive dispatch configuration
///
/// # Returns
///
/// Tuple of `(RBFSurrogate, TrainingDiagnostics)`
///
/// # Errors
///
/// Returns [`Err`] if training data is empty, `x_data` and `y_data` lengths
/// differ, or the linear solve fails (e.g., singular matrix, GPU buffer error).
///
/// # Examples
///
/// ```no_run
/// use barracuda::surrogate::adaptive::{train_adaptive, AdaptiveConfig};
/// use barracuda::surrogate::RBFKernel;
/// use barracuda::prelude::WgpuDevice;
/// use std::sync::Arc;
///
/// # async fn example() -> barracuda::error::Result<()> {
/// let device = Arc::new(WgpuDevice::new().await?);
/// let x_train: Vec<Vec<f64>> = (0..10).map(|i| vec![i as f64]).collect();
/// let y_train: Vec<f64> = x_train.iter().map(|x| x[0] * x[0]).collect();
///
/// let config = AdaptiveConfig::default();
/// let (surrogate, diag) = train_adaptive(
///     device, &x_train, &y_train, RBFKernel::ThinPlateSpline, 1e-12, &config
/// )?;
///
/// tracing::info!("Used f32: {}, system size: {}", diag.used_f32_distances, diag.system_size);
/// # Ok(())
/// # }
/// ```
pub fn train_adaptive(
    device: Arc<WgpuDevice>,
    x_data: &[Vec<f64>],
    y_data: &[f64],
    kernel: RBFKernel,
    smoothing: f64,
    config: &AdaptiveConfig,
) -> Result<(RBFSurrogate, TrainingDiagnostics)> {
    let n_train = x_data.len();

    if n_train == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "Training data cannot be empty".to_string(),
        });
    }

    if y_data.len() != n_train {
        return Err(BarracudaError::InvalidInput {
            message: format!(
                "x_data and y_data length mismatch: {} vs {}",
                n_train,
                y_data.len()
            ),
        });
    }

    let n_dim = x_data[0].len();

    // Flatten training data
    let train_x: Vec<f64> = x_data.iter().flat_map(|row| row.iter().copied()).collect();

    // Decide dispatch path
    let use_f32 = !config.force_f64 && n_train >= config.f32_threshold;

    // Compute pairwise distances
    let distances = if use_f32 {
        compute_distances_f32_promoted(&train_x, &train_x, n_train, n_train, n_dim)
    } else {
        compute_distances_f64(&train_x, &train_x, n_train, n_train, n_dim)
    };

    // Assemble and solve (always f64, GPU)
    let surrogate = assemble_and_solve(
        device, &train_x, &distances, y_data, kernel, smoothing, n_train, n_dim,
    )?;

    let diagnostics = TrainingDiagnostics {
        used_f32_distances: use_f32,
        used_gpu: false,
        n_train,
        n_dim,
        system_size: n_train + n_dim + 1,
        max_distance_error: None,
    };

    Ok((surrogate, diagnostics))
}

/// Train an RBF surrogate with GPU-accelerated distance computation.
///
/// Uses the GPU `cdist.wgsl` shader for the O(n²·d) pairwise distance
/// computation, providing 10-14× speedup over CPU for large datasets.
/// The kernel evaluation and linear solve use f64 on CPU for numerical stability.
///
/// # Arguments
///
/// * `x_data` - Training points `[[x₁₁, x₁₂, ...], ...]`
/// * `y_data` - Training values `[y₁, y₂, ...]`
/// * `kernel` - RBF kernel type
/// * `smoothing` - Regularization parameter
/// * `device` - GPU device for distance computation
///
/// # Returns
///
/// Tuple of `(RBFSurrogate, TrainingDiagnostics)`
///
/// # Examples
///
/// ```rust,ignore
/// use barracuda::surrogate::adaptive::train_adaptive_gpu;
/// use barracuda::surrogate::RBFKernel;
/// use barracuda::prelude::WgpuDevice;
/// use std::sync::Arc;
///
/// # async fn example() -> barracuda::error::Result<()> {
/// let device = Arc::new(WgpuDevice::new().await?);
///
/// let x_train: Vec<Vec<f64>> = (0..500).map(|i| vec![i as f64 / 500.0]).collect();
/// let y_train: Vec<f64> = x_train.iter().map(|x| x[0] * x[0]).collect();
///
/// let (surrogate, diag) = train_adaptive_gpu(
///     &x_train, &y_train, RBFKernel::ThinPlateSpline, 1e-12, device
/// ).await?;
///
/// assert!(diag.used_gpu);
/// tracing::info!("Trained with {} points on GPU", diag.n_train);
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// Returns [`Err`] if training data is empty, `x_data` and `y_data` lengths
/// differ, GPU distance computation fails, or the linear solve fails.
pub async fn train_adaptive_gpu(
    x_data: &[Vec<f64>],
    y_data: &[f64],
    kernel: RBFKernel,
    smoothing: f64,
    device: Arc<WgpuDevice>,
) -> Result<(RBFSurrogate, TrainingDiagnostics)> {
    let n_train = x_data.len();

    if n_train == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "Training data cannot be empty".to_string(),
        });
    }

    if y_data.len() != n_train {
        return Err(BarracudaError::InvalidInput {
            message: format!(
                "x_data and y_data length mismatch: {} vs {}",
                n_train,
                y_data.len()
            ),
        });
    }

    let n_dim = x_data[0].len();

    // Flatten training data to f32 for GPU
    let train_x_f32: Vec<f32> = x_data
        .iter()
        .flat_map(|row| row.iter().map(|&v| v as f32))
        .collect();

    // Compute pairwise distances on GPU
    let distances = compute_distances_gpu(&train_x_f32, n_train, n_dim, device.clone()).await?;

    // Flatten training data to f64 for kernel/solve
    let train_x: Vec<f64> = x_data.iter().flat_map(|row| row.iter().copied()).collect();

    // Assemble and solve (always f64, GPU)
    let surrogate = assemble_and_solve(
        device.clone(),
        &train_x,
        &distances,
        y_data,
        kernel,
        smoothing,
        n_train,
        n_dim,
    )?;

    let diagnostics = TrainingDiagnostics {
        used_f32_distances: true,
        used_gpu: true,
        n_train,
        n_dim,
        system_size: n_train + n_dim + 1,
        max_distance_error: None,
    };

    Ok((surrogate, diagnostics))
}

/// Compute pairwise Euclidean distances on GPU using cdist.wgsl shader.
///
/// This is the GPU fast path that replaces CPU f32 computation for large N.
/// Returns f64 distances (promoted from f32 GPU output).
async fn compute_distances_gpu(
    x_f32: &[f32],
    n: usize,
    n_dim: usize,
    device: Arc<WgpuDevice>,
) -> Result<Vec<f64>> {
    // Create GPU tensor (zero-copy: from_data borrows slice)
    let tensor = Tensor::from_data(x_f32, vec![n, n_dim], device)?;

    // Compute pairwise distances using cdist shader
    // For self-distance matrix, input_a == input_b
    let distances_tensor = tensor
        .clone()
        .cdist_wgsl(tensor, DistanceMetric::Euclidean)?;

    // Read back to CPU and promote to f64
    let distances_f32 = distances_tensor.to_vec()?;
    let distances_f64: Vec<f64> = distances_f32.iter().map(|&v| v as f64).collect();

    Ok(distances_f64)
}

/// Train with validation: compute both f32 and f64 distances and report error.
///
/// Useful for verifying that the f32 path doesn't introduce significant error.
///
/// # Errors
///
/// Returns [`Err`] if training data is empty, `x_data` and `y_data` lengths
/// differ, or the linear solve fails.
pub fn train_with_validation(
    device: Arc<WgpuDevice>,
    x_data: &[Vec<f64>],
    y_data: &[f64],
    kernel: RBFKernel,
    smoothing: f64,
) -> Result<(RBFSurrogate, TrainingDiagnostics)> {
    let n_train = x_data.len();

    if n_train == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "Training data cannot be empty".to_string(),
        });
    }

    if y_data.len() != n_train {
        return Err(BarracudaError::InvalidInput {
            message: format!(
                "x_data and y_data length mismatch: {} vs {}",
                n_train,
                y_data.len()
            ),
        });
    }

    let n_dim = x_data[0].len();
    let train_x: Vec<f64> = x_data.iter().flat_map(|row| row.iter().copied()).collect();

    // Compute both paths
    let distances_f64 = compute_distances_f64(&train_x, &train_x, n_train, n_train, n_dim);
    let distances_f32 = compute_distances_f32_promoted(&train_x, &train_x, n_train, n_train, n_dim);

    // Compute max error
    let max_error = distances_f64
        .iter()
        .zip(distances_f32.iter())
        .map(|(a, b)| (a - b).abs())
        .fold(0.0_f64, f64::max);

    // Use f64 distances for the actual solve (GPU)
    let surrogate = assemble_and_solve(
        device,
        &train_x,
        &distances_f64,
        y_data,
        kernel,
        smoothing,
        n_train,
        n_dim,
    )?;

    let diagnostics = TrainingDiagnostics {
        used_f32_distances: false,
        used_gpu: false,
        n_train,
        n_dim,
        system_size: n_train + n_dim + 1,
        max_distance_error: Some(max_error),
    };

    Ok((surrogate, diagnostics))
}

/// Assemble the augmented system and solve for RBF weights.
fn assemble_and_solve(
    device: Arc<WgpuDevice>,
    train_x: &[f64],
    distances: &[f64],
    y_data: &[f64],
    kernel: RBFKernel,
    smoothing: f64,
    n_train: usize,
    n_dim: usize,
) -> Result<RBFSurrogate> {
    let n_poly = n_dim + 1;
    let n_total = n_train + n_poly;

    let mut a = vec![0.0; n_total * n_total];
    let mut b = vec![0.0; n_total];

    // Top-left: Kernel matrix K + smoothing·I
    for i in 0..n_train {
        for j in 0..n_train {
            let k_ij = kernel.eval(distances[i * n_train + j]);
            let smooth = if i == j { smoothing } else { 0.0 };
            a[i * n_total + j] = k_ij + smooth;
        }
    }

    // Top-right and bottom-left: Polynomial matrix P
    for i in 0..n_train {
        a[i * n_total + n_train] = 1.0;
        a[n_train * n_total + i] = 1.0;

        for d in 0..n_dim {
            a[i * n_total + (n_train + 1 + d)] = train_x[i * n_dim + d];
            a[(n_train + 1 + d) * n_total + i] = train_x[i * n_dim + d];
        }
    }

    b[..n_train].copy_from_slice(y_data);

    let mut weights = solve_f64(device.clone(), &a, &b, n_total)?;
    let poly_coeffs = weights.split_off(n_train);

    Ok(RBFSurrogate::from_parts(
        device,
        RbfTrainingData {
            train_x: train_x.to_vec(),
            train_y: y_data.to_vec(),
            n_train,
            n_dim,
        },
        RbfTrainedModel {
            weights,
            poly_coeffs,
            kernel,
            smoothing,
        },
    ))
}

/// Compute pairwise Euclidean distances in f64.
fn compute_distances_f64(x1: &[f64], x2: &[f64], n1: usize, n2: usize, n_dim: usize) -> Vec<f64> {
    let mut distances = vec![0.0; n1 * n2];

    for i in 0..n1 {
        for j in 0..n2 {
            let mut dist_sq = 0.0;
            for d in 0..n_dim {
                let diff = x1[i * n_dim + d] - x2[j * n_dim + d];
                dist_sq = diff.mul_add(diff, dist_sq);
            }
            distances[i * n2 + j] = dist_sq.sqrt();
        }
    }

    distances
}

/// Compute pairwise Euclidean distances in f32, promoted to f64.
///
/// This is the CPU fast path that mirrors what `cdist.wgsl` does on GPU.
/// The f32 arithmetic is ~2× faster due to SIMD width and the distances
/// are sufficiently accurate for RBF kernel evaluation.
///
/// When a GPU is available, this function can be replaced with:
/// ```ignore
/// let tensor_a = Tensor::from_data(x1_f32, [n1, n_dim], device);
/// let tensor_b = Tensor::from_data(x2_f32, [n2, n_dim], device);
/// let distances = tensor_a.cdist_wgsl(tensor_b, DistanceMetric::Euclidean)?;
/// ```
fn compute_distances_f32_promoted(
    x1: &[f64],
    x2: &[f64],
    n1: usize,
    n2: usize,
    n_dim: usize,
) -> Vec<f64> {
    // Downcast to f32 for fast computation
    let x1_f32: Vec<f32> = x1.iter().map(|&v| v as f32).collect();
    let x2_f32: Vec<f32> = x2.iter().map(|&v| v as f32).collect();

    let mut distances = vec![0.0f64; n1 * n2];

    for i in 0..n1 {
        for j in 0..n2 {
            let mut dist_sq = 0.0f32;
            for d in 0..n_dim {
                let diff = x1_f32[i * n_dim + d] - x2_f32[j * n_dim + d];
                dist_sq = diff.mul_add(diff, dist_sq);
            }
            // Promote result to f64
            distances[i * n2 + j] = (dist_sq.sqrt()) as f64;
        }
    }

    distances
}

#[cfg(test)]
mod tests;