Skip to content

Commit

Permalink
Use opt_einsum in NumPy backend
Browse files Browse the repository at this point in the history
numpy.einsum is very slow in some cases.
  • Loading branch information
yuchenpang committed Jul 7, 2020
1 parent c179116 commit 731f1bf
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
install_requires=[
'numpy',
'scipy',
'opt_einsum',
],
)
3 changes: 2 additions & 1 deletion tensorbackends/backends/numpy/numpy_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import numpy.linalg as la
from opt_einsum import contract

from ...interface import Backend
from ...utils import einstr
Expand Down Expand Up @@ -175,7 +176,7 @@ def wrapped_result(*args, **kwargs):
return result

def _einsum(self, expr, operands):
result = np.einsum(expr.indices_string, *(operand.tsr for operand in operands), optimize='greedy')
result = contract(expr.indices_string, *(operand.tsr for operand in operands))
if isinstance(result, np.ndarray) and result.ndim != 0:
newshape = expr.outputs[0].newshape(result.shape)
result = result.reshape(*newshape)
Expand Down

0 comments on commit 731f1bf

Please sign in to comment.