Skip to main content

Tape

Struct Tape 

Source
pub struct Tape {
    pub ops: Vec<TapeOp>,
}
Expand description

A flattened expression tape. The result of evaluation is the value at slot ops.len() - 1 (i.e. the last op).

Fields§

§ops: Vec<TapeOp>

Implementations§

Source§

impl Tape

Source

pub fn build(expr: &Expr) -> Self

Build a tape from an Expr tree. CSE bodies (Expr::Cse(rc)) are cached by Rc pointer identity so each body is emitted once even when referenced many times.

Source

pub fn forward(&self, x: &[f64]) -> Vec<f64>

Forward sweep: returns vals[i] = value of tape slot i. The scalar tape result is vals[ops.len() - 1].

Source

pub fn eval(&self, x: &[f64]) -> f64

Source

pub fn gradient_seed(&self, x: &[f64], seed: f64, grad: &mut [f64])

Reverse-mode AD: accumulate seed * df/dx_i into grad[i] for every problem variable i referenced by the tape. grad is not zeroed by this routine — the caller can chain multiple gradient accumulations into the same buffer.

Source

pub fn variables(&self) -> Vec<usize>

Sorted distinct problem-variable indices that the tape depends on.

Source

pub fn forward_into(&self, x: &[f64], vals: &mut [f64])

Forward sweep into a caller-supplied buffer. Avoids the per-call allocation of forward() so hot paths can reuse one scratch arena across many tapes.

Source

pub fn hessian_directional( &self, vals: &[f64], seed: &[f64], weight: f64, out: &mut [f64], dot: &mut [f64], adj: &mut [f64], adj_dot: &mut [f64], )

Directional Hessian-vector product: emits weight * (∇²f · seed)[k] into out[k] for every problem variable k the tape references. Caller supplies the forward-pass result vals (use forward_into) plus three scratch buffers (dot, adj, adj_dot), each at least self.ops.len() long. out must be at least one past the largest variable index in the tape; the routine reads seed[k] for each Var(k) and writes out[k] += weight * (Hess · seed)[k].

This is one forward-over-reverse AD pass — O(n_ops) work — regardless of how many variables the tape depends on, which is what makes Hessian coloring efficient: a single directional pass recovers a whole color group of columns.

Source

pub fn hessian_accumulate( &self, x: &[f64], weight: f64, hess_map: &HashMap<(usize, usize), usize>, values: &mut [f64], )

Forward-over-reverse Hessian: for each variable j the tape depends on, accumulate weight * (d²f / dx_i dx_j) into values[hess_map[(i, j)]] for every (i, j) lower-triangle pair in the map. The same routine is used for the objective (with weight = obj_factor) and each active constraint (with weight = lambda[k]); contributions sum into the shared map.

Source

pub fn hessian_sparsity(&self) -> BTreeSet<(usize, usize)>

Structural Hessian sparsity (lower triangle, row >= col). Propagates per-slot variable-dependence sets forward; each nonlinear op emits the cross/self products of its operand sets. Linear ops contribute no second-derivative pairs.

Trait Implementations§

Source§

impl Clone for Tape

Source§

fn clone(&self) -> Tape

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Tape

§

impl RefUnwindSafe for Tape

§

impl Send for Tape

§

impl Sync for Tape

§

impl Unpin for Tape

§

impl UnsafeUnpin for Tape

§

impl UnwindSafe for Tape

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.