code: def test_sort(self, device):
      # on CUDA 2048 vs >2048 have different code path for the dim being sorted
      for SIZE in (4, 2049):
          x = torch.rand(4, SIZE, device=device)
          res1val, res1ind = torch.sort(x)
  
          # Test inplace
          y = x.clone()
          y_inds = torch.tensor((), dtype=torch.int64, device=device)
          torch.sort(y, out=(y, y_inds))
          x_vals, x_inds = torch.sort(x)
          self.assertEqual(x_vals, y)
          self.assertEqual(x_inds, y_inds)
  
          # Test use of result tensor
          res2val = torch.tensor((), device=device)
          res2ind = torch.tensor((), device=device, dtype=torch.long)
          torch.sort(x, out=(res2val, res2ind))
          self.assertEqual(res1val, res2val, atol=0, rtol=0)
          self.assertEqual(res1ind, res2ind, atol=0, rtol=0)
          self.assertEqual(torch.argsort(x), res1ind)
          self.assertEqual(x.argsort(), res1ind)
  
          # Test sorting of random numbers
          self.assertIsOrdered(
              "ascending", x, res2val, res2ind, "random"
          )
  
          # Test simple sort
          self.assertEqual(
              torch.sort(
                  torch.tensor((50, 40, 30, 20, 10), device=device)
              )[0],
              torch.tensor((10, 20, 30, 40, 50), device=device),
              atol=0,
              rtol=0,
          )
  
          # Test that we still have proper sorting with duplicate keys
          x = torch.floor(torch.rand(4, SIZE, device=device) * 10)
          torch.sort(x, out=(res2val, res2ind))
          self.assertIsOrdered(
              "ascending",
              x,
              res2val,
              res2ind,
              "random with duplicate keys",
          )
  
          # DESCENDING SORT
          x = torch.rand(4, SIZE, device=device)
          res1val, res1ind = torch.sort(x, x.dim() - 1, True)
  
          # Test use of result tensor
          res2val = torch.tensor((), device=device)
          res2ind = torch.tensor((), device=device, dtype=torch.long)
          torch.sort(x, x.dim() - 1, True, out=(res2val, res2ind))
          self.assertEqual(res1val, res2val, atol=0, rtol=0)
          self.assertEqual(res1ind, res2ind, atol=0, rtol=0)
          self.assertEqual(torch.argsort(x, x.dim() - 1, True), res1ind)
          self.assertEqual(x.argsort(x.dim() - 1, True), res1ind)
  
          # Test sorting of random numbers
          self.assertIsOrdered(
              "descending", x, res2val, res2ind, "random"
          )
  
          # Test simple sort task
          self.assertEqual(
              torch.sort(
                  torch.tensor((10, 20, 30, 40, 50), device=device),
                  0,
                  True,
              )[0],
              torch.tensor((50, 40, 30, 20, 10), device=device),
              atol=0,
              rtol=0,
          )
  
          # Test that we still have proper sorting with duplicate keys
          self.assertIsOrdered(
              "descending",
              x,
              res2val,
              res2ind,
              "random with duplicate keys",
          )
  
          # Test argument sorting with and without stable
          x = torch.tensor([1, 10, 2, 2, 3, 7, 7, 8, 9, 9] * 3)
          self.assertEqual(
              torch.argsort(x, stable=True),
              torch.sort(x, stable=True).indices,
          )
          self.assertEqual(
              torch.argsort(x, stable=False),
              torch.sort(x, stable=False).indices,
          )
          self.assertEqual(torch.argsort(x), torch.sort(x).indices)
  
          # Test sorting with NaNs
          x = torch.rand(4, SIZE, device=device)
          x[1][2] = float("NaN")
          x[3][0] = float("NaN")
          torch.sort(x, out=(res2val, res2ind))
          self.assertIsOrdered(
              "ascending", x, res2val, res2ind, "random with NaNs"
          )
          torch.sort(x, out=(res2val, res2ind), descending=True)
          self.assertIsOrdered(
              "descending", x, res2val, res2ind, "random with NaNs"
          )
  
imports: import torch
  import numpy
  import random
  from torch import nan
  from itertools import permutations, product
  from torch.testing import make_tensor
  from torch.testing._internal.common_dtype import all_types, all_types_and, floating_types_and, integral_types
  from torch.testing._internal.common_utils import TestCase, run_tests, slowTest, skipIfTorchDynamo
  from torch.testing._internal.common_device_type import instantiate_device_type_tests, dtypes, onlyNativeDeviceTypes, onlyCUDA, dtypesIfCUDA, dtypesIfCPU, onlyCPU, largeTensorTest
label_desc: Use image node_id 2 for calling the TestSortAndSelect obj's underlying member method code with example usage: obj.test_sort(device) without return types