/**
 * Copyright (c) 2026 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.
 *
 * Generated By CANNBot
 */

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <map>
#include <memory>
#include <new>
#include "assert.h"

#include "graph.h"
#include "types.h"
#include "tensor.h"
#include "ge_error_codes.h"
#include "ge_api_types.h"
#include "ge_api.h"
#include "array_ops.h"
#include "ge_ir_build.h"

#include "nn_other.h"
#include "../op_graph/ndtri_proto.h"

#define FAILED -1
#define SUCCESS 0

using namespace ge;
using std::map;
using std::string;
using std::vector;
#define ADD_INPUT(intputIndex, intputName, intputDtype, inputShape)                                                 \
    vector<int64_t> placeholder##intputIndex##_shape = inputShape;                                                  \
    auto placeholder##intputIndex = op::Data("placeholder" + intputIndex).set_attr_index(0);                        \
    TensorDesc placeholder##intputIndex##_desc = TensorDesc(ge::Shape(placeholder##intputIndex##_shape), FORMAT_ND, \
                                                            intputDtype);                                           \
    placeholder##intputIndex##_desc.SetPlacement(ge::kPlacementHost);                                               \
    placeholder##intputIndex##_desc.SetFormat(FORMAT_ND);                                                           \
    Tensor tensor_placeholder##intputIndex;                                                                         \
    ret = GenOnesDataFloat32(placeholder##intputIndex##_shape, tensor_placeholder##intputIndex,                     \
                             placeholder##intputIndex##_desc, 0.5f);                                                \
    if (ret != SUCCESS) {                                                                                           \
        printf("%s - ERROR - [XIR]: Generate input data failed\n", GetTime().c_str());                              \
        return FAILED;                                                                                              \
    }                                                                                                               \
    placeholder##intputIndex.update_input_desc_x(placeholder##intputIndex##_desc);                                  \
    placeholder##intputIndex.update_output_desc_y(placeholder##intputIndex##_desc);                                 \
    input.push_back(tensor_placeholder##intputIndex);                                                               \
    graph.AddOp(placeholder##intputIndex);                                                                          \
    ndtri_op.set_input_##intputName(placeholder##intputIndex);                                                      \
    inputs.push_back(placeholder##intputIndex);

#define LOG_PRINT(message, ...)         \
    do {                                \
        printf(message, ##__VA_ARGS__); \
    } while (0)

string GetTime()
{
    time_t timep;
    time(&timep);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S,000", localtime(&timep));
    return tmp;
}

uint32_t GetDataTypeSize(DataType dt)
{
    static const std::map<DataType, uint32_t> kSize = {
        {ge::DT_FLOAT, 4}, {ge::DT_FLOAT16, 2}, {ge::DT_BF16, 2},  {ge::DT_INT16, 2},  {ge::DT_UINT16, 2},
        {ge::DT_INT32, 4}, {ge::DT_UINT32, 4},  {ge::DT_INT64, 8}, {ge::DT_UINT64, 8}, {ge::DT_INT8, 1},
    };
    auto it = kSize.find(dt);
    return it != kSize.end() ? it->second : 1;
}

// IEEE 754 FP32 to FP16, rounded to nearest with ties to even.
static uint16_t FloatToHalfBits(float value)
{
    uint32_t bits;
    memcpy(&bits, &value, sizeof(bits));
    uint32_t sign = (bits >> 16) & 0x8000U;
    uint32_t exponent = (bits >> 23) & 0xffU;
    uint32_t mantissa = bits & 0x7fffffU;
    if (exponent == 0xffU) {
        return static_cast<uint16_t>(sign | 0x7c00U | (mantissa != 0 ? 0x0200U : 0U));
    }
    int32_t half_exponent = static_cast<int32_t>(exponent) - 127 + 15;
    if (half_exponent >= 31) {
        return static_cast<uint16_t>(sign | 0x7c00U);
    }
    if (half_exponent < -10) {
        return static_cast<uint16_t>(sign);
    }
    uint32_t shift = 13;
    if (half_exponent <= 0) {
        mantissa |= 0x800000U;
        shift = static_cast<uint32_t>(14 - half_exponent);
        half_exponent = 0;
    }
    uint32_t half_bits = (static_cast<uint32_t>(half_exponent) << 10) + (mantissa >> shift);
    uint32_t remainder = mantissa & ((1U << shift) - 1U);
    uint32_t halfway = 1U << (shift - 1U);
    if (remainder > halfway || (remainder == halfway && (half_bits & 1U) != 0)) {
        ++half_bits;
    }
    return static_cast<uint16_t>(sign | half_bits);
}

static float HalfBitsToFloat(uint16_t bits)
{
    uint32_t sign = static_cast<uint32_t>(bits & 0x8000U) << 16;
    uint32_t exponent = (bits >> 10) & 0x1fU;
    uint32_t mantissa = bits & 0x3ffU;
    if (exponent == 0) {
        float value = static_cast<float>(mantissa) / (1U << 24);
        return sign != 0 ? -value : value;
    }
    uint32_t float_bits = sign | ((exponent == 31 ? 0xffU : exponent + 112U) << 23) | (mantissa << 13);
    float value;
    memcpy(&value, &float_bits, sizeof(value));
    return value;
}

int32_t GenOnesDataFloat32(vector<int64_t> shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc, float value)
{
    input_tensor_desc.SetRealDimCnt(shapes.size());
    size_t size = 1;
    for (uint32_t i = 0; i < shapes.size(); i++) {
        size *= shapes[i];
    }
    DataType dtype = input_tensor_desc.GetDataType();
    uint32_t item_size = GetDataTypeSize(dtype);
    uint32_t data_len = size * item_size;
    std::unique_ptr<uint8_t[]> pData(new (std::nothrow) uint8_t[data_len]);
    if (pData == nullptr) {
        printf("alloc failed\n");
        return FAILED;
    }

    for (size_t i = 0; i < size; ++i) {
        if (dtype == DT_FLOAT) {
            memcpy(pData.get() + i * item_size, &value, item_size);
        } else {
            uint32_t float_bits;
            memcpy(&float_bits, &value, sizeof(float_bits));
            // The fixed sample value 0.5 is exactly representable in BF16.
            uint16_t bits = dtype == DT_FLOAT16 ? FloatToHalfBits(value) : static_cast<uint16_t>(float_bits >> 16);
            memcpy(pData.get() + i * item_size, &bits, item_size);
        }
    }
    input_tensor = Tensor(input_tensor_desc);
    return input_tensor.SetData(pData.get(), data_len) == SUCCESS ? SUCCESS : FAILED;
}

int32_t WriteDataToFile(string bin_file, uint64_t data_size, uint8_t* inputData)
{
    FILE* fp = fopen(bin_file.c_str(), "wb");
    if (fp == nullptr) {
        printf("fopen %s failed\n", bin_file.c_str());
        return FAILED;
    }
    size_t written = fwrite(inputData, sizeof(uint8_t), data_size, fp);
    int close_ret = fclose(fp);
    if (written != data_size || close_ret != 0) {
        printf("write %s failed\n", bin_file.c_str());
        return FAILED;
    }
    return SUCCESS;
}

int CreateOppInGraph(DataType inDtype, std::vector<ge::Tensor>& input, std::vector<Operator>& inputs,
                     std::vector<Operator>& outputs, Graph& graph)
{
    Status ret = SUCCESS;
    // 添加 Ndtri 算子到图:输入 x(概率 ∈ (0,1)),输出 y(正态分位点)
    auto ndtri_op = op::Ndtri("ndtri_op");
    std::vector<int64_t> xShape = {4, 2};
    ADD_INPUT(1, x, inDtype, xShape);

    outputs.push_back(ndtri_op);
    return SUCCESS;
}

int main(int argc, char* argv[])
{
    DataType inDtype = DT_FLOAT;
    if (argc >= 2) {
        string dtype_arg = argv[1];
        if (dtype_arg == "float32" || dtype_arg == "0") {
            inDtype = DT_FLOAT;
        } else if (dtype_arg == "float16" || dtype_arg == "1") {
            inDtype = DT_FLOAT16;
        } else if (dtype_arg == "bfloat16" || dtype_arg == "27") {
            inDtype = DT_BF16;
        } else {
            printf("Unsupported dtype: %s\n", argv[1]);
            return FAILED;
        }
    }
    if (argc < 2) {
        printf("INFO: %s invoked without dtype arg, defaulting to DT_FLOAT\n", argv[0] ? argv[0] : "test_geir_ndtri");
    }
    const char* graph_name = "tc_ge_irrun_test";
    Graph graph(graph_name);
    std::vector<ge::Tensor> input;

    printf("%s - INFO - [XIR]: Start to initialize ge using ge global options\n", GetTime().c_str());
    std::map<AscendString, AscendString> global_options = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}};
    Status ret = ge::GEInitialize(global_options);
    if (ret != SUCCESS) {
        printf("%s - INFO - [XIR]: Initialize ge using ge global options failed\n", GetTime().c_str());
        return FAILED;
    }
    printf("%s - INFO - [XIR]: Initialize ge using ge global options success\n", GetTime().c_str());

    std::vector<Operator> inputs{};
    std::vector<Operator> outputs{};

    if (argc >= 2) {
        std::cout << argv[1] << std::endl;
    }

    std::cout << inDtype << std::endl;

    ret = CreateOppInGraph(inDtype, input, inputs, outputs, graph);
    if (ret != SUCCESS) {
        printf("%s - ERROR - [XIR]: Create ir session using build options failed\n", GetTime().c_str());
        GEFinalize();
        return FAILED;
    }

    if (!inputs.empty() && !outputs.empty()) {
        graph.SetInputs(inputs).SetOutputs(outputs);
    }

    std::map<AscendString, AscendString> build_options = {

    };
    printf("%s - INFO - [XIR]: Start to create ir session using build options\n", GetTime().c_str());
    std::unique_ptr<ge::Session> session(new (std::nothrow) Session(build_options));

    if (session == nullptr) {
        printf("%s - ERROR - [XIR]: Create ir session using build options failed\n", GetTime().c_str());
        GEFinalize();
        return FAILED;
    }
    printf("%s - INFO - [XIR]: Create ir session using build options success\n", GetTime().c_str());
    printf("%s - INFO - [XIR]: Start to add compute graph to ir session\n", GetTime().c_str());

    std::map<AscendString, AscendString> graph_options = {

    };
    uint32_t graph_id = 0;
    ret = session->AddGraph(graph_id, graph, graph_options);
    if (ret != SUCCESS) {
        printf("%s - ERROR - [XIR]: Session add graph failed\n", GetTime().c_str());
        session.reset();
        GEFinalize();
        return FAILED;
    }

    printf("%s - INFO - [XIR]: Session add ir compute graph to ir session success\n", GetTime().c_str());
    printf("%s - INFO - [XIR]: dump graph to txt\n", GetTime().c_str());
    std::string file_path = "./dump";
    aclgrphDumpGraph(graph, file_path.c_str(), file_path.length());
    printf("%s - INFO - [XIR]: Start to run ir compute graph\n", GetTime().c_str());
    std::vector<ge::Tensor> output;
    ret = session->RunGraph(graph_id, input, output);
    if (ret != SUCCESS) {
        printf("%s - INFO - [XIR]: Run graph failed\n", GetTime().c_str());
        session.reset();
        GEFinalize();
        return FAILED;
    }
    printf("%s - INFO - [XIR]: Session run ir compute graph success\n", GetTime().c_str());

    int input_num = input.size();
    for (int i = 0; i < input_num; i++) {
        std::cout << "input " << i << " dtype :  " << input[i].GetTensorDesc().GetDataType() << std::endl;
        string input_file = "./tc_ge_irrun_test_0008_npu_input_" + std::to_string(i) + ".bin";
        uint8_t* input_data_i = input[i].GetData();
        int64_t input_shape = input[i].GetTensorDesc().GetShape().GetShapeSize();
        std::cout << "this is " << i << "th input, input shape size =" << input_shape << std::endl;
        uint32_t data_size = input_shape * GetDataTypeSize(input[i].GetTensorDesc().GetDataType());
        if (WriteDataToFile(input_file, data_size, input_data_i) != SUCCESS) {
            session.reset();
            GEFinalize();
            return FAILED;
        }
    }

    int output_num = output.size();
    for (int i = 0; i < output_num; i++) {
        std::cout << "output " << i << " dtype :  " << output[i].GetTensorDesc().GetDataType() << std::endl;
        string output_file = "./tc_ge_irrun_test_0008_npu_output_" + std::to_string(i) + ".bin";
        uint8_t* output_data_i = output[i].GetData();
        int64_t output_shape = output[i].GetTensorDesc().GetShape().GetShapeSize();
        std::cout << "this is " << i << "th output, output shape size =" << output_shape << std::endl;
        uint32_t data_size = output_shape * GetDataTypeSize(output[i].GetTensorDesc().GetDataType());
        if (WriteDataToFile(output_file, data_size, output_data_i) != SUCCESS) {
            session.reset();
            GEFinalize();
            return FAILED;
        }
        for (int64_t j = 0; j < output_shape; j++) {
            float result_value;
            DataType out_dtype = output[i].GetTensorDesc().GetDataType();
            if (out_dtype == DT_FLOAT) {
                memcpy(&result_value, output_data_i + j * sizeof(float), sizeof(float));
            } else {
                uint16_t bits;
                memcpy(&bits, output_data_i + j * sizeof(uint16_t), sizeof(bits));
                if (out_dtype == DT_FLOAT16) {
                    result_value = HalfBitsToFloat(bits);
                } else {
                    uint32_t float_bits = static_cast<uint32_t>(bits) << 16;
                    memcpy(&result_value, &float_bits, sizeof(float));
                }
            }
            LOG_PRINT("result[%ld] is: %f\n", j, result_value);
        }
    }

    ge::AscendString error_msg = ge::GEGetErrorMsgV2();
    std::string error_str(error_msg.GetString());
    std::cout << "Error message: " << error_str << std::endl;
    ge::AscendString warning_msg = ge::GEGetWarningMsgV2();
    std::string warning_str(warning_msg.GetString());
    std::cout << "Warning message: " << warning_str << std::endl;
    printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());
    session.reset();
    ret = ge::GEFinalize();
    if (ret != SUCCESS) {
        printf("%s - INFO - [XIR]: Finalize ir graph session failed\n", GetTime().c_str());
        return FAILED;
    }
    printf("%s - INFO - [XIR]: Finalize ir graph session success\n", GetTime().c_str());
    return SUCCESS;
}