Skip to content

Commit 0a33653

Browse files
voidstackloopclaude
andcommitted
Gradcheck sweeps: 15 linalg + 42 core-op backward passes vs finite differences (all pass)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 1e3afa1 commit 0a33653

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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));
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//! Finite-difference gradient checks over differentiable linear-algebra ops (a sweep for backward-pass bugs).
2+
3+
use oxideon_core::autograd_utils::gradcheck;
4+
use oxideon_core::{Result, Tensor};
5+
6+
fn t(v: &[f64], shape: &[usize]) -> Tensor {
7+
Tensor::from_slice(v, shape).unwrap()
8+
}
9+
10+
fn spd() -> Tensor {
11+
t(&[4.0, 1.0, 0.5, 1.0, 3.0, 0.2, 0.5, 0.2, 2.0], &[3, 3])
12+
}
13+
fn gmat() -> Tensor {
14+
t(&[2.0, 0.3, -0.5, 0.7, 1.5, 0.4, -0.2, 0.9, 1.8], &[3, 3])
15+
}
16+
fn tall() -> Tensor {
17+
t(&[1.0, 0.5, 0.2, 1.3, -0.4, 0.8, 0.6, -0.9], &[4, 2])
18+
}
19+
20+
fn sym(x: &Tensor) -> Result<Tensor> {
21+
x.add(&x.transpose(0, 1)?)?.mul_scalar(0.5)
22+
}
23+
24+
macro_rules! check {
25+
($name:ident, $inputs:expr, $f:expr) => {
26+
#[test]
27+
fn $name() {
28+
let r = gradcheck($f, &$inputs, 1e-6, 1e-5, 1e-4);
29+
assert!(r.is_ok(), "{}: {:?}", stringify!($name), r.err());
30+
}
31+
};
32+
}
33+
34+
check!(inv, [gmat()], |x| x[0].inv());
35+
check!(det, [gmat()], |x| x[0].det());
36+
check!(slogdet_logabs, [gmat()], |x| Ok(x[0].slogdet()?.1));
37+
check!(solve, [gmat(), t(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[3, 2])], |x| x[0].solve(&x[1]));
38+
check!(cholesky, [spd()], |x| sym(&x[0])?.cholesky());
39+
check!(qr_r, [tall()], |x| Ok(x[0].qr()?.1));
40+
check!(qr_q, [tall()], |x| Ok(x[0].qr()?.0));
41+
check!(eigh_values, [spd()], |x| Ok(sym(&x[0])?.eigh()?.0));
42+
check!(eigh_vectors_gauge_free, [spd()], |x| {
43+
// eigenvectors are defined up to sign: check a sign-invariant function, the projector V V^T scaled by values
44+
let (w, v) = sym(&x[0])?.eigh()?;
45+
v.mul(&w.unsqueeze(0)?)?.matmul(&v.transpose(0, 1)?)
46+
});
47+
check!(svd_values, [tall()], |x| Ok(x[0].svd()?.1));
48+
check!(svd_reconstruction_gauge_free, [tall()], |x| {
49+
let (u, s, vt) = x[0].svd()?;
50+
u.mul(&s.unsqueeze(0)?)?.matmul(&vt)
51+
});
52+
check!(pinv, [tall()], |x| x[0].pinv(1e-12));
53+
check!(lstsq, [tall(), t(&[1.0, 2.0, 3.0, 4.0], &[4, 1])], |x| x[0].lstsq(&x[1], 1e-12));
54+
check!(matrix_exp, [t(&[0.1, 0.3, -0.2, 0.05], &[2, 2])], |x| x[0].matrix_exp());
55+
check!(cholesky_solve, [spd(), t(&[1.0, 2.0, 3.0], &[3, 1])], |x| {
56+
let l = sym(&x[0])?.cholesky()?;
57+
x[1].cholesky_solve(&l, false)
58+
});

0 commit comments

Comments
 (0)