Skip to main content

PreparedEinsum

Struct PreparedEinsum 

Source
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>

Source

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.

Source

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.

Source

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§

Source§

impl Debug for PreparedEinsum<'_>

Source§

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

Formats the value using the given formatter. Read more

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> 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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V