gradcheck 0.1.0

Finite-difference gradient checking for Rust ML frameworks. Verifies an autodiff engine against an independent numerical oracle, with a negative control that must fail.
// gradcheck — finite-difference gradient checking for Rust ML frameworks.
// Copyright (c) 2026 Henos D <henosd19@gmail.com> (GitHub: @4ktLuffy)
// Repository: https://github.com/4ktLuffy/gradcheck
// SPDX-License-Identifier: MIT OR Apache-2.0

use burn_tensor::module::avg_pool1d;
use burn_tensor::{Tensor, TensorData};

fn device() -> burn_tensor::Device {
    burn_tensor::Device::default().autodiff()
}

/// Churn the allocator with a recognisable value, then let it go.
fn poison() {
    let big = vec![1234.5f32; 8192];
    let t = Tensor::<2>::from_data(TensorData::new(big, [64, 128]), &device());
    let _: Vec<f32> = (t.clone() * t).sum().to_data().to_vec().unwrap();
}

fn main() {
    for c in [1usize, 2, 3, 4] {
        poison();
        let d: Vec<f32> = (0..c * 6).map(|i| i as f32 * 0.1 + 0.5).collect();
        let x = Tensor::<3>::from_data(TensorData::new(d, [1, c, 6]), &device()).require_grad();
        let g = avg_pool1d(x.clone(), 3, 2, 1, true, false).sum().backward();
        let grad: Vec<f32> = x.grad(&g).unwrap().to_data().to_vec().unwrap();
        println!("c={c} grad={grad:?}");
    }
}