Skip to content

Slicing bug gh 1135 #1136

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

Merged
merged 2 commits into from
Mar 24, 2023
Merged

Slicing bug gh 1135 #1136

merged 2 commits into from
Mar 24, 2023

Conversation

oleksandr-pavlyk
Copy link
Contributor

Closes gh-1135.

Handle x[True] and x[False] as NumPy does, even though the behavior
may be undocumented. NumPy treats True as None (insert axis with size 1),
and treats False as None followed by empty slicing (insert axis with size 0).

Changed the logic of _basic_slice_meta utility function to correctly handle
boolean scalars (surprisingly, insinstance(True, int) evaluates to True).
0d arrays are handled on the same footing as the Python scalars.

Introduced _is_integral and _is_boolean utilty functions and used them in
_basic_slice_meta utility.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?

Handle x[True] and x[False] as NumPy does, even though the behavior
may be undocumented. NumPy treats True as None (insert axis with size 1),
and treats False as None followed by empty slicing (insert axis with size 0).

Changed the logic of _basic_slice_meta utility function to correctly handle
boolean scalars (surprisingly, `insinstance(True, int)` evaluates to `True`).
0d arrays are handled by Python scalars.

Introduced _is_integral and _is_boolean utilty functions and used them in
`_basic_slice_meta` utility.
@github-actions
Copy link

@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_21 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

@antonwolfy
Copy link
Collaborator

The below example works differently with numpy:

import numpy
import dpctl.tensor as dpt

numpy.ones((3, 2))[True, False].shape
Out: (0, 3, 2)

dpt.ones((3, 2))[True, False].shape
Out: (1, 0, 3, 2)

@oleksandr-pavlyk
Copy link
Contributor Author

Actually this behavior in NumPy is undocumented and we should not rely on it: https://stackoverflow.com/questions/75828008/docs-about-boolean-scalars-in-indexing-of-numpy-array

I will change the implementation to raise IndexError for boolean scalar indexing.

@antonwolfy
Copy link
Collaborator

Looks it was answered here, referred from numpy issue #9515, and stated as expected behavior intended for 0-dimensional boolean index in numpy.

@oleksandr-pavlyk
Copy link
Contributor Author

Ok, in that case, I am inclined to leave things as is. The behavior of np.nonzero on non-zero array is slated to change, so this behavior will change in NumPy too.

@oleksandr-pavlyk oleksandr-pavlyk merged commit 3a22dd9 into master Mar 24, 2023
@github-actions
Copy link

Deleted rendered PR docs from intelpython.github.com/dpctl, latest should be updated shortly. 🤞

@oleksandr-pavlyk oleksandr-pavlyk deleted the slicing-bug-gh-1135 branch March 24, 2023 14:27
@github-actions
Copy link

Array API standard conformance tests for dpctl=0.14.3dev0=py310h76be34b_21 ran successfully.
Passed: 46
Failed: 788
Skipped: 280

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

Successfully merging this pull request may close these issues.

Using of Boolean scalar for slicing usm_ndarray needs work
2 participants