triton.language.extra.cann.libdevice.ull2float_ru

triton.language.extra.cann.libdevice.ull2float_ru#

triton.language.extra.cann.libdevice.ull2float_ru(arg0, _semantic=None)#

Converts a 64-bit unsigned integer to a floating-point number with round-up mode.

参数:

arg0 (scalar or tl.tensor) -- x. Supported dtype(s): uint64.

返回:

The converted floating-point number.

返回类型:

float32

示例

import pytest
import triton
import triton.language as tl
import triton.language.extra.cann.libdevice as libdevice
import torch
from triton.backends.ascend.compiler import _is_a5_target_arch
from triton.backends.ascend.driver import NPUUtils

_SIMT_SKIP_MSG = ("SIMT libdevice ops require an Ascend 950PR&950DT products target; skipping.")


def torch_ull2float_ru_reference(x0):
    assert x0.device.type == "cpu"
    assert x0.dtype == torch.uint64
    return x0.to(torch.float32)


@triton.jit
def triton_kernel(input0, out, n_elements, XBLOCK: tl.constexpr, XBLOCK_SUB: tl.constexpr):
    offset = tl.program_id(0) * XBLOCK
    base = tl.arange(0, XBLOCK_SUB)
    loops: tl.constexpr = XBLOCK // XBLOCK_SUB
    for loop in range(loops):
        x0 = offset + (loop * XBLOCK_SUB) + base
        mask = x0 < n_elements
        tmp0 = tl.load(input0 + (x0), mask=mask)
        tmp1 = libdevice.ull2float_ru(tmp0)
        tl.store(out + (x0), tmp1, mask=mask)


@pytest.mark.skipif(not _is_a5_target_arch(NPUUtils().get_arch()), reason=_SIMT_SKIP_MSG)
def test_ull2float_ru():
    x0 = (torch.randint(0, 100, (8, ), dtype=torch.int64)).to(torch.uint64)
    ref = (torch_ull2float_ru_reference(x0)).npu()
    x0 = x0.npu()
    out = torch.empty(8, dtype=torch.float32, device='npu')
    triton_kernel[(1, )](x0, out, 8, XBLOCK=8, XBLOCK_SUB=8, compile_mode='simt_only')
    out = out.cpu()
    ref = ref.cpu()
    torch.testing.assert_close(out, ref, rtol=1e-04, atol=1e-04, equal_nan=True)


if __name__ == "__main__":
    if not _is_a5_target_arch(NPUUtils().get_arch()):
        print(_SIMT_SKIP_MSG)
    else:
        test_ull2float_ru()
        print("test_ull2float_ru passed!")

特殊说明

Platform and compilation modes:

  • Atlas A2 products/Atlas A3 products support SIMD.

  • Ascend 950PR&950DT products support SIMD, SIMT.