/**
 * 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 the root of the software repository for the full text of the License.
 */

#include "grouped_broadcast.h"
#include "opdev/op_log.h"
#include "opdev/op_dfx.h"
#include "opdev/shape_utils.h"
#include "opdev/make_op_executor.h"
#include "opdev/platform.h"
#include "opdev/data_type_utils.h"

using namespace op;

namespace l0op {

OP_TYPE_REGISTER(GroupedBroadcast);

static const std::initializer_list<op::DataType> AICORE_DTYPE_SUPPORT_LIST = {
    DataType::DT_FLOAT16,
    DataType::DT_BF16,
    DataType::DT_FLOAT
};

static bool IsAiCoreSupport(const aclTensor* x, const aclTensor* group_list)
{
    auto npuArch = GetCurrentPlatformInfo().GetCurNpuArch();
    OP_CHECK(npuArch == NpuArch::DAV_2201,
             OP_LOGE(ACLNN_ERR_PARAM_INVALID,
                     "GroupedBroadcast not supported on this platform: npuArch=%d.",
                     static_cast<int>(npuArch)),
             return false);

    OP_CHECK(CheckType(x->GetDataType(), AICORE_DTYPE_SUPPORT_LIST),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID,
                     "GroupedBroadcast not supported: dtype x=%d. Supported dtypes: FLOAT16, BF16, FLOAT.",
                     static_cast<int>(x->GetDataType())),
             return false);

    OP_CHECK(group_list->GetDataType() == DataType::DT_INT64,
             OP_LOGE(ACLNN_ERR_PARAM_INVALID,
                     "group_list must be INT64 type, got dtype=%d.",
                     static_cast<int>(group_list->GetDataType())),
             return false);

    return true;
}

static bool GroupedBroadcastInferShape(const aclTensor* x, const aclTensor* group_list,
                                        const aclTensor* assist,
                                        op::Shape& outShape)
{
    auto xShape = x->GetViewShape();
    auto group_listShape = group_list->GetViewShape();

    OP_CHECK(xShape.GetDimNum() == 2,
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "x must be 2D tensor, got dimNum=%zu.", xShape.GetDimNum()),
             return false);

    OP_CHECK(group_listShape.GetDimNum() == 1,
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "group_list must be 1D tensor, got dimNum=%zu.", group_listShape.GetDimNum()),
             return false);

    OP_CHECK(group_listShape.GetDim(0) == xShape.GetDim(0),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID,
                     "group_list length must equal x rows: group_list.dim0=%ld, x.dim0=%ld.",
                     group_listShape.GetDim(0), xShape.GetDim(0)),
             return false);

    // group_list语义为累积和(前缀和),NL = group_list[N-1]

    OP_CHECK(assist != nullptr,
             OP_LOGE(ACLNN_ERR_PARAM_NULLPTR, "assist is nullptr."),
             return false);

    auto assistShape = assist->GetViewShape();
    OP_CHECK(assistShape.GetDimNum() == 2,
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "assist must be 2D tensor, got dimNum=%zu.", assistShape.GetDimNum()),
             return false);

    OP_CHECK(assist->GetDataType() == x->GetDataType(),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "assist dtype must match x dtype: assist=%d, x=%d.",
                     static_cast<int>(assist->GetDataType()), static_cast<int>(x->GetDataType())),
             return false);

    OP_CHECK(assistShape.GetDim(1) == xShape.GetDim(1),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID,
                     "assist.dim(1) must equal x.dim(1): assistD=%ld, xD=%ld.",
                     assistShape.GetDim(1), xShape.GetDim(1)),
             return false);

    outShape = Shape({assistShape.GetDim(0), assistShape.GetDim(1)});
    return true;
}

static const aclTensor* GroupedBroadcastAiCore(const aclTensor* x, const aclTensor* group_list,
                                                const aclTensor* assist,
                                                const aclTensor* out, aclOpExecutor* executor)
{
    L0_DFX(GroupedBroadcastAiCore, x, group_list, assist, out);

    auto ret = ADD_TO_LAUNCHER_LIST_AICORE(GroupedBroadcast,
        OP_INPUT(x, group_list, assist), OP_OUTPUT(out));

    OP_CHECK(ret == ACLNN_SUCCESS,
             OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "GroupedBroadcastAiCore failed."),
             return nullptr);

    return out;
}

const aclTensor* GroupedBroadcast(const aclTensor* x, const aclTensor* group_list,
                                   const aclTensor* assist, aclOpExecutor* executor)
{
    Shape outShape;

    OP_CHECK(GroupedBroadcastInferShape(x, group_list, assist, outShape),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Infer shape failed."),
             return nullptr);

    OP_CHECK(IsAiCoreSupport(x, group_list),
             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "IsAiCoreSupport check failed."),
             return nullptr);

    auto out = executor->AllocTensor(outShape, x->GetDataType());
    OP_CHECK(out != nullptr,
             OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "AllocTensor failed for output tensor."),
             return nullptr);

    return GroupedBroadcastAiCore(x, group_list, assist, out, executor);
}

} // namespace l0op