Line data Source code
1 : /**
2 : * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "aclnn_foreach_mul_scalar_v2.h"
12 : #include "foreach_mul_scalar_v2.h"
13 : #include "aclnn_kernels/contiguous.h"
14 : #include "op_api/aclnn_util.h"
15 : #include "op_api/op_api_def_nn.h"
16 : #include "aclnn_kernels/common/op_error_check.h"
17 : #include "opdev/op_dfx.h"
18 : #include "opdev/make_op_executor.h"
19 : #include "opdev/platform.h"
20 :
21 : using namespace op;
22 :
23 : #ifdef __cplusplus
24 : extern "C" {
25 : #endif
26 :
27 : static const std::initializer_list<DataType> TENSOR_DTYPE_DTYPE_SUPPORT_LIST = {
28 : DataType::DT_FLOAT, DataType::DT_FLOAT16, DataType::DT_BF16, DataType::DT_INT32};
29 :
30 : static const std::initializer_list<DataType> FOREACH_SCALAR_FLOAT_SUPPORT_LIST = {DataType::DT_FLOAT,
31 : DataType::DT_DOUBLE};
32 :
33 : static const std::initializer_list<DataType> FOREACH_SCALAR_FLOAT16_SUPPORT_LIST = {DataType::DT_FLOAT16,
34 : DataType::DT_DOUBLE};
35 :
36 : static const std::initializer_list<DataType> FOREACH_SCALAR_FLOAT16_SUPPORT_LIST_910D = {
37 : DataType::DT_FLOAT16, DataType::DT_FLOAT, DataType::DT_DOUBLE};
38 :
39 : static const std::initializer_list<DataType> FOREACH_SCALAR_INT_SUPPORT_LIST = {DataType::DT_INT32, DataType::DT_INT64};
40 :
41 : static const std::initializer_list<DataType> EMPTY_LIST = {};
42 : static inline bool CheckNotNull(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
43 : {
44 : OP_CHECK_NULL(self, return false);
45 : OP_CHECK_NULL(scalar, return false);
46 : OP_CHECK_NULL(out, return false);
47 : return true;
48 : }
49 :
50 : static inline bool CheckFormat(const aclTensorList* self, const aclTensorList* out)
51 : {
52 : for (uint64_t j = 0; j < self->Size(); j++) {
53 : // self格式不能是私有格式
54 : if (IsPrivateFormat((*self)[j]->GetStorageFormat()) || IsPrivateFormat((*out)[j]->GetStorageFormat())) {
55 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Format only support ND, NCHW, NHWC, HWCN, NDHWC, NCDHW.");
56 : return false;
57 : }
58 : }
59 : return true;
60 : }
61 :
62 : static const std::initializer_list<DataType>& GetDtypeSupportList()
63 : {
64 : if (GetCurrentPlatformInfo().GetCurNpuArch() == NpuArch::DAV_2201 || Ops::NN::AclnnUtil::IsRegbase()) {
65 : return TENSOR_DTYPE_DTYPE_SUPPORT_LIST;
66 : } else {
67 : OP_LOGE(ACLNN_ERR_RUNTIME_ERROR, "support for arch %u is not implemented",
68 : static_cast<uint32_t>(GetCurrentPlatformInfo().GetCurNpuArch()));
69 : return EMPTY_LIST;
70 : }
71 : }
72 :
73 : static inline bool CheckDtype(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
74 : {
75 : const auto& dtypeSupportList = GetDtypeSupportList();
76 : if (dtypeSupportList.size() == 0) {
77 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "support for arch %u is not implemented",
78 : static_cast<uint32_t>(GetCurrentPlatformInfo().GetCurNpuArch()));
79 : return false;
80 : }
81 : if (self->Size() == 0) {
82 : return true;
83 : }
84 :
85 : // checkself input dtype, and check the releation of input and out
86 : OP_CHECK_NULL((*self)[0], return false);
87 : auto selfDtyte = (*self)[0]->GetDataType();
88 : OP_CHECK_DTYPE_NOT_SUPPORT((*self)[0], dtypeSupportList, return false);
89 : for (uint64_t i = 0; i < self->Size(); i++) {
90 : OP_CHECK_NULL((*self)[i], return false);
91 : OP_CHECK_DTYPE_NOT_MATCH((*self)[i], selfDtyte, return false);
92 : }
93 :
94 : for (uint64_t i = 0; i < out->Size(); i++) {
95 : OP_CHECK_NULL((*out)[i], return false);
96 : OP_CHECK_DTYPE_NOT_MATCH((*out)[i], selfDtyte, return false);
97 : }
98 :
99 : // check the releation of self and scalar
100 : if (selfDtyte == DataType::DT_BF16 || selfDtyte == DataType::DT_FLOAT) {
101 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT_SUPPORT_LIST, return false);
102 : } else if (selfDtyte == DataType::DT_FLOAT16) {
103 : if (Ops::NN::AclnnUtil::IsRegbase()) {
104 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT16_SUPPORT_LIST_910D, return false);
105 : } else {
106 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT16_SUPPORT_LIST, return false);
107 : }
108 : } else {
109 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_INT_SUPPORT_LIST, return false);
110 : }
111 : return true;
112 : }
113 :
114 : static inline bool CheckShape(const aclTensorList* self, const aclTensorList* out)
115 : {
116 : // tensorlist size检查
117 : OP_CHECK_TENSORLIST_SIZE_EQUAL(self, out, return false);
118 :
119 : // tensor 维度检查
120 : for (uint64_t n = 0; n < self->Size(); n++) {
121 : OP_CHECK_MAX_DIM((*self)[n], MAX_SUPPORT_DIMS_NUMS, return false);
122 : }
123 :
124 : // self和out的shape必须一致
125 : for (uint64_t n = 0; n < self->Size(); n++) {
126 : OP_CHECK_SHAPE_NOT_EQUAL((*self)[n], (*out)[n], return false);
127 : }
128 : return true;
129 : }
130 :
131 : static inline aclnnStatus CheckParams(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
132 : {
133 : // 1. 检查参数是否为空指针
134 : CHECK_RET(CheckNotNull(self, scalar, out), ACLNN_ERR_PARAM_NULLPTR);
135 :
136 : // Check every entry in tensor lists is not null, to avoid null pointer
137 : // dereference in CheckDtype/CheckShape/CheckFormat.
138 12 : for (uint64_t i = 0; i < self->Size(); i++) {
139 6 : if ((*self)[i] == nullptr) {
140 0 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "self[%lu] is null.", i);
141 0 : return ACLNN_ERR_PARAM_INVALID;
142 : }
143 : }
144 12 : for (uint64_t i = 0; i < out->Size(); i++) {
145 6 : if ((*out)[i] == nullptr) {
146 0 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "out[%lu] is null.", i);
147 0 : return ACLNN_ERR_PARAM_INVALID;
148 : }
149 : }
150 :
151 : // 2. 检查输入的数据类型是否在API支持的数据类型范围之内,需要根据api定义校验
152 : CHECK_RET(CheckDtype(self, scalar, out), ACLNN_ERR_PARAM_INVALID);
153 : // 3. 检查shape是否满足约束
154 : CHECK_RET(CheckShape(self, out), ACLNN_ERR_PARAM_INVALID);
155 : // 4. 检查Format是否满足约束
156 : CHECK_RET(CheckFormat(self, out), ACLNN_ERR_PARAM_INVALID);
157 : return ACLNN_SUCCESS;
158 : }
159 :
160 : static aclnnStatus ExecForeachMulScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar,
161 : const aclTensorList* out, uint64_t* workspaceSize,
162 : aclOpExecutor** executor)
163 : {
164 : // 固定写法,创建OpExecutor
165 : auto uniqueExecutor = CREATE_EXECUTOR();
166 : CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
167 :
168 : // 固定写法,参数检查
169 : auto ret_1 = CheckParams(x, scalar, out);
170 : CHECK_RET(ret_1 == ACLNN_SUCCESS, ret_1);
171 :
172 : // 空Tensorlist处理
173 : if (x->Size() == 0) {
174 : *workspaceSize = 0;
175 : uniqueExecutor.ReleaseTo(executor);
176 : return ACLNN_SUCCESS;
177 : }
178 :
179 : // self如果非连续,需要转连续
180 : std::vector<const aclTensor*> tensorsVec;
181 : for (size_t j = 0; j < x->Size(); ++j) {
182 : auto secondContiguous = l0op::Contiguous((*x)[j], uniqueExecutor.get());
183 : CHECK_RET(secondContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
184 : tensorsVec.push_back(secondContiguous);
185 : }
186 : auto contiguousTensors = uniqueExecutor.get()->AllocTensorList(tensorsVec.data(), tensorsVec.size());
187 : CHECK_RET(contiguousTensors != nullptr, ACLNN_ERR_INNER_NULLPTR);
188 :
189 : // sclar to tensor
190 : const aclTensor* otherTensor;
191 : if ((*x)[0]->GetDataType() == DataType::DT_BF16) {
192 : otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, DataType::DT_FLOAT);
193 : } else if (Ops::NN::AclnnUtil::IsRegbase() && (*x)[0]->GetDataType() == DataType::DT_FLOAT16 &&
194 : (scalar->GetDataType() == DataType::DT_FLOAT || scalar->GetDataType() == DataType::DT_DOUBLE)) {
195 : otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, DataType::DT_FLOAT);
196 : } else {
197 : otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, (*x)[0]->GetDataType());
198 : }
199 : CHECK_RET(otherTensor != nullptr, ACLNN_ERR_INNER_NULLPTR);
200 : // 调用l0算子ForeachMulScalarV2进行计算
201 : auto result = l0op::ForeachMulScalarV2(contiguousTensors, otherTensor, out, uniqueExecutor.get());
202 : CHECK_RET(result != nullptr, ACLNN_ERR_PARAM_INVALID);
203 :
204 : // 固定写法,获取计算过程中需要使用的workspace大小
205 : *workspaceSize = uniqueExecutor->GetWorkspaceSize();
206 : uniqueExecutor.ReleaseTo(executor);
207 : return ACLNN_SUCCESS;
208 : }
209 :
210 : aclnnStatus aclnnForeachMulScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar, aclTensorList* out,
211 : uint64_t* workspaceSize, aclOpExecutor** executor)
212 : {
213 : L2_DFX_PHASE_1(aclnnForeachMulScalarV2, DFX_IN(x, scalar), DFX_OUT(out));
214 : return ExecForeachMulScalarV2GetWorkspaceSize(x, scalar, out, workspaceSize, executor);
215 : }
216 :
217 : aclnnStatus aclnnForeachMulScalarV2(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor,
218 : const aclrtStream stream)
219 : {
220 : L2_DFX_PHASE_2(aclnnForeachMulScalarV2);
221 : return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
222 : }
223 :
224 : #ifdef __cplusplus
225 : }
226 : #endif
|