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
// SPDX-License-Identifier: AGPL-3.0-or-later
//! BFGS Quasi-Newton Optimizer
//!
//! The Broyden-Fletcher-Goldfarb-Shanno (BFGS) algorithm is a quasi-Newton
//! optimization method that builds an approximation to the inverse Hessian
//! matrix using gradient information.
//!
//! # Algorithm
//!
//! BFGS iteratively updates:
//! 1. Search direction: d = -H·∇f
//! 2. Line search: α = argmin f(x + α·d)
//! 3. Position update: x ← x + α·d
//! 4. Hessian approximation update using BFGS formula
//!
//! # Applications
//!
//! - **Parameter fitting**: Physics models, neural networks
//! - **Maximum likelihood**: Statistical inference
//! - **Energy minimization**: Molecular geometry optimization
//! - **Control optimization**: Trajectory planning
//!
//! # References
//!
//! - Nocedal, J. & Wright, S. (2006), "Numerical Optimization"
//! - Broyden, C. G. (1970)

use crate::error::{BarracudaError, Result};

/// GPU shader for BFGS inverse Hessian update (O(n²) parallel).
///
/// f64 canonical — f32 derived via downcast when needed.
const WGSL_BFGS_UPDATE_F64: &str = include_str!("../shaders/optimizer/bfgs_update_f64.wgsl");

/// Entry points: `bfgs_update`, `dot_product`, `mat_vec_mul`, `compute_Hy_and_yHy`.
pub static WGSL_BFGS_UPDATE: std::sync::LazyLock<String> =
    std::sync::LazyLock::new(|| WGSL_BFGS_UPDATE_F64.to_string());

/// GPU shader for batch numerical gradient via central/forward differences.
///
/// Entry points: `central_difference`, `forward_difference`, `generate_perturbed_points`.
const WGSL_BATCH_GRADIENT_F64: &str = include_str!("../shaders/optimizer/batch_gradient_f64.wgsl");

/// WGSL batch gradient shader (f32, downcast from f64).
pub static WGSL_BATCH_GRADIENT: std::sync::LazyLock<String> =
    std::sync::LazyLock::new(|| WGSL_BATCH_GRADIENT_F64.to_string());

/// Configuration for the BFGS optimizer.
#[derive(Debug, Clone)]
pub struct BfgsConfig {
    /// Maximum number of iterations
    pub max_iter: usize,
    /// Gradient norm tolerance for convergence
    pub gtol: f64,
    /// Step size tolerance for convergence
    pub xtol: f64,
    /// Function value tolerance for convergence
    pub ftol: f64,
    /// Line search parameters (c1, c2 for Wolfe conditions)
    pub c1: f64,
    /// Second Wolfe condition parameter (curvature)
    pub c2: f64,
    /// Maximum line search iterations
    pub max_linesearch: usize,
}

impl Default for BfgsConfig {
    fn default() -> Self {
        Self {
            max_iter: 1000,
            gtol: 1e-6,
            xtol: 1e-8,
            ftol: 1e-8,
            c1: 1e-4,
            c2: 0.9,
            max_linesearch: 20,
        }
    }
}

impl BfgsConfig {
    /// Create a new configuration with specified tolerances.
    #[must_use]
    pub fn new(gtol: f64, max_iter: usize) -> Self {
        Self {
            max_iter,
            gtol,
            ..Default::default()
        }
    }
}

/// Result of BFGS optimization.
#[derive(Debug)]
pub struct BfgsResult {
    /// Optimal point found
    pub x: Vec<f64>,
    /// Function value at optimum
    pub f: f64,
    /// Gradient at optimum
    pub grad: Vec<f64>,
    /// Number of iterations
    pub n_iter: usize,
    /// Number of function evaluations
    pub n_feval: usize,
    /// Number of gradient evaluations
    pub n_geval: usize,
    /// Convergence status
    pub converged: bool,
    /// Reason for termination
    pub message: String,
}

