triton.language.sort

目录

triton.language.sort#

triton.language.sort(x, dim: constexpr = None, descending: constexpr = constexpr[0])#

Sorts the values in x along the given dimension.

参数:
  • x -- the input tensor to be sorted.

  • dim -- the dimension along which to sort. If None, the last dimension is used.

  • descending -- whether to sort in descending order. Default is False.

示例

import triton
import pytest
import torch
import triton.language as tl


@triton.jit
def sort_kernel_1d(X, Z, M: tl.constexpr, descending: tl.constexpr):
    off = tl.arange(0, M)
    x = tl.load(X + off)
    x = tl.sort(x, descending=descending, dim=0)
    tl.store(Z + off, x)


def test_sort_1d():
    shape = (2, )
    descending = True
    x = torch.randint(size=shape, low=0, high=2000, dtype=torch.int16).npu()
    triton_res = torch.empty_like(x)

    torch_res = torch.sort(x, descending=descending)[0]
    M = x.shape[0]
    sort_kernel_1d[(1, )](x, triton_res, M, descending)
    assert (torch_res == triton_res).all(), (torch_res, triton_res)


if __name__ == '__main__':
    test_sort_1d()

数据类型支持

uint8

int8

uint16

int16

uint32

int32

uint64

int64

fp16

fp32

fp64

bf16

fp8e(e4m3)

fp8e5(e5m2)

bool

Atlas A2 products/Atlas A3 products

×

√

×

√

×

×

×

×

√

√

×

√

×

×

×

Ascend 950PR&950DT products

×

√

×

√

×

√

×

√

√

√

×

√

√

√

√

特殊说明

  • Note: tl.sort may have precision problems in big shape or multidimensional shape, more recommendeded to use triton.language.extra.cann.extension.sort.