... in general, the actual implementation will vary with data type.

From the numpy docs. Since PyTorch tensors are 32bit float precision by default, and numpy arrays 64 bit, the behaviour is not guaranteed to be consistent.

Answer from End genocide - save Gaza on Stack Overflow
🌐
PyTorch
docs.pytorch.org › reference api › torch.msort
torch.msort — PyTorch 2.10 documentation
January 1, 2023 - Sorts the elements of the input tensor along its first dimension in ascending order by value · torch.msort(t) is equivalent to torch.sort(t, dim=0)[0]. See also torch.sort()
🌐
PyTorch
docs.pytorch.org › reference api › torch.argsort
torch.argsort — PyTorch 2.11 documentation
January 1, 2023 - torch.argsort(input, dim=-1, descending=False, *, stable=False) → Tensor# Returns the indices that sort a tensor along a given dimension in ascending order by value. This is the second value returned by torch.sort(). See its documentation ...
Discussions

python - Difference between numpy.argsort and torch.argsort - Stack Overflow
I have checked argsort function of pytorch and numpy are same by definition. However, when I use those functions along different size of random tensor(or ndarray), It makes difference. I have no id... More on stackoverflow.com
🌐 stackoverflow.com
How torch.argsort works?
i believe this function sometime is confusing haha, pls re-read the doc again : Returns the indices that sort a tensor along a given dimension in ascending order by value. it returns the sorted indices of your original tensor values indices of the element in sorted order. So, 2, 0, 1 here are the original indices for tensors 3, 5, and 9 in your declared a 5 -> index 0 9 -> index 1 3 -> index 2 asc order: 2, 0, 1 More on reddit.com
🌐 r/pytorch
4
8
October 8, 2022
`torch.cum{min,max}, torch.sort, argsort` do not check the `dim` when the input is 0-d tensor
🐛 Describe the bug torch.cummin, torch.cummax, torch.sort and torch.argsort do not check the dim when the input is 0-d tensor import torch input = torch.rand([], dtype=torch.float64) dim = 100 torc... More on github.com
🌐 github.com
3
January 19, 2022
Stable torch.sort and torch.argsort
🚀 Feature Add stable version of torch.sort and torch.argsort. Stable sort algorithms sort repeated elements in the same order that they appear in the input. Motivation In some applications we need ... More on github.com
🌐 github.com
15
May 18, 2020
🌐
GitHub
github.com › pytorch › pytorch › issues › 112710
Discrepancy in Behavior of torch.argmin+torch.argsort Between Eager Execution and 'torch.compiled' Optimized Mode · Issue #112710 · pytorch/pytorch
November 2, 2023 - import torch import torch.nn as nn import traceback def forward(x, device): x = torch.argmin(input=x, dim=0,keepdim=True) x = torch.argsort(input=x, descending=True,dim=1,stable=False) return x input_tensor = torch.rand([10, 9], dtype=torch.float32).to('cpu') cuda_tensor = input_tensor.clone().to('cuda') no_op_info = forward(input_tensor, 'cpu') print("build succeded") op_info = torch.compile(forward, mode='max-autotune',fullgraph=False,dynamic=True)(cuda_tensor, 'cuda') same_val = torch.allclose(no_op_info.to('cpu'), op_info.to('cpu'), rtol=1e-3, atol=1e-3, equal_nan=True) if same_val == False : print("BUGBUG DIFFERENTIAL") raise ValueError('diff value') else : print("no_error")
Author   zoux1a
🌐
DEV Community
dev.to › hyperkai › sort-argsort-and-msort-in-pytorch-f58
sort, argsort and msort in PyTorch - DEV Community
November 5, 2024 - import torch my_tensor = torch.tensor([7, 1, -5, 7, 9, -3, 0, -3]) torch.argsort(input=my_tensor) my_tensor.argsort() torch.argsort(input=my_tensor, dim=0, descending=False, stable=False) torch.argsort(input=my_tensor, dim=-1, descending=False, stable=False) torch.argsort(input=my_tensor, dim=0, descending=False, stable=True) torch.argsort(input=my_tensor, dim=-1, descending=False, stable=True) # tensor([2, 5, 7, 6, 1, 0, 3, 4]) torch.argsort(input=my_tensor, dim=0, descending=True, stable=False) torch.argsort(input=my_tensor, dim=-1, descending=True, stable=False) # tensor([4, 0, 3, 1, 6, 5,
🌐
GitHub
github.com › pytorch › pytorch › issues › 71477
`torch.cum{min,max}, torch.sort, argsort` do not check the `dim` when the input is 0-d tensor · Issue #71477 · pytorch/pytorch
January 19, 2022 - torch.cummin, torch.cummax, torch.sort and torch.argsort do not check the dim when the input is 0-d tensor ·
Author   TestSomething22
Find elsewhere
🌐
Glaringlee
glaringlee.github.io › generated › torch.argsort.html
torch.argsort — PyTorch master documentation
torch.argsort · Shortcuts · torch.argsort(input, dim=-1, descending=False) → LongTensor¶ · Returns the indices that sort a tensor along a given dimension in ascending order by value. This is the second value returned by torch.sort(). See its documentation for the exact semantics of this ...
🌐
GitHub
github.com › pytorch › pytorch › issues › 38681
Stable torch.sort and torch.argsort · Issue #38681 · pytorch/pytorch
May 18, 2020 - def stable_argsort(arr, dim=-1, descending=False): arr_np = arr.detach().cpu().numpy() if descending: indices = np.argsort(-arr_np, axis=dim, kind='stable') else: indices = np.argsort(arr_np, axis=dim, kind='stable') return torch.from_numpy(indices).long().to(arr.device)
Published   May 18, 2020
Author   agadetsky
🌐
GitHub
github.com › pytorch › pytorch › issues › 33412
argsort not found when ONNX exporting [JIT] · Issue #33412 · pytorch/pytorch
February 17, 2020 - import torch from torch import nn import numpy as np class Demo(torch.nn.Module): def __init__(self): super().__init__() def forward(self, x): v, inds = x.sort(descending=True) # inds = x.argsort(descending=True) return inds if __name__ == "__main__": input_tensor = torch.range(20, 80) demo = Demo() out = demo(input_tensor) torch.onnx.export(demo, input_tensor, "debug.onnx", verbose=True, input_names=['data'], opset_version=11, do_constant_folding=True, dynamic_axes={'data':{0:'batch'}})
Author   Godricly
🌐
PyTorch Forums
discuss.pytorch.org › t › using-torch-argsort-in-cnn › 182607
Using torch.argsort in CNN - PyTorch Forums
June 21, 2023 - Output of my CNN network is log probabilities. I want to get 10 best indices of the output and further use the indices to get loss. Hence, I have to use torch.argsort. But using torch.argsort breaks gradients. Can anyon…
🌐
PyTorch
docs.pytorch.org › reference api › torch.tensor › torch.tensor.sort
torch.Tensor.sort — PyTorch 2.10 documentation
January 1, 2023 - See torch.sort() On this page · Show Source · PyTorch Libraries · torchao · torchrec · torchft · TorchCodec · torchvision · ExecuTorch · PyTorch on XLA Devices · Access comprehensive developer documentation for PyTorch View Docs · Get in-depth tutorials for beginners and advanced ...
🌐
GitHub
github.com › pytorch › pytorch › issues › 120874
The result of torch.argsort is inconsistent between CPU and GPU · Issue #120874 · pytorch/pytorch
February 29, 2024 - import torch input_data = torch.tensor([3, 1, 4, 1, 5, 9, 2, 6, 5]) # CPU sorted_indices = torch.argsort(input_data) print(sorted_indices) # GPU input_data = input_data.cuda() sorted_indices = torch.argsort(input_data) print(sorted_indices)
Author   vwrewsge
🌐
GitHub
github.com › pytorch › pytorch › issues › 24158
Documentation for torch.argsort includes an `out` argument · Issue #24158 · pytorch/pytorch
August 11, 2019 - Documentation for torch.argsort includes an out argument#24158 · Copy link · Labels · enhancementNot as big of a feature, but technically not a bug. Should be easy to fixNot as big of a feature, but technically not a bug.
Author   calincru
🌐
MindSpore
mindspore.cn › docs › migration_guide › en › r1.6 › api_mapping › pytorch_diff › Sort.html
Function Differences with torch.argsort | MindSpore 1.6 documentation | MindSpore
import numpy as np import torch import mindspore.ops as ops from mindspore import Tensor, Parameter from mindspore import dtype as mstype # MindSpore x = Tensor(np.array([[8, 2, 1], [5, 9, 3], [4, 6, 7]]), mstype.float16) sort = ops.Sort() output = sort(x) print(output) # Out: # (Tensor(shape=[3, 3], dtype=Float16, value= # [[ 1.0000e+00, 2.0000e+00, 8.0000e+00], # [ 3.0000e+00, 5.0000e+00, 9.0000e+00], # [ 4.0000e+00, 6.0000e+00, 7.0000e+00]]), Tensor(shape=[3, 3], dtype=Int32, value= # [[2, 1, 0], # [2, 0, 1], # [0, 1, 2]])) # Pytorch a = torch.tensor([[8, 2, 1], [5, 9, 3], [4, 6, 7]], dtype=torch.int8) torch.argsort(a, dim=1) # Out: # tensor([[2, 1, 0], # [2, 0, 1], # [0, 1, 2]])
🌐
GitHub
github.com › pytorch › pytorch › issues › 15764
torch.argsort descends wrongly · Issue #15764 · pytorch/pytorch
January 6, 2019 - get_top = torch.argsort(similarity, dim=1) [20485, 30807, 27706, ..., 117, 6, 1], [29628, 16835, 23989, ..., 6773, 10377, 2], ..., [30120, 23853, 24914, ..., 118, 50, 47], [23528, 23663, 25283, ..., 78, 77, 48], [31738, 17062, 29731, ..., 1240, 79, 49]
Author   vergilus
🌐
PyTorch
docs.pytorch.org › function at::argsort(const at::tensor&, at::dimname, bool)
Function at::argsort(const at::Tensor&, at::Dimname, bool) — PyTorch main documentation
January 1, 2023 - inline at::Tensor at::argsort(const at::Tensor &self, at::Dimname dim, bool descending = false)# On this page · Show Source · PyTorch Libraries · ExecuTorch · Helion · torchao · kineto · torchtitan · TorchRL · torchvision · torchaudio · tensordict ·