LCOV - code coverage report
Current view: top level - ut/foreach/foreach_pow_scalar/op_host/op_api - aclnn_foreach_pow_scalar_v2.cpp Coverage Total Hit
Test: CHG Lines: 0.0 % 8 0
Test Date: 2026-08-29 15:24:08
Legend: Lines: hit not hit

            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_pow_scalar_v2.h"
      12              : #include "foreach_pow_scalar_v2.h"
      13              : #include "aclnn_kernels/contiguous.h"
      14              : #include "op_api/op_api_def_nn.h"
      15              : #include "op_api/aclnn_util.h"
      16              : #include "aclnn_kernels/common/op_error_check.h"
      17              : #include "opdev/platform.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              : static const std::initializer_list<DataType> ASCEND910BC_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_INT_SUPPORT_LIST = {DataType::DT_INT32, DataType::DT_INT64};
      37              : 
      38              : static const std::initializer_list<DataType> EMPTY_LIST = {};
      39              : 
      40              : static inline bool CheckNotNull(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
      41              : {
      42              :     OP_CHECK_NULL(self, return false);
      43              :     OP_CHECK_NULL(scalar, return false);
      44              :     OP_CHECK_NULL(out, return false);
      45              :     return true;
      46              : }
      47              : 
      48              : static inline bool CheckFormat(const aclTensorList* self, const aclTensorList* out)
      49              : {
      50              :     for (uint64_t i = 0; i < self->Size(); i++) {
      51              :         // self格式不能是私有格式
      52              :         if (IsPrivateFormat((*self)[i]->GetStorageFormat()) || IsPrivateFormat((*out)[i]->GetStorageFormat())) {
      53              :             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "Format only support ND, NCHW, NHWC, HWCN, NDHWC, NCDHW.");
      54              :             return false;
      55              :         }
      56              :     }
      57              :     return true;
      58              : }
      59              : 
      60              : static const std::initializer_list<DataType>& GetDtypeSupportList()
      61              : {
      62              :     auto curArchs = GetCurrentPlatformInfo().GetCurNpuArch();
      63              :     if (curArchs == NpuArch::DAV_2201 || Ops::NN::AclnnUtil::IsRegbase(curArchs)) {
      64              :         return ASCEND910BC_TENSOR_DTYPE_DTYPE_SUPPORT_LIST;
      65              :     } else {
      66              :         OP_LOGE(ACLNN_ERR_RUNTIME_ERROR, "support for %s is not implemented",
      67              :                 op::ToString(GetCurrentPlatformInfo().GetSocVersion()).GetString());
      68              :         return EMPTY_LIST;
      69              :     }
      70              : }
      71              : 
      72              : static inline bool CheckDtypePowScalar(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
      73              : {
      74              :     const auto& dtypeSupportList = GetDtypeSupportList();
      75              :     auto selfDtyte_1 = (*self)[0]->GetDataType();
      76              :     if (dtypeSupportList.size() == 0) {
      77              :         OP_LOGE(ACLNN_ERR_PARAM_INVALID, "support for %s is not implemented",
      78              :                 op::ToString(GetCurrentPlatformInfo().GetSocVersion()).GetString());
      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_DTYPE_NOT_SUPPORT((*self)[0], dtypeSupportList, return false);
      87              :     for (uint64_t i = 0; i < self->Size(); i++) {
      88              :         OP_CHECK_DTYPE_NOT_MATCH((*self)[i], selfDtyte_1, return false);
      89              :     }
      90              : 
      91              :     for (uint64_t j = 0; j < out->Size(); j++) {
      92              :         OP_CHECK_DTYPE_NOT_MATCH((*out)[j], selfDtyte_1, return false);
      93              :     }
      94              : 
      95              :     // check the releation of self and scalar
      96              :     if (selfDtyte_1 == DataType::DT_BF16 || selfDtyte_1 == DataType::DT_FLOAT) {
      97              :         OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT_SUPPORT_LIST, return false);
      98              :     } else if (selfDtyte_1 == DataType::DT_FLOAT16) {
      99              :         OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_FLOAT16_SUPPORT_LIST, return false);
     100              :     } else {
     101              :         OP_CHECK_DTYPE_NOT_SUPPORT(scalar, FOREACH_SCALAR_INT_SUPPORT_LIST, return false);
     102              :     }
     103              :     return true;
     104              : }
     105              : 
     106              : static inline bool CheckShape(const aclTensorList* self, const aclTensorList* out)
     107              : {
     108              :     // tensor 维度检查
     109              :     for (uint64_t m = 0; m < self->Size(); m++) {
     110              :         OP_CHECK_MAX_DIM((*self)[m], MAX_SUPPORT_DIMS_NUMS, return false);
     111              :     }
     112              : 
     113              :     // self和out的shape必须一致
     114              :     for (uint64_t j = 0; j < self->Size(); j++) {
     115              :         OP_CHECK_SHAPE_NOT_EQUAL((*self)[j], (*out)[j], return false);
     116              :     }
     117              :     return true;
     118              : }
     119              : 
     120              : static inline aclnnStatus CheckParams(const aclTensorList* self, const aclScalar* scalar, const aclTensorList* out)
     121              : {
     122              :     // 1. 检查参数是否为空指针
     123              :     CHECK_RET(CheckNotNull(self, scalar, out), ACLNN_ERR_PARAM_NULLPTR);
     124              : 
     125              :     // Check every entry in tensor lists is not null, to avoid null pointer
     126              :     // dereference in CheckDtypePowScalar/CheckShape/CheckFormat.
     127            0 :     for (uint64_t i = 0; i < self->Size(); i++) {
     128            0 :         if ((*self)[i] == nullptr) {
     129            0 :             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "self[%lu] is null.", i);
     130            0 :             return ACLNN_ERR_PARAM_INVALID;
     131              :         }
     132              :     }
     133            0 :     for (uint64_t i = 0; i < out->Size(); i++) {
     134            0 :         if ((*out)[i] == nullptr) {
     135            0 :             OP_LOGE(ACLNN_ERR_PARAM_INVALID, "out[%lu] is null.", i);
     136            0 :             return ACLNN_ERR_PARAM_INVALID;
     137              :         }
     138              :     }
     139              : 
     140              :     // 2. 检查输入的数据类型是否在API支持的数据类型范围之内,需要根据api定义校验
     141              :     CHECK_RET(CheckDtypePowScalar(self, scalar, out), ACLNN_ERR_PARAM_INVALID);
     142              :     // 3. 检查shape是否满足约束
     143              :     CHECK_RET(CheckShape(self, out), ACLNN_ERR_PARAM_INVALID);
     144              :     // 4. 检查Format是否满足约束
     145              :     CHECK_RET(CheckFormat(self, out), ACLNN_ERR_PARAM_INVALID);
     146              :     return ACLNN_SUCCESS;
     147              : }
     148              : 
     149              : static aclnnStatus ExecForeachPowScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar,
     150              :                                                           const aclTensorList* out, uint64_t* workspaceSize,
     151              :                                                           aclOpExecutor** executor)
     152              : {
     153              :     // 固定写法,创建OpExecutor
     154              :     auto uniqueExecutor = CREATE_EXECUTOR();
     155              :     CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);
     156              : 
     157              :     // 固定写法,参数检查
     158              :     auto ret = CheckParams(x, scalar, out);
     159              :     CHECK_RET(ret == ACLNN_SUCCESS, ret);
     160              : 
     161              :     // 空Tensorlist处理
     162              :     if (x->Size() == 0) {
     163              :         *workspaceSize = 0;
     164              :         uniqueExecutor.ReleaseTo(executor);
     165              :         return ACLNN_SUCCESS;
     166              :     }
     167              : 
     168              :     // self如果非连续,需要转连续
     169              :     std::vector<const aclTensor*> tensorsVec;
     170              :     for (size_t a = 0; a < x->Size(); ++a) {
     171              :         auto secondContiguous = l0op::Contiguous((*x)[a], uniqueExecutor.get());
     172              :         CHECK_RET(secondContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);
     173              :         tensorsVec.push_back(secondContiguous);
     174              :     }
     175              :     auto contiguousTensors = uniqueExecutor.get()->AllocTensorList(tensorsVec.data(), tensorsVec.size());
     176              :     CHECK_RET(contiguousTensors != nullptr, ACLNN_ERR_INNER_NULLPTR);
     177              : 
     178              :     // sclar to tensor
     179              :     const aclTensor* otherTensor;
     180              :     if ((*x)[0]->GetDataType() == DataType::DT_BF16) {
     181              :         otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, DataType::DT_FLOAT);
     182              :     } else {
     183              :         otherTensor = uniqueExecutor.get()->ConvertToTensor(scalar, (*x)[0]->GetDataType());
     184              :     }
     185              : 
     186              :     // 调用l0算子ForeachPowScalarV2进行计算
     187              :     auto result = l0op::ForeachPowScalarV2(contiguousTensors, otherTensor, out, uniqueExecutor.get());
     188              :     CHECK_RET(result != nullptr, ACLNN_ERR_INNER_NULLPTR);
     189              : 
     190              :     // 固定写法,获取计算过程中需要使用的workspace大小
     191              :     *workspaceSize = uniqueExecutor->GetWorkspaceSize();
     192              :     uniqueExecutor.ReleaseTo(executor);
     193              :     return ACLNN_SUCCESS;
     194              : }
     195              : 
     196              : aclnnStatus aclnnForeachPowScalarV2GetWorkspaceSize(const aclTensorList* x, const aclScalar* scalar, aclTensorList* out,
     197              :                                                     uint64_t* workspaceSize, aclOpExecutor** executor)
     198              : {
     199              :     L2_DFX_PHASE_1(aclnnForeachPowScalarV2, DFX_IN(x, scalar), DFX_OUT(out));
     200              :     return ExecForeachPowScalarV2GetWorkspaceSize(x, scalar, out, workspaceSize, executor);
     201              : }
     202              : 
     203              : aclnnStatus aclnnForeachPowScalarV2(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor,
     204              :                                     const aclrtStream stream)
     205              : {
     206              :     L2_DFX_PHASE_2(aclnnForeachPowScalarV2);
     207              :     return CommonOpExecutorRun(workspace, workspaceSize, executor, stream);
     208              : }
     209              : 
     210              : #ifdef __cplusplus
     211              : }
     212              : #endif
        

Generated by: LCOV version 2.0-1