Repository navigation
BUG: do not optimize einsum with only 2 arguments. - #10359
Conversation
fixes numpygh-10357. Avoid slow execution of 2-argument einsum calls, by ensuring we use the c code directly.
|
Looks like a reasonable fix to me. |
| # 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) |
There was a problem hiding this comment.
Do we want a stronger condition than this, and completely ignore the optimize arg with two arguments, rather than only change its default?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
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. |
|
Remove backport candidate as 1.14.1 does not default to optimization of any kind. |
fixes gh-10357.
Avoid slow execution of 2-argument einsum calls, by ensuring we use the c code directly.