Skip to main content

ReverseModeEngine

Struct ReverseModeEngine 

Source
pub struct ReverseModeEngine<T: ReverseScalar> { /* private fields */ }
Expand description

Reverse-mode AD engine (gradient tape)

Implementations§

Source§

impl<T: ReverseScalar> ReverseModeEngine<T>

Source

pub fn new() -> Self

Create a new reverse-mode AD engine

Source

pub fn set_recording(&mut self, recording: bool)

Enable/disable gradient recording.

While recording is disabled, subsequent operations are still evaluated and appended to the tape, but they are recorded as constants: they keep the tape’s index invariant intact while blocking gradient flow.

Source

pub fn is_recording(&self) -> bool

Check if recording is enabled

Source

pub fn enable_higher_order(&mut self, enabled: bool)

Enable higher-order gradients

Source

pub fn higher_order_enabled(&self) -> bool

Whether higher-order gradient tracking is enabled

Source

pub fn clear_tape(&mut self)

Clear the computation tape

Source

pub fn tape_len(&self) -> usize

Number of nodes currently on the tape

Source

pub fn create_variable( &mut self, name: &str, value: Array1<T>, requiresgrad: bool, ) -> usize

Create a variable with gradient tracking (1-D, shape [value.len()])

Source

pub fn create_variable_with_shape( &mut self, name: &str, value: Array1<T>, shape: &[usize], requiresgrad: bool, ) -> Result<usize>

Create a variable with an explicit logical shape.

value is the row-major flattening of a tensor whose logical shape is shape; the product of shape must equal value.len().

Source

pub fn create_constant(&mut self, value: Array1<T>) -> usize

Create a constant (no gradient tracking)

Source

pub fn set_variable_value( &mut self, varid: usize, value: Array1<T>, ) -> Result<()>

Replace the value of an existing leaf variable.

The tape is marked dirty; ReverseModeEngine::forward (called automatically by ReverseModeEngine::backward) re-evaluates every dependent node.

Source

pub fn set_variable_value_by_name( &mut self, name: &str, value: Array1<T>, ) -> Result<()>

Replace the value of an existing leaf variable, by name

Source

pub fn add(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Addition operation

Source

pub fn subtract(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Subtraction operation

Source

pub fn multiply(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Element-wise multiplication operation

Source

pub fn divide(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Element-wise division operation

Source

pub fn power(&mut self, base: usize, exponent: T) -> Result<usize>

Power operation

Source

pub fn exp(&mut self, input: usize) -> Result<usize>

Exponential function

Source

pub fn log(&mut self, input: usize) -> Result<usize>

Natural logarithm

Source

pub fn sin(&mut self, input: usize) -> Result<usize>

Sine function

Source

pub fn cos(&mut self, input: usize) -> Result<usize>

Cosine function

Source

pub fn tanh(&mut self, input: usize) -> Result<usize>

Hyperbolic tangent

Source

pub fn sigmoid(&mut self, input: usize) -> Result<usize>

Sigmoid function

Source

pub fn relu(&mut self, input: usize) -> Result<usize>

ReLU function

Source

pub fn leaky_relu(&mut self, input: usize, alpha: f64) -> Result<usize>

Leaky ReLU function

Source

pub fn matmul(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Matrix multiplication.

lhs must have logical shape [m, k]; rhs must have shape [k, n] or [k] (interpreted as a [k, 1] column). The result has shape [m, n] (or [m] when rhs was 1-D).

Source

pub fn dot(&mut self, lhs: usize, rhs: usize) -> Result<usize>

Dot product of two vectors of equal length; produces a scalar node.

Source

pub fn sum(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>

Sum reduction over all elements

Source

pub fn mean(&mut self, input: usize, _axis: Option<usize>) -> Result<usize>

Mean reduction over all elements

Source

pub fn norm(&mut self, input: usize) -> Result<usize>

L2 norm over all elements

Source

pub fn reshape(&mut self, input: usize, newshape: &[usize]) -> Result<usize>

Reshape operation (element count must be preserved)

Source

pub fn forward(&mut self) -> Result<()>

Re-evaluate every non-leaf node of the tape from the current leaf values.

Nodes are visited in tape order, which is a valid topological order because every node’s index exceeds all of its inputs’ indices.

Source

pub fn value(&self, varid: usize) -> Result<&Array1<T>>

Forward value of a node

Source

pub fn value_by_name(&self, name: &str) -> Result<&Array1<T>>

Forward value of a node, by variable name

Source

pub fn shape(&self, varid: usize) -> Result<&[usize]>

Logical shape of a node

Source

pub fn scalar_value(&self, varid: usize) -> Result<T>

Scalar value of a node that holds exactly one element

Source

pub fn backward( &mut self, outputid: usize, gradient: Option<Array1<T>>, ) -> Result<()>

Backward pass: compute gradients of outputid w.r.t. every leaf.

If gradient is None, the output is seeded with ones matching the output’s length.

Source

pub fn get_gradient(&self, varid: usize) -> Option<&Array1<T>>

Get gradient for a variable

Source

pub fn get_gradient_by_name(&self, name: &str) -> Option<&Array1<T>>

Get gradient by variable name

Source

pub fn zero_gradients(&mut self)

Zero all gradients

Source

pub fn get_all_gradients(&self) -> HashMap<String, Array1<T>>

Get all named variable gradients

Source

pub fn get_tape_stats(&self) -> ReverseModeStats

Get tape statistics

Trait Implementations§

Source§

impl<T: ReverseScalar> Default for ReverseModeEngine<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ReverseModeEngine<T>
where Vec<ReverseOperation<T>>: Freeze, Vec<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Freeze, Vec<Option<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>>: Freeze, HashMap<usize, ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Freeze,

§

impl<T> RefUnwindSafe for ReverseModeEngine<T>

§

impl<T> Send for ReverseModeEngine<T>
where Vec<ReverseOperation<T>>: Send, Vec<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Send, Vec<Option<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>>: Send, HashMap<usize, ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Send,

§

impl<T> Sync for ReverseModeEngine<T>
where Vec<ReverseOperation<T>>: Sync, Vec<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Sync, Vec<Option<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>>: Sync, HashMap<usize, ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Sync,

§

impl<T> Unpin for ReverseModeEngine<T>
where Vec<ReverseOperation<T>>: Unpin, Vec<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Unpin, Vec<Option<ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>>: Unpin, HashMap<usize, ArrayBase<OwnedRepr<T>, Dim<[usize; 1]>>>: Unpin,

§

impl<T> UnsafeUnpin for ReverseModeEngine<T>

§

impl<T> UnwindSafe for ReverseModeEngine<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V