pub trait Differentiable {
type Tangent: Clone;
// Required methods
fn zero_tangent(&self) -> Self::Tangent;
fn accumulate_tangent(a: Self::Tangent, b: &Self::Tangent) -> Self::Tangent;
}Expand description
Trait defining the tangent space for a differentiable type.
This is the core abstraction of the AD framework, analogous to Julia’s ChainRulesCore.jl tangent type system. Any type that participates in automatic differentiation must implement this trait.
The tangent type represents infinitesimal perturbations of the value.
For most tensor types, Tangent = Self (e.g., the tangent of a matrix
is another matrix of the same shape).
Note: This trait intentionally does not require Clone on the primal
type. Clone is only required on Tangent (for gradient accumulation).
Large values (e.g., tensors) may be expensive to clone; the AD engine
avoids cloning primals by taking ownership where needed.
§Examples
use chainrules_core::Differentiable;
// Tensor<f64> implements Differentiable with Tangent = Tensor<f64>
// (defined in tenferro-tensor crate)
fn example<V: Differentiable>(x: &V) {
let zero = x.zero_tangent();
let _acc = V::accumulate_tangent(zero.clone(), &x.zero_tangent());
}Required Associated Types§
Required Methods§
Sourcefn zero_tangent(&self) -> Self::Tangent
fn zero_tangent(&self) -> Self::Tangent
Returns the zero tangent for this value (additive identity).
Sourcefn accumulate_tangent(a: Self::Tangent, b: &Self::Tangent) -> Self::Tangent
fn accumulate_tangent(a: Self::Tangent, b: &Self::Tangent) -> Self::Tangent
Accumulates (adds) two tangents: a + b.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.