-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
peymanbr
wants to merge
1
commit into
triton-lang:main
Choose a base branch
from
peymanbr:topk
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.
+102
−3
Open
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,6 +147,8 @@ Scan/Sort Ops | |
histogram | ||
sort | ||
gather | ||
topk | ||
|
||
|
||
Atomic Ops | ||
---------- | ||
|
This file contains 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
from ..runtime.jit import jit | ||
from . import core | ||
from . import math | ||
import triton.language as tl | ||
|
||
# constexpr utilities | ||
|
||
|
@@ -452,3 +453,48 @@ def interleave(a, b): | |
# understand that if we take the `if` above we definitely don't run this | ||
# `else`. | ||
return core.reshape(c, c.shape[:-2] + [2 * c.shape[-2]]) | ||
|
||
|
||
# topk | ||
|
||
|
||
@core._tensor_member_fn | ||
@jit | ||
def topk(x, k: core.constexpr, descending: core.constexpr = core.CONSTEXPR_1): | ||
""" | ||
Returns the top-k elements and their indices along the last dimension. | ||
:param x: The input tensor. | ||
:type x: Tensor | ||
:param k: The number of top elements to return. | ||
: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 commentThe 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 |
||
:rtype: Tuple[Tensor, Tensor] | ||
""" | ||
core.static_assert(k > 0, "k must be greater than 0.") | ||
core.static_assert(k <= x.shape[-1], "k must not exceed the size of the last dimension.") | ||
# Verify that the shape of the input tensor satisfies Triton's requirements. | ||
core.static_assert(_is_power_of_two(x.shape[-1]), "Last dimension must be a power of 2.") | ||
|
||
# Sort the tensor along the last dimension. | ||
sorted_elements = tl.sort(x, dim=len(x.shape) - 1, descending=descending) | ||
|
||
# Create a range tensor to map the top-k elements back to their original indices. | ||
# n_outer represents the number of elements outside the last dimension being processed. | ||
n_outer: core.constexpr = x.numel // x.shape[-1] | ||
last_dim_size: core.constexpr = x.shape[-1] | ||
|
||
# Create a tensor for the original indices. | ||
original_indices = core.arange(0, last_dim_size) | ||
original_indices = core.reshape(original_indices, [1, last_dim_size]) | ||
original_indices = core.broadcast_to(original_indices, [n_outer, last_dim_size]) | ||
|
||
# Create a mask to keep only the first k elements and their indices. | ||
mask = original_indices < k | ||
|
||
# Apply the mask to the sorted elements and indices. | ||
topk_elements = sorted_elements * mask.to(sorted_elements.dtype) | ||
topk_indices = original_indices * mask.to(original_indices.dtype) | ||
|
||
return (topk_elements, topk_indices) |
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.
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