Merged
添加simt simulator样例 #2692
DragonBornHD84Add on May 28
添加simt simulator样例 #2692
Merged
Merged fromDragonBornHD84/asc-devkitdev2intocann/asc-devkitmaster
Total: 8 files changed+428-6
| @@ -0,0 +1,27 @@ | |||
| 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 | + add.asc | ||
| 20 | +) | ||
| 21 | + | ||
| 22 | +set(CMAKE_ASC_ARCHITECTURES "dav-3510" CACHE STRING "NPU ARCH, e.g. dav-3510") | ||
| 23 | + | ||
| 24 | +target_compile_options(demo PRIVATE | ||
| 25 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES} --enable-simt> | ||
| 26 | + -g | ||
| 27 | +) | ||
| @@ -0,0 +1,127 @@ | |||
| 1 | +# 基于Add的CAmodel仿真样例说明 | ||
| 2 | + | ||
| 3 | +## 概述 | ||
| 4 | + | ||
| 5 | +本样例以Add计算为载体,展示Ascend C程序在CAmodel仿真模式下的编译、运行、结果校验与性能数据采集流程。用户可通过`msprof op simulator`获取仿真性能数据,用于分析性能瓶颈,提升性能分析效率。 | ||
| 6 | + | ||
| 7 | +## 支持的产品 | ||
| 8 | + | ||
| 9 | +- Ascend 950PR/Ascend 950DT | ||
| 10 | + | ||
| 11 | +## 支持的CANN软件版本 | ||
| 12 | +- \>= CANN 9.1.0 | ||
| 13 | + | ||
| 14 | +## 目录结构介绍 | ||
| 15 | + | ||
| 16 | +``` | ||
| 17 | +├── 07_simulator | ||
| 18 | +│ ├── CMakeLists.txt // 编译工程文件 | ||
| 19 | +│ ├── data_utils.h // 数据读写辅助函数 | ||
| 20 | +│ ├── README.md // 样例说明 | ||
| 21 | +│ ├── scripts // 输入数据生成和结果校验脚本 | ||
| 22 | +│ └── add.asc // SIMT实现Add调用样例 | ||
| 23 | +``` | ||
| 24 | + | ||
| 25 | +## 样例描述 | ||
| 26 | + | ||
| 27 | +- 样例功能 | ||
| 28 | + | ||
| 29 | + 本样例基于Ascend C SIMT编程方式实现两个输入张量逐元素相加得到输出张量,计算公式如下: | ||
| 30 | + | ||
| 31 | + ```text | ||
| 32 | + z = x + y | ||
| 33 | + ``` | ||
| 34 | + | ||
| 35 | + 样例中`x`和`y`的shape为`48 * 256`,数据类型为`float`。`scripts/gen_data.py`用于生成输入数据和golden数据,样例执行后将结果写入`output/output.bin`,再通过`scripts/verify_result.py`完成结果校验。 | ||
| 36 | + | ||
| 37 | +- 样例规格 | ||
| 38 | + | ||
| 39 | + <table border="2"> | ||
| 40 | + <caption>表1:Add样例规格描述</caption> | ||
| 41 | + <tr><td rowspan="1" align="center">样例类型(OpType)</td><td colspan="4" align="center">Add</td></tr> | ||
| 42 | + <tr><td align="center"></td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td><td align="center">format</td></tr> | ||
| 43 | + <tr><td align="center">样例输入</td><td align="center">x</td><td align="center">[48, 256]</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 44 | + <tr><td align="center">样例输入</td><td align="center">y</td><td align="center">[48, 256]</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 45 | + <tr><td align="center">样例输出</td><td align="center">z</td><td align="center">[48, 256]</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 46 | + <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">add_custom</td></tr> | ||
| 47 | + </table> | ||
| 48 | + | ||
| 49 | +## 编译运行 | ||
| 50 | + | ||
| 51 | +在本样例根目录下执行如下步骤,编译并执行程序。 | ||
| 52 | + | ||
| 53 | +- 配置环境变量 | ||
| 54 | + 请根据当前环境上CANN开发套件包的[安装方式](../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 55 | + | ||
| 56 | + > 💡 使用 msProf 工具需安装 CANN 商用/社区版,详细信息可参考[msOpProf工具安装指南](https://www.hiascend.com/document/detail/zh/canncommercial/900/devaids/optool/docs/zh/install_guide/msopprof_install_guide.md)。 | ||
| 57 | + | ||
| 58 | + - 默认路径,root用户安装CANN软件包 | ||
| 59 | + ```bash | ||
| 60 | + source /usr/local/Ascend/cann/set_env.sh | ||
| 61 | + ``` | ||
| 62 | + | ||
| 63 | + - 默认路径,非root用户安装CANN软件包 | ||
| 64 | + ```bash | ||
| 65 | + source $HOME/Ascend/cann/set_env.sh | ||
| 66 | + ``` | ||
| 67 | + | ||
| 68 | + - 指定路径install_path,安装CANN软件包 | ||
| 69 | + ```bash | ||
| 70 | + source ${install_path}/cann/set_env.sh | ||
| 71 | + ``` | ||
| 72 | + | ||
| 73 | +- 样例执行 | ||
| 74 | + | ||
| 75 | + ```bash | ||
| 76 | + mkdir -p build && cd build | ||
| 77 | + cmake -DCMAKE_ASC_RUN_MODE=sim -DCMAKE_ASC_ARCHITECTURES=dav-3510 ..; make -j | ||
| 78 | + python3 ../scripts/gen_data.py | ||
| 79 | + msprof op simulator --soc-version=<soc_version> ./demo | ||
| 80 | + python3 ../scripts/verify_result.py ./output/output.bin ./output/golden.bin | ||
| 81 | + ``` | ||
| 82 | + | ||
| 83 | + 请根据实际测试的NPU硬件架构选择对应的`CMAKE_ASC_ARCHITECTURES`参数。 | ||
| 84 | + | ||
| 85 | + | 选项 | 说明 | | ||
| 86 | + |------|------| | ||
| 87 | + | `CMAKE_ASC_RUN_MODE` | 指定为`sim`,开启NPU仿真模式 | | ||
| 88 | + | `CMAKE_ASC_ARCHITECTURES` | 指定NPU架构版本号。本样例仅支持`dav-3510`(Ascend 950PR/Ascend 950DT) | | ||
| 89 | + | ||
| 90 | + > 💡 CMakeLists.txt中已添加`-g`编译选项,用于生成调试信息,使仿真器能够采集指令流水图。如需在自有工程中使用仿真调优功能,请确保编译时同样添加该选项。 | ||
| 91 | + | ||
| 92 | + 精度对比成功时,输出如下: | ||
| 93 | + | ||
| 94 | + ```bash | ||
| 95 | + test pass! | ||
| 96 | + ``` | ||
| 97 | + | ||
| 98 | +## 仿真调优 | ||
| 99 | + | ||
| 100 | +基于`./demo`可通过`msprof op simulator`进行仿真性能分析,生成可视化的指令流水图等信息,指令如下: | ||
| 101 | + | ||
| 102 | +```bash | ||
| 103 | +msprof op simulator --soc-version=<soc_version> ./demo | ||
| 104 | +``` | ||
| 105 | + | ||
| 106 | +> AI处理器的型号`<soc_version>`请通过如下方式获取: | ||
| 107 | +> | ||
| 108 | +> - Ascend 950PR/Ascend 950DT | ||
| 109 | +> - 针对以上产品型号,在安装昇腾AI处理器的服务器执行`npu-smi info -t board -i <id> -c <chip_id>`命令进行查询,获取**Chip Name**和**NPU Name**信息,实际配置值为Chip Name_NPU Name。例如**Chip Name**取值为Ascendxxx,**NPU Name**取值为1234,实际配置值为Ascendxxx_1234。 | ||
| 110 | +> | ||
| 111 | +> 其中,`id`为设备ID,通过`npu-smi info -l`命令查出的NPU ID即为设备ID;`chip_id`为芯片ID,通过`npu-smi info -m`命令查出的Chip ID即为芯片ID。 | ||
| 112 | + | ||
| 113 | +命令完成后,会在当前目录下生成以`OPPROF_{timestamp}_XXX`命名的文件夹,产物结构如下: | ||
| 114 | + | ||
| 115 | +``` | ||
| 116 | +OPPROF_{timestamp}_XXX/ | ||
| 117 | +├── dump // 原始性能数据,用户无需关注 | ||
| 118 | +└── simulator | ||
| 119 | + ├── core*.veccore*/ // 各向量核的仿真指令流水图文件 | ||
| 120 | + └── visualize_data.bin // MindStudio Insight呈现文件 | ||
| 121 | +``` | ||
| 122 | + | ||
| 123 | +执行后可通过以下方式查看指令流水图: | ||
| 124 | + | ||
| 125 | +- **MindStudio Insight**:打开`visualize_data.bin`进行可视化呈现。 | ||
| 126 | + | ||
| 127 | + 更多详细内容可以查看msProf工具使用方式,[MindStudio工具调优(msProf)快速入门](https://www.hiascend.com/document/detail/zh/canncommercial/900/devaids/optool/docs/zh/quick_start/msopprof_quick_start.md)。 | ||
| @@ -0,0 +1,81 @@ | |||
| 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 add.asc | ||
| 13 | + * \brief | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "data_utils.h" | ||
| 17 | +#include "acl/acl.h" | ||
| 18 | + | ||
| 19 | +constexpr uint32_t TOTAL_LENGTH = 48 * 256; | ||
| 20 | + | ||
| 21 | +__global__ void add_custom(float* x, float* y, float* z, uint64_t total_length) | ||
| 22 | +{ | ||
| 23 | + int32_t idx = blockIdx.x * blockDim.x + threadIdx.x; | ||
| 24 | + if (idx >= total_length) { | ||
| 25 | + return; | ||
| 26 | + } | ||
| 27 | + z[idx] = x[idx] + y[idx]; | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +int32_t main(int32_t argc, char* argv[]) | ||
| 31 | +{ | ||
| 32 | + size_t input_byte_size = TOTAL_LENGTH * sizeof(float); | ||
| 33 | + size_t output_byte_size = TOTAL_LENGTH * sizeof(float); | ||
| 34 | + | ||
| 35 | + aclInit(nullptr); | ||
| 36 | + int32_t deviceId = 0; | ||
| 37 | + aclrtSetDevice(deviceId); | ||
| 38 | + aclrtStream stream = nullptr; | ||
| 39 | + aclrtCreateStream(&stream); | ||
| 40 | + | ||
| 41 | + uint8_t* xHost = nullptr; | ||
| 42 | + float* xDevice = nullptr; | ||
| 43 | + aclrtMallocHost((void**)&xHost, input_byte_size); | ||
| 44 | + aclrtMalloc((void**)&xDevice, input_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 45 | + ReadFile("./input/x_gm.bin", input_byte_size, xHost, input_byte_size); | ||
| 46 | + aclrtMemcpy(xDevice, input_byte_size, xHost, input_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 47 | + | ||
| 48 | + uint8_t* yHost = nullptr; | ||
| 49 | + float* yDevice = nullptr; | ||
| 50 | + aclrtMallocHost((void**)&yHost, input_byte_size); | ||
| 51 | + aclrtMalloc((void**)&yDevice, input_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 52 | + ReadFile("./input/y_gm.bin", input_byte_size, yHost, input_byte_size); | ||
| 53 | + aclrtMemcpy(yDevice, input_byte_size, yHost, input_byte_size, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 54 | + | ||
| 55 | + uint8_t* zHost = nullptr; | ||
| 56 | + float* zDevice = nullptr; | ||
| 57 | + aclrtMallocHost((void**)&zHost, output_byte_size); | ||
| 58 | + aclrtMalloc((void**)&zDevice, output_byte_size, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 59 | + | ||
| 60 | + uint32_t block_num = 48; | ||
| 61 | + uint32_t thread_num_per_block = 256; | ||
| 62 | + uint32_t dyn_ubuf_size = 0; | ||
| 63 | + add_custom<<<block_num, thread_num_per_block, dyn_ubuf_size, stream>>>(xDevice, yDevice, zDevice, TOTAL_LENGTH); | ||
| 64 | + | ||
| 65 | + aclrtSynchronizeStream(stream); | ||
| 66 | + | ||
| 67 | + aclrtMemcpy(zHost, output_byte_size, zDevice, output_byte_size, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 68 | + WriteFile("./output/output.bin", zHost, output_byte_size); | ||
| 69 | + | ||
| 70 | + aclrtFree(xDevice); | ||
| 71 | + aclrtFreeHost(xHost); | ||
| 72 | + aclrtFree(yDevice); | ||
| 73 | + aclrtFreeHost(yHost); | ||
| 74 | + aclrtFree(zDevice); | ||
| 75 | + aclrtFreeHost(zHost); | ||
| 76 | + | ||
| 77 | + aclrtDestroyStream(stream); | ||
| 78 | + aclrtResetDevice(deviceId); | ||
| 79 | + aclFinalize(); | ||
| 80 | + return 0; | ||
| 81 | +} | ||
| @@ -0,0 +1,94 @@ | |||
| 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 | +/* ! | ||
| 13 | + * \file data_utils.h | ||
| 14 | + * \brief | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +#ifndef DATA_UTILS_H | ||
| 18 | +#define DATA_UTILS_H | ||
| 19 | +#include <fcntl.h> | ||
| 20 | +#include <sys/stat.h> | ||
| 21 | +#include <unistd.h> | ||
| 22 | +#include <fstream> | ||
| 23 | + | ||
| 24 | +#define ERROR_LOG(fmt, args...) fprintf(stdout, "[ERROR] " fmt "\n", ##args) | ||
| 25 | + | ||
| 26 | +bool ReadFile(const std::string &filePath, size_t &fileSize, void *buffer, size_t bufferSize) | ||
| 27 | +{ | ||
| 28 | + struct stat sBuf; | ||
| 29 | + int fileStatus = stat(filePath.data(), &sBuf); | ||
| 30 | + if (fileStatus == -1) { | ||
| 31 | + ERROR_LOG("failed to get file"); | ||
| 32 | + return false; | ||
| 33 | + } | ||
| 34 | + if (S_ISREG(sBuf.st_mode) == 0) { | ||
| 35 | + ERROR_LOG("%s is not a file, please enter a file", filePath.c_str()); | ||
| 36 | + return false; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + std::ifstream file; | ||
| 40 | + file.open(filePath, std::ios::binary); | ||
| 41 | + if (!file.is_open()) { | ||
| 42 | + ERROR_LOG("Open file failed. path = %s", filePath.c_str()); | ||
| 43 | + return false; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + std::filebuf *buf = file.rdbuf(); | ||
| 47 | + size_t size = buf->pubseekoff(0, std::ios::end, std::ios::in); | ||
| 48 | + if (size == 0) { | ||
| 49 | + ERROR_LOG("file size is 0"); | ||
| 50 | + file.close(); | ||
| 51 | + return false; | ||
| 52 | + } | ||
| 53 | + if (size > bufferSize) { | ||
| 54 | + ERROR_LOG("file size is larger than buffer size"); | ||
| 55 | + file.close(); | ||
| 56 | + return false; | ||
| 57 | + } | ||
| 58 | + buf->pubseekpos(0, std::ios::in); | ||
| 59 | + buf->sgetn(static_cast<char *>(buffer), size); | ||
| 60 | + fileSize = size; | ||
| 61 | + file.close(); | ||
| 62 | + return true; | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +/** | ||
| 66 | + * @brief Write data to file | ||
| 67 | + * @param [in] filePath: file path | ||
| 68 | + * @param [in] buffer: data to write to file | ||
| 69 | + * @param [in] size: size to write | ||
| 70 | + * @return write result | ||
| 71 | + */ | ||
| 72 | +bool WriteFile(const std::string &filePath, const void *buffer, size_t size) | ||
| 73 | +{ | ||
| 74 | + if (buffer == nullptr) { | ||
| 75 | + ERROR_LOG("Write file failed. buffer is nullptr"); | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + int fd = open(filePath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWRITE); | ||
| 80 | + if (fd < 0) { | ||
| 81 | + ERROR_LOG("Open file failed. path = %s", filePath.c_str()); | ||
| 82 | + return false; | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + size_t writeSize = write(fd, buffer, size); | ||
| 86 | + (void)close(fd); | ||
| 87 | + if (writeSize != size) { | ||
| 88 | + ERROR_LOG("Write file Failed."); | ||
| 89 | + return false; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + return true; | ||
| 93 | +} | ||
| 94 | +#endif // DATA_UTILS_H | ||
| @@ -0,0 +1,32 @@ | |||
| 1 | +#!/usr/bin/python3 | ||
| 2 | +# coding=utf-8 | ||
| 3 | + | ||
| 4 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 6 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 7 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 8 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 9 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 10 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 11 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 12 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +import os | ||
| 16 | +import numpy as np | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +def gen_golden_data(): | ||
| 20 | + total_length = 48 * 256 | ||
| 21 | + input_x = np.random.randint(1, 10, [total_length]).astype(np.float32) | ||
| 22 | + input_y = np.random.randint(1, 10, [total_length]).astype(np.float32) | ||
| 23 | + golden = input_x + input_y | ||
| 24 | + os.makedirs("input", exist_ok=True) | ||
| 25 | + os.makedirs("output", exist_ok=True) | ||
| 26 | + input_x.tofile("./input/x_gm.bin") | ||
| 27 | + input_y.tofile("./input/y_gm.bin") | ||
| 28 | + golden.tofile("./output/golden.bin") | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +if __name__ == "__main__": | ||
| 32 | + gen_golden_data() | ||
| @@ -0,0 +1,59 @@ | |||
| 1 | +#!/usr/bin/python3 | ||
| 2 | +# coding=utf-8 | ||
| 3 | + | ||
| 4 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 6 | +# This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 7 | +# CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 8 | +# Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 9 | +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 10 | +# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 11 | +# See LICENSE in the root of the software repository for the full text of the License. | ||
| 12 | +# ---------------------------------------------------------------------------------------------------------- | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +import sys | ||
| 16 | +import numpy as np | ||
| 17 | + | ||
| 18 | + | ||
| 19 | +RELATIVE_TOL = 1e-6 | ||
| 20 | +ABSOLUTE_TOL = 1e-9 | ||
| 21 | +ERROR_TOL = 1e-4 | ||
| 22 | + | ||
| 23 | + | ||
| 24 | +def verify_result(output, golden): | ||
| 25 | + output = np.fromfile(output, dtype=np.float32).reshape(-1) | ||
| 26 | + golden = np.fromfile(golden, dtype=np.float32).reshape(-1) | ||
| 27 | + different_element_results = np.isclose(output, | ||
| 28 | + golden, | ||
| 29 | + rtol=RELATIVE_TOL, | ||
| 30 | + atol=ABSOLUTE_TOL, | ||
| 31 | + equal_nan=True) | ||
| 32 | + different_element_indexes = np.where(different_element_results == False)[0] | ||
| 33 | + for index in range(len(different_element_indexes)): | ||
| 34 | + real_index = different_element_indexes[index] | ||
| 35 | + golden_data = golden[real_index] | ||
| 36 | + output_data = output[real_index] | ||
| 37 | + print( | ||
| 38 | + "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f" % | ||
| 39 | + (real_index, golden_data, output_data, | ||
| 40 | + abs(output_data - golden_data) / golden_data)) | ||
| 41 | + if index == 100: | ||
| 42 | + break | ||
| 43 | + print("golden_data : ", golden) | ||
| 44 | + print("output : ", output) | ||
| 45 | + error_ratio = float(different_element_indexes.size) / golden.size | ||
| 46 | + print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, ERROR_TOL)) | ||
| 47 | + return error_ratio <= ERROR_TOL | ||
| 48 | + | ||
| 49 | + | ||
| 50 | +if __name__ == '__main__': | ||
| 51 | + try: | ||
| 52 | + res = verify_result(sys.argv[1], sys.argv[2]) | ||
| 53 | + if not res: | ||
| 54 | + raise ValueError("[ERROR] result error") | ||
| 55 | + else: | ||
| 56 | + print("test pass!") | ||
| 57 | + except Exception as e: | ||
| 58 | + print(e) | ||
| 59 | + sys.exit(1) | ||
| @@ -6,6 +6,7 @@ | |||
| 6 | | 目录名称 | 功能描述 | | 6 | | 目录名称 | 功能描述 | |
| 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日志功能的使用方法,包括日志打屏、落盘及级别控制。 | | 11 | +| [07_simulator](./07_simulator) | 本样例展示Ascend C程序在CAmodel仿真模式下的编译、运行、结果校验与性能数据采集流程。 | |
| 12 | +| [09_log](./09_log) | 本样例展示Ascend日志功能的使用方法,包括日志打屏、落盘及级别控制。 | | ||
| @@ -6,6 +6,7 @@ Simple examples based on Ascend C SIMT programming, introducing on-board debuggi | |||
| 6 | | Directory Name | Function Description | | 6 | | Directory Name | Function Description | |
| 7 | | -------------------------- | -------------------- | | 7 | | -------------------------- | -------------------- | |
| 8 | | [00_printf](./00_printf) | This example demonstrates the usage of on-board printing functionality. | | 8 | | [00_printf](./00_printf) | This example demonstrates the usage of on-board printing functionality. | |
| 9 | -| [01_assert](./01_assert/) | This example demonstrates the on-board assert assertion functionality. | | 9 | +| [01_assert](./01_assert) | This example demonstrates the on-board assert assertion functionality. | |
| 10 | -| [03_profiling](./03_profiling/) | This example demonstrates how to use Profiling tools to collect performance data in on-board environments. | | 10 | +| [03_profiling](./03_profiling) | This example demonstrates how to use Profiling tools to collect performance data in on-board environments. | |
| 11 | -| [09_log](./09_log/) | This example demonstrates the usage of Ascend logging functionality, including log output to screen, log file storage, and log level control. | | 11 | +| [07_simulator](./07_simulator) | This example demonstrates the compilation, execution, result verification, and performance data collection workflow of Ascend C programs in CAmodel simulator mode. | |
| 12 | +| [09_log](./09_log) | This example demonstrates the usage of Ascend logging functionality, including log output to screen, log file storage, and log level control. | | ||