dgasmith /opt_einsum

⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.

★ 994⑂ 82👁 21⑃ 130● 142▤ 20
PythonMITCreated 2014-12-12dgasmith.github.io/opt_einsum/
下载:v3.4.0v3.4.0 ↓3.4.02024-09-26
+1
日增长
994
stars
82
forks
21
watchers
130
prs
142
issues
20
releases

README

Optimized Einsum









 Optimized Einsum: A tensor contraction order optimizer

Optimized einsum can significantly reduce the overall execution time of einsum-like expressions (e.g.,
np.einsum,
dask.array.einsum,
pytorch.einsum,
tensorflow.einsum,
)
by optimizing the expression's contraction order and dispatching many
operations to canonical BLAS, cuBLAS, or other specialized routines.

Optimized
einsum is agnostic to the backend and can handle NumPy, Dask, PyTorch,
Tensorflow, CuPy, Sparse, Theano, JAX, and Autograd arrays as well as potentially
any library which conforms to a standard API. See the
documentation for more
information.

 Example usage

The opt_einsum.contract
function can often act as a drop-in replacement for einsum
functions without further changes to the code while providing superior performance.
Here, a tensor contraction is performed with and without optimization:

python
import numpy as np
from opt_einsum import contract

N = 10
C = np.random.rand(N, N)
I = np.random.rand(N, N, N, N)

%timeit np.einsum('pi,qj,ijkl,rk,sl-pqrs', C, C, I, C, C)
1 loops, best of 3: 934 ms per loop

%timeit contract('pi,qj,ijkl,rk,sl-pqrs', C, C, I, C, C)
1000 loops, best of 3: 324 us per loop


In this particular example, we see a ~3000x performance improvement which is
not uncommon when compared against unoptimized contractions. See the backend
examples
for more information on using other backends.

 Features

The algorithms found in this repository often power the einsum optimizations
in many of the above projects. For example, the optimization of np.einsum
has been passed upstream and most of the same features that can be found in
this repository can be enabled with np.einsum(..., optimize=True). However,
this repository often has more up to date algorithms for complex contractions.

The following capabilities are enabled by opt_einsum:

 Inspect detailed information about the path chosen.
 Perform contractions with numerous backends, including on the GPU and with libraries such as TensorFlow and PyTorch.
 Generate reusable expressions, potentially with constant tensors, that can be compiled for greater performance.
 Use an arbitrary number of indices to find contractions for hundreds or even thousands of tensors.
 Share intermediate computations among multiple contractions.
 Compute gradients of tensor contractions using autograd or jax

Please see the documentation for more features!

 Installation

opt_einsum can be installed via pip:

bash
pip install opt_einsum


or with uv:

bash
uv pip install opt_einsum


It is also available on conda-forge: conda install opt_einsum -c conda-forge.

For development, clone the repository and install with optional extras:

bash
uv pip install -e ".[dev]"


See the installation documentation for further methods.

 Citation

If this code has benefited your research, please support us by citing:

Daniel G. A. Smith and Johnnie Gray, opt_einsum - A Python package for optimizing contraction order for eins

Top issues

Latest release

v3.4.0 v3.4.0 2024-09-26
3.4.0 / 2024-09-26

NumPy has been removed from opt_einsum as a dependency allowing for more flexible installs. Type hints have been added to the code base as well as an overhaul of the documentation to MkDocs here: https://dgasmith.github.io/opt_einsum/

New Features

 - \160 Migrates docs to MkDocs Material and GitHub pages hosting.
 - \161 Adds Python type annotations to the code base.
 - \204 Removes NumPy as a hard dependency.

Enhancements

- \154 Prevents an infinite recursion error when the memory_limit was set very low for the dp algorithm. 
- \155 Adds flake8 spell check

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON