-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtopk.py
More file actions
39 lines (34 loc) · 1.09 KB
/
topk.py
File metadata and controls
39 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import infinicore
from infinicore.lib import _infinicore
from infinicore.tensor import Tensor
def topk(
input: Tensor,
k: int,
dim: int = -1,
largest: bool = True,
sorted: bool = True,
*,
out=None,
):
r"""Returns the k largest elements of the given input tensor along a given dimension."""
if infinicore.use_ntops and input.device.type in ("cuda", "musa"):
return infinicore.ntops.torch.topk(input, k, dim, largest, sorted, out=out)
if out is None:
res_values, res_indices = _infinicore.topk(
input._underlying, k, dim, largest, sorted
)
return Tensor(res_values), Tensor(res_indices)
else:
if not isinstance(out, (tuple, list)) or len(out) != 2:
raise ValueError("out argument must be a tuple of (values, indices)")
out_values, out_indices = out
_infinicore.topk_(
input._underlying,
out_values._underlying,
out_indices._underlying,
k,
dim,
largest,
sorted,
)
return out_values, out_indices