Merged
simt新增profiling样例 #1989
shaonaiteAdd on May 7
simt新增profiling样例 #1989
Merged
Merged fromshaonaite/asc-devkitprofilingintocann/asc-devkitmaster
Total: 6 files changed+295-1
| @@ -0,0 +1,8 @@ | |||
| 1 | +# Profiling样例介绍 | ||
| 2 | +## 概述 | ||
| 3 | +本样例展示了如何通过Profiling工具采集性能数据。 | ||
| 4 | + | ||
| 5 | +## 样例列表 | ||
| 6 | +| 目录名称 | 功能描述 | | ||
| 7 | +| ------------------------------------------------------------ | ---------------------------------------------------- | | ||
| 8 | +| [msProf](./msProf) | 本样例基于Ascend C编程语言实现了Gather样例,同时使用msProf工具调试,给出了对应的端到端实现。 | | ||
| @@ -0,0 +1,30 @@ | |||
| 1 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 2 | +# This program is free software, you can redistribute it and/or modify it. | ||
| 3 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 4 | +# This file is a part of the CANN Open Software. | ||
| 5 | +# Licensed under CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 6 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 7 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 8 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 9 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 10 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 11 | + | ||
| 12 | +cmake_minimum_required(VERSION 3.16) | ||
| 13 | + | ||
| 14 | +find_package(ASC REQUIRED) | ||
| 15 | + | ||
| 16 | +project(kernel_samples LANGUAGES ASC CXX) | ||
| 17 | + | ||
| 18 | +add_executable(demo | ||
| 19 | + gather.asc | ||
| 20 | +) | ||
| 21 | + | ||
| 22 | +# ====================================================================================== | ||
| 23 | +# NPU 编译选项配置 | ||
| 24 | +# | ||
| 25 | +# 说明: | ||
| 26 | +# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 27 | +# ====================================================================================== | ||
| 28 | +target_compile_options(demo PRIVATE | ||
| 29 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510 --enable-simt> | ||
| 30 | +) | ||
| @@ -0,0 +1,89 @@ | |||
| 1 | +# 基于Gahter算子的profiling样例 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例基于Gather算子,演示了通过msProf采集上板的性能数据的方法。用户可根据输出的性能数据,快速定位算子的软、硬件性能瓶颈,提升算子性能的分析效率。 | ||
| 6 | + | ||
| 7 | +## 支持的产品 | ||
| 8 | + | ||
| 9 | +- Ascend 950PR/Ascend 950DT | ||
| 10 | + | ||
| 11 | +## 支持的CANN软件版本 | ||
| 12 | +- \>= CANN 9.0.0 | ||
| 13 | + | ||
| 14 | +## 目录结构介绍 | ||
| 15 | + | ||
| 16 | +``` | ||
| 17 | +├── msProf | ||
| 18 | +│ ├── CMakeLists.txt # cmake编译文件 | ||
| 19 | +│ ├── gahter.asc # SIMT实现gather调用样例 | ||
| 20 | +| └── README.md | ||
| 21 | +``` | ||
| 22 | + | ||
| 23 | +## 算子描述 | ||
| 24 | + gather算子实现了从形状为100000 * 128的二维向量中获取指定索引的12288行数据的功能,具体功能描述可参考[Gather算子详情](../../../02_features/00_resource_management/basic_gather/README.md)章节。 | ||
| 25 | + | ||
| 26 | +## msProf工具介绍 | ||
| 27 | +msProf工具是单算子性能分析工具。包含msprof op和msprof op simulator两种使用方式。该工具协助用户定位算子内存、算子代码以及算子指令的异常,实现全方位的算子调优。当前支持基于不同运行模式(上板或仿真)和不同文件形式(可执行文件或算子二进制.o文件)进行性能数据的采集和自动解析。 | ||
| 28 | + | ||
| 29 | +- 上板性能采集 | ||
| 30 | + | ||
| 31 | + 通过上板性能采集,可以直接测定算子昇腾AI处理器上的运行时间。该方式适合在板环境中快速定位算子性能问题。 | ||
| 32 | + | ||
| 33 | + 基于可执行文件demo通过msprof op执行算子调优: | ||
| 34 | + ``` | ||
| 35 | + msprof op ./demo | ||
| 36 | + ``` | ||
| 37 | + | ||
| 38 | + - 性能数据说明 | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + 命令完成后,会在默认目录下生成以“OPPROF_{timestamp}_XXX”命名的文件夹,性能数据文件夹结构示例如下: | ||
| 42 | + | ||
| 43 | + ```bash | ||
| 44 | + ├──dump # 原始的性能数据,用户无需关注 | ||
| 45 | + ├──ArithmeticUtilization.csv # cube/vector指令cycle占比 | ||
| 46 | + ├──L2Cache.csv # L2 Cache命中率 | ||
| 47 | + ├──Memory.csv # UB,L1和主存储器读写带宽速率 | ||
| 48 | + ├──MemoryL0.csv # L0A,L0B,和L0C读写带宽速率 | ||
| 49 | + ├──MemoryUB.csv # Vector和Scalar到UB的读写带宽速率 | ||
| 50 | + ├──OpBasicInfo.csv # 算子基础信息 | ||
| 51 | + ├──PipeUtilization.csv # 采集计算单元和搬运单元耗时和占比 | ||
| 52 | + ├──ResourceConflictRatio.csv # UB上的 bank group、bank conflict和资源冲突率在所有指令中的占比 | ||
| 53 | + └──visualize_data.bin # MindStudio Insight呈现文件 | ||
| 54 | + ``` | ||
| 55 | + | ||
| 56 | +用户可以使用 MindStudio Insight 打开 `visualize_data.bin` 文件可视化查看算子信息,包含算子基础信息、核间负载分析、计算工作负载分析、内存负载分析等。更多msProf工具使用方式请参考[算子开发工具](https://www.hiascend.com/document/redirect/CannCommercialToolOpDev)中算子调优(msOpProf)的内容。 | ||
| 57 | + | ||
| 58 | + | ||
| 59 | +## 编译运行 | ||
| 60 | + | ||
| 61 | +在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 62 | +- 配置环境变量 | ||
| 63 | + | ||
| 64 | + 请根据当前环境上CANN开发套件包的[安装方式](../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 65 | + - 默认路径,root用户安装CANN软件包 | ||
| 66 | + ```bash | ||
| 67 | + source /usr/local/Ascend/cann/set_env.sh | ||
| 68 | + ``` | ||
| 69 | + | ||
| 70 | + - 默认路径,非root用户安装CANN软件包 | ||
| 71 | + ```bash | ||
| 72 | + source $HOME/Ascend/cann/set_env.sh | ||
| 73 | + ``` | ||
| 74 | + | ||
| 75 | + - 指定路径install_path,安装CANN软件包 | ||
| 76 | + ```bash | ||
| 77 | + source ${install_path}/cann/set_env.sh | ||
| 78 | + ``` | ||
| 79 | + | ||
| 80 | +- 样例执行 | ||
| 81 | + ```bash | ||
| 82 | + mkdir -p build && cd build; # 创建并进入build目录 | ||
| 83 | + cmake ..;make -j; # 编译工程 | ||
| 84 | + msprof op ./demo # 基于可执行文件demo通过msprof op执行算子调优 | ||
| 85 | + ``` | ||
| 86 | + 执行结果如下,说明精度对比成功。 | ||
| 87 | + ``` | ||
| 88 | + [Success] Case accuracy is verification passed. | ||
| 89 | + ``` | ||
| @@ -0,0 +1,166 @@ | |||
| 1 | +/** | ||
| 2 | +* Copyright (c) 2025 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 | +/* ! | ||
| 13 | + * \file gather.asc | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#include <iostream> | ||
| 18 | +#include <iterator> | ||
| 19 | +#include <vector> | ||
| 20 | +#include "acl/acl.h" | ||
| 21 | +#include "tiling/platform/platform_ascendc.h" | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +template <typename type_data, typename type_idx> | ||
| 25 | +__global__ void gather_custom( | ||
| 26 | + type_data* input, | ||
| 27 | + type_idx* index, | ||
| 28 | + type_data* gather_output, | ||
| 29 | + uint32_t in_width, | ||
| 30 | + uint32_t index_total_length) | ||
| 31 | +{ | ||
| 32 | + // Calculate global thread ID | ||
| 33 | + int32_t out_row = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 34 | + | ||
| 35 | + // Maps to the row index of output tensor | ||
| 36 | + if (out_row >= index_total_length) { | ||
| 37 | + return; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + // Single thread processes entire row (all columns) - enables coalesced memory access | ||
| 41 | + uint32_t in_row = index[out_row]; | ||
| 42 | + for (int32_t col = 0; col < in_width; col++) { | ||
| 43 | + int input_idx = in_row * in_width + col; | ||
| 44 | + int output_idx = out_row * in_width + col; | ||
| 45 | + gather_output[output_idx] = input[input_idx]; | ||
| 46 | + } | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +std::vector<float> gather(std::vector<float>& input, const uint32_t* in_shape, std::vector<uint32_t>& index) | ||
| 50 | +{ | ||
| 51 | + std::vector<float> output_stub; | ||
| 52 | + uint32_t input_total_length = input.size(); | ||
| 53 | + size_t input_total_byte_size = input_total_length * sizeof(float); | ||
| 54 | + | ||
| 55 | + uint32_t index_total_length = index.size(); | ||
| 56 | + size_t index_total_byte_size = index_total_length * sizeof(uint32_t); | ||
| 57 | + | ||
| 58 | + uint32_t output_total_length = index.size() * in_shape[1]; | ||
| 59 | + size_t output_total_byte_size = output_total_length * sizeof(float); | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + int32_t device_id = 0; | ||
| 64 | + aclrtStream stream = nullptr; | ||
| 65 | + | ||
| 66 | + uint8_t* input_host = reinterpret_cast<uint8_t *>(input.data()); | ||
| 67 | + uint8_t* index_host = reinterpret_cast<uint8_t *>(index.data()); | ||
| 68 | + uint8_t* output_host = nullptr; | ||
| 69 | + float* input_device = nullptr; | ||
| 70 | + uint32_t* index_device = nullptr; | ||
| 71 | + float* output_device = nullptr; | ||
| 72 | + // Init | ||
| 73 | + aclInit(nullptr); | ||
| 74 | + aclrtSetDevice(device_id); | ||
| 75 | + aclrtCreateStream(&stream); | ||
| 76 | + // Malloc memory in host and device | ||
| 77 | + aclrtMallocHost((void **)(&output_host), output_total_byte_size); | ||
| 78 | + aclrtMalloc((void **)&input_device, input_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 79 | + aclrtMalloc((void **)&index_device, index_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 80 | + aclrtMalloc((void **)&output_device, output_total_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 81 | + aclrtMemcpy(input_device, input_total_byte_size, input_host, input_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 82 | + aclrtMemcpy(index_device, index_total_byte_size, index_host, index_total_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 83 | + // Calc splite params | ||
| 84 | + uint32_t numBlocks = 48; | ||
| 85 | + uint32_t thread_num_per_block = 256; | ||
| 86 | + uint32_t dyn_ubuf_size = 0; // No need to alloc dynamic memory. | ||
| 87 | + // Call kernel funtion with <<<...>>> | ||
| 88 | + gather_custom<<<numBlocks, thread_num_per_block, dyn_ubuf_size, stream>>>( | ||
| 89 | + input_device, index_device, output_device, in_shape[1], index_total_length); | ||
| 90 | + aclrtSynchronizeStream(stream); | ||
| 91 | + // Copy result from device to host | ||
| 92 | + aclrtMemcpy(output_host, output_total_byte_size, output_device, output_total_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 93 | + std::vector<float> output((float *)output_host, (float *)(output_host + output_total_byte_size)); | ||
| 94 | + // Free memory | ||
| 95 | + aclrtFree(input_device); | ||
| 96 | + aclrtFree(index_device); | ||
| 97 | + aclrtFree(output_device); | ||
| 98 | + aclrtFreeHost(output_host); | ||
| 99 | + | ||
| 100 | + const char* err = aclGetRecentErrMsg(); | ||
| 101 | + if (err != nullptr) { | ||
| 102 | + fprintf(stderr, "%s\n", err); | ||
| 103 | + } | ||
| 104 | + // DeInt | ||
| 105 | + aclrtDestroyStream(stream); | ||
| 106 | + aclrtResetDevice(device_id); | ||
| 107 | + aclFinalize(); | ||
| 108 | + return output; | ||
| 109 | +} | ||
| 110 | + | ||
| 111 | +uint32_t verify_result(std::vector<float>& output, std::vector<float>& golden) | ||
| 112 | +{ | ||
| 113 | + auto print_tensor = [](std::vector<float>& tensor, const char* name) { | ||
| 114 | + constexpr size_t max_print_size = 20; | ||
| 115 | + std::cout << name << ": "; | ||
| 116 | + std::copy(tensor.begin(), tensor.begin() + std::min(tensor.size(), max_print_size), | ||
| 117 | + std::ostream_iterator<float>(std::cout, " ")); | ||
| 118 | + if (tensor.size() > max_print_size) { | ||
| 119 | + std::cout << "..."; | ||
| 120 | + } | ||
| 121 | + std::cout << std::endl; | ||
| 122 | + }; | ||
| 123 | + print_tensor(output, "Output"); | ||
| 124 | + print_tensor(golden, "Golden"); | ||
| 125 | + if (std::equal(output.begin(), output.end(), golden.begin())) { | ||
| 126 | + std::cout << "[Success] Case accuracy is verification passed." << std::endl; | ||
| 127 | + return 0; | ||
| 128 | + } else { | ||
| 129 | + std::cout << "[Failed] Case accuracy is verification failed!" << std::endl; | ||
| 130 | + return 1; | ||
| 131 | + } | ||
| 132 | + return 0; | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +int32_t main(int32_t argc, char* argv[]) | ||
| 136 | +{ | ||
| 137 | + constexpr uint32_t in_shape[2] = {100000, 128}; | ||
| 138 | + constexpr uint32_t input_total_length = in_shape[0] * in_shape[1]; | ||
| 139 | + std::vector<float> input(input_total_length); | ||
| 140 | + for (uint32_t i = 0; i < input_total_length; i++) { | ||
| 141 | + input[i] = i *1.2f; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + constexpr uint32_t index_total_length = 12288; | ||
| 145 | + std::vector<uint32_t> index(index_total_length); | ||
| 146 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 147 | + if (i % 2 == 0) { | ||
| 148 | + index[i] = i + 5; | ||
| 149 | + } else { | ||
| 150 | + index[i] = i + 3; | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + std::vector<float> golden(index_total_length * in_shape[1]); | ||
| 155 | + for (uint32_t i = 0; i < index_total_length; i++) { | ||
| 156 | + for (uint32_t j = 0; j < in_shape[1]; j++) { | ||
| 157 | + golden[i * in_shape[1] + j] = input[index[i] * in_shape[1] + j]; | ||
| 158 | + } | ||
| 159 | + } | ||
| 160 | + | ||
| 161 | + std::vector<float> output = gather(input, in_shape, index); | ||
| 162 | + if (output.empty()) { | ||
| 163 | + return 1; | ||
| 164 | + } | ||
| 165 | + return verify_result(output, golden); | ||
| 166 | +} | ||
| @@ -7,3 +7,4 @@ | |||
| 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工具采集性能数据。 | | ||
| @@ -122,7 +122,7 @@ | |||
| 122 | 122 | ||
| 123 | 每个线程块先在块内完成上述两阶段局部归约,再由 `tid = 0` 的线程将本线程块的局部和写入 `block_sums[blockIdx.x]`。写入后执行 `asc_threadfence()`,再将全局计数器加 1。最后一个对计数器加 1 的线程块(即 `ticket = gridDim.x - 1` 的线程块)会读取 `block_sums` 并执行第二次两阶段归约,输出最终结果。 | 123 | 每个线程块先在块内完成上述两阶段局部归约,再由 `tid = 0` 的线程将本线程块的局部和写入 `block_sums[blockIdx.x]`。写入后执行 `asc_threadfence()`,再将全局计数器加 1。最后一个对计数器加 1 的线程块(即 `ticket = gridDim.x - 1` 的线程块)会读取 `block_sums` 并执行第二次两阶段归约,输出最终结果。 |
| 124 | 124 | ||
| 125 | -以 4 个线程块为例,跨线程块协作过程如图2所示: | 125 | +以 8 个线程块为例,跨线程块协作过程如图2所示: |
| 126 | 126 | ||
| 127 | <p align="center"> | 127 | <p align="center"> |
| 128 | <img src="./figure/跨线程块协作.png" width="75%"> | 128 | <img src="./figure/跨线程块协作.png" width="75%"> |