Merged
新增simt kernel log样例 #2131
DragonBornHD84Add on May 13
新增simt kernel log样例 #2131
Merged
Merged fromDragonBornHD84/asc-devkitdev1intocann/asc-devkitmaster
Total: 4 files changed+308-1
| @@ -0,0 +1,26 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 10 | + | ||
| 11 | +cmake_minimum_required(VERSION 3.16) | ||
| 12 | + | ||
| 13 | +set(CMAKE_ASC_RUN_MODE "npu" CACHE STRING "Run mode: npu, sim") | ||
| 14 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU architecture: dav-3510") | ||
| 15 | + | ||
| 16 | +find_package(ASC REQUIRED) | ||
| 17 | + | ||
| 18 | +project(kernel_samples LANGUAGES ASC CXX) | ||
| 19 | + | ||
| 20 | +add_executable(demo | ||
| 21 | + log.asc | ||
| 22 | +) | ||
| 23 | + | ||
| 24 | +target_compile_options(demo PRIVATE | ||
| 25 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 26 | +) | ||
| @@ -0,0 +1,148 @@ | |||
| 1 | +# SIMT Kernel日志功能样例 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例演示在SIMT编程模式下,Ascend日志功能的完整使用方式,包括日志打屏输出、日志落盘存储、日志级别控制等功能。通过配置环境变量,开发者可以灵活控制日志输出行为,辅助算子开发与问题定位。 | ||
| 6 | + | ||
| 7 | +更多日志功能详情请参考:[Ascend 日志功能参考](https://hiascend.com/document/redirect/CannCommunitylogref) | ||
| 8 | + | ||
| 9 | +## 支持的产品 | ||
| 10 | + | ||
| 11 | +- Ascend 950PR/Ascend 950DT | ||
| 12 | + | ||
| 13 | +## 支持的CANN软件版本 | ||
| 14 | + | ||
| 15 | +- \>= CANN 9.1.0 | ||
| 16 | + | ||
| 17 | +## 目录结构介绍 | ||
| 18 | + | ||
| 19 | +``` | ||
| 20 | +├── 09_log | ||
| 21 | +│ ├── CMakeLists.txt // cmake编译文件 | ||
| 22 | +│ ├── log.asc // SIMT Kernel示例代码 | ||
| 23 | +| └── README.md | ||
| 24 | +``` | ||
| 25 | + | ||
| 26 | +## 样例描述 | ||
| 27 | + | ||
| 28 | +- 日志功能说明: | ||
| 29 | + - **日志打屏**:通过环境变量控制日志是否输出到标准输出(屏幕),便于实时查看 | ||
| 30 | + - **日志落盘**:通过环境变量指定日志文件存储路径,便于后续分析 | ||
| 31 | + - **日志级别**:通过环境变量控制日志输出级别,按需过滤日志内容 | ||
| 32 | + | ||
| 33 | + 日志级别说明: | ||
| 34 | + | ||
| 35 | + | 值 | 级别 | 说明 | | ||
| 36 | + |----|------|------| | ||
| 37 | + | 0 | DEBUG | 输出所有日志(DEBUG/INFO/WARNING/ERROR),信息最详细 | | ||
| 38 | + | 1 | INFO | 输出INFO及以上级别日志(INFO/WARNING/ERROR) | | ||
| 39 | + | 2 | WARNING | 输出WARNING及以上级别日志(WARNING/ERROR) | | ||
| 40 | + | 3 | ERROR | 仅输出ERROR级别日志 | | ||
| 41 | + | 4 | NULL | 不输出日志 | | ||
| 42 | + | ||
| 43 | +- 样例实现: | ||
| 44 | + 本样例通过构造异常场景,演示Kernel侧异常抛出与Host侧错误捕获的完整流程: | ||
| 45 | + | ||
| 46 | + - Kernel实现 | ||
| 47 | + 在Kernel函数中使用标准C `assert` 断言,当条件不满足时触发设备侧异常。为避免所有线程重复打印,通常仅由 `thread 0` 触发断言。 | ||
| 48 | + | ||
| 49 | + - Host实现 | ||
| 50 | + Kernel执行后,Host侧通过 `ASCENDC_CHECK` 宏捕获并打印错误信息。`ASCENDC_CHECK` 是用于捕获并打印ACL错误的工具宏,定义如下: | ||
| 51 | + | ||
| 52 | + ```c | ||
| 53 | + #define ASCENDC_CHECK(expr) do { \ | ||
| 54 | + aclError ret = (expr); \ | ||
| 55 | + if (ret != ACL_SUCCESS) { \ | ||
| 56 | + fprintf(stderr, "Ascend Error: %s:%d code=%d %s\n", \ | ||
| 57 | + __FILE__, __LINE__, ret, aclGetRecentErrMsg()); \ | ||
| 58 | + } \ | ||
| 59 | + } while(0) | ||
| 60 | + ``` | ||
| 61 | + | ||
| 62 | + 将ACL接口调用作为参数传入,宏会自动检查返回值并在失败时打印错误信息。 | ||
| 63 | + | ||
| 64 | + ```c | ||
| 65 | + // 捕获Kernel执行错误(包括assert触发的错误) | ||
| 66 | + ASCENDC_CHECK(aclrtGetLastError(ACL_RT_THREAD_LEVEL)); | ||
| 67 | + ASCENDC_CHECK(aclrtSynchronizeDevice()); | ||
| 68 | + ``` | ||
| 69 | + | ||
| 70 | +## 编译运行 | ||
| 71 | + | ||
| 72 | +在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 73 | + | ||
| 74 | +- 配置环境变量 | ||
| 75 | + 请根据当前环境上CANN开发套件包的[安装方式](../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 76 | + - 默认路径,root用户安装CANN软件包 | ||
| 77 | + ```bash | ||
| 78 | + source /usr/local/Ascend/cann/set_env.sh | ||
| 79 | + ``` | ||
| 80 | + | ||
| 81 | + - 默认路径,非root用户安装CANN软件包 | ||
| 82 | + ```bash | ||
| 83 | + source $HOME/Ascend/cann/set_env.sh | ||
| 84 | + ``` | ||
| 85 | + | ||
| 86 | + - 指定路径install_path,安装CANN软件包 | ||
| 87 | + ```bash | ||
| 88 | + source ${install_path}/cann/set_env.sh | ||
| 89 | + ``` | ||
| 90 | + | ||
| 91 | +- 配置日志环境变量(仅NPU模式) | ||
| 92 | + 本样例支持通过环境变量控制日志输出行为(以下环境变量仅在NPU模式下生效): | ||
| 93 | + ```bash | ||
| 94 | + export ASCEND_PROCESS_LOG_PATH=./log // 日志落盘路径 | ||
| 95 | + export ASCEND_SLOG_PRINT_TO_STDOUT=1 // 控制日志是否打印到屏幕(1:开启,0:关闭) | ||
| 96 | + export ASCEND_GLOBAL_LOG_LEVEL=1 // 控制日志级别 | ||
| 97 | + ``` | ||
| 98 | + | ||
| 99 | + > 注意: | ||
| 100 | + > - 日志环境变量控制Ascend内部库(RUNTIME/ASCENDCL等)的日志输出。Kernel内部的`printf`输出不受这些环境变量影响。 | ||
| 101 | + > - 日志打屏和落盘功能互斥。开启打屏(`ASCEND_SLOG_PRINT_TO_STDOUT=1`)后,即使配置了 `ASCEND_PROCESS_LOG_PATH` 也不会落盘。 | ||
| 102 | + > - 打屏模式下,可通过 shell 重定向保存日志:`./demo > err.log 2>&1` | ||
| 103 | + > - 落盘模式下,需关闭打屏并配置 `ASCEND_PROCESS_LOG_PATH`,日志将写入指定目录 | ||
| 104 | + | ||
| 105 | +- 样例执行 | ||
| 106 | + ```bash | ||
| 107 | + mkdir -p build && cd build; | ||
| 108 | + cmake -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # 编译工程(默认npu模式) | ||
| 109 | + ./demo | ||
| 110 | + ``` | ||
| 111 | + | ||
| 112 | + 使用 NPU仿真 模式时,添加 `-DCMAKE_ASC_RUN_MODE=sim` 参数即可。 | ||
| 113 | + | ||
| 114 | + 示例如下: | ||
| 115 | + ```bash | ||
| 116 | + cmake -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..;make -j; # NPU仿真模式 | ||
| 117 | + ``` | ||
| 118 | + | ||
| 119 | + > **注意:** 切换编译模式前需清理 cmake 缓存,可在 build 目录下执行 `rm CMakeCache.txt` 后重新 cmake。 | ||
| 120 | + | ||
| 121 | +- 编译选项说明 | ||
| 122 | + | 选项 | 可选值 | 说明 | | ||
| 123 | + |------|--------|------| | ||
| 124 | + | `CMAKE_ASC_RUN_MODE` | `npu`(默认)、`sim` | 运行模式:NPU 运行、NPU仿真 | | ||
| 125 | + | `CMAKE_ASC_ARCHITECTURES` | `dav-3510` | NPU 架构:本样例仅支持 dav-3510(Ascend 950PR/Ascend 950DT) | | ||
| 126 | + | ||
| 127 | +- 执行结果(以非打屏模式下为例,落盘文件详情可参考[Ascend 日志功能参考](https://hiascend.com/document/redirect/CannCommunitylogref)) | ||
| 128 | + 执行后会打印类似以下信息: | ||
| 129 | + ```bash | ||
| 130 | + [INFO] Input shape: 12288 | ||
| 131 | + [INFO] Launching kernel with assert(total_length < 100)... | ||
| 132 | + Ascend Error: <your_path>/09_log/log.asc:88 code=507035 EZ9999: Inner Error! | ||
| 133 | + ... | ||
| 134 | + rtDeviceSynchronize execution failed, reason=vector core exception | ||
| 135 | + ... | ||
| 136 | + | ||
| 137 | + [ASSERT] <your_path>/09_log/log.asc:47: void add_custom(float *, float *, float *, uint64_t): Assertion `total_length < 100 && "Total length exceeds expected limit!"' failed. | ||
| 138 | + [INFO] Execution completed. Check for error messages above. | ||
| 139 | + ``` | ||
| 140 | + | ||
| 141 | + > **说明:** | ||
| 142 | + > - `<your_path>` 表示样例代码所在目录的绝对路径,实际输出中会替换为具体路径 | ||
| 143 | + > - 关键错误信息解读: | ||
| 144 | + > - `Ascend Error: ... code=507035 EZ9999: Inner Error!` — ACL接口返回的底层错误码 | ||
| 145 | + > - `vector core exception` — AI Core执行异常,Kernel任务失败 | ||
| 146 | + > - `Assertion 'total_length < 100 ...' failed` — 断言失败的具体位置和条件,帮助定位问题代码 | ||
| 147 | + | ||
| 148 | + 同时在`./log`目录下会生成日志文件,包含详细的运行日志。 | ||
| @@ -0,0 +1,132 @@ | |||
| 1 | +/** | ||
| 2 | +* Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | +* This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | +* CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | +* Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | +* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | +* See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | +*/ | ||
| 10 | + | ||
| 11 | +/* ! | ||
| 12 | + * \file log.asc | ||
| 13 | + * \brief 演示SIMT编程模式下使用assert抛出错误,以及通过ASCENDC_CHECK宏捕获错误 | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include <iostream> | ||
| 17 | +#include <iterator> | ||
| 18 | +#include <vector> | ||
| 19 | +#include "asc_assert.h" | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | +#include "asc_simt.h" | ||
| 22 | + | ||
| 23 | +// [核心] ASCENDC_CHECK宏:用于捕获并打印ACL错误信息 | ||
| 24 | +#define ASCENDC_CHECK(expr) do { \ | ||
| 25 | + aclError ret = (expr); \ | ||
| 26 | + if (ret != ACL_SUCCESS) { \ | ||
| 27 | + fprintf(stderr, \ | ||
| 28 | + "Ascend Error: %s:%d code=%d %s\n", \ | ||
| 29 | + __FILE__, __LINE__, \ | ||
| 30 | + ret, aclGetRecentErrMsg()); \ | ||
| 31 | + } \ | ||
| 32 | + } while(0) | ||
| 33 | + | ||
| 34 | +/** | ||
| 35 | + * @brief Kernel函数:演示assert抛出错误 | ||
| 36 | + */ | ||
| 37 | +__global__ void add_custom(float* x, float* y, float* z, uint64_t total_length) | ||
| 38 | +{ | ||
| 39 | + int32_t idx = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 40 | + | ||
| 41 | + if (idx >= total_length) { | ||
| 42 | + return; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + // assert抛出错误:仅thread 0触发断言,避免所有线程都打印 | ||
| 46 | + if (idx == 0) { | ||
| 47 | + assert(total_length < 100 && "Total length exceeds expected limit!"); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + z[idx] = x[idx] + y[idx]; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +/** | ||
| 54 | + * @brief Host端Kernel调用函数 | ||
| 55 | + */ | ||
| 56 | +void run_kernel(std::vector<float>& x, std::vector<float>& y, uint64_t total_length) | ||
| 57 | +{ | ||
| 58 | + size_t total_byte_size = x.size() * sizeof(float); | ||
| 59 | + int32_t device_id = 0; | ||
| 60 | + aclrtStream stream = nullptr; | ||
| 61 | + | ||
| 62 | + uint8_t* x_host = reinterpret_cast<uint8_t *>(x.data()); | ||
| 63 | + uint8_t* y_host = reinterpret_cast<uint8_t *>(y.data()); | ||
| 64 | + | ||
| 65 | + float* x_device = nullptr; | ||
| 66 | + float* y_device = nullptr; | ||
| 67 | + float* z_device = nullptr; | ||
| 68 | + | ||
| 69 | + aclInit(nullptr); | ||
| 70 | + aclrtSetDevice(device_id); | ||
| 71 | + aclrtCreateStream(&stream); | ||
| 72 | + | ||
| 73 | + aclrtMalloc((void **)&x_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 74 | + aclrtMalloc((void **)&y_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 75 | + aclrtMalloc((void **)&z_device, total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 76 | + | ||
| 77 | + aclrtMemcpy(x_device, total_byte_size, x_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 78 | + aclrtMemcpy(y_device, total_byte_size, y_host, total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 79 | + | ||
| 80 | + uint32_t block_num = 48; | ||
| 81 | + uint32_t thread_num_per_block = 256; | ||
| 82 | + uint32_t dyn_ubuf_size = 0; | ||
| 83 | + | ||
| 84 | + add_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(x_device, y_device, z_device, total_length); | ||
| 85 | + | ||
| 86 | + // [核心] 使用ASCENDC_CHECK宏捕获Kernel执行错误(包括assert触发的错误) | ||
| 87 | + ASCENDC_CHECK(aclrtGetLastError(ACL_RT_THREAD_LEVEL)); | ||
| 88 | + ASCENDC_CHECK(aclrtSynchronizeDevice()); | ||
| 89 | + | ||
| 90 | + aclrtSynchronizeStream(stream); | ||
| 91 | + | ||
| 92 | + const char* err = aclGetRecentErrMsg(); | ||
| 93 | + if (err != nullptr) { | ||
| 94 | + fprintf(stderr, "[ERROR] %s\n", err); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + aclrtFree(x_device); | ||
| 98 | + aclrtFree(y_device); | ||
| 99 | + aclrtFree(z_device); | ||
| 100 | + | ||
| 101 | + aclrtDestroyStream(stream); | ||
| 102 | + aclrtResetDevice(device_id); | ||
| 103 | + aclFinalize(); | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +/** | ||
| 107 | + * @brief 主函数:构造输入数据使assert触发,验证错误捕获 | ||
| 108 | + */ | ||
| 109 | +int32_t main(int32_t argc, char* argv[]) | ||
| 110 | +{ | ||
| 111 | + constexpr uint32_t in_shape = 48 * 256; | ||
| 112 | + | ||
| 113 | + std::vector<float> x(in_shape); | ||
| 114 | + for (uint32_t i = 0; i < in_shape; i++) { | ||
| 115 | + x[i] = i * 1.1f; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + std::vector<float> y(in_shape); | ||
| 119 | + for (uint32_t i = 0; i < in_shape; i++) { | ||
| 120 | + y[i] = i + 3.4f; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + std::cout << "[INFO] Input shape: " << in_shape << std::endl; | ||
| 124 | + std::cout << "[INFO] Launching kernel with assert(total_length < 100)..." << std::endl; | ||
| 125 | + | ||
| 126 | + // 调用Kernel(assert会触发,ASCENDC_CHECK宏捕获错误) | ||
| 127 | + run_kernel(x, y, in_shape); | ||
| 128 | + | ||
| 129 | + std::cout << "[INFO] Execution completed. Check for error messages above." << std::endl; | ||
| 130 | + | ||
| 131 | + return 0; | ||
| 132 | +} | ||
| @@ -7,4 +7,5 @@ | |||
| 7 | | -------------------------- | -------------------- | | 7 | | -------------------------- | -------------------- | |
| 8 | | [00_printf](./00_printf) | 本样例展示上板打印功能使用方法。 | | 8 | | [00_printf](./00_printf) | 本样例展示上板打印功能使用方法。 | |
| 9 | | [01_assert](./01_assert/) | 本样例展示上板assert断言的功能。 | | 9 | | [01_assert](./01_assert/) | 本样例展示上板assert断言的功能。 | |
| 10 | -| [03_profiling](./03_profiling/) | 本样例展示如何在上板环境使用Profiling工具采集性能数据。 | | 10 | +| [03_profiling](./03_profiling/) | 本样例展示如何在上板环境使用Profiling工具采集性能数据。 | |
| 11 | +| [09_log](./09_log/) | 本样例展示Ascend日志功能的使用方法,包括日志打屏、落盘及级别控制。 | | ||