/**
 * 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_sparse_apply_ftrl.cpp
 * \brief GE IR graph mode call example for SparseApplyFtrl
 */

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

#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/sparse_apply_ftrl_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 GenOnesData(vector<int64_t> shapes, Tensor& input_tensor, TensorDesc& input_tensor_desc, DataType data_type,
                    int 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);
    uint8_t* pData = new (std::nothrow) uint8_t[data_len];
    if (pData == nullptr) {
        return FAILED;
    }
    if (data_type == ge::DT_FLOAT) {
        auto* fData = reinterpret_cast<float*>(pData);
        for (uint32_t i = 0; i < size; ++i) {
            fData[i] = static_cast<float>(value);
        }
    } else if (data_type == ge::DT_INT32 || data_type == ge::DT_UINT32) {
        auto* iData = reinterpret_cast<int32_t*>(pData);
        for (uint32_t i = 0; i < size; ++i) {
            iData[i] = value;
        }
    } else if (data_type == ge::DT_INT64 || data_type == ge::DT_UINT64) {
        auto* iData = reinterpret_cast<int64_t*>(pData);
        for (uint32_t i = 0; i < size; ++i) {
            iData[i] = value;
        }
    } else {
        for (uint32_t i = 0; i < data_len; ++i) {
            pData[i] = 0;
        }
    }
    input_tensor = Tensor(input_tensor_desc, pData, data_len);
    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("ERROR - Failed to open file %s for writing\n", bin_file.c_str());
        return FAILED;
    }
    size_t written = fwrite(inputData, sizeof(uint8_t), data_size, fp);
    if (written != data_size) {
        printf("ERROR - Failed to write data to file %s, expected %lu, written %zu\n", bin_file.c_str(), data_size,
               written);
        fclose(fp);
        return FAILED;
    }
    fclose(fp);
    return SUCCESS;
}

