KEMBAR78
The unexpected behavior of `sort()` · Issue #129298 · pytorch/pytorch · GitHub
Skip to content

The unexpected behavior of sort() #129298

@hyperkai

Description

@hyperkai

🐛 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    actionablemodule: docsRelated to our documentation, both in docs/ and docblocksmodule: python frontendFor issues relating to PyTorch's Python frontendtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions