Merged
add datacopy best practices #2137
每天都要吃馒头Add on May 13
add datacopy best practices #2137
Merged
Merged fromyuaner1057/asc-devkitbest-practicesintocann/asc-devkitmaster
Total: 52 files changed+1129-1891
Mexamples/01_simd_cpp_api/02_features/03_basic_api/00_data_movement/data_copy_gm2ub_nddma/README.md+16-16File content is under review, please try again later
Mexamples/01_simd_cpp_api/02_features/03_basic_api/00_data_movement/data_copy_gm2ub_nddma/multidimensional_data_movement.asc+4-4File content is under review, please try again later
Mexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/README.md+2-3File content is under review, please try again later
Mexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/README_en.md+2-3File content is under review, please try again later
| @@ -1,8 +0,0 @@ | |||
| 1 | -# bank冲突算子直调样例 | ||
| 2 | -## 概述 | ||
| 3 | -本目录提供 bank 冲突调优样例,包括 ND2NZ 数据重排引发的 bank 冲突,并提供核函数直调方法。 | ||
| 4 | - | ||
| 5 | -## 样例列表 | ||
| 6 | -| 目录名称 | 功能描述 | 支持的产品 | | ||
| 7 | -| ------------------------------------------------------------ | ---------------------------------------------------- | --- | | ||
| 8 | -| [bank_conflict_nd2nz](./bank_conflict_nd2nz) | 本样例介绍 Atlas A2/A3 系列产品和 Ascend 950PR/Ascend 950DT 上,ND 矩阵转换为 NZ 布局时通过调整 UB 内写步长规避 bank 冲突的实现,并提供核函数直调方法 | Ascend 950PR/Ascend 950DT<br>Atlas A3 训练系列产品/Atlas A3 推理系列产品<br>Atlas A2 训练系列产品/Atlas A2 推理系列产品 | | ||
Dexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/README_en.md+0-9
| @@ -1,9 +0,0 @@ | |||
| 1 | -# Bank Conflict Operator Direct Invocation Example | ||
| 2 | - | ||
| 3 | -## Overview | ||
| 4 | -This directory provides bank conflict tuning examples, including bank conflict caused by ND2NZ data rearrangement, and provides a kernel function direct invocation method. | ||
| 5 | - | ||
| 6 | -## Example List | ||
| 7 | -| Directory Name | Description | Supported Products | | ||
| 8 | -| ------------------------------------------------------------ | ---------------------------------------------------- | --- | | ||
| 9 | -| [bank_conflict_nd2nz](./bank_conflict_nd2nz) | This example introduces the implementation of avoiding bank conflict by adjusting UB write stride during ND matrix to NZ layout conversion on Atlas A2/A3 series products and Ascend 950PR/Ascend 950DT, and provides a kernel function direct invocation method | Ascend 950PR/Ascend 950DT<br>Atlas A3 Training Series Products/Atlas A3 Inference Series Products<br>Atlas A2 Training Series Products/Atlas A2 Inference Series Products | | ||
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/CMakeLists.txt→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/CMakeLists.txt+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/README.md→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/README.md+11-10File content is under review, please try again later
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/bank_conflict_nd2nz.asc→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/bank_conflict_nd2nz.asc+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/bank_conflict_nd2nz_3510.h→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/bank_conflict_nd2nz_3510.h+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/data_utils.h→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/data_utils.h+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/datand2nzS1.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/datand2nzS1.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/datand2nzS2.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/datand2nzS2.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/nd2nz.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/nd2nz.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/s1bank2201.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/s1bank2201.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/s1bank3510.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/s1bank3510.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/s2bank2201.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/s2bank2201.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/s2bank3510.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/s2bank3510.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/ubBankStruct2201.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/ubBankStruct2201.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/figures/ubBankStruct3510.png→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/figures/ubBankStruct3510.png+0-0
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/scripts/gen_data.py→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/scripts/gen_data.py+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict/bank_conflict_nd2nz/scripts/verify_result.py→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/bank_conflict_nd2nz/scripts/verify_result.py+0-0
File renamed without changes.
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/gm_address_conflict/CMakeLists.txt→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/CMakeLists.txt+40-15
| @@ -1,5 +1,5 @@ | |||
| 1 | # ---------------------------------------------------------------------------------------------------------- | 1 | # ---------------------------------------------------------------------------------------------------------- |
| 2 | -# Copyright (c) 2025 Huawei Technologies Co., Ltd. | 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 | 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. |
| @@ -11,28 +11,53 @@ | |||
| 11 | 11 | ||
| 12 | cmake_minimum_required(VERSION 3.16) | 12 | cmake_minimum_required(VERSION 3.16) |
| 13 | 13 | ||
| 14 | +set(CMAKE_ASC_ARCHITECTURES "dav-2201" CACHE STRING "NPU architecture: dav-2201, dav-3510") | ||
| 15 | +set(SCENARIO_NUM "1" CACHE STRING "Scenario number: UB supports 1-8, L1 supports 1-7") | ||
| 16 | +set(COPY_DST "UB" CACHE STRING "Copy destination: UB, L1") | ||
| 17 | + | ||
| 14 | find_package(ASC REQUIRED) | 18 | find_package(ASC REQUIRED) |
| 15 | 19 | ||
| 16 | project(kernel_samples LANGUAGES ASC CXX) | 20 | project(kernel_samples LANGUAGES ASC CXX) |
| 17 | 21 | ||
| 22 | +if(NOT COPY_DST STREQUAL "UB" AND NOT COPY_DST STREQUAL "L1") | ||
| 23 | + message(FATAL_ERROR "COPY_DST must be UB or L1, but got ${COPY_DST}") | ||
| 24 | +endif() | ||
| 25 | + | ||
| 26 | +if(COPY_DST STREQUAL "UB") | ||
| 27 | + if(NOT SCENARIO_NUM MATCHES "^[1-8]$") | ||
| 28 | + message(FATAL_ERROR "COPY_DST=UB supports SCENARIO_NUM 1 to 8, but got ${SCENARIO_NUM}") | ||
| 29 | + endif() | ||
| 30 | +elseif(COPY_DST STREQUAL "L1") | ||
| 31 | + if(NOT SCENARIO_NUM MATCHES "^[1-7]$") | ||
| 32 | + message(FATAL_ERROR "COPY_DST=L1 supports SCENARIO_NUM 1 to 7, but got ${SCENARIO_NUM}") | ||
| 33 | + endif() | ||
| 34 | +endif() | ||
| 35 | + | ||
| 18 | add_executable(demo | 36 | add_executable(demo |
| 19 | - gm_address_conflict.asc | 37 | + data_copy.asc |
| 20 | ) | 38 | ) |
| 21 | 39 | ||
| 22 | -target_link_libraries(demo PRIVATE | 40 | +message(STATUS "[INFO] SCENARIO_NUM is ${SCENARIO_NUM}") |
| 23 | - tiling_api | 41 | +message(STATUS "[INFO] COPY_DST is ${COPY_DST}") |
| 24 | - register | 42 | + |
| 25 | - platform | 43 | +if(COPY_DST STREQUAL "UB") |
| 26 | - m | 44 | + target_compile_definitions(demo PRIVATE COPY_DST_UB=1) |
| 27 | - dl | 45 | +elseif(COPY_DST STREQUAL "L1") |
| 46 | + target_compile_definitions(demo PRIVATE COPY_DST_L1=1) | ||
| 47 | +endif() | ||
| 48 | + | ||
| 49 | +target_compile_definitions(demo PRIVATE | ||
| 50 | + SCENARIO_NUM=${SCENARIO_NUM} | ||
| 28 | ) | 51 | ) |
| 29 | 52 | ||
| 30 | -# ====================================================================================== | 53 | +if(CMAKE_ASC_ARCHITECTURES STREQUAL "dav-3510") |
| 31 | -# NPU 编译选项配置 | 54 | + target_compile_definitions(demo PRIVATE ARCH_3510=1) |
| 32 | -# | 55 | +elseif(CMAKE_ASC_ARCHITECTURES STREQUAL "dav-2201") |
| 33 | -# 说明: | 56 | + target_compile_definitions(demo PRIVATE ARCH_2201=1) |
| 34 | -# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | 57 | +else() |
| 35 | -# ====================================================================================== | 58 | + message(FATAL_ERROR "Unsupported CMAKE_ASC_ARCHITECTURES: ${CMAKE_ASC_ARCHITECTURES}") |
| 59 | +endif() | ||
| 60 | + | ||
| 36 | target_compile_options(demo PRIVATE | 61 | target_compile_options(demo PRIVATE |
| 37 | - $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-2201> | 62 | + $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=${CMAKE_ASC_ARCHITECTURES}> |
| 38 | ) | 63 | ) |
Aexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/README.md+448-0File content is under review, please try again later
Aexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/data_copy.asc+183-0
| @@ -0,0 +1,183 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under 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 data_copy.asc | ||
| 13 | + * \brief DataCopy best practices | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "data_copy_l1.h" | ||
| 17 | +#include "data_copy_ub.h" | ||
| 18 | +#include "data_utils.h" | ||
| 19 | + | ||
| 20 | +#ifdef ASCENDC_CPU_DEBUG | ||
| 21 | +#include "cpu_debug_launch.h" | ||
| 22 | +#endif | ||
| 23 | + | ||
| 24 | +constexpr uint32_t scenarioNum = SCENARIO_NUM; | ||
| 25 | + | ||
| 26 | +#if defined(ARCH_3510) | ||
| 27 | +constexpr uint32_t AIV_CORE_NUM = 64; | ||
| 28 | +constexpr uint32_t AIC_CORE_NUM = 32; | ||
| 29 | +constexpr uint32_t UB_ADDR_CONFLICT_M = 8192; | ||
| 30 | +constexpr uint32_t L1_ADDR_CONFLICT_M = 8192; | ||
| 31 | +#else | ||
| 32 | +constexpr uint32_t AIV_CORE_NUM = 48; | ||
| 33 | +constexpr uint32_t AIC_CORE_NUM = 24; | ||
| 34 | +constexpr uint32_t UB_ADDR_CONFLICT_M = 6144; | ||
| 35 | +constexpr uint32_t L1_ADDR_CONFLICT_M = 6144; | ||
| 36 | +#endif | ||
| 37 | + | ||
| 38 | +template <uint32_t mValue, uint32_t nValue, uint32_t numBlocksValue, uint32_t tileMValue, uint32_t tileNValue> | ||
| 39 | +struct ScenarioConfig { | ||
| 40 | + static constexpr uint32_t M = mValue; | ||
| 41 | + static constexpr uint32_t N = nValue; | ||
| 42 | + static constexpr uint32_t NUM_BLOCKS = numBlocksValue; | ||
| 43 | + static constexpr uint32_t SINGLE_CORE_M = M / NUM_BLOCKS; | ||
| 44 | + static constexpr uint32_t TILE_M = tileMValue; | ||
| 45 | + static constexpr uint32_t TILE_N = tileNValue; | ||
| 46 | +}; | ||
| 47 | + | ||
| 48 | +template <uint32_t scenario> | ||
| 49 | +struct UbScenario; | ||
| 50 | + | ||
| 51 | +// UB 场景:1~4 为分块和尾块,5~6 为重复搬运,7~8 为同地址冲突。 | ||
| 52 | +template <> | ||
| 53 | +struct UbScenario<1> : ScenarioConfig<12288, 12288, AIV_CORE_NUM, 1, 64> {}; | ||
| 54 | +template <> | ||
| 55 | +struct UbScenario<2> : ScenarioConfig<12288, 12288, AIV_CORE_NUM, 64, 64> {}; | ||
| 56 | +template <> | ||
| 57 | +struct UbScenario<3> : ScenarioConfig<12288, 12288, AIV_CORE_NUM, 64, 1024> {}; | ||
| 58 | +template <> | ||
| 59 | +struct UbScenario<4> : ScenarioConfig<12288, 12287, AIV_CORE_NUM, 64, 1024> {}; | ||
| 60 | +template <> | ||
| 61 | +struct UbScenario<5> : ScenarioConfig<12288, 12288, AIV_CORE_NUM, 64, 1024> {}; | ||
| 62 | +template <> | ||
| 63 | +struct UbScenario<6> : ScenarioConfig<12288, 12288, AIV_CORE_NUM, 64, 1024> {}; | ||
| 64 | +template <> | ||
| 65 | +struct UbScenario<7> : ScenarioConfig<UB_ADDR_CONFLICT_M, 512, AIV_CORE_NUM, 128, 64> {}; | ||
| 66 | +template <> | ||
| 67 | +struct UbScenario<8> : ScenarioConfig<UB_ADDR_CONFLICT_M, 512, AIV_CORE_NUM, 128, 64> {}; | ||
| 68 | + | ||
| 69 | +template <uint32_t scenario> | ||
| 70 | +struct L1Scenario; | ||
| 71 | + | ||
| 72 | +// L1 场景:1~3 为分块和尾块,4~5 为重复搬运,6~7 为同地址冲突。 | ||
| 73 | +template <> | ||
| 74 | +struct L1Scenario<1> : ScenarioConfig<12288, 12288, AIC_CORE_NUM, 64, 64> {}; | ||
| 75 | +template <> | ||
| 76 | +struct L1Scenario<2> : ScenarioConfig<12288, 12288, AIC_CORE_NUM, 64, 256> {}; | ||
| 77 | +template <> | ||
| 78 | +struct L1Scenario<3> : ScenarioConfig<12288, 12287, AIC_CORE_NUM, 64, 256> {}; | ||
| 79 | +template <> | ||
| 80 | +struct L1Scenario<4> : ScenarioConfig<12288, 12288, AIC_CORE_NUM, 64, 1024> {}; | ||
| 81 | +template <> | ||
| 82 | +struct L1Scenario<5> : ScenarioConfig<12288, 12288, AIC_CORE_NUM, 64, 1024> {}; | ||
| 83 | +template <> | ||
| 84 | +struct L1Scenario<6> : ScenarioConfig<L1_ADDR_CONFLICT_M, 512, AIC_CORE_NUM, 256, 64> {}; | ||
| 85 | +template <> | ||
| 86 | +struct L1Scenario<7> : ScenarioConfig<L1_ADDR_CONFLICT_M, 512, AIC_CORE_NUM, 256, 64> {}; | ||
| 87 | + | ||
| 88 | +#ifdef COPY_DST_UB | ||
| 89 | +using CurrentScenario = UbScenario<scenarioNum>; | ||
| 90 | +#elif defined(COPY_DST_L1) | ||
| 91 | +using CurrentScenario = L1Scenario<scenarioNum>; | ||
| 92 | +#endif | ||
| 93 | + | ||
| 94 | +// 编译期固定当前场景参数,运行时只需要按输入数据执行对应 kernel。 | ||
| 95 | +constexpr uint32_t M = CurrentScenario::M; | ||
| 96 | +constexpr uint32_t N = CurrentScenario::N; | ||
| 97 | +constexpr uint32_t NUM_BLOCKS = CurrentScenario::NUM_BLOCKS; | ||
| 98 | +constexpr uint32_t SINGLE_CORE_M = CurrentScenario::SINGLE_CORE_M; | ||
| 99 | +constexpr uint32_t TILE_M = CurrentScenario::TILE_M; | ||
| 100 | +constexpr uint32_t TILE_N = CurrentScenario::TILE_N; | ||
| 101 | +constexpr size_t FILE_SIZE = M * N * sizeof(half); | ||
| 102 | + | ||
| 103 | +template <uint32_t mValue, uint32_t nValue, uint32_t numBlocksValue, uint32_t singleCoreMValue, | ||
| 104 | + uint32_t tileMValue, uint32_t tileNValue> | ||
| 105 | +__global__ __vector__ void kernel_data_copy_pad_gm2ub(GM_ADDR src) | ||
| 106 | +{ | ||
| 107 | + AscendC::InitSocState(); | ||
| 108 | + KernelDataCopyPadGm2Ub<mValue, nValue, numBlocksValue, singleCoreMValue, tileMValue, tileNValue> op; | ||
| 109 | + op.Init(reinterpret_cast<__gm__ half*>(src)); | ||
| 110 | + if constexpr (scenarioNum == 5) { | ||
| 111 | + op.ProcessRepeatWhole4Times(); | ||
| 112 | + } else if constexpr (scenarioNum == 6) { | ||
| 113 | + op.ProcessSplitFourAndRepeat4Times(); | ||
| 114 | + } else if constexpr (scenarioNum == 7) { | ||
| 115 | + op.ProcessFullMatrixSameAddr(); | ||
| 116 | + } else if constexpr (scenarioNum == 8) { | ||
| 117 | + op.ProcessFullMatrixOffsetAddr(); | ||
| 118 | + } else { | ||
| 119 | + op.Process(); | ||
| 120 | + } | ||
| 121 | + AscendC::PipeBarrier<PIPE_ALL>(); | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +template <uint32_t mValue, uint32_t nValue, uint32_t numBlocksValue, uint32_t singleCoreMValue, | ||
| 125 | + uint32_t tileMValue, uint32_t tileNValue> | ||
| 126 | +__global__ __cube__ void kernel_data_copy_gm2l1(GM_ADDR src) | ||
| 127 | +{ | ||
| 128 | + AscendC::InitSocState(); | ||
| 129 | + KernelDataCopyGm2L1<mValue, nValue, numBlocksValue, singleCoreMValue, tileMValue, tileNValue> op; | ||
| 130 | + op.Init(reinterpret_cast<__gm__ half*>(src)); | ||
| 131 | + if constexpr (scenarioNum == 4) { | ||
| 132 | + op.ProcessRepeatWhole4Times(); | ||
| 133 | + } else if constexpr (scenarioNum == 5) { | ||
| 134 | + op.ProcessSplitFourAndRepeat4Times(); | ||
| 135 | + } else if constexpr (scenarioNum == 6) { | ||
| 136 | + op.ProcessFullMatrixSameAddr(); | ||
| 137 | + } else if constexpr (scenarioNum == 7) { | ||
| 138 | + op.ProcessFullMatrixOffsetAddr(); | ||
| 139 | + } else { | ||
| 140 | + op.Process(); | ||
| 141 | + } | ||
| 142 | + AscendC::PipeBarrier<PIPE_ALL>(); | ||
| 143 | +} | ||
| 144 | + | ||
| 145 | +int32_t main(int32_t argc, char* argv[]) | ||
| 146 | +{ | ||
| 147 | + size_t fileSize = FILE_SIZE; | ||
| 148 | + uint32_t deviceId = 0; | ||
| 149 | + aclrtStream stream = nullptr; | ||
| 150 | + aclrtContext context; | ||
| 151 | + | ||
| 152 | + aclInit(nullptr); | ||
| 153 | + aclrtSetDevice(deviceId); | ||
| 154 | + aclrtCreateContext(&context, deviceId); | ||
| 155 | + aclrtCreateStream(&stream); | ||
| 156 | + | ||
| 157 | + uint8_t* inputHost; | ||
| 158 | + uint8_t* inputDevice; | ||
| 159 | + aclrtMallocHost((void**)(&inputHost), fileSize); | ||
| 160 | + aclrtMalloc((void**)&inputDevice, fileSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 161 | + ReadFile("./input/input.bin", fileSize, inputHost, fileSize); | ||
| 162 | + aclrtMemcpy(inputDevice, fileSize, inputHost, fileSize, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 163 | + | ||
| 164 | +#ifdef COPY_DST_UB | ||
| 165 | + kernel_data_copy_pad_gm2ub<M, N, NUM_BLOCKS, SINGLE_CORE_M, TILE_M, TILE_N> | ||
| 166 | + <<<NUM_BLOCKS, nullptr, stream>>>(inputDevice); | ||
| 167 | +#elif defined(COPY_DST_L1) | ||
| 168 | + kernel_data_copy_gm2l1<M, N, NUM_BLOCKS, SINGLE_CORE_M, TILE_M, TILE_N> | ||
| 169 | + <<<NUM_BLOCKS, nullptr, stream>>>(inputDevice); | ||
| 170 | +#endif | ||
| 171 | + | ||
| 172 | + aclrtSynchronizeStream(stream); | ||
| 173 | + | ||
| 174 | + aclrtFree(inputDevice); | ||
| 175 | + aclrtFreeHost(inputHost); | ||
| 176 | + | ||
| 177 | + aclrtDestroyStream(stream); | ||
| 178 | + aclrtDestroyContext(context); | ||
| 179 | + aclrtResetDevice(deviceId); | ||
| 180 | + aclFinalize(); | ||
| 181 | + | ||
| 182 | + return 0; | ||
| 183 | +} | ||
Aexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/data_copy_l1.h+170-0File content is under review, please try again later
Aexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/data_copy_ub.h+151-0File content is under review, please try again later
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/gm_address_conflict/data_utils.h→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/data_utils.h+39-31File content is under review, please try again later
Rexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/optimize_datacopy/optimize_datacopy_nddma/scripts/gen_data.py→examples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/data_copy/scripts/gen_data.py+63-11File content is under review, please try again later
Dexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/gm_address_conflict/README.md+0-91
| @@ -1,91 +0,0 @@ | |||
| 1 | -# 同地址冲突算子直调样例 | ||
| 2 | -## 概述 | ||
| 3 | - | ||
| 4 | -本样例介绍了同地址冲突的影响以及两种解决方法,并提供核函数直调方法。 | ||
| 5 | - | ||
| 6 | -## 支持的产品 | ||
| 7 | -- Atlas A3 训练系列产品/Atlas A3 推理系列产品 | ||
| 8 | -- Atlas A2 训练系列产品/Atlas A2 推理系列产品 | ||
| 9 | - | ||
| 10 | -## 目录结构介绍 | ||
| 11 | -``` | ||
| 12 | -├── gm_address_conflict | ||
| 13 | -│ ├── scripts | ||
| 14 | -│ │ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 15 | -│ │ └── verify_result.py // 验证输出数据和真值数据是否一致的验证脚本 | ||
| 16 | -│ ├── CMakeLists.txt // 编译工程文件 | ||
| 17 | -│ ├── data_utils.h // 数据读入写出函数 | ||
| 18 | -│ └── gm_address_conflict.asc // Ascend C算子实现 & 调用样例 | ||
| 19 | -``` | ||
| 20 | - | ||
| 21 | -## 算子描述 | ||
| 22 | -- 算子功能: | ||
| 23 | - Adds算子实现了一个Tensor与标量值2.0相加,返回相加结果的功能。 | ||
| 24 | - | ||
| 25 | - 对应的数学表达式为: | ||
| 26 | - | ||
| 27 | - ```python | ||
| 28 | - z = x + 2.0 | ||
| 29 | - ``` | ||
| 30 | - | ||
| 31 | - - x:输入,形状为\[8192, 128],数据类型为float; | ||
| 32 | - - z:输出,形状为\[8192, 128],数据类型为float; | ||
| 33 | - | ||
| 34 | -- 算子规格: | ||
| 35 | - | ||
| 36 | -<table> | ||
| 37 | -<tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="4" align="center">Adds</td></tr> | ||
| 38 | -<tr><td rowspan="2" 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> | ||
| 39 | -<tr><td align="center">x</td><td align="center">8192 * 128</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 40 | -<tr><td rowspan="1" align="center">算子输出</td><td align="center">z</td><td align="center">8192 * 128</td><td align="center">float</td><td align="center">ND</td></tr> | ||
| 41 | -<tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">adds_custom_v1/adds_custom_v2/adds_custom_v3</td></tr> | ||
| 42 | -</table> | ||
| 43 | - | ||
| 44 | - | ||
| 45 | -- 算子实现: | ||
| 46 | - | ||
| 47 | - - kernel实现 | ||
| 48 | - | ||
| 49 | - 计算逻辑是:本样例主要介绍数据搬运中的同地址冲突对搬运效率的影响,在Global Memory的数据访问中,数据访问请求(读/写)在AI 处理器内部会按照512 Bytes对齐进行地址转换,同一时刻如果多核的数据访问请求在转换后落在连续的512 Bytes范围内,出于数据一致性的要求,AI 处理器会对落入同一个512Bytes范围内的请求进行串行处理,导致搬运效率降低,即发生了同地址访问现象。 | ||
| 50 | - | ||
| 51 | - 本样例中共有3个实现版本: | ||
| 52 | - adds_custom_v1:基础实现版本,每个核的计算顺序一致,存在同地址冲突,带宽效率较差。 | ||
| 53 | - adds_custom_v2:通过调整每个核的计算顺序,避免发生同地址冲突。 | ||
| 54 | - adds_custom_v3:通过调整切分顺序,避免发生同地址冲突。 | ||
| 55 | - | ||
| 56 | - 当前算子执行机制保证用户kernel入参(包括workspace/tiling)的地址是512 Bytes对齐的,因此用户只需要根据地址的偏移量即可判断两个地址是否会落入连续的512 Bytes范围内。 | ||
| 57 | - | ||
| 58 | - | ||
| 59 | -## 编译运行 | ||
| 60 | -在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 61 | -- 配置环境变量 | ||
| 62 | - 请根据当前环境上CANN开发套件包的[安装方式](../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 63 | - - 默认路径,root用户安装CANN软件包 | ||
| 64 | - ```bash | ||
| 65 | - source /usr/local/Ascend/cann/set_env.sh | ||
| 66 | - ``` | ||
| 67 | - | ||
| 68 | - - 默认路径,非root用户安装CANN软件包 | ||
| 69 | - ```bash | ||
| 70 | - source $HOME/Ascend/cann/set_env.sh | ||
| 71 | - ``` | ||
| 72 | - | ||
| 73 | - - 指定路径install_path,安装CANN软件包 | ||
| 74 | - ```bash | ||
| 75 | - source ${install_path}/cann/set_env.sh | ||
| 76 | - ``` | ||
| 77 | - | ||
| 78 | -- 样例执行 | ||
| 79 | - ```bash | ||
| 80 | - mkdir -p build && cd build; # 创建并进入build目录 | ||
| 81 | - cmake ..;make -j; # 编译工程 | ||
| 82 | - python3 ../scripts/gen_data.py # 生成测试输入数据 | ||
| 83 | - ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 84 | - python3 ../scripts/verify_result.py output/output_z_1.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 85 | - python3 ../scripts/verify_result.py output/output_z_2.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 86 | - python3 ../scripts/verify_result.py output/output_z_3.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 87 | - ``` | ||
| 88 | - 执行结果如下,说明精度对比成功。 | ||
| 89 | - ```bash | ||
| 90 | - test pass! | ||
| 91 | - ``` | ||
| @@ -1,379 +0,0 @@ | |||
| 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 gm_address_conflict.asc | ||
| 14 | - * \brief | ||
| 15 | - */ | ||
| 16 | -#include <cstdint> | ||
| 17 | -#include "data_utils.h" | ||
| 18 | -#include "kernel_operator.h" | ||
| 19 | -#include "acl/acl.h" | ||
| 20 | - | ||
| 21 | -constexpr uint32_t TILINGDATA_SIZE = 5; | ||
| 22 | -constexpr uint32_t TILING_KEY_IS = 3; | ||
| 23 | -constexpr uint32_t USED_CORE_NUM = 16; | ||
| 24 | -struct AddsCustomTilingData | ||
| 25 | -{ | ||
| 26 | - uint32_t m; | ||
| 27 | - uint32_t n; | ||
| 28 | - uint32_t tileM; | ||
| 29 | - uint32_t tileN; | ||
| 30 | - uint32_t loopOneCore; | ||
| 31 | -}; | ||
| 32 | - | ||
| 33 | - | ||
| 34 | -bool GenerateTiling(uint8_t *tilingBuf) | ||
| 35 | -{ | ||
| 36 | - | ||
| 37 | - AddsCustomTilingData *tiling = reinterpret_cast<AddsCustomTilingData *>(tilingBuf); | ||
| 38 | - constexpr uint32_t M = 8192; | ||
| 39 | - constexpr uint32_t N = 128; | ||
| 40 | - constexpr uint32_t TILE_M = 512; | ||
| 41 | - constexpr uint32_t TILE_N = 8; | ||
| 42 | - constexpr uint32_t LOOP_ONE_CORE = M / TILE_M; | ||
| 43 | - tiling->m = M; | ||
| 44 | - tiling->n = N; | ||
| 45 | - tiling->tileM = TILE_M; | ||
| 46 | - tiling->tileN = TILE_N; | ||
| 47 | - tiling->loopOneCore = LOOP_ONE_CORE; | ||
| 48 | - return true; | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | - | ||
| 52 | -using AscendC::TPosition; | ||
| 53 | -class KernelAddsV1 { | ||
| 54 | -public: | ||
| 55 | - __aicore__ inline KernelAddsV1() {} | ||
| 56 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* z, AddsCustomTilingData *tilingPtr, AscendC::TPipe* pipeIn) | ||
| 57 | - { | ||
| 58 | - tiling = tilingPtr; | ||
| 59 | - pipe = pipeIn; | ||
| 60 | - xGm.SetGlobalBuffer((__gm__ float *)x + AscendC::GetBlockIdx() * tiling->tileN); | ||
| 61 | - zGm.SetGlobalBuffer((__gm__ float *)z + AscendC::GetBlockIdx() * tiling->tileN); | ||
| 62 | - // the gm address conflict happens when multi cores visit the same addr range(512Bytes) | ||
| 63 | - // we disable the L2 cache mode to highlight the influence of the gm address conflict | ||
| 64 | - xGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 65 | - zGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 66 | - pipe->InitBuffer(inQueueX, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 67 | - pipe->InitBuffer(outQueueZ, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 68 | - } | ||
| 69 | - __aicore__ inline void Process() | ||
| 70 | - { | ||
| 71 | - for (int32_t i = 0; i < tiling->loopOneCore; i++) { | ||
| 72 | - // the following two SyncAll in this case are unnecessary actually, | ||
| 73 | - // we just used them to highlight the influence of gm address conflict in each loop | ||
| 74 | - AscendC::SyncAll(); | ||
| 75 | - CopyIn(i); | ||
| 76 | - Compute(); | ||
| 77 | - AscendC::SyncAll(); | ||
| 78 | - CopyOut(i); | ||
| 79 | - } | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | -private: | ||
| 83 | - __aicore__ inline void CopyIn(int32_t progress) | ||
| 84 | - { | ||
| 85 | - AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>(); | ||
| 86 | - AscendC::DataCopyParams params; | ||
| 87 | - params.blockCount = tiling->tileM; | ||
| 88 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 89 | - params.srcStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 90 | - params.dstStride = 0; | ||
| 91 | - AscendC::DataCopy(xLocal, xGm[progress * tiling->tileM * tiling->n], params); | ||
| 92 | - inQueueX.EnQue(xLocal); | ||
| 93 | - } | ||
| 94 | - __aicore__ inline void Compute() | ||
| 95 | - { | ||
| 96 | - AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>(); | ||
| 97 | - AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>(); | ||
| 98 | - constexpr float scale = 2.0; | ||
| 99 | - AscendC::Adds(zLocal, xLocal, scale, tiling->tileM * tiling->tileN); | ||
| 100 | - outQueueZ.EnQue<float>(zLocal); | ||
| 101 | - inQueueX.FreeTensor(xLocal); | ||
| 102 | - } | ||
| 103 | - __aicore__ inline void CopyOut(int32_t progress) | ||
| 104 | - { | ||
| 105 | - AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>(); | ||
| 106 | - AscendC::DataCopyParams params; | ||
| 107 | - params.blockCount = tiling->tileM; | ||
| 108 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 109 | - params.srcStride = 0; | ||
| 110 | - params.dstStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 111 | - AscendC::DataCopy(zGm[progress * tiling->tileM * tiling->n], zLocal, params); | ||
| 112 | - outQueueZ.FreeTensor(zLocal); | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | -private: | ||
| 116 | - static constexpr int32_t BUFFER_NUM = 2; | ||
| 117 | - static constexpr int32_t BLOCK_SIZE = 32; | ||
| 118 | - | ||
| 119 | - AscendC::TPipe* pipe; | ||
| 120 | - AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX; | ||
| 121 | - AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ; | ||
| 122 | - AscendC::GlobalTensor<float> xGm; | ||
| 123 | - AscendC::GlobalTensor<float> zGm; | ||
| 124 | - AddsCustomTilingData *tiling; | ||
| 125 | -}; | ||
| 126 | - | ||
| 127 | -class KernelAddsV2 { | ||
| 128 | -public: | ||
| 129 | - __aicore__ inline KernelAddsV2() {} | ||
| 130 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* z, AddsCustomTilingData *tilingPtr, AscendC::TPipe* pipeIn) | ||
| 131 | - { | ||
| 132 | - tiling = tilingPtr; | ||
| 133 | - pipe = pipeIn; | ||
| 134 | - xGm.SetGlobalBuffer((__gm__ float *)x + AscendC::GetBlockIdx() * tiling->tileN); | ||
| 135 | - zGm.SetGlobalBuffer((__gm__ float *)z + AscendC::GetBlockIdx() * tiling->tileN); | ||
| 136 | - // the gm address conflict happens when multi cores visit the same addr range(512Bytes) | ||
| 137 | - // we disable the L2 cache mode to highlight the influence of the gm address conflict | ||
| 138 | - xGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 139 | - zGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 140 | - pipe->InitBuffer(inQueueX, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 141 | - pipe->InitBuffer(outQueueZ, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 142 | - } | ||
| 143 | - __aicore__ inline void Process() | ||
| 144 | - { | ||
| 145 | - for (int32_t i = 0; i < tiling->loopOneCore; i++) { | ||
| 146 | - // adjust the loop order to avoid the gm address conflict: | ||
| 147 | - // the loop order of core0 : 0, 1, 2, 3, ..., 13, 14, 15 | ||
| 148 | - // the loop order of core1 : 1, 2, 3, 4, ..., 14, 15, 0 | ||
| 149 | - // ... | ||
| 150 | - // the loop order of core15 : 15, 0, 1, 2, ..., 12, 13, 14 | ||
| 151 | - int32_t newProgress = (i + AscendC::GetBlockIdx()) % tiling->loopOneCore; | ||
| 152 | - // the following two SyncAll in this case are unnecessary actually, | ||
| 153 | - // we just used them to highlight the influence of gm address conflict in each loop | ||
| 154 | - AscendC::SyncAll(); | ||
| 155 | - CopyIn(newProgress); | ||
| 156 | - Compute(); | ||
| 157 | - AscendC::SyncAll(); | ||
| 158 | - CopyOut(newProgress); | ||
| 159 | - } | ||
| 160 | - } | ||
| 161 | - | ||
| 162 | -private: | ||
| 163 | - __aicore__ inline void CopyIn(int32_t progress) | ||
| 164 | - { | ||
| 165 | - AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>(); | ||
| 166 | - AscendC::DataCopyParams params; | ||
| 167 | - params.blockCount = tiling->tileM; | ||
| 168 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 169 | - params.srcStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 170 | - params.dstStride = 0; | ||
| 171 | - AscendC::DataCopy(xLocal, xGm[progress * tiling->tileM * tiling->n], params); | ||
| 172 | - inQueueX.EnQue(xLocal); | ||
| 173 | - } | ||
| 174 | - __aicore__ inline void Compute() | ||
| 175 | - { | ||
| 176 | - AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>(); | ||
| 177 | - AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>(); | ||
| 178 | - constexpr float scale = 2.0; | ||
| 179 | - AscendC::Adds(zLocal, xLocal, scale, tiling->tileM * tiling->tileN); | ||
| 180 | - outQueueZ.EnQue<float>(zLocal); | ||
| 181 | - inQueueX.FreeTensor(xLocal); | ||
| 182 | - } | ||
| 183 | - __aicore__ inline void CopyOut(int32_t progress) | ||
| 184 | - { | ||
| 185 | - AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>(); | ||
| 186 | - AscendC::DataCopyParams params; | ||
| 187 | - params.blockCount = tiling->tileM; | ||
| 188 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 189 | - params.srcStride = 0; | ||
| 190 | - params.dstStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 191 | - AscendC::DataCopy(zGm[progress * tiling->tileM * tiling->n], zLocal, params); | ||
| 192 | - outQueueZ.FreeTensor(zLocal); | ||
| 193 | - } | ||
| 194 | - | ||
| 195 | -private: | ||
| 196 | - static constexpr int32_t BUFFER_NUM = 2; | ||
| 197 | - static constexpr int32_t BLOCK_SIZE = 32; | ||
| 198 | - | ||
| 199 | - AscendC::TPipe* pipe; | ||
| 200 | - AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX; | ||
| 201 | - AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ; | ||
| 202 | - AscendC::GlobalTensor<float> xGm; | ||
| 203 | - AscendC::GlobalTensor<float> zGm; | ||
| 204 | - AddsCustomTilingData *tiling; | ||
| 205 | -}; | ||
| 206 | - | ||
| 207 | -class KernelAddsV3 { | ||
| 208 | -public: | ||
| 209 | - __aicore__ inline KernelAddsV3() {} | ||
| 210 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* z, AddsCustomTilingData *tilingPtr, AscendC::TPipe* pipeIn) | ||
| 211 | - { | ||
| 212 | - tiling = tilingPtr; | ||
| 213 | - pipe = pipeIn; | ||
| 214 | - // change the tile method from column split to row split | ||
| 215 | - xGm.SetGlobalBuffer((__gm__ float *)x + AscendC::GetBlockIdx() * tiling->tileM * tiling->n); | ||
| 216 | - zGm.SetGlobalBuffer((__gm__ float *)z + AscendC::GetBlockIdx() * tiling->tileM * tiling->n); | ||
| 217 | - // the gm address conflict happens when multi cores visit the same addr range(512Bytes) | ||
| 218 | - // we disable the L2 cache mode to highlight the influence of the gm address conflict | ||
| 219 | - xGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 220 | - zGm.SetL2CacheHint(AscendC::CacheMode::CACHE_MODE_DISABLE); | ||
| 221 | - pipe->InitBuffer(inQueueX, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 222 | - pipe->InitBuffer(outQueueZ, BUFFER_NUM, tiling->tileM * tiling->tileN * sizeof(float)); | ||
| 223 | - } | ||
| 224 | - __aicore__ inline void Process() | ||
| 225 | - { | ||
| 226 | - for (int32_t i = 0; i < tiling->loopOneCore; i++) { | ||
| 227 | - // the following two SyncAll in this case are unnecessary actually, | ||
| 228 | - // we just used them to highlight the influence of gm address conflict in each loop | ||
| 229 | - AscendC::SyncAll(); | ||
| 230 | - CopyIn(i); | ||
| 231 | - Compute(); | ||
| 232 | - AscendC::SyncAll(); | ||
| 233 | - CopyOut(i); | ||
| 234 | - } | ||
| 235 | - } | ||
| 236 | - | ||
| 237 | -private: | ||
| 238 | - __aicore__ inline void CopyIn(int32_t progress) | ||
| 239 | - { | ||
| 240 | - AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>(); | ||
| 241 | - AscendC::DataCopyParams params; | ||
| 242 | - params.blockCount = tiling->tileM; | ||
| 243 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 244 | - params.srcStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 245 | - params.dstStride = 0; | ||
| 246 | - AscendC::DataCopy(xLocal, xGm[progress * tiling->tileN], params); | ||
| 247 | - inQueueX.EnQue(xLocal); | ||
| 248 | - } | ||
| 249 | - __aicore__ inline void Compute() | ||
| 250 | - { | ||
| 251 | - AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>(); | ||
| 252 | - AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>(); | ||
| 253 | - constexpr float scale = 2.0; | ||
| 254 | - AscendC::Adds(zLocal, xLocal, scale, tiling->tileM * tiling->tileN); | ||
| 255 | - outQueueZ.EnQue<float>(zLocal); | ||
| 256 | - inQueueX.FreeTensor(xLocal); | ||
| 257 | - } | ||
| 258 | - __aicore__ inline void CopyOut(int32_t progress) | ||
| 259 | - { | ||
| 260 | - AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>(); | ||
| 261 | - AscendC::DataCopyParams params; | ||
| 262 | - params.blockCount = tiling->tileM; | ||
| 263 | - params.blockLen = tiling->tileN * sizeof(float) / BLOCK_SIZE; | ||
| 264 | - params.srcStride = 0; | ||
| 265 | - params.dstStride = (tiling->n - tiling->tileN) * sizeof(float) / BLOCK_SIZE; | ||
| 266 | - AscendC::DataCopy(zGm[progress * tiling->tileN], zLocal, params); | ||
| 267 | - outQueueZ.FreeTensor(zLocal); | ||
| 268 | - } | ||
| 269 | - | ||
| 270 | -private: | ||
| 271 | - static constexpr int32_t BUFFER_NUM = 2; | ||
| 272 | - static constexpr int32_t BLOCK_SIZE = 32; | ||
| 273 | - | ||
| 274 | - AscendC::TPipe* pipe; | ||
| 275 | - AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX; | ||
| 276 | - AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ; | ||
| 277 | - AscendC::GlobalTensor<float> xGm; | ||
| 278 | - AscendC::GlobalTensor<float> zGm; | ||
| 279 | - AddsCustomTilingData *tiling; | ||
| 280 | -}; | ||
| 281 | - | ||
| 282 | -extern "C" __global__ __mix__(0, 1) void adds_custom_v1(__gm__ uint8_t* x, __gm__ uint8_t* z, __gm__ uint8_t* workspace, AddsCustomTilingData tiling) | ||
| 283 | -{ | ||
| 284 | - AscendC::TPipe pipe; | ||
| 285 | - REGISTER_TILING_DEFAULT(AddsCustomTilingData); | ||
| 286 | - KernelAddsV1 op; | ||
| 287 | - op.Init(x, z, &tiling, &pipe); | ||
| 288 | - op.Process(); | ||
| 289 | -} | ||
| 290 | - | ||
| 291 | -extern "C" __global__ __mix__(0, 1) void adds_custom_v2(__gm__ uint8_t* x, __gm__ uint8_t* z, __gm__ uint8_t* workspace, AddsCustomTilingData tiling) | ||
| 292 | -{ | ||
| 293 | - AscendC::TPipe pipe; | ||
| 294 | - REGISTER_TILING_DEFAULT(AddsCustomTilingData); | ||
| 295 | - KernelAddsV2 op; | ||
| 296 | - op.Init(x, z, &tiling, &pipe); | ||
| 297 | - op.Process(); | ||
| 298 | -} | ||
| 299 | - | ||
| 300 | -extern "C" __global__ __mix__(0, 1) void adds_custom_v3(__gm__ uint8_t* x, __gm__ uint8_t* z, __gm__ uint8_t* workspace, AddsCustomTilingData tiling) | ||
| 301 | -{ | ||
| 302 | - AscendC::TPipe pipe; | ||
| 303 | - REGISTER_TILING_DEFAULT(AddsCustomTilingData); | ||
| 304 | - KernelAddsV3 op; | ||
| 305 | - op.Init(x, z, &tiling, &pipe); | ||
| 306 | - op.Process(); | ||
| 307 | -} | ||
| 308 | - | ||
| 309 | -int32_t main(int32_t argc, char *argv[]) { | ||
| 310 | - uint8_t *tiling = nullptr; | ||
| 311 | - size_t tilingSize = TILINGDATA_SIZE * sizeof(uint32_t); | ||
| 312 | - | ||
| 313 | - aclInit(nullptr); | ||
| 314 | - int32_t deviceId = 0; | ||
| 315 | - aclrtSetDevice(deviceId); | ||
| 316 | - aclrtStream stream = nullptr; | ||
| 317 | - aclrtCreateStream(&stream); | ||
| 318 | - | ||
| 319 | - uint8_t *xHost, *yHost; | ||
| 320 | - uint8_t *xDevice, *yDevice; | ||
| 321 | - | ||
| 322 | - aclrtMallocHost((void **)(&tiling), tilingSize); | ||
| 323 | - | ||
| 324 | - GenerateTiling(tiling); | ||
| 325 | - | ||
| 326 | - auto tilingData = reinterpret_cast<AddsCustomTilingData *>(tiling); | ||
| 327 | - | ||
| 328 | - size_t inputSize = tilingData->m * tilingData->n * sizeof(float); | ||
| 329 | - size_t outputSize = tilingData->m * tilingData->n * sizeof(float); | ||
| 330 | - | ||
| 331 | - aclrtMallocHost((void **)(&xHost), inputSize); | ||
| 332 | - aclrtMallocHost((void **)(&yHost), outputSize); | ||
| 333 | - | ||
| 334 | - aclrtMalloc((void **)&xDevice, inputSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 335 | - aclrtMalloc((void **)&yDevice, outputSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 336 | - | ||
| 337 | - ReadFile("./input/input_x.bin", inputSize, xHost, inputSize); | ||
| 338 | - | ||
| 339 | - // Copy host memory to device memory | ||
| 340 | - aclrtMemcpy(xDevice, inputSize, xHost, inputSize, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 341 | - | ||
| 342 | - // Execute the kernel | ||
| 343 | - AddsCustomTilingData tiling_data; | ||
| 344 | - aclrtMemcpy(&tiling_data, sizeof(AddsCustomTilingData), tiling, sizeof(AddsCustomTilingData), ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 345 | - | ||
| 346 | - adds_custom_v1<<<USED_CORE_NUM, nullptr, stream>>>(xDevice, yDevice, 0, tiling_data); | ||
| 347 | - // Wait for the stop event to complete | ||
| 348 | - aclrtSynchronizeStream(stream); | ||
| 349 | - // Copy result to host memory and write to output file | ||
| 350 | - aclrtMemcpy(yHost, outputSize, yDevice, outputSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 351 | - WriteFile("./output/output_z_1.bin", yHost, outputSize); | ||
| 352 | - | ||
| 353 | - adds_custom_v2<<<USED_CORE_NUM, nullptr, stream>>>(xDevice, yDevice, 0, tiling_data); | ||
| 354 | - // Wait for the stop event to complete | ||
| 355 | - aclrtSynchronizeStream(stream); | ||
| 356 | - // Copy result to host memory and write to output file | ||
| 357 | - aclrtMemcpy(yHost, outputSize, yDevice, outputSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 358 | - WriteFile("./output/output_z_2.bin", yHost, outputSize); | ||
| 359 | - | ||
| 360 | - adds_custom_v3<<<USED_CORE_NUM, nullptr, stream>>>(xDevice, yDevice, 0, tiling_data); | ||
| 361 | - // Wait for the stop event to complete | ||
| 362 | - aclrtSynchronizeStream(stream); | ||
| 363 | - // Copy result to host memory and write to output file | ||
| 364 | - aclrtMemcpy(yHost, outputSize, yDevice, outputSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 365 | - WriteFile("./output/output_z_3.bin", yHost, outputSize); | ||
| 366 | - | ||
| 367 | - // Clean up memory | ||
| 368 | - aclrtFree(xDevice); | ||
| 369 | - aclrtFree(yDevice); | ||
| 370 | - aclrtFreeHost(xHost); | ||
| 371 | - aclrtFreeHost(yHost); | ||
| 372 | - aclrtFreeHost(tiling); | ||
| 373 | - | ||
| 374 | - aclrtDestroyStream(stream); | ||
| 375 | - aclrtResetDevice(deviceId); | ||
| 376 | - aclFinalize(); | ||
| 377 | - | ||
| 378 | - return 0; | ||
| 379 | -} | ||
| @@ -1,30 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 numpy as np | ||
| 16 | -import os | ||
| 17 | - | ||
| 18 | - | ||
| 19 | -def gen_golden_data_simple(): | ||
| 20 | - input_x = np.random.uniform(1, 100, [8192, 128]).astype(np.float32) | ||
| 21 | - golden = (input_x + 2.0).astype(np.float32) | ||
| 22 | - | ||
| 23 | - os.makedirs("input", exist_ok=True) | ||
| 24 | - os.makedirs("output", exist_ok=True) | ||
| 25 | - input_x.tofile("./input/input_x.bin") | ||
| 26 | - golden.tofile("./output/golden.bin") | ||
| 27 | - | ||
| 28 | - | ||
| 29 | -if __name__ == "__main__": | ||
| 30 | - gen_golden_data_simple() | ||
| @@ -1,57 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 | -# for float32 | ||
| 19 | -relative_tol = 1e-4 | ||
| 20 | -absolute_tol = 1e-5 | ||
| 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 | - error_ratio = float(different_element_indexes.size) / golden.size | ||
| 44 | - print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, error_tol)) | ||
| 45 | - return error_ratio <= error_tol | ||
| 46 | - | ||
| 47 | - | ||
| 48 | -if __name__ == '__main__': | ||
| 49 | - try: | ||
| 50 | - res = verify_result(sys.argv[1], sys.argv[2]) | ||
| 51 | - if not res: | ||
| 52 | - raise ValueError("[ERROR] result error") | ||
| 53 | - else: | ||
| 54 | - print("test pass!") | ||
| 55 | - except Exception as e: | ||
| 56 | - print(e) | ||
| 57 | - sys.exit(1) | ||
Dexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/optimize_datacopy/README.md+0-13
| @@ -1,13 +0,0 @@ | |||
| 1 | -# Optimize Datacopy样例介绍 | ||
| 2 | - | ||
| 3 | -## 概述 | ||
| 4 | - | ||
| 5 | -基于搬运类API使用的优化样例,通过<<<>>>直调的实现方式,介绍了减少无效数据搬运、减少搬运指令数量等方法。 | ||
| 6 | - | ||
| 7 | -## 样例列表 | ||
| 8 | - | ||
| 9 | -| 目录名称 | 功能描述 | 支持的产品 | | ||
| 10 | -| -------------------------------------------------- | ---------------------------------------------------- | --- | | ||
| 11 | -| [optimize_datacopy_loop_mode](./optimize_datacopy_loop_mode) | 在使用DataCopyPad接口时,使用loop模式减少DataCopyPad指令的条数。 | Ascend 950PR/Ascend 950DT | | ||
| 12 | -| [optimize_datacopy_nddma](./optimize_datacopy_nddma) | 在进行非对齐数据搬运时,使用nddma搬运减少搬运指令的条数。 | Ascend 950PR/Ascend 950DT | | ||
| 13 | -| [optimize_reduce_invalid_datacopy](./optimize_reduce_invalid_datacopy) | 在使用DataCopyPad接口时,通过设置Compact模式减少无效数据的搬运。 | Ascend 950PR/Ascend 950DT | | ||
Dexamples/01_simd_cpp_api/04_best_practices/04_memory_access_practices/optimize_datacopy/README_en.md+0-13
| @@ -1,13 +0,0 @@ | |||
| 1 | -# Optimize Datacopy Sample Introduction | ||
| 2 | - | ||
| 3 | -## Overview | ||
| 4 | - | ||
| 5 | -This sample demonstrates optimization techniques for data copy APIs, implemented through the `<<<>>>` direct invocation method. It covers methods such as reducing invalid data copying and reducing the number of copy instructions. | ||
| 6 | - | ||
| 7 | -## Sample List | ||
| 8 | - | ||
| 9 | -| Directory Name | Description | Supported Products | | ||
| 10 | -| -------------------------------------------------- | ---------------------------------------------------- | --- | | ||
| 11 | -| [optimize_datacopy_loop_mode](./optimize_datacopy_loop_mode) | Use loop mode to reduce the number of DataCopyPad instructions when using the DataCopyPad API. | Ascend 950PR/Ascend 950DT | | ||
| 12 | -| [optimize_datacopy_nddma](./optimize_datacopy_nddma) | Use nddma copy to reduce the number of copy instructions when performing non-aligned data copying. | Ascend 950PR/Ascend 950DT | | ||
| 13 | -| [optimize_reduce_invalid_datacopy](./optimize_reduce_invalid_datacopy) | Reduce invalid data copying by setting Compact mode when using the DataCopyPad API. | Ascend 950PR/Ascend 950DT | | ||
| @@ -1,37 +0,0 @@ | |||
| 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 | -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 | - optimize_datacopy_loop_mode.asc | ||
| 20 | -) | ||
| 21 | - | ||
| 22 | -target_link_libraries(demo PRIVATE | ||
| 23 | - tiling_api | ||
| 24 | - register | ||
| 25 | - platform | ||
| 26 | - m | ||
| 27 | -) | ||
| 28 | - | ||
| 29 | -# ====================================================================================== | ||
| 30 | -# NPU 编译选项配置 | ||
| 31 | -# | ||
| 32 | -# 说明: | ||
| 33 | -# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 34 | -# ====================================================================================== | ||
| 35 | -target_compile_options(demo PRIVATE | ||
| 36 | - $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510> | ||
| 37 | -) | ||
| @@ -1,71 +0,0 @@ | |||
| 1 | -# optimize_datacopy_loop_mode样例 | ||
| 2 | - | ||
| 3 | -## 概述 | ||
| 4 | -在使用DataCopyPad接口时,使用loop模式减少DataCopyPad指令的条数 | ||
| 5 | - | ||
| 6 | -## 支持的产品 | ||
| 7 | -- Ascend 950PR/Ascend 950DT | ||
| 8 | - | ||
| 9 | -## 目录结构介绍 | ||
| 10 | -``` | ||
| 11 | -├── optimize_datacopy_loop_mode | ||
| 12 | -│ ├── scripts | ||
| 13 | -│ │ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 14 | -│ │ └── verify_result.py // 验证输出数据和真值数据是否一致的验证脚本 | ||
| 15 | -│ ├── CMakeLists.txt // 编译工程文件 | ||
| 16 | -│ ├── data_utils.h // 数据读入写出函数 | ||
| 17 | -│ └── optimize_datacopy_loop_mode.asc // AscendC算子实现 & 调用样例 | ||
| 18 | -``` | ||
| 19 | - | ||
| 20 | -## 算子描述 | ||
| 21 | -- 算子功能: | ||
| 22 | - 在使用DataCopyPad接口时,使用loop模式减少DataCopyPad指令的条数 | ||
| 23 | -- 算子规格: | ||
| 24 | - <table> | ||
| 25 | - <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="3" align="center">AIC算子</td></tr> | ||
| 26 | - </tr> | ||
| 27 | - <tr><td rowspan="2" align="center">算子输入</td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td></tr> | ||
| 28 | - <tr><td align="center">x</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 29 | - </tr> | ||
| 30 | - </tr> | ||
| 31 | - <tr><td rowspan="1" align="center">算子输出</td><td align="center">y</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 32 | - </tr> | ||
| 33 | - <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">optimize_datacopy_loop_mode</td></tr> | ||
| 34 | - </table> | ||
| 35 | -- 算子实现: | ||
| 36 | - 在使用DataCopyPad接口时,使用loop模式减少DataCopyPad指令的条数 | ||
| 37 | - | ||
| 38 | - - 调用实现 | ||
| 39 | - 使用内核调用符<<<>>>调用核函数。 | ||
| 40 | - | ||
| 41 | -## 编译运行 | ||
| 42 | -在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 43 | -- 配置环境变量 | ||
| 44 | - 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 45 | - - 默认路径,root用户安装CANN软件包 | ||
| 46 | - ```bash | ||
| 47 | - source /usr/local/Ascend/cann/set_env.sh | ||
| 48 | - ``` | ||
| 49 | - | ||
| 50 | - - 默认路径,非root用户安装CANN软件包 | ||
| 51 | - ```bash | ||
| 52 | - source $HOME/Ascend/cann/set_env.sh | ||
| 53 | - ``` | ||
| 54 | - | ||
| 55 | - - 指定路径install_path,安装CANN软件包 | ||
| 56 | - ```bash | ||
| 57 | - source ${install_path}/cann/set_env.sh | ||
| 58 | - ``` | ||
| 59 | - | ||
| 60 | -- 样例执行 | ||
| 61 | - ```bash | ||
| 62 | - mkdir -p build && cd build; # 创建并进入build目录 | ||
| 63 | - cmake ..;make -j; # 编译工程 | ||
| 64 | - python3 ../scripts/gen_data.py # 生成测试输入数据 | ||
| 65 | - ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 66 | - python3 ../scripts/verify_result.py output/output.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 67 | - ``` | ||
| 68 | - 执行结果如下,说明精度对比成功。 | ||
| 69 | - ```bash | ||
| 70 | - test pass! | ||
| 71 | - ``` | ||
| @@ -1,94 +0,0 @@ | |||
| 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 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 | ||
| @@ -1,120 +0,0 @@ | |||
| 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 | -#include "acl/acl.h" | ||
| 13 | -#include "kernel_operator.h" | ||
| 14 | -#include "data_utils.h" | ||
| 15 | - | ||
| 16 | -template<typename T> | ||
| 17 | -class Kernel { | ||
| 18 | - public: | ||
| 19 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* y, uint32_t count, AscendC::TPipe* pipeIn) | ||
| 20 | - { | ||
| 21 | - this->pipe = pipeIn; | ||
| 22 | - this->count = count; | ||
| 23 | - this->xGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(x)); | ||
| 24 | - this->yGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(y)); | ||
| 25 | - this->pipe->InitBuffer(inQueueX, 1, sizeof(T) * count); | ||
| 26 | - this->pipe->InitBuffer(outQueueY, 1, sizeof(T) * count); | ||
| 27 | - } | ||
| 28 | - __aicore__ inline void CopyIn() | ||
| 29 | - { | ||
| 30 | - AscendC::LoopModeParams loopModeParams; | ||
| 31 | - loopModeParams.loop1Size = 2; | ||
| 32 | - loopModeParams.loop2Size = 2; | ||
| 33 | - loopModeParams.loop1SrcStride = 96; | ||
| 34 | - loopModeParams.loop1DstStride = 128; | ||
| 35 | - loopModeParams.loop2SrcStride = 192; | ||
| 36 | - loopModeParams.loop2DstStride = 288; | ||
| 37 | - AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>(); | ||
| 38 | - AscendC::Duplicate<T>(xLocal, 0, count); | ||
| 39 | - AscendC::DataCopyParams dataCopyParams; | ||
| 40 | - dataCopyParams.blockCount = 2; | ||
| 41 | - dataCopyParams.blockLen = 48; | ||
| 42 | - dataCopyParams.srcStride = 0; | ||
| 43 | - dataCopyParams.dstStride = 0; | ||
| 44 | - AscendC::DataCopyPadParams dataCopyPadParams; | ||
| 45 | - dataCopyPadParams.isPad = 0; | ||
| 46 | - dataCopyPadParams.leftPadding = 0; | ||
| 47 | - dataCopyPadParams.rightPadding = 0; | ||
| 48 | - dataCopyPadParams.paddingValue = 0; | ||
| 49 | - AscendC::SetLoopModePara(loopModeParams, AscendC::DataCopyMVType::OUT_TO_UB); | ||
| 50 | - AscendC::DataCopyPad<T, AscendC::PaddingMode::Compact>(xLocal, xGm, dataCopyParams, dataCopyPadParams); | ||
| 51 | - AscendC::ResetLoopModePara(AscendC::DataCopyMVType::OUT_TO_UB); | ||
| 52 | - inQueueX.EnQue<T>(xLocal); | ||
| 53 | - } | ||
| 54 | - __aicore__ inline void Compute() | ||
| 55 | - { | ||
| 56 | - AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>(); | ||
| 57 | - AscendC::LocalTensor<T> yLocal = outQueueY.AllocTensor<T>(); | ||
| 58 | - AscendC::Duplicate<T>(yLocal, 0, count); | ||
| 59 | - DataCopy(yLocal, xLocal, count); | ||
| 60 | - outQueueY.EnQue<T>(yLocal); | ||
| 61 | - } | ||
| 62 | - __aicore__ inline void CopyOut() | ||
| 63 | - { | ||
| 64 | - AscendC::LocalTensor<T> yLocal = outQueueY.DeQue<T>(); | ||
| 65 | - AscendC::DataCopy(yGm, yLocal, count); | ||
| 66 | - } | ||
| 67 | - __aicore__ inline void Process() | ||
| 68 | - { | ||
| 69 | - CopyIn(); | ||
| 70 | - Compute(); | ||
| 71 | - CopyOut(); | ||
| 72 | - } | ||
| 73 | - private: | ||
| 74 | - AscendC::TPipe* pipe = nullptr; | ||
| 75 | - uint32_t count; | ||
| 76 | - AscendC::GlobalTensor<T> xGm; | ||
| 77 | - AscendC::GlobalTensor<T> yGm; | ||
| 78 | - AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX; | ||
| 79 | - AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueY; | ||
| 80 | -}; | ||
| 81 | - | ||
| 82 | -__global__ __vector__ void optimize_datacopy_loop_mode(__gm__ uint8_t* x, __gm__ uint8_t* y) | ||
| 83 | -{ | ||
| 84 | - AscendC::TPipe pipe; | ||
| 85 | - Kernel<float> kernel; | ||
| 86 | - kernel.Init(x, y, 1024, &pipe); | ||
| 87 | - kernel.Process(); | ||
| 88 | -} | ||
| 89 | - | ||
| 90 | -int32_t main(int32_t argc, char *argv[]) | ||
| 91 | -{ | ||
| 92 | - uint32_t numBlocks = 1; | ||
| 93 | - aclInit(nullptr); | ||
| 94 | - int32_t deviceId = 0; | ||
| 95 | - aclrtSetDevice(deviceId); | ||
| 96 | - aclrtStream stream = nullptr; | ||
| 97 | - aclrtCreateStream(&stream); | ||
| 98 | - size_t aInputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 99 | - size_t outputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 100 | - uint8_t *xHost, *yHost; | ||
| 101 | - uint8_t *xDevice, *yDevice; | ||
| 102 | - aclrtMallocHost((void **)(&xHost), aInputByteSize); | ||
| 103 | - aclrtMallocHost((void **)(&yHost), outputByteSize); | ||
| 104 | - aclrtMalloc((void **)&xDevice, aInputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 105 | - aclrtMalloc((void **)&yDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 106 | - ReadFile("./input/input.bin", aInputByteSize, xHost, aInputByteSize); | ||
| 107 | - aclrtMemcpy(xDevice, aInputByteSize, xHost, aInputByteSize, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 108 | - optimize_datacopy_loop_mode<<<numBlocks, nullptr, stream>>>(xDevice, yDevice); | ||
| 109 | - aclrtSynchronizeStream(stream); | ||
| 110 | - aclrtMemcpy(yHost, outputByteSize, yDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 111 | - WriteFile("./output/output.bin", yHost, outputByteSize); | ||
| 112 | - aclrtFree(xDevice); | ||
| 113 | - aclrtFree(yDevice); | ||
| 114 | - aclrtFreeHost(xHost); | ||
| 115 | - aclrtFreeHost(yHost); | ||
| 116 | - aclrtDestroyStream(stream); | ||
| 117 | - aclrtResetDevice(deviceId); | ||
| 118 | - aclFinalize(); | ||
| 119 | - return 0; | ||
| 120 | -} | ||
| @@ -1,39 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 data_copy_pad_compact_loop(): | ||
| 20 | - src = np.ones([1024]).astype(np.float32) | ||
| 21 | - dst = np.zeros([1024]).astype(np.float32) | ||
| 22 | - offset = 0 | ||
| 23 | - for i in range(2): | ||
| 24 | - for j in range(2): | ||
| 25 | - offset = 72 * i + j * 32 | ||
| 26 | - dst[offset:offset + 2 * 12] = src[offset:offset + 2 * 12] | ||
| 27 | - return src, dst | ||
| 28 | - | ||
| 29 | - | ||
| 30 | -def gen_golden_data_simple(): | ||
| 31 | - os.makedirs("input", exist_ok=True) | ||
| 32 | - os.makedirs("output", exist_ok=True) | ||
| 33 | - src, golden = data_copy_pad_compact_loop() | ||
| 34 | - src.tofile("./input/input.bin") | ||
| 35 | - golden.tofile("./output/golden.bin") | ||
| 36 | - | ||
| 37 | - | ||
| 38 | -if __name__ == "__main__": | ||
| 39 | - gen_golden_data_simple() | ||
| @@ -1,61 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 | -import tensorflow as tf | ||
| 18 | - | ||
| 19 | - | ||
| 20 | -float32 = tf.float32.as_numpy_dtype | ||
| 21 | -RELATIVE_TOL = 1e-3 | ||
| 22 | -ABSOLUTE_TOL = 1e-5 | ||
| 23 | -ERROR_TOL = 1e-3 | ||
| 24 | -data_type = float32 | ||
| 25 | - | ||
| 26 | -def verify_result(output, golden): | ||
| 27 | - output = np.fromfile(output, dtype=data_type).reshape(-1) | ||
| 28 | - golden = np.fromfile(golden, dtype=data_type).reshape(-1) | ||
| 29 | - different_element_results = np.isclose(output, | ||
| 30 | - golden, | ||
| 31 | - rtol=RELATIVE_TOL, | ||
| 32 | - atol=ABSOLUTE_TOL, | ||
| 33 | - equal_nan=True) | ||
| 34 | - different_element_indexes = np.where(different_element_results == False)[0] | ||
| 35 | - for index in range(len(different_element_indexes)): | ||
| 36 | - real_index = different_element_indexes[index] | ||
| 37 | - golden_data = golden[real_index] | ||
| 38 | - output_data = output[real_index] | ||
| 39 | - print( | ||
| 40 | - "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f" % | ||
| 41 | - (real_index, golden_data, output_data, | ||
| 42 | - abs(output_data - golden_data) / golden_data)) | ||
| 43 | - if index == 100: | ||
| 44 | - break | ||
| 45 | - print("golden_data : ", golden) | ||
| 46 | - print("output : ", output) | ||
| 47 | - error_ratio = float(different_element_indexes.size) / golden.size | ||
| 48 | - print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, ERROR_TOL)) | ||
| 49 | - return error_ratio <= ERROR_TOL | ||
| 50 | - | ||
| 51 | - | ||
| 52 | -if __name__ == '__main__': | ||
| 53 | - try: | ||
| 54 | - res = verify_result(sys.argv[1], sys.argv[2]) | ||
| 55 | - if not res: | ||
| 56 | - raise ValueError("[ERROR] result error") | ||
| 57 | - else: | ||
| 58 | - print("test pass!") | ||
| 59 | - except Exception as e: | ||
| 60 | - print(e) | ||
| 61 | - sys.exit(1) | ||
| @@ -1,37 +0,0 @@ | |||
| 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 | -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 | - optimize_datacopy_nddma.asc | ||
| 20 | -) | ||
| 21 | - | ||
| 22 | -target_link_libraries(demo PRIVATE | ||
| 23 | - tiling_api | ||
| 24 | - register | ||
| 25 | - platform | ||
| 26 | - m | ||
| 27 | -) | ||
| 28 | - | ||
| 29 | -# ====================================================================================== | ||
| 30 | -# NPU 编译选项配置 | ||
| 31 | -# | ||
| 32 | -# 说明: | ||
| 33 | -# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 34 | -# ====================================================================================== | ||
| 35 | -target_compile_options(demo PRIVATE | ||
| 36 | - $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510> | ||
| 37 | -) | ||
| @@ -1,70 +0,0 @@ | |||
| 1 | -# optimize_datacopy_nddma样例 | ||
| 2 | - | ||
| 3 | -## 概述 | ||
| 4 | -在进行非对齐数据搬运时,使用nddma搬运减少搬运指令的条数 | ||
| 5 | - | ||
| 6 | -## 支持的产品 | ||
| 7 | -- Ascend 950PR/Ascend 950DT | ||
| 8 | - | ||
| 9 | -## 目录结构介绍 | ||
| 10 | -``` | ||
| 11 | -├── optimize_datacopy_nddma | ||
| 12 | -│ ├── scripts | ||
| 13 | -│ │ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 14 | -│ │ └── verify_result.py // 验证输出数据和真值数据是否一致的验证脚本 | ||
| 15 | -│ ├── CMakeLists.txt // 编译工程文件 | ||
| 16 | -│ ├── data_utils.h // 数据读入写出函数 | ||
| 17 | -│ └── optimize_datacopy_nddma.asc // AscendC算子实现 & 调用样例 | ||
| 18 | -``` | ||
| 19 | - | ||
| 20 | -## 算子描述 | ||
| 21 | -- 算子功能: | ||
| 22 | - 在进行非对齐数据搬运时,使用nddma搬运减少搬运指令的条数 | ||
| 23 | -- 算子规格: | ||
| 24 | - <table> | ||
| 25 | - <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="3" align="center">AIC算子</td></tr> | ||
| 26 | - </tr> | ||
| 27 | - <tr><td rowspan="2" align="center">算子输入</td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td></tr> | ||
| 28 | - <tr><td align="center">x</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 29 | - </tr> | ||
| 30 | - </tr> | ||
| 31 | - <tr><td rowspan="1" align="center">算子输出</td><td align="center">y</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 32 | - </tr> | ||
| 33 | - <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">optimize_datacopy_nddma</td></tr> | ||
| 34 | - </table> | ||
| 35 | -- 算子实现: | ||
| 36 | - 在进行非对齐数据搬运时,使用nddma搬运减少搬运指令的条数 | ||
| 37 | - | ||
| 38 | - - 调用实现 | ||
| 39 | - 使用内核调用符<<<>>>调用核函数。 | ||
| 40 | -## 编译运行 | ||
| 41 | -在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 42 | -- 配置环境变量 | ||
| 43 | - 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 44 | - - 默认路径,root用户安装CANN软件包 | ||
| 45 | - ```bash | ||
| 46 | - source /usr/local/Ascend/cann/set_env.sh | ||
| 47 | - ``` | ||
| 48 | - | ||
| 49 | - - 默认路径,非root用户安装CANN软件包 | ||
| 50 | - ```bash | ||
| 51 | - source $HOME/Ascend/cann/set_env.sh | ||
| 52 | - ``` | ||
| 53 | - | ||
| 54 | - - 指定路径install_path,安装CANN软件包 | ||
| 55 | - ```bash | ||
| 56 | - source ${install_path}/cann/set_env.sh | ||
| 57 | - ``` | ||
| 58 | - | ||
| 59 | -- 样例执行 | ||
| 60 | - ```bash | ||
| 61 | - mkdir -p build && cd build; # 创建并进入build目录 | ||
| 62 | - cmake ..;make -j; # 编译工程 | ||
| 63 | - python3 ../scripts/gen_data.py # 生成测试输入数据 | ||
| 64 | - ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 65 | - python3 ../scripts/verify_result.py output/output.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 66 | - ``` | ||
| 67 | - 执行结果如下,说明精度对比成功。 | ||
| 68 | - ```bash | ||
| 69 | - test pass! | ||
| 70 | - ``` | ||
| @@ -1,94 +0,0 @@ | |||
| 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 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 | ||
| @@ -1,105 +0,0 @@ | |||
| 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 | -#include "acl/acl.h" | ||
| 13 | -#include "kernel_operator.h" | ||
| 14 | -#include "data_utils.h" | ||
| 15 | - | ||
| 16 | -template<typename T> | ||
| 17 | -class Kernel { | ||
| 18 | - public: | ||
| 19 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* y, uint32_t count, AscendC::TPipe* pipeIn) | ||
| 20 | - { | ||
| 21 | - this->pipe = pipeIn; | ||
| 22 | - this->count = count; | ||
| 23 | - this->xGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(x)); | ||
| 24 | - this->yGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(y)); | ||
| 25 | - this->pipe->InitBuffer(inQueueX, 1, sizeof(T) * count); | ||
| 26 | - this->pipe->InitBuffer(outQueueY, 1, sizeof(T) * count); | ||
| 27 | - } | ||
| 28 | - __aicore__ inline void CopyIn() | ||
| 29 | - { | ||
| 30 | - AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>(); | ||
| 31 | - AscendC::Duplicate<T>(xLocal, 0, count); | ||
| 32 | - AscendC::NdDmaLoopInfo<2> loopInfo{{1, 32}, {1, 32}, {32, 32}, {0, 0}, {0, 0}}; | ||
| 33 | - AscendC::NdDmaParams<T, 2> params = {loopInfo, 0}; | ||
| 34 | - AscendC::NdDmaDci(); | ||
| 35 | - static constexpr AscendC::NdDmaConfig config = {false}; | ||
| 36 | - AscendC::DataCopy<T, 2, config>(xLocal, xGm, params); | ||
| 37 | - inQueueX.EnQue<T>(xLocal); | ||
| 38 | - } | ||
| 39 | - __aicore__ inline void Compute() | ||
| 40 | - { | ||
| 41 | - AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>(); | ||
| 42 | - AscendC::LocalTensor<T> yLocal = outQueueY.AllocTensor<T>(); | ||
| 43 | - AscendC::Duplicate<T>(yLocal, 0, count); | ||
| 44 | - DataCopy(yLocal, xLocal, count); | ||
| 45 | - outQueueY.EnQue<T>(yLocal); | ||
| 46 | - } | ||
| 47 | - __aicore__ inline void CopyOut() | ||
| 48 | - { | ||
| 49 | - AscendC::LocalTensor<T> yLocal = outQueueY.DeQue<T>(); | ||
| 50 | - AscendC::DataCopy(yGm, yLocal, count); | ||
| 51 | - } | ||
| 52 | - __aicore__ inline void Process() | ||
| 53 | - { | ||
| 54 | - CopyIn(); | ||
| 55 | - Compute(); | ||
| 56 | - CopyOut(); | ||
| 57 | - } | ||
| 58 | - private: | ||
| 59 | - AscendC::TPipe* pipe = nullptr; | ||
| 60 | - uint32_t count; | ||
| 61 | - AscendC::GlobalTensor<T> xGm; | ||
| 62 | - AscendC::GlobalTensor<T> yGm; | ||
| 63 | - AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX; | ||
| 64 | - AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueY; | ||
| 65 | -}; | ||
| 66 | - | ||
| 67 | -__global__ __vector__ void optimize_datacopy_nddma(__gm__ uint8_t* x, __gm__ uint8_t* y) | ||
| 68 | -{ | ||
| 69 | - AscendC::TPipe pipe; | ||
| 70 | - Kernel<float> kernel; | ||
| 71 | - kernel.Init(x, y, 1024, &pipe); | ||
| 72 | - kernel.Process(); | ||
| 73 | -} | ||
| 74 | - | ||
| 75 | -int32_t main(int32_t argc, char *argv[]) | ||
| 76 | -{ | ||
| 77 | - uint32_t numBlocks = 1; | ||
| 78 | - aclInit(nullptr); | ||
| 79 | - int32_t deviceId = 0; | ||
| 80 | - aclrtSetDevice(deviceId); | ||
| 81 | - aclrtStream stream = nullptr; | ||
| 82 | - aclrtCreateStream(&stream); | ||
| 83 | - size_t aInputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 84 | - size_t outputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 85 | - uint8_t *xHost, *yHost; | ||
| 86 | - uint8_t *xDevice, *yDevice; | ||
| 87 | - aclrtMallocHost((void **)(&xHost), aInputByteSize); | ||
| 88 | - aclrtMallocHost((void **)(&yHost), outputByteSize); | ||
| 89 | - aclrtMalloc((void **)&xDevice, aInputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 90 | - aclrtMalloc((void **)&yDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 91 | - ReadFile("./input/input.bin", aInputByteSize, xHost, aInputByteSize); | ||
| 92 | - aclrtMemcpy(xDevice, aInputByteSize, xHost, aInputByteSize, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 93 | - optimize_datacopy_nddma<<<numBlocks, nullptr, stream>>>(xDevice, yDevice); | ||
| 94 | - aclrtSynchronizeStream(stream); | ||
| 95 | - aclrtMemcpy(yHost, outputByteSize, yDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 96 | - WriteFile("./output/output.bin", yHost, outputByteSize); | ||
| 97 | - aclrtFree(xDevice); | ||
| 98 | - aclrtFree(yDevice); | ||
| 99 | - aclrtFreeHost(xHost); | ||
| 100 | - aclrtFreeHost(yHost); | ||
| 101 | - aclrtDestroyStream(stream); | ||
| 102 | - aclrtResetDevice(deviceId); | ||
| 103 | - aclFinalize(); | ||
| 104 | - return 0; | ||
| 105 | -} | ||
| @@ -1,61 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 | -import tensorflow as tf | ||
| 18 | - | ||
| 19 | - | ||
| 20 | -float32 = tf.float32.as_numpy_dtype | ||
| 21 | -RELATIVE_TOL = 1e-3 | ||
| 22 | -ABSOLUTE_TOL = 1e-5 | ||
| 23 | -ERROR_TOL = 1e-3 | ||
| 24 | -data_type = float32 | ||
| 25 | - | ||
| 26 | -def verify_result(output, golden): | ||
| 27 | - output = np.fromfile(output, dtype=data_type).reshape(-1) | ||
| 28 | - golden = np.fromfile(golden, dtype=data_type).reshape(-1) | ||
| 29 | - different_element_results = np.isclose(output, | ||
| 30 | - golden, | ||
| 31 | - rtol=RELATIVE_TOL, | ||
| 32 | - atol=ABSOLUTE_TOL, | ||
| 33 | - equal_nan=True) | ||
| 34 | - different_element_indexes = np.where(different_element_results == False)[0] | ||
| 35 | - for index in range(len(different_element_indexes)): | ||
| 36 | - real_index = different_element_indexes[index] | ||
| 37 | - golden_data = golden[real_index] | ||
| 38 | - output_data = output[real_index] | ||
| 39 | - print( | ||
| 40 | - "data index: %06d, expected: %-.9f, actual: %-.9f, rdiff: %-.6f" % | ||
| 41 | - (real_index, golden_data, output_data, | ||
| 42 | - abs(output_data - golden_data) / golden_data)) | ||
| 43 | - if index == 100: | ||
| 44 | - break | ||
| 45 | - print("golden_data : ", golden) | ||
| 46 | - print("output : ", output) | ||
| 47 | - error_ratio = float(different_element_indexes.size) / golden.size | ||
| 48 | - print("error ratio: %.4f, tolerance: %.4f" % (error_ratio, ERROR_TOL)) | ||
| 49 | - return error_ratio <= ERROR_TOL | ||
| 50 | - | ||
| 51 | - | ||
| 52 | -if __name__ == '__main__': | ||
| 53 | - try: | ||
| 54 | - res = verify_result(sys.argv[1], sys.argv[2]) | ||
| 55 | - if not res: | ||
| 56 | - raise ValueError("[ERROR] result error") | ||
| 57 | - else: | ||
| 58 | - print("test pass!") | ||
| 59 | - except Exception as e: | ||
| 60 | - print(e) | ||
| 61 | - sys.exit(1) | ||
| @@ -1,37 +0,0 @@ | |||
| 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 | -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 | - optimize_reduce_invalid_datacopy.asc | ||
| 20 | -) | ||
| 21 | - | ||
| 22 | -target_link_libraries(demo PRIVATE | ||
| 23 | - tiling_api | ||
| 24 | - register | ||
| 25 | - platform | ||
| 26 | - m | ||
| 27 | -) | ||
| 28 | - | ||
| 29 | -# ====================================================================================== | ||
| 30 | -# NPU 编译选项配置 | ||
| 31 | -# | ||
| 32 | -# 说明: | ||
| 33 | -# - 需根据实际部署的 NPU 硬件架构选择对应的 `npu-arch` 参数。 | ||
| 34 | -# ====================================================================================== | ||
| 35 | -target_compile_options(demo PRIVATE | ||
| 36 | - $<$<COMPILE_LANGUAGE:ASC>:--npu-arch=dav-3510> | ||
| 37 | -) | ||
| @@ -1,70 +0,0 @@ | |||
| 1 | -# optimize_reduce_invalid_datacopy样例 | ||
| 2 | - | ||
| 3 | -## 概述 | ||
| 4 | -在使用DataCopyPad接口时,通过设置Compact模式减少无效数据的搬运 | ||
| 5 | - | ||
| 6 | -## 支持的产品 | ||
| 7 | -- Ascend 950PR/Ascend 950DT | ||
| 8 | - | ||
| 9 | -## 目录结构介绍 | ||
| 10 | -``` | ||
| 11 | -├── optimize_reduce_invalid_datacopy | ||
| 12 | -│ ├── scripts | ||
| 13 | -│ │ ├── gen_data.py // 输入数据和真值数据生成脚本 | ||
| 14 | -│ │ └── verify_result.py // 验证输出数据和真值数据是否一致的验证脚本 | ||
| 15 | -│ ├── CMakeLists.txt // 编译工程文件 | ||
| 16 | -│ ├── data_utils.h // 数据读入写出函数 | ||
| 17 | -│ └── optimize_reduce_invalid_datacopy.asc // AscendC算子实现 & 调用样例 | ||
| 18 | -``` | ||
| 19 | - | ||
| 20 | -## 算子描述 | ||
| 21 | -- 算子功能: | ||
| 22 | - 在使用DataCopyPad接口时,通过设置Compact模式减少无效数据的搬运 | ||
| 23 | -- 算子规格: | ||
| 24 | - <table> | ||
| 25 | - <tr><td rowspan="1" align="center">算子类型(OpType)</td><td colspan="3" align="center">AIC算子</td></tr> | ||
| 26 | - </tr> | ||
| 27 | - <tr><td rowspan="2" align="center">算子输入</td><td align="center">name</td><td align="center">shape</td><td align="center">data type</td></tr> | ||
| 28 | - <tr><td align="center">x</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 29 | - </tr> | ||
| 30 | - </tr> | ||
| 31 | - <tr><td rowspan="1" align="center">算子输出</td><td align="center">y</td><td align="center">1024</td><td align="center">float</td></tr> | ||
| 32 | - </tr> | ||
| 33 | - <tr><td rowspan="1" align="center">核函数名</td><td colspan="4" align="center">optimize_reduce_invalid_datacopy</td></tr> | ||
| 34 | - </table> | ||
| 35 | -- 算子实现: | ||
| 36 | - 在使用DataCopyPad接口时,通过设置Compact模式减少无效数据的搬运 | ||
| 37 | - | ||
| 38 | - - 调用实现 | ||
| 39 | - 使用内核调用符<<<>>>调用核函数。 | ||
| 40 | -## 编译运行 | ||
| 41 | -在本样例根目录下执行如下步骤,编译并执行算子。 | ||
| 42 | -- 配置环境变量 | ||
| 43 | - 请根据当前环境上CANN开发套件包的[安装方式](../../../../../../docs/quick_start.md#prepare&install),选择对应配置环境变量的命令。 | ||
| 44 | - - 默认路径,root用户安装CANN软件包 | ||
| 45 | - ```bash | ||
| 46 | - source /usr/local/Ascend/cann/set_env.sh | ||
| 47 | - ``` | ||
| 48 | - | ||
| 49 | - - 默认路径,非root用户安装CANN软件包 | ||
| 50 | - ```bash | ||
| 51 | - source $HOME/Ascend/cann/set_env.sh | ||
| 52 | - ``` | ||
| 53 | - | ||
| 54 | - - 指定路径install_path,安装CANN软件包 | ||
| 55 | - ```bash | ||
| 56 | - source ${install_path}/cann/set_env.sh | ||
| 57 | - ``` | ||
| 58 | - | ||
| 59 | -- 样例执行 | ||
| 60 | - ```bash | ||
| 61 | - mkdir -p build && cd build; # 创建并进入build目录 | ||
| 62 | - cmake ..;make -j; # 编译工程 | ||
| 63 | - python3 ../scripts/gen_data.py # 生成测试输入数据 | ||
| 64 | - ./demo # 执行编译生成的可执行程序,执行样例 | ||
| 65 | - python3 ../scripts/verify_result.py output/output.bin output/golden.bin # 验证输出结果是否正确,确认算法逻辑正确 | ||
| 66 | - ``` | ||
| 67 | - 执行结果如下,说明精度对比成功。 | ||
| 68 | - ```bash | ||
| 69 | - test pass! | ||
| 70 | - ``` | ||
| @@ -1,94 +0,0 @@ | |||
| 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 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 | ||
| @@ -1,111 +0,0 @@ | |||
| 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 | -#include "acl/acl.h" | ||
| 13 | -#include "kernel_operator.h" | ||
| 14 | -#include "data_utils.h" | ||
| 15 | - | ||
| 16 | -template<typename T> | ||
| 17 | -class Kernel { | ||
| 18 | - public: | ||
| 19 | - __aicore__ inline void Init(__gm__ uint8_t* x, __gm__ uint8_t* y, uint32_t count, AscendC::TPipe* pipeIn) | ||
| 20 | - { | ||
| 21 | - this->pipe = pipeIn; | ||
| 22 | - this->count = count; | ||
| 23 | - this->xGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(x)); | ||
| 24 | - this->yGm.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(y)); | ||
| 25 | - this->pipe->InitBuffer(inQueueX, 1, sizeof(T) * count); | ||
| 26 | - this->pipe->InitBuffer(outQueueY, 1, sizeof(T) * count); | ||
| 27 | - } | ||
| 28 | - __aicore__ inline void CopyIn() | ||
| 29 | - { | ||
| 30 | - AscendC::LocalTensor<T> xLocal = inQueueX.AllocTensor<T>(); | ||
| 31 | - AscendC::Duplicate<T>(xLocal, 0, count); | ||
| 32 | - AscendC::DataCopyParams dataCopyParams; | ||
| 33 | - dataCopyParams.blockCount = 3; | ||
| 34 | - dataCopyParams.blockLen = 48; | ||
| 35 | - dataCopyParams.srcStride = 0; | ||
| 36 | - dataCopyParams.dstStride = 0; | ||
| 37 | - AscendC::DataCopyPadParams dataCopyPadParams; | ||
| 38 | - dataCopyPadParams.isPad = 1; | ||
| 39 | - dataCopyPadParams.leftPadding = 0; | ||
| 40 | - dataCopyPadParams.rightPadding = 4; | ||
| 41 | - dataCopyPadParams.paddingValue = 0; | ||
| 42 | - AscendC::DataCopyPad<T, AscendC::PaddingMode::Compact>(xLocal, xGm, dataCopyParams, dataCopyPadParams); | ||
| 43 | - inQueueX.EnQue<T>(xLocal); | ||
| 44 | - } | ||
| 45 | - __aicore__ inline void Compute() | ||
| 46 | - { | ||
| 47 | - AscendC::LocalTensor<T> xLocal = inQueueX.DeQue<T>(); | ||
| 48 | - AscendC::LocalTensor<T> yLocal = outQueueY.AllocTensor<T>(); | ||
| 49 | - AscendC::Duplicate<T>(yLocal, 0, count); | ||
| 50 | - DataCopy(yLocal, xLocal, count); | ||
| 51 | - outQueueY.EnQue<T>(yLocal); | ||
| 52 | - } | ||
| 53 | - __aicore__ inline void CopyOut() | ||
| 54 | - { | ||
| 55 | - AscendC::LocalTensor<T> yLocal = outQueueY.DeQue<T>(); | ||
| 56 | - AscendC::DataCopy(yGm, yLocal, count); | ||
| 57 | - } | ||
| 58 | - __aicore__ inline void Process() | ||
| 59 | - { | ||
| 60 | - CopyIn(); | ||
| 61 | - Compute(); | ||
| 62 | - CopyOut(); | ||
| 63 | - } | ||
| 64 | - private: | ||
| 65 | - AscendC::TPipe* pipe = nullptr; | ||
| 66 | - uint32_t count; | ||
| 67 | - AscendC::GlobalTensor<T> xGm; | ||
| 68 | - AscendC::GlobalTensor<T> yGm; | ||
| 69 | - AscendC::TQue<AscendC::TPosition::VECIN, 1> inQueueX; | ||
| 70 | - AscendC::TQue<AscendC::TPosition::VECOUT, 1> outQueueY; | ||
| 71 | -}; | ||
| 72 | - | ||
| 73 | -__global__ __vector__ void optimize_reduce_invalid_datacopy(__gm__ uint8_t* x, __gm__ uint8_t* y) | ||
| 74 | -{ | ||
| 75 | - AscendC::TPipe pipe; | ||
| 76 | - Kernel<float> kernel; | ||
| 77 | - kernel.Init(x, y, 1024, &pipe); | ||
| 78 | - kernel.Process(); | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -int32_t main(int32_t argc, char *argv[]) | ||
| 82 | -{ | ||
| 83 | - uint32_t numBlocks = 1; | ||
| 84 | - aclInit(nullptr); | ||
| 85 | - int32_t deviceId = 0; | ||
| 86 | - aclrtSetDevice(deviceId); | ||
| 87 | - aclrtStream stream = nullptr; | ||
| 88 | - aclrtCreateStream(&stream); | ||
| 89 | - size_t aInputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 90 | - size_t outputByteSize = static_cast<size_t>(1) * 1024 * sizeof(float); | ||
| 91 | - uint8_t *xHost, *yHost; | ||
| 92 | - uint8_t *xDevice, *yDevice; | ||
| 93 | - aclrtMallocHost((void **)(&xHost), aInputByteSize); | ||
| 94 | - aclrtMallocHost((void **)(&yHost), outputByteSize); | ||
| 95 | - aclrtMalloc((void **)&xDevice, aInputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 96 | - aclrtMalloc((void **)&yDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST); | ||
| 97 | - ReadFile("./input/input.bin", aInputByteSize, xHost, aInputByteSize); | ||
| 98 | - aclrtMemcpy(xDevice, aInputByteSize, xHost, aInputByteSize, ACL_MEMCPY_HOST_TO_DEVICE); | ||
| 99 | - optimize_reduce_invalid_datacopy<<<numBlocks, nullptr, stream>>>(xDevice, yDevice); | ||
| 100 | - aclrtSynchronizeStream(stream); | ||
| 101 | - aclrtMemcpy(yHost, outputByteSize, yDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST); | ||
| 102 | - WriteFile("./output/output.bin", yHost, outputByteSize); | ||
| 103 | - aclrtFree(xDevice); | ||
| 104 | - aclrtFree(yDevice); | ||
| 105 | - aclrtFreeHost(xHost); | ||
| 106 | - aclrtFreeHost(yHost); | ||
| 107 | - aclrtDestroyStream(stream); | ||
| 108 | - aclrtResetDevice(deviceId); | ||
| 109 | - aclFinalize(); | ||
| 110 | - return 0; | ||
| 111 | -} | ||
| @@ -1,36 +0,0 @@ | |||
| 1 | -#!/usr/bin/python3 | ||
| 2 | -# coding=utf-8 | ||
| 3 | - | ||
| 4 | -# ---------------------------------------------------------------------------------------------------------- | ||
| 5 | -# Copyright (c) 2025 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 data_copy_pad_compact(): | ||
| 20 | - src = np.ones([1024]).astype(np.float32) | ||
| 21 | - dst = np.zeros([1024]).astype(np.float32) | ||
| 22 | - dst[:3 * 12] = src[:3 * 12] | ||
| 23 | - dst[3 * 12:3 * 12 + 6] = 0 | ||
| 24 | - return src, dst | ||
| 25 | - | ||
| 26 | - | ||
| 27 | -def gen_golden_data_simple(): | ||
| 28 | - os.makedirs("input", exist_ok=True) | ||
| 29 | - os.makedirs("output", exist_ok=True) | ||
| 30 | - src, golden = data_copy_pad_compact() | ||
| 31 | - src.tofile("./input/input.bin") | ||
| 32 | - golden.tofile("./output/golden.bin") | ||
| 33 | - | ||
| 34 | - | ||
| 35 | -if __name__ == "__main__": | ||
| 36 | - gen_golden_data_simple() | ||