pub struct Einsum<L: Label = usize> {
pub ixs: Vec<Vec<L>>,
pub iy: Vec<L>,
pub size_dict: HashMap<L, usize>,
/* private fields */
}Expand description
Einsum specification and execution engine.
Supports contraction order optimization via omeco.
§Example
use omeinsum::{Einsum, Tensor, Cpu};
use omeinsum::algebra::MaxPlus;
use std::collections::HashMap;
// A[i,j] × B[j,k] → C[i,k]
let a = Tensor::<f32, Cpu>::from_data(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
let b = Tensor::<f32, Cpu>::from_data(&[5.0, 6.0, 7.0, 8.0], &[2, 2]);
let sizes: HashMap<usize, usize> = [(0, 2), (1, 2), (2, 2)].into();
let mut ein = Einsum::new(
vec![vec![0, 1], vec![1, 2]],
vec![0, 2],
sizes,
);
ein.optimize_greedy();
let result = ein.execute::<MaxPlus<f32>, f32, Cpu>(&[&a, &b]);
assert_eq!(result.shape(), &[2, 2]);Fields§
§ixs: Vec<Vec<L>>Input index labels for each tensor
iy: Vec<L>Output index labels
size_dict: HashMap<L, usize>Dimension sizes for each index
Implementations§
Source§impl<L: Label> Einsum<L>
impl<L: Label> Einsum<L>
Sourcepub fn new(ixs: Vec<Vec<L>>, iy: Vec<L>, size_dict: HashMap<L, usize>) -> Self
pub fn new(ixs: Vec<Vec<L>>, iy: Vec<L>, size_dict: HashMap<L, usize>) -> Self
Create a new einsum specification.
§Arguments
ixs- Index labels for each input tensoriy- Output index labelssize_dict- Mapping from index labels to dimension sizes
Sourcepub fn optimize_greedy(&mut self) -> &mut Self
pub fn optimize_greedy(&mut self) -> &mut Self
Optimize contraction order using greedy algorithm.
Fast O(n²) algorithm, good for most cases.
Sourcepub fn optimize_treesa(&mut self) -> &mut Self
pub fn optimize_treesa(&mut self) -> &mut Self
Optimize contraction order using simulated annealing.
Slower but finds better orderings for complex networks.
Sourcepub fn is_optimized(&self) -> bool
pub fn is_optimized(&self) -> bool
Check if optimization has been performed.
Sourcepub fn contraction_tree(&self) -> Option<&NestedEinsum<L>>
pub fn contraction_tree(&self) -> Option<&NestedEinsum<L>>
Get the optimized contraction tree.
Source§impl Einsum<usize>
impl Einsum<usize>
Sourcepub fn execute<A, T, B>(&self, tensors: &[&Tensor<T, B>]) -> Tensor<T, B>
pub fn execute<A, T, B>(&self, tensors: &[&Tensor<T, B>]) -> Tensor<T, B>
Execute the einsum contraction.
§Type Parameters
A- The algebra to use (e.g.,Standard<f32>,MaxPlus<f32>)T- The scalar typeB- The backend type
Auto Trait Implementations§
impl<L> Freeze for Einsum<L>
impl<L> RefUnwindSafe for Einsum<L>where
L: RefUnwindSafe,
impl<L> Send for Einsum<L>
impl<L> Sync for Einsum<L>
impl<L> Unpin for Einsum<L>where
L: Unpin,
impl<L> UnwindSafe for Einsum<L>where
L: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> Twhere
Self: Distribution<T>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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