* Copyright (c) 2025 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.
*/
#include "cross.h"
#include "opdev/aicpu/aicpu_task.h"
#include "opdev/make_op_executor.h"
#include "opdev/op_dfx.h"
#include "op_api/aclnn_check.h"
using namespace op;
namespace l0op {
OP_TYPE_REGISTER(Cross);
OP_TYPE_REGISTER(CrossV2);
static const int64_t FORMER_LLENGTH_BOUND = 10240;
static const int64_t TAIL_LLENGTH_BOUND = 64;
static const std::initializer_list<op::DataType> REGBASE_AICORE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> AICORE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_INT8,
op::DataType::DT_INT16, op::DataType::DT_INT32, op::DataType::DT_UINT8};
static const std::initializer_list<op::DataType> V2_AICORE_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16,
op::DataType::DT_INT16, op::DataType::DT_INT32,
op::DataType::DT_BF16};
inline static bool IsAiCoreSupport(const aclTensor *self)
{
auto curArch = GetCurrentPlatformInfo().GetCurNpuArch();
if (IsRegBase(curArch)) {
return CheckType(self->GetDataType(), REGBASE_AICORE_DTYPE_SUPPORT_LIST);
}
return CheckType(self->GetDataType(), AICORE_DTYPE_SUPPORT_LIST);
}
static inline bool IsV2AiCoreSupport(const aclTensor *self) {
if (IsRegBase()){
return false;
}
if (op::GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_2201 ||
op::GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_3510) {
return CheckType(self->GetDataType(), V2_AICORE_DTYPE_SUPPORT_LIST);
}
return false;
}
inline static const aclTensor *CrossAiCore(const aclTensor *self, const aclTensor *other, int64_t dim,
aclTensor *crossOut, aclOpExecutor *executor)
{
L0_DFX(CrossAiCore, self, other, dim, crossOut);
auto ret = ADD_TO_LAUNCHER_LIST_AICORE(Cross, OP_ATTR_NAMES({"dim"}), OP_INPUT(self, other), OP_OUTPUT(crossOut),
OP_ATTR(dim));
OP_CHECK(ret == ACL_SUCCESS, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "CrossAiCore ADD_TO_LAUNCHER_LIST_AICORE failed."), return nullptr);
return crossOut;
}
inline static const aclTensor *CrossV2AiCore(const aclTensor *self, const aclTensor *other, int64_t dim,
aclTensor *crossOut, aclOpExecutor *executor)
{
L0_DFX(CrossV2AiCore, self, other, crossOut, dim);
auto ret = ADD_TO_LAUNCHER_LIST_AICORE(CrossV2, OP_INPUT(self, other), OP_OUTPUT(crossOut),
OP_ATTR(dim));
OP_CHECK(ret == ACL_SUCCESS, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "CrossV2AiCore ADD_TO_LAUNCHER_LIST_AICORE failed."), return nullptr);
return crossOut;
}
inline static const aclTensor *CrossAiCpu(const aclTensor *self, const aclTensor *other, int64_t dim,
aclTensor *crossOut, aclOpExecutor *executor)
{
L0_DFX(CrossAiCpu, self, other, dim, crossOut);
static internal::AicpuTaskSpace space("Cross");
auto ret = ADD_TO_LAUNCHER_LIST_AICPU(Cross, OP_ATTR_NAMES({"dim"}), OP_INPUT(self, other), OP_OUTPUT(crossOut),
OP_ATTR(dim));
OP_CHECK(ret == ACL_SUCCESS, OP_LOGE(ACLNN_ERR_INNER_NULLPTR, "CrossAiCpu ADD_TO_LAUNCHER_LIST_AICPU failed."), return nullptr);
return crossOut;
}
const aclTensor *Cross(const aclTensor *self, const aclTensor *other, int64_t dim, aclOpExecutor *executor)
{
Shape broadcastShape;
if (!BroadcastInferShape(self->GetViewShape(), other->GetViewShape(), broadcastShape)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "Broadcast %s and %s failed.", op::ToString(self->GetViewShape()).GetString(),
op::ToString(other->GetViewShape()).GetString());
return nullptr;
}
auto crossOut = executor->AllocTensor(broadcastShape, self->GetDataType(), op::Format::FORMAT_ND);
if (IsV2AiCoreSupport(self)) {
return CrossV2AiCore(self, other, dim, crossOut, executor);
} else if (IsAiCoreSupport(self)) {
return CrossAiCore(self, other, dim, crossOut, executor);
} else {
return CrossAiCpu(self, other, dim, crossOut, executor);
}
}
}