/// BFGS optimizer for unconstrained minimization.
///
/// # Arguments
///
/// * `f` - Objective function f(x) -> f64
/// * `grad_f` - Gradient function ∇f(x) -> `Vec<f64>`
/// * `x0` - Initial guess
/// * `config` - Optimizer configuration
///
/// # Returns
///
/// [`BfgsResult`] containing the solution and diagnostics.
///
/// # Example
///
/// ```
/// use barracuda::optimize::bfgs::{bfgs, BfgsConfig};
///
/// // Minimize f(x) = x₀² + x₁²
/// let f = |x: &[f64]| x[0] * x[0] + x[1] * x[1];
/// let grad = |x: &[f64]| vec![2.0 * x[0], 2.0 * x[1]];
///
/// let config = BfgsConfig::default();
/// let result = bfgs(&f, &grad, &[1.0, 1.0], &config).unwrap();
///
/// assert!(result.x[0].abs() < 1e-5);
/// assert!(result.x[1].abs() < 1e-5);
/// ```
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn bfgs<F, G>(f: &F, grad_f: &G, x0: &[f64], config: &BfgsConfig) -> Result<BfgsResult>
where
    F: Fn(&[f64]) -> f64,
    G: Fn(&[f64]) -> Vec<f64>,
{
    let n = x0.len();
    if n == 0 {
        return Err(BarracudaError::InvalidInput {
            message: "x0 must not be empty".to_string(),
        });
    }

    let mut x = x0.to_vec();
    let mut fx = f(&x);
    let mut gx = grad_f(&x);
    let mut n_feval = 1;
    let mut n_geval = 1;

    // Initialize inverse Hessian approximation as identity
    let mut h_inv = identity_matrix(n);

    for iter in 0..config.max_iter {
        // Check gradient convergence
        let grad_norm = gx.iter().map(|g| g * g).sum::<f64>().sqrt();
        if grad_norm < config.gtol {
            return Ok(BfgsResult {
                x,
                f: fx,
                grad: gx,
                n_iter: iter,
                n_feval,
                n_geval,
                converged: true,
                message: "Gradient norm below tolerance".to_string(),
            });
        }

        // Compute search direction: d = -H⁻¹·g
        let d = mat_vec_mul(&h_inv, &gx)
            .into_iter()
            .map(|di| -di)
            .collect::<Vec<_>>();

        // Line search with backtracking
        let (alpha, fx_new, ls_evals) = backtracking_line_search(f, &x, &d, fx, &gx, config)?;
        n_feval += ls_evals;

        // Check for sufficient decrease
        if (fx - fx_new).abs() < config.ftol {
            return Ok(BfgsResult {
                x,
                f: fx,
                grad: gx,
                n_iter: iter,
                n_feval,
                n_geval,
                converged: true,
                message: "Function value change below tolerance".to_string(),
            });
        }

        // Update position: x_new = x + α·d
        let x_new: Vec<f64> = x
            .iter()
            .zip(d.iter())
            .map(|(xi, di)| xi + alpha * di)
            .collect();

        // Compute step and gradient difference
        let s: Vec<f64> = x_new.iter().zip(x.iter()).map(|(xn, xo)| xn - xo).collect();
        let step_norm = s.iter().map(|si| si * si).sum::<f64>().sqrt();

        if step_norm < config.xtol {
            return Ok(BfgsResult {
                x: x_new,
                f: fx_new,
                grad: gx,
                n_iter: iter,
                n_feval,
                n_geval,
                converged: true,
                message: "Step size below tolerance".to_string(),
            });
        }

        // Compute new gradient
        let gx_new = grad_f(&x_new);
        n_geval += 1;

        let y: Vec<f64> = gx_new
            .iter()
            .zip(gx.iter())
            .map(|(gn, go)| gn - go)
            .collect();

        // BFGS update: H⁻¹ ← (I - ρsy^T)H⁻¹(I - ρys^T) + ρss^T
        let sy: f64 = s.iter().zip(y.iter()).map(|(si, yi)| si * yi).sum();

        // Skip update if curvature condition is not satisfied
        if sy > 1e-10 {
            let rho = 1.0 / sy;
            bfgs_update(&mut h_inv, &s, &y, rho);
        }

        x = x_new;
        fx = fx_new;
        gx = gx_new;
    }

    Ok(BfgsResult {
        x,
        f: fx,
        grad: gx,
        n_iter: config.max_iter,
        n_feval,
        n_geval,
        converged: false,
        message: "Maximum iterations reached".to_string(),
    })
}

/// Backtracking line search satisfying Armijo condition.
fn backtracking_line_search<F>(
    f: &F,
    x: &[f64],
    d: &[f64],
    fx: f64,
    gx: &[f64],
    config: &BfgsConfig,
) -> Result<(f64, f64, usize)>
where
    F: Fn(&[f64]) -> f64,
{
    let mut alpha = 1.0;
    let dg: f64 = d.iter().zip(gx.iter()).map(|(di, gi)| di * gi).sum(); // directional derivative

    if dg >= 0.0 {
        return Err(BarracudaError::Numerical {
            message: "Search direction is not a descent direction".to_string(),
        });
    }

    let mut n_evals = 0;
    let rho = 0.5; // backtracking factor

    for _ in 0..config.max_linesearch {
        let x_new: Vec<f64> = x
            .iter()
            .zip(d.iter())
            .map(|(xi, di)| xi + alpha * di)
            .collect();
        let fx_new = f(&x_new);
        n_evals += 1;

        // Armijo condition: f(x + αd) ≤ f(x) + c₁·α·∇f·d
        if fx_new <= (config.c1 * alpha).mul_add(dg, fx) {
            return Ok((alpha, fx_new, n_evals));
        }

        alpha *= rho;
    }

    // Return best found even if line search didn't fully succeed
    let x_final: Vec<f64> = x
        .iter()
        .zip(d.iter())
        .map(|(xi, di)| xi + alpha * di)
        .collect();
    Ok((alpha, f(&x_final), n_evals + 1))
}

/// BFGS inverse Hessian update.
fn bfgs_update(h_inv: &mut [Vec<f64>], s: &[f64], y: &[f64], rho: f64) {
    let n = s.len();

    // Hy = H⁻¹·y
    let hy = mat_vec_mul(h_inv, y);

    // yHy = y^T·H⁻¹·y
    let yhy: f64 = y.iter().zip(hy.iter()).map(|(yi, hyi)| yi * hyi).sum();

    // Update H⁻¹ using Sherman-Morrison-Woodbury-like formula
    // H_new = (I - ρsy^T)H(I - ρys^T) + ρss^T
    // = H - ρ(sy^T)H - ρH(ys^T) + ρ²(sy^T)H(ys^T) + ρss^T
    // = H - ρ(s⊗Hy) - ρ(Hy⊗s) + ρ²(yHy)(s⊗s) + ρ(s⊗s)
    // = H - ρ(s⊗Hy + Hy⊗s) + (ρ²yHy + ρ)(s⊗s)
    // = H - ρ(s⊗Hy + Hy⊗s) + ρ(1 + ρyHy)(s⊗s)

    let factor = rho * rho.mul_add(yhy, 1.0);

    for i in 0..n {
        for j in 0..n {
            h_inv[i][j] +=
                (factor * s[i]).mul_add(s[j], -(rho * s[i].mul_add(hy[j], hy[i] * s[j])));
        }
    }
}

/// Create n×n identity matrix.
fn identity_matrix(n: usize) -> Vec<Vec<f64>> {
    let mut mat = vec![vec![0.0; n]; n];
    for (i, row) in mat.iter_mut().enumerate() {
        row[i] = 1.0;
    }
    mat
}

/// Matrix-vector multiplication.
fn mat_vec_mul(mat: &[Vec<f64>], vec: &[f64]) -> Vec<f64> {
    mat.iter()
        .map(|row| row.iter().zip(vec.iter()).map(|(a, b)| a * b).sum())
        .collect()
}

/// Compute numerical gradient using central differences.
///
/// Useful when analytical gradients are not available.
pub fn numerical_gradient<F>(f: &F, x: &[f64], eps: f64) -> Vec<f64>
where
    F: Fn(&[f64]) -> f64,
{
    let n = x.len();
    let mut grad = vec![0.0; n];
    let mut x_plus = x.to_vec();
    let mut x_minus = x.to_vec();

    for i in 0..n {
        x_plus[i] = x[i] + eps;
        x_minus[i] = x[i] - eps;

        grad[i] = (f(&x_plus) - f(&x_minus)) / (2.0 * eps);

        x_plus[i] = x[i];
        x_minus[i] = x[i];
    }

    grad
}

/// BFGS with numerical gradient (convenience wrapper).
///
/// Uses central difference approximation for gradients.
///
/// # Errors
///
/// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
/// readback fails (e.g. device lost or out of memory).
pub fn bfgs_numerical<F>(f: &F, x0: &[f64], config: &BfgsConfig) -> Result<BfgsResult>
where
    F: Fn(&[f64]) -> f64,
{
    let eps = 1e-8;
    let grad_f = |x: &[f64]| numerical_gradient(f, x, eps);
    bfgs(f, &grad_f, x0, config)
}

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

    /// Maximum iterations for difficult optimization problems (e.g. Rosenbrock).
    const BFGS_MAX_ITER_EXTENDED: usize = 5000;

    #[test]
    fn test_bfgs_quadratic() {
        // Minimize f(x) = x₀² + 2x₁²
        let f = |x: &[f64]| x[0].mul_add(x[0], 2.0 * x[1] * x[1]);
        let grad = |x: &[f64]| vec![2.0 * x[0], 4.0 * x[1]];

        let config = BfgsConfig::default();
        let result = bfgs(&f, &grad, &[1.0, 1.0], &config).unwrap();

        assert!(result.converged);
        assert!(result.x[0].abs() < 1e-3, "x[0] = {}", result.x[0]);
        assert!(result.x[1].abs() < 1e-3, "x[1] = {}", result.x[1]);
        assert!(result.f < 1e-5, "f = {}", result.f);
    }

    #[test]
    fn test_bfgs_rosenbrock() {
        // Rosenbrock function: f(x) = (1-x₀)² + 100(x₁-x₀²)²
        let f =
            |x: &[f64]| (1.0 - x[0]).mul_add(1.0 - x[0], 100.0 * x[0].mul_add(-x[0], x[1]).powi(2));
        let grad = |x: &[f64]| {
            vec![
                (-2.0f64).mul_add(1.0 - x[0], -(400.0 * x[0] * x[0].mul_add(-x[0], x[1]))),
                200.0 * x[0].mul_add(-x[0], x[1]),
            ]
        };

        let config = BfgsConfig {
            max_iter: BFGS_MAX_ITER_EXTENDED,
            gtol: 1e-6,
            ..Default::default()
        };

        let result = bfgs(&f, &grad, &[0.0, 0.0], &config).unwrap();

        assert!(result.converged, "Failed to converge: {}", result.message);
        assert!((result.x[0] - 1.0).abs() < 1e-4, "x[0] = {}", result.x[0]);
        assert!((result.x[1] - 1.0).abs() < 1e-4, "x[1] = {}", result.x[1]);
    }

    #[test]
    fn test_bfgs_numerical_gradient() {
        let f = |x: &[f64]| x[0].mul_add(x[0], x[1] * x[1]);

        let config = BfgsConfig::default();
        let result = bfgs_numerical(&f, &[1.0, 1.0], &config).unwrap();

        assert!(result.converged);
        assert!(result.x[0].abs() < 1e-4);
        assert!(result.x[1].abs() < 1e-4);
    }

    #[test]
    fn test_numerical_gradient() {
        let f = |x: &[f64]| x[0].mul_add(x[1], x[0].mul_add(x[0], 2.0 * x[1] * x[1]));
        let x = vec![1.0, 2.0];

        let grad = numerical_gradient(&f, &x, 1e-6);

        // Analytical: ∂f/∂x₀ = 2x₀ + x₁ = 4
        //             ∂f/∂x₁ = 4x₁ + x₀ = 9
        assert!((grad[0] - 4.0).abs() < 1e-4);
        assert!((grad[1] - 9.0).abs() < 1e-4);
    }

    #[test]
    fn test_bfgs_1d() {
        // f(x) = (x-2)²
        let f = |x: &[f64]| (x[0] - 2.0).powi(2);
        let grad = |x: &[f64]| vec![2.0 * (x[0] - 2.0)];

        let config = BfgsConfig::default();
        let result = bfgs(&f, &grad, &[0.0], &config).unwrap();

        assert!(result.converged);
        assert!((result.x[0] - 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_bfgs_high_dim() {
        // f(x) = Σᵢ xᵢ²
        let n = 10;
        let f = |x: &[f64]| x.iter().map(|xi| xi * xi).sum();
        let grad = |x: &[f64]| x.iter().map(|xi| 2.0 * xi).collect();

        let x0: Vec<f64> = (0..n).map(|i| (i + 1) as f64).collect();

        let config = BfgsConfig::default();
        let result = bfgs(&f, &grad, &x0, &config).unwrap();

        assert!(result.converged);
        for (i, xi) in result.x.iter().enumerate() {
            assert!(xi.abs() < 1e-5, "x[{i}] = {xi}, expected ~0");
        }
    }

    #[test]
    fn test_config_builder() {
        let config = BfgsConfig::new(1e-8, 500);
        assert!((config.gtol - 1e-8).abs() < 1e-14);
        assert_eq!(config.max_iter, 500);
    }
}