import triton
import triton.language as tl
import triton.language.extra.cann.libdevice as libdevice
import torch
import pytest
import test_common
import os
PERF_TEST_ENABLE = os.getenv('PERF_TEST_ENABLE', 'False').lower() == 'true'
def torch_pointwise(x0, x1):
return torch.maximum(x0.cpu(), x1.cpu()).npu()
@triton.jit
def triton_llmax(in_ptr0, in_ptr1, out_ptr0, XBLOCK: tl.constexpr, XBLOCK_SUB: tl.constexpr):
offset = tl.program_id(0) * XBLOCK
base1 = tl.arange(0, XBLOCK_SUB)
loops1: tl.constexpr = (XBLOCK + XBLOCK_SUB - 1) // XBLOCK_SUB
for loop1 in range(loops1):
x = offset + (loop1 * XBLOCK_SUB) + base1
tmp0 = tl.load(in_ptr0 + x, None)
tmp1 = tl.load(in_ptr1 + x, None)
tmp2 = libdevice.llmax(tmp0, tmp1)
tl.store(out_ptr0 + x, tmp2, None)
default_param_list = test_common.make_default_param_list(['int64'])
full_param_list = test_common.make_full_param_list(['int64'])
@pytest.mark.parametrize('param_list', default_param_list if not PERF_TEST_ENABLE else full_param_list)
def test_common_case(param_list):
dtype, shape, ncore, xblock, xblock_sub = param_list
x0 = test_common.generate_tensor(shape, dtype)
x1 = test_common.generate_tensor(shape, dtype)
if 'llmax' in ('umax', 'umin', 'ullmax', 'ullmin', 'uhadd', 'urhadd', 'umul24', 'umulhi'):
x0 = torch.abs(x0)
x1 = torch.abs(x1)
y_ref = torch_pointwise(x0, x1)
x0 = x0.npu()
x1 = x1.npu()
y_cal = torch.zeros(shape, dtype=torch.int64).npu()
if PERF_TEST_ENABLE:
test_common.run_with_profiler(
lambda: triton_llmax[ncore, 1, 1](x0, x1, y_cal, xblock, xblock_sub, force_simt_only=True),
shape,
'llmax'
)
else:
triton_llmax[ncore, 1, 1](x0, x1, y_cal, xblock, xblock_sub, force_simt_only=True)
test_common.validate_cmp('int64', y_cal, y_ref)