* 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.
*/
* \file aclnn_trace.cpp
* \brief
*/
#include "aclnn_trace.h"
#include "trace.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/contiguous.h"
#include "opdev/common_types.h"
#include "opdev/data_type_utils.h"
#include "opdev/format_utils.h"
#include "opdev/op_dfx.h"
#include "opdev/op_executor.h"
#include "opdev/op_log.h"
#include "opdev/platform.h"
#include "opdev/shape_utils.h"
#include "opdev/tensor_view_utils.h"
#include "aclnn_kernels/common/op_error_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static const std::initializer_list<op::DataType> ASCEND910_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE, op::DataType::DT_COMPLEX64,
op::DataType::DT_COMPLEX128, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_INT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_BOOL};
static const std::initializer_list<op::DataType> ASCEND910B_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE, op::DataType::DT_COMPLEX64,
op::DataType::DT_COMPLEX128, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_INT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_BOOL, op::DataType::DT_BF16};
static const std::initializer_list<op::DataType> ASCEND950_DTYPE_SUPPORT_LIST = {
op::DataType::DT_FLOAT, op::DataType::DT_FLOAT16, op::DataType::DT_DOUBLE, op::DataType::DT_COMPLEX64,
op::DataType::DT_COMPLEX128, op::DataType::DT_INT32, op::DataType::DT_INT64, op::DataType::DT_INT16,
op::DataType::DT_INT8, op::DataType::DT_UINT8, op::DataType::DT_BOOL, op::DataType::DT_BF16};
static const size_t DIM_SUPPORT_ONLY = 2;
static const std::initializer_list<DataType>& GetDtypeSupportList()
{
if (GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_3510) {
return ASCEND950_DTYPE_SUPPORT_LIST;
} else if (GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_2201) {
return ASCEND910B_DTYPE_SUPPORT_LIST;
} else {
return ASCEND910_DTYPE_SUPPORT_LIST;
}
}
static bool CheckNotNull(const aclTensor* self, const aclTensor* out)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(out, return false);
return true;
}
static bool CheckDtypeValid(const aclTensor* self, const aclTensor* out)
{
auto DTYPE_SUPPORT_LIST = GetDtypeSupportList();
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST, return false);
auto outDtype = (IsIntegralType(self->GetDataType(), true)) ? op::DataType::DT_INT64 : self->GetDataType();
OP_CHECK_DTYPE_NOT_MATCH(out, outDtype, return false);
return true;
}
static bool CheckNonNegativeShape(const op::Shape& shape, const char* name)
{
for (size_t i = 0; i < shape.GetDimNum(); ++i) {
if (shape.GetDim(i) < 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "%s has a negative dimension at index %zu: %ld.", name, i,
shape.GetDim(i));
return false;
}
}
return true;
}
static bool CheckParamValid(const aclTensor* self, const aclTensor* out)
{
auto dim = self->GetViewShape().GetDimNum();
if (dim != DIM_SUPPORT_ONLY) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "trace: expected a matrix, but got tensor with dim %zu", dim);
return false;
}
dim = out->GetViewShape().GetDimNum();
if (dim != 0) {
OP_LOGE(ACLNN_ERR_PARAM_INVALID, "expected 0D output tensor, but got %s tensor.",
op::ToString(out->GetViewShape()).GetString());
return false;
}
return CheckNonNegativeShape(self->GetViewShape(), "self view shape") &&
CheckNonNegativeShape(self->GetStorageShape(), "self storage shape") &&
CheckNonNegativeShape(out->GetStorageShape(), "out storage shape");
}
static aclnnStatus CheckParams(const aclTensor* self, const aclTensor* out)
{
CHECK_RET(CheckNotNull(self, out), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckParamValid(self, out), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckDtypeValid(self, out), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnTraceGetWorkspaceSize(const aclTensor* self, aclTensor* out, uint64_t* workspaceSize,
aclOpExecutor** executor)
{
OP_CHECK_COMM_INPUT(workspaceSize, executor);
L2_DFX_PHASE_1(aclnnTrace, DFX_IN(self), DFX_OUT(out));
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(self, out);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
const aclTensor* traceOpOut;
if (self->IsEmpty()) {
const aclScalar* valueScalar = (uniqueExecutor.get())->AllocScalar(0);
CHECK_RET(valueScalar != nullptr, ACLNN_ERR_INNER_NULLPTR);
traceOpOut = (uniqueExecutor.get())->ConvertToTensor(valueScalar, out->GetDataType());
} else {
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto inputdtype = selfContiguous->GetDataType();
bool isLowPrecision = (inputdtype == DataType::DT_FLOAT16 || inputdtype == DataType::DT_BF16);
bool isNeedCast = isLowPrecision && GetCurrentPlatformInfo().GetSocVersion() != SocVersion::ASCEND950;
auto selfCast = isNeedCast ? l0op::Cast(selfContiguous, DataType::DT_FLOAT, uniqueExecutor.get()) :
selfContiguous;
CHECK_RET(selfCast != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto traceOpOutCast = l0op::Trace(selfCast, uniqueExecutor.get());
traceOpOut = isNeedCast ? l0op::Cast(traceOpOutCast, out->GetDataType(), uniqueExecutor.get()) : traceOpOutCast;
}
CHECK_RET(traceOpOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(traceOpOut, out, uniqueExecutor.get());
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnTrace(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnTrace);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif