Skip to content

BUG: do not optimize einsum with only 2 arguments. - #10359

Merged
charris merged 1 commit into
numpy:masterfrom
mhvk:einsum-do-not-optimize-2-arg
Feb 5, 2018
Merged

charris merged 1 commit into
numpy:masterfrom
mhvk:einsum-do-not-optimize-2-arg

Conversation

@mhvk

@mhvk mhvk commented Jan 10, 2018

Copy link
Copy Markdown
Contributor

fixes gh-10357.

Avoid slow execution of 2-argument einsum calls, by ensuring we use the c code directly.

fixes numpygh-10357.

Avoid slow execution of 2-argument einsum calls, by ensuring
we use the c code directly.
@larsoner

Copy link
Copy Markdown
Contributor

Looks like a reasonable fix to me.

Comment thread numpy/core/einsumfunc.py
# Grab non-einsum kwargs
optimize_arg = kwargs.pop('optimize', True)
# Grab non-einsum kwargs; never optimize 2-argument case.
optimize_arg = kwargs.pop('optimize', len(operands) > 3)

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.

Do we want a stronger condition than this, and completely ignore the optimize arg with two arguments, rather than only change its default?

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 seems that it would make sense. Things are probably more complicated, as optimization could depend on the dimensions and specific contractions, but I think we do have data that it isn't a trivial problem. For 1.14 I'd be tempted to just change the optimization default back to False.

I won't be making 1.14.1 before Feb, as it usually takes a couple of weeks for all the initial problems to trickle in. 1.14.0 has been unusual in that regard. Anyway, time enough for @dgasmith to chime in after the holidays.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The optimize flag also allows usage of the various dot functions which can greatly reduce the cost of many other types of expressions. So while you do occur a static overhead, it is typically well worth it for problems that are non-trivial in size. I believe there was quite a bit of discussion on this originally and it was decided (guessed) that the distribution of use cases would err away from small problems.

As @charris mentioned it's difficult to figure out an even faster way to examine the expression to see if the optimization is worth it. One original suggestion was to have a simple input size cutoff where binary expressions would either go through optimization or not. This can go awry in a number of ways, but it is a possible heuristic.

Binding the current einsum parsing tech to Python would be good, but it is fairly intricately tied to the main c_einsum function at the moment.

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.

I would be quite surprised if there isn't a lot of code that uses einsum for, say, actual einstein summations, i.e., matrices with dimensions of just 3 or 4, where the static overhead is large. The reason we ran into the problems in astropy is that we used einsum as a work-around for not having matmul for coordinate transformations (i.e., we deal with 3x3 matrices operating on 3-element vectors, broadcast over the other dimensions).

@charris charris added this to the 1.14.1 release milestone Jan 10, 2018
@charris
charris merged commit c83653b into numpy:master Feb 5, 2018
@charris

charris commented Feb 5, 2018

Copy link
Copy Markdown
Member

Let's put this in. It is clear that optimization is still a somewhat open problem, so all this may change in the future, but for now this PR solves an unfortunate regression. Einsum is such a useful function that I hope work on it continues.

Thanks Marten.

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Feb 5, 2018
@charris

charris commented Feb 5, 2018

Copy link
Copy Markdown
Member

Remove backport candidate as 1.14.1 does not default to optimization of any kind.

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.

Major slowdown as einsum optimize path used even for two arguments

5 participants