autodiff: finite-diff gradient-check harness

New xtrain-autodiff crate with a reusable central finite-difference
gradient check: grad_check(x, shape, f, analytic_grad, cfg) compares an
analytic gradient against (f(x+ε)-f(x-ε))/2ε per element with a relative
tolerance. Host-only (no CUDA): the loss closure owns any GPU work, so
T4's per-op backward checks can reuse it directly. Includes host unit
tests (sum(x²) grad 2x passes; a wrong grad is rejected).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-15 15:26:42 +08:00
parent fbd07a578c
commit 9ca98efd98
5 changed files with 157 additions and 0 deletions

8
Cargo.lock generated
View File

@@ -88,6 +88,13 @@ version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
[[package]]
name = "xtrain-autodiff"
version = "0.1.0"
dependencies = [
"xtrain-tensor",
]
[[package]]
name = "xtrain-cuda"
version = "0.1.0"
@@ -101,6 +108,7 @@ version = "0.1.0"
dependencies = [
"half",
"smallvec",
"xtrain-autodiff",
"xtrain-cuda",
]