Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Avhz RustQuant Accumulate

From Leeroopedia
Revision as of 14:30, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Avhz_RustQuant_Accumulate.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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:

  1. Allocate the adjoint array: A vector of zeros is created with one entry per vertex in the graph.
  2. 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.
  3. 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

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]);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment