/**
 * 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/yolo_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;
};

struct ShapeCombo {
    std::vector<int64_t> xShape;
    int64_t boxes;
    int64_t coords;
    int64_t classes;
    std::string yolo_version;
    bool softmax;
    bool background;
    std::string name;
};

#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>(                                    \
                                                                     placeholder##intputIndex##_real_shape.size(),   \
                                                                     -1) :                                           \
                                                                 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);                                                                           \
    yolo1.set_input_##intputName(placeholder##intputIndex);                                                          \
    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>(outputShape.size(), -1) :               \
                                                            outputShape;                                            \
    TensorDesc outputName##outputIndex##_desc = TensorDesc(ge::Shape(output##outputIndex##_graph_shape), FORMAT_ND, \
                                                           outputDtype);                                            \
    yolo1.update_output_desc_##outputName(outputName##outputIndex##_desc);

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 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];

    for (size_t i = 0; i < size; ++i) {
        *(pData + i) = value;
    }
    input_tensor = Tensor(input_tensor_desc, (uint8_t*)pData, data_len);
    delete[] pData;
    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];
    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;
}

int CreateOppInGraph(RunMode mode, DataType inDtype, const ShapeCombo& combo, std::vector<ge::Tensor>& input,
                     std::vector<Operator>& inputs, std::vector<Operator>& outputs, Graph& graph)
{
    Status ret = SUCCESS;
    auto yolo1 = op::Yolo("yolo1");
    ADD_INPUT_MODE(1, x, inDtype, combo.xShape, mode);

    int64_t N = combo.xShape[0];
    int64_t H = combo.xShape[2];
    int64_t W = combo.xShape[3];
    int64_t HW = H * W;
    int64_t B = combo.boxes;
    int64_t coordCnt = combo.coords;
    int64_t K = combo.classes;

    vector<int64_t> coord_data_shape = {N, B * coordCnt, HW};
    vector<int64_t> obj_prob_shape = {N, B * HW};
    vector<int64_t> classes_prob_shape = {N, K, B * HW};

    ADD_OUTPUT_MODE(1, coord_data, inDtype, coord_data_shape, mode);
    ADD_OUTPUT_MODE(2, obj_prob, inDtype, obj_prob_shape, mode);
    ADD_OUTPUT_MODE(3, classes_prob, inDtype, classes_prob_shape, mode);

    yolo1.set_attr_boxes(combo.boxes);
    yolo1.set_attr_coords(combo.coords);
    yolo1.set_attr_classes(combo.classes);
    yolo1.SetAttr("yolo_version", combo.yolo_version.c_str());
    yolo1.set_attr_softmax(combo.softmax);
    yolo1.set_attr_background(combo.background);
    yolo1.set_attr_softmaxtree(false);

    outputs.push_back(yolo1);
    return SUCCESS;
}

CaseResult RunOneCase(ge::Session* session, uint32_t graph_id, RunMode mode, DataType dtype, const ShapeCombo& combo,
                      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, combo, 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", "0"}, {"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());

    struct DtypeEntry {
        DataType dt;
        std::string name;
    };
    std::vector<DtypeEntry> dtype_list = {
        {DT_FLOAT, "FP32"},
        {DT_FLOAT16, "FP16"},
    };

    // Yolo requires 4D NCHW input (N, boxes*(coords+1+classes), H, W) with H*W > 0.
    // Standard scenarios (scalar, 1D, empty, 8D) are invalid for Yolo and skipped.
    // Each scenario carries its own attr values since C = boxes*(coords+1+classes).
    std::vector<ShapeCombo> shape_list = {
        {{1, 255, 13, 13}, 3, 4, 80, "V3", false, false, "v3_13x13"},
        {{1, 255, 26, 26}, 3, 4, 80, "V3", false, false, "v3_26x26"},
        {{1, 255, 52, 52}, 3, 4, 80, "V3", false, false, "v3_52x52"},
        {{1, 125, 13, 13}, 5, 4, 20, "V2", true, false, "v2_13x13_mode2"},
        {{1, 30, 4, 4}, 5, 4, 1, "V2", false, true, "small_4x4_mode3"},
    };

    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;

    for (const auto& d : dtype_list) {
        for (const auto& s : shape_list) {
            // Dynamic shape (_D) skipped: yolo output shape depends on attrs (boxes/coords/classes),
            // GE infershape fails on dynamic input shape with ret=4294967295. Static shape (_S) all pass.
            for (auto mode : {RUN_MODE_S}) {
                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, 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;
}