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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

The unexpected behavior of sort() #129298

Open
hyperkai opened this issue Jun 22, 2024 · 1 comment
Open

The unexpected behavior of sort() #129298

hyperkai opened this issue Jun 22, 2024 · 1 comment
Labels
actionable module: docs Related to our documentation, both in docs/ and docblocks module: python frontend For issues relating to PyTorch's Python frontend triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@hyperkai
Copy link

hyperkai commented Jun 22, 2024

馃悰 Describe the bug

sort() works with 4 argument keywords as shown below:

import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.sort(input=my_tensor, dim=0, descending=False, stable=False) # Works
# torch.return_types.sort(
# values=tensor([-5, -3, -3,  0,  1,  7,  7,  9]),
# indices=tensor([2, 5, 7, 6, 1, 0, 3, 4]))

But sort() doesn't work without 4 argument keywords as shown below:

import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.sort(my_tensor, 0, False, False) # Error
TypeError: sort() received an invalid combination of arguments - got (Tensor, int, bool, bool), but expected one of:
 * (Tensor input, *, bool stable, int dim, bool descending, tuple of Tensors out)
 * (Tensor input, int dim, bool descending, *, tuple of Tensors out)
 * (Tensor input, *, bool stable, name dim, bool descending, tuple of Tensors out)
 * (Tensor input, name dim, bool descending, *, tuple of Tensors out)

In addition, sort() works without 2 or 3 argument keywords as shown below:

import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.sort(my_tensor, 0) # Works
torch.sort(my_tensor, 0, False) # Works
# torch.return_types.sort(
# values=tensor([-5, -3, -3,  0,  1,  7,  7,  9]),
# indices=tensor([2, 5, 7, 6, 1, 0, 3, 4]))

Versions

import torch

my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3])

torch.__version__ # 2.3.0+cu121

cc @svekars @brycebortree @albanD

@malfet malfet added module: python frontend For issues relating to PyTorch's Python frontend module: docs Related to our documentation, both in docs/ and docblocks triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module actionable labels Jun 24, 2024
@malfet
Copy link
Contributor

malfet commented Jun 24, 2024

Hmm, it works as expected according to the posted function signatures (note that arguments after * are keyword only arguments but I guess it could've been documented better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable module: docs Related to our documentation, both in docs/ and docblocks module: python frontend For issues relating to PyTorch's Python frontend triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

2 participants