-
Notifications
You must be signed in to change notification settings - Fork 20
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
Implement the side
keyword argument as in numpy
#1
Comments
hi, could you check out the latest version of |
Hi, just checked. Here are a few comments about the code: In the pytest test here, why do we have separate B =[1, 100, 200]
A_val = [1, 50, 500]
V_val = [1, 12, 120]
side_val = ['left', 'right']
nrepeat = 100
@pytest.mark.parametrize('B,A,V,side', product(Ba_val, Bv_val, A_val, V_val, side_val))
def test_searchsorted_correct(B, A, V, side, device):
for test in range(nrepeat):
a = torch.sort(torch.rand(B, A, device=device), dim=1)[0]
v = torch.rand(B, V, device=device)
.... I see the function For testing the results in Regarding the implementation of If you run My guess is that some bug in the code for |
Hello I am having similar troubles with the side parameter. I am trying to get a minimal code for reproducing the bug but it seems the trouble is happening randomly. |
Thanks for your feedback concerning this:
This is because actually one from I however realize that the test code is indeed wrong, because it should be:
This is fixed now. Concerning the placement of this Now, concerning the tests:
what do you think ? |
ok, I found out the error in the endless loop: this was happening when the last element of |
Great ! Your last update solved the bug on my side. Thanks for the nice code 👍 |
no pb, sorry for this, I shouldn't have merged with master so early |
Well, I argue that we do care about what happens in case the value to insert is already present in the array. The whole point of the If the value is not preset in the array, so both sides give the same answer: >>> a = np.array([1, 2, 3, 5, 6, 7])
>>> v = 4
>>> np.searchsorted(a, v, side='left')
3
>>> np.searchsorted(a, v, side='right')
3 With >>> a = np.array([1, 2, 3, 3, 3, 3, 3, 3, 6, 7])
>>> v = 3
>>> np.searchsorted(a, v, side='left')
2 On the contrary, >>> a = np.array([1, 2, 3, 3, 3, 3, 3, 3, 6, 7])
>>> v = 3
>>> np.searchsorted(a, v, side='left')
8 Now, for my use case. I use searchsorted to bucketize a series of real-valued distances into bins of unequal size. E.g. the distance The bounds of these buckets are represented like this, with the last bucket being buckets = [0, 1, 5, 10, 20, 100] Therefore, for these input distances: dist = [0, .3, 4, 4.5, 5, 6.8, 23.4, 123, 401] I expect this output: idxs = [0, 0, 1, 1, 2, 2, 4, 5, 5] Which I can get by doing >>> np.searchsorted(buckets, dist, side='right') - 1
[0, 0, 1, 1, 2, 2, 4, 5, 5] While doing this would give the wrong index for items on the bucket boundaries: >>> np.searchsorted(buckets, dist, side='left')
[0, 1, 2, 2, 2, 3, 5, 6, 6] |
Ah about the broadcasting behavior for the batch dimension. I understand now what that test and the I can update the python code where needed, should be quite quick. Then some time next week I'll have a look at the C++ and CUDA parts to address the |
ha! thanks for the explanations! ok, feel free to update the code and make a new PR when ready |
I have a working version of this in my fork. I rewrote most of the search code, which should now be more readable and possibly faster. Do you want to check it out? I'll send a PR very soon, just want to write a benchmark script ;) |
ok, cool, thanks for this ! antoine |
The Numpy version of searchsorted takes an additional parameter to specify the semantic of the returned index:
The current implementation of
searchsorted
for Pytorch follows theleft
behavior, is it possible to implement theright
version to allow ordering from the right?The text was updated successfully, but these errors were encountered: