axonml-autograd
Overview
axonml-autograd provides reverse-mode automatic differentiation (backpropagation) for computing gradients of tensor operations. This is the foundation for training neural networks using gradient descent optimization in the AxonML framework.
Features
-
Dynamic Computational Graph - Build computational graphs dynamically during the forward pass, enabling flexible model architectures.
-
Reverse-Mode Autodiff - Efficient backpropagation through the graph to compute gradients for all learnable parameters.
-
Gradient Accumulation - Automatic gradient accumulation for parameters used multiple times in the computation.
-
No-Grad Context - Temporarily disable gradient tracking for inference or evaluation with
no_grad()andNoGradGuard. -
Inference Mode - Optimized inference mode for production deployment with
inference_mode(). -
Gradient Checking - Built-in numerical gradient verification using finite differences for debugging.
Modules
| Module | Description |
|---|---|
variable |
Variable struct wrapping tensors with gradient tracking and differentiable operations |
graph |
Computational graph management with topological ordering for backpropagation |
backward |
Backward pass implementation traversing the graph in reverse order |
grad_fn |
GradientFunction trait and implementations for all differentiable operations |
no_grad |
Context managers for disabling gradient computation (NoGradGuard, InferenceModeGuard) |
functions |
Gradient implementations for arithmetic, activation, loss, and linear algebra operations |
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Example
use ;
use Tensor;
// Create variables with gradient tracking
let x = new;
// Forward pass builds computational graph
let y = x.pow; // y = x^2
let loss = y.sum; // scalar loss
// Backward pass computes gradients
loss.backward;
// Access gradients: dy/dx = 2x = [4.0, 6.0]
let grad = x.grad.unwrap;
println!;
Chained Operations
use Variable;
use Tensor;
let a = new;
let b = new;
// Build complex computation
let c = &a * &b; // c = a * b
let d = c.pow; // d = c^2 = (a*b)^2
let loss = d.sum;
loss.backward;
// dc/da = b = 3.0, dd/dc = 2c = 12.0, dL/da = 36.0
println!;
// dc/db = a = 2.0, dd/dc = 2c = 12.0, dL/db = 24.0
println!;
No-Grad Context
use ;
use Tensor;
let x = new;
// Using closure
let output = no_grad;
// Using guard
// Gradient tracking restored here
Loss Functions
use Variable;
use Tensor;
let predictions = new;
let targets = new;
// MSE Loss
let loss = predictions.mse_loss;
loss.backward;
// Binary Cross Entropy
let probs = new;
let labels = new;
let bce_loss = probs.binary_cross_entropy;
Matrix Operations with Gradients
use Variable;
use Tensor;
// Linear layer: y = xW + b
let x = new;
let w = new;
let b = new;
let y = x.matmul.add_var;
let loss = y.sum;
loss.backward;
// Gradients available for w and b
println!;
println!;
Gradient Checking
use ;
use Tensor;
let x = new;
// Compute numerical gradient
let numerical = numerical_gradient;
// Compare with analytical gradient
let y = x.pow.sum;
y.backward;
let analytical = x.grad.unwrap;
// Verify gradients match
assert!;
Tests
Run the test suite:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.