Skip to content

ENH: Einsum calls BLAS if it advantageous to do so - #9425

Merged
charris merged 4 commits into
numpy:masterfrom
dgasmith:einsum_blas
Jul 18, 2017
Merged

charris merged 4 commits into
numpy:masterfrom
dgasmith:einsum_blas

Conversation

@dgasmith

@dgasmith dgasmith commented Jul 15, 2017 •

Copy link
Copy Markdown
Contributor

Einsum will now call tensordot if it is advantageous to do so:

a = np.random.rand(100, 100, 100)

%timeit np.einsum('ijk,jkl->il', a, a, optimize=False)
10 loops, best of 3: 50.1 ms per loop

%timeit np.einsum('ijk,jkl->il', a, a, optimize=True)
1000 loops, best of 3: 1.6 ms per loop

This is also quite advantageous even if a copy is required:

%timeit np.einsum('ikj,jkl->il', a, a, optimize=False)
10 loops, best of 3: 79.4 ms per loop

%timeit np.einsum('ikj,jkl->il', a, a, optimize=True)
100 loops, best of 3: 4.87 ms per loop

For level 3 BLAS the array is always copied while for level 1 and 2 the data must be aligned for BLAS to be called as einsum is faster than BLAS in these cases.

There was also a small bug in tensordot where it would fail for inner product cases.

This PR also switches einsum to optimize the contraction by default. I was originally concerned that there may be remaining bugs in the path code, but I have not seen any issues since this feature was originally released. I hope the algorithm is battle tested enough that the optimized path can be the default due to the speedups that can occur.

The downside is the path logic takes ~20-50us:

%timeit np.einsum('ij,ji->', a, a, optimize=True)
10000 loops, best of 3: 37.6 µs per loop

%timeit np.einsum('ij,ji->', a, a, optimize=False)
1000000 loops, best of 3: 1.67 µs per loop

A few arbitrary cutoffs can be placed to prevent the logic overhead (switching back to c_einsum immediately if there are less then say 1000 values in total). However, some discussion is desired if this is need or wanted as I feel a little uncomfortable taking different paths based on size of a contraction.

Comment thread numpy/core/einsumfunc.py Outdated

# Standard GEMM operation
>>> _can_blas(['ij', 'jk'], 'ik', set('j'))
'GEMM'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be True

Comment thread numpy/core/einsumfunc.py Outdated
elif inputs[0] == inputs[1]:
return True

# DDOT doesnt make sense if you have to tranpose

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tranpose -> transpose

Comment thread numpy/core/einsumfunc.py Outdated

# Checks have already been handled
input_str, results_index = einsum_str.split('->')
input_indices = input_str.split(',')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using integer indexing below, use tuple unpacking, e.g.,

indices_left, indices_right = input_str.split(',')

Comment thread numpy/core/einsumfunc.py Outdated
return False

# DDOT
elif inputs[0] == inputs[1]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that each of these clauses contains a return, consider making them all if instead of elif

Comment thread numpy/core/einsumfunc.py Outdated
sets = [set(x) for x in inputs]
keep_left = sets[0] - idx_removed
keep_right = sets[1] - idx_removed
input_left = inputs[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer tuple unpacking here: input_left, input_right = inputs

Comment thread numpy/core/einsumfunc.py
# Can only do two
if len(inputs) != 2:
return False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, I would probably unpack input_left, input_right = inputs and do everything with the separated inputs.

Comment thread numpy/core/einsumfunc.py Outdated
return False

# Make sure there is overlap
if len(set(inputs[0]) & set(inputs[1])) == 0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer implicit boolean evaluation: if not (left_inputs & right_inputs)

Comment thread numpy/core/einsumfunc.py
# Cannot handle partial inner
if len(keep_left & keep_right):
return False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good place to summarize the invariants we've established and the logic in the big if/else below. E.g., if I'm following this correctly:
We've shown the expression can be expressed as a BLAS call with tensordot. Now check if it can be done without a copy.

Comment thread numpy/core/einsumfunc.py Outdated
return False

# Conventional matrix tensordot
else:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't immediately follow to me that the conditions above check all other possibilities. Maybe there's a more tree like way to express them that would make this clearer?

Also, what exactly does "conventional matrix tensordot" mean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comments, changes have been made based off them. I would be very open to cleaning up the logic if you have any explicit ideas.

@shoyer shoyer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me!

Comment thread numpy/core/einsumfunc.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputs_left == inputs_right

Comment thread numpy/core/einsumfunc.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not keep_left or not keep_right:

@charris
charris merged commit e3b5e88 into numpy:master Jul 18, 2017
@charris

charris commented Jul 18, 2017

Copy link
Copy Markdown
Member

Thanks @dgasmith .

@matthew-brett

Copy link
Copy Markdown
Contributor

Nice - thanks!

charris pushed a commit to charris/numpy that referenced this pull request Sep 26, 2017
* Einsum now optionally uses BLAS

* The einsum call is now optimized by default

* cleans up the dot logic

* MAINT: Correct spelling, tranpose <- transpose.
@jakirkham jakirkham mentioned this pull request Nov 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants