pub trait Function:
Send
+ Sync
+ Clone {
type Trace: Clone + Eq + Send + Sync + Trace;
type Storage: Default + Send;
type Workspace: Default + Send;
type TapeStorage: Default + Send;
type PointEval: TracingEvaluator<Data = f32, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync;
type IntervalEval: TracingEvaluator<Data = Interval, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync;
type FloatSliceEval: BulkEvaluator<Data = f32, TapeStorage = Self::TapeStorage> + Send + Sync;
type GradSliceEval: BulkEvaluator<Data = Grad, TapeStorage = Self::TapeStorage> + Send + Sync;
Show 13 methods
// Required methods
fn point_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::PointEval as TracingEvaluator>::Tape;
fn interval_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::IntervalEval as TracingEvaluator>::Tape;
fn float_slice_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::FloatSliceEval as BulkEvaluator>::Tape;
fn grad_slice_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::GradSliceEval as BulkEvaluator>::Tape;
fn simplify(
&self,
trace: &Self::Trace,
storage: Self::Storage,
workspace: &mut Self::Workspace,
) -> Result<Self, Error>
where Self: Sized;
fn recycle(self) -> Option<Self::Storage>;
fn size(&self) -> usize;
fn vars(&self) -> &VarMap;
fn can_simplify(&self) -> bool;
// Provided methods
fn new_point_eval() -> Self::PointEval { ... }
fn new_interval_eval() -> Self::IntervalEval { ... }
fn new_float_slice_eval() -> Self::FloatSliceEval { ... }
fn new_grad_slice_eval() -> Self::GradSliceEval { ... }
}
Expand description
A function represents something that can be evaluated
It is mostly agnostic to how that something is represented; we simply require that it can generate evaluators of various kinds.
Inputs to the function should be represented as Var
values; the vars()
function returns the mapping from
Var
to position in the input slice.
Functions are shared between threads, so they should be cheap to clone. In
most cases, they’re a thin wrapper around an Arc<..>
.
Required Associated Types§
Sourcetype Trace: Clone + Eq + Send + Sync + Trace
type Trace: Clone + Eq + Send + Sync + Trace
Associated type traces collected during tracing evaluation
This type must implement Eq
so that traces can be compared; calling
Function::simplify
with traces that compare equal should produce an
identical result and may be cached.
Sourcetype Workspace: Default + Send
type Workspace: Default + Send
Associated type for workspace used during function simplification
Sourcetype TapeStorage: Default + Send
type TapeStorage: Default + Send
Associated type for storage used by tapes
For simplicity, we require that every tape use the same type for storage. This could change in the future!
Sourcetype PointEval: TracingEvaluator<Data = f32, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync
type PointEval: TracingEvaluator<Data = f32, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync
Associated type for single-point tracing evaluation
Sourcetype IntervalEval: TracingEvaluator<Data = Interval, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync
type IntervalEval: TracingEvaluator<Data = Interval, Trace = Self::Trace, TapeStorage = Self::TapeStorage> + Send + Sync
Associated type for single interval tracing evaluation
Sourcetype FloatSliceEval: BulkEvaluator<Data = f32, TapeStorage = Self::TapeStorage> + Send + Sync
type FloatSliceEval: BulkEvaluator<Data = f32, TapeStorage = Self::TapeStorage> + Send + Sync
Associated type for evaluating many points in one call
Sourcetype GradSliceEval: BulkEvaluator<Data = Grad, TapeStorage = Self::TapeStorage> + Send + Sync
type GradSliceEval: BulkEvaluator<Data = Grad, TapeStorage = Self::TapeStorage> + Send + Sync
Associated type for evaluating many gradients in one call
Required Methods§
Sourcefn point_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::PointEval as TracingEvaluator>::Tape
fn point_tape( &self, storage: Self::TapeStorage, ) -> <Self::PointEval as TracingEvaluator>::Tape
Returns an evaluation tape for a point evaluator
Sourcefn interval_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::IntervalEval as TracingEvaluator>::Tape
fn interval_tape( &self, storage: Self::TapeStorage, ) -> <Self::IntervalEval as TracingEvaluator>::Tape
Returns an evaluation tape for an interval evaluator
Sourcefn float_slice_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::FloatSliceEval as BulkEvaluator>::Tape
fn float_slice_tape( &self, storage: Self::TapeStorage, ) -> <Self::FloatSliceEval as BulkEvaluator>::Tape
Returns an evaluation tape for a float slice evaluator
Sourcefn grad_slice_tape(
&self,
storage: Self::TapeStorage,
) -> <Self::GradSliceEval as BulkEvaluator>::Tape
fn grad_slice_tape( &self, storage: Self::TapeStorage, ) -> <Self::GradSliceEval as BulkEvaluator>::Tape
Returns an evaluation tape for a float slice evaluator
Sourcefn simplify(
&self,
trace: &Self::Trace,
storage: Self::Storage,
workspace: &mut Self::Workspace,
) -> Result<Self, Error>where
Self: Sized,
fn simplify(
&self,
trace: &Self::Trace,
storage: Self::Storage,
workspace: &mut Self::Workspace,
) -> Result<Self, Error>where
Self: Sized,
Computes a simplified tape using the given trace, and reusing storage
Sourcefn recycle(self) -> Option<Self::Storage>
fn recycle(self) -> Option<Self::Storage>
Attempt to reclaim storage from this function
This may fail, because functions are Clone
and are often implemented
using an Arc
around a heavier data structure.
Sourcefn size(&self) -> usize
fn size(&self) -> usize
Returns a size associated with this function
This is underspecified and only used for unit testing; for tape-based functions, it’s typically the length of the tape,
Sourcefn can_simplify(&self) -> bool
fn can_simplify(&self) -> bool
Checks to see whether this function can ever be simplified
Provided Methods§
Sourcefn new_point_eval() -> Self::PointEval
fn new_point_eval() -> Self::PointEval
Builds a new point evaluator
Sourcefn new_interval_eval() -> Self::IntervalEval
fn new_interval_eval() -> Self::IntervalEval
Builds a new interval evaluator
Sourcefn new_float_slice_eval() -> Self::FloatSliceEval
fn new_float_slice_eval() -> Self::FloatSliceEval
Builds a new float slice evaluator
Sourcefn new_grad_slice_eval() -> Self::GradSliceEval
fn new_grad_slice_eval() -> Self::GradSliceEval
Builds a new gradient slice evaluator
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.