* 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 "aclnn_linalg_cross.h"
#include "cross.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/common/op_error_check.h"
#include "opdev/op_dfx.h"
#include "conversion/broadcast_to/op_api/broadcast_to.h"
#include "op_api/op_api_def.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE, op::DataType::DT_INT8,
op::DataType::DT_INT16, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_COMPLEX64,
op::DataType::DT_COMPLEX128, op::DataType::DT_UINT8};
static const std::initializer_list<op::DataType> DTYPE_SUPPORT_LIST_910B = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE, op::DataType::DT_INT8,
op::DataType::DT_INT16, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_COMPLEX64,
op::DataType::DT_COMPLEX128, op::DataType::DT_UINT8, op::DataType::DT_BF16};
inline static bool CheckNotNull(const aclTensor *self, const aclTensor *other, const aclTensor *out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(other, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static bool CheckDtypeValid(const aclTensor *self, const aclTensor *other, const aclTensor *out)
{
if (op::GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_2201 ||
op::GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_3510) {
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST_910B, return false);
} else {
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, return false);
}
OP_CHECK_DTYPE_NOT_MATCH(self, other->GetDataType(), return false);
OP_CHECK_DTYPE_NOT_MATCH(self, out->GetDataType(), return false);
return true;
}
static bool CheckShape(const aclTensor *self, const aclTensor *other, const aclTensor *out)
{
OP_CHECK_MAX_DIM(self, MAX_SUPPORT_DIMS_NUMS, return false);
OP_CHECK_MAX_DIM(other, MAX_SUPPORT_DIMS_NUMS, return false);
OP_CHECK_MAX_DIM(out, MAX_SUPPORT_DIMS_NUMS, return false);
return true;
}
static bool CheckBroadcastShape(const aclTensor *self, const aclTensor *other, const aclTensor *out)
{
OP_CHECK_BROADCAST(self, other, return false);
op::Shape broadcastShape;
BroadcastInferShape(self->GetViewShape(), other->GetViewShape(), broadcastShape);
if (broadcastShape != out->GetViewShape()) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"expected consistent tensor shape for the broadcast shape and out, but got %s and %s respectively.",
op::ToString(broadcastShape).GetString(), op::ToString(out->GetViewShape()).GetString());
return false;
}
return true;
}
static bool CheckDim(const aclTensor *self, int64_t dim)
{
auto dimSize = static_cast<int64_t>(self->GetViewShape().GetDimNum());
if (dim >= dimSize || dim < -1 * dimSize) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID,
"Dimension out of range(expected to be in range of [%ld, %ld], but got %ld.",
-1 * dimSize, dimSize - 1, dim);
return false;
}
return true;
}
static aclnnStatus CheckParams(const aclTensor *self, const aclTensor *other, int64_t dim,
const aclTensor *out)
{
CHECK_RET(CheckNotNull(self, other, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckBroadcastShape(self, other, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDim(self, dim), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
inline static aclTensor* BroadcastTensor(const op::Shape dstShape, const aclTensor* src, aclOpExecutor* executor)
{
auto dstTensor = executor->AllocTensor(dstShape, src->GetDataType());
op::FVector<int64_t, op::MAX_DIM_NUM> broadcastDims = op::ToShapeVector(dstShape);
auto shape = executor->ConvertToTensor(broadcastDims.data(), broadcastDims.size(),
static_cast<op::DataType>(ACL_INT64));
auto result = l0op::BroadcastTo(src, dstTensor, shape, executor);
return const_cast<aclTensor*>(result);
}
static aclnnStatus ExecLinalgCrossGetWorkspaceSize(const aclTensor *self, const aclTensor *other,
int64_t dim, aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor)
{
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, other, dim, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (out->IsEmpty()) {
OP_LOGD("empty input tensor");
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACL_SUCCESS;
}
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto otherContiguous = l0op::Contiguous(other, uniqueExecutor.get());
CHECK_RET(otherContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
const aclTensor *selfBroadCast = selfContiguous;
const aclTensor *otherBroadCast = otherContiguous;
auto dimInner = dim;
if (selfContiguous->GetViewShape() != otherContiguous->GetViewShape() && !IsRegBase()) {
selfBroadCast = BroadcastTensor(out->GetViewShape(), selfContiguous, uniqueExecutor.get());
CHECK_RET(selfBroadCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
otherBroadCast = BroadcastTensor(out->GetViewShape(), otherContiguous, uniqueExecutor.get());
CHECK_RET(otherBroadCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (dimInner >= 0 && self->GetViewShape().GetDimNum() < other->GetViewShape().GetDimNum()) {
dimInner += (other->GetViewShape().GetDimNum() - self->GetViewShape().GetDimNum());
}
}
if (dimInner < 0) {
dimInner += selfBroadCast->GetViewShape().GetDimNum();
}
if (selfBroadCast->GetViewShape().GetDim(dimInner) != 3) {
if (dim < 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "dimension %ld does not have size 3.", dim);
} else {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "dimension %ld does not have size 3.", dimInner);
}
return ACLNN_ERR_PARAM_INVALID;
}
if (self->GetDataType() == op::DataType::DT_BF16 &&
op::GetCurrentPlatformInfo().GetCurNpuArch() != NpuArch::DAV_2201 &&
op::GetCurrentPlatformInfo().GetCurNpuArch() != NpuArch::DAV_3510) {
selfBroadCast = l0op::Cast(selfBroadCast, op::DataType::DT_FLOAT, uniqueExecutor.get());
CHECK_RET(selfBroadCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
otherBroadCast = l0op::Cast(otherBroadCast, op::DataType::DT_FLOAT, uniqueExecutor.get());
CHECK_RET(otherBroadCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto crossOpOut = l0op::Cross(selfBroadCast, otherBroadCast, dimInner, uniqueExecutor.get());
CHECK_RET(crossOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
if (self->GetDataType() == op::DataType::DT_BF16 &&
op::GetCurrentPlatformInfo().GetCurNpuArch() != NpuArch::DAV_2201 &&
op::GetCurrentPlatformInfo().GetCurNpuArch() != NpuArch::DAV_3510) {
crossOpOut = l0op::Cast(crossOpOut, op::DataType::DT_BF16, uniqueExecutor.get());
CHECK_RET(crossOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
}
auto viewCopyResult = l0op::ViewCopy(crossOpOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnLinalgCrossGetWorkspaceSize(const aclTensor *self, const aclTensor *other, int64_t dim,
aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor)
{
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnLinalgCross, DFX_IN(self, other, dim), DFX_OUT(out));
return ExecLinalgCrossGetWorkspaceSize(self, other, dim, out, workspaceSize, executor);
}
aclnnStatus aclnnLinalgCross(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor,
aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnLinalgCross);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif