Skip to content

[torchax] Fixes test for kthvalue pytorch/xla#7458 #9223

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions torchax/test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"histogram", # hard op: AssertionError: Tensor-likes are not close!
"histogramdd", # TypeError: histogram requires ndarray or scalar arguments, got <class 'list'> at position 1.
"index_reduce",
"kthvalue",
"linalg.ldl_solve",
"max_pool2d_with_indices_backward",
"nn.functional.adaptive_max_pool1d",
Expand Down Expand Up @@ -176,7 +175,7 @@ def run_export_and_compare(testcase,
# Sort related ops should ignore index;
# For example: sort( [1, 0, 0]) -> [0, 0, 1]
# the correct index can be [1, 2, 0] or [2, 1, 0]
should_ignore_indexes = {"topk", "mode"}
should_ignore_indexes = {"topk", "mode", "kthvalue"}


class TestOpInfo(TestCase):
Expand Down
17 changes: 17 additions & 0 deletions torchax/torchax/ops/jaten.py
Original file line number Diff line number Diff line change
Expand Up @@ -5452,3 +5452,20 @@ def linear(input, weight, bias=None):
if bias is not None:
res += bias
return res


@op(torch.ops.aten.kthvalue)
def kthvalue(input, k, dim=None, keepdim=False, *, out=None):
if input.ndim == 0:
return input, jnp.array(0)
dimension = -1
if dim is not None:
dimension = dim
while dimension < 0:
dimension = dimension + input.ndim
values = jax.lax.index_in_dim(
jnp.partition(input, k - 1, dimension), k - 1, dimension, keepdim)
indices = jax.lax.index_in_dim(
jnp.argpartition(input, k - 1, dimension).astype('int64'), k - 1,
dimension, keepdim)
return values, indices