/**
 * 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 <cmath>
#include <cstdint>
#include <iostream>
#include <map>
#include <memory>
#include <vector>

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

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

namespace {

constexpr uint32_t GRAPH_ID = 0U;
constexpr int64_t STEPS = 4;
constexpr float START = 0.0F;
constexpr float END = 3.0F;
constexpr float BASE = 2.0F;
constexpr float ABS_TOLERANCE = 1.0e-6F;

ge::Tensor MakeAssistTensor(const ge::TensorDesc& desc)
{
    const std::vector<float> values = {37.0F};
    const auto* firstByte = reinterpret_cast<const uint8_t*>(values.data());
    const std::vector<uint8_t> bytes(firstByte, firstByte + values.size() * sizeof(float));
    return ge::Tensor(desc, bytes);
}

bool CheckOutput(const std::vector<ge::Tensor>& outputs)
{
    if (outputs.size() != 1U || outputs[0].GetTensorDesc().GetDataType() != ge::DT_FLOAT ||
        outputs[0].GetTensorDesc().GetShape().GetShapeSize() != STEPS) {
        return false;
    }
    const std::vector<float> expected = {1.0F, 2.0F, 4.0F, 8.0F};
    if (outputs[0].GetSize() != expected.size() * sizeof(float)) {
        return false;
    }
    const float* actual = reinterpret_cast<const float*>(outputs[0].GetData());
    if (actual == nullptr) {
        return false;
    }
    for (size_t i = 0; i < expected.size(); ++i) {
        if (std::fabs(actual[i] - expected[i]) > ABS_TOLERANCE) {
            std::cerr << "LogSpaceD output mismatch at " << i << ": actual=" << actual[i]
                      << ", expected=" << expected[i] << std::endl;
            return false;
        }
    }
    return true;
}

int RunGraph()
{
    ge::Graph graph("log_space_d_example");
    auto assist = ge::op::Data("assist").set_attr_index(0);
    ge::TensorDesc assistDesc(ge::Shape({1}), ge::FORMAT_ND, ge::DT_FLOAT);
    assistDesc.SetPlacement(ge::kPlacementHost);
    assist.update_input_desc_x(assistDesc);
    assist.update_output_desc_y(assistDesc);

    auto logSpaceD = ge::op::LogSpaceD("log_space_d")
                         .set_input_assist(assist)
                         .set_attr_start(START)
                         .set_attr_end(END)
                         .set_attr_steps(STEPS)
                         .set_attr_base(BASE)
                         .set_attr_dtype(1);
    ge::TensorDesc outputDesc(ge::Shape({STEPS}), ge::FORMAT_ND, ge::DT_FLOAT);
    logSpaceD.update_output_desc_y(outputDesc);

    graph.SetInputs(std::vector<ge::Operator>{assist});
    graph.SetOutputs(std::vector<ge::Operator>{logSpaceD});

    std::map<ge::AscendString, ge::AscendString> sessionOptions;
    auto session = std::make_unique<ge::Session>(sessionOptions);
    std::map<ge::AscendString, ge::AscendString> graphOptions;
    if (session == nullptr || session->AddGraph(GRAPH_ID, graph, graphOptions) != ge::SUCCESS) {
        return 1;
    }

    std::vector<ge::Tensor> outputs;
    const std::vector<ge::Tensor> inputs = {MakeAssistTensor(assistDesc)};
    const ge::Status status = session->RunGraph(GRAPH_ID, inputs, outputs);
    const ge::Status removeStatus = session->RemoveGraph(GRAPH_ID);
    return status == ge::SUCCESS && removeStatus == ge::SUCCESS && CheckOutput(outputs) ? 0 : 1;
}

} // namespace

int main()
{
    const std::map<ge::AscendString, ge::AscendString> options = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}};
    if (ge::GEInitialize(options) != ge::SUCCESS) {
        std::cerr << "GEInitialize failed" << std::endl;
        return 1;
    }
    const int result = RunGraph();
    const ge::Status finalizeStatus = ge::GEFinalize();
    if (result != 0 || finalizeStatus != ge::SUCCESS) {
        std::cerr << "LogSpaceD GE example failed" << std::endl;
        return 1;
    }
    std::cout << "LogSpaceD GE example passed" << std::endl;
    return 0;
}