Skip to main content

Build Status codecov Anaconda-Server Badge PyPI Documentation Status DOI

News: Opt_einsum will be in NumPy 1.12 and BLAS features in NumPy 1.14! Call opt_einsum as np.einsum(..., optimize=True). This repository contains more advanced features such as Dask or Tensorflow backends as well as a testing ground for newer features in this ecosystem.

Optimized Einsum: A tensor contraction order optimizer

Optimized einsum can greatly reduce the overall time np.einsum takes by optimizing the expression's contraction order and dispatching many operations to canonical BLAS routines. See the documention for more information.

As well as opt_einsum.contract acting as a drop-in replacement for np.einsum, the following capabilities are enabled by opt_einsum:

Quick tutorial

Einsum is a powerful function for contracting tensors of arbitrary dimension and index. However, it is only optimized to contract two terms at a time resulting in non-optimal scaling.

For example, let us examine the following index transformation: M_{pqrs} = C_{pi} C_{qj} I_{ijkl} C_{rk} C_{sl}

We can then develop two seperate implementations that produce the same result:

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

def naive(I, C):
    # N^8 scaling
    return np.einsum('pi,qj,ijkl,rk,sl->pqrs', C, C, I, C, C)

def optimized(I, C):
    # N^5 scaling
    K = np.einsum('pi,ijkl->pjkl', C, I)
    K = np.einsum('qj,pjkl->pqkl', C, K)
    K = np.einsum('rk,pqkl->pqrl', C, K)
    K = np.einsum('sl,pqrl->pqrs', C, K)
    return K

The np.einsum function does not consider building intermediate arrays; therefore, helping einsum out by building these intermediate arrays can result in a considerable cost saving even for small N (N=10):

np.allclose(naive(I, C), optimized(I, C))
True

%timeit naive(I, C)
1 loops, best of 3: 934 ms per loop

%timeit optimized(I, C)
1000 loops, best of 3: 527 µs per loop

A 2000 fold speed up for 4 extra lines of code! This contraction can be further complicated by considering that the shape of the C matrices need not be the same, in this case, the ordering in which the indices are transformed matters significantly. Logic can be built that optimizes the ordering; however, this is a lot of time and effort for a single expression.

The opt_einsum package is a drop-in replacement for the np.einsum function and can handle all of this logic for you:

from opt_einsum import contract

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

The above will automatically find the optimal contraction order, in this case, identical to that of the optimized function above, and compute the products for you. In this case, it even uses np.dot under the hood to exploit any vendor BLAS functionality that your NumPy build has!

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.

Metadata

Release files for opt-einsum 2.1.2

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 2.1.2
File Size Uploaded
opt_einsum-2.1.2.tar.gz 49.5 kB Details

Release files / opt_einsum-2.1.2.tar.gz

Download URL opt_einsum-2.1.2.tar.gz
Size 49.5 kB
Tags Source
SHA-256 checksum
How to use checksums
815fbb96d60edb10087c0bd80128d11116c374582ac489050cf31928195278d0
BLAKE2b-256 checksum
How to use checksums
362497005b8d821e798ed68957b297595f161c22f2f31107703c0433e98195c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.14.0 CPython/3.6.6

Release history Release notifications | RSS feed

3.4.0

2 release files

3.3.0

2 release files

3.2.1

2 release files

3.2.0

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

This release

2.1.2 This release

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