/**
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/


/* !
 * \file quant.asc
 * \brief
 */

#include "acl/acl.h"
#include "data_utils.h"
#include "kernel_operator.h"
#include "tiling/tiling_api.h"

namespace optiling {
BEGIN_TILING_DATA_DEF(QuantCustomTilingData)
TILING_DATA_FIELD_DEF(uint32_t, dataLength);
TILING_DATA_FIELD_DEF(uint32_t, sharedTmpBufferSize);
END_TILING_DATA_DEF;

REGISTER_TILING_DATA_CLASS(QuantCustom, QuantCustomTilingData)
} // namespace optiling

void ComputeTiling(const uint32_t dataLength, optiling::QuantCustomTilingData& tiling)
{
    std::vector<int64_t> shapeVec = {dataLength};
    ge::Shape srcShape(shapeVec);
    uint32_t typeSize = sizeof(float);
    uint32_t maxTmpSize;
    uint32_t minTmpSize;
    AscendC::GetAscendQuantMaxMinTmpSize(srcShape, typeSize, maxTmpSize, minTmpSize);
    uint32_t localWorkspaceSize = minTmpSize;
    tiling.set_dataLength(dataLength);
    tiling.set_sharedTmpBufferSize(localWorkspaceSize);
}

uint8_t* GetTilingBuf(optiling::QuantCustomTilingData* tilingData)
{
    uint32_t tilingSize = sizeof(optiling::QuantCustomTilingData);
    uint8_t* buf = (uint8_t*)malloc(tilingSize);
    tilingData->SaveToBuffer(buf, tilingSize);
    return buf;
}

uint8_t* GenerateTiling(uint32_t dataLength)
{
    optiling::QuantCustomTilingData tiling;
    ComputeTiling(dataLength, tiling);
    return GetTilingBuf(&tiling);
}

namespace MyCustomKernel {
struct VecTiling {
    uint32_t dataLength;
    uint32_t sharedTmpBufferSize;
};

template <typename srcType>
class KernelQuant {
public:
    __aicore__ inline KernelQuant() {}
    __aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t inputSize, AscendC::TPipe* pipeIn)
    {
        pipe = pipeIn;
        dataSize = inputSize;

        srcGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ srcType*>(srcGm), dataSize);
        dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ int8_t*>(dstGm), dataSize);

        pipe->InitBuffer(inQueueX, 1, dataSize * sizeof(srcType));
        pipe->InitBuffer(outQueue, 1, dataSize * sizeof(int8_t));
    }
    __aicore__ inline void Process()
    {
        CopyIn();
        Compute();
        CopyOut();
    }

private:
    __aicore__ inline void CopyIn()
    {
        AscendC::LocalTensor<srcType> srcLocal = inQueueX.AllocTensor<srcType>();
        AscendC::DataCopy(srcLocal, srcGlobal, dataSize);
        inQueueX.EnQue(srcLocal);
    }
    __aicore__ inline void Compute()
    {
        const float scale = 2.0;
        const float offset = 0.9;
        AscendC::LocalTensor<int8_t> dstLocal = outQueue.AllocTensor<int8_t>();
        AscendC::LocalTensor<srcType> srcLocal = inQueueX.DeQue<srcType>();
        AscendC::AscendQuant<srcType>(dstLocal, srcLocal, scale, offset, dataSize);
        outQueue.EnQue<int8_t>(dstLocal);
        inQueueX.FreeTensor(srcLocal);
    }
    __aicore__ inline void CopyOut()
    {
        AscendC::LocalTensor<int8_t> dstLocal = outQueue.DeQue<int8_t>();
        AscendC::DataCopy(dstGlobal, dstLocal, dataSize);
        outQueue.FreeTensor(dstLocal);
    }

private:
    AscendC::GlobalTensor<srcType> srcGlobal;
    AscendC::GlobalTensor<int8_t> dstGlobal;
    AscendC::TPipe* pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueue;
    uint32_t dataSize = 0;
};

} // namespace MyCustomKernel

__aicore__ inline void CopyTiling(MyCustomKernel::VecTiling* tiling, GM_ADDR tilingGM)
{
    uint32_t* ptr = reinterpret_cast<uint32_t*>(tiling);
    auto tiling32 = reinterpret_cast<__gm__ uint32_t*>(tilingGM);

    for (uint32_t i = 0; i < sizeof(MyCustomKernel::VecTiling) / sizeof(uint32_t); i++, ptr++) {
        *ptr = *(tiling32 + i);
    }
    return;
}

