Skip to main content

ConcreteEinsumPlan

Struct ConcreteEinsumPlan 

Source
pub struct ConcreteEinsumPlan { /* private fields */ }
Expand description

Prepared concrete einsum plan for repeated executions with fixed input dtype and shape metadata.

Preparing a plan parses and optimizes the contraction tree once. Execution validates the later inputs against the prepared dtype and shape contract, then runs the stored tree without re-planning.

§Examples

use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, Tensor};

let lhs = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let rhs = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare([&lhs, &rhs], "ij,jk->ik")?;

let mut backend = CpuBackend::new();
let out = backend
    .with_backend_session(|session| plan.execute([&lhs, &rhs], session))?;
assert_eq!(out.shape(), &[2, 4]);

Implementations§

Source§

impl ConcreteEinsumPlan

Source

pub fn prepare<'a, I>(inputs: I, subscripts: &str) -> Result<Self>
where I: AsRef<[&'a Tensor]>,

Prepare a plan from dtype-erased concrete tensor inputs and string notation.

§Errors

Returns Error::InvalidSubscripts for malformed notation, Error::Validation for rank, shape, or dtype contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_subscripts<'a, I>( inputs: I, subscripts: &EinsumSubscripts, ) -> Result<Self>
where I: AsRef<[&'a Tensor]>,

Prepare a plan from dtype-erased concrete tensor inputs and parsed integer-label subscripts.

§Errors

Returns Error::Validation for rank, shape, or dtype contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_notation<'a, I>( inputs: I, notation: &EinsumNotation, ) -> Result<Self>
where I: AsRef<[&'a Tensor]>,

Prepare a plan from rank-unresolved notation and concrete tensor inputs.

§Errors

Returns Error::InvalidSubscripts for malformed axis tokens, Error::Validation for rank, shape, or dtype violations, or Error::Planning when no contraction tree can be built.

Source

pub fn prepare_typed<'a, T, I>(inputs: I, subscripts: &str) -> Result<Self>
where T: TensorScalar, I: AsRef<[&'a TypedTensor<T>]>,

Prepare a plan from typed concrete tensor inputs and string notation.

§Errors

Returns Error::InvalidSubscripts for malformed notation, Error::Validation for rank or shape contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_typed_subscripts<'a, T, I>( inputs: I, subscripts: &EinsumSubscripts, ) -> Result<Self>
where T: TensorScalar, I: AsRef<[&'a TypedTensor<T>]>,

Prepare a plan from typed concrete tensor inputs and parsed integer-label subscripts.

§Errors

Returns Error::Validation for rank or shape contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_typed_notation<'a, T, I>( inputs: I, notation: &EinsumNotation, ) -> Result<Self>
where T: TensorScalar, I: AsRef<[&'a TypedTensor<T>]>,

Prepare a plan from rank-unresolved notation and typed concrete inputs.

§Errors

Returns Error::InvalidSubscripts for malformed axis tokens, Error::Validation for rank or shape violations, or Error::Planning when no contraction tree can be built.

Source

pub fn prepare_read<'a, I>(inputs: I, subscripts: &str) -> Result<Self>
where I: AsRef<[TensorRead<'a>]>,

Prepare a plan from read-only tensor inputs and string notation.

§Errors

Returns Error::InvalidSubscripts for malformed notation, Error::Validation for rank, shape, or dtype contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_read_subscripts<'a, I>( inputs: I, subscripts: &EinsumSubscripts, ) -> Result<Self>
where I: AsRef<[TensorRead<'a>]>,

Prepare a plan from read-only tensor inputs and parsed integer-label subscripts.

§Errors

Returns Error::Validation for rank, shape, or dtype contract violations, or Error::Planning when no valid contraction tree can be built.

Source

pub fn prepare_read_notation<'a, I>( inputs: I, notation: &EinsumNotation, ) -> Result<Self>
where I: AsRef<[TensorRead<'a>]>,

Prepare a plan from rank-unresolved notation and read-only inputs.

§Errors

Returns Error::InvalidSubscripts for malformed axis tokens, Error::Validation for rank, shape, or dtype violations, or Error::Planning when no contraction tree can be built.

Source

pub fn execute<'a, I>( &self, inputs: I, session: &mut dyn BackendSession, ) -> Result<Tensor>
where I: AsRef<[&'a Tensor]>,

Execute this plan on dtype-erased concrete tensor inputs inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, Tensor};

let lhs = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let rhs = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare([&lhs, &rhs], "ij,jk->ik")?;

let mut backend = CpuBackend::new();
let out = backend
    .with_backend_session(|session| plan.execute([&lhs, &rhs], session))?;
assert_eq!(out.shape(), &[2, 4]);
§Errors

Returns Error::Validation when inputs violate the prepared rank, shape, or input-count contract, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload when an input dtype differs from the prepared contract, or Error::Tensor for a typed backend failure.

Source

pub fn execute_typed<'a, T, I>( &self, inputs: I, session: &mut dyn BackendSession, ) -> Result<TypedTensor<T>>
where T: TensorScalar, I: AsRef<[&'a TypedTensor<T>]>,

Execute this plan on typed concrete tensor inputs inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, TypedTensor};

let lhs = TypedTensor::<f64>::from_vec_col_major(vec![2, 3], vec![1.0; 6]).unwrap();
let rhs = TypedTensor::<f64>::from_vec_col_major(vec![3, 4], vec![1.0; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare_typed([&lhs, &rhs], "ij,jk->ik")?;

let mut backend = CpuBackend::new();
let out = backend
    .with_backend_session(|session| plan.execute_typed([&lhs, &rhs], session))?;
assert_eq!(out.shape(), &[2, 4]);
§Errors

Returns Error::Validation when inputs violate the prepared rank, shape, or input-count contract, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload when the prepared dtype differs from T or the eager result dtype, or Error::Tensor for a typed backend failure.

Source

pub fn execute_read<'a, I>( &self, inputs: I, session: &mut dyn BackendSession, ) -> Result<Tensor>
where I: AsRef<[TensorRead<'a>]>,

Execute this plan on read-only tensor inputs inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, Tensor, TensorRead};

let lhs = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let rhs = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare_read(
    [TensorRead::from_tensor(&lhs), TensorRead::from_tensor(&rhs)],
    "ij,jk->ik",
)?;

let mut backend = CpuBackend::new();
let reads = [TensorRead::from_tensor(&lhs), TensorRead::from_tensor(&rhs)];
let out = backend
    .with_backend_session(|session| plan.execute_read(reads, session))?;
assert_eq!(out.shape(), &[2, 4]);
§Errors

Returns Error::Validation when inputs violate the prepared rank, shape, or input-count contract, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload when an input dtype differs from the prepared contract, or Error::Tensor for a typed backend failure.

Source

pub fn execute_into<'a, I>( &self, inputs: I, session: &mut dyn BackendSession, out: TensorWrite<'_>, ) -> Result<()>
where I: AsRef<[&'a Tensor]>,

Execute this plan on dtype-erased concrete tensor inputs into caller-provided output inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, Tensor, TensorWrite};

let lhs = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let rhs = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare([&lhs, &rhs], "ij,jk->ik")?;

let mut backend = CpuBackend::new();
let mut out = Tensor::from_vec_col_major(vec![2, 4], vec![0.0_f64; 8]).unwrap();
backend.with_backend_session(|session| {
    plan.execute_into(
        [&lhs, &rhs],
        session,
        TensorWrite::from_tensor(&mut out),
    )
})?;
assert_eq!(out.as_slice::<f64>()?, vec![3.0_f64; 8].as_slice());
§Errors

Returns Error::Validation for input or output rank, shape, or input-count contract violations, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload for dtype mismatches, or Error::Tensor for a typed backend failure.

Source

pub fn execute_typed_into<'a, 'out, T, I, O>( &self, inputs: I, session: &mut dyn BackendSession, out: O, ) -> Result<()>
where T: TensorScalar, I: AsRef<[&'a TypedTensor<T>]>, O: Into<TypedTensorWrite<'out, T>>,

Execute this plan on typed concrete tensor inputs into caller-provided output inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, TypedTensor};

let lhs = TypedTensor::<f64>::from_vec_col_major(vec![2, 3], vec![1.0; 6]).unwrap();
let rhs = TypedTensor::<f64>::from_vec_col_major(vec![3, 4], vec![1.0; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare_typed([&lhs, &rhs], "ij,jk->ik")?;

let mut backend = CpuBackend::new();
let mut out = TypedTensor::<f64>::from_vec_col_major(vec![2, 4], vec![0.0; 8]).unwrap();
backend.with_backend_session(|session| {
    plan.execute_typed_into([&lhs, &rhs], session, &mut out)
})?;
assert_eq!(out.as_slice()?, vec![3.0_f64; 8].as_slice());
§Errors

Returns Error::Validation for input or output rank, shape, or input-count contract violations, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload when the prepared dtype differs from T or the output dtype, or Error::Tensor for a typed backend failure.

Source

pub fn execute_read_into<'a, I>( &self, inputs: I, session: &mut dyn BackendSession, out: TensorWrite<'_>, ) -> Result<()>
where I: AsRef<[TensorRead<'a>]>,

Execute this plan on read-only tensor inputs into caller-provided output inside a borrowed backend session.

Validation and the contraction itself run in the caller’s session; this method never enters a new backend session.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{BackendSessionHost, Tensor, TensorRead, TensorWrite};

let lhs = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let rhs = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let plan = ConcreteEinsumPlan::prepare_read(
    [TensorRead::from_tensor(&lhs), TensorRead::from_tensor(&rhs)],
    "ij,jk->ik",
)?;

let mut backend = CpuBackend::new();
let mut out = Tensor::from_vec_col_major(vec![2, 4], vec![0.0_f64; 8]).unwrap();
let reads = [TensorRead::from_tensor(&lhs), TensorRead::from_tensor(&rhs)];
backend.with_backend_session(|session| {
    plan.execute_read_into(reads, session, TensorWrite::from_tensor(&mut out))
})?;
assert_eq!(out.as_slice::<f64>()?, vec![3.0_f64; 8].as_slice());
§Errors

Returns Error::Validation for input or output rank, shape, or input-count contract violations, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload for dtype mismatches, or Error::Tensor for a typed backend failure.

Source

pub fn execute_read_into_accum<'a, I>( &self, inputs: I, session: &mut dyn BackendSession, accumulation: DotGeneralAccumulation, out: TensorWrite<'_>, ) -> Result<()>
where I: AsRef<[TensorRead<'a>]>,

Execute this plan on read-only inputs with scaled output accumulation inside a borrowed backend session.

accumulation follows the dot-general contract: out = alpha * einsum(inputs) + beta * out.

§Examples
use tenferro_cpu::CpuBackend;
use tenferro_einsum::ConcreteEinsumPlan;
use tenferro_tensor::{
    BackendSessionHost, DotGeneralAccumulation, DType, Tensor, TensorRead, TensorWrite,
};

let lhs = Tensor::from_vec_col_major(vec![1], vec![2.0_f64])?;
let rhs = Tensor::from_vec_col_major(vec![1], vec![3.0_f64])?;
let mut out = Tensor::from_vec_col_major(vec![], vec![1.0_f64])?;
let plan = ConcreteEinsumPlan::prepare([&lhs, &rhs], "i,i->")?;
let mut backend = CpuBackend::new();
backend.with_backend_session(|session| {
    plan.execute_read_into_accum(
        [TensorRead::from_tensor(&lhs), TensorRead::from_tensor(&rhs)],
        session,
        DotGeneralAccumulation::add_to(DType::F64)?,
        TensorWrite::from_tensor(&mut out),
    )
})?;
assert_eq!(out.as_slice::<f64>()?, &[7.0]);
§Errors

Returns Error::Validation for input or output rank, shape, or input-count contract violations, Error::Tensor with a tenferro_tensor::Error::Validation DTypeMismatch payload for dtype mismatches, Error::Numerical for an invalid accumulation, or Error::Tensor for a typed backend failure.

Trait Implementations§

Source§

impl Debug for ConcreteEinsumPlan

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> MaybeSend for T
where T: Send,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,

§

impl<T> MaybeSync for T
where T: Sync,

§

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,

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