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
impl ContractionTree
Sourcepub fn optimize(subscripts: &Subscripts, shapes: &[&[usize]]) -> Result<Self>
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 tensorsshapes— Shape of each input tensor
§Errors
Returns an error if subscripts and shapes are inconsistent.
Sourcepub fn from_pairs(
subscripts: &Subscripts,
shapes: &[&[usize]],
pairs: &[(usize, usize)],
) -> Result<Self>
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 tensorsshapes— Shape of each input tensorpairs— 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.