Skip to content

Missing a boundary check for sorter argument in dpctl.tensor.searchsorted #1599

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

Closed
antonwolfy opened this issue Mar 20, 2024 · 1 comment · Fixed by #1601
Closed

Missing a boundary check for sorter argument in dpctl.tensor.searchsorted #1599

antonwolfy opened this issue Mar 20, 2024 · 1 comment · Fixed by #1601

Comments

@antonwolfy
Copy link
Collaborator

antonwolfy commented Mar 20, 2024

In below example sorter has an index which is out of the range of input array a, but no validation exception raised by dpctl:

a = dpt.asarray([5, 2, 1, 3, 4])
v = dpt.asarray(4)
sorter = dpt.asarray([0, 1, 2, 3, 5])

dpt.searchsorted(a, v, sorter=sorter)
# Out: usm_ndarray(4)

a = numpy.asarray([5, 2, 1, 3, 4])
v = numpy.asarray(4)
sorter = numpy.asarray([0, 1, 2, 3, 5])
numpy.searchsorted(a, v, sorter=sorter)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[10], line 1
----> 1 numpy.searchsorted(a, v, sorter=sorter)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/numpy/core/fromnumeric.py:1400, in searchsorted(a, v, side, sorter)
   1332 @array_function_dispatch(_searchsorted_dispatcher)
   1333 def searchsorted(a, v, side='left', sorter=None):
   1334     """
   1335     Find indices where elements should be inserted to maintain order.
   1336
   (...)
   1398
   1399     """
-> 1400     return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)

File ~/miniconda3/envs/dpnp_dev/lib/python3.9/site-packages/numpy/core/fromnumeric.py:59, in _wrapfunc(obj, method, *args, **kwds)
     56     return _wrapit(obj, method, *args, **kwds)
     58 try:
---> 59     return bound(*args, **kwds)
     60 except TypeError:
     61     # A TypeError occurs if the object does have such a method in its
     62     # class, but its signature is not identical to that of NumPy's. This
   (...)
     66     # Call _wrapit from within the except clause to ensure a potential
     67     # exception has a traceback chain.
     68     return _wrapit(obj, method, *args, **kwds)

ValueError: Sorter index out of range.
@oleksandr-pavlyk
Copy link
Contributor

Yes, we use default index wrap mode. Perhaps this needs to be explicitly documented, but it is done for performance reasons. It is consistent with behavior of __getitem__ and of dpctl.tensor.take.

dpnp can use dpt.min, dpt.max to implement its own validation, if deemed necessary.

I will expand the documentation string.

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 a pull request may close this issue.

2 participants