ttnn.sort

ttnn.sort(input_tensor: ttnn.Tensor, dim: int = -1, descending: bool = False, stable: bool = False, *, memory_config: ttnn.MemoryConfig = None, out: Tuple[ttnn.Tensor, ttnn.Tensor] = None) → List[ttnn.Tensor]

Sorts the elements of the input tensor along the specified dimension in ascending order by default. If no dimension is specified, the last dimension of the input tensor is used.

Parameters:
  • input_tensor (ttnn.Tensor)The input tensor to be sorted.

  • dim (int, optional)The dimension along which to sort. Defaults to -1 (last dimension).

  • descending (bool, optional)If True, sorts in descending order. Defaults to False.

  • stable (bool, optional)If True, ensures the original order of equal elements is preserved. Defaults to False. With stable=False the returned indices are a valid permutation (no duplicates inside tie groups, no out-of-range indices), but which of several equal elements comes first is unspecified. Known exception: on the single-core path (sort dimension padded to at most 2048), an input containing the padding value (-inf descending / +inf ascending, or 0 / 65535 for uint16) can still emit padding indices past the logical row. float32 inputs have -0.0 canonicalized to +0.0 in the returned values with stable=True on every factory (the two zeros form one tie class, ordered by index as in PyTorch), and with stable=False on the cross-core path and on padded multi-core DRAM sorts; bit-exact consumers of signed zeros (copysign, 1/x, signbit) see +0.0 there.

Keyword Arguments:
  • memory_config (ttnn.MemoryConfig, optional)Specifies the memory configuration for the output tensor. Defaults to None.

  • out (Tuple[ttnn.Tensor, ttnn.Tensor], optional)Preallocated output tensors for the sorted values and indices. Defaults to None. The index tensor must be UINT32 for FLOAT32 and UINT16 inputs, and UINT16 or UINT32 for BFLOAT16 inputs.

Returns:

List[ttnn.Tensor]A list containing two tensors: The first tensor contains the sorted values, the second tensor contains the indices of the original elements in the sorted order.

Note

Supported dtypes and layouts for input tensor values:

Dtypes

Layouts

BFLOAT16

TILE, ROW_MAJOR

UINT16

TILE, ROW_MAJOR

FLOAT32

TILE, ROW_MAJOR

Supported dtypes and layouts for index tensor values:

Dtypes

Layouts

UINT16, UINT32

TILE, ROW_MAJOR

NaN input is unsupported (undefined ordering). The bfloat16 datapath canonicalizes NaN to same-sign infinity before comparing, and FLOAT32 orders NaN by sign, so NaN placement deviates from torch.sort, which treats NaN as larger than any number. Mask or replace NaNs before sorting.

Memory Support:
  • Interleaved: DRAM and L1

  • Sharded: HEIGHT_SHARDED and WIDTH_SHARDED in DRAM and L1; BLOCK_SHARDED in L1 only

Example

# Create a tensor
input_tensor = torch.Tensor([[3, 1, 2], [3, 1, 2]])

# Convert tensor to ttnn format
input_tensor_ttnn = ttnn.from_torch(input_tensor, dtype=ttnn.bfloat16, layout=ttnn.Layout.TILE, device=device)

# Sort the tensor in ascending order
sorted_tensor, indices = ttnn.sort(input_tensor_ttnn)
logger.info(f"Sorted Tensor: {sorted_tensor}, Indices: {indices}")

# Sort the tensor in descending order
sorted_tensor_desc, indices_desc = ttnn.sort(input_tensor_ttnn, descending=True)
logger.info(f"Sorted Tensor Descending: {sorted_tensor_desc}, Indices: {indices_desc}")

# Sort along a specific dimension
input_tensor_2d = torch.Tensor([[3, 1, 2], [6, 5, 4]])
input_tensor_2d_ttnn = ttnn.from_torch(input_tensor_2d, dtype=ttnn.bfloat16, layout=ttnn.Layout.TILE, device=device)
sorted_tensor_dim, indices_dim = ttnn.sort(input_tensor_2d_ttnn, dim=1)
logger.info(f"Sorted Tensor along dim 1: {sorted_tensor_dim}, Indices: {indices_dim}")