bevy_autodiff
Higher-order automatic differentiation using Bevy ECS as the computational graph backend.
bevy_autodiff implements Taylor-mode AD, where variables are ECS entities, operations are components, and Taylor coefficients are propagated through the graph to compute derivatives of any order.
Key Features
- ECS as computation graph — entities are variables, components store operations and Taylor coefficients
- Taylor-mode AD — O(n²) complexity for the n-th derivative, vs O(exp(n)) for naive nesting
- Univariate decomposition — directional derivatives avoid multivariate Bell polynomial complexity
- Forward and reverse mode — forward mode for higher-order derivatives, reverse mode for efficient gradients
- Incremental order growth — compute higher derivatives on demand without recomputation
- Lazy evaluation — derivatives computed and cached only when requested
Quick Start
Add to your Cargo.toml:
[]
= { = "https://github.com/VisVivaSpace/bevy_autodiff.git" }
use AutoDiff;
let mut ad = new;
// Create input variable
let x = ad.var;
// Build computation graph: f(x) = x² + 3x + 1
let x_squared = ad.square;
let three = ad.constant;
let three_x = ad.mul;
let one = ad.constant;
let sum = ad.add;
let f = ad.add;
// Evaluate
assert_eq!; // f(2) = 4 + 6 + 1
// Derivatives of any order
assert_eq!; // f'(2) = 2·2 + 3
assert_eq!; // f''(x) = 2
assert_eq!; // f'''(x) = 0
Gradients and Hessians
use AutoDiff;
let mut ad = new;
let x = ad.var;
let y = ad.var;
// f(x, y) = x² + xy + y²
let x2 = ad.square;
let xy = ad.mul;
let y2 = ad.square;
let sum = ad.add;
let f = ad.add;
// Forward-mode gradient
let grad = ad.gradient; // [∂f/∂x, ∂f/∂y] = [4.0, 5.0]
// Reverse-mode gradient (more efficient for many inputs)
let grad_rev = ad.gradient_reverse;
// Hessian matrix (second-order partials)
let hessian = ad.hessian;
// [[∂²f/∂x², ∂²f/∂x∂y],
// [∂²f/∂y∂x, ∂²f/∂y²]] = [[2, 1], [1, 2]]
Supported Operations
| Category | Operations |
|---|---|
| Arithmetic | add, sub, mul, div, neg, square |
| Powers | sqrt, pow, powi, powf |
| Trigonometric | sin, cos, tan, asin, acos, atan |
| Hyperbolic | sinh, cosh, tanh, asinh, acosh, atanh |
| Exponential | exp, ln |
Expression Macros
The expr! macro provides natural mathematical syntax:
use ;
let mut ad = new;
let x = ad.var;
let y = ad.var;
let f = expr!;
assert_eq!; // 4 + 6
With the proc-macros feature, the #[autodiff] attribute transforms regular functions:
[]
= { = "https://github.com/VisVivaSpace/bevy_autodiff.git", = ["proc-macros"] }
use ;
let mut ad = new;
let x = ad.var;
let y = ad.var;
let f = rosenbrock; // Adds `ad` parameter automatically
How It Works
Taylor-Mode AD
Instead of symbolic differentiation or dual numbers, bevy_autodiff propagates truncated Taylor polynomials through the computation graph:
- Parameterize along a direction:
p(t) = x + t·d - Propagate Taylor series of
f(p(t))through each operation using recurrence relations - Extract derivatives:
f⁽ⁿ⁾(a) = n! · coefficient[n]
Each operation (exp, sin, mul, etc.) has an O(n²) recurrence relation for computing the n-th Taylor coefficient from lower-order coefficients. Mixed partial derivatives are recovered via the polarization identity from directional derivatives.
ECS Architecture
The Bevy ECS world stores the computation graph:
- Entities represent variables (inputs, constants, intermediate results)
- Components store values (
Value), operations (UnaryOp,BinaryOp), connectivity (UnaryInput,BinaryInputs), and cached Taylor coefficients (TaylorData) - Dependency tracking via bitmasks identifies which inputs affect each variable
Examples
Run the included examples:
Testing
The test suite validates correctness through multiple approaches:
Oracle validation against the independent autodiff crate ensures first-order derivative correctness. Higher-order derivatives are validated through mathematical identities (Pythagorean, exp/ln inverse, power laws) and forward/reverse mode agreement.
References
- Griewank, A. & Walther, A. (2008). Evaluating Derivatives: Principles and Techniques of Algorithmic Differentiation, 2nd ed. SIAM. Tables 13.1–13.2 for Taylor coefficient recurrences.
License
MIT