Crate chainrules_core

Crate chainrules_core 

Source
Expand description

§chainrules-core

chainrules-core defines the engine-independent AD protocol used by chainrules-rs.

It is intentionally small and does not provide function rules. The crate exists to define the traits and error types that downstream AD engines and rule libraries build on.

§What It Provides

  • Differentiable
  • ReverseRule
  • ForwardRule
  • AutodiffError
  • NodeId
  • SavePolicy

§Example

use chainrules_core::NodeId;

let id = NodeId::new(7);
assert_eq!(id.index(), 7);

§Notes

This crate is protocol-only. Shared scalar frule and rrule helpers live in chainrules. Core AD trait definitions (like Julia’s ChainRulesCore.jl).

This crate defines the interface for automatic differentiation without providing an AD engine. It contains:

AD engines (Tape, TrackedValue, DualValue, pullback, hvp) live in separate crates, for example tidu.

Operation-specific AD rules (e.g., einsum rrule/frule) live in the crate that defines the operation.

§Examples

Implementing Differentiable for a custom type:

use chainrules_core::Differentiable;

#[derive(Clone)]
struct MyVec(Vec<f64>);

impl Differentiable for MyVec {
    type Tangent = MyVec;
    fn zero_tangent(&self) -> MyVec {
        MyVec(vec![0.0; self.0.len()])
    }
    fn accumulate_tangent(a: MyVec, b: &MyVec) -> MyVec {
        MyVec(a.0.iter().zip(&b.0).map(|(x, y)| x + y).collect())
    }
    fn num_elements(&self) -> usize {
        self.0.len()
    }
    fn seed_cotangent(&self) -> MyVec {
        MyVec(vec![1.0; self.0.len()])
    }
}

Structs§

NodeId
Stable identifier of an AD graph node.

Enums§

AutodiffError
AD-specific error type.
SavePolicy
Saved-tensor retention policy for reverse-mode rules.

Traits§

Differentiable
Trait defining the tangent space for a differentiable type.
ForwardRule
Forward-mode AD rule interface (frule).
ReverseRule
Reverse-mode AD rule interface (rrule).

Type Aliases§

AdResult
Result alias for AD APIs.
PullbackEntry
Reverse-rule pullback output entry (input_node, input_cotangent).
PullbackWithTangentsEntry
Reverse-rule pullback-with-tangents output entry.