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

Fix torch.min and torch.mode (#7513) #8092

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
5 changes: 2 additions & 3 deletions experimental/torch_xla2/test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@
"lu_unpack",
"masked.median",
"max_pool2d_with_indices_backward",
"min",
"mode",
"multinomial",
"mvlgamma",
"nanmedian",
Expand Down Expand Up @@ -245,7 +243,8 @@ def run_export_and_compare(testcase,
# 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"
"topk",
"mode"
}


Expand Down
22 changes: 18 additions & 4 deletions experimental/torch_xla2/torch_xla2/ops/jaten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,25 @@ def reduce_fn(a, b):


@op(torch.ops.aten.min)
def _aten_min(x, axis=None):
if axis:
return jnp.min(x, axis=axis), jnp.argmin(x, axis=axis).astype(jnp.int64)
def _aten_min(x, dim=None, keepdim=False):
if dim is not None:
return _with_reduction_scalar(jnp.min, x, dim, keepdim), _with_reduction_scalar(jnp.argmin, x, dim, keepdim).astype(jnp.int64)
else:
return jnp.min(x, axis=axis)
return _with_reduction_scalar(jnp.min, x, dim, keepdim)


@op(torch.ops.aten.mode)
def _aten_mode(input, dim=-1, keepdim=False, *, out=None):
if input.ndim == 0: # single number
return input, jnp.array(0)
dim = (input.ndim + dim) % input.ndim # jnp.scipy.stats.mode does not accept -1 as dim
# keepdims must be True for accurate broadcasting
mode, _ = jax.scipy.stats.mode(input, axis=dim, keepdims=True)
mode_broadcast = jnp.broadcast_to(mode, input.shape)
if not keepdim:
mode = mode.squeeze(axis=dim)
indices = jnp.argmax(jnp.equal(mode_broadcast, input), axis=dim, keepdims=keepdim)
return mode, indices


@op(torch.ops.aten.amin)
Expand Down
Loading