Skip to content

Fix torch.argmax and torch.argmin without dim - #2923

Open
sohailshk wants to merge 1 commit into
apple:mainfrom
sohailshk:fix/argmax-argmin-no-dim
Open

sohailshk wants to merge 1 commit into
apple:mainfrom
sohailshk:fix/argmax-argmin-no-dim

Conversation

@sohailshk

Copy link
Copy Markdown

torch.argmax(x) and torch.argmin(x) without dim return the index into the flattened input (a scalar, or a tensor of ones with keepdim=True). The converter reduced over the last axis instead, so for a (2, 5, 7) input it produced a (2, 5) output where torch returns a scalar. For a (1, N) input the result happens to match, which is why this goes unnoticed for single-sample logits.

This flattens the input before reducing when dim is None, and reshapes to all-ones when keepdim is set.

Checks run (Mac, Core ML predictions vs torch, CPU_ONLY):

  • New test_argmax_without_dim / test_argmin_without_dim (rank 1-3, keepdim True/False, float32 and int32 input, TorchScript and torch.export, neuralnetwork and mlprogram): 64 of the 96 new cases fail on main (the rank-1 cases already pass), all pass with this change.
  • -k "TestArgmax or TestArgmin": 288 passed.

Without dim, torch returns the index into the flattened input (a scalar,
or all-ones shape with keepdim). The converter reduced over the last axis
instead, so a (2, 5, 7) input gave a (2, 5) output.

This branch has not been deployed

No deployments
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.

1 participant