/**
 * 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 sinh_tiling.cpp
 * \brief Sinh Host Tiling implementation (atvoss Elewise mode)
 */

#include "register/op_def_registry.h"
#include "op_common/log/log.h"
#include "atvoss/elewise/elewise_tiling.h"
#include "../../op_kernel/arch35/sinh_dag.h"
#include "../../op_kernel/arch35/sinh_struct.h"

namespace optiling {
using namespace ge;

// atvoss DAG scheduler workspace (required for all Elewise operators)
const size_t ASCEND_WORKSPACE = 16777216; // 16 * 1024 * 1024

static ge::graphStatus SinhTilingFunc(gert::TilingContext* context)
{
    auto tilingData = context->GetTilingData<SinhTilingData>();

    auto inputDesc = context->GetInputDesc(0);
    if (inputDesc == nullptr) {
        OP_LOGE(context, "Sinh: GetInputDesc(0) returned nullptr");
        return ge::GRAPH_FAILED;
    }
    ge::DataType dtype = inputDesc->GetDataType();

    ::Ops::Base::ElewiseBaseTiling elewiseBaseTiling(context);
    ge::graphStatus ret;

    // Select DAG variant based on dtype
    if (dtype == ge::DT_FLOAT16) {
        ret = elewiseBaseTiling.DoTiling<NsSinh::SinhWithCast<half>::OpDag>(
            tilingData->baseTiling);
    } else if (dtype == ge::DT_BF16) {
        ret = elewiseBaseTiling.DoTiling<NsSinh::SinhWithCast<bfloat16_t>::OpDag>(
            tilingData->baseTiling);
    } else {
        // float32
        ret = elewiseBaseTiling.DoTiling<NsSinh::SinhWithoutCast<float>::OpDag>(
            tilingData->baseTiling);
    }

    if (ret != ge::GRAPH_SUCCESS) {
        OP_LOGE(context, "Sinh: ElewiseBaseTiling DoTiling failed");
        return ret;
    }

    // Allocate workspace for atvoss DAG scheduler (required)
    size_t* currentWorkspace = context->GetWorkspaceSizes(1);
    if (currentWorkspace != nullptr) {
        currentWorkspace[0] = ASCEND_WORKSPACE;
    }

    // Set block dimension from tiling data's blockNum
    context->SetBlockDim(tilingData->baseTiling.blockNum);

    uint32_t tilingKey = GET_TPL_TILING_KEY(
        (uint64_t)tilingData->baseTiling.scheMode);
    context->SetTilingKey(tilingKey);

    OP_LOGI(context, "Sinh: Tiling success, blockDim=%ld, blockNum=%ld, coreNum=%d",
            elewiseBaseTiling.GetBlockDim(),
            tilingData->baseTiling.blockNum,
            tilingData->baseTiling.coreNum);
    return ge::GRAPH_SUCCESS;
}

static ge::graphStatus TilingParseForSinh(
    [[maybe_unused]] gert::TilingParseContext* context)
{
    return ge::GRAPH_SUCCESS;
}

struct SinhCompileInfo {};

IMPL_OP_OPTILING(Sinh)
    .Tiling(SinhTilingFunc)
    .TilingParse<SinhCompileInfo>(TilingParseForSinh);

} // namespace optiling