* 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_fill_tensor.h"
#include "fill.h"
#include "aclnn_kernels/contiguous.h"
#include "aclnn_kernels/cast.h"
#include "aclnn_kernels/common/op_error_check.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/shape_utils.h"
#include "opdev/tensor_view_utils.h"
#include "opdev/platform.h"
#include "op_api/aclnn_check.h"
using namespace op;
#ifdef __cplusplus
extern "C" {
#endif
static constexpr size_t MAX_DIM_LEN = 8;
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_910 = {
DataType::DT_FLOAT, DataType::DT_INT32, DataType::DT_INT64, DataType::DT_FLOAT16,
DataType::DT_INT16, DataType::DT_INT8, DataType::DT_UINT8, DataType::DT_DOUBLE,
DataType::DT_BOOL, DataType::DT_COMPLEX64, DataType::DT_COMPLEX128};
static const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_910B = {
DataType::DT_FLOAT, DataType::DT_INT32, DataType::DT_INT64, DataType::DT_FLOAT16,
DataType::DT_INT16, DataType::DT_INT8, DataType::DT_UINT8, DataType::DT_DOUBLE,
DataType::DT_BOOL, DataType::DT_COMPLEX64, DataType::DT_COMPLEX128, DataType::DT_BF16};
static inline bool CheckNotNull(const aclTensor* self, const aclTensor* value)
{
OP_CHECK_NULL(self, return false);
OP_CHECK_NULL(value, return false);
return true;
}
static inline bool CheckDtypeValid(const aclTensor* self)
{
auto npuArch = op::GetCurrentPlatformInfo().GetCurNpuArch();
bool is910bSocVersion = (npuArch == NpuArch::DAV_2201 || IsRegBase(npuArch));
const std::initializer_list<DataType> DTYPE_SUPPORT_LIST_CURRENT =
is910bSocVersion ? DTYPE_SUPPORT_LIST_910B : DTYPE_SUPPORT_LIST_910;
OP_CHECK_DTYPE_NOT_SUPPORT(self, DTYPE_SUPPORT_LIST_CURRENT, return false);
return true;
}
static inline bool CheckShape(const aclTensor* self, const aclTensor* value)
{
OP_CHECK_MAX_DIM(self, MAX_DIM_LEN, return false);
auto& valueViewShape = value->GetViewShape();
if (valueViewShape.GetDimNum() > 1 || (valueViewShape.GetDimNum() == 1 && valueViewShape.GetDim(0) != 1)) {
OP_LOGE(
ACLNN_ERR_PARAM_INVALID, "value shape should be 0D or 1D with size = 1, but got %s.",
ToString(value->GetViewShape()).GetString());
return false;
}
return true;
}
static aclnnStatus CheckParams(const aclTensor* self, const aclTensor* value)
{
CHECK_RET(CheckNotNull(self, value), ACLNN_ERR_PARAM_NULLPTR);
CHECK_RET(CheckDtypeValid(self), ACLNN_ERR_PARAM_INVALID);
CHECK_RET(CheckShape(self, value), ACLNN_ERR_PARAM_INVALID);
return ACLNN_SUCCESS;
}
static inline FVector<int64_t> getShape(const aclTensor* self)
{
FVector<int64_t> shape;
size_t dimNum = self->GetViewShape().GetDimNum();
if (dimNum == 0) {
shape.push_back(1);
} else {
for (size_t idx = 0; idx < dimNum; idx++) {
int64_t tmpVal = self->GetViewShape().GetDim(idx);
shape.push_back(tmpVal);
}
}
return shape;
}
aclnnStatus aclnnInplaceFillTensorGetWorkspaceSize(
aclTensor* selfRef, const aclTensor* value, size_t* workspaceSize, aclOpExecutor** executor)
{
L2_DFX_PHASE_1(aclnnInplaceFillTensor, DFX_IN(selfRef, value), DFX_OUT());
auto uniqueExecutor = CREATE_EXECUTOR();
aclOpExecutor* executorP = uniqueExecutor.get();
CHECK_RET(executorP != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
auto ret = CheckParams(selfRef, value);
CHECK_RET(ret == ACLNN_SUCCESS, ret);
if (selfRef->IsEmpty()) {
*workspaceSize = 0;
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
FVector<int64_t> fillShape = getShape(selfRef);
const aclTensor* dims = executorP->ConvertToTensor(fillShape.data(), fillShape.size(), DataType::DT_INT64);
CHECK_RET(dims != nullptr, ACLNN_ERR_INNER_NULLPTR);
aclIntArray* shapeArray = executorP->AllocIntArray(fillShape.data(), fillShape.size());
CHECK_RET(shapeArray != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto contiguousValue = l0op::Contiguous(value, uniqueExecutor.get());
CHECK_RET(contiguousValue != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto castValue = l0op::Cast(contiguousValue, selfRef->GetDataType(), uniqueExecutor.get());
CHECK_RET(castValue != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto fillOut = l0op::Fill(dims, castValue, shapeArray, executorP);
CHECK_RET(fillOut != nullptr, ACLNN_ERR_INNER_NULLPTR);
auto viewCopyResult = l0op::ViewCopy(fillOut, selfRef, executorP);
CHECK_RET(viewCopyResult != nullptr, ACLNN_ERR_INNER_NULLPTR);
*workspaceSize = uniqueExecutor->GetWorkspaceSize();
uniqueExecutor.ReleaseTo(executor);
return ACLNN_SUCCESS;
}
aclnnStatus aclnnInplaceFillTensor(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)
{
L2_DFX_PHASE_2(aclnnInplaceFillTensor);
return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
}
#ifdef __cplusplus
}
#endif