|
| 1 | +//! Finite-difference gradient checks over core differentiable ops (activations, reductions, shape ops, conv/pool, norms, |
| 2 | +//! attention, indexing). Inputs avoid kinks (ties, zero) so central differences are valid. |
| 3 | +
|
| 4 | +use oxideon_core::autograd_utils::gradcheck; |
| 5 | +use oxideon_core::nn::functional_ext::{group_norm, layer_norm, scaled_dot_product_attention}; |
| 6 | +use oxideon_core::Tensor; |
| 7 | + |
| 8 | +fn r(n: usize, shape: &[usize], seed: f64) -> Tensor { |
| 9 | + let v: Vec<f64> = (0..n).map(|i| ((i as f64 + seed) * 0.913).sin() * 1.3 + 0.17 * (i as f64 % 3.0) + 0.05).collect(); |
| 10 | + Tensor::from_slice(&v, shape).unwrap() |
| 11 | +} |
| 12 | +fn pos(n: usize, shape: &[usize]) -> Tensor { |
| 13 | + r(n, shape, 1.0).abs().unwrap().add_scalar(0.5).unwrap() |
| 14 | +} |
| 15 | + |
| 16 | +macro_rules! check { |
| 17 | + ($name:ident, $inputs:expr, $f:expr) => { |
| 18 | + #[test] |
| 19 | + fn $name() { |
| 20 | + let r = gradcheck($f, &$inputs, 1e-6, 1e-5, 1e-4); |
| 21 | + assert!(r.is_ok(), "{}: {:?}", stringify!($name), r.err()); |
| 22 | + } |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +check!(gelu, [r(6, &[6], 0.0)], |x| x[0].gelu()); |
| 27 | +check!(silu, [r(6, &[6], 0.0)], |x| x[0].silu()); |
| 28 | +check!(elu, [r(6, &[6], 0.0)], |x| x[0].elu(1.0)); |
| 29 | +check!(softplus, [r(6, &[6], 0.0)], |x| x[0].softplus()); |
| 30 | +check!(tanh, [r(6, &[6], 0.0)], |x| x[0].tanh()); |
| 31 | +check!(sigmoid, [r(6, &[6], 0.0)], |x| x[0].sigmoid()); |
| 32 | +check!(erf, [r(6, &[6], 0.0)], |x| x[0].erf()); |
| 33 | +check!(sqrt_rsqrt, [pos(6, &[6])], |x| x[0].sqrt()?.add(&x[0].rsqrt()?)); |
| 34 | +check!(asin_atan, [r(6, &[6], 0.0).mul_scalar(0.4).unwrap()], |x| x[0].asin()?.add(&x[0].atan()?)); |
| 35 | +check!(sinh_expm1_log1p, [pos(6, &[6])], |x| x[0].sinh()?.add(&x[0].expm1()?)?.add(&x[0].log1p()?)); |
| 36 | +check!(pow_tensor, [pos(6, &[6]), r(6, &[6], 3.0)], |x| x[0].pow(&x[1])); |
| 37 | +check!(atan2, [r(6, &[6], 0.0), pos(6, &[6])], |x| x[0].atan2(&x[1])); |
| 38 | +check!(maximum, [r(6, &[6], 0.0), r(6, &[6], 9.0)], |x| x[0].maximum(&x[1])); |
| 39 | +check!(softmax, [r(12, &[3, 4], 0.0)], |x| x[0].softmax(-1)); |
| 40 | +check!(log_softmax, [r(12, &[3, 4], 0.0)], |x| x[0].log_softmax(0)); |
| 41 | +check!(logsumexp, [r(12, &[3, 4], 0.0)], |x| x[0].logsumexp(1, false)); |
| 42 | +check!(var_std, [r(12, &[3, 4], 0.0)], |x| x[0].var_axis(1, true, false)?.add(&x[0].std_axis(1, false, false)?)); |
| 43 | +check!(prod, [pos(5, &[5])], |x| x[0].prod()); |
| 44 | +check!(cumprod, [pos(6, &[2, 3])], |x| x[0].cumprod(1)); |
| 45 | +check!(cumsum, [r(6, &[2, 3], 0.0)], |x| x[0].cumsum(0)); |
| 46 | +check!(cummax_values, [r(6, &[6], 0.0)], |x| Ok(x[0].cummax(0)?.0)); |
| 47 | +check!(max_axis, [r(12, &[3, 4], 0.0)], |x| x[0].max_axis(1, false)); |
| 48 | +check!(sort_values, [r(6, &[6], 0.0)], |x| Ok(x[0].sort(0, true)?.0)); |
| 49 | +check!(topk_values, [r(8, &[2, 4], 0.0)], |x| Ok(x[0].topk(2, 1, true, true)?.0)); |
| 50 | +check!(gather, [r(12, &[3, 4], 0.0)], |x| { |
| 51 | + let idx = Tensor::from_slice(&[0i64, 3, 1, 2, 2, 0], &[3, 2]).unwrap(); |
| 52 | + x[0].gather(1, &idx) |
| 53 | +}); |
| 54 | +check!(index_select, [r(12, &[3, 4], 0.0)], |x| { |
| 55 | + let idx = Tensor::from_slice(&[2i64, 0, 2], &[3]).unwrap(); |
| 56 | + x[0].index_select(0, &idx) |
| 57 | +}); |
| 58 | +check!(scatter_src, [r(6, &[2, 3], 0.0), r(4, &[2, 2], 5.0)], |x| { |
| 59 | + let idx = Tensor::from_slice(&[0i64, 2, 1, 0], &[2, 2]).unwrap(); |
| 60 | + x[0].scatter(1, &idx, &x[1]) |
| 61 | +}); |
| 62 | +check!(where_cond, [r(6, &[6], 0.0), r(6, &[6], 4.0)], |x| { |
| 63 | + let c = Tensor::from_slice(&[true, false, true, true, false, false], &[6]).unwrap(); |
| 64 | + Tensor::where_cond(&c, &x[0], &x[1]) |
| 65 | +}); |
| 66 | +check!(clamp, [r(6, &[6], 0.0).mul_scalar(2.0).unwrap()], |x| x[0].clamp(-1.1, 0.9)); |
| 67 | +check!(shape_ops, [r(12, &[3, 4], 0.0)], |x| { |
| 68 | + x[0].narrow(1, 1, 2)?.flip(&[0])?.roll(1, 1)?.pad(&[(1, 0), (0, 2)], 0.5)?.repeat(&[1, 2]) |
| 69 | +}); |
| 70 | +check!(expand_sum, [r(4, &[4, 1], 0.0)], |x| x[0].expand(&[4, 3])?.sum_axes(&[0], false)); |
| 71 | +check!(tril_triu_trace, [r(9, &[3, 3], 0.0)], |x| x[0].tril(0)?.add(&x[0].triu(1)?)?.mul_scalar(1.0)?.add_scalar(0.0)?.add(&x[0].trace()?.unsqueeze(0)?)); |
| 72 | +check!(einsum, [r(6, &[2, 3], 0.0), r(12, &[3, 4], 2.0)], |x| Tensor::einsum("ij,jk->ik", &[&x[0], &x[1]])); |
| 73 | +check!(matmul_batched, [r(12, &[2, 2, 3], 0.0), r(6, &[3, 2], 2.0)], |x| x[0].matmul(&x[1])); |
| 74 | +check!(conv1d, [r(16, &[1, 2, 8], 0.0), r(12, &[2, 2, 3], 3.0), r(2, &[2], 4.0)], |x| x[0].conv1d(&x[1], Some(&x[2]), 1, 1, 1)); |
| 75 | +check!(conv2d, [r(50, &[1, 2, 5, 5], 0.0), r(36, &[2, 2, 3, 3], 3.0), r(2, &[2], 4.0)], |x| x[0].conv2d(&x[1], Some(&x[2]), (2, 1), (1, 0), (1, 1))); |
| 76 | +check!(max_pool2d, [r(36, &[1, 1, 6, 6], 0.0)], |x| x[0].max_pool2d((2, 2), (2, 2), (0, 0))); |
| 77 | +check!(avg_pool2d, [r(36, &[1, 1, 6, 6], 0.0)], |x| x[0].avg_pool2d((3, 3), (1, 1), (1, 1))); |
| 78 | +check!(layer_norm_all, [r(12, &[3, 4], 0.0), r(4, &[4], 1.0), r(4, &[4], 2.0)], |x| layer_norm(&x[0], &[4], Some(&x[1]), Some(&x[2]), 1e-5)); |
| 79 | +check!(group_norm_all, [r(32, &[2, 4, 2, 2], 0.0), r(4, &[4], 1.0), r(4, &[4], 2.0)], |x| group_norm(&x[0], 2, Some(&x[1]), Some(&x[2]), 1e-5)); |
| 80 | +check!(attention, [r(12, &[1, 3, 4], 0.0), r(12, &[1, 3, 4], 1.0), r(12, &[1, 3, 4], 2.0)], |x| scaled_dot_product_attention(&x[0], &x[1], &x[2], None, false, None)); |
| 81 | +check!(attention_causal, [r(12, &[1, 3, 4], 0.0), r(12, &[1, 3, 4], 1.0), r(12, &[1, 3, 4], 2.0)], |x| scaled_dot_product_attention(&x[0], &x[1], &x[2], None, true, None)); |
0 commit comments