int CreateOppInGraph(DataType inDtype, std::vector<ge::Tensor>& input, std::vector<Operator>& inputs,
                     std::vector<Operator>& outputs, Graph& graph)
{
    Status ret = SUCCESS;
    auto sparse_apply_ftrl1 = op::SparseApplyFtrl("sparse_apply_ftrl1");

    // var: (100, 16) float32
    std::vector<int64_t> varShape = {100, 16};
    auto placeholderVar = op::Data("placeholder_var").set_attr_index(0);
    TensorDesc placeholderVar_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    placeholderVar_desc.SetPlacement(ge::kPlacementHost);
    placeholderVar_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderVar;
    ret = GenOnesData(varShape, tensor_placeholderVar, placeholderVar_desc, DT_FLOAT, 1);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate var data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderVar.update_input_desc_x(placeholderVar_desc);
    input.push_back(tensor_placeholderVar);
    graph.AddOp(placeholderVar);
    sparse_apply_ftrl1.set_input_var(placeholderVar);
    inputs.push_back(placeholderVar);

    // accum: (100, 16) float32
    auto placeholderAccum = op::Data("placeholder_accum").set_attr_index(1);
    TensorDesc placeholderAccum_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    placeholderAccum_desc.SetPlacement(ge::kPlacementHost);
    placeholderAccum_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderAccum;
    ret = GenOnesData(varShape, tensor_placeholderAccum, placeholderAccum_desc, DT_FLOAT, 1);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate accum data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderAccum.update_input_desc_x(placeholderAccum_desc);
    input.push_back(tensor_placeholderAccum);
    graph.AddOp(placeholderAccum);
    sparse_apply_ftrl1.set_input_accum(placeholderAccum);
    inputs.push_back(placeholderAccum);

    // linear: (100, 16) float32
    auto placeholderLinear = op::Data("placeholder_linear").set_attr_index(2);
    TensorDesc placeholderLinear_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    placeholderLinear_desc.SetPlacement(ge::kPlacementHost);
    placeholderLinear_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderLinear;
    ret = GenOnesData(varShape, tensor_placeholderLinear, placeholderLinear_desc, DT_FLOAT, 0);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate linear data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderLinear.update_input_desc_x(placeholderLinear_desc);
    input.push_back(tensor_placeholderLinear);
    graph.AddOp(placeholderLinear);
    sparse_apply_ftrl1.set_input_linear(placeholderLinear);
    inputs.push_back(placeholderLinear);

    // grad: (10, 16) float32
    std::vector<int64_t> gradShape = {10, 16};
    auto placeholderGrad = op::Data("placeholder_grad").set_attr_index(3);
    TensorDesc placeholderGrad_desc = TensorDesc(ge::Shape(gradShape), FORMAT_ND, DT_FLOAT);
    placeholderGrad_desc.SetPlacement(ge::kPlacementHost);
    placeholderGrad_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderGrad;
    ret = GenOnesData(gradShape, tensor_placeholderGrad, placeholderGrad_desc, DT_FLOAT, 1);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate grad data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderGrad.update_input_desc_x(placeholderGrad_desc);
    input.push_back(tensor_placeholderGrad);
    graph.AddOp(placeholderGrad);
    sparse_apply_ftrl1.set_input_grad(placeholderGrad);
    inputs.push_back(placeholderGrad);

    // indices: (10,) int32, values = [0, 1, 2, ..., 9] (must be unique)
    std::vector<int64_t> indicesShape = {10};
    auto placeholderIndices = op::Data("placeholder_indices").set_attr_index(4);
    TensorDesc placeholderIndices_desc = TensorDesc(ge::Shape(indicesShape), FORMAT_ND, DT_INT32);
    placeholderIndices_desc.SetPlacement(ge::kPlacementHost);
    placeholderIndices_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderIndices;
    ret = GenOnesData(indicesShape, tensor_placeholderIndices, placeholderIndices_desc, DT_INT32, 0);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate indices data failed\n", GetTime().c_str());
        return FAILED;
    }
    int32_t* indicesData = reinterpret_cast<int32_t*>(tensor_placeholderIndices.GetData());
    for (int32_t i = 0; i < 10; ++i) {
        indicesData[i] = i;
    }
    placeholderIndices.update_input_desc_x(placeholderIndices_desc);
    input.push_back(tensor_placeholderIndices);
    graph.AddOp(placeholderIndices);
    sparse_apply_ftrl1.set_input_indices(placeholderIndices);
    inputs.push_back(placeholderIndices);

    // lr: scalar float32
    std::vector<int64_t> scalarShape = {};
    auto placeholderLr = op::Const("placeholder_lr");
    TensorDesc placeholderLr_desc = TensorDesc(ge::Shape(scalarShape), FORMAT_ND, DT_FLOAT);
    placeholderLr_desc.SetPlacement(ge::kPlacementHost);
    placeholderLr_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderLr;
    ret = GenOnesData({1}, tensor_placeholderLr, placeholderLr_desc, DT_FLOAT, 1);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate lr data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderLr.SetAttr("value", tensor_placeholderLr);
    placeholderLr.update_output_desc_y(placeholderLr_desc);
    graph.AddOp(placeholderLr);
    sparse_apply_ftrl1.set_input_lr(placeholderLr);
    sparse_apply_ftrl1.update_input_desc_lr(placeholderLr_desc);
    inputs.push_back(placeholderLr);

    // l1: scalar float32
    auto placeholderL1 = op::Const("placeholder_l1");
    TensorDesc placeholderL1_desc = TensorDesc(ge::Shape(scalarShape), FORMAT_ND, DT_FLOAT);
    placeholderL1_desc.SetPlacement(ge::kPlacementHost);
    placeholderL1_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderL1;
    ret = GenOnesData({1}, tensor_placeholderL1, placeholderL1_desc, DT_FLOAT, 0);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate l1 data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderL1.SetAttr("value", tensor_placeholderL1);
    placeholderL1.update_output_desc_y(placeholderL1_desc);
    graph.AddOp(placeholderL1);
    sparse_apply_ftrl1.set_input_l1(placeholderL1);
    sparse_apply_ftrl1.update_input_desc_l1(placeholderL1_desc);
    inputs.push_back(placeholderL1);

    // l2: scalar float32
    auto placeholderL2 = op::Const("placeholder_l2");
    TensorDesc placeholderL2_desc = TensorDesc(ge::Shape(scalarShape), FORMAT_ND, DT_FLOAT);
    placeholderL2_desc.SetPlacement(ge::kPlacementHost);
    placeholderL2_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderL2;
    ret = GenOnesData({1}, tensor_placeholderL2, placeholderL2_desc, DT_FLOAT, 0);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate l2 data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderL2.SetAttr("value", tensor_placeholderL2);
    placeholderL2.update_output_desc_y(placeholderL2_desc);
    graph.AddOp(placeholderL2);
    sparse_apply_ftrl1.set_input_l2(placeholderL2);
    sparse_apply_ftrl1.update_input_desc_l2(placeholderL2_desc);
    inputs.push_back(placeholderL2);

    // lr_power: scalar float32
    auto placeholderLrPower = op::Const("placeholder_lr_power");
    TensorDesc placeholderLrPower_desc = TensorDesc(ge::Shape(scalarShape), FORMAT_ND, DT_FLOAT);
    placeholderLrPower_desc.SetPlacement(ge::kPlacementHost);
    placeholderLrPower_desc.SetFormat(FORMAT_ND);
    Tensor tensor_placeholderLrPower;
    ret = GenOnesData({1}, tensor_placeholderLrPower, placeholderLrPower_desc, DT_FLOAT, 1);
    if (ret != SUCCESS) {
        printf("%s - ERROR - Generate lr_power data failed\n", GetTime().c_str());
        return FAILED;
    }
    placeholderLrPower.SetAttr("value", tensor_placeholderLrPower);
    placeholderLrPower.update_output_desc_y(placeholderLrPower_desc);
    graph.AddOp(placeholderLrPower);
    sparse_apply_ftrl1.set_input_lr_power(placeholderLrPower);
    sparse_apply_ftrl1.update_input_desc_lr_power(placeholderLrPower_desc);
    inputs.push_back(placeholderLrPower);

    // Outputs
    TensorDesc varOut_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    sparse_apply_ftrl1.update_output_desc_var(varOut_desc);
    TensorDesc accumOut_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    sparse_apply_ftrl1.update_output_desc_accum(accumOut_desc);
    TensorDesc linearOut_desc = TensorDesc(ge::Shape(varShape), FORMAT_ND, DT_FLOAT);
    sparse_apply_ftrl1.update_output_desc_linear(linearOut_desc);

    outputs.push_back(sparse_apply_ftrl1);
    return SUCCESS;
}

int main(int argc, char* argv[])
{
    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{};

    DataType inDtype = DT_FLOAT;

    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());
    std::unique_ptr<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);
    if (ret != SUCCESS) {
        printf("%s - ERROR - [XIR]: Add compute graph to ir session 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]: 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 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();
        uint32_t data_size = output_shape * GetDataTypeSize(output[i].GetTensorDesc().GetDataType());
        WriteDataToFile((const char*)output_file.c_str(), data_size, output_data_i);
    }

    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());
    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;
}