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 (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 optimize_with_options( subscripts: &Subscripts, shapes: &[&[usize]], options: &ContractionOptimizerOptions, ) -> Result<Self>

Automatically compute an optimized contraction order with explicit planner options.

This routes automatic planning through TreeSA using the provided configuration. The default options correspond to a greedy-initialized TreeSA with zero annealing iterations.

§Errors

Returns an error if subscripts, shapes, or planner options are invalid.

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.

Source

pub fn step_count(&self) -> usize

Return the number of pairwise contraction steps in this tree.

§Examples
use tenferro_einsum::{ContractionTree, Subscripts};

let subs = Subscripts::new(&[&[0, 1], &[1, 2], &[2, 3]], &[0, 3]);
let tree = ContractionTree::from_pairs(
    &subs,
    &[&[2, 2], &[2, 2], &[2, 2]],
    &[(1, 2), (0, 3)],
)
.unwrap();
assert_eq!(tree.step_count(), 2);
Source

pub fn step_pair(&self, step_idx: usize) -> Option<(usize, usize)>

Return the operand indices for a pairwise contraction step.

The returned indices refer to the original inputs (0..n_inputs) and then to intermediates (n_inputs..) produced by earlier steps.

§Examples
use tenferro_einsum::{ContractionTree, Subscripts};

let subs = Subscripts::new(&[&[0, 1], &[1, 2], &[2, 3]], &[0, 3]);
let tree = ContractionTree::from_pairs(
    &subs,
    &[&[2, 2], &[2, 2], &[2, 2]],
    &[(1, 2), (0, 3)],
)
.unwrap();
assert_eq!(tree.step_pair(0), Some((1, 2)));
Source

pub fn step_subscripts( &self, step_idx: usize, ) -> Option<(&[u32], &[u32], &[u32])>

Return the (lhs, rhs, output) subscripts for a pairwise step.

The output subscripts are the intermediate labels preserved after the contraction, or the final output labels on the last step.

§Examples
use tenferro_einsum::{ContractionTree, Subscripts};

let subs = Subscripts::new(&[&[0, 1], &[1, 2], &[2, 3]], &[0, 3]);
let tree = ContractionTree::from_pairs(
    &subs,
    &[&[2, 2], &[2, 2], &[2, 2]],
    &[(1, 2), (0, 3)],
)
.unwrap();
let (lhs, rhs, out) = tree.step_subscripts(0).unwrap();
assert_eq!(lhs, &[1, 2]);
assert_eq!(rhs, &[2, 3]);
assert_eq!(out, &[1, 3]);

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<T> DistributionExt for T
where T: ?Sized,

§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

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

§

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

§

impl<T> MaybeSend for T

§

impl<T> MaybeSendSync for T

§

impl<T> MaybeSync for T