Merged
fix(foreach): 加固 foreach_erfc arch35 校验与 aclnn 健壮性并修正资料口径 #13805
duxinleiAdd on 16 days ago
fix(foreach): 加固 foreach_erfc arch35 校验与 aclnn 健壮性并修正资料口径 #13805
Merged
Merged fromduxinlei/ops-nnfix/foreach-erfc-hardeningintocann/ops-nnmaster
Total: 9 files changed+449-97
| @@ -48,14 +48,14 @@ | |||
| 48 | <tr> | 48 | <tr> |
| 49 | <td>x</td> | 49 | <td>x</td> |
| 50 | <td>输入</td> | 50 | <td>输入</td> |
| 51 | - <td>表示进行误差函数运算的输入张量列表,对应公式中的`x`。该参数中所有Tensor的数据类型保持一致。</td> | 51 | + <td>表示进行互补误差函数运算的输入张量列表,对应公式中的`x`。该参数中所有Tensor的数据类型保持一致。</td> |
| 52 | <td>FLOAT32、FLOAT16、BFLOAT16</td> | 52 | <td>FLOAT32、FLOAT16、BFLOAT16</td> |
| 53 | <td>ND</td> | 53 | <td>ND</td> |
| 54 | </tr> | 54 | </tr> |
| 55 | <tr> | 55 | <tr> |
| 56 | <td>y</td> | 56 | <td>y</td> |
| 57 | <td>输出</td> | 57 | <td>输出</td> |
| 58 | - <td>表示进行误差函数运算的输出张量列表,对应公式中的`y`。该参数中所有Tensor的数据类型保持一致。数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape size大于等于入参`x`的shape size。</td> | 58 | + <td>表示进行互补误差函数运算的输出张量列表,对应公式中的`y`。该参数中所有Tensor的数据类型保持一致。数据类型和数据格式与入参`x`的数据类型和数据格式一致,shape size大于等于入参`x`的shape size。</td> |
| 59 | <td>FLOAT32、FLOAT16、BFLOAT16</td> | 59 | <td>FLOAT32、FLOAT16、BFLOAT16</td> |
| 60 | <td>ND</td> | 60 | <td>ND</td> |
| 61 | </tr> | 61 | </tr> |
| @@ -65,7 +65,11 @@ | |||
| 65 | 65 | ||
| 66 | ## 约束说明 | 66 | ## 约束说明 |
| 67 | 67 | ||
| 68 | -- 输出不支持非连续Tensor。 | 68 | +- x与out的Tensor数量一致,且out中对应位置Tensor的shape size大于等于x中对应位置Tensor的shape size。 |
| 69 | +- 单个Tensor的维度不超过8。 | ||
| 70 | +- x/y中单个Tensor列表的Tensor数量不能超过256。 | ||
| 71 | +- x与out数量为0(空列表)时直接返回成功,不执行计算。 | ||
| 72 | +- 支持非连续Tensor:非连续输入/输出自动转连续计算,结果按原布局写回。 | ||
| 69 | 73 | ||
| 70 | ## 调用说明 | 74 | ## 调用说明 |
| 71 | 75 | ||
| @@ -50,14 +50,14 @@ English | [简体中文](./README.md) | |||
| 50 | <tr> | 50 | <tr> |
| 51 | <td>x</td> | 51 | <td>x</td> |
| 52 | <td>Input</td> | 52 | <td>Input</td> |
| 53 | - <td>Denotes the input tensor list for the error function operation, corresponding to `x` in the formula. All tensors in this parameter have the same data type.</td> | 53 | + <td>Denotes the input tensor list for the complementary error function operation, corresponding to `x` in the formula. All tensors in this parameter have the same data type.</td> |
| 54 | <td>FLOAT32, FLOAT16, BFLOAT16</td> | 54 | <td>FLOAT32, FLOAT16, BFLOAT16</td> |
| 55 | <td>ND</td> | 55 | <td>ND</td> |
| 56 | </tr> | 56 | </tr> |
| 57 | <tr> | 57 | <tr> |
| 58 | <td>y</td> | 58 | <td>y</td> |
| 59 | <td>Output</td> | 59 | <td>Output</td> |
| 60 | - <td>Denotes the output tensor list for the error function operation, corresponding to `y` in the formula. All tensors in this parameter have the same data type. Its data type and data format are consistent with those of input parameter `x`, and its shape size is greater than or equal to the shape size of input parameter `x`.</td> | 60 | + <td>Denotes the output tensor list for the complementary error function operation, corresponding to `y` in the formula. All tensors in this parameter have the same data type. Its data type and data format are consistent with those of input parameter `x`, and its shape size is greater than or equal to the shape size of input parameter `x`.</td> |
| 61 | <td>FLOAT32, FLOAT16, BFLOAT16</td> | 61 | <td>FLOAT32, FLOAT16, BFLOAT16</td> |
| 62 | <td>ND</td> | 62 | <td>ND</td> |
| 63 | </tr> | 63 | </tr> |
| @@ -67,7 +67,12 @@ English | [简体中文](./README.md) | |||
| 67 | 67 | ||
| 68 | ## Constraints | 68 | ## Constraints |
| 69 | 69 | ||
| 70 | -- The output does not support non-contiguous tensors. | 70 | +- The numbers of tensors in x and out must be the same, and the shape size of each tensor in out must be greater than or equal to that of the tensor at the corresponding position in x. |
| 71 | +- All tensors within a single tensor list of x or out must have the same data type, and the data types of x and out must be consistent, all within the supported range declared in the parameter table. | ||
| 72 | +- The number of dimensions of a single tensor cannot exceed 8. | ||
| 73 | +- The number of tensors in a single tensor list of x/y cannot exceed 256. | ||
| 74 | +- When the numbers of tensors in x and out are 0 (empty lists), the API returns success directly without performing computation. | ||
| 75 | +- Non-contiguous tensors are supported: non-contiguous inputs/outputs are automatically made contiguous for computation, and results are written back in the original layout. | ||
| 71 | 76 | ||
| 72 | ## Invocation Description | 77 | ## Invocation Description |
| 73 | 78 | ||
| @@ -42,7 +42,7 @@ | |||
| 42 | ```Cpp | 42 | ```Cpp |
| 43 | aclnnStatus aclnnForeachErfcGetWorkspaceSize( | 43 | aclnnStatus aclnnForeachErfcGetWorkspaceSize( |
| 44 | const aclTensorList *x, | 44 | const aclTensorList *x, |
| 45 | - const aclTensorList *out, | 45 | + aclTensorList *out, |
| 46 | uint64_t *workspaceSize, | 46 | uint64_t *workspaceSize, |
| 47 | aclOpExecutor **executor) | 47 | aclOpExecutor **executor) |
| 48 | ``` | 48 | ``` |
| @@ -149,16 +149,14 @@ aclnnStatus aclnnForeachErfc( | |||
| 149 | <td>传入的x或out是空指针。</td> | 149 | <td>传入的x或out是空指针。</td> |
| 150 | </tr> | 150 | </tr> |
| 151 | <tr> | 151 | <tr> |
| 152 | - <td rowspan="2">ACLNN_ERR_PARAM_INVALID</td> | 152 | + <td rowspan="8">ACLNN_ERR_PARAM_INVALID</td> |
| 153 | - <td rowspan="2">161002</td> | 153 | + <td rowspan="8">161002</td> |
| 154 | <td>x或out的数据类型不在支持的范围之内。</td> | 154 | <td>x或out的数据类型不在支持的范围之内。</td> |
| 155 | </tr> | 155 | </tr> |
| 156 | <tr> | 156 | <tr> |
| 157 | <td>x和out的数据类型不一致。</td> | 157 | <td>x和out的数据类型不一致。</td> |
| 158 | </tr> | 158 | </tr> |
| 159 | <tr> | 159 | <tr> |
| 160 | - <td rowspan="3">ACLNN_ERR_INNER_TILING_ERROR</td> | ||
| 161 | - <td rowspan="3">561002</td> | ||
| 162 | <td>x与out的shape不满足约束。</td> | 160 | <td>x与out的shape不满足约束。</td> |
| 163 | </tr> | 161 | </tr> |
| 164 | <tr> | 162 | <tr> |
| @@ -167,6 +165,20 @@ aclnnStatus aclnnForeachErfc( | |||
| 167 | <tr> | 165 | <tr> |
| 168 | <td>x或out中的Tensor维度超过8维。</td> | 166 | <td>x或out中的Tensor维度超过8维。</td> |
| 169 | </tr> | 167 | </tr> |
| 168 | + <tr> | ||
| 169 | + <td>x与out的Tensor数量不一致。</td> | ||
| 170 | + </tr> | ||
| 171 | + <tr> | ||
| 172 | + <td>x或out的Tensor列表中存在空指针的Tensor。</td> | ||
| 173 | + </tr> | ||
| 174 | + <tr> | ||
| 175 | + <td>x或out中的Tensor格式为私有格式。</td> | ||
| 176 | + </tr> | ||
| 177 | + <tr> | ||
| 178 | + <td>ACLNN_ERR_INNER_TILING_ERROR</td> | ||
| 179 | + <td>561002</td> | ||
| 180 | + <td>x或out中单个Tensor列表的Tensor数量超过256。</td> | ||
| 181 | + </tr> | ||
| 170 | </tbody></table> | 182 | </tbody></table> |
| 171 | 183 | ||
| 172 | ## aclnnForeachErfc | 184 | ## aclnnForeachErfc |
| @@ -214,6 +226,9 @@ aclnnStatus aclnnForeachErfc( | |||
| 214 | 226 | ||
| 215 | ## 约束说明 | 227 | ## 约束说明 |
| 216 | 228 | ||
| 229 | +- x/out中单个Tensor列表的Tensor数量不能超过256。 | ||
| 230 | +- x与out数量为0(空列表)时,接口直接返回成功,不执行计算。 | ||
| 231 | +- 支持非连续Tensor:非连续输入/输出自动转连续计算,结果按原布局写回。 | ||
| 217 | - 确定性计算: | 232 | - 确定性计算: |
| 218 | - aclnnForeachErfc默认确定性实现。 | 233 | - aclnnForeachErfc默认确定性实现。 |
| 219 | 234 | ||
| @@ -44,7 +44,7 @@ Each operator uses a [two-phase API](../../../docs/en/context/two_phase_api.md). | |||
| 44 | ```Cpp | 44 | ```Cpp |
| 45 | aclnnStatus aclnnForeachErfcGetWorkspaceSize( | 45 | aclnnStatus aclnnForeachErfcGetWorkspaceSize( |
| 46 | const aclTensorList *x, | 46 | const aclTensorList *x, |
| 47 | - const aclTensorList *out, | 47 | + aclTensorList *out, |
| 48 | uint64_t *workspaceSize, | 48 | uint64_t *workspaceSize, |
| 49 | aclOpExecutor **executor) | 49 | aclOpExecutor **executor) |
| 50 | ``` | 50 | ``` |
| @@ -101,7 +101,7 @@ aclnnStatus aclnnForeachErfc( | |||
| 101 | <td>BFLOAT16, FLOAT16, FLOAT32</td> | 101 | <td>BFLOAT16, FLOAT16, FLOAT32</td> |
| 102 | <td>ND</td> | 102 | <td>ND</td> |
| 103 | <td>0-8</td> | 103 | <td>0-8</td> |
| 104 | - <td>×</td> | 104 | + <td>√</td> |
| 105 | </tr> | 105 | </tr> |
| 106 | <tr> | 106 | <tr> |
| 107 | <td>workspaceSize (uint64_t*)</td> | 107 | <td>workspaceSize (uint64_t*)</td> |
| @@ -151,16 +151,14 @@ aclnnStatus aclnnForeachErfc( | |||
| 151 | <td>x or out is a null pointer.</td> | 151 | <td>x or out is a null pointer.</td> |
| 152 | </tr> | 152 | </tr> |
| 153 | <tr> | 153 | <tr> |
| 154 | - <td rowspan="2">ACLNN_ERR_PARAM_INVALID</td> | 154 | + <td rowspan="8">ACLNN_ERR_PARAM_INVALID</td> |
| 155 | - <td rowspan="2">161002</td> | 155 | + <td rowspan="8">161002</td> |
| 156 | <td>The data type of x or out is not in the supported range.</td> | 156 | <td>The data type of x or out is not in the supported range.</td> |
| 157 | </tr> | 157 | </tr> |
| 158 | <tr> | 158 | <tr> |
| 159 | <td>The data types of x and out are inconsistent.</td> | 159 | <td>The data types of x and out are inconsistent.</td> |
| 160 | </tr> | 160 | </tr> |
| 161 | <tr> | 161 | <tr> |
| 162 | - <td rowspan="3">ACLNN_ERR_INNER_TILING_ERROR</td> | ||
| 163 | - <td rowspan="3">561002</td> | ||
| 164 | <td>The shapes of x and out do not meet the constraints.</td> | 162 | <td>The shapes of x and out do not meet the constraints.</td> |
| 165 | </tr> | 163 | </tr> |
| 166 | <tr> | 164 | <tr> |
| @@ -169,6 +167,20 @@ aclnnStatus aclnnForeachErfc( | |||
| 169 | <tr> | 167 | <tr> |
| 170 | <td>The dimensions of tensors in x or out exceed 8.</td> | 168 | <td>The dimensions of tensors in x or out exceed 8.</td> |
| 171 | </tr> | 169 | </tr> |
| 170 | + <tr> | ||
| 171 | + <td>The numbers of tensors in x and out are inconsistent.</td> | ||
| 172 | + </tr> | ||
| 173 | + <tr> | ||
| 174 | + <td>A null tensor exists in the tensor list of x or out.</td> | ||
| 175 | + </tr> | ||
| 176 | + <tr> | ||
| 177 | + <td>The format of a tensor in x or out is a private format.</td> | ||
| 178 | + </tr> | ||
| 179 | + <tr> | ||
| 180 | + <td>ACLNN_ERR_INNER_TILING_ERROR</td> | ||
| 181 | + <td>561002</td> | ||
| 182 | + <td>The number of tensors in a single tensor list of x or out exceeds 256.</td> | ||
| 183 | + </tr> | ||
| 172 | </tbody></table> | 184 | </tbody></table> |
| 173 | 185 | ||
| 174 | ## aclnnForeachErfc | 186 | ## aclnnForeachErfc |
| @@ -216,6 +228,9 @@ aclnnStatus aclnnForeachErfc( | |||
| 216 | 228 | ||
| 217 | ## Constraints | 229 | ## Constraints |
| 218 | 230 | ||
| 231 | +- The number of tensors in a single tensor list of x/out cannot exceed 256. | ||
| 232 | +- When the numbers of tensors in x and out are 0 (empty lists), the API returns success directly without performing computation. | ||
| 233 | +- Non-contiguous tensors are supported: non-contiguous inputs/outputs are automatically made contiguous for computation, and results are written back in the original layout. | ||
| 219 | - Deterministic computation: | 234 | - Deterministic computation: |
| 220 | - aclnnForeachErfc defaults to a deterministic implementation. | 235 | - aclnnForeachErfc defaults to a deterministic implementation. |
| 221 | 236 | ||
| @@ -1,5 +1,5 @@ | |||
| 1 | /** | 1 | /** |
| 2 | - * Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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"). | 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. | 5 | * Please refer to the License for details. You may not use this file except in compliance with the License. |
| @@ -9,6 +9,7 @@ | |||
| 9 | */ | 9 | */ |
| 10 | 10 | ||
| 11 | #include <iostream> | 11 | #include <iostream> |
| 12 | +#include <memory> | ||
| 12 | #include <vector> | 13 | #include <vector> |
| 13 | #include "acl/acl.h" | 14 | #include "acl/acl.h" |
| 14 | #include "aclnnop/aclnn_foreach_erfc.h" | 15 | #include "aclnnop/aclnn_foreach_erfc.h" |
| @@ -70,15 +71,8 @@ int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& | |||
| 70 | return 0; | 71 | return 0; |
| 71 | } | 72 | } |
| 72 | 73 | ||
| 73 | -int main() | 74 | +int ForeachErfcExample(int32_t deviceId, const aclrtStream stream) |
| 74 | { | 75 | { |
| 75 | - // 1. (固定写法)device/stream初始化,参考acl API手册 | ||
| 76 | - // 根据自己的实际device填写deviceId | ||
| 77 | - int32_t deviceId = 0; | ||
| 78 | - aclrtStream stream; | ||
| 79 | - auto ret = Init(deviceId, &stream); | ||
| 80 | - CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | ||
| 81 | - | ||
| 82 | // 2. 构造输入与输出,需要根据API的接口自定义构造 | 76 | // 2. 构造输入与输出,需要根据API的接口自定义构造 |
| 83 | std::vector<int64_t> selfShape1 = {2, 3}; | 77 | std::vector<int64_t> selfShape1 = {2, 3}; |
| 84 | std::vector<int64_t> selfShape2 = {1, 3}; | 78 | std::vector<int64_t> selfShape2 = {1, 3}; |
| @@ -98,25 +92,33 @@ int main() | |||
| 98 | std::vector<float> out2HostData(3, 0); | 92 | std::vector<float> out2HostData(3, 0); |
| 99 | 93 | ||
| 100 | // 创建input1 aclTensor | 94 | // 创建input1 aclTensor |
| 101 | - ret = CreateAclTensor(input1HostData, selfShape1, &input1DeviceAddr, aclDataType::ACL_FLOAT, &input1); | 95 | + auto ret = CreateAclTensor(input1HostData, selfShape1, &input1DeviceAddr, aclDataType::ACL_FLOAT, &input1); |
| 96 | + std::unique_ptr<void, aclError (*)(void*)> input1DeviceAddrPtr(input1DeviceAddr, aclrtFree); | ||
| 102 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 97 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 103 | 98 | ||
| 104 | // 创建input2 aclTensor | 99 | // 创建input2 aclTensor |
| 105 | ret = CreateAclTensor(input2HostData, selfShape2, &input2DeviceAddr, aclDataType::ACL_FLOAT, &input2); | 100 | ret = CreateAclTensor(input2HostData, selfShape2, &input2DeviceAddr, aclDataType::ACL_FLOAT, &input2); |
| 101 | + std::unique_ptr<void, aclError (*)(void*)> input2DeviceAddrPtr(input2DeviceAddr, aclrtFree); | ||
| 106 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 102 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 107 | 103 | ||
| 108 | // 创建out1 aclTensor | 104 | // 创建out1 aclTensor |
| 109 | ret = CreateAclTensor(out1HostData, outShape1, &out1DeviceAddr, aclDataType::ACL_FLOAT, &out1); | 105 | ret = CreateAclTensor(out1HostData, outShape1, &out1DeviceAddr, aclDataType::ACL_FLOAT, &out1); |
| 106 | + std::unique_ptr<void, aclError (*)(void*)> out1DeviceAddrPtr(out1DeviceAddr, aclrtFree); | ||
| 110 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 107 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 111 | 108 | ||
| 112 | // 创建out2 aclTensor | 109 | // 创建out2 aclTensor |
| 113 | ret = CreateAclTensor(out2HostData, outShape2, &out2DeviceAddr, aclDataType::ACL_FLOAT, &out2); | 110 | ret = CreateAclTensor(out2HostData, outShape2, &out2DeviceAddr, aclDataType::ACL_FLOAT, &out2); |
| 111 | + std::unique_ptr<void, aclError (*)(void*)> out2DeviceAddrPtr(out2DeviceAddr, aclrtFree); | ||
| 114 | CHECK_RET(ret == ACL_SUCCESS, return ret); | 112 | CHECK_RET(ret == ACL_SUCCESS, return ret); |
| 115 | 113 | ||
| 116 | std::vector<aclTensor*> tempInput{input1, input2}; | 114 | std::vector<aclTensor*> tempInput{input1, input2}; |
| 117 | aclTensorList* tensorListInput = aclCreateTensorList(tempInput.data(), tempInput.size()); | 115 | aclTensorList* tensorListInput = aclCreateTensorList(tempInput.data(), tempInput.size()); |
| 116 | + std::unique_ptr<aclTensorList, aclnnStatus (*)(const aclTensorList*)> tensorListInputPtr(tensorListInput, | ||
| 117 | + aclDestroyTensorList); | ||
| 118 | std::vector<aclTensor*> tempOutput{out1, out2}; | 118 | std::vector<aclTensor*> tempOutput{out1, out2}; |
| 119 | aclTensorList* tensorListOutput = aclCreateTensorList(tempOutput.data(), tempOutput.size()); | 119 | aclTensorList* tensorListOutput = aclCreateTensorList(tempOutput.data(), tempOutput.size()); |
| 120 | + std::unique_ptr<aclTensorList, aclnnStatus (*)(const aclTensorList*)> tensorListOutputPtr(tensorListOutput, | ||
| 121 | + aclDestroyTensorList); | ||
| 120 | 122 | ||
| 121 | // 3. 调用CANN算子库API,需要修改为具体的API名称 | 123 | // 3. 调用CANN算子库API,需要修改为具体的API名称 |
| 122 | uint64_t workspaceSize = 0; | 124 | uint64_t workspaceSize = 0; |
| @@ -126,9 +128,11 @@ int main() | |||
| 126 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnForeachErfcGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); | 128 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnForeachErfcGetWorkspaceSize failed. ERROR: %d\n", ret); return ret); |
| 127 | // 根据第一段接口计算出的workspaceSize申请device内存 | 129 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 128 | void* workspaceAddr = nullptr; | 130 | void* workspaceAddr = nullptr; |
| 131 | + std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree); | ||
| 129 | if (workspaceSize > 0) { | 132 | if (workspaceSize > 0) { |
| 130 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); | 133 | ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST); |
| 131 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); | 134 | CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret); |
| 135 | + workspaceAddrPtr.reset(workspaceAddr); | ||
| 132 | } | 136 | } |
| 133 | // 调用aclnnForeachErfc第二段接口 | 137 | // 调用aclnnForeachErfc第二段接口 |
| 134 | ret = aclnnForeachErfc(workspaceAddr, workspaceSize, executor, stream); | 138 | ret = aclnnForeachErfc(workspaceAddr, workspaceSize, executor, stream); |
| @@ -157,20 +161,24 @@ int main() | |||
| 157 | LOG_PRINT("out2 result[%ld] is: %f\n", i, out2Data[i]); | 161 | LOG_PRINT("out2 result[%ld] is: %f\n", i, out2Data[i]); |
| 158 | } | 162 | } |
| 159 | 163 | ||
| 160 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 164 | + return ACL_SUCCESS; |
| 161 | - aclDestroyTensorList(tensorListInput); | 165 | +} |
| 162 | - aclDestroyTensorList(tensorListOutput); | ||
| 163 | 166 | ||
| 164 | - // 7.释放device资源,需要根据具体API的接口定义修改 | 167 | +int main() |
| 165 | - aclrtFree(input1DeviceAddr); | 168 | +{ |
| 166 | - aclrtFree(input2DeviceAddr); | 169 | + // 1. (固定写法)device/stream初始化,参考acl API手册 |
| 167 | - aclrtFree(out1DeviceAddr); | 170 | + // 根据自己的实际device填写deviceId |
| 168 | - aclrtFree(out2DeviceAddr); | 171 | + int32_t deviceId = 0; |
| 169 | - if (workspaceSize > 0) { | 172 | + aclrtStream stream; |
| 170 | - aclrtFree(workspaceAddr); | 173 | + auto ret = Init(deviceId, &stream); |
| 171 | - } | 174 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 175 | + | ||
| 176 | + ret = ForeachErfcExample(deviceId, stream); | ||
| 177 | + | ||
| 178 | + // 6/7. 释放资源:aclTensor、TensorList、device内存在示例内任意路径(含失败提前返回)均由 | ||
| 179 | + // unique_ptr 自动释放;stream与设备上下文在此处统一释放 | ||
| 172 | aclrtDestroyStream(stream); | 180 | aclrtDestroyStream(stream); |
| 173 | aclrtResetDevice(deviceId); | 181 | aclrtResetDevice(deviceId); |
| 174 | aclFinalize(); | 182 | aclFinalize(); |
| 175 | - return 0; | 183 | + return ret == ACL_SUCCESS ? 0 : ret; |
| 176 | } | 184 | } |
| @@ -21,6 +21,7 @@ | |||
| 21 | #include "tiling/tiling_api.h" | 21 | #include "tiling/tiling_api.h" |
| 22 | #include "platform/platform_info.h" | 22 | #include "platform/platform_info.h" |
| 23 | #include "log/log.h" | 23 | #include "log/log.h" |
| 24 | +#include "graph/utils/type_utils.h" | ||
| 24 | #include "foreach_erfc_tiling_arch35.h" | 25 | #include "foreach_erfc_tiling_arch35.h" |
| 25 | 26 | ||
| 26 | using namespace AscendC; | 27 | using namespace AscendC; |
| @@ -31,6 +32,7 @@ constexpr int64_t DCACHE_SIZE = 128 * 1024; | |||
| 31 | constexpr int64_t SINGLE_CORE_MIN_ELEMENTS = 1024; | 32 | constexpr int64_t SINGLE_CORE_MIN_ELEMENTS = 1024; |
| 32 | constexpr int64_t ALIGN_SIZE = 32; | 33 | constexpr int64_t ALIGN_SIZE = 32; |
| 33 | constexpr int64_t INPUT_IDX = 0; | 34 | constexpr int64_t INPUT_IDX = 0; |
| 35 | +constexpr size_t MAX_DIM_NUM = 8U; | ||
| 34 | 36 | ||
| 35 | static ge::graphStatus GetTilingKeyByDtype(gert::TilingContext* context, ge::DataType dtype, uint64_t& tilingKey) | 37 | static ge::graphStatus GetTilingKeyByDtype(gert::TilingContext* context, ge::DataType dtype, uint64_t& tilingKey) |
| 36 | { | 38 | { |
| @@ -66,6 +68,99 @@ static ge::graphStatus GetPlatformInfoFallback(gert::TilingContext* context, int | |||
| 66 | return ge::GRAPH_FAILED; | 68 | return ge::GRAPH_FAILED; |
| 67 | } | 69 | } |
| 68 | 70 | ||
| 71 | +static bool IsSupportedInputDtype(ge::DataType dtype) | ||
| 72 | +{ | ||
| 73 | + return dtype == ge::DT_FLOAT || dtype == ge::DT_FLOAT16 || dtype == ge::DT_BF16; | ||
| 74 | +} | ||
| 75 | + | ||
| 76 | +static ge::graphStatus ValidateForeachErfc(gert::TilingContext* context, int32_t tensorNum) | ||
| 77 | +{ | ||
| 78 | + // 校验 x 与 y 的 Tensor 数量一致 | ||
| 79 | + OP_CHECK_IF(context->GetComputeNodeOutputNum() != static_cast<uint64_t>(tensorNum), | ||
| 80 | + OP_LOGE_FOR_INVALID_TENSORNUMS_WITH_REASON( | ||
| 81 | + context->GetNodeName(), "x and y", | ||
| 82 | + (std::to_string(tensorNum) + " and " + std::to_string(context->GetComputeNodeOutputNum())).c_str(), | ||
| 83 | + "The tensor nums in {x, y} must be the same"), | ||
| 84 | + return ge::GRAPH_FAILED); | ||
| 85 | + | ||
| 86 | + ge::DataType listDtype = ge::DT_UNDEFINED; | ||
| 87 | + for (int32_t i = 0; i < tensorNum; i++) { | ||
| 88 | + auto inputDesc = context->GetDynamicInputDesc(INPUT_IDX, i); | ||
| 89 | + // 校验 x 的 desc 非空 | ||
| 90 | + OP_CHECK_IF(inputDesc == nullptr, OP_LOGE(context, "x[%d] descriptor is null", i), return ge::GRAPH_FAILED); | ||
| 91 | + auto inputShape = context->GetDynamicInputShape(INPUT_IDX, i); | ||
| 92 | + // 校验 x 的 shape 非空 | ||
| 93 | + OP_CHECK_IF(inputShape == nullptr, OP_LOGE(context, "x[%d] shape is null", i), return ge::GRAPH_FAILED); | ||
| 94 | + const gert::Shape& inputStorageShape = inputShape->GetStorageShape(); | ||
| 95 | + // 校验 x 的维度不超过 8 维 | ||
| 96 | + OP_CHECK_IF(inputStorageShape.GetDimNum() > MAX_DIM_NUM, | ||
| 97 | + OP_LOGE_FOR_INVALID_SHAPEDIM_WITH_REASON( | ||
| 98 | + context->GetNodeName(), "x", std::to_string(inputStorageShape.GetDimNum()).c_str(), | ||
| 99 | + ("The shape dim of the " + std::to_string(i) + | ||
| 100 | + "th tensor in the tensor list should be less than or equal to " + std::to_string(MAX_DIM_NUM)) | ||
| 101 | + .c_str()), | ||
| 102 | + return ge::GRAPH_FAILED); | ||
| 103 | + // 校验 x 的元素数非负(GetShapeSize 溢出时返回 INT64_MIN 哨兵,一并拦截) | ||
| 104 | + OP_CHECK_IF(inputStorageShape.GetShapeSize() < 0, | ||
| 105 | + OP_LOGE_FOR_INVALID_SHAPESIZE_WITH_REASON( | ||
| 106 | + context->GetNodeName(), "x", std::to_string(inputStorageShape.GetShapeSize()).c_str(), | ||
| 107 | + ("The shape size of the " + std::to_string(i) + | ||
| 108 | + "th tensor in the tensor list must be 0 or a positive number") | ||
| 109 | + .c_str()), | ||
| 110 | + return ge::GRAPH_FAILED); | ||
| 111 | + ge::DataType inputDtype = inputDesc->GetDataType(); | ||
| 112 | + // 校验 x 的 dtype 在支持范围内 | ||
| 113 | + OP_CHECK_IF(!IsSupportedInputDtype(inputDtype), | ||
| 114 | + OP_LOGE_FOR_INVALID_DTYPE(context->GetNodeName(), "x", | ||
| 115 | + ge::TypeUtils::DataTypeToSerialString(inputDtype).c_str(), | ||
| 116 | + "DT_FLOAT, DT_FLOAT16 or DT_BF16"), | ||
| 117 | + return ge::GRAPH_FAILED); | ||
| 118 | + if (listDtype == ge::DT_UNDEFINED) { | ||
| 119 | + listDtype = inputDtype; | ||
| 120 | + } else { | ||
| 121 | + // 校验 x 列表内各 Tensor 的 dtype 一致 | ||
| 122 | + OP_CHECK_IF(inputDtype != listDtype, | ||
| 123 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 124 | + context->GetNodeName(), "x", ge::TypeUtils::DataTypeToSerialString(inputDtype).c_str(), | ||
| 125 | + ("The dtypes of all tensors in the tensor list must be the same. " | ||
| 126 | + "Currently, the dtype of the " + | ||
| 127 | + std::to_string(i) + "th tensor is inconsistent with that (" + | ||
| 128 | + ge::TypeUtils::DataTypeToSerialString(listDtype) + ") of other tensors") | ||
| 129 | + .c_str()), | ||
| 130 | + return ge::GRAPH_FAILED); | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + auto outputDesc = context->GetOutputDesc(i); | ||
| 134 | + // 校验 y 的 desc 非空 | ||
| 135 | + OP_CHECK_IF(outputDesc == nullptr, OP_LOGE(context, "y[%d] descriptor is null", i), return ge::GRAPH_FAILED); | ||
| 136 | + auto outputShape = context->GetOutputShape(i); | ||
| 137 | + // 校验 y 的 shape 非空 | ||
| 138 | + OP_CHECK_IF(outputShape == nullptr, OP_LOGE(context, "y[%d] shape is null", i), return ge::GRAPH_FAILED); | ||
| 139 | + // 校验 y 的 dtype 与 x 一致 | ||
| 140 | + OP_CHECK_IF( | ||
| 141 | + outputDesc->GetDataType() != listDtype, | ||
| 142 | + OP_LOGE_FOR_INVALID_DTYPE_WITH_REASON( | ||
| 143 | + context->GetNodeName(), "y", ge::TypeUtils::DataTypeToSerialString(outputDesc->GetDataType()).c_str(), | ||
| 144 | + "The dtype of y must be the same as x"), | ||
| 145 | + return ge::GRAPH_FAILED); | ||
| 146 | + const gert::Shape& outputStorageShape = outputShape->GetStorageShape(); | ||
| 147 | + // 校验 y 的 shape size 不小于对应位置的 x(与 arch22 公共校验口径一致:仅拒绝 y 小于 x) | ||
| 148 | + OP_CHECK_IF(inputStorageShape.GetShapeSize() > outputStorageShape.GetShapeSize(), | ||
| 149 | + OP_LOGE_FOR_INVALID_SHAPESIZES_WITH_REASON( | ||
| 150 | + context->GetNodeName(), "x and y", | ||
| 151 | + (std::to_string(inputStorageShape.GetShapeSize()) + " and " + | ||
| 152 | + std::to_string(outputStorageShape.GetShapeSize())) | ||
| 153 | + .c_str(), | ||
| 154 | + ("The shape size of " + std::to_string(i) + | ||
| 155 | + "th tensor in tensor list y should be greater than or equal to that of the tensor " | ||
| 156 | + "in the same position of the another tensor list x") | ||
| 157 | + .c_str()), | ||
| 158 | + return ge::GRAPH_FAILED); | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + return ge::GRAPH_SUCCESS; | ||
| 162 | +} | ||
| 163 | + | ||
| 69 | static ge::graphStatus ForeachErfcTilingFunc(gert::TilingContext* context) | 164 | static ge::graphStatus ForeachErfcTilingFunc(gert::TilingContext* context) |
| 70 | { | 165 | { |
| 71 | int64_t coreNum = 0; | 166 | int64_t coreNum = 0; |
| @@ -80,18 +175,24 @@ static ge::graphStatus ForeachErfcTilingFunc(gert::TilingContext* context) | |||
| 80 | OP_CHECK_NULL_WITH_CONTEXT(context, computeNodeInfoPtr); | 175 | OP_CHECK_NULL_WITH_CONTEXT(context, computeNodeInfoPtr); |
| 81 | auto idxInstanceInfoPtr = computeNodeInfoPtr->GetInputInstanceInfo(INPUT_IDX); | 176 | auto idxInstanceInfoPtr = computeNodeInfoPtr->GetInputInstanceInfo(INPUT_IDX); |
| 82 | OP_CHECK_NULL_WITH_CONTEXT(context, idxInstanceInfoPtr); | 177 | OP_CHECK_NULL_WITH_CONTEXT(context, idxInstanceInfoPtr); |
| 83 | - uint64_t tensorNum = idxInstanceInfoPtr->GetInstanceNum(); | 178 | + uint64_t instanceNum = idxInstanceInfoPtr->GetInstanceNum(); |
| 84 | - | 179 | + // 窄化前校验数量上限,防止超界实例数经 int32 截断绕过定长数组边界 |
| 85 | - OP_CHECK_IF((static_cast<int32_t>(tensorNum) > MAX_TENSOR_NUM_FOREACH_ERFC), | 180 | + OP_CHECK_IF(instanceNum > static_cast<uint64_t>(MAX_TENSOR_NUM_FOREACH_ERFC), |
🟡 Medium Priority 建议:核对 op_host 日志宏头文件中 tensor 数量非法对应的宏名(含 _WITH_REASON 变体),将第 178 行改成与第 80 行一致的名字(如 OP_LOGE_FOR_INVALID_TENSORNUMS),并确认实参顺序与宏定义一致。 ![]() ![]() Incorrect? | |||
| 86 | - OP_LOGE(context, "tensorNum %lu exceeds MAX_TENSOR_NUM_FOREACH_ERFC %d", tensorNum, | 181 | + OP_LOGE_FOR_INVALID_TENSORNUM( |
| 87 | - MAX_TENSOR_NUM_FOREACH_ERFC), | 182 | + context->GetNodeName(), "x", static_cast<int64_t>(instanceNum), |
| 183 | + ("within the range [0, " + std::to_string(MAX_TENSOR_NUM_FOREACH_ERFC) + "]").c_str()), | ||
| 88 | return ge::GRAPH_FAILED); | 184 | return ge::GRAPH_FAILED); |
| 185 | + int32_t tensorNum = static_cast<int32_t>(instanceNum); | ||
| 186 | + | ||
| 187 | + if (ValidateForeachErfc(context, tensorNum) != ge::GRAPH_SUCCESS) { | ||
| 188 | + return ge::GRAPH_FAILED; | ||
| 189 | + } | ||
| 89 | 190 | ||
| 90 | int64_t totalElements = 0; | 191 | int64_t totalElements = 0; |
| 91 | ge::DataType dataType = ge::DT_FLOAT; | 192 | ge::DataType dataType = ge::DT_FLOAT; |
| 92 | ForeachErfcTilingData* tilingData = context->GetTilingData<ForeachErfcTilingData>(); | 193 | ForeachErfcTilingData* tilingData = context->GetTilingData<ForeachErfcTilingData>(); |
| 93 | 194 | ||
| 94 | - for (uint64_t i = 0; i < tensorNum; i++) { | 195 | + for (int32_t i = 0; i < tensorNum; i++) { |
| 95 | auto idxTensorShapePtr = context->GetDynamicInputShape(INPUT_IDX, i); | 196 | auto idxTensorShapePtr = context->GetDynamicInputShape(INPUT_IDX, i); |
| 96 | OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorShapePtr); | 197 | OP_CHECK_NULL_WITH_CONTEXT(context, idxTensorShapePtr); |
| 97 | auto idxTensorShape = idxTensorShapePtr->GetStorageShape(); | 198 | auto idxTensorShape = idxTensorShapePtr->GetStorageShape(); |
| @@ -167,6 +167,8 @@ static aclnnStatus ExecForeachErfcGetWorkspaceSize(const aclTensorList* x, const | |||
| 167 | aclnnStatus aclnnForeachErfcGetWorkspaceSize(const aclTensorList* x, aclTensorList* out, uint64_t* workspaceSize, | 167 | aclnnStatus aclnnForeachErfcGetWorkspaceSize(const aclTensorList* x, aclTensorList* out, uint64_t* workspaceSize, |
| 168 | aclOpExecutor** executor) | 168 | aclOpExecutor** executor) |
| 169 | { | 169 | { |
| 170 | + OP_CHECK_COMM_INPUT(workspaceSize, executor); | ||
| 171 | + | ||
| 170 | L2_DFX_PHASE_1(aclnnForeachErfc, DFX_IN(x), DFX_OUT(out)); | 172 | L2_DFX_PHASE_1(aclnnForeachErfc, DFX_IN(x), DFX_OUT(out)); |
| 171 | return ExecForeachErfcGetWorkspaceSize(x, out, workspaceSize, executor); | 173 | return ExecForeachErfcGetWorkspaceSize(x, out, workspaceSize, executor); |
| 172 | } | 174 | } |
| @@ -11,6 +11,8 @@ | |||
| 11 | /* Generated By CANNBot */ | 11 | /* Generated By CANNBot */ |
| 12 | 12 | ||
| 13 | #include <iostream> | 13 | #include <iostream> |
| 14 | +#include <map> | ||
| 15 | +#include <vector> | ||
| 14 | #include <gtest/gtest.h> | 16 | #include <gtest/gtest.h> |
| 15 | #include "log/log.h" | 17 | #include "log/log.h" |
| 16 | #include "kernel_run_context_facker.h" | 18 | #include "kernel_run_context_facker.h" |
| @@ -19,69 +21,121 @@ | |||
| 19 | #include "exe_graph/runtime/storage_shape.h" | 21 | #include "exe_graph/runtime/storage_shape.h" |
| 20 | #include "platform/platform_infos_def.h" | 22 | #include "platform/platform_infos_def.h" |
| 21 | #include "test_cube_util.h" | 23 | #include "test_cube_util.h" |
| 22 | -#include "ut_op_util.h" | ||
| 23 | #include "../../../../op_kernel/arch35/foreach_erfc_tiling_data.h" | 24 | #include "../../../../op_kernel/arch35/foreach_erfc_tiling_data.h" |
| 24 | #include "../../../../op_kernel/arch35/foreach_erfc_tiling_key.h" | 25 | #include "../../../../op_kernel/arch35/foreach_erfc_tiling_key.h" |
| 25 | 26 | ||
| 26 | -using namespace ut_util; | ||
| 27 | - | ||
| 28 | class ForeachErfcTilingTest : public testing::Test { | 27 | class ForeachErfcTilingTest : public testing::Test { |
| 29 | protected: | 28 | protected: |
| 30 | static void SetUpTestCase() { std::cout << "ForeachErfcTilingTest SetUp" << std::endl; } | 29 | static void SetUpTestCase() { std::cout << "ForeachErfcTilingTest SetUp" << std::endl; } |
| 31 | static void TearDownTestCase() { std::cout << "ForeachErfcTilingTest TearDown" << std::endl; } | 30 | static void TearDownTestCase() { std::cout << "ForeachErfcTilingTest TearDown" << std::endl; } |
| 32 | }; | 31 | }; |
| 33 | 32 | ||
| 33 | +static const char* FOREACH_ERFC_PLATFORM_JSON = R"({ | ||
| 34 | + "hardware_info": {"BT_SIZE": 0, "load3d_constraints": "1", | ||
| 35 | + "Intrinsic_fix_pipe_l0c2out": false, "Intrinsic_data_move_l12ub": true, "Intrinsic_data_move_l0c2ub": true, "Intrinsic_data_move_out2l1_nd2nz": false, | ||
| 36 | + "UB_SIZE": 253952, "L2_SIZE": 33554432, "L1_SIZE": 524288, | ||
| 37 | + "L0A_SIZE": 65536, "L0B_SIZE": 65536, "L0C_SIZE": 131072, | ||
| 38 | + "CORE_NUM": 64} | ||
| 39 | + })"; | ||
| 40 | + | ||
| 41 | +static gert::StorageShape MakeStorageShape(const std::vector<int64_t>& dims) | ||
| 42 | +{ | ||
| 43 | + gert::StorageShape shape; | ||
| 44 | + for (auto dim : dims) { | ||
| 45 | + shape.MutableOriginShape().AppendDim(dim); | ||
| 46 | + shape.MutableStorageShape().AppendDim(dim); | ||
| 47 | + } | ||
| 48 | + return shape; | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +static void RunTilingCase(const std::vector<std::vector<int64_t>>& xDimsList, | ||
| 52 | + const std::vector<std::vector<int64_t>>& yDimsList, ge::DataType xDtype, | ||
| 53 | + const std::vector<ge::DataType>& yDtypes, bool expectSuccess, uint64_t expectKey = 0U) | ||
| 54 | +{ | ||
| 55 | + const std::string opType("ForeachErfc"); | ||
| 56 | + ASSERT_NE(gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str()), nullptr); | ||
| 57 | + auto tilingFunc = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling; | ||
| 58 | + auto tilingParseFunc = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling_parse; | ||
| 59 | + ASSERT_NE(tilingFunc, nullptr); | ||
| 60 | + ASSERT_NE(tilingParseFunc, nullptr); | ||
| 61 | + | ||
| 62 | + std::map<std::string, std::string> socInfos; | ||
| 63 | + std::map<std::string, std::string> aicoreSpec; | ||
| 64 | + std::map<std::string, std::string> intrinsics; | ||
| 65 | + GetPlatFormInfos(FOREACH_ERFC_PLATFORM_JSON, socInfos, aicoreSpec, intrinsics); | ||
| 66 | + fe::PlatFormInfos platformInfo; | ||
| 67 | + platformInfo.Init(); | ||
| 68 | + struct ForeachErfcCompileInfo { | ||
| 69 | + } compileInfo; | ||
| 70 | + | ||
| 71 | + auto parseHolder = gert::KernelRunContextFaker() | ||
| 72 | + .KernelIONum(2, 1) | ||
| 73 | + .Inputs( | ||
| 74 | + {const_cast<char*>(FOREACH_ERFC_PLATFORM_JSON), reinterpret_cast<void*>(&platformInfo)}) | ||
| 75 | + .Outputs({&compileInfo}) | ||
| 76 | + .Build(); | ||
| 77 | + ASSERT_TRUE(parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->Init()); | ||
| 78 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("SoCInfo", socInfos); | ||
| 79 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreSpec", aicoreSpec); | ||
| 80 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 81 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", | ||
| 82 | + intrinsics); | ||
| 83 | + ASSERT_EQ(tilingParseFunc(parseHolder.GetContext<gert::KernelContext>()), ge::GRAPH_SUCCESS); | ||
| 84 | + | ||
| 85 | + std::vector<gert::StorageShape> xStorages; | ||
| 86 | + std::vector<gert::StorageShape> yStorages; | ||
| 87 | + xStorages.reserve(xDimsList.size()); | ||
| 88 | + yStorages.reserve(yDimsList.size()); | ||
| 89 | + for (const auto& dims : xDimsList) { | ||
| 90 | + xStorages.emplace_back(MakeStorageShape(dims)); | ||
| 91 | + } | ||
| 92 | + for (const auto& dims : yDimsList) { | ||
| 93 | + yStorages.emplace_back(MakeStorageShape(dims)); | ||
| 94 | + } | ||
| 95 | + std::vector<gert::StorageShape*> xShapePtrs; | ||
| 96 | + std::vector<gert::StorageShape*> yShapePtrs; | ||
| 97 | + for (auto& shape : xStorages) { | ||
| 98 | + xShapePtrs.emplace_back(&shape); | ||
| 99 | + } | ||
| 100 | + for (auto& shape : yStorages) { | ||
| 101 | + yShapePtrs.emplace_back(&shape); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + auto param = gert::TilingData::CreateCap(4096); | ||
| 105 | + ASSERT_NE(param, nullptr); | ||
| 106 | + auto workspaceSizeHolder = gert::ContinuousVector::Create<size_t>(4096); | ||
| 107 | + auto wsSize = reinterpret_cast<gert::ContinuousVector*>(workspaceSizeHolder.get()); | ||
| 108 | + gert::TilingContextFaker tilingFaker; | ||
| 109 | + tilingFaker.NodeIoNum(1, 1) | ||
| 110 | + .IrInstanceNum({static_cast<uint32_t>(xShapePtrs.size())}, {static_cast<uint32_t>(yShapePtrs.size())}) | ||
| 111 | + .InputShapes(xShapePtrs) | ||
| 112 | + .OutputShapes(yShapePtrs) | ||
| 113 | + .CompileInfo(&compileInfo) | ||
| 114 | + .PlatformInfo(reinterpret_cast<char*>(&platformInfo)) | ||
| 115 | + .NodeInputTd(0, xDtype, ge::FORMAT_ND, ge::FORMAT_ND) | ||
| 116 | + .TilingData(param.get()) | ||
| 117 | + .Workspace(wsSize); | ||
| 118 | + for (size_t i = 0; i < yDtypes.size(); i++) { | ||
| 119 | + tilingFaker.NodeOutputTd(static_cast<int32_t>(i), yDtypes[i], ge::FORMAT_ND, ge::FORMAT_ND); | ||
| 120 | + } | ||
| 121 | + auto holder = tilingFaker.Build(); | ||
| 122 | + auto* tilingContext = holder.GetContext<gert::TilingContext>(); | ||
| 123 | + tilingContext->GetPlatformInfo()->SetPlatformRes("SoCInfo", socInfos); | ||
| 124 | + tilingContext->GetPlatformInfo()->SetPlatformRes("AICoreSpec", aicoreSpec); | ||
| 125 | + tilingContext->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 126 | + tilingContext->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intrinsics); | ||
| 127 | + | ||
| 128 | + if (expectSuccess) { | ||
| 129 | + EXPECT_EQ(tilingFunc(tilingContext), ge::GRAPH_SUCCESS); | ||
| 130 | + EXPECT_EQ(tilingContext->GetTilingKey(), expectKey); | ||
| 131 | + } else { | ||
| 132 | + EXPECT_EQ(tilingFunc(tilingContext), ge::GRAPH_FAILED); | ||
| 133 | + } | ||
| 134 | +} | ||
| 135 | + | ||
| 34 | static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint64_t expectKey) | 136 | static void DoCase(std::initializer_list<int64_t> xShape, ge::DataType dt, uint64_t expectKey) |
| 35 | { | 137 | { |
| 36 | - fe::PlatFormInfos pf; | 138 | + RunTilingCase({std::vector<int64_t>(xShape)}, {std::vector<int64_t>(xShape)}, dt, {dt}, true, expectKey); |
| 37 | - std::map<std::string, std::string> soc, ai, intr; | ||
| 38 | - GetPlatFormInfos(R"({"hardware_info":{"UB_SIZE":253952,"CORE_NUM":64}})", soc, ai, intr); | ||
| 39 | - pf.Init(); | ||
| 40 | - | ||
| 41 | - struct ForeachErfcCompileInfo { | ||
| 42 | - } ci; | ||
| 43 | - auto opType = std::string("ForeachErfc"); | ||
| 44 | - auto tilingFn = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling; | ||
| 45 | - auto tilingParseFn = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling_parse; | ||
| 46 | - ASSERT_NE(tilingFn, nullptr); | ||
| 47 | - ASSERT_NE(tilingParseFn, nullptr); | ||
| 48 | - | ||
| 49 | - std::string ciStr = R"({"device_id":null})"; | ||
| 50 | - auto kh = gert::KernelRunContextFaker() | ||
| 51 | - .KernelIONum(2, 1) | ||
| 52 | - .Inputs({const_cast<char*>(ciStr.c_str()), reinterpret_cast<void*>(&pf)}) | ||
| 53 | - .Outputs({&ci}) | ||
| 54 | - .Build(); | ||
| 55 | - kh.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->Init(); | ||
| 56 | - kh.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("SoCInfo", soc); | ||
| 57 | - kh.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreSpec", ai); | ||
| 58 | - kh.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 59 | - kh.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intr); | ||
| 60 | - ASSERT_EQ(tilingParseFn(kh.GetContext<gert::KernelContext>()), ge::GRAPH_SUCCESS); | ||
| 61 | - | ||
| 62 | - gert::StorageShape xS = {xShape, xShape}; | ||
| 63 | - gert::StorageShape yS = {xShape, xShape}; | ||
| 64 | - auto param = gert::TilingData::CreateCap(4096); | ||
| 65 | - auto wsh = gert::ContinuousVector::Create<size_t>(4096); | ||
| 66 | - auto th = gert::TilingContextFaker() | ||
| 67 | - .NodeIoNum(1, 1) | ||
| 68 | - .IrInstanceNum({1}) | ||
| 69 | - .InputShapes({&xS}) | ||
| 70 | - .OutputShapes({&yS}) | ||
| 71 | - .CompileInfo(&ci) | ||
| 72 | - .PlatformInfo(reinterpret_cast<char*>(&pf)) | ||
| 73 | - .NodeInputTd(0, dt, ge::FORMAT_ND, ge::FORMAT_ND) | ||
| 74 | - .NodeOutputTd(0, dt, ge::FORMAT_ND, ge::FORMAT_ND) | ||
| 75 | - .TilingData(param.get()) | ||
| 76 | - .Workspace(reinterpret_cast<gert::ContinuousVector*>(wsh.get())) | ||
| 77 | - .Build(); | ||
| 78 | - auto* ctx = th.GetContext<gert::TilingContext>(); | ||
| 79 | - ctx->GetPlatformInfo()->SetPlatformRes("SoCInfo", soc); | ||
| 80 | - ctx->GetPlatformInfo()->SetPlatformRes("AICoreSpec", ai); | ||
| 81 | - ctx->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 82 | - ctx->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intr); | ||
| 83 | - EXPECT_EQ(tilingFn(ctx), ge::GRAPH_SUCCESS); | ||
| 84 | - EXPECT_EQ(ctx->GetTilingKey(), expectKey); | ||
| 85 | } | 139 | } |
| 86 | 140 | ||
| 87 | TEST_F(ForeachErfcTilingTest, foreach_erfc_float32) | 141 | TEST_F(ForeachErfcTilingTest, foreach_erfc_float32) |
| @@ -98,3 +152,128 @@ TEST_F(ForeachErfcTilingTest, foreach_erfc_bfloat16) | |||
| 98 | { | 152 | { |
| 99 | DoCase({4, 4}, ge::DT_BF16, GET_TPL_TILING_KEY(FOREACH_ERFC_TPL_SCH_MODE_BF16)); | 153 | DoCase({4, 4}, ge::DT_BF16, GET_TPL_TILING_KEY(FOREACH_ERFC_TPL_SCH_MODE_BF16)); |
| 100 | } | 154 | } |
| 155 | + | ||
| 156 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_multi_instance_success) | ||
| 157 | +{ | ||
| 158 | + RunTilingCase({{4, 4}, {2, 8}}, {{4, 4}, {2, 8}}, ge::DT_FLOAT, {ge::DT_FLOAT, ge::DT_FLOAT}, true, | ||
| 159 | + GET_TPL_TILING_KEY(FOREACH_ERFC_TPL_SCH_MODE_FP32)); | ||
| 160 | +} | ||
| 161 | + | ||
| 162 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_mixed_dtype_list_rejected) | ||
| 163 | +{ | ||
| 164 | + const std::string opType("ForeachErfc"); | ||
| 165 | + ASSERT_NE(gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str()), nullptr); | ||
| 166 | + auto tilingFunc = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling; | ||
| 167 | + auto tilingParseFunc = gert::OpImplRegistry::GetInstance().GetOpImpl(opType.c_str())->tiling_parse; | ||
| 168 | + ASSERT_NE(tilingFunc, nullptr); | ||
| 169 | + ASSERT_NE(tilingParseFunc, nullptr); | ||
| 170 | + | ||
| 171 | + std::map<std::string, std::string> socInfos; | ||
| 172 | + std::map<std::string, std::string> aicoreSpec; | ||
| 173 | + std::map<std::string, std::string> intrinsics; | ||
| 174 | + GetPlatFormInfos(FOREACH_ERFC_PLATFORM_JSON, socInfos, aicoreSpec, intrinsics); | ||
| 175 | + fe::PlatFormInfos platformInfo; | ||
| 176 | + platformInfo.Init(); | ||
| 177 | + struct ForeachErfcCompileInfo { | ||
| 178 | + } compileInfo; | ||
| 179 | + | ||
| 180 | + auto parseHolder = gert::KernelRunContextFaker() | ||
| 181 | + .KernelIONum(2, 1) | ||
| 182 | + .Inputs( | ||
| 183 | + {const_cast<char*>(FOREACH_ERFC_PLATFORM_JSON), reinterpret_cast<void*>(&platformInfo)}) | ||
| 184 | + .Outputs({&compileInfo}) | ||
| 185 | + .Build(); | ||
| 186 | + ASSERT_TRUE(parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->Init()); | ||
| 187 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("SoCInfo", socInfos); | ||
| 188 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreSpec", aicoreSpec); | ||
| 189 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 190 | + parseHolder.GetContext<gert::TilingParseContext>()->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", | ||
| 191 | + intrinsics); | ||
| 192 | + ASSERT_EQ(tilingParseFunc(parseHolder.GetContext<gert::KernelContext>()), ge::GRAPH_SUCCESS); | ||
| 193 | + | ||
| 194 | + gert::StorageShape x0Shape = MakeStorageShape({4, 4}); | ||
| 195 | + gert::StorageShape x1Shape = MakeStorageShape({2, 8}); | ||
| 196 | + gert::StorageFormat storageFormat(ge::FORMAT_ND, ge::FORMAT_ND, gert::ExpandDimsType()); | ||
| 197 | + gert::TensorV2 x0(x0Shape, storageFormat, gert::TensorPlacement::kOnHost, ge::DT_FLOAT, nullptr); | ||
| 198 | + gert::TensorV2 x1(x1Shape, storageFormat, gert::TensorPlacement::kOnHost, ge::DT_BF16, nullptr); | ||
| 199 | + std::vector<gert::TensorV2*> inputTensors = {&x0, &x1}; | ||
| 200 | + | ||
| 201 | + std::vector<gert::StorageShape> yStorages; | ||
| 202 | + yStorages.emplace_back(MakeStorageShape({4, 4})); | ||
| 203 | + yStorages.emplace_back(MakeStorageShape({2, 8})); | ||
| 204 | + std::vector<gert::StorageShape*> yShapePtrs; | ||
| 205 | + for (auto& shape : yStorages) { | ||
| 206 | + yShapePtrs.emplace_back(&shape); | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + auto param = gert::TilingData::CreateCap(4096); | ||
| 210 | + ASSERT_NE(param, nullptr); | ||
| 211 | + auto workspaceSizeHolder = gert::ContinuousVector::Create<size_t>(4096); | ||
| 212 | + auto wsSize = reinterpret_cast<gert::ContinuousVector*>(workspaceSizeHolder.get()); | ||
| 213 | + gert::TilingContextFaker tilingFaker; | ||
| 214 | + tilingFaker.NodeIoNum(1, 1) | ||
| 215 | + .IrInstanceNum({2}, {2}) | ||
| 216 | + .InputTensors(inputTensors) | ||
| 217 | + .OutputShapes(yShapePtrs) | ||
| 218 | + .CompileInfo(&compileInfo) | ||
| 219 | + .PlatformInfo(reinterpret_cast<char*>(&platformInfo)) | ||
| 220 | + .TilingData(param.get()) | ||
| 221 | + .Workspace(wsSize); | ||
| 222 | + tilingFaker.NodeOutputTd(0, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND); | ||
| 223 | + tilingFaker.NodeOutputTd(1, ge::DT_FLOAT, ge::FORMAT_ND, ge::FORMAT_ND); | ||
| 224 | + auto holder = tilingFaker.Build(); | ||
| 225 | + auto* tilingContext = holder.GetContext<gert::TilingContext>(); | ||
| 226 | + tilingContext->GetPlatformInfo()->SetPlatformRes("SoCInfo", socInfos); | ||
| 227 | + tilingContext->GetPlatformInfo()->SetPlatformRes("AICoreSpec", aicoreSpec); | ||
| 228 | + tilingContext->GetPlatformInfo()->SetCoreNumByCoreType("AICore"); | ||
| 229 | + tilingContext->GetPlatformInfo()->SetPlatformRes("AICoreintrinsicDtypeMap", intrinsics); | ||
| 230 | + | ||
| 231 | + EXPECT_EQ(tilingFunc(tilingContext), ge::GRAPH_FAILED); | ||
| 232 | +} | ||
| 233 | + | ||
| 234 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_unsupported_dtype_rejected) | ||
| 235 | +{ | ||
| 236 | + RunTilingCase({{4, 4}}, {{4, 4}}, ge::DT_INT32, {ge::DT_INT32}, false); | ||
| 237 | +} | ||
| 238 | + | ||
| 239 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_negative_shape_size_rejected) | ||
| 240 | +{ | ||
| 241 | + RunTilingCase({{4611686018427387904LL, 2}}, {{4611686018427387904LL, 2}}, ge::DT_FLOAT, {ge::DT_FLOAT}, false); | ||
| 242 | +} | ||
| 243 | + | ||
| 244 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_tensor_num_over_256_rejected) | ||
| 245 | +{ | ||
| 246 | + RunTilingCase(std::vector<std::vector<int64_t>>(257U, {1}), {{1}}, ge::DT_FLOAT, {ge::DT_FLOAT}, false); | ||
| 247 | +} | ||
| 248 | + | ||
| 249 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_output_num_mismatch_rejected) | ||
| 250 | +{ | ||
| 251 | + RunTilingCase({{4, 4}, {2, 8}}, {{4, 4}}, ge::DT_FLOAT, {ge::DT_FLOAT}, false); | ||
| 252 | +} | ||
| 253 | + | ||
| 254 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_output_dtype_mismatch_rejected) | ||
| 255 | +{ | ||
| 256 | + RunTilingCase({{4, 4}}, {{4, 4}}, ge::DT_FLOAT, {ge::DT_BF16}, false); | ||
| 257 | +} | ||
| 258 | + | ||
| 259 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_output_shape_size_smaller_rejected) | ||
| 260 | +{ | ||
| 261 | + RunTilingCase({{4, 4}}, {{2, 4}}, ge::DT_FLOAT, {ge::DT_FLOAT}, false); | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_output_shape_size_larger_accepted) | ||
| 265 | +{ | ||
| 266 | + RunTilingCase({{4, 4}}, {{4, 8}}, ge::DT_FLOAT, {ge::DT_FLOAT}, true, | ||
| 267 | + GET_TPL_TILING_KEY(FOREACH_ERFC_TPL_SCH_MODE_FP32)); | ||
| 268 | +} | ||
| 269 | + | ||
| 270 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_output_same_size_diff_dims_accepted) | ||
| 271 | +{ | ||
| 272 | + RunTilingCase({{4, 4}}, {{2, 8}}, ge::DT_FLOAT, {ge::DT_FLOAT}, true, | ||
| 273 | + GET_TPL_TILING_KEY(FOREACH_ERFC_TPL_SCH_MODE_FP32)); | ||
| 274 | +} | ||
| 275 | + | ||
| 276 | +TEST_F(ForeachErfcTilingTest, foreach_erfc_dim_over_8_rejected) | ||
| 277 | +{ | ||
| 278 | + RunTilingCase({{1, 1, 1, 1, 1, 1, 1, 1, 1}}, {{1, 1, 1, 1, 1, 1, 1, 1, 1}}, ge::DT_FLOAT, {ge::DT_FLOAT}, false); | ||
| 279 | +} | ||
| @@ -42,3 +42,26 @@ TEST_F(l2_foreach_erfc_test, ascend910B2_foreach_erfc_test_non_contiguous) | |||
| 42 | aclnnStatus getWorkspaceResult = ut.TestGetWorkspaceSize(&workspaceSize); | 42 | aclnnStatus getWorkspaceResult = ut.TestGetWorkspaceSize(&workspaceSize); |
| 43 | EXPECT_EQ(getWorkspaceResult, ACL_SUCCESS); | 43 | EXPECT_EQ(getWorkspaceResult, ACL_SUCCESS); |
| 44 | } | 44 | } |
| 45 | + | ||
| 46 | +// OP_CHECK_COMM_INPUT_workspaceSize_nullptr | ||
| 47 | +TEST_F(l2_foreach_erfc_test, ascend910B2_foreach_erfc_test_workspace_size_nullptr) | ||
| 48 | +{ | ||
| 49 | + vector<int64_t> selfDims = {2, 4}; | ||
| 50 | + vector<int64_t> outDims = {2, 4}; | ||
| 51 | + auto x = TensorDesc(selfDims, ACL_FLOAT, ACL_FORMAT_ND).ValueRange(-1, 1); | ||
| 52 | + auto out = TensorDesc(outDims, ACL_FLOAT, ACL_FORMAT_ND).Precision(0.001, 0.001); | ||
| 53 | + auto xList = TensorListDesc({x}); | ||
| 54 | + auto outList = TensorListDesc({out}); | ||
| 55 | + | ||
| 56 | + auto ut = OP_API_UT(aclnnForeachErfc, INPUT(xList), OUTPUT(outList)); | ||
| 57 | + aclnnStatus getWorkspaceResult = ut.TestGetWorkspaceSize(nullptr); | ||
| 58 | + EXPECT_EQ(getWorkspaceResult, ACLNN_ERR_PARAM_NULLPTR); | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +// OP_CHECK_COMM_INPUT_executor_nullptr | ||
| 62 | +TEST_F(l2_foreach_erfc_test, ascend910B2_foreach_erfc_test_executor_nullptr) | ||
| 63 | +{ | ||
| 64 | + uint64_t workspaceSize = 0; | ||
| 65 | + aclnnStatus getWorkspaceResult = aclnnForeachErfcGetWorkspaceSize(nullptr, nullptr, &workspaceSize, nullptr); | ||
| 66 | + EXPECT_EQ(getWorkspaceResult, ACLNN_ERR_PARAM_NULLPTR); | ||
| 67 | +} | ||


后半句在参数说明中已有,这里不需要重复体现了吧