use crate::{
Error,
context::{Context, Node},
types::{Grad, Interval},
var::VarMap,
};
#[cfg(any(test, feature = "eval-tests"))]
#[allow(missing_docs)]
pub mod test;
mod bulk;
mod tracing;
pub use bulk::{BulkEvaluator, BulkOutput};
pub use tracing::TracingEvaluator;
pub trait Tape: Send + Sync + Clone {
type Storage: Default;
fn recycle(self) -> Option<Self::Storage>;
fn vars(&self) -> &VarMap;
fn output_count(&self) -> usize;
}
pub trait Trace {
fn copy_from(&mut self, other: &Self);
}
impl<T: Copy + Clone + Default> Trace for Vec<T> {
fn copy_from(&mut self, other: &Self) {
self.resize(other.len(), T::default());
self.copy_from_slice(other);
}
}
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;
fn new_point_eval() -> Self::PointEval {
Self::PointEval::new()
}
type IntervalEval: TracingEvaluator<
Data = Interval,
Trace = Self::Trace,
TapeStorage = Self::TapeStorage,
> + Send
+ Sync;
fn new_interval_eval() -> Self::IntervalEval {
Self::IntervalEval::new()
}
type FloatSliceEval: BulkEvaluator<Data = f32, TapeStorage = Self::TapeStorage>
+ Send
+ Sync;
fn new_float_slice_eval() -> Self::FloatSliceEval {
Self::FloatSliceEval::new()
}
type GradSliceEval: BulkEvaluator<Data = Grad, TapeStorage = Self::TapeStorage>
+ Send
+ Sync;
fn new_grad_slice_eval() -> Self::GradSliceEval {
Self::GradSliceEval::new()
}
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;
}
pub trait MathFunction: Function {
fn new(ctx: &Context, nodes: &[Node]) -> Result<Self, Error>
where
Self: Sized;
}