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

No out of bounds check during advanced array indexing #8127

Closed
kc611 opened this issue Jun 2, 2022 · 5 comments · Fixed by #8247
Closed

No out of bounds check during advanced array indexing #8127

kc611 opened this issue Jun 2, 2022 · 5 comments · Fixed by #8247
Labels
feature_request good first issue A good issue for a first time contributor

Comments

@kc611
Copy link
Collaborator

kc611 commented Jun 2, 2022

Numba ignores any indices that are out of bounds for a given array shape. This behavior is present in both basic and advanced indexing.

import numpy as np
import numba

@numba.njit
def something():
    arr = np.ones((3,3))
    arr[100] = 0
    arr[:, np.array([2,3,5,100])] = 0
    return arr

print(something()) # Does not raise error, ignores the out of bound indices completely.
print(something.py_func()) # Raises out of bounds error

Not sure if we should be considering this as a bug or a feature :-)

Should be easily fixed by adding a bounds check during indexing during function runtime.

@esc
Copy link
Member

esc commented Jun 2, 2022

@kc611 thank you for reporting this. IIRC there is a keyword argument that can be passed to the njit decorator:

https://numba.readthedocs.io/en/stable/reference/pysemantics.html?highlight=boundscheck#bounds-checking

Is this perhaps what you are looking for?

@kc611
Copy link
Collaborator Author

kc611 commented Jun 2, 2022

Thank you that was exactly what I was looking for.

@kc611 kc611 closed this as completed Jun 2, 2022
@esc esc added the no action required No action was needed to resolve. label Jun 2, 2022
@kc611
Copy link
Collaborator Author

kc611 commented Jun 2, 2022

So, uh it does work for basic indexing, same cannot be said for advanced indexing:

import numpy as np
import numba

@numba.njit(boundscheck=True)
def something():
    arr = np.ones((3,3))
    arr[:, np.array([2,3,5,100])] = 0
    return arr

print(something()) # Does not raise error, ignores the out of bound indices completely.
print(something.py_func()) # Raises out of bounds error

Note: The planning for a new implementation of advanced indexing is already underway, so this needs to handled/labelled correctly such that nobody takes this up till the respective PR is made.

@kc611 kc611 reopened this Jun 2, 2022
@kc611 kc611 removed the no action required No action was needed to resolve. label Jun 2, 2022
@kc611 kc611 changed the title No out of bounds check during array indexing No out of bounds check during advanced array indexing Jun 2, 2022
@stuartarchibald
Copy link
Contributor

@kc611 suggest adding:

diff --git a/numba/np/arrayobj.py b/numba/np/arrayobj.py
index 850bd1d..5b1d48e 100644
--- a/numba/np/arrayobj.py
+++ b/numba/np/arrayobj.py
@@ -1617,7 +1617,8 @@ def fancy_setslice(context, builder, sig, args, index_types, indices):
     dest_ptr = cgutils.get_item_pointer2(context, builder, dest_data,
                                          dest_shapes, dest_strides,
                                          aryty.layout, dest_indices,
-                                         wraparound=False)
+                                         wraparound=False,
+                                         boundscheck=context.enable_boundscheck)
     store_item(context, builder, aryty, val, dest_ptr)

see if that helps?

@kc611
Copy link
Collaborator Author

kc611 commented Jun 2, 2022

Marking this as a good first issue for any newcomer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature_request good first issue A good issue for a first time contributor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants