Expand description
High-level einsum with N-ary contraction tree optimization.
This crate provides:
- String notation:
"ij,jk->ik"(NumPy/PyTorch compatible) - Parenthesized notation:
"ij,(jk,kl)->il"respects user-specified contraction order viaNestedEinsum - Integer label notation: using
u32labels - Repeated labels:
"ii->i"extracts diagonals,"ii->"traces, and"i->ii"embeds a vector on a diagonal - N-ary contraction: Automatic or manual optimization of pairwise
contraction order via
ContractionTree - Tensordot sugar: NumPy-style axis-pair contraction extension methods, implemented as contraction sugar rather than as linear algebra APIs.
- Concrete execution: backend-explicit
TensorEinsumExt,TypedTensorEinsumExt,TensorReadEinsumExt, andConcreteEinsumPlanAPIs for non-AD tensor values. - Extension runtime: traced einsum lowers to a registered tenferro extension runtime, keeping core op definitions small.
- Tensor extension traits: graph-building and immediate-execution
helpers are available as methods on
GraphCompiler, concrete input slices/arrays, eager input slices/arrays, and tensor receivers.
§Examples
use tenferro_einsum::{ContractionTree, Subscripts};
let subs = Subscripts::parse("ij,jk->ik").unwrap();
let tree = ContractionTree::optimize(&subs, &[&[2, 3], &[3, 4]]).unwrap();
assert_eq!(tree.step_count(), 1);use tenferro_cpu::CpuBackend;
use tenferro_einsum::TensorEinsumExt;
use tenferro_tensor::Tensor;
let a = Tensor::from_vec_col_major(vec![2, 3], vec![1.0_f64; 6]).unwrap();
let b = Tensor::from_vec_col_major(vec![3, 4], vec![1.0_f64; 12]).unwrap();
let mut backend = CpuBackend::new();
let out = [&a, &b].einsum("ij,jk->ik", &mut backend)?;
assert_eq!(out.shape(), &[2, 4]);use tenferro_einsum::Subscripts;
let trace = Subscripts::parse("ii->").unwrap();
let diagonal = Subscripts::parse("ii->i").unwrap();
let embedded = Subscripts::parse("i->ii").unwrap();
let higher_rank = Subscripts::parse("iij->ij").unwrap();
assert!(trace.output.is_empty());
assert_eq!(diagonal.output, vec![b'i' as u32]);
assert_eq!(embedded.output, vec![b'i' as u32, b'i' as u32]);
assert_eq!(higher_rank.inputs[0], vec![b'i' as u32, b'i' as u32, b'j' as u32]);Modules§
- lowering
- Read-only einsum lowering plans.
Structs§
- Concrete
Einsum Plan - Prepared concrete einsum plan for repeated executions with fixed input dtype and shape metadata.
- Contraction
Optimizer Options - Public options for automatic contraction-path optimization.
- Contraction
Tree - Contraction tree determining pairwise contraction order for N-ary einsum.
- Einsum
Subscripts - Canonical N-ary einsum subscripts using integer labels.
- Subscripts
- Einsum subscripts using integer labels (omeinsum-rs compatible).
Enums§
- Einsum
Optimize - Controls how the contraction path is determined for N-ary einsum.
- Error
- Errors produced while parsing, planning, or lowering einsum expressions.
- Nested
Einsum - Recursive einsum tree that preserves parenthesized grouping.
- Tensor
DotAxes - Axis specification for
TracedTensorEinsumExt::tensordotcontraction sugar.
Constants§
- EINSUM_
EXTENSION_ FAMILY_ ID - Stable family identifier for the standard tenferro einsum extension.
Traits§
- Eager
Einsum Ext - Eager einsum extension methods for slices or arrays of
EagerTensorrefs. - Eager
Tensor Einsum Ext - Eager tensor contraction-sugar methods.
- Graph
Compiler Einsum Ext - Traced einsum extension methods for
GraphCompiler. - Tensor
Einsum Ext - Backend-explicit einsum methods for dtype-erased concrete tensors.
- Tensor
Einsum Into Ext - Backend-explicit preallocated-output einsum methods for dtype-erased tensors.
- Tensor
Read Einsum Ext - Backend-explicit einsum methods for
TensorReadinputs. - Tensor
Read Einsum Into Ext - Backend-explicit preallocated-output einsum methods for
TensorReadinputs. - Traced
Tensor Einsum Ext - Traced tensor contraction-sugar methods.
- Typed
Tensor Einsum Ext - Backend-explicit einsum methods for typed concrete tensors.
- Typed
Tensor Einsum Into Ext - Backend-explicit preallocated-output einsum methods for typed concrete tensors.
Functions§
- ad_
rules - Return the explicit einsum extension AD rule set.
- parse_
einsum_ subscripts - Parse string einsum notation into canonical integer labels.
- register_
runtime
Type Aliases§
- Result
- Result type alias for einsum parsing, planning, and lowering.