Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

peymanbr
Copy link

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.

    • I have added tests.
      • /test for lit tests
      • /unittest for C++ tests
      • /python/test for end-to-end tests
    • This PR does not need a test because FILL THIS IN.
  • Select one of the following.

    • I have not added any lit tests.
    • The 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.)

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.
Copy link
Contributor

@Jokeren Jokeren left a 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.
Copy link
Contributor

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]
Copy link
Contributor

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

Copy link
Contributor

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

@Jokeren
Copy link
Contributor

Jokeren commented Jan 27, 2025

A possible solution would be tl.sort + tl.gather (1...k) IMO

@ThomasRaoux
Copy link
Collaborator

IMO, we could add topk but I'm unsure if we should adopt this approach considering it's just a simple wrapper around sort

+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.

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.

3 participants