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_maximum_scalar_v2.h"
12 : #include "foreach_maximum_scalar_v2.h"
13 : #include "aclnn_kernels/contiguous.h"
14 : #include "op_api/op_api_def_nn.h"
15 : #include "opdev/platform.h"
16 : #include "op_api/aclnn_util.h"
17 : #include "aclnn_kernels/common/op_error_check.h"
18 : #include "opdev/op_dfx.h"
19 : #include "opdev/make_op_executor.h"
20 :
21 : using namespace op;
22 :
23 : #ifdef __cplusplus
24 : extern "C" {
25 : #endif
26 :
27 : namespace {
28 : const float FLOAT32_MAX_VALUE = 3.4028235e+38f;
29 : const float FLOAT32_MIN_VALUE = -3.4028235e+38f;
30 : const float FLOAT16_MAX_VALUE = 65504.0f;
31 : const float FLOAT16_MIN_VALUE = -65504.0f;
32 : const float BFLOAT16_MAX_VALUE = 3.3895314e+38f;
33 : const float BFLOAT16_MIN_VALUE = -3.3895314e+38f;
34 : const int32_t INT32_MIN_VAL = -2147483648;
35 : const int32_t INT32_MAX_VAL = 2147483647;
36 : } // namespace
37 :
38 : static const std::initializer_list<DataType> ASCEND910BC_TENSOR_DTYPE_DTYPE_SUPPORT_LIST = {
39 : DataType::DT_FLOAT, DataType::DT_FLOAT16, DataType::DT_BF16, DataType::DT_INT32};
40 :
41 : static const std::initializer_list<DataType> FOREACH_SCALAR_FLOAT_SUPPORT_LIST = {DataType::DT_FLOAT,
42 : DataType::DT_DOUBLE};
43 :
44 : static const std::initializer_list<DataType> FOREACH_SCALAR_FLOAT16_SUPPORT_LIST = {DataType::DT_FLOAT16,
45 : DataType::DT_DOUBLE};
46 :
47 : static const std::initializer_list<DataType> FOREACH_SCALAR_INT_SUPPORT_LIST = {DataType::DT_INT32, DataType::DT_INT64};
48 :
49 : static const std::initializer_list<DataType> EMPTY_LIST = {};
50 :
51 : static inline bool CheckNotNull(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
52 : {
53 : OP_CHECK_NULL(self, return false);
54 : OP_CHECK_NULL(scalar, return false);
55 : OP_CHECK_NULL(out, return false);
56 : return true;
57 : }
58 :
59 : static inline bool CheckFormat(const aclTensorList* self, const aclTensorList* out)
60 : {
61 : for (uint64_t k = 0; k < self->Size(); k++) {
62 : // self格式不能是私有格式
63 : if (IsPrivateFormat((*self)[k]->GetStorageFormat()) || IsPrivateFormat((*out)[k]->GetStorageFormat())) {
64 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Format only support ND, NCHW, NHWC, HWCN, NDHWC, NCDHW.");
65 : return false;
66 : }
67 : }
68 : return true;
69 : }
70 :
71 : static const std::initializer_list<DataType>& GetDtypeSupportList()
72 : {
73 : auto curArch_1 = GetCurrentPlatformInfo().GetCurNpuArch();
74 : if (curArch_1 == NpuArch::DAV_2201 || Ops::NN::AclnnUtil::IsRegbase(curArch_1)) {
75 : return ASCEND910BC_TENSOR_DTYPE_DTYPE_SUPPORT_LIST;
76 : } else {
77 : OP_LOGE(ACLNN_ERR_RUNTIME_ERROR, "support for %s is not implemented",
78 : op::ToString(GetCurrentPlatformInfo().GetSocVersion()).GetString());
79 : return EMPTY_LIST;
80 : }
81 : }
82 :
83 : static inline bool CheckDtypeValid(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
84 : {
85 : const auto& dtypeSupportList_1 = GetDtypeSupportList();
86 : if (dtypeSupportList_1.size() == 0) {
87 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "support for %s is not implemented",
88 : op::ToString(GetCurrentPlatformInfo().GetSocVersion()).GetString());
89 : return false;
90 : }
91 : if (self->Size() == 0) {
92 : return true;
93 : }
94 :
95 : // checkself input dtype, and check the releation of input and out
96 : auto selfDtyte = (*self)[0]->GetDataType();
97 : OP_CHECK_DTYPE_NOT_SUPPORT((*self)[0], dtypeSupportList_1, return false);
98 : for (uint64_t j = 0; j < self->Size(); j++) {
99 : OP_CHECK_DTYPE_NOT_MATCH((*self)[j], selfDtyte, return false);
100 : }
101 :
102 : for (uint64_t j = 0; j < out->Size(); j++) {
103 : OP_CHECK_DTYPE_NOT_MATCH((*out)[j], selfDtyte, return false);
104 : }
105 :
106 : // check the releation of self and scalar
107 : if (selfDtyte == DataType::DT_BF16 || selfDtyte == DataType::DT_FLOAT) {
108 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT_SUPPORT_LIST, return false);
109 : } else if (selfDtyte == DataType::DT_FLOAT16) {
110 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT16_SUPPORT_LIST, return false);
111 : } else {
112 : OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_INT_SUPPORT_LIST, return false);
113 : }
114 : return true;
115 : }
116 :
117 : static inline bool CheckShapeValid(const aclTensorList* self, const aclTensorList* out)
118 : {
119 : // tensor 维度检查
120 : for (uint64_t j = 0; j < self->Size(); j++) {
121 : OP_CHECK_MAX_DIM((*self)[j], MAX_SUPPORT_DIMS_NUMS, return false);
122 : }
123 :
124 : // self和out的shape必须一致
125 : for (uint64_t j = 0; j < self->Size(); j++) {
126 : OP_CHECK_SHAPE_NOT_EQUAL((*self)[j], (*out)[j], 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 CheckDtypeValid/CheckShapeValid/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(CheckDtypeValid(self, scalar, out), ACLNN_ERR_PARAM_INVALID);
153 : // 3. 检查shape是否满足约束
154 : CHECK_RET(CheckShapeValid(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 inline aclnnStatus CheckScalarValueValid(const aclScalar* scalar, const DataType tensorDtype)
161 : {
162 : if (scalar->GetDataType() == tensorDtype) {
163 : return ACLNN_SUCCESS;
164 : }
165 :
166 : double val_1 = static_cast<double>(scalar->ToDouble());
167 :
168 : switch (tensorDtype) {
169 : case DataType::DT_INT32:
170 : if (val_1 < static_cast<double>(INT32_MIN_VAL) || val_1 > static_cast<double>(INT32_MAX_VAL)) {
171 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Scalar value exceeds int32 range.");
172 : return ACLNN_ERR_PARAM_INVALID;
173 : }
174 : return ACLNN_SUCCESS;
175 : case DataType::DT_FLOAT:
176 : if (val_1 < static_cast<double>(FLOAT32_MIN_VALUE) || val_1 > static_cast<double>(FLOAT32_MAX_VALUE)) {
177 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Scalar value exceeds float32 range.");
178 : return ACLNN_ERR_PARAM_INVALID;
179 : }
180 : return ACLNN_SUCCESS;
181 : case DataType::DT_FLOAT16:
182 : if (val_1 < static_cast<double>(FLOAT16_MIN_VALUE) || val_1 > static_cast<double>(FLOAT16_MAX_VALUE)) {
183 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Scalar value exceeds float16 range.");
184 : return ACLNN_ERR_PARAM_INVALID;
185 : }
186 : return ACLNN_SUCCESS;
187 : case DataType::DT_BF16:
188 : if (val_1 < static_cast<double>(BFLOAT16_MIN_VALUE) || val_1 > static_cast<double>(BFLOAT16_MAX_VALUE)) {
189 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Scalar value exceeds bfloat16 range.");
190 : return ACLNN_ERR_PARAM_INVALID;
191 : }
192 : return ACLNN_SUCCESS;
193 : default:
194 : OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Unsupported tensor data type for scalar value check");
195 : return ACLNN_ERR_PARAM_INVALID;
196 : }
197 : }
198 :
199 : static aclnnStatus ExecForeachMaximumScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar,
200 : const aclTensorList* out, uint64_t* workspaceSize,
201 : aclOpExecutor** executor)
202 : {
203 : // 固定写法,创建OpExecutor
204 : auto uniqueExecutor = CREATE_EXECUTOR();
205 : CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
206 :
207 : // 固定写法,参数检查
208 : auto ret_2 = CheckParams(x, scalar, out);
209 : CHECK_RET(ret_2 == ACLNN_SUCCESS, ret_2);
210 :
211 : // 空Tensorlist处理
212 : if (x->Size() == 0) {
213 : *workspaceSize = 0;
214 : uniqueExecutor.ReleaseTo(executor);
215 : return ACLNN_SUCCESS;
216 : }
217 :
218 : // self如果非连续,需要转连续
219 : std::vector<const aclTensor*> tensorsVec;
220 : for (size_t i = 0; i < x->Size(); ++i) {
221 : auto secondContiguous = l0op::Contiguous((*x)[i], uniqueExecutor.get());
222 : CHECK_RET(secondContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
223 : tensorsVec.push_back(secondContiguous);
224 : }
225 : auto contiguousTensors = uniqueExecutor.get()->AllocTensorList(tensorsVec.data(), tensorsVec.size());
226 : CHECK_RET(contiguousTensors != nullptr, ACLNN_ERR_INNER_NULLPTR);
227 :
228 : // 校验scalar的数值是否满足约束
229 : ret_2 = CheckScalarValueValid(scalar, (*x)[0]->GetDataType());
230 : CHECK_RET(ret_2 == ACLNN_SUCCESS, ret_2);
231 :
232 : // sclar to tensor
233 : const aclTensor* otherTensor;
234 : if ((*x)[0]->GetDataType() == DataType::DT_BF16) {
235 : otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, DataType::DT_FLOAT);
236 : } else {
237 : otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, (*x)[0]->GetDataType());
238 : }
239 :
240 : // 调用l0算子ForeachMaximumScalarV2进行计算
241 : auto result = l0op::ForeachMaximumScalarV2(contiguousTensors, otherTensor, out, uniqueExecutor.get());
242 : CHECK_RET(result != nullptr, ACLNN_ERR_INNER_NULLPTR);
243 :
244 : // 固定写法,获取计算过程中需要使用的workspace大小
245 : *workspaceSize = uniqueExecutor->GetWorkspaceSize();
246 : uniqueExecutor.ReleaseTo(executor);
247 : return ACLNN_SUCCESS;
248 : }
249 :
250 : aclnnStatus aclnnForeachMaximumScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar,
251 : aclTensorList* out, uint64_t* workspaceSize,
252 : aclOpExecutor** executor)
253 : {
254 : L2_DFX_PHASE_1(aclnnForeachMaximumScalarV2, DFX_IN(x, scalar), DFX_OUT(out));
255 : return ExecForeachMaximumScalarV2GetWorkspaceSize(x, scalar, out, workspaceSize, executor);
256 : }
257 :
258 : aclnnStatus aclnnForeachMaximumScalarV2(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor,
259 : const aclrtStream stream)
260 : {
261 : L2_DFX_PHASE_2(aclnnForeachMaximumScalarV2);
262 : return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
263 : }
264 :
265 : #ifdef __cplusplus
266 : }
267 : #endif
|