Merged
fix(heaviside): 补全tiling入参/dtype校验与溢出防护,整改静态检查问题 #13876
tan_xinAdd on 14 days ago
fix(heaviside): 补全tiling入参/dtype校验与溢出防护,整改静态检查问题 #13876
Merged
tan_xinAdd on 14 days ago
Total: 27 files changed+144-74
@@ -25,9 +25,9 @@ English | [简体中文](./README.md)
25 $$25 $$
26 \text{Heaviside}(\text{input}, \text{values}) =26 \text{Heaviside}(\text{input}, \text{values}) =
27 \begin{cases}27 \begin{cases}
28- 0, & \text{如果input} < 0 \\28+ 0, & \text{if input} < 0 \\
29- \text{values}, & \text{如果input} = 0 \\29+ \text{values}, & \text{if input} = 0 \\
30- 1, & \text{如果input} > 030+ 1, & \text{if input} > 0
31 \end{cases}31 \end{cases}
32 $$32 $$
33 33 
@@ -152,7 +152,7 @@ aclnnStatus aclnnHeaviside(
152 </colgroup>152 </colgroup>
153 <thead>153 <thead>
154 <tr>154 <tr>
155- <th>Error Code</th>155+ <th>Return Code</th>
156 <th>Error Code</th>156 <th>Error Code</th>
157 <th>Description</th>157 <th>Description</th>
158 </tr>158 </tr>
@@ -164,10 +164,13 @@ aclnnStatus aclnnHeaviside(
164 <td>The input parameter (input, values) or output parameter out is a null pointer.</td>164 <td>The input parameter (input, values) or output parameter out is a null pointer.</td>
165 </tr>165 </tr>
166 <tr>166 <tr>
167- <td rowspan="3">ACLNN_ERR_PARAM_INVALID</td>167+ <td rowspan="4">ACLNN_ERR_PARAM_INVALID</td>
168- <td rowspan="3">161002</td>168+ <td rowspan="4">161002</td>
169 <td>The data type of the input/output parameter is not in the supported range.</td>169 <td>The data type of the input/output parameter is not in the supported range.</td>
170 </tr>170 </tr>
171+ <tr>
172+ <td>The data types of input parameters input and values are inconsistent.</td>
173+ </tr>
171 <tr>174 <tr>
172 <td>The shapes of input parameters input and values cannot be broadcast.</td>175 <td>The shapes of input parameters input and values cannot be broadcast.</td>
173 </tr>176 </tr>
@@ -39,9 +39,10 @@ int Init(int32_t deviceId, aclrtStream* stream)
39 auto ret = aclInit(nullptr);39 auto ret = aclInit(nullptr);
40 CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);40 CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
41 ret = aclrtSetDevice(deviceId);41 ret = aclrtSetDevice(deviceId);
42- CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);42+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); aclFinalize(); return ret);
43 ret = aclrtCreateStream(stream);43 ret = aclrtCreateStream(stream);
44- CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);44+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); aclrtResetDevice(deviceId);
45+ aclFinalize(); return ret);
45 return 0;46 return 0;
46}47}
47 48 
@@ -56,7 +57,8 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>&
56 57 
57 // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上58 // 调用aclrtMemcpy将host侧数据拷贝到device侧内存上
58 ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);59 ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
59- CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);60+ CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); aclrtFree(*deviceAddr);
61+ *deviceAddr = nullptr; return ret);
60 62 
61 // 计算连续tensor的strides63 // 计算连续tensor的strides
62 std::vector<int64_t> strides(shape.size(), 1);64 std::vector<int64_t> strides(shape.size(), 1);
@@ -95,13 +97,15 @@ int main()
95 97 
96 // 创建self aclTensor98 // 创建self aclTensor
97 ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);99 ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
98- CHECK_RET(ret == ACL_SUCCESS, return ret);100+ CHECK_RET(ret == ACL_SUCCESS, aclrtResetDevice(deviceId); aclFinalize(); return ret);
99 // 创建other aclTensor101 // 创建other aclTensor
100 ret = CreateAclTensor(valuesHostData, valuesShape, &valuesDeviceAddr, aclDataType::ACL_FLOAT, &values);102 ret = CreateAclTensor(valuesHostData, valuesShape, &valuesDeviceAddr, aclDataType::ACL_FLOAT, &values);
101- CHECK_RET(ret == ACL_SUCCESS, return ret);103+ CHECK_RET(ret == ACL_SUCCESS, aclDestroyTensor(self); aclrtFree(selfDeviceAddr); aclrtResetDevice(deviceId);
104+ aclFinalize(); return ret);
102 // 创建out aclTensor105 // 创建out aclTensor
103 ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);106 ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
104- CHECK_RET(ret == ACL_SUCCESS, return ret);107+ CHECK_RET(ret == ACL_SUCCESS, aclDestroyTensor(self); aclDestroyTensor(values); aclrtFree(selfDeviceAddr);
108+ aclrtFree(valuesDeviceAddr); aclrtResetDevice(deviceId); aclFinalize(); return ret);
105 109 
106 // 3.调用CANN算子库API,需要修改为具体的HostApi110 // 3.调用CANN算子库API,需要修改为具体的HostApi
107 uint64_t workspaceSize = 0;111 uint64_t workspaceSize = 0;
@@ -147,4 +151,4 @@ int main()
147 aclrtResetDevice(deviceId);151 aclrtResetDevice(deviceId);
148 aclFinalize();152 aclFinalize();
149 return 0;153 return 0;
150-}154+}
@@ -38,7 +38,7 @@ extern "C" {
38 * @param [in] values: npu38 * @param [in] values: npu
39 * device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16,shape需要与input满足broadcast关系。39 * device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16,shape需要与input满足broadcast关系。
40 * 支持非连续的Tensor,数据格式支持ND,且数据格式需要与input一致。40 * 支持非连续的Tensor,数据格式支持ND,且数据格式需要与input一致。
41- * @param [in] out: npu41+ * @param [out] out: npu
42 * device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16,shape需要是input与values42 * device侧的aclTensor,数据类型支持FLOAT,FLOAT16,BFLOAT16,shape需要是input与values
43 * broadcast之后的shape,数据格式支持ND,且数据格式需要与input一致。支持非连续Tensor。43 * broadcast之后的shape,数据格式支持ND,且数据格式需要与input一致。支持非连续Tensor。
44 * @param [out] workspaceSize: 返回用户需要在npu device侧申请的workspace大小。44 * @param [out] workspaceSize: 返回用户需要在npu device侧申请的workspace大小。
@@ -51,7 +51,7 @@ ACLNN_API aclnnStatus aclnnHeavisideGetWorkspaceSize(const aclTensor* input, con
51/**51/**
52 * @brief aclnnHeaviside的第二段接口,用于执行计算。52 * @brief aclnnHeaviside的第二段接口,用于执行计算。
53 * @param [in] workspace: 在npu device侧申请的workspace内存起址。53 * @param [in] workspace: 在npu device侧申请的workspace内存起址。
54- * @param [in] workspaceSize: 在npu device侧申请的workspace大小,由第一段接口aclnnForeachAbsGetWorkspaceSize获取。54+ * @param [in] workspaceSize: 在npu device侧申请的workspace大小,由第一段接口aclnnHeavisideGetWorkspaceSize获取。
55 * @param [in] stream: acl stream流。55 * @param [in] stream: acl stream流。
56 * @param [in] executor: op执行器,包含了算子计算流程。56 * @param [in] executor: op执行器,包含了算子计算流程。
57 * @return aclnnStatus: 返回状态码。57 * @return aclnnStatus: 返回状态码。
@@ -1,11 +1,11 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
9 9 
10message(STATUS "=== Debug: start ops.activation.heaviside.graph_plugin.CMakeLists.txt ")10message(STATUS "=== Debug: start ops.activation.heaviside.graph_plugin.CMakeLists.txt ")
11-add_graph_plugin_sources()11+add_graph_plugin_sources()
@@ -38,4 +38,4 @@ REG_OP(Heaviside)
38 38 
39} // namespace ge39} // namespace ge
40 40 
41-#endif41+#endif
@@ -122,4 +122,4 @@
122 ]122 ]
123 }123 }
124 ]124 ]
125-}125+}
@@ -122,4 +122,4 @@
122 ]122 ]
123 }123 }
124 ]124 ]
125-}125+}
@@ -122,4 +122,4 @@
122 ]122 ]
123 }123 }
124 ]124 ]
125-}125+}
@@ -82,4 +82,4 @@
82 ]82 ]
83 }83 }
84 ]84 ]
85-}85+}
@@ -82,4 +82,4 @@
82 ]82 ]
83 }83 }
84 ]84 ]
85-}85+}
@@ -66,9 +66,17 @@ static graphStatus InferDataType4Heaviside(gert::InferDataTypeContext* context)
66{66{
67 OP_LOGD(context, "Begin to do InferDataType4Heaviside");67 OP_LOGD(context, "Begin to do InferDataType4Heaviside");
68 68 
69- auto input_dtype = context->GetInputDataType(IDX_0);69+ auto inputDtype = context->GetInputDataType(IDX_0);
70+ auto valuesDtype = context->GetInputDataType(1);
70 71 
71- context->SetOutputDataType(IDX_0, input_dtype);72+ OP_CHECK_IF(
73+ inputDtype == ge::DT_UNDEFINED || valuesDtype == ge::DT_UNDEFINED || inputDtype != valuesDtype,
74+ OP_LOGE_WITH_INVALID_INPUT(
75+ ((context) == nullptr || (context)->GetNodeName() == nullptr ? "heaviside" : (context)->GetNodeName()),
76+ "values"),
77+ return ge::GRAPH_FAILED);
78+ 
79+ context->SetOutputDataType(IDX_0, inputDtype);
72 80 
73 OP_LOGD(context, "End to do InferDataType4Heaviside");81 OP_LOGD(context, "End to do InferDataType4Heaviside");
74 82 
@@ -13,6 +13,7 @@
13 * \brief heaviside_tiling source file13 * \brief heaviside_tiling source file
14 */14 */
15#include "heaviside_tiling.h"15#include "heaviside_tiling.h"
16+#include <limits>
16#include <vector>17#include <vector>
17#include "op_common/log/log.h"18#include "op_common/log/log.h"
18#include "register/tilingdata_base.h"19#include "register/tilingdata_base.h"
@@ -43,11 +44,15 @@ private:
43 44 
44 inline int64_t CeilA2B(const int64_t a, const int64_t b) const45 inline int64_t CeilA2B(const int64_t a, const int64_t b) const
45 {46 {
46- if (b != 0) {47+ if (b == 0) {
47- return (a + b - 1) / b;
48- } else {
49 return a;48 return a;
50 }49 }
50+ if (a <= 0) {
51+ return 0;
52+ }
53+ // (a + b - 1) / b can overflow signed int64 when a is near INT64_MAX;
54+ // (a - 1) / b + 1 is equivalent for positive a and never overflows.
55+ return (a - 1) / b + 1;
51 }56 }
52 57 
53 int32_t GetNeedCoreNum(const int32_t coreNumPlatform) const58 int32_t GetNeedCoreNum(const int32_t coreNumPlatform) const
@@ -97,21 +102,44 @@ ge::graphStatus HeavisideTiling::RunBigKernelTiling()
97 return ge::GRAPH_FAILED;102 return ge::GRAPH_FAILED;
98 }103 }
99 104 
100- auto temp = tilingContext->GetInputDesc(0);105+ auto inputDesc = tilingContext->GetInputDesc(0);
101- if (temp == nullptr) {106+ if (inputDesc == nullptr) {
107+ return ge::GRAPH_FAILED;
108+ }
109+ dataType = inputDesc->GetDataType();
110+ 
111+ auto valuesDesc = tilingContext->GetInputDesc(1);
112+ if (valuesDesc == nullptr) {
113+ return ge::GRAPH_FAILED;
114+ }
115+ if (valuesDesc->GetDataType() != dataType) {
116+ OP_LOGE(tilingContext->GetNodeName(),
117+ "values dtype %d is inconsistent with input dtype %d, they must be the same.",
118+ static_cast<int32_t>(valuesDesc->GetDataType()), static_cast<int32_t>(dataType));
102 return ge::GRAPH_FAILED;119 return ge::GRAPH_FAILED;
103 }120 }
104- dataType = tilingContext->GetInputDesc(0)->GetDataType();
105 121 
106 tilingContext->SetTilingKey(TILING_KEY_DEFAULT);122 tilingContext->SetTilingKey(TILING_KEY_DEFAULT);
107 123 
108 // 获取input和values的shape124 // 获取input和values的shape
109 auto srcShape0 = tilingContext->GetInputShape(0);125 auto srcShape0 = tilingContext->GetInputShape(0);
110 auto srcShape1 = tilingContext->GetInputShape(1);126 auto srcShape1 = tilingContext->GetInputShape(1);
127+ if (srcShape0 == nullptr || srcShape1 == nullptr) {
128+ OP_LOGE(tilingContext->GetNodeName(),
129+ "Tiling framework object input shape is unavailable; check the operator registration and "
130+ "context construction.");
131+ return ge::GRAPH_FAILED;
132+ }
111 inputShape = srcShape0->GetOriginShape();133 inputShape = srcShape0->GetOriginShape();
112 inputShapeSize = inputShape.GetShapeSize();134 inputShapeSize = inputShape.GetShapeSize();
113 valuesShape = srcShape1->GetOriginShape();135 valuesShape = srcShape1->GetOriginShape();
114 valuesShapeSize = valuesShape.GetShapeSize();136 valuesShapeSize = valuesShape.GetShapeSize();
137+ if (inputShapeSize == std::numeric_limits<int64_t>::min() ||
138+ valuesShapeSize == std::numeric_limits<int64_t>::min()) {
139+ OP_LOGE(tilingContext->GetNodeName(),
140+ "input/values shape size overflow (GetShapeSize returned the invalid sentinel).");
141+ return ge::GRAPH_FAILED;
142+ }
115 if (inputShape.GetDimNum() > static_cast<size_t>(MAX_INPUT_DIM_NUM)) {143 if (inputShape.GetDimNum() > static_cast<size_t>(MAX_INPUT_DIM_NUM)) {
116 OP_LOGE(tilingContext->GetNodeName(), "input dim num %d exceeds the supported maximum %d.",144 OP_LOGE(tilingContext->GetNodeName(), "input dim num %d exceeds the supported maximum %d.",
117 static_cast<int32_t>(inputShape.GetDimNum()), MAX_INPUT_DIM_NUM);145 static_cast<int32_t>(inputShape.GetDimNum()), MAX_INPUT_DIM_NUM);
@@ -121,19 +149,43 @@ ge::graphStatus HeavisideTiling::RunBigKernelTiling()
121 return ge::GRAPH_FAILED;149 return ge::GRAPH_FAILED;
122 }150 }
123 151 
124- auto platformInfo = platform_ascendc::PlatformAscendC(tilingContext->GetPlatformInfo());152+ auto platformInfoPtr = tilingContext->GetPlatformInfo();
125- uint32_t needCoreNum = GetNeedCoreNum(platformInfo.GetCoreNumAiv());153+ if (platformInfoPtr == nullptr) {
154+ OP_LOGE(tilingContext->GetNodeName(),
155+ "Tiling framework object platformInfo is unavailable; check the operator registration and "
156+ "context construction.");
157+ return ge::GRAPH_FAILED;
158+ }
159+ auto platformInfo = platform_ascendc::PlatformAscendC(platformInfoPtr);
160+ uint32_t coreNumPlatform = platformInfo.GetCoreNumAiv();
161+ if (coreNumPlatform == 0) {
162+ OP_LOGE(tilingContext->GetNodeName(), "platform core num is 0, GetCoreNumAiv failed.");
163+ return ge::GRAPH_FAILED;
164+ }
165+ uint32_t needCoreNum = GetNeedCoreNum(static_cast<int32_t>(coreNumPlatform));
126 166 
127 size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);167 size_t* currentWorkspace = tilingContext->GetWorkspaceSizes(1);
168+ if (currentWorkspace == nullptr) {
169+ OP_LOGE(tilingContext->GetNodeName(),
170+ "Tiling framework object workspace is unavailable; check the operator registration and "
171+ "context construction.");
172+ return ge::GRAPH_FAILED;
173+ }
128 currentWorkspace[0] = 0;174 currentWorkspace[0] = 0;
129 175 
130 tilingData.set_elementNum(inputShapeSize);176 tilingData.set_elementNum(inputShapeSize);
131 tilingData.set_valuesType(valuesType);177 tilingData.set_valuesType(valuesType);
132 tilingData.set_needCoreNum(needCoreNum);178 tilingData.set_needCoreNum(needCoreNum);
133 179 
134- tilingData.SaveToBuffer(tilingContext->GetRawTilingData()->GetData(),180+ auto rawTilingData = tilingContext->GetRawTilingData();
135- tilingContext->GetRawTilingData()->GetCapacity());181+ if (rawTilingData == nullptr) {
136- tilingContext->GetRawTilingData()->SetDataSize(tilingData.GetDataSize());182+ OP_LOGE(tilingContext->GetNodeName(),
183+ "Tiling framework object rawTilingData is unavailable; check the operator registration and "
184+ "context construction.");
185+ return ge::GRAPH_FAILED;
186+ }
187+ tilingData.SaveToBuffer(rawTilingData->GetData(), rawTilingData->GetCapacity());
188+ rawTilingData->SetDataSize(tilingData.GetDataSize());
137 189 
138 tilingContext->SetBlockDim(needCoreNum);190 tilingContext->SetBlockDim(needCoreNum);
139 return ge::GRAPH_SUCCESS;191 return ge::GRAPH_SUCCESS;
@@ -31,4 +31,4 @@ END_TILING_DATA_DEF;
31REGISTER_TILING_DATA_CLASS(Heaviside, HeavisideTilingData)31REGISTER_TILING_DATA_CLASS(Heaviside, HeavisideTilingData)
32} // namespace optiling32} // namespace optiling
33 33 
34-#endif34+#endif
@@ -35,4 +35,4 @@ extern "C" __global__ __aicore__ void heaviside(GM_ADDR input, GM_ADDR values, G
35 }35 }
36#else36#else
37#endif37#endif
38-}38+}
@@ -269,4 +269,4 @@ __aicore__ inline void HeavisideND<T>::CastAndCopyOut(int64_t outputOffset, int6
269 SetFlag<HardEvent::MTE3_MTE2>(eventId);269 SetFlag<HardEvent::MTE3_MTE2>(eventId);
270}270}
271} // namespace Heaviside271} // namespace Heaviside
272-#endif272+#endif
@@ -1,8 +1,8 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
@@ -1,8 +1,8 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
@@ -14,4 +14,4 @@ foreach(SUB_DIR ${CURRENT_SOURCE_DIRS})
14 if(EXISTS "${SUB_DIR}/CMakeLists.txt")14 if(EXISTS "${SUB_DIR}/CMakeLists.txt")
15 add_subdirectory(${SUB_DIR})15 add_subdirectory(${SUB_DIR})
16 endif()16 endif()
17-endforeach()17+endforeach()
@@ -1,8 +1,8 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
@@ -16,4 +16,4 @@ foreach(SUB_DIR ${CURRENT_DIRS})
16 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt")16 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt")
17 add_subdirectory(${SUB_DIR})17 add_subdirectory(${SUB_DIR})
18 endif()18 endif()
19-endforeach()19+endforeach()
@@ -1,8 +1,8 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
@@ -28,4 +28,4 @@ TEST_F(Heaviside, Heaviside_infershape_case_0)
28 28 
29 EXPECT_EQ(InferShapeTest(op), ge::GRAPH_SUCCESS);29 EXPECT_EQ(InferShapeTest(op), ge::GRAPH_SUCCESS);
30 EXPECT_EQ(InferDataTypeTest(op), ge::GRAPH_SUCCESS);30 EXPECT_EQ(InferDataTypeTest(op), ge::GRAPH_SUCCESS);
31-}31+}
@@ -1,8 +1,8 @@
1# Copyright (c) 2025 Huawei Technologies Co., Ltd.1# Copyright (c) 2025 Huawei Technologies Co., Ltd.
2-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 2+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
3# CANN Open Software License Agreement Version 2.0 (the "License").3# CANN Open Software License Agreement Version 2.0 (the "License").
4# Please refer to the License for details. You may not use this file except in compliance with the License.4# Please refer to the License for details. You may not use this file except in compliance with the License.
5-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 5+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.6# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
7# See LICENSE in the root of the software repository for the full text of the License.7# See LICENSE in the root of the software repository for the full text of the License.
8#/8#/
@@ -2,10 +2,10 @@
2# -*- coding: utf-8 -*-2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 5+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6# CANN Open Software License Agreement Version 2.0 (the "License").6# CANN Open Software License Agreement Version 2.0 (the "License").
7# Please refer to the License for details. You may not use this file except in compliance with the License.7# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 8+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------11# ----------------------------------------------------------------------------
@@ -13,27 +13,26 @@ import sys
13import numpy as np13import numpy as np
14import glob14import glob
15import os15import os
16-import math
17-import torch
18 16 
19curr_dir = os.path.dirname(os.path.realpath(__file__))17curr_dir = os.path.dirname(os.path.realpath(__file__))
20 18 
19+ 
21def compare_data(golden_file_lists, output_file_lists, d_type):20def compare_data(golden_file_lists, output_file_lists, d_type):
22 np_dtype = np.float3221 np_dtype = np.float32
23 if d_type == "float16":22 if d_type == "float16":
24 np_dtype = np.float1623 np_dtype = np.float16
25- precision = 1/10024+ precision = 1 / 100
26 elif d_type == "float32":25 elif d_type == "float32":
27- precision = 1/1000026+ precision = 1 / 10000
28 else:27 else:
29- precision = 1/10028+ precision = 1 / 100
30 29 
31 data_same = True30 data_same = True
32 for gold, out in zip(golden_file_lists, output_file_lists):31 for gold, out in zip(golden_file_lists, output_file_lists):
33 tmp_out = np.fromfile(out, np_dtype)32 tmp_out = np.fromfile(out, np_dtype)
34 tmp_gold = np.fromfile(gold, np_dtype)33 tmp_gold = np.fromfile(gold, np_dtype)
35 diff_res = np.isclose(tmp_out, tmp_gold, precision, 0, True)34 diff_res = np.isclose(tmp_out, tmp_gold, precision, 0, True)
36- diff_idx = np.where(diff_res != True)[0]35+ diff_idx = np.where(~diff_res)[0]
37 if len(diff_idx) == 0:36 if len(diff_idx) == 0:
38 print("PASSED!")37 print("PASSED!")
39 else:38 else:
@@ -44,14 +43,17 @@ def compare_data(golden_file_lists, output_file_lists, d_type):
44 # data_same = False43 # data_same = False
45 return data_same44 return data_same
46 45 
46+ 
47def get_file_lists(dtype):47def get_file_lists(dtype):
48 golden_file_lists = sorted(glob.glob(curr_dir + "/*golden*.bin"))48 golden_file_lists = sorted(glob.glob(curr_dir + "/*golden*.bin"))
49 output_file_lists = sorted(glob.glob(curr_dir + "/*output*.bin"))49 output_file_lists = sorted(glob.glob(curr_dir + "/*output*.bin"))
50 return golden_file_lists, output_file_lists50 return golden_file_lists, output_file_lists
51 51 
52+ 
52def process(d_type):53def process(d_type):
53 golden_file_lists, output_file_lists = get_file_lists(d_type)54 golden_file_lists, output_file_lists = get_file_lists(d_type)
54- result = compare_data(golden_file_lists, output_file_lists, d_type)55+ compare_data(golden_file_lists, output_file_lists, d_type)
55 56 
56-if __name__ == '__main__':57+ 
57- process(sys.argv[1])58+if __name__ == "__main__":
59+ process(sys.argv[1])
@@ -2,31 +2,31 @@
2# -*- coding: utf-8 -*-2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5-# This program is free software, you can redistribute it and/or modify it under the terms and conditions of 5+# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
6# CANN Open Software License Agreement Version 2.0 (the "License").6# CANN Open Software License Agreement Version 2.0 (the "License").
7# Please refer to the License for details. You may not use this file except in compliance with the License.7# Please refer to the License for details. You may not use this file except in compliance with the License.
8-# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 8+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.9# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
10# See LICENSE in the root of the software repository for the full text of the License.10# See LICENSE in the root of the software repository for the full text of the License.
11# ----------------------------------------------------------------------------11# ----------------------------------------------------------------------------
12import sys12import sys
13import os13import os
14import numpy as np14import numpy as np
15-import re
16import tensorflow as tf15import tensorflow as tf
17import torch16import torch
18 17 
19 18 
20def parse_str_to_shape_list(shape_str):19def parse_str_to_shape_list(shape_str):
21- shape_str = shape_str.strip('(').strip(')')20+ shape_str = shape_str.strip("(").strip(")")
22 shape_list = [int(x) for x in shape_str.split(",")]21 shape_list = [int(x) for x in shape_str.split(",")]
23 return np.array(shape_list), shape_list22 return np.array(shape_list), shape_list
24 23 
24+ 
25def gen_data_and_golden(input_shape_str, output_size_str, d_type="float32"):25def gen_data_and_golden(input_shape_str, output_size_str, d_type="float32"):
26 d_type_dict = {26 d_type_dict = {
27 "float32": np.float32,27 "float32": np.float32,
28 "float16": np.float16,28 "float16": np.float16,
29- "bfloat16_t": tf.bfloat16.as_numpy_dtype29+ "bfloat16_t": tf.bfloat16.as_numpy_dtype,
30 }30 }
31 np_type = d_type_dict[d_type]31 np_type = d_type_dict[d_type]
32 input_shape, _ = parse_str_to_shape_list(input_shape_str)32 input_shape, _ = parse_str_to_shape_list(input_shape_str)
@@ -45,10 +45,11 @@ def gen_data_and_golden(input_shape_str, output_size_str, d_type="float32"):
45 tmp_values.astype(np_type).tofile(f"{d_type}_values_heaviside.bin")45 tmp_values.astype(np_type).tofile(f"{d_type}_values_heaviside.bin")
46 tmp_golden.astype(np_type).tofile(f"{d_type}_golden_heaviside.bin")46 tmp_golden.astype(np_type).tofile(f"{d_type}_golden_heaviside.bin")
47 47 
48+ 
48if __name__ == "__main__":49if __name__ == "__main__":
49 if len(sys.argv) != 4:50 if len(sys.argv) != 4:
50 print("Param num must be 4, actually is ", len(sys.argv))51 print("Param num must be 4, actually is ", len(sys.argv))
51 exit(1)52 exit(1)
52 # 清理bin文件53 # 清理bin文件
53 os.system("rm -rf *.bin")54 os.system("rm -rf *.bin")
54- gen_data_and_golden(sys.argv[1], sys.argv[2], sys.argv[3])55+ gen_data_and_golden(sys.argv[1], sys.argv[2], sys.argv[3])
@@ -23,7 +23,7 @@
23#define __CCE_UT_TEST__23#define __CCE_UT_TEST__
24 24 
25struct HeavisideTilingData {25struct HeavisideTilingData {
26- int32_t elementNum = 8;26+ int64_t elementNum = 8;
27 bool valuesType = false;27 bool valuesType = false;
28 uint32_t needCoreNum = 1;28 uint32_t needCoreNum = 1;
29};29};
@@ -36,4 +36,4 @@ inline void IHeavisideTilingData(uint8_t* tiling, HeavisideTilingData* const_dat
36#define GET_TILING_DATA(tilingData, tilingPointer) \36#define GET_TILING_DATA(tilingData, tilingPointer) \
37 HeavisideTilingData tilingData; \37 HeavisideTilingData tilingData; \
38 IHeavisideTilingData(tilingPointer, &tilingData)38 IHeavisideTilingData(tilingPointer, &tilingData)
39-#endif39+#endif
@@ -181,4 +181,4 @@ TEST_F(heaviside_test, test_heaviside_bfloat16_2)
181 AscendC::GmFree((void*)workspace);181 AscendC::GmFree((void*)workspace);
182 AscendC::GmFree((void*)tiling);182 AscendC::GmFree((void*)tiling);
183 kernel_ut::RunCompareData("./heaviside_data", {"'bfloat16_t'"});183 kernel_ut::RunCompareData("./heaviside_data", {"'bfloat16_t'"});
184-}184+}