已合并
AddRmsNormDynamicMxQuant算子example整改以及md修改 #3119
duchaune创建于 3月24日
AddRmsNormDynamicMxQuant算子example整改以及md修改 #3119
已合并
共 2 个文件变更+103-328
| @@ -309,11 +309,26 @@ aclnnStatus aclnnAddRmsNormDynamicMxQuant( | |||
| 309 | <td>输入或输出的数据类型不在支持的范围之内。</td> | 309 | <td>输入或输出的数据类型不在支持的范围之内。</td> |
| 310 | </tr> | 310 | </tr> |
| 311 | <tr> | 311 | <tr> |
| 312 | - <td rowspan="2">ACLNN_ERR_INNER_TILING_ERROR</td> | 312 | + <td rowspan="9">ACLNN_ERR_INNER_TILING_ERROR</td> |
| 313 | - <td rowspan="2">561002</td> | 313 | + <td rowspan="9">561002</td> |
| 314 | </tr> | 314 | </tr> |
| 315 | <tr> | 315 | <tr> |
| 316 | - <td>输入和输出。</td> | 316 | + <td>scaleAlg不是0或1,roundModeOptional(非空时)不是 {rint, floor, round}。</td> |
| 317 | + </tr> | ||
| 318 | + <tr> | ||
| 319 | + <td>dstType为 fp8 时,roundModeOptional不是 rint。</td> | ||
| 320 | + </tr> | ||
| 321 | + <tr> | ||
| 322 | + <td>输入x1、输出yOut的shape不是完全相同的shape。</td> | ||
| 323 | + </tr> | ||
| 324 | + <tr> | ||
| 325 | + <td>mxscaleOut的维度数不等于输入x1的维度数+1。</td> | ||
| 326 | + </tr> | ||
| 327 | + <tr> | ||
| 328 | + <td>gamma、beta(若存在)的shape不是完全相同的shape,或者类型不相同。</td> | ||
| 329 | + </tr> | ||
| 330 | + <tr> | ||
| 331 | + <td>gamma的维度和x1的需要作norm的维度不相同,或rstdOut的维度和x1的不需要norm的维度不相同,或x1的需要norm的维度数不为1。</td> | ||
| 317 | </tr> | 332 | </tr> |
| 318 | </tbody></table> | 333 | </tbody></table> |
| 319 | 334 | ||
| @@ -12,6 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | + | ||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | 18 | ||
| @@ -109,23 +110,20 @@ int CreateAclTensor( | |||
| 109 | 110 | ||
| 110 | int main() | 111 | int main() |
| 111 | { | 112 | { |
| 112 | - // 1. (固定写法)device/stream初始化,参考acl API手册 | 113 | + // 1. (固定写法)device/stream初始化 |
| 113 | - // 根据自己的实际device填写deviceId | ||
| 114 | int32_t deviceId = 0; | 114 | int32_t deviceId = 0; |
| 115 | aclrtStream stream; | 115 | aclrtStream stream; |
| 116 | auto ret = Init(deviceId, &stream); | 116 | auto ret = Init(deviceId, &stream); |
| 117 | - // check根据自己的需要处理 | ||
| 118 | CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); | 117 | CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret); |
| 119 | 118 | ||
| 120 | // 检查硬件支持 | 119 | // 检查硬件支持 |
| 121 | if (!CheckHardwareSupport()) { | 120 | if (!CheckHardwareSupport()) { |
| 122 | LOG_PRINT("\n=== Test SKIPPED (hardware not supported) ===\n"); | 121 | LOG_PRINT("\n=== Test SKIPPED (hardware not supported) ===\n"); |
| 123 | Finalize(deviceId, stream); | 122 | Finalize(deviceId, stream); |
| 124 | - return 0; // 返回0表示测试通过(被跳过) | 123 | + return 0; |
| 125 | } | 124 | } |
| 126 | 125 | ||
| 127 | - // 2. 构造输入与输出,需要根据API的接口自定义构造 | 126 | + // 2. 构造输入与输出 |
| 128 | - // 注意:当前算子仅支持输出FP8类型(E4M3FN或E5M2) | ||
| 129 | std::vector<int64_t> x1_shape = {2, 32}; | 127 | std::vector<int64_t> x1_shape = {2, 32}; |
| 130 | std::vector<int64_t> x2_shape = {2, 32}; | 128 | std::vector<int64_t> x2_shape = {2, 32}; |
| 131 | std::vector<int64_t> gamma_shape = {32}; | 129 | std::vector<int64_t> gamma_shape = {32}; |
| @@ -133,8 +131,8 @@ int main() | |||
| 133 | std::vector<int64_t> x_out_shape = {2, 32}; | 131 | std::vector<int64_t> x_out_shape = {2, 32}; |
| 134 | 132 | ||
| 135 | // MX scale shape: [H, numBlocks, 2] where numBlocks = ceil(N/32) | 133 | // MX scale shape: [H, numBlocks, 2] where numBlocks = ceil(N/32) |
| 136 | - int64_t mx_block_num = (32 + 31) / 32; // = 1 | 134 | + int64_t mx_block_num = (32 + 31) / 32; |
| 137 | - int64_t mxscale_dim = (mx_block_num + 1) / 2; // = 1 | 135 | + int64_t mxscale_dim = (mx_block_num + 1) / 2; |
| 138 | std::vector<int64_t> mxscale_out_shape = {2, mxscale_dim, 2}; | 136 | std::vector<int64_t> mxscale_out_shape = {2, mxscale_dim, 2}; |
| 139 | std::vector<int64_t> rstd_out_shape = {2, 1}; | 137 | std::vector<int64_t> rstd_out_shape = {2, 1}; |
| 140 | 138 | ||
| @@ -166,9 +164,9 @@ int main() | |||
| 166 | std::vector<float> rstd_out_host_data(GetShapeSize(rstd_out_shape), 0.0f); | 164 | std::vector<float> rstd_out_host_data(GetShapeSize(rstd_out_shape), 0.0f); |
| 167 | 165 | ||
| 168 | double epsilon = 1e-6; | 166 | double epsilon = 1e-6; |
| 169 | - int64_t quant_alg = 0; // OCP algorithm | 167 | + int64_t quant_alg = 0; |
| 170 | char* round_mode_optional = const_cast<char*>("rint"); | 168 | char* round_mode_optional = const_cast<char*>("rint"); |
| 171 | - int64_t dst_type = GE_DT_FLOAT8_E4M3FN; // FP8 E4M3FN output | 169 | + int64_t dst_type = GE_DT_FLOAT8_E4M3FN; |
| 172 | bool output_rstd = true; | 170 | bool output_rstd = true; |
| 173 | 171 | ||
| 174 | LOG_PRINT("Input shape: [2, 32], Total elements: %ld\n", (int64_t)64); | 172 | LOG_PRINT("Input shape: [2, 32], Total elements: %ld\n", (int64_t)64); |
| @@ -178,260 +176,105 @@ int main() | |||
| 178 | 176 | ||
| 179 | // 创建x1 aclTensor (FP16输入) | 177 | // 创建x1 aclTensor (FP16输入) |
| 180 | ret = CreateAclTensor(x1_host_data, x1_shape, &x1_device_addr, aclDataType::ACL_FLOAT16, &x1); | 178 | ret = CreateAclTensor(x1_host_data, x1_shape, &x1_device_addr, aclDataType::ACL_FLOAT16, &x1); |
| 181 | - if (ret != ACL_SUCCESS) { | 179 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> x1TensorPtr(x1, aclDestroyTensor); |
| 182 | - LOG_PRINT("CreateAclTensor x1 failed. ERROR: %d\n", ret); | 180 | + std::unique_ptr<void, aclError (*)(void*)> x1DeviceAddrPtr(x1_device_addr, aclrtFree); |
| 183 | - aclrtDestroyStream(stream); | 181 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor x1 failed. ERROR: %d\n", ret); |
| 184 | - aclrtResetDevice(deviceId); | 182 | + Finalize(deviceId, stream); return ret); |
| 185 | - aclFinalize(); | ||
| 186 | - return ret; | ||
| 187 | - } | ||
| 188 | 183 | ||
| 189 | // 创建x2 aclTensor (FP16输入) | 184 | // 创建x2 aclTensor (FP16输入) |
| 190 | ret = CreateAclTensor(x2_host_data, x2_shape, &x2_device_addr, aclDataType::ACL_FLOAT16, &x2); | 185 | ret = CreateAclTensor(x2_host_data, x2_shape, &x2_device_addr, aclDataType::ACL_FLOAT16, &x2); |
| 191 | - if (ret != ACL_SUCCESS) { | 186 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> x2TensorPtr(x2, aclDestroyTensor); |
| 192 | - LOG_PRINT("CreateAclTensor x2 failed. ERROR: %d\n", ret); | 187 | + std::unique_ptr<void, aclError (*)(void*)> x2DeviceAddrPtr(x2_device_addr, aclrtFree); |
| 193 | - aclDestroyTensor(x1); | 188 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor x2 failed. ERROR: %d\n", ret); |
| 194 | - aclrtFree(x1_device_addr); | 189 | + Finalize(deviceId, stream); return ret); |
| 195 | - aclrtDestroyStream(stream); | ||
| 196 | - aclrtResetDevice(deviceId); | ||
| 197 | - aclFinalize(); | ||
| 198 | - return ret; | ||
| 199 | - } | ||
| 200 | 190 | ||
| 201 | // 创建gamma aclTensor (FP16输入) | 191 | // 创建gamma aclTensor (FP16输入) |
| 202 | ret = CreateAclTensor(gamma_host_data, gamma_shape, &gamma_device_addr, aclDataType::ACL_FLOAT16, &gamma); | 192 | ret = CreateAclTensor(gamma_host_data, gamma_shape, &gamma_device_addr, aclDataType::ACL_FLOAT16, &gamma); |
| 203 | - if (ret != ACL_SUCCESS) { | 193 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> gammaTensorPtr(gamma, aclDestroyTensor); |
| 204 | - LOG_PRINT("CreateAclTensor gamma failed. ERROR: %d\n", ret); | 194 | + std::unique_ptr<void, aclError (*)(void*)> gammaDeviceAddrPtr(gamma_device_addr, aclrtFree); |
| 205 | - aclDestroyTensor(x1); | 195 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor gamma failed. ERROR: %d\n", ret); |
| 206 | - aclDestroyTensor(x2); | 196 | + Finalize(deviceId, stream); return ret); |
| 207 | - aclrtFree(x1_device_addr); | ||
| 208 | - aclrtFree(x2_device_addr); | ||
| 209 | - aclrtDestroyStream(stream); | ||
| 210 | - aclrtResetDevice(deviceId); | ||
| 211 | - aclFinalize(); | ||
| 212 | - return ret; | ||
| 213 | - } | ||
| 214 | 197 | ||
| 215 | // 创建y_out aclTensor (FP8 E4M3FN输出) | 198 | // 创建y_out aclTensor (FP8 E4M3FN输出) |
| 216 | - ret = CreateAclTensor(y_out_host_data, y_out_shape, &y_out_device_addr, | 199 | + ret = CreateAclTensor(y_out_host_data, y_out_shape, &y_out_device_addr, |
| 217 | - aclDataType::ACL_FLOAT8_E4M3FN, &y_out); | 200 | + aclDataType::ACL_FLOAT8_E4M3FN, &y_out); |
| 218 | - if (ret != ACL_SUCCESS) { | 201 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yOutTensorPtr(y_out, aclDestroyTensor); |
| 219 | - LOG_PRINT("CreateAclTensor y_out failed. ERROR: %d\n", ret); | 202 | + std::unique_ptr<void, aclError (*)(void*)> yOutDeviceAddrPtr(y_out_device_addr, aclrtFree); |
| 220 | - aclDestroyTensor(x1); | 203 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor y_out failed. ERROR: %d\n", ret); |
| 221 | - aclDestroyTensor(x2); | 204 | + Finalize(deviceId, stream); return ret); |
| 222 | - aclDestroyTensor(gamma); | ||
| 223 | - aclrtFree(x1_device_addr); | ||
| 224 | - aclrtFree(x2_device_addr); | ||
| 225 | - aclrtFree(gamma_device_addr); | ||
| 226 | - aclrtDestroyStream(stream); | ||
| 227 | - aclrtResetDevice(deviceId); | ||
| 228 | - aclFinalize(); | ||
| 229 | - return ret; | ||
| 230 | - } | ||
| 231 | 205 | ||
| 232 | // 创建x_out aclTensor (FP16输出) | 206 | // 创建x_out aclTensor (FP16输出) |
| 233 | ret = CreateAclTensor(x_out_host_data, x_out_shape, &x_out_device_addr, aclDataType::ACL_FLOAT16, &x_out); | 207 | ret = CreateAclTensor(x_out_host_data, x_out_shape, &x_out_device_addr, aclDataType::ACL_FLOAT16, &x_out); |
| 234 | - if (ret != ACL_SUCCESS) { | 208 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> xOutTensorPtr(x_out, aclDestroyTensor); |
| 235 | - LOG_PRINT("CreateAclTensor x_out failed. ERROR: %d\n", ret); | 209 | + std::unique_ptr<void, aclError (*)(void*)> xOutDeviceAddrPtr(x_out_device_addr, aclrtFree); |
| 236 | - aclDestroyTensor(x1); | 210 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor x_out failed. ERROR: %d\n", ret); |
| 237 | - aclDestroyTensor(x2); | 211 | + Finalize(deviceId, stream); return ret); |
| 238 | - aclDestroyTensor(gamma); | ||
| 239 | - aclDestroyTensor(y_out); | ||
| 240 | - aclrtFree(x1_device_addr); | ||
| 241 | - aclrtFree(x2_device_addr); | ||
| 242 | - aclrtFree(gamma_device_addr); | ||
| 243 | - aclrtFree(y_out_device_addr); | ||
| 244 | - aclrtDestroyStream(stream); | ||
| 245 | - aclrtResetDevice(deviceId); | ||
| 246 | - aclFinalize(); | ||
| 247 | - return ret; | ||
| 248 | - } | ||
| 249 | 212 | ||
| 250 | // 创建mxscale_out aclTensor (FP8 E8M0输出) | 213 | // 创建mxscale_out aclTensor (FP8 E8M0输出) |
| 251 | - ret = CreateAclTensor(mxscale_out_host_data, mxscale_out_shape, &mxscale_out_device_addr, | 214 | + ret = CreateAclTensor(mxscale_out_host_data, mxscale_out_shape, &mxscale_out_device_addr, |
| 252 | - aclDataType::ACL_FLOAT8_E8M0, &mxscale_out); | 215 | + aclDataType::ACL_FLOAT8_E8M0, &mxscale_out); |
| 253 | - if (ret != ACL_SUCCESS) { | 216 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> mxscaleOutTensorPtr(mxscale_out, aclDestroyTensor); |
| 254 | - LOG_PRINT("CreateAclTensor mxscale_out failed. ERROR: %d\n", ret); | 217 | + std::unique_ptr<void, aclError (*)(void*)> mxscaleOutDeviceAddrPtr(mxscale_out_device_addr, aclrtFree); |
| 255 | - aclDestroyTensor(x1); | 218 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor mxscale_out failed. ERROR: %d\n", ret); |
| 256 | - aclDestroyTensor(x2); | 219 | + Finalize(deviceId, stream); return ret); |
| 257 | - aclDestroyTensor(gamma); | ||
| 258 | - aclDestroyTensor(y_out); | ||
| 259 | - aclDestroyTensor(x_out); | ||
| 260 | - aclrtFree(x1_device_addr); | ||
| 261 | - aclrtFree(x2_device_addr); | ||
| 262 | - aclrtFree(gamma_device_addr); | ||
| 263 | - aclrtFree(y_out_device_addr); | ||
| 264 | - aclrtFree(x_out_device_addr); | ||
| 265 | - aclrtDestroyStream(stream); | ||
| 266 | - aclrtResetDevice(deviceId); | ||
| 267 | - aclFinalize(); | ||
| 268 | - return ret; | ||
| 269 | - } | ||
| 270 | 220 | ||
| 271 | // 创建rstd_out aclTensor (float输出) | 221 | // 创建rstd_out aclTensor (float输出) |
| 272 | - ret = CreateAclTensor(rstd_out_host_data, rstd_out_shape, &rstd_out_device_addr, | 222 | + ret = CreateAclTensor(rstd_out_host_data, rstd_out_shape, &rstd_out_device_addr, |
| 273 | - aclDataType::ACL_FLOAT, &rstd_out); | 223 | + aclDataType::ACL_FLOAT, &rstd_out); |
| 274 | - if (ret != ACL_SUCCESS) { | 224 | + std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> rstdOutTensorPtr(rstd_out, aclDestroyTensor); |
| 275 | - LOG_PRINT("CreateAclTensor rstd_out failed. ERROR: %d\n", ret); | 225 | + std::unique_ptr<void, aclError (*)(void*)> rstdOutDeviceAddrPtr(rstd_out_device_addr, aclrtFree); |
| 276 | - aclDestroyTensor(x1); | 226 | + CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("CreateAclTensor rstd_out failed. ERROR: %d\n", ret); |
| 277 | - aclDestroyTensor(x2); | 227 | + Finalize(deviceId, stream); return ret); |
| 278 | - aclDestroyTensor(gamma); | ||
| 279 | - aclDestroyTensor(y_out); | ||
| 280 | - aclDestroyTensor(x_out); | ||
| 281 | - aclDestroyTensor(mxscale_out); | ||
| 282 | - aclrtFree(x1_device_addr); | ||
| 283 | - aclrtFree(x2_device_addr); | ||
| 284 | - aclrtFree(gamma_device_addr); | ||
| 285 | - aclrtFree(y_out_device_addr); | ||
| 286 | - aclrtFree(x_out_device_addr); | ||
| 287 | - aclrtFree(mxscale_out_device_addr); | ||
| 288 | - aclrtDestroyStream(stream); | ||
| 289 | - aclrtResetDevice(deviceId); | ||
| 290 | - aclFinalize(); | ||
| 291 | - return ret; | ||
| 292 | - } | ||
| 293 | 228 | ||
| 294 | - // 3. 调用CANN算子库API,需要修改为具体的API | 229 | + // 3. 调用CANN算子库API |
| 295 | uint64_t workspace_size = 0; | 230 | uint64_t workspace_size = 0; |
| 296 | aclOpExecutor* executor = nullptr; | 231 | aclOpExecutor* executor = nullptr; |
| 297 | 232 | ||
| 298 | // 调用aclnnAddRmsNormDynamicMxQuant第一段接口 | 233 | // 调用aclnnAddRmsNormDynamicMxQuant第一段接口 |
| 299 | LOG_PRINT("Calling aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize...\n"); | 234 | LOG_PRINT("Calling aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize...\n"); |
| 300 | ret = aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize( | 235 | ret = aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize( |
| 301 | - x1, x2, gamma, nullptr, epsilon, quant_alg, round_mode_optional, dst_type, output_rstd, | 236 | + x1, x2, gamma, nullptr, epsilon, quant_alg, round_mode_optional, dst_type, output_rstd, |
| 302 | y_out, x_out, mxscale_out, rstd_out, &workspace_size, &executor); | 237 | y_out, x_out, mxscale_out, rstd_out, &workspace_size, &executor); |
| 303 | 238 | ||
| 304 | - if (ret != ACL_SUCCESS) { | 239 | + CHECK_RET(ret == ACL_SUCCESS, |
| 305 | - LOG_PRINT("aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize failed. ERROR: %d\n", ret); | 240 | + LOG_PRINT("aclnnAddRmsNormDynamicMxQuantGetWorkspaceSize failed. ERROR: %d\n", ret); |
| 306 | - aclDestroyTensor(x1); | 241 | + Finalize(deviceId, stream); return ret); |
| 307 | - aclDestroyTensor(x2); | ||
| 308 | - aclDestroyTensor(gamma); | ||
| 309 | - aclDestroyTensor(y_out); | ||
| 310 | - aclDestroyTensor(x_out); | ||
| 311 | - aclDestroyTensor(mxscale_out); | ||
| 312 | - aclDestroyTensor(rstd_out); | ||
| 313 | - aclrtFree(x1_device_addr); | ||
| 314 | - aclrtFree(x2_device_addr); | ||
| 315 | - aclrtFree(gamma_device_addr); | ||
| 316 | - aclrtFree(y_out_device_addr); | ||
| 317 | - aclrtFree(x_out_device_addr); | ||
| 318 | - aclrtFree(mxscale_out_device_addr); | ||
| 319 | - aclrtFree(rstd_out_device_addr); | ||
| 320 | - aclrtDestroyStream(stream); | ||
| 321 | - aclrtResetDevice(deviceId); | ||
| 322 | - aclFinalize(); | ||
| 323 | - return ret; | ||
| 324 | - } | ||
| 325 | 242 | ||
| 326 | LOG_PRINT("Workspace size: %lu bytes (%.2f KB)\n", workspace_size, workspace_size / 1024.0); | 243 | LOG_PRINT("Workspace size: %lu bytes (%.2f KB)\n", workspace_size, workspace_size / 1024.0); |
| 327 | 244 | ||
| 328 | // 根据第一段接口计算出的workspaceSize申请device内存 | 245 | // 根据第一段接口计算出的workspaceSize申请device内存 |
| 246 | + std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree); | ||
| 329 | if (workspace_size > 0) { | 247 | if (workspace_size > 0) { |
| 330 | ret = aclrtMalloc(&workspace_addr, workspace_size, ACL_MEM_MALLOC_HUGE_FIRST); | 248 | ret = aclrtMalloc(&workspace_addr, workspace_size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 331 | - if (ret != ACL_SUCCESS) { | 249 | + CHECK_RET(ret == ACL_SUCCESS, |
| 332 | - LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); | 250 | + LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); |
| 333 | - aclDestroyTensor(x1); | 251 | + Finalize(deviceId, stream); return ret); |
| 334 | - aclDestroyTensor(x2); | 252 | + workspaceAddrPtr.reset(workspace_addr); |
| 335 | - aclDestroyTensor(gamma); | ||
| 336 | - aclDestroyTensor(y_out); | ||
| 337 | - aclDestroyTensor(x_out); | ||
| 338 | - aclDestroyTensor(mxscale_out); | ||
| 339 | - aclDestroyTensor(rstd_out); | ||
| 340 | - aclrtFree(x1_device_addr); | ||
| 341 | - aclrtFree(x2_device_addr); | ||
| 342 | - aclrtFree(gamma_device_addr); | ||
| 343 | - aclrtFree(y_out_device_addr); | ||
| 344 | - aclrtFree(x_out_device_addr); | ||
| 345 | - aclrtFree(mxscale_out_device_addr); | ||
| 346 | - aclrtFree(rstd_out_device_addr); | ||
| 347 | - aclrtDestroyStream(stream); | ||
| 348 | - aclrtResetDevice(deviceId); | ||
| 349 | - aclFinalize(); | ||
| 350 | - return ret; | ||
| 351 | - } | ||
| 352 | } | 253 | } |
| 353 | 254 | ||
| 354 | // 调用aclnnAddRmsNormDynamicMxQuant第二段接口 | 255 | // 调用aclnnAddRmsNormDynamicMxQuant第二段接口 |
| 355 | LOG_PRINT("Calling aclnnAddRmsNormDynamicMxQuant...\n"); | 256 | LOG_PRINT("Calling aclnnAddRmsNormDynamicMxQuant...\n"); |
| 356 | - ret = aclnnAddRmsNormDynamicMxQuant(workspace_addr, workspace_size, executor, stream); | 257 | + ret = aclnnAddRmsNormDynamicMxQuant(workspaceAddrPtr.get(), workspace_size, executor, stream); |
| 357 | - if (ret != ACL_SUCCESS) { | 258 | + CHECK_RET(ret == ACL_SUCCESS, |
| 358 | - LOG_PRINT("aclnnAddRmsNormDynamicMxQuant failed. ERROR: %d\n", ret); | 259 | + LOG_PRINT("aclnnAddRmsNormDynamicMxQuant failed. ERROR: %d\n", ret); |
| 359 | - if (workspace_addr) aclrtFree(workspace_addr); | 260 | + Finalize(deviceId, stream); return ret); |
| 360 | - aclDestroyTensor(x1); | ||
| 361 | - aclDestroyTensor(x2); | ||
| 362 | - aclDestroyTensor(gamma); | ||
| 363 | - aclDestroyTensor(y_out); | ||
| 364 | - aclDestroyTensor(x_out); | ||
| 365 | - aclDestroyTensor(mxscale_out); | ||
| 366 | - aclDestroyTensor(rstd_out); | ||
| 367 | - aclrtFree(x1_device_addr); | ||
| 368 | - aclrtFree(x2_device_addr); | ||
| 369 | - aclrtFree(gamma_device_addr); | ||
| 370 | - aclrtFree(y_out_device_addr); | ||
| 371 | - aclrtFree(x_out_device_addr); | ||
| 372 | - aclrtFree(mxscale_out_device_addr); | ||
| 373 | - aclrtFree(rstd_out_device_addr); | ||
| 374 | - aclrtDestroyStream(stream); | ||
| 375 | - aclrtResetDevice(deviceId); | ||
| 376 | - aclFinalize(); | ||
| 377 | - return ret; | ||
| 378 | - } | ||
| 379 | 261 | ||
| 380 | - // 4. (固定写法)同步等待任务执行结束 | 262 | + // 4. 同步等待任务执行结束 |
| 381 | ret = aclrtSynchronizeStream(stream); | 263 | ret = aclrtSynchronizeStream(stream); |
| 382 | - if (ret != ACL_SUCCESS) { | 264 | + CHECK_RET(ret == ACL_SUCCESS, |
| 383 | - LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); | 265 | + LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); |
| 384 | - if (workspace_addr) aclrtFree(workspace_addr); | 266 | + Finalize(deviceId, stream); return ret); |
| 385 | - aclDestroyTensor(x1); | ||
| 386 | - aclDestroyTensor(x2); | ||
| 387 | - aclDestroyTensor(gamma); | ||
| 388 | - aclDestroyTensor(y_out); | ||
| 389 | - aclDestroyTensor(x_out); | ||
| 390 | - aclDestroyTensor(mxscale_out); | ||
| 391 | - aclDestroyTensor(rstd_out); | ||
| 392 | - aclrtFree(x1_device_addr); | ||
| 393 | - aclrtFree(x2_device_addr); | ||
| 394 | - aclrtFree(gamma_device_addr); | ||
| 395 | - aclrtFree(y_out_device_addr); | ||
| 396 | - aclrtFree(x_out_device_addr); | ||
| 397 | - aclrtFree(mxscale_out_device_addr); | ||
| 398 | - aclrtFree(rstd_out_device_addr); | ||
| 399 | - aclrtDestroyStream(stream); | ||
| 400 | - aclrtResetDevice(deviceId); | ||
| 401 | - aclFinalize(); | ||
| 402 | - return ret; | ||
| 403 | - } | ||
| 404 | 267 | ||
| 405 | - // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改 | 268 | + // 5. 获取输出的值,将device侧内存上的结果拷贝至host侧 |
| 406 | { | 269 | { |
| 407 | // 拷贝y_out结果 | 270 | // 拷贝y_out结果 |
| 408 | auto size = GetShapeSize(y_out_shape); | 271 | auto size = GetShapeSize(y_out_shape); |
| 409 | std::vector<uint8_t> y_out_result(size, 0); | 272 | std::vector<uint8_t> y_out_result(size, 0); |
| 410 | - ret = aclrtMemcpy( | 273 | + ret = aclrtMemcpy(y_out_result.data(), y_out_result.size() * sizeof(y_out_result[0]), |
| 411 | - y_out_result.data(), y_out_result.size() * sizeof(y_out_result[0]), | 274 | + yOutDeviceAddrPtr.get(), size * sizeof(uint8_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 412 | - y_out_device_addr, size * sizeof(uint8_t), ACL_MEMCPY_DEVICE_TO_HOST); | 275 | + CHECK_RET(ret == ACL_SUCCESS, |
| 413 | - if (ret != ACL_SUCCESS) { | 276 | + LOG_PRINT("copy y_out from device to host failed. ERROR: %d\n", ret); |
| 414 | - LOG_PRINT("copy y_out from device to host failed. ERROR: %d\n", ret); | 277 | + Finalize(deviceId, stream); return ret); |
| 415 | - if (workspace_addr) aclrtFree(workspace_addr); | ||
| 416 | - aclDestroyTensor(x1); | ||
| 417 | - aclDestroyTensor(x2); | ||
| 418 | - aclDestroyTensor(gamma); | ||
| 419 | - aclDestroyTensor(y_out); | ||
| 420 | - aclDestroyTensor(x_out); | ||
| 421 | - aclDestroyTensor(mxscale_out); | ||
| 422 | - aclDestroyTensor(rstd_out); | ||
| 423 | - aclrtFree(x1_device_addr); | ||
| 424 | - aclrtFree(x2_device_addr); | ||
| 425 | - aclrtFree(gamma_device_addr); | ||
| 426 | - aclrtFree(y_out_device_addr); | ||
| 427 | - aclrtFree(x_out_device_addr); | ||
| 428 | - aclrtFree(mxscale_out_device_addr); | ||
| 429 | - aclrtFree(rstd_out_device_addr); | ||
| 430 | - aclrtDestroyStream(stream); | ||
| 431 | - aclrtResetDevice(deviceId); | ||
| 432 | - aclFinalize(); | ||
| 433 | - return ret; | ||
| 434 | - } | ||
| 435 | 278 | ||
| 436 | LOG_PRINT("Output yOut (first 10 values):\n"); | 279 | LOG_PRINT("Output yOut (first 10 values):\n"); |
| 437 | for (int64_t i = 0; i < std::min(size, (int64_t)10); i++) { | 280 | for (int64_t i = 0; i < std::min(size, (int64_t)10); i++) { |
| @@ -441,31 +284,11 @@ int main() | |||
| 441 | // 拷贝x_out结果 | 284 | // 拷贝x_out结果 |
| 442 | size = GetShapeSize(x_out_shape); | 285 | size = GetShapeSize(x_out_shape); |
| 443 | std::vector<uint16_t> x_out_result(size, 0); | 286 | std::vector<uint16_t> x_out_result(size, 0); |
| 444 | - ret = aclrtMemcpy( | 287 | + ret = aclrtMemcpy(x_out_result.data(), x_out_result.size() * sizeof(x_out_result[0]), |
| 445 | - x_out_result.data(), x_out_result.size() * sizeof(x_out_result[0]), | 288 | + xOutDeviceAddrPtr.get(), size * sizeof(uint16_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 446 | - x_out_device_addr, size * sizeof(uint16_t), ACL_MEMCPY_DEVICE_TO_HOST); | 289 | + CHECK_RET(ret == ACL_SUCCESS, |
| 447 | - if (ret != ACL_SUCCESS) { | 290 | + LOG_PRINT("copy x_out from device to host failed. ERROR: %d\n", ret); |
| 448 | - LOG_PRINT("copy x_out from device to host failed. ERROR: %d\n", ret); | 291 | + Finalize(deviceId, stream); return ret); |
| 449 | - if (workspace_addr) aclrtFree(workspace_addr); | ||
| 450 | - aclDestroyTensor(x1); | ||
| 451 | - aclDestroyTensor(x2); | ||
| 452 | - aclDestroyTensor(gamma); | ||
| 453 | - aclDestroyTensor(y_out); | ||
| 454 | - aclDestroyTensor(x_out); | ||
| 455 | - aclDestroyTensor(mxscale_out); | ||
| 456 | - aclDestroyTensor(rstd_out); | ||
| 457 | - aclrtFree(x1_device_addr); | ||
| 458 | - aclrtFree(x2_device_addr); | ||
| 459 | - aclrtFree(gamma_device_addr); | ||
| 460 | - aclrtFree(y_out_device_addr); | ||
| 461 | - aclrtFree(x_out_device_addr); | ||
| 462 | - aclrtFree(mxscale_out_device_addr); | ||
| 463 | - aclrtFree(rstd_out_device_addr); | ||
| 464 | - aclrtDestroyStream(stream); | ||
| 465 | - aclrtResetDevice(deviceId); | ||
| 466 | - aclFinalize(); | ||
| 467 | - return ret; | ||
| 468 | - } | ||
| 469 | 292 | ||
| 470 | LOG_PRINT("Output xOut (first 10 values):\n"); | 293 | LOG_PRINT("Output xOut (first 10 values):\n"); |
| 471 | for (int64_t i = 0; i < std::min(size, (int64_t)10); i++) { | 294 | for (int64_t i = 0; i < std::min(size, (int64_t)10); i++) { |
| @@ -475,31 +298,11 @@ int main() | |||
| 475 | // 拷贝mxscale_out结果 | 298 | // 拷贝mxscale_out结果 |
| 476 | size = GetShapeSize(mxscale_out_shape); | 299 | size = GetShapeSize(mxscale_out_shape); |
| 477 | std::vector<uint8_t> mxscale_out_result(size, 0); | 300 | std::vector<uint8_t> mxscale_out_result(size, 0); |
| 478 | - ret = aclrtMemcpy( | 301 | + ret = aclrtMemcpy(mxscale_out_result.data(), mxscale_out_result.size() * sizeof(mxscale_out_result[0]), |
| 479 | - mxscale_out_result.data(), mxscale_out_result.size() * sizeof(mxscale_out_result[0]), | 302 | + mxscaleOutDeviceAddrPtr.get(), size * sizeof(uint8_t), ACL_MEMCPY_DEVICE_TO_HOST); |
| 480 | - mxscale_out_device_addr, size * sizeof(uint8_t), ACL_MEMCPY_DEVICE_TO_HOST); | 303 | + CHECK_RET(ret == ACL_SUCCESS, |
| 481 | - if (ret != ACL_SUCCESS) { | 304 | + LOG_PRINT("copy mxscale_out from device to host failed. ERROR: %d\n", ret); |
| 482 | - LOG_PRINT("copy mxscale_out from device to host failed. ERROR: %d\n", ret); | 305 | + Finalize(deviceId, stream); return ret); |
| 483 | - if (workspace_addr) aclrtFree(workspace_addr); | ||
| 484 | - aclDestroyTensor(x1); | ||
| 485 | - aclDestroyTensor(x2); | ||
| 486 | - aclDestroyTensor(gamma); | ||
| 487 | - aclDestroyTensor(y_out); | ||
| 488 | - aclDestroyTensor(x_out); | ||
| 489 | - aclDestroyTensor(mxscale_out); | ||
| 490 | - aclDestroyTensor(rstd_out); | ||
| 491 | - aclrtFree(x1_device_addr); | ||
| 492 | - aclrtFree(x2_device_addr); | ||
| 493 | - aclrtFree(gamma_device_addr); | ||
| 494 | - aclrtFree(y_out_device_addr); | ||
| 495 | - aclrtFree(x_out_device_addr); | ||
| 496 | - aclrtFree(mxscale_out_device_addr); | ||
| 497 | - aclrtFree(rstd_out_device_addr); | ||
| 498 | - aclrtDestroyStream(stream); | ||
| 499 | - aclrtResetDevice(deviceId); | ||
| 500 | - aclFinalize(); | ||
| 501 | - return ret; | ||
| 502 | - } | ||
| 503 | 306 | ||
| 504 | LOG_PRINT("MX scale values:\n"); | 307 | LOG_PRINT("MX scale values:\n"); |
| 505 | for (int64_t i = 0; i < size; i++) { | 308 | for (int64_t i = 0; i < size; i++) { |
| @@ -509,31 +312,11 @@ int main() | |||
| 509 | // 拷贝rstd_out结果 | 312 | // 拷贝rstd_out结果 |
| 510 | size = GetShapeSize(rstd_out_shape); | 313 | size = GetShapeSize(rstd_out_shape); |
| 511 | std::vector<float> rstd_out_result(size, 0.0f); | 314 | std::vector<float> rstd_out_result(size, 0.0f); |
| 512 | - ret = aclrtMemcpy( | 315 | + ret = aclrtMemcpy(rstd_out_result.data(), rstd_out_result.size() * sizeof(rstd_out_result[0]), |
| 513 | - rstd_out_result.data(), rstd_out_result.size() * sizeof(rstd_out_result[0]), | 316 | + rstdOutDeviceAddrPtr.get(), size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); |
| 514 | - rstd_out_device_addr, size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST); | 317 | + CHECK_RET(ret == ACL_SUCCESS, |
| 515 | - if (ret != ACL_SUCCESS) { | 318 | + LOG_PRINT("copy rstd_out from device to host failed. ERROR: %d\n", ret); |
| 516 | - LOG_PRINT("copy rstd_out from device to host failed. ERROR: %d\n", ret); | 319 | + Finalize(deviceId, stream); return ret); |
| 517 | - if (workspace_addr) aclrtFree(workspace_addr); | ||
| 518 | - aclDestroyTensor(x1); | ||
| 519 | - aclDestroyTensor(x2); | ||
| 520 | - aclDestroyTensor(gamma); | ||
| 521 | - aclDestroyTensor(y_out); | ||
| 522 | - aclDestroyTensor(x_out); | ||
| 523 | - aclDestroyTensor(mxscale_out); | ||
| 524 | - aclDestroyTensor(rstd_out); | ||
| 525 | - aclrtFree(x1_device_addr); | ||
| 526 | - aclrtFree(x2_device_addr); | ||
| 527 | - aclrtFree(gamma_device_addr); | ||
| 528 | - aclrtFree(y_out_device_addr); | ||
| 529 | - aclrtFree(x_out_device_addr); | ||
| 530 | - aclrtFree(mxscale_out_device_addr); | ||
| 531 | - aclrtFree(rstd_out_device_addr); | ||
| 532 | - aclrtDestroyStream(stream); | ||
| 533 | - aclrtResetDevice(deviceId); | ||
| 534 | - aclFinalize(); | ||
| 535 | - return ret; | ||
| 536 | - } | ||
| 537 | 320 | ||
| 538 | LOG_PRINT("Rstd values:\n"); | 321 | LOG_PRINT("Rstd values:\n"); |
| 539 | for (int64_t i = 0; i < size; i++) { | 322 | for (int64_t i = 0; i < size; i++) { |
| @@ -543,31 +326,8 @@ int main() | |||
| 543 | 326 | ||
| 544 | LOG_PRINT("\n=== AddRmsNormDynamicMxQuant Test PASSED ===\n"); | 327 | LOG_PRINT("\n=== AddRmsNormDynamicMxQuant Test PASSED ===\n"); |
| 545 | 328 | ||
| 546 | - if (workspace_addr) { | 329 | + // 6. 资源自动释放 |
| 547 | - aclrtFree(workspace_addr); | 330 | + Finalize(deviceId, stream); |
| 548 | - } | ||
| 549 | 331 | ||
| 550 | - // 6. 释放aclTensor,需要根据具体API的接口定义修改 | 332 | + return 0; |
| 551 | - aclDestroyTensor(x1); | 333 | +} |
| 552 | - aclDestroyTensor(x2); | ||
| 553 | - aclDestroyTensor(gamma); | ||
| 554 | - aclDestroyTensor(y_out); | ||
| 555 | - aclDestroyTensor(x_out); | ||
| 556 | - aclDestroyTensor(mxscale_out); | ||
| 557 | - aclDestroyTensor(rstd_out); | ||
| 558 | - | ||
| 559 | - // 7. 释放device资源,需要根据具体API的接口定义修改 | ||
| 560 | - aclrtFree(x1_device_addr); | ||
| 561 | - aclrtFree(x2_device_addr); | ||
| 562 | - aclrtFree(gamma_device_addr); | ||
| 563 | - aclrtFree(y_out_device_addr); | ||
| 564 | - aclrtFree(x_out_device_addr); | ||
| 565 | - aclrtFree(mxscale_out_device_addr); | ||
| 566 | - aclrtFree(rstd_out_device_addr); | ||
| 567 | - | ||
| 568 | - aclrtDestroyStream(stream); | ||
| 569 | - aclrtResetDevice(deviceId); | ||
| 570 | - aclFinalize(); | ||
| 571 | - | ||
| 572 | - return (ret == ACL_SUCCESS) ? 0 : ret; | ||
| 573 | -} | ||