Implementation:Avhz RustQuant Accumulate
| Knowledge Sources | |
|---|---|
| Domains | Automatic_Differentiation, Mathematics |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
The Accumulate trait defines the reverse-mode gradient accumulation pass that traverses the computational graph backwards to compute all partial derivatives.
Description
The Accumulate trait is a generic trait parameterized by an output type OUT, with a single method accumulate(&self) -> OUT. The primary implementation is for Variable<'_>, which returns a Vec<f64> containing the adjoint (partial derivative) for every vertex in the computation graph.
The algorithm proceeds in three steps:
- Allocate the adjoint array: A vector of zeros is created with one entry per vertex in the graph.
- Set the seed: The adjoint of the output variable is set to
1.0(since dy/dy = 1), implementing the starting condition for reverse-mode differentiation. - Reverse traversal: The graph vertices are iterated in reverse topological order (from output back to inputs). For each vertex, the adjoint value is propagated to its parents by multiplying with the stored partial derivatives and accumulating via addition. This implements the generalized chain rule.
After accumulation, individual derivatives can be retrieved from the returned Vec<f64> using the Gradient trait's .wrt() method, which indexes into the adjoint vector by a variable's graph index.
The module documentation notes planned future support for accumulation over matrix types (DMatrix<Variable>, DVector<Variable>, Array<Variable, Ix2>), though these are not yet implemented due to lifetime constraints.
Usage
Call .accumulate() on any Variable that represents the output of a differentiable computation. The returned adjoint vector contains partial derivatives with respect to every intermediate and input variable in the graph. Use the Gradient::wrt() method to extract specific derivatives.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_autodiff/src/accumulate.rs
- Lines: 1-52
Signature
/// Trait to reverse accumulate the gradient for different types.
pub trait Accumulate<OUT> {
/// Function to reverse accumulate the gradient.
fn accumulate(&self) -> OUT;
}
impl Accumulate<Vec<f64>> for Variable<'_> {
fn accumulate(&self) -> Vec<f64> {
let mut adjoints = vec![0.0; self.graph.len()];
adjoints[self.index] = 1.0; // SEED: dy/dy = 1
for (index, vertex) in self.graph.vertices.borrow().iter().enumerate().rev() {
let deriv = adjoints[index];
adjoints[vertex.parents[0]] += vertex.partials[0] * deriv;
adjoints[vertex.parents[1]] += vertex.partials[1] * deriv;
}
adjoints
}
}
Import
use RustQuant::autodiff::{Accumulate, Gradient};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | &Variable<'_> |
Yes | A reference to the output variable of a differentiable computation. The variable must be part of a valid Graph with at least one vertex.
|
Outputs
| Name | Type | Description |
|---|---|---|
| adjoints | Vec<f64> |
A vector of length equal to the number of vertices in the graph. Each element at index i contains the partial derivative of the output variable with respect to the variable at graph index i. Use the Gradient::wrt() method to look up specific derivatives by Variable reference.
|
Usage Examples
use RustQuant_autodiff::*;
let g = Graph::new();
let x = g.var(1.0);
let y = g.var(2.0);
// f(x, y) = x * y + sin(x)
let z = x * y + x.sin();
// Reverse accumulate to get all partial derivatives
let grad = z.accumulate();
// Retrieve individual gradients
// df/dx = y + cos(x) = 2.0 + cos(1.0)
let dz_dx = grad.wrt(&x);
// df/dy = x = 1.0
let dz_dy = grad.wrt(&y);
assert_eq!(dz_dy, 1.0);
// Retrieve gradients for multiple variables at once
let all_grads = grad.wrt(&[x, y]);