/**
 * 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 <cstddef>
#include <cstdint>
#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 "experiment_ops.h"
#include "nn_other.h"
#include "../op_graph/max_pool_ext2_proto.h"

#define FAILED -1
#define SUCCESS 0

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

enum RunMode { RUN_MODE_S = 0, RUN_MODE_D = 1 };

struct CaseResult {
    std::string case_name;
    bool build_ok;
    bool run_ok;
    bool output_exists;
    int output_count;
    std::string err_msg;
};

#define ADD_INPUT_MODE(intputIndex, intputName, intputDtype, inputShape, mode)                                       \
    vector<int64_t> placeholder##intputIndex##_real_shape = inputShape;                                              \
    vector<int64_t> placeholder##intputIndex##_graph_shape = ((mode) == RUN_MODE_D) ?                                \
                                                                 vector<int64_t>{-2} :                               \
                                                                 placeholder##intputIndex##_real_shape;              \
    auto placeholder##intputIndex = op::Data("placeholder" #intputIndex).set_attr_index(0);                          \
    TensorDesc placeholder##intputIndex##_desc_graph = TensorDesc(ge::Shape(placeholder##intputIndex##_graph_shape), \
                                                                  FORMAT_ND, intputDtype);                           \
    placeholder##intputIndex##_desc_graph.SetPlacement(ge::kPlacementHost);                                          \
    placeholder##intputIndex##_desc_graph.SetFormat(FORMAT_ND);                                                      \
    TensorDesc placeholder##intputIndex##_desc_real = TensorDesc(ge::Shape(placeholder##intputIndex##_real_shape),   \
                                                                 FORMAT_ND, intputDtype);                            \
    placeholder##intputIndex##_desc_real.SetPlacement(ge::kPlacementHost);                                           \
    placeholder##intputIndex##_desc_real.SetFormat(FORMAT_ND);                                                       \
    placeholder##intputIndex##_desc_real.SetRealDimCnt(placeholder##intputIndex##_real_shape.size());                \
    Tensor tensor_placeholder##intputIndex;                                                                          \
    ret = GenOnesData(placeholder##intputIndex##_real_shape, tensor_placeholder##intputIndex,                        \
                      placeholder##intputIndex##_desc_real, intputDtype, 2);                                         \
    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_graph);                             \
    placeholder##intputIndex.update_output_desc_y(placeholder##intputIndex##_desc_graph);                            \
    input.push_back(tensor_placeholder##intputIndex);                                                                \
    graph.AddOp(placeholder##intputIndex);                                                                           \
    add1.set_input_##intputName(placeholder##intputIndex);                                                           \
    inputs.push_back(placeholder##intputIndex);

#define ADD_CONST_INPUT(intputIndex, intputName, intputDtype, inputShape)                                           \
    vector<int64_t> placeholder##intputIndex##_shape = inputShape;                                                  \
    auto placeholder##intputIndex = op::Const("placeholder" #intputIndex);                                          \
    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 = GenOnesData(placeholder##intputIndex##_shape, tensor_placeholder##intputIndex,                            \
                      placeholder##intputIndex##_desc, intputDtype, 2);                                             \
    if (ret != SUCCESS) {                                                                                           \
        printf("%s - ERROR - [XIR]: Generate input data failed\n", GetTime().c_str());                              \
        return FAILED;                                                                                              \
    }                                                                                                               \
    placeholder##intputIndex.SetAttr("value", tensor_placeholder##intputIndex);                                     \
    placeholder##intputIndex.update_output_desc_y(placeholder##intputIndex##_desc);                                 \
    graph.AddOp(placeholder##intputIndex);                                                                          \
    add1.set_input_##intputName(placeholder##intputIndex);                                                          \
    add1.update_input_desc_##intputName(placeholder##intputIndex##_desc);                                           \
    inputs.push_back(placeholder##intputIndex);

#define ADD_OUTPUT_MODE(outputIndex, outputName, outputDtype, outputShape, mode)                                    \
    vector<int64_t> output##outputIndex##_graph_shape = ((mode) == RUN_MODE_D) ? vector<int64_t>{-2} : outputShape; \
    TensorDesc outputName##outputIndex##_desc = TensorDesc(ge::Shape(output##outputIndex##_graph_shape), FORMAT_ND, \
                                                           outputDtype);                                            \
    add1.update_output_desc_##outputName(outputName##outputIndex##_desc);

string GetTime()
{
    time_t timep;
    time(&timep);
    char tmp[64];
    struct tm* tm_info = localtime(&timep);
    if (tm_info == nullptr) {
        return "unknown";
    }
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S,000", tm_info);
    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;
    } else if (dt == ge::DT_UINT8) {
        dilation = oneByte;
    }
    return dilation;
}

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];
    }
    uint32_t byteSizeFloat32 = 4;
    uint32_t data_len = size * byteSizeFloat32;
    float* pData = new (std::nothrow) float[size];
    if (pData == nullptr) {
        printf("%s - ERROR - [XIR]: get pData failed\n", GetTime().c_str());
        return FAILED;
    }

    for (size_t i = 0; i < size; ++i) {
        *(pData + i) = value;
    }
    input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);
    return SUCCESS;
}

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);
    int32_t* pData = new (std::nothrow) int32_t[data_len];
    if (pData == nullptr) {
        printf("%s - ERROR - [XIR]: get pData failed\n", GetTime().c_str());
        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);
    return SUCCESS;
}

std::vector<int64_t> ComputeMaxPoolOutputShape(const std::vector<int64_t>& inputShape,
                                               const std::vector<int64_t>& ksize, const std::vector<int64_t>& strides,
                                               const std::string& padding, const std::string& data_format)
{
    std::vector<int64_t> outputShape = inputShape;
    if (inputShape.size() == 4) {
        bool isNHWC = (data_format == "NHWC");
        size_t hDim = isNHWC ? 1 : 2;
        size_t wDim = isNHWC ? 2 : 3;
        int64_t H = inputShape[hDim];
        int64_t W = inputShape[wDim];
        int64_t kH = ksize[hDim];
        int64_t kW = ksize[wDim];
        int64_t sH = strides[hDim];
        int64_t sW = strides[wDim];
        if (padding == "SAME") {
            outputShape[hDim] = (H + sH - 1) / sH;
            outputShape[wDim] = (W + sW - 1) / sW;
        } else {
            outputShape[hDim] = (H - kH + sH) / sH;
            outputShape[wDim] = (W - kW + sW) / sW;
        }
    }
    return outputShape;
}

int CreateOppInGraph(RunMode mode, DataType inDtype, const std::vector<int64_t>& xShape,
                     const std::vector<int64_t>& ksize, const std::vector<int64_t>& strides, const std::string& padding,
                     const std::string& data_format, std::vector<ge::Tensor>& input, std::vector<Operator>& inputs,
                     std::vector<Operator>& outputs, Graph& graph)
{
    Status ret = SUCCESS;
    // 自定义代码:添加单算子定义到图中
    auto add1 = op::MaxPoolExt2("add1");
    ADD_INPUT_MODE(1, x, inDtype, xShape, mode);

    std::vector<int64_t> yShape = ComputeMaxPoolOutputShape(xShape, ksize, strides, padding, data_format);
    ADD_OUTPUT_MODE(1, y, inDtype, yShape, mode);

    add1.set_attr_ksize(ksize);
    add1.set_attr_strides(strides);
    add1.SetAttr("padding", padding.c_str());
    add1.SetAttr("data_format", data_format.c_str());

    outputs.push_back(add1);
    return SUCCESS;
}

CaseResult RunOneCase(ge::Session* session, uint32_t graph_id, RunMode mode, DataType dtype,
                      const std::vector<int64_t>& shape, const std::vector<int64_t>& ksize,
                      const std::vector<int64_t>& strides, const std::string& padding, const std::string& data_format,
                      const std::string& case_name)
{
    CaseResult r;
    r.case_name = case_name;
    r.build_ok = false;
    r.run_ok = false;
    r.output_exists = false;
    r.output_count = 0;
    r.err_msg = "";

    std::string graph_name = "tc_ge_irrun_test_" + std::to_string(graph_id);
    Graph graph(graph_name.c_str());
    std::vector<ge::Tensor> input;
    std::vector<Operator> inputs{};
    std::vector<Operator> outputs{};

    Status ret = CreateOppInGraph(mode, dtype, shape, ksize, strides, padding, data_format, input, inputs, outputs,
                                  graph);
    if (ret != SUCCESS) {
        r.err_msg = "CreateOppInGraph failed";
        return r;
    }
    if (!inputs.empty() && !outputs.empty()) {
        graph.SetInputs(inputs).SetOutputs(outputs);
    }

    std::map<AscendString, AscendString> graph_options = {};
    ret = session->AddGraph(graph_id, graph, graph_options);
    if (ret != SUCCESS) {
        r.err_msg = "AddGraph failed, ret=" + std::to_string(ret);
        return r;
    }
    r.build_ok = true;

    std::vector<ge::Tensor> output;
    ret = session->RunGraph(graph_id, input, output);
    session->RemoveGraph(graph_id);
    if (ret != SUCCESS) {
        r.err_msg = "RunGraph failed, ret=" + std::to_string(ret);
        return r;
    }
    r.run_ok = true;
    r.output_count = output.size();
    r.output_exists = (output.size() > 0);

    for (size_t i = 0; i < output.size(); i++) {
        int64_t shape_size = output[i].GetTensorDesc().GetShape().GetShapeSize();
        printf("  [%s] output[%zu] dtype=%d shape_size=%lld\n", case_name.c_str(), i,
               output[i].GetTensorDesc().GetDataType(), (long long)shape_size);
    }

    return r;
}

void PrintReport(const std::vector<CaseResult>& results)
{
    printf("\n");
    printf("====================================================================================================\n");
    printf("| %-22s | %-8s | %-9s | %-12s | %-7s | %-20s\n", "Case", "Build", "RunGraph", "OutputExists", "OutCnt",
           "ErrMsg");
    printf("----------------------------------------------------------------------------------------------------\n");
    int pass_cnt = 0;
    int total = results.size();
    for (const auto& r : results) {
        bool pass = r.build_ok && r.run_ok && r.output_exists;
        if (pass)
            pass_cnt++;
        printf("| %-22s | %-8s | %-9s | %-12s | %-7d | %-20s\n", r.case_name.c_str(), r.build_ok ? "OK" : "FAIL",
               r.run_ok ? "OK" : "FAIL", r.output_exists ? "OK" : "FAIL", r.output_count,
               r.err_msg.empty() ? "-" : r.err_msg.c_str());
    }
    printf("====================================================================================================\n");
    printf("Summary: %d/%d passed\n", pass_cnt, total);
}

int main(int argc, char* argv[])
{
    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", "1"}, // device id 通过`npu-smi info`命令查询,环境中状态为`OK`的device id为1
        {"ge.graphRunMode", "0"},
        {"ge.exec.precision_mode", "must_keep_origin_dtype"}};
    // 初始化图引擎
    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());

    // dtype 矩阵(取自 reg_op.dtype_set)
    struct DtypeEntry {
        DataType dt;
        std::string name;
    };
    std::vector<DtypeEntry> dtype_list = {
        {DT_FLOAT16, "FP16"}, {DT_FLOAT, "FP32"},  {DT_INT8, "INT8"},   {DT_INT16, "INT16"},
        {DT_INT32, "INT32"},  {DT_INT64, "INT64"}, {DT_UINT8, "UINT8"}, {DT_UINT16, "UINT16"},
    };

    // shape 场景矩阵(max pooling 要求 4D 输入,场景已适配为 4D)
    struct ShapeEntry {
        std::vector<int64_t> shape;
        std::string name;
        std::vector<int64_t> ksize;
        std::vector<int64_t> strides;
        std::string padding;
        std::string data_format;
    };
    std::vector<ShapeEntry> shape_list = {
        {{32, 4, 4, 4}, "regular", {1, 2, 2, 1}, {1, 2, 2, 1}, "SAME", "NHWC"},
        {{1, 1, 1, 1}, "minimal", {1, 2, 2, 1}, {1, 2, 2, 1}, "SAME", "NHWC"},
        {{1, 8, 1, 3}, "1d_spatial", {1, 2, 2, 1}, {1, 2, 2, 1}, "SAME", "NHWC"},
        {{0, 4, 4, 4}, "empty", {1, 2, 2, 1}, {1, 2, 2, 1}, "SAME", "NHWC"},
        {{2, 32, 32, 16}, "large", {1, 2, 2, 1}, {1, 2, 2, 1}, "SAME", "NHWC"},
        {{50, 100, 150, 70}, "bb_l1_3", {1, 3, 3, 1}, {1, 1, 1, 1}, "VALID", "NHWC"},
    };

    // 单 session 复用
    std::map<AscendString, AscendString> build_options = {};
    ge::Session* session = new Session(build_options);
    if (session == nullptr) {
        printf("%s - ERROR - [XIR]: create session failed\n", GetTime().c_str());
        ge::GEFinalize();
        return FAILED;
    }

    std::vector<CaseResult> results;
    uint32_t graph_id = 0;

    // N_dtype × N_shape × 2 mode 全矩阵
    for (const auto& d : dtype_list) {
        for (const auto& s : shape_list) {
            for (auto mode : {RUN_MODE_S, RUN_MODE_D}) {
                std::string mode_name = (mode == RUN_MODE_S) ? "S" : "D";
                std::string case_name = d.name + "_" + s.name + "_" + mode_name;
                printf("\n%s - INFO - [XIR]: ===== %s =====\n", GetTime().c_str(), case_name.c_str());
                CaseResult r = RunOneCase(session, graph_id, mode, d.dt, s.shape, s.ksize, s.strides, s.padding,
                                          s.data_format, case_name);
                results.push_back(r);
                graph_id++;
            }
        }
    }

    PrintReport(results);

    bool all_pass = true;
    for (const auto& r : results) {
        if (!r.build_ok || !r.run_ok || !r.output_exists) {
            all_pass = false;
        }
    }
    if (all_pass) {
        printf("\n%s - INFO - [XIR]: ALL CASES PASSED\n", GetTime().c_str());
    } else {
        printf("\n%s - ERROR - [XIR]: SOME CASES FAILED, see report above\n", GetTime().c_str());
    }

    delete session;
    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 all_pass ? SUCCESS : FAILED;
}