ContractionTree

Struct ContractionTree 

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

Contraction tree determining pairwise contraction order for N-ary einsum.

When contracting more than two tensors, the order in which pairwise contractions are performed significantly affects performance. ContractionTree encodes this order as a binary tree.

§Optimization

Use ContractionTree::optimize for automatic cost-based optimization (e.g., greedy algorithm based on tensor sizes), or ContractionTree::from_pairs for manual specification.

Implementations§

Source§

impl ContractionTree

Source

pub fn optimize(subscripts: &Subscripts, shapes: &[&[usize]]) -> Result<Self>

Automatically compute an optimized contraction order.

Uses a cost-based heuristic (e.g., greedy algorithm) to determine the pairwise contraction sequence that minimizes total operation count.

§Arguments
  • subscripts — Einsum subscripts for all tensors
  • shapes — Shape of each input tensor
§Errors

Returns an error if subscripts and shapes are inconsistent.

Source

pub fn from_pairs( subscripts: &Subscripts, shapes: &[&[usize]], pairs: &[(usize, usize)], ) -> Result<Self>

Manually build a contraction tree from a pairwise contraction sequence.

Each pair (i, j) specifies which two tensors (or intermediate results) to contract next. Intermediate results are assigned indices starting from the number of input tensors.

§Arguments
  • subscripts — Einsum subscripts for all tensors
  • shapes — Shape of each input tensor
  • pairs — Ordered list of pairwise contractions
§Examples
// Three tensors: A[ij] B[jk] C[kl] -> D[il]
// Contract B and C first, then A with the result:
let subs = Subscripts::new(&[&[0, 1], &[1, 2], &[2, 3]], &[0, 3]);
let shapes = [&[3, 4][..], &[4, 5], &[5, 6]];
let tree = ContractionTree::from_pairs(
    &subs,
    &shapes,
    &[(1, 2), (0, 3)],  // B*C -> T(index=3), then A*T -> D
).unwrap();
§Errors

Returns an error if the pairs do not form a valid contraction sequence.

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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