Skip to main content

Build Status codecov Anaconda-Server Badge PyPI PyPIStats Documentation Status DOI

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 futher changes to the code while providing superior performance. Here, a tensor contraction is preformed 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.

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:

Please see the documentation for more features!

Installation

opt_einsum can either be installed via pip install opt_einsum or from conda conda install opt_einsum -c conda-forge. See the installation documenation 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 einsum-like expressions. Journal of Open Source Software, 2018, 3(26), 753

DOI: https://doi.org/10.21105/joss.00753

Contributing

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.

Release files for opt-einsum 3.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opt-einsum 3.2.0
File Size Uploaded
opt_einsum-3.2.0.tar.gz 71.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opt-einsum 3.2.0
File Interpreter ABI Platform
opt_einsum-3.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 134.5 kB

Release files / opt_einsum-3.2.0.tar.gz

Download URL opt_einsum-3.2.0.tar.gz
Size 71.1 kB
Tags Source
SHA-256 checksum
How to use checksums
738b0a1db1d3084d360081bb64d826f9db06d2df7cc0bf8e2c9356028da1fa31
BLAKE2b-256 checksum
How to use checksums
7dbab7b87195f0e9f7d452d1fb9eed4e43ecf077c1b08d2a87b56f721d7aa1dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200209 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

Release files / opt_einsum-3.2.0-py3-none-any.whl

Download URL opt_einsum-3.2.0-py3-none-any.whl
Size 63.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6fbfbb759e670305c5c9a4dc4432e30f65e06fede88293dacfe19915af73386
BLAKE2b-256 checksum
How to use checksums
b2492233e63052d5686c72131b579837ddfb98ba9dd0b92bb91efcb441ada8ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200209 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.5

Release history Release notifications | RSS feed

3.4.0

2 release files

3.3.0

2 release files

3.2.1

2 release files

This release

3.2.0 This release

2 release files

3.1.0

1 release file

3.0.1

1 release file

2.3.2

1 release file

2.3.1

1 release file

2.2.0

1 release file

2.1.3

1 release file

2.1.2

1 release file

2.1.1

1 release file

2.0.0

2 release files

1.0.1

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page