-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
WIP: top_k draft implementation #26666
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
Open
JuliaPoo
wants to merge
15
commits into
numpy:main
Choose a base branch
from
JuliaPoo:issue-15128-topk-feat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
5984fe6
WIP: top_k draft implementation
JuliaPoo d724d21
Fix lint errors
JuliaPoo e8f7403
Fix lint errors
JuliaPoo 11e11d1
DOC: Added notes to np.top_k
JuliaPoo 6620311
DOC: Modified notes to np.top_k
JuliaPoo 7322015
DOC: Modified notes to np.top_k
JuliaPoo 7008981
DOC: add notes about sort order of nan to top_k
JuliaPoo c15dfbf
Merge branch 'main' into issue-15128-topk-feat
JuliaPoo 5b6e153
Update submodules from merge
JuliaPoo d2f3d39
DOC: Add release note for top_k
JuliaPoo 85147d6
DOC: Add release note for top_k
JuliaPoo a4e501f
Merge branch 'issue-15128-topk-feat' of https://github.com/JuliaPoo/n…
JuliaPoo fed3e6a
DOC: update top_k docs to pass doctest
JuliaPoo 8d52223
Update submodules
JuliaPoo 54fbc56
np.top_k push nans to the back for floating and complex
JuliaPoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
WIP: top_k draft implementation
Following previous discussion at #15128. I made a small change to the interface in the previous discussion by changing the `mode` keyword into a `largest` bool flag. This follows API such as from [torch.topk](https://pytorch.org/docs/stable/generated/torch.topk.html). Carrying from the previous discussion, a parameter might be useful is `sorted`. This is also implemented in `torch.topk`, and follows from previous work at #19117. Co-authored-by: quarrying
- Loading branch information
commit 5984fe6ffb867fc65208627fffe252c55d2d5e5f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful to explicitly note the semantics in the presence of NaN values. Is this the same as
sort(a)[:k]
/sort(a)[-k:]
, or it the same assort(a[~isnan(a)])[:k]
/sort(a[~isnan(a)])[-k:]
?Also: does the API make any guarantees about the order of the returned results?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With regards to
np.nan
, from what I understand, the underlyingnp.argpartition
is not intentional in how it treatsnp.nan
. For floats by the nature of how the partial sort is implemented,np.nan
is unintentionally treated likenp.inf
since it fails for every comparison with a number. This might change in the future as the underlying implementation changes. Should I add a note that the treatment ofnp.nan
is not defined?About the order of the returned results,
np.argpartition
by default uses a partial sort which is unstable, so the returned indices is not guaranteed to be the first occurrence of the element. E.g.,np.top_k([3,3], 1)
returns(array([3]), array([1]))
. I'll add that as a note.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NumPy uses a sort order pushing NaNs to the end consistently. I don't think we should change that.
Now, there is a problem with respect to adding a kwarg to choose a descending sort (which you propose here for
top_k
). In that case it might be argued that NaNs should also be sorted to the end!And if we want that, it would require specific logic to sort in descending order (not just for unstable sorts).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I asked about the order of returned elements, what I had in mind was this:
As I see it, there are three logically-consistent conventions:
[3, 4]
[4, 3]
[3, 4]
or[4, 3]
It would be helpful to specify in the documentation which of these is the case for NumPy's implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the current implementation returns results that are always sorted. Which seems to me like it's the nicest option for the user. If that falls out naturally, then great. And for other implementations, matching that behavior doesn't seem too costly performance-wise even if their implementation doesn't yield sorted values naturally, because the returned arrays are typically very small compared to the input arrays, so sorting the end result is fast.
Does that sound right to everyone?
@JuliaPoo based on the implementation, do you see a reason that this is hard to guarantee?