# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.

# Licensed under the Apache License, Version 2.0 (the "License");

# you may not use this file except in compliance with the License.

# You may obtain a copy of the License at

#

# http://www.apache.org/licenses/LICENSE-2.0

#

# Unless required by applicable law or agreed to in writing, software

# distributed under the License is distributed on an "AS IS" BASIS,

# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

# See the License for the specific language governing permissions and

# limitations under the License.



import triton

import triton.language as tl

import triton.language.extra.cann.libdevice as libdevice

import numpy as np

import torch

import pytest

import test_common

import os



PERF_TEST_ENABLE = os.getenv('PERF_TEST_ENABLE', 'False').lower() == 'true'





def torch_ll2float_rd(x):

    return x.to(torch.float32)





@triton.jit

def triton_ll2float_rd(in_ptr0, 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):

        x0 = offset + (loop1 * XBLOCK_SUB) + base1

        tmp0 = tl.load(in_ptr0 + (x0), None)

        tmp1 = libdevice.ll2float_rd(tmp0)

        tl.store(out_ptr0 + (x0), tmp1, 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)

    y_ref = torch_ll2float_rd(x0).npu()

    x0 = x0.npu()

    y_cal = torch.zeros(shape, dtype=torch.float32).npu()

    if PERF_TEST_ENABLE:

        test_common.run_with_profiler(

            lambda: triton_ll2float_rd[ncore, 1, 1](x0, y_cal, xblock, xblock_sub, force_simt_only=True),

            shape, 'll2float_rd'

        )

    else:

        triton_ll2float_rd[ncore, 1, 1](x0, y_cal, xblock, xblock_sub, force_simt_only=True)

    test_common.validate_cmp('float32', y_cal, y_ref)





@pytest.mark.parametrize('param_list',

                        [

                            ['int64', (1, 16), 1, 16, 16],

                        ]

                        )

def test_special_case(param_list):

    dtype, shape, ncore, xblock, xblock_sub = param_list

    x0 = test_common.generate_tensor(shape, dtype)

    x0[0, 0] = 0

    x0[0, 1] = 9223372036854775807

    x0[0, 2] = -9223372036854775808

    y_ref = torch_ll2float_rd(x0).npu()

    x0 = x0.npu()

    y_cal = torch.zeros(shape, dtype=torch.float32).npu()

    triton_ll2float_rd[ncore, 1, 1](x0, y_cal, xblock, xblock_sub, force_simt_only=True)

    test_common.validate_cmp('float32', y_cal, y_ref)