extern "C" __global__ __aicore__ void quant_custom(GM_ADDR srcGm, GM_ADDR dstGm, GM_ADDR workspace, GM_ADDR tiling)
{
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
    AscendC::TPipe pipe;
    MyCustomKernel::KernelQuant<float> op;
    MyCustomKernel::VecTiling tilingData;
    CopyTiling(&tilingData, tiling);
    op.Init(srcGm, dstGm, tilingData.dataLength, &pipe);
    op.Process();
}

constexpr uint32_t NUM_BLOCKS = 1;
constexpr uint32_t DATALENGTH = 1024;
constexpr uint32_t TILINGDATA_SIZE = 2;
constexpr uint32_t WORKSPACE_SIZE = 16 * 1024 * 1024;

static bool CompareResult(const void* outputData, int64_t outSize)
{
    void* goldenData;
    aclrtMallocHost((void**)(&goldenData), outSize);
    size_t goldenSize = outSize;
    bool ret = ReadFile("./output/golden.bin", goldenSize, goldenData, goldenSize);
    if (ret) {
        printf("ReadFile golden.bin success!\n");
    } else {
        printf("test failed!\n");
        aclrtFreeHost(goldenData);
        return false;
    }
    constexpr float EPS = 1e-5;
    int64_t wrongNum = 0;

    for (int i = 0; i < outSize / sizeof(int8_t); i++) {
        float a = (reinterpret_cast<const int8_t*>(outputData))[i];
        float b = (reinterpret_cast<const int8_t*>(goldenData))[i];
        float ae = std::abs(a - b);
        float re = ae / std::abs(b);
        if (ae > EPS && re > EPS) {
            printf("CompareResult golden.bin failed. Output[%d] is %lf, golden is %lf\n", i, a, b);
            wrongNum++;
        }
    }
    aclrtFreeHost(goldenData);
    if (wrongNum != 0) {
        return false;
    } else {
        printf("CompareResult golden.bin success!\n");
        return true;
    }
}

int32_t main(int32_t argc, char* argv[])
{
    uint32_t numBlocks = NUM_BLOCKS;
    size_t dataLength = DATALENGTH;
    size_t inpSize = DATALENGTH * sizeof(float);
    size_t outSize = DATALENGTH * sizeof(int8_t);
    size_t tilingFileSize = TILINGDATA_SIZE * sizeof(uint32_t);
    size_t workspaceSize = WORKSPACE_SIZE;
    uint8_t* tilingBuf = GenerateTiling(dataLength);

    aclInit(nullptr);
    aclrtContext context;
    int32_t deviceId = 0;
    aclrtSetDevice(deviceId);
    aclrtCreateContext(&context, deviceId);
    aclrtStream stream = nullptr;
    aclrtCreateStream(&stream);

    uint8_t *inputHost, *outputHost, *workspaceHost, *tilingHost;
    uint8_t *inputDevice, *outputDevice, *workspaceDevice, *tilingDevice;

    aclrtMallocHost((void**)(&inputHost), inpSize);
    aclrtMallocHost((void**)(&outputHost), outSize);
    aclrtMallocHost((void**)(&workspaceHost), workspaceSize);
    aclrtMallocHost((void**)(&tilingHost), tilingFileSize);
    aclrtMalloc((void**)&inputDevice, inpSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void**)&outputDevice, outSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void**)&workspaceDevice, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
    aclrtMalloc((void**)&tilingDevice, tilingFileSize, ACL_MEM_MALLOC_HUGE_FIRST);

    ReadFile("./input/input.bin", inpSize, inputHost, inpSize);

    aclrtMemcpy(tilingDevice, tilingFileSize, tilingBuf, tilingFileSize, ACL_MEMCPY_HOST_TO_DEVICE);
    aclrtMemcpy(inputDevice, inpSize, inputHost, inpSize, ACL_MEMCPY_HOST_TO_DEVICE);

    quant_custom<<<numBlocks, nullptr, stream>>>(inputDevice, outputDevice, workspaceDevice, tilingDevice);
    aclrtSynchronizeStream(stream);
    aclrtMemcpy(outputHost, outSize, outputDevice, outSize, ACL_MEMCPY_DEVICE_TO_HOST);

    WriteFile("./output/output.bin", outputHost, outSize);

    bool goldenResult = CompareResult(outputHost, outSize);
    if (goldenResult) {
        printf("test pass!\n");
    } else {
        printf("test failed!\n");
    }

    aclrtFree(inputDevice);
    aclrtFree(outputDevice);
    aclrtFree(tilingDevice);
    aclrtFreeHost(inputHost);
    aclrtFreeHost(outputHost);
    aclrtFreeHost(tilingHost);
    aclrtFree(workspaceDevice);
    aclrtFreeHost(workspaceHost);

    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    free(tilingBuf);
    return 0;
}