question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Einsum indexing very fragile, because it tests for int (and int64 is not int)

See original GitHub issue

The index arrays for einsum do not accept the output of any numpy array, because these datatypes cannot be ints (see #2951; #12322). The test should be modified to accept numpy ints, because converting the datatype to ‘int’ explicitly is counter-intuitive and unnecessary.

Though easy to circumvent, this will incur a lot of unnecessary debugging time, as it appears to break abstraction boundaries, has strange interactions with tolist, and indices that work in test environments fail when constructed programatically.

Reproducing code example:

import numpy as np
X = np.arange(9).reshape(3,3)

# each of these results in an identical list [0] as far as equality test is concerned 
idx1 = [0]                # list[ int ] 
idx2 = np.unique(idx1)    # np.array [int64]
idx3 = idx2.tolist()      # list [int]
idx3 = list(idx2)         # list [int64]

np.einsum(X, [0], idx1 ) # succeeds
np.einsum(X, [0], idx2 ) # fails
np.einsum(X, [0], idx3 ) # succeeds
np.einsum(X, [0], idx4 ) # fails

Error message:

ValueError: each subscript must be either an integer or an ellipsis

Full error message if optimize_arg is False:

<__array_function__ internals> in einsum(*args, **kwargs)

~/.local/lib/python3.6/site-packages/numpy/core/einsumfunc.py in einsum(*operands, **kwargs)
   1354     # If no optimization, run pure einsum
   1355     if optimize_arg is False:
-> 1356         return c_einsum(*operands, **kwargs)
   1357 
   1358     valid_einsum_kwargs = ['out', 'dtype', 'order', 'casting']

ValueError: each subscript must be either an integer or an ellipsis

Full error message when optimize_arg is True:

<__array_function__ internals> in einsum(*args, **kwargs)

~/.local/lib/python3.6/site-packages/numpy/core/einsumfunc.py in einsum(*operands, **kwargs)
   1377     # Build the contraction list and operand
   1378     operands, contraction_list = einsum_path(*operands, optimize=optimize_arg,
-> 1379                                              einsum_call=True)
   1380 
   1381     handle_out = False

Numpy/Python version information:

1.17.3 3.6.9 (default, Nov 7 2019, 10:44:02) [GCC 8.3.0]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tinaoberoicommented, Apr 21, 2020

@guilhermeleobas you working on this ? If not I would like to take this up. Thanks

0reactions
rlintottcommented, Apr 25, 2020

I’m also a first time contributor. I might try fixing this. I don’t really mind if someone else contribute first. In any case, the experience is good for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.einsum sometimes ignores dtype argument
Suppose I have two arrays of type int8. I want to use einsum on them in such a way that all the calculations...
Read more >
NumPy 1.13.0 Release Notes
partition , TypeError when non-integer partition index is used. ... change in implementation some very delicate tests may fail that did not ......
Read more >
Release Notes — NumPy v1.12 Manual - GitHub Pages
Non -integers used as index values raise TypeError , e.g., in reshape ... Due to the change in implementation some very delicate tests...
Read more >
What's New — xarray 0.10.2 documentation
Calling repr() or printing xarray objects at the command line or in a Jupyter Notebook will not longer automatically compute dask variables or...
Read more >
Python API: test/test_jit.py Source File
233 # The debug state struct does not own its graph so we make a copy of it. ... 1226 run(dtype=torch.int, inputs_require_grads=False).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found