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
DifferentiableReverseRuleForwardRuleAutodiffErrorNodeIdSavePolicy
§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:
Differentiable— tangent space definition for any value typeReverseRule— per-operation reverse-mode rule (rrule/pullback)ForwardRule— per-operation forward-mode rule (frule/pushforward)- Error types (
AutodiffError,AdResult) NodeId,SavePolicy— graph node identifier and save strategy
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§
- Autodiff
Error - AD-specific error type.
- Save
Policy - Saved-tensor retention policy for reverse-mode rules.
Traits§
- Differentiable
- Trait defining the tangent space for a differentiable type.
- Forward
Rule - Forward-mode AD rule interface (frule).
- Reverse
Rule - Reverse-mode AD rule interface (rrule).
Type Aliases§
- AdResult
- Result alias for AD APIs.
- Pullback
Entry - Reverse-rule pullback output entry
(input_node, input_cotangent). - Pullback
With Tangents Entry - Reverse-rule pullback-with-tangents output entry.