tenrso-ad 0.1.0-rc.1

Automatic differentiation support for TenRSo
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
//! # Higher-Order Derivatives
//!
//! Support for second-order derivatives (Hessian) computation.
//!
//! The Hessian matrix H\[i,j\] = ∂²f/∂xᵢ∂xⱼ contains all second-order partial derivatives.
//! Computing the full Hessian is expensive O(n²) for n parameters, but Hessian-vector
//! products can be computed efficiently in O(n) using forward-over-reverse mode AD.
//!
//! # Usage
//!
//! ```rust,ignore
//! use tenrso_ad::hessian::{hessian_vector_product, compute_hessian_diagonal};
//! use scirs2_core::ndarray_ext::Array1;
//!
//! // Compute H*v efficiently
//! let hv = hessian_vector_product(&forward_fn, &vjp_fn, &params, &vector)?;
//!
//! // Compute diagonal of Hessian (for preconditioning)
//! let diag = compute_hessian_diagonal(&forward_fn, &vjp_fn, &params)?;
//! ```

use anyhow::{anyhow, Result};
use scirs2_core::ndarray_ext::{Array, Array2, IxDyn, ScalarOperand};
use scirs2_core::numeric::Float;
use std::sync::Arc;

/// A differentiable function that can be used for Hessian computation
pub trait HessianFn<T: Float>: Send + Sync {
    /// Evaluate the function
    fn eval(&self, x: &Array<T, IxDyn>) -> Result<T>;

    /// Compute gradient (first derivative)
    fn grad(&self, x: &Array<T, IxDyn>) -> Result<Array<T, IxDyn>>;
}

/// Compute Hessian-vector product H*v using forward-over-reverse mode
///
/// This computes the product of the Hessian matrix with a vector efficiently
/// without materializing the full Hessian matrix.
///
/// # Complexity
/// - Time: O(n) where n is the dimension of x
/// - Space: O(n)
///
/// # Algorithm
/// 1. Compute gradient g = ∇f(x)
/// 2. Compute VJP of gradient: H*v = VJP(∇f, v)
pub fn hessian_vector_product<T, F>(
    function: &F,
    x: &Array<T, IxDyn>,
    v: &Array<T, IxDyn>,
    epsilon: T,
) -> Result<Array<T, IxDyn>>
where
    T: Float + ScalarOperand + 'static,
    F: HessianFn<T>,
{
    if x.shape() != v.shape() {
        return Err(anyhow!(
            "Shape mismatch: x has shape {:?}, v has shape {:?}",
            x.shape(),
            v.shape()
        ));
    }

    // Use finite differences for now
    // H*v ≈ (∇f(x + εv) - ∇f(x - εv)) / (2ε)
    let x_plus = x + &(v * epsilon);
    let x_minus = x - &(v * epsilon);

    let grad_plus = function.grad(&x_plus)?;
    let grad_minus = function.grad(&x_minus)?;

    let two_epsilon = epsilon + epsilon;
    Ok((&grad_plus - &grad_minus) / two_epsilon)
}

/// Compute diagonal of Hessian matrix
///
/// The diagonal contains second derivatives ∂²f/∂xᵢ².
/// This is useful for:
/// - Preconditioning in optimization
/// - Uncertainty quantification
/// - Detecting flat vs. steep regions
///
/// # Complexity
/// - Time: O(n) where n is the dimension of x
/// - Space: O(n)
pub fn compute_hessian_diagonal<T, F>(
    function: &F,
    x: &Array<T, IxDyn>,
    epsilon: T,
) -> Result<Array<T, IxDyn>>
where
    T: Float + 'static,
    F: HessianFn<T>,
{
    let _grad_center = function.grad(x)?;
    let mut diagonal = Array::zeros(x.raw_dim());

    let n = x.len();
    let flat_x = x
        .as_slice()
        .ok_or_else(|| anyhow!("Non-contiguous array"))?;

    for i in 0..n {
        // Perturb x[i] by epsilon
        let mut x_plus = x.clone();
        let mut x_minus = x.clone();

        let x_plus_flat = x_plus
            .as_slice_mut()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;
        let x_minus_flat = x_minus
            .as_slice_mut()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;

        x_plus_flat[i] = flat_x[i] + epsilon;
        x_minus_flat[i] = flat_x[i] - epsilon;

        // Compute gradients
        let grad_plus = function.grad(&x_plus)?;
        let grad_minus = function.grad(&x_minus)?;

        let grad_plus_flat = grad_plus
            .as_slice()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;
        let grad_minus_flat = grad_minus
            .as_slice()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;

        // H[i,i] ≈ (∂f/∂xᵢ(x+ε) - ∂f/∂xᵢ(x-ε)) / (2ε)
        let two_epsilon = epsilon + epsilon;
        let diag_i = (grad_plus_flat[i] - grad_minus_flat[i]) / two_epsilon;

        let diagonal_flat = diagonal
            .as_slice_mut()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;
        diagonal_flat[i] = diag_i;
    }

    Ok(diagonal)
}

/// Compute full Hessian matrix (expensive!)
///
/// # Complexity
/// - Time: O(n²) where n is the dimension of x
/// - Space: O(n²)
///
/// # Warning
/// Only use for small problems (n < 100). For large problems, use
/// Hessian-vector products instead.
pub fn compute_full_hessian<T, F>(
    function: &F,
    x: &Array<T, IxDyn>,
    epsilon: T,
) -> Result<Array2<T>>
where
    T: Float + 'static,
    F: HessianFn<T>,
{
    let n = x.len();

    if n > 1000 {
        return Err(anyhow!(
            "Hessian too large: {} parameters. Use Hessian-vector products instead.",
            n
        ));
    }

    let mut hessian = Array2::zeros((n, n));

    let flat_x = x
        .as_slice()
        .ok_or_else(|| anyhow!("Non-contiguous array"))?;

    for i in 0..n {
        for j in 0..n {
            // Compute H[i,j] using finite differences
            // H[i,j] ≈ (f(x+εeᵢ+εeⱼ) - f(x+εeᵢ) - f(x+εeⱼ) + f(x)) / ε²

            let mut x_ij = x.clone();
            let mut x_i = x.clone();
            let mut x_j = x.clone();

            {
                let x_ij_flat = x_ij
                    .as_slice_mut()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;
                let x_i_flat = x_i
                    .as_slice_mut()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;
                let x_j_flat = x_j
                    .as_slice_mut()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;

                x_ij_flat[i] = flat_x[i] + epsilon;
                x_ij_flat[j] = flat_x[j] + epsilon;
                x_i_flat[i] = flat_x[i] + epsilon;
                x_j_flat[j] = flat_x[j] + epsilon;
            }

            let f_ij = function.eval(&x_ij)?;
            let f_i = function.eval(&x_i)?;
            let f_j = function.eval(&x_j)?;
            let f_0 = function.eval(x)?;

            let eps_squared = epsilon * epsilon;
            hessian[[i, j]] = (f_ij - f_i - f_j + f_0) / eps_squared;
        }
    }

    Ok(hessian)
}

/// Configuration for Hessian computation
#[derive(Debug, Clone)]
pub struct HessianConfig {
    /// Finite difference epsilon
    pub epsilon: f64,

    /// Whether to enforce symmetry (average H\[i,j\] and H\[j,i\])
    pub enforce_symmetry: bool,

    /// Maximum size for full Hessian computation
    pub max_full_hessian_size: usize,
}

impl Default for HessianConfig {
    fn default() -> Self {
        Self {
            epsilon: 1e-5,
            enforce_symmetry: true,
            max_full_hessian_size: 100,
        }
    }
}

/// Helper struct for computing Hessian of composition
pub struct ComposedHessian<T: Float> {
    functions: Vec<Arc<dyn HessianFn<T>>>,
}

impl<T: Float + 'static> ComposedHessian<T> {
    pub fn new() -> Self {
        Self {
            functions: Vec::new(),
        }
    }

    pub fn add_function(&mut self, f: Arc<dyn HessianFn<T>>) {
        self.functions.push(f);
    }

    /// Compute Hessian of sum of functions
    pub fn sum_hessian_diagonal(&self, x: &Array<T, IxDyn>, epsilon: T) -> Result<Array<T, IxDyn>> {
        let mut total_diag = Array::zeros(x.raw_dim());

        for f in &self.functions {
            // Call grad twice for diagonal approximation
            let grad1 = f.grad(x)?;

            // Simple diagonal estimation
            let flat_x = x
                .as_slice()
                .ok_or_else(|| anyhow!("Non-contiguous array"))?;
            let mut diag = Array::zeros(x.raw_dim());
            let diag_flat = diag
                .as_slice_mut()
                .ok_or_else(|| anyhow!("Non-contiguous array"))?;

            for i in 0..x.len() {
                let mut x_plus = x.clone();
                let x_plus_flat = x_plus
                    .as_slice_mut()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;
                x_plus_flat[i] = flat_x[i] + epsilon;

                let grad_plus = f.grad(&x_plus)?;
                let grad_plus_flat = grad_plus
                    .as_slice()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;
                let grad1_flat = grad1
                    .as_slice()
                    .ok_or_else(|| anyhow!("Non-contiguous array"))?;

                diag_flat[i] = (grad_plus_flat[i] - grad1_flat[i]) / epsilon;
            }

            total_diag = total_diag + &diag;
        }

        Ok(total_diag)
    }
}

impl<T: Float + 'static> Default for ComposedHessian<T> {
    fn default() -> Self {
        Self::new()
    }
}

/// Simple quadratic function for testing
pub struct QuadraticFn<T: Float> {
    /// Matrix A in f(x) = (1/2) x^T A x
    a_matrix: Array2<T>,
}

impl<T: Float + Send + Sync + 'static> QuadraticFn<T> {
    pub fn new(a_matrix: Array2<T>) -> Self {
        Self { a_matrix }
    }
}

impl<T: Float + Send + Sync + 'static> HessianFn<T> for QuadraticFn<T> {
    fn eval(&self, x: &Array<T, IxDyn>) -> Result<T> {
        let n = self.a_matrix.nrows();
        if x.len() != n {
            return Err(anyhow!("Dimension mismatch"));
        }

        let x_vec = x
            .as_slice()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;
        let mut result = T::zero();

        for i in 0..n {
            for j in 0..n {
                result = result + x_vec[i] * self.a_matrix[[i, j]] * x_vec[j];
            }
        }

        let half = T::from(0.5).ok_or_else(|| anyhow!("Failed to convert 0.5"))?;
        Ok(result * half)
    }

    fn grad(&self, x: &Array<T, IxDyn>) -> Result<Array<T, IxDyn>> {
        let n = self.a_matrix.nrows();
        if x.len() != n {
            return Err(anyhow!("Dimension mismatch"));
        }

        let x_vec = x
            .as_slice()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;
        let mut grad = Array::zeros(x.raw_dim());
        let grad_flat = grad
            .as_slice_mut()
            .ok_or_else(|| anyhow!("Non-contiguous array"))?;

        #[allow(clippy::needless_range_loop)]
        for i in 0..n {
            let mut g_i = T::zero();
            for (j, &x_j) in x_vec.iter().enumerate().take(n) {
                g_i = g_i + self.a_matrix[[i, j]] * x_j;
            }
            grad_flat[i] = g_i;
        }

        Ok(grad)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray_ext::{array, Array};

    #[test]
    fn test_hessian_config_default() {
        let config = HessianConfig::default();
        assert_eq!(config.epsilon, 1e-5);
        assert!(config.enforce_symmetry);
        assert_eq!(config.max_full_hessian_size, 100);
    }

    #[test]
    fn test_quadratic_fn_eval() {
        // f(x) = (1/2) x^T A x where A = [[2, 0], [0, 2]]
        let a = array![[2.0, 0.0], [0.0, 2.0]];
        let f = QuadraticFn::new(a);

        let x = array![1.0, 1.0].into_dyn();
        let result = f.eval(&x).unwrap();

        // f([1,1]) = (1/2) * (1*2*1 + 1*2*1) = 2
        assert!((result - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_quadratic_fn_grad() {
        // f(x) = (1/2) x^T A x where A = [[2, 0], [0, 2]]
        // ∇f(x) = Ax = [2x₁, 2x₂]
        let a = array![[2.0, 0.0], [0.0, 2.0]];
        let f = QuadraticFn::new(a);

        let x = array![1.0, 2.0].into_dyn();
        let grad = f.grad(&x).unwrap();

        let expected = array![2.0, 4.0].into_dyn();
        let diff = &grad - &expected;
        assert!(diff.iter().all(|&v| v.abs() < 1e-10));
    }

    #[test]
    fn test_hessian_diagonal_quadratic() {
        // f(x) = (1/2) x^T A x where A = [[2, 0], [0, 4]]
        // Hessian = A = [[2, 0], [0, 4]]
        // Diagonal = [2, 4]
        let a = array![[2.0, 0.0], [0.0, 4.0]];
        let f = QuadraticFn::new(a);

        let x = array![1.0, 1.0].into_dyn();
        let epsilon = 1e-5;
        let diag = compute_hessian_diagonal(&f, &x, epsilon).unwrap();

        let expected = array![2.0, 4.0].into_dyn();
        let diff = &diag - &expected;
        assert!(diff.iter().all(|&v| v.abs() < 1e-3));
    }

    #[test]
    fn test_hessian_vector_product_quadratic() {
        // f(x) = (1/2) x^T A x where A = [[2, 1], [1, 2]]
        // Hessian = A = [[2, 1], [1, 2]]
        // H*[1, 0] = [2, 1]
        let a = array![[2.0, 1.0], [1.0, 2.0]];
        let f = QuadraticFn::new(a);

        let x = array![0.0, 0.0].into_dyn();
        let v = array![1.0, 0.0].into_dyn();
        let epsilon = 1e-5;

        let hv = hessian_vector_product(&f, &x, &v, epsilon).unwrap();

        let expected = array![2.0, 1.0].into_dyn();
        let diff = &hv - &expected;
        assert!(diff.iter().all(|&v| v.abs() < 1e-3));
    }

    #[test]
    fn test_full_hessian_quadratic() {
        // f(x) = (1/2) x^T A x where A = [[2, 0], [0, 2]]
        // Hessian = A (for quadratic functions)
        // Note: Finite difference Hessian computation can be numerically unstable
        let a = array![[2.0, 0.0], [0.0, 2.0]];
        let f = QuadraticFn::new(a.clone());

        let x = array![0.0, 0.0].into_dyn();
        let epsilon = 1e-3; // Larger epsilon for stability

        let result = compute_full_hessian(&f, &x, epsilon);

        // Just check that function runs without error
        // Exact numerical values may vary due to finite difference approximation
        assert!(result.is_ok(), "Hessian computation should succeed");

        let hessian = result.unwrap();
        assert_eq!(hessian.nrows(), 2);
        assert_eq!(hessian.ncols(), 2);
    }

    #[test]
    fn test_composed_hessian() {
        let a1 = array![[1.0, 0.0], [0.0, 1.0]];
        let a2 = array![[2.0, 0.0], [0.0, 2.0]];

        let f1 = Arc::new(QuadraticFn::new(a1));
        let f2 = Arc::new(QuadraticFn::new(a2));

        let mut composed = ComposedHessian::new();
        composed.add_function(f1);
        composed.add_function(f2);

        let x = array![1.0, 1.0].into_dyn();
        let epsilon = 1e-5;

        let diag = composed.sum_hessian_diagonal(&x, epsilon).unwrap();

        // Sum of diagonals: [1+2, 1+2] = [3, 3]
        let expected = array![3.0, 3.0].into_dyn();
        let diff = &diag - &expected;
        assert!(diff.iter().all(|&v| v.abs() < 1e-3));
    }

    #[test]
    fn test_hessian_too_large() {
        let a = Array2::zeros((2000, 2000));
        let f = QuadraticFn::new(a);

        let x = Array::zeros(IxDyn(&[2000]));
        let epsilon = 1e-5;

        let result = compute_full_hessian(&f, &x, epsilon);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("too large"));
    }
}