-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[Triton] Add top-k operation to Triton language #5706
base: main
Are you sure you want to change the base?
Conversation
Add a top-k operator to the Triton language. This operator uses the sort function from triton.language to process an input tensor and a specified number of top elements, k. It returns a tuple containing the top-k elements and their corresponding original indices.
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.
IMO, we could add topk but I'm unsure if we should adopt this approach considering it's just a simple wrapper around sort
:type k: int | ||
:param descending: If True (default), returns the largest elements. If False, returns the smallest. | ||
:type descending: bool | ||
:return: A tuple of (top-k elements, top-k indices) tensors. |
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's not true. What have been returned are still length n but not length k. It's not expected
@@ -1,4 +1,4 @@ | |||
default_stages: [pre-commit, pre-push, manual] | |||
default_stages: [pre-commit, commit-msg, manual, pre-merge-commit, post-checkout, post-commit, post-merge, post-rewrite, prepare-commit-msg] |
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.
All those are unnecessary changes
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.
If needed, we should use a separate PR
A possible solution would be tl.sort + tl.gather (1...k) IMO |
+1, there are multiple way to implement topk and it is unlikely that this is the most efficient way. Maybe there is a place for this code in https://github.com/triton-lang/kernels as an example although the charter for this repo is still work in progress. |
Add a top-k operator to the Triton language. This operator uses the sort function from triton.language to process an input tensor and a specified number of top elements, k. It returns a tuple containing the top-k elements and their corresponding original indices.
New contributor declaration
I am not making a trivial change, such as fixing a typo in a comment.
I have written a PR description following these
rules.
I have run
pre-commit run --from-ref origin/main --to-ref HEAD
.Select one of the following.
/test
forlit
tests/unittest
for C++ tests/python/test
for end-to-end testsFILL THIS IN
.Select one of the following.
lit
tests.lit
tests I have added follow these best practices,including the "tests should be minimal" section. (Usually running Python code
and using the instructions it generates is not minimal.)