pub struct PreparedEinsum<'operands> { /* private fields */ }Expand description
One prepared N-ary einsum, reusable across operations of a session.
Preparing validates the labels and plans the contraction once; every
PreparedEinsum::execute and PreparedEinsum::execute_into reuses that plan, so
a stage keeps one plan across as many evaluations as it needs instead of preparing
per call. The plan borrows its operands, so it cannot outlive them, and it holds no
execution state: the session stays the caller’s.
§Examples
use tensor4all_tensorbackend::{explicit::PreparedEinsum, CpuExecutionContext};
use tenferro::Tensor;
use tensor4all_tensorbackend::CpuBackend;
use tenferro_tensor::{TensorRead, TensorWrite};
let context = CpuExecutionContext::from_backend(CpuBackend::with_threads(1)?);
let a = Tensor::from_vec_col_major(vec![2, 2], vec![1.0_f64, 2.0, 3.0, 4.0])?;
let b = Tensor::from_vec_col_major(vec![2, 1], vec![5.0_f64, 6.0])?;
let reads = [TensorRead::from_tensor(&a), TensorRead::from_tensor(&b)];
let plan = PreparedEinsum::prepare(&reads, &[&[0, 1], &[1, 2]], &[0, 2])?;
// Reused across a stage, including into a caller-provided destination.
let mut out = Tensor::from_vec_col_major(vec![2, 1], vec![0.0_f64, 0.0])?;
context.with_concrete_session(|session| -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let product = plan.execute(session)?;
assert_eq!(product.as_slice::<f64>()?, &[23.0, 34.0]);
plan.execute_into(session, TensorWrite::Tensor(&mut out))?;
Ok(())
})??;
assert_eq!(out.as_slice::<f64>()?, &[23.0, 34.0]);Implementations§
Source§impl<'operands> PreparedEinsum<'operands>
impl<'operands> PreparedEinsum<'operands>
Sourcepub fn prepare(
operands: &[TensorRead<'operands>],
input_ids: &[&[usize]],
output_ids: &[usize],
) -> Result<Self>
pub fn prepare( operands: &[TensorRead<'operands>], input_ids: &[&[usize]], output_ids: &[usize], ) -> Result<Self>
Plan an N-ary einsum over borrowed operands.
§Errors
Returns [tenferro_einsum::Error::InvalidSubscripts] for malformed labels, a
label list whose length does not match the operand’s rank, or a label-list count
that does not match the operand count, and [tenferro_einsum::Error] from the
contraction planner when the expression cannot be planned.
Sourcepub fn execute(&self, session: &mut Session<'_>) -> Result<NativeTensor>
pub fn execute(&self, session: &mut Session<'_>) -> Result<NativeTensor>
Evaluate the prepared plan on session, allocating the result.
§Errors
Returns [tenferro_einsum::Error::Tensor] when the session rejects the operands
or the contraction itself fails, and
[tenferro_einsum::Error::Validation] when the operands no longer match the
planned expression.
Sourcepub fn execute_into(
&self,
session: &mut Session<'_>,
out: TensorWrite<'_>,
) -> Result<()>
pub fn execute_into( &self, session: &mut Session<'_>, out: TensorWrite<'_>, ) -> Result<()>
Evaluate the prepared plan into a caller-provided destination.
The destination is fully written and is never zero-filled first. Operands and destination must already share one dtype; this route does not convert into a caller-owned output.
§Errors
Returns [tenferro_einsum::Error::Tensor] when the session rejects the operands,
the destination or the contraction, and
[tenferro_einsum::Error::Validation] when the operands no longer match the
planned expression.
Trait Implementations§
Auto Trait Implementations§
impl<'operands> !Freeze for PreparedEinsum<'operands>
impl<'operands> !RefUnwindSafe for PreparedEinsum<'operands>
impl<'operands> !UnwindSafe for PreparedEinsum<'operands>
impl<'operands> Send for PreparedEinsum<'operands>
impl<'operands> Sync for PreparedEinsum<'operands>
impl<'operands> Unpin for PreparedEinsum<'operands>
impl<'operands> UnsafeUnpin for PreparedEinsum<'operands>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T, U> Imply<T> for U
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