/**
 * 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
 */

/**
 * @file test_geir_apply_rms_prop.cpp
 * @brief ApplyRMSProp 算子 GE IR 图模式调用示例(对齐 canndev REG_OP(ApplyRMSProp) 单输出原型)
 *
 * 构图并运行单算子子图;ApplyRMSProp 有 8 个输入 / 1 个输出(ms / mom 通过 inplace 输入更新):
 *   - 输入:var, ms, mom, lr, rho, momentum, epsilon, grad
 *   - 输出:var(单输出;ms / mom 输入即输出,inplace 更新)
 *   - 属性:use_locking (bool, default false)
 *
 * 目标平台:Ascend950 PR / Ascend950 DT(arch35 / DAV_3510)
 */

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <map>
#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 "../op_graph/apply_rms_prop_proto.h"

#define FAILED -1
#define SUCCESS 0

using namespace ge;
using std::map;
using std::string;
using std::vector;

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)
{
    uint32_t dilation = 1;
    uint32_t oneByte = 1;
    uint32_t twoByte = 2;
    uint32_t fourByte = 4;
    uint32_t eightByte = 8;

    if (dt == ge::DT_FLOAT) {
        dilation = fourByte;
    } else if (dt == ge::DT_FLOAT16) {
        dilation = twoByte;
    } else if (dt == ge::DT_BF16) {
        dilation = twoByte;
    } else if (dt == ge::DT_INT16) {
        dilation = twoByte;
    } else if (dt == ge::DT_UINT16) {
        dilation = twoByte;
    } else if (dt == ge::DT_INT32) {
        dilation = fourByte;
    } else if (dt == ge::DT_UINT32) {
        dilation = fourByte;
    } else if (dt == ge::DT_INT64) {
        dilation = eightByte;
    } else if (dt == ge::DT_UINT64) {
        dilation = eightByte;
    } else if (dt == ge::DT_INT8) {
        dilation = oneByte;
    }
    return dilation;
}

int32_t GenFloatData(vector<int64_t> shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc, DataType data_type,
                     float value)
{
    input_tensor_desc.SetRealDimCnt(shapes.size());
    size_t size = 1;
    for (uint32_t i = 0; i < shapes.size(); i++) {
        size *= shapes[i];
    }
    uint32_t data_len = size * GetDataTypeSize(data_type);
    float* pData = new (std::nothrow) float[size];
    if (pData == nullptr) {
        printf("GenFloatData: allocation failed for size=%zu\n", size);
        return FAILED;
    }
    for (size_t i = 0; i < size; ++i) {
        *(pData + i) = value;
    }
    input_tensor = Tensor(input_tensor_desc, reinterpret_cast<uint8_t*>(pData), data_len);
    delete[] pData;
    return SUCCESS;
}

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("WriteDataToFile: fopen failed for %s\n", bin_file.c_str());
        return FAILED;
    }
    size_t written = fwrite(inputData, sizeof(uint8_t), data_size, fp);
    fclose(fp);
    if (written != data_size) {
        printf("WriteDataToFile: short write %zu/%lu\n", written, data_size);
        return FAILED;
    }
    return SUCCESS;
}

template <typename SetterFn>
int32_t AddDataInput(int placeholderIndex, SetterFn portSetter, DataType dtype, const vector<int64_t>& shape,
                     float fillValue, Graph& graph, std::vector<ge::Tensor>& input, std::vector<Operator>& inputs)
{
    std::string name = "placeholder" + std::to_string(placeholderIndex);
    auto data = op::Data(name.c_str()).set_attr_index(placeholderIndex);
    TensorDesc desc = TensorDesc(ge::Shape(shape), FORMAT_ND, dtype);
    desc.SetPlacement(ge::kPlacementHost);
    desc.SetFormat(FORMAT_ND);
    Tensor tensor;
    Status ret = GenFloatData(shape, tensor, desc, dtype, fillValue);
    if (ret != SUCCESS) {
        printf("%s - ERROR - [XIR]: Generate input data failed for %s\n", GetTime().c_str(), name.c_str());
        return FAILED;
    }
    data.update_input_desc_x(desc);
    input.push_back(tensor);
    graph.AddOp(data);
    portSetter(data);
    inputs.push_back(data);
    return SUCCESS;
}

int CreateOppInGraph(DataType inDtype, std::vector<ge::Tensor>& input, std::vector<Operator>& inputs,
                     std::vector<Operator>& outputs, Graph& graph)
{
    auto rmsprop = op::ApplyRMSProp("apply_rms_prop1");

    std::vector<int64_t> tensorShape = {2, 4, 4, 4}; // var / ms / mom / grad shape (128 elements)
    std::vector<int64_t> scalarShape = {1};          // lr / rho / momentum / epsilon shape

    int idx = 0;
    // 8 inputs — order must match REG_OP(ApplyRMSProp) .INPUT order
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_var(d); }, inDtype, tensorShape, 1.0f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_ms(d); }, inDtype, tensorShape, 0.1f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_mom(d); }, inDtype, tensorShape, 0.0f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_lr(d); }, inDtype, scalarShape, 0.01f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_rho(d); }, inDtype, scalarShape, 0.9f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_momentum(d); }, inDtype, scalarShape, 0.0f, graph, input,
            inputs) != SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_epsilon(d); }, inDtype, scalarShape, 1e-7f, graph, input,
            inputs) != SUCCESS)
        return FAILED;
    if (AddDataInput(
            idx++, [&](Operator& d) { rmsprop.set_input_grad(d); }, inDtype, tensorShape, 0.2f, graph, input, inputs) !=
        SUCCESS)
        return FAILED;

    rmsprop.set_attr_use_locking(false);

    // Single output (var)
    TensorDesc varOutDesc = TensorDesc(ge::Shape(tensorShape), FORMAT_ND, inDtype);
    rmsprop.update_output_desc_var(varOutDesc);

    outputs.push_back(rmsprop);
    return SUCCESS;
}

int main(int argc, char* argv[])
{
    const char* graph_name = "tc_apply_rms_prop_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 > 1) {
        std::cout << "device id arg: " << argv[1] << std::endl;
    }

    DataType inDtype = DT_FLOAT;
    std::cout << "input dtype: " << 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());
        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());
    ge::Session* session = new Session(build_options);

    if (session == nullptr) {
        printf("%s - ERROR - [XIR]: Create ir session using build options failed\n", GetTime().c_str());
        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);

    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());
        delete session;
        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_apply_rms_prop_ge_irrun_test_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());
        WriteDataToFile((const char*)input_file.c_str(), data_size, input_data_i);
    }

    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_apply_rms_prop_ge_irrun_test_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());
        WriteDataToFile((const char*)output_file.c_str(), data_size, output_data_i);
    }

    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]: Precision is ok\n", GetTime().c_str());
    printf("%s - INFO - [XIR]: Start to finalize ir graph session\n", GetTime().c_str());
    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;
}