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

Incorrect array returned from guvectorize #4893

Closed
sjster opened this issue Nov 26, 2019 · 7 comments
Closed

Incorrect array returned from guvectorize #4893

sjster opened this issue Nov 26, 2019 · 7 comments
Labels
no action required No action was needed to resolve.

Comments

@sjster
Copy link

sjster commented Nov 26, 2019

The results returned from the function accelerated don't match the array values in the function. The values computed in the function are correct, however when I print them out in the caller the results are different. The signature of my function is shown here

@guvectorize(
    ["u8, u8, u8, u8, f8[:,:], f8[:,:], f8[:,:], f8[:,:], f8[:], f8[:], f8[:,:]"], "(), (), (), (), (nrow, nrow), (ncol, ncol), (nrow, ncol), (nrow, ncol), (nrow), (ndim) -> (nrow, ndim)", nopython=True,  target="cpu"
)
def numba_svd_derivative(j, nrow, ncol, ncomp, u, v, Xorig, X, d_sq, dum_var, result):


This is assigned in the caller as

res = numba_svd_derivative(j, self.nrow, self.ncol, self.projection_components, \
                                        self.u, self.v, self.Xorig, self.X, d_sq, dum_var)

I print out the result in the function and the caller as

Result from function  [[-0.05272821 -0.0053777 ]
 [-0.41784501  0.02318533]
 [ 0.44103981 -0.05173443]
 [-0.4346603   0.02583883]
 [ 0.46492926 -0.03837702]]

Result in caller  [[ 1.41032176e-08 -2.77016590e-07]
 [-5.57580582e-09  1.01076087e-06]
 [ 5.33546625e-08 -8.56518529e-07]
 [ 3.96530548e-08 -1.49919655e-06]
 [-3.67771149e-08  7.18771000e-07]]

I have verified the return types and shapes of the result array and they are both float64. Any ideas why this should happen?

@esc esc added the needtriage label Nov 26, 2019
@esc
Copy link
Member

esc commented Nov 26, 2019

@sjster thanks for reporting this on the Numba issue tracker. It is not possible to diagnose or reproduce this w/o having access to the code. Maybe you could construct a minimal reproducer which you can submit that highlights the issue? In any case, just guessing from the numbers in the result in the caller it might be that you are seeing is an uninitialized memory region, for example created with np.empty.?

@sjster
Copy link
Author

sjster commented Nov 26, 2019

I can put a self-contained code in a repository in GitHub and give you access. Would that work?

@sjster
Copy link
Author

sjster commented Nov 26, 2019

You can access the code here https://github.com/sjster/Numba/blob/master/numba_test.py

You run it as python numba_test.py and in the output there are two relevant sections.

  1. The output matrix under 'Result from function' (A)differs from the
  2. output matrix under 'Result in calling function' (B).

A is correct however B is not and as you suggested probably a Numpy initialized location. The function that is accelerated by Numba is named 'numba_svd_derivative '(A) and the calling function is named 'compute_svd_derivative' (B).

Please let me know if I can provide any further information.

@esc
Copy link
Member

esc commented Nov 27, 2019

@sjster thanks for submitting this, it should help to figure out what is going on. Since the code seems quite involved, it may take some time to get to the bottom of this, however.

@stuartarchibald
Copy link
Contributor

This line is likely the problem:
https://github.com/sjster/Numba/blob/master/numba_test.py#L174
it should read:

result[:, :] = res_elem1 + res_elem2

such that the value from the addition is assigned back into the memory allocated in the result array. At present result = <new memory> is probably just making the underlying pointer point to somewhere it shouldn't be, hence no update and weird values.

@stuartarchibald stuartarchibald added no action required No action was needed to resolve. and removed needtriage labels Nov 27, 2019
@sjster
Copy link
Author

sjster commented Nov 28, 2019

Thanks for the pointer and the explanation, this fixed it!

@esc
Copy link
Member

esc commented Nov 28, 2019

@sjster thanks for confirming, will close the issue now as I believe it is resolved. If you encounter any Numba snags in future please don't hesitate to open an issue! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no action required No action was needed to resolve.
Projects
None yet
Development

No branches or pull requests

3 participants