Skip to content

einsum broadcast regression (with optimize=True) #10343

Closed

Description

@d70-t

In numpy 1.13.3, it was possible to execute the following snippet without errors, while in 1.14.0 this happens:

In [1]: import numpy as np
In [2]: a = np.ones((10,2))
In [3]: b = np.ones((1,2))
In [4]: np.einsum('t...i,ti->t...', a, b)
Traceback (most recent call last):
  File "<ipython-input-4-fa62d1d882f9>", line 1, in <module>
    np.einsum('t...i,ti->t...', a, b)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/einsumfunc.py", line 1087, in einsum
    einsum_call=True)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/einsumfunc.py", line 710, in einsum_path
    "not match previous terms.", char, tnum)
ValueError: ("Size of label '%s' for operand %d does not match previous terms.", 't', 1)

However optimize=False solves the problem:

In [5]: np.einsum('t...i,ti->t...', a, b, optimize=False)
Out[5]: array([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])

Is this intended behavior and the user is now responsible to explicitly disable optimization or is this a bug?

Activity

  1. added this to the milestone on Jan 8, 2018
  2. charris commented on Jan 8, 2018

    @charris
    Member

    Both? optimize=True was made the default, but it shouldn't be causing errors.

    @dgasmith Another one ;)

  3. eric-wieser commented on Jan 8, 2018

    @eric-wieser
    Member

    Seems we failed to format the error message correctly either

  4. mhvk commented on Jan 8, 2018

    @mhvk
    Contributor

    This hit at least one user of astropy (though not our tests, annoyingly!). The following breaks (but works with optimize=False):

    np.einsum('...ij,j...->i...', np.eye(3)[np.newaxis], np.array([[1., 1.], [2., 2.], [3., 3.]]))
    
  5. mhvk commented on Jan 10, 2018

    @mhvk
    Contributor

    The fix in #10352 seems correct. Note also that the current optimization choice makes no sense: for 2 argument, optimization should not be attempted; see #10357.

  6. mhvk commented on Jan 10, 2018

    @mhvk
    Contributor

    A very simple fix for the slowdown is in #10359 - I think we should consider reverting the change in default of optimize, though. In the end, einsum is very likely used mostly with small numbers of arrays, for which the additional cost is not worth it. It seems more likely that users with long expressions will read the docstring and realize there is an optimize keyword than that users with short ones will realize the default is bad for their case.

  7. mhvk commented on Jan 14, 2018

    @mhvk
    Contributor

    @charris - this issue is biting lots of astropy users now (astropy/astropy#7051 (comment)) as it screws up one of our coordinate transformations. Those are all with 3x3 matrices, so pending not just a solution but also a better understanding of impacts of performance, I would indeed suggest to revert the default for 1.14.1. Obviously, we also need #10352 and especially its tests so this doesn't recur! (We missed it in astropy in part because for python3 our transformations use matmul -- guess we should test our last python2 version also with numpy-dev...)

  8. charris commented on Jan 14, 2018

    @charris
    Member
  9. mhvk commented on Jan 14, 2018

    @mhvk
    Contributor

    OK, good, that gives more time.

  10. added a commit that references this issue on Mar 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions