Trait Differentiable
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;
fn num_elements(&self) -> usize;
fn seed_cotangent(&self) -> 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§
fn zero_tangent(&self) -> Self::Tangent
fn zero_tangent(&self) -> Self::Tangent
Returns the zero tangent for this value (additive identity).
fn 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.
fn num_elements(&self) -> usize
fn num_elements(&self) -> usize
Returns the number of scalar elements in this value.
For scalar types (f64, f32), this is always 1. For tensor types, this is the total number of elements.
fn seed_cotangent(&self) -> Self::Tangent
fn seed_cotangent(&self) -> Self::Tangent
Returns the seed cotangent for reverse-mode pullback.
For a scalar loss, this returns the “one” tangent (1.0 for scalars,
ones-like for single-element tensors). Used internally by
Tape::pullback to initialize the
backward pass.
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.