Differentiable

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;
}
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§

type Tangent: Clone

The tangent type for this value.

For most types, this is Self (e.g., tangent of a tensor is a tensor).

Required Methods§

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

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.

Implementations on Foreign Types§

§

impl Differentiable for f32

§

type Tangent = f32

§

fn zero_tangent(&self) -> f32

§

fn accumulate_tangent(a: f32, b: &f32) -> f32

§

impl Differentiable for f64

§

type Tangent = f64

§

fn zero_tangent(&self) -> f64

§

fn accumulate_tangent(a: f64, b: &f64) -> f64

Implementors§