⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.
See the codeOptimized 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.
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:
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.
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:
Please see the documentation for more features!
opt_einsum can be installed via pip:
pip install opt_einsum
or with uv:
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:
uv pip install -e ".[dev]"
See the installation documentation for further methods.
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 einsum-like expressions. Journal of Open Source Software, 2018, 3(26), 753
DOI: https://doi.org/10.21105/joss.00753
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.
A detailed overview on how to contribute can be found in the contributing guide.
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
551 followers · starred Jan 2015
13,725 followers · starred Aug 2018
1,883 followers · starred Mar 2018
3,249 followers · starred Jun 2025
⚡️Optimizing einsum functions in NumPy, Tensorflow, Dask, and more with contraction order optimization.
See the codeOptimized 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.
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:
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.
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:
Please see the documentation for more features!
opt_einsum can be installed via pip:
pip install opt_einsum
or with uv:
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:
uv pip install -e ".[dev]"
See the installation documentation for further methods.
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 einsum-like expressions. Journal of Open Source Software, 2018, 3(26), 753
DOI: https://doi.org/10.21105/joss.00753
All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.
A detailed overview on how to contribute can be found in the contributing guide.
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
551 followers · starred Jan 2015
13,725 followers · starred Aug 2018
1,883 followers · starred Mar 2018
3,249 followers · starred Jun 2025