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
impl Tape
Sourcepub fn build(expr: &Expr) -> Self
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.
Sourcepub fn forward(&self, x: &[f64]) -> Vec<f64>
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].
pub fn eval(&self, x: &[f64]) -> f64
Sourcepub fn gradient_seed(&self, x: &[f64], seed: f64, grad: &mut [f64])
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.
Sourcepub fn variables(&self) -> Vec<usize>
pub fn variables(&self) -> Vec<usize>
Sorted distinct problem-variable indices that the tape depends on.
Sourcepub fn forward_into(&self, x: &[f64], vals: &mut [f64])
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.
Sourcepub fn hessian_directional(
&self,
vals: &[f64],
seed: &[f64],
weight: f64,
out: &mut [f64],
dot: &mut [f64],
adj: &mut [f64],
adj_dot: &mut [f64],
)
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.
Sourcepub fn hessian_accumulate(
&self,
x: &[f64],
weight: f64,
hess_map: &HashMap<(usize, usize), usize>,
values: &mut [f64],
)
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.
Sourcepub fn hessian_sparsity(&self) -> BTreeSet<(usize, usize)>
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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