/**
 * 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 kl_div_apt.cpp
 * \brief KLDiv 路由 kernel 纯转发层
 */

#include "atvoss/reduce/reduce_sch.h"
#include "kl_div_tiling_key.h"
#include "../kl_div_v2/kl_div_v2_dag.h"

#include "../kl_div_v2/kl_div_v2_tiling_data.h"

using namespace Ops::Base;
using namespace ReduceOpTmpl;
using namespace AscendC;

enum Reduction {
    None = 0,     // STR_2_INT: "none"
    Mean = 1,     //           "mean"
    Sum = 2,      //           "sum"
    Batchmean = 3 //           "batchmean"
};

//   (V2: <REDUCE_TPL_PARAM, int32_t reduction, int32_t logTarget>)
template <REDUCE_TPL_PARAM, int32_t reduction>
__global__ __aicore__ void kl_div(GM_ADDR x, GM_ADDR target, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling)
{
    REGISTER_TILING_DEFAULT(KLDivV2TilingData); // TilingData 结构复用 V2
    GET_TILING_DATA_WITH_STRUCT(KLDivV2TilingData, tilingData, tiling);
    TPipe pipe;
    if constexpr (reduction == Reduction::Mean || reduction == Reduction::Batchmean) {
        // mean / batchmean → MeanLogFalse DAG(meanVar 装载 + SetVar,Muls 单次乘法)
        using Op = ReduceSch<REDUCE_TPL_VALUE, KLDivV2::KLDivDagMeanLogFalse<DTYPE_X, float>::OpDag>;
        Op op((ReduceOpTilingData*)&tilingData.reduceTiling);
        op.template SetVar<float, 0>(tilingData.reduceTiling.meanVar);
        op.Init(&pipe, x, target, y, workspace);
        op.Process(tilingData.emptyValue); // 空 tensor 时短路输出 emptyValue
    } else {                               // Sum / None → SumLogFalse DAG
        using Op = ReduceSch<REDUCE_TPL_VALUE, KLDivV2::KLDivDagSumLogFalse<DTYPE_X, float>::OpDag>;
        Op op((ReduceOpTilingData*)&tilingData.reduceTiling);
        op.Init(&pipe, x, target, y, workspace);
        op.Process(tilingData.emptyValue);
    }
}