Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

numpy.ndarray.copy() incorrect when operating on less common array layouts #3557

Closed
QB3 opened this issue Dec 3, 2018 · 4 comments
Closed
Labels

Comments

@QB3
Copy link

QB3 commented Dec 3, 2018

Description

The combination of transpose + reshape operations gives different results in numba and numpy.

  • I am using numba version 0.41.0
  • I have included below a minimal working reproducer
import numpy as np
from numba import njit

@njit
def resh_numba(a):
    return a.transpose(1, 0, 2).copy().reshape(2, 12)

x = np.arange(24).reshape(2, 2, 6)

print("numpy")
x_numpy = x.transpose(1, 0, 2).copy().reshape(2, 12)
print(x_numpy)

print("numba:")
x_numba = resh_numba(x)
print(x_numba)
@stuartarchibald
Copy link
Contributor

Thanks for the report. I think the problem is with Numba's np.ndarray.copy implementation:

import numpy as np
from numba import njit

def transp(a):
    return a.transpose(1, 0, 2)

def transp_copy(a):
    return a.transpose(1, 0, 2).copy()

x = np.arange(24).reshape(2, 2, 6)

def test(fn):
    print("Testing %s" % fn.__name__)
    np.testing.assert_allclose(fn(x), njit(fn)(x))
    print("ok\n")

if __name__ == "__main__":
    test(transp)
    test(transp_copy)

gives:

$ python issue3557.py 
Testing transp
ok

Testing transp_copy
Traceback (most recent call last):
  File "issue3557.py", line 19, in <module>
    test(transp_copy)
  File "issue3557.py", line 14, in test
    np.testing.assert_allclose(fn(x), njit(fn)(x))
  File "<path>/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 1452, in assert_allclose
    verbose=verbose, header=header, equal_nan=equal_nan)
  File "<path>/lib/python3.7/site-packages/numpy/testing/_private/utils.py", line 789, in assert_array_compare
    raise AssertionError(msg)
AssertionError: 
Not equal to tolerance rtol=1e-07, atol=0

(mismatch 91.66666666666667%)
 x: array([ 0,  1,  2,  3,  4,  5, 12, 13, 14, 15, 16, 17,  6,  7,  8,  9, 10,
       11, 18, 19, 20, 21, 22, 23])
 y: array([ 0,  4,  8, 12, 16, 20,  2,  6, 10, 14, 18, 22,  1,  5,  9, 13, 17,
       21,  3,  7, 11, 15, 19, 23])

@QB3
Copy link
Author

QB3 commented Dec 3, 2018

Thanks a lot for this quick answer.
Shall I rename the issue and change the minimal example then ?

@stuartarchibald stuartarchibald changed the title transpose + reshape gives different results in numpy and numba numpy.ndarray.copy() incorrect when operating on less common array layouts Dec 3, 2018
@stuartarchibald
Copy link
Contributor

@QB3 no problem. Title updated and the sample in #3557 (comment) is sufficient thanks. I'm slightly suspicious that once the copy is fixed that the reshape will also need attention.

@stuartarchibald
Copy link
Contributor

On mainline for 0.55, neither the OP reproducer or that in #3557 (comment) reproduces any more. Closing assume fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants