From 245ebc14aa1265eb6ad5a102d0c2aee6ea9b6ca1 Mon Sep 17 00:00:00 2001 From: bluesky90 Date: Tue, 12 Nov 2024 10:43:18 +0800 Subject: [PATCH 1/2] support sim mode in framework kernel launch --- .../README.md | 38 +++++++++++++- .../build.sh | 36 ++++++++++--- .../testcases/cmake/fun.cmake | 52 +++++++++++++++++++ .../testcases/npu/CMakeLists.txt | 13 ++++- .../testcases/npu/softmax_custom_main.cpp | 4 +- 5 files changed, 130 insertions(+), 13 deletions(-) diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/README.md b/examples/activation/softmax/kernel_launch_method_by_framework/README.md index ae41a741..4a5557b1 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/README.md +++ b/examples/activation/softmax/kernel_launch_method_by_framework/README.md @@ -75,5 +75,39 @@ cd build_out ``` ./softmax_custom_npu ``` -### 注意事项 -本样例工程会自动识别执行的硬件平台,无需单独设置SOC_VERSION \ No newline at end of file + +### 6.sim仿真模式的编译与运行(可选) +若要执行sim仿真,需要在编译时指定对应的SOC_VERSION,参考如下操作: + - 在当前算子工程目录下执行如下命令,进行算子工程及仿真执行程序的编译。 + + ``` + bash build.sh -v [SOC_VERSION] + ``` + 其中参数说明如下: + - SOC_VERSION :昇腾AI处理器型号,如果无法确定具体的[SOC_VERSION],则在安装昇腾AI处理器的服务器执行npu-smi info命令进行查询,在查询到的“Name”前增加Ascend信息,例如“Name”对应取值为xxxyy,实际配置的[SOC_VERSION]值为Ascendxxxyy。支持以下参数取值(xxx请替换为具体取值): + - Atlas 推理系列产品(Ascend 310P处理器)参数值:Ascend310P1、Ascend310P3 + - Atlas A2训练系列产品参数值:AscendxxxB1、AscendxxxB2、AscendxxxB3、AscendxxxB4 + + 示例如下: + ``` + bash build.sh -v Ascend310P1 + ``` + 编译成功后,build_out目录下除了生成自定义算子包之外,还会生成仿真样例可执行程序softmax_custom_sim。 + - 参考步骤4,重新安装部署自定义算子包。 + - 设置仿真环境变量,再执行仿真样例可执行程序,其中SOC_VERSION请参考上述参数说明。 + ``` + export LD_LIBRARY_PATH=${ASCEND_HOME_DIR}/tools/simulator/${SOC_VERSION}/lib:$LD_LIBRARY_PATH + ./softmax_custom_sim + ``` + + + +### 7.不同环境上的编译与运行(可选) +若执行样例环境与编译样例环境不一致,请在执行环境中进行如下操作,确保该环境上能够正确执行样例。 + - 参考步骤1,配置环境变量。 + - 参考步骤2,生成输入和真值数据,或者将编译环境下生成的input和output目录复制到执行环境。 + - 将编译环境下编译生成的自定义算子包,复制到执行环境。 + - 参考步骤4,在执行环境,安装部署自定义算子包。 + - 在当前目录下创建一个临时目录,将编译环境build_out目录下生成的可执行程序和op_host/libcust_opapi.so文件,拷贝到临时目录下。 + - 进入临时目录,参考步骤5,执行可执行程序,即可运行样例。 +注意,以上方法仅支持编译环境与运行环境是相同的物理硬件架构,比如编译环境和执行环境均为x86硬件架构;若硬件架构不一致,必须重新编译算子工程,再安装部署和运行样例。 diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/build.sh b/examples/activation/softmax/kernel_launch_method_by_framework/build.sh index 768199f3..7a935f4b 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/build.sh +++ b/examples/activation/softmax/kernel_launch_method_by_framework/build.sh @@ -7,9 +7,31 @@ # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. # See LICENSE in the root of the software repository for the full text of the License. # ====================================================================================================================== - +SHORT=v:, +LONG=soc-version:, +OPTS=$(getopt -a --options $SHORT --longoptions $LONG -- "$@") +eval set -- "$OPTS" +while : +do + case "$1" in + (-v | --soc-version ) + SOC_VERSION="$2" + shift 2;; + (--) + shift; + break;; + (*) + echo "[ERROR] Unexpected option: $1"; + break;; + esac +done script_path=$(realpath $(dirname $0)) +if [ -z "$SOC_VERSION" ]; then + echo "soc version is not set in sim mode" + SOC_VERSION=Ascend910B1 +fi + source $ASCEND_HOME_DIR/bin/setenv.bash cp -rf ../host_tiling/* op_host/ ln -s $ASCEND_HOME_DIR/tools/op_project_templates/ascendc/customize/cmake/util/ ./cmake/util @@ -44,17 +66,17 @@ then target=package if [ "$1"x != ""x ]; then target=$1; fi if [ "$cmake_version" \< "3.19.0" ] ; then - cmake .. $opts -DENABLE_CROSS_COMPILE=0 + cmake .. -DSOC_VERSION=${SOC_VERSION} $opts -DENABLE_CROSS_COMPILE=0 else - cmake .. --preset=default -DENABLE_CROSS_COMPILE=0 + cmake .. -DSOC_VERSION=${SOC_VERSION} --preset=default -DENABLE_CROSS_COMPILE=0 fi cmake_run_package $target cp -r kernel ../ rm -rf * if [ "$cmake_version" \< "3.19.0" ] ; then - cmake .. $opts + cmake .. -DSOC_VERSION=${SOC_VERSION} $opts else - cmake .. --preset=default + cmake .. -DSOC_VERSION=${SOC_VERSION} --preset=default fi cmake --build . --target $target -j16 @@ -70,9 +92,9 @@ else target=package if [ "$1"x != ""x ]; then target=$1; fi if [ "$cmake_version" \< "3.19.0" ] ; then - cmake .. $opts + cmake .. -DSOC_VERSION=${SOC_VERSION} $opts else - cmake .. --preset=default + cmake .. -DSOC_VERSION=${SOC_VERSION} --preset=default fi cmake_run_package $target fi diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/cmake/fun.cmake b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/cmake/fun.cmake index b7bdbf56..16096ae7 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/cmake/fun.cmake +++ b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/cmake/fun.cmake @@ -6,6 +6,30 @@ # INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. # See LICENSE in the root of the software repository for the full text of the License. # ====================================================================================================================== +set(ascend910b_list ascend910b1 ascend910b2 ascend910b2c ascend910b3 ascend910b4 ascend910b4-1 ascend910_9391 ascend910_9381 ascend910_9372 ascend910_9392 ascend910_9382 ascend910_9361 ascend910c1 ascend910c2 ascend910c3 ascend910c4 ascend910c4-1) +set(ascend910_list ascend910a ascend910proa ascend910b ascend910prob ascend910premiuma) +set(ascend310p_list ascend310p1 ascend310p3 ascend310p3vir01 ascend310p3vir02 ascend310p3vir04 ascend310p3vir08) +set(ascend310b_list ascend310b1 ascend310b2 ascend310b3 ascend310b4) +set(all_product ${ascend910b_list} ${ascend910_list} ${ascend310p_list}) + +if(NOT DEFINED SOC_VERSION) + message(FATAL_ERROR "SOC_VERSION value not set.") +endif() + +string(TOLOWER "${SOC_VERSION}" _LOWER_SOC_VERSION) + +if(_LOWER_SOC_VERSION IN_LIST ascend910b_list) + set(DYNAMIC_MODE ON) + set(BUILD_MODE aiv) +elseif(_LOWER_SOC_VERSION IN_LIST ascend910_list) + set(BUILD_MODE c100) +elseif(_LOWER_SOC_VERSION IN_LIST ascend310p_list) + set(BUILD_MODE m200) +elseif(_LOWER_SOC_VERSION IN_LIST ascend310b_list) + set(BUILD_MODE m300) +else() + message(FATAL_ERROR "SOC_VERSION ${SOC_VERSION} does not support, the support list is ${all_product}") +endif() set(UPER_CHARS A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) function(string_to_snake str_in snake_out) @@ -59,3 +83,31 @@ function(add_npu_target) RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} ) endfunction() + +function(add_sim_target) + cmake_parse_arguments(NPU_TEST "" "OP" "SRC" ${ARGN}) + string_to_snake("${NPU_TEST_OP}" op_snake) + add_executable(${op_snake}_sim ${NPU_TEST_SRC}) + target_compile_options(${op_snake}_sim PRIVATE -g) + target_include_directories(${op_snake}_sim PRIVATE + ${ASCEND_CANN_PACKAGE_PATH}/include/acl + ${ASCEND_AUTOGEN_PATH} + ) + + target_link_directories(${op_snake}_sim PRIVATE + ${ASCEND_CANN_PACKAGE_PATH}/lib64 + ${ASCEND_CANN_PACKAGE_PATH}/tools/simulator/${SOC_VERSION}/lib + ) + + target_link_libraries(${op_snake}_sim PRIVATE + intf_pub + cust_opapi + ascendcl + nnopbase + runtime_camodel + $<$:npu_drv> + ) + set_target_properties(${op_snake}_sim PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} + ) +endfunction() diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/CMakeLists.txt b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/CMakeLists.txt index afb9d02c..09a0cac0 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/CMakeLists.txt +++ b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/CMakeLists.txt @@ -16,4 +16,15 @@ add_custom_target(run_npu_test COMMAND $ COMMAND echo "===============================================================================" ) -add_dependencies(run_npu_test softmax_custom_npu) \ No newline at end of file +add_dependencies(run_npu_test softmax_custom_npu) + +add_sim_target(OP SoftmaxCustom SRC softmax_custom_main.cpp) + +add_custom_target(run_sim_test + COMMAND echo "===============================================================================" + COMMAND echo " Run SIM test at ${CMAKE_CURRENT_BINARY_DIR}" + COMMAND echo "===============================================================================" + COMMAND $ + COMMAND echo "===============================================================================" + ) +add_dependencies(run_sim_test softmax_custom_sim) \ No newline at end of file diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/softmax_custom_main.cpp b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/softmax_custom_main.cpp index 64fe2c91..4e422a1f 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/softmax_custom_main.cpp +++ b/examples/activation/softmax/kernel_launch_method_by_framework/testcases/npu/softmax_custom_main.cpp @@ -160,9 +160,7 @@ int32_t main(void) } ret = aclnnSoftmaxCustom(workspace, workspaceSize, handle, stream); printf("aclnnSoftmaxCustom ret %u\n", ret); - if (aclrtSynchronizeStreamWithTimeout(stream, 5000) != ACL_SUCCESS) { - printf("Synchronize stream failed\n"); - } + CHECK_ACL(aclrtSynchronizeStream(stream)); uint8_t *zHost, *maxHost, *sumHost; int64_t maxHostSize = GetDataSize(&(tensorDesc[1])); -- Gitee From 0b50905e876206a992f9689807dbb4c932a0fb53 Mon Sep 17 00:00:00 2001 From: bluesky90 Date: Tue, 12 Nov 2024 10:57:30 +0800 Subject: [PATCH 2/2] fix readme --- .../softmax/kernel_launch_method_by_framework/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/activation/softmax/kernel_launch_method_by_framework/README.md b/examples/activation/softmax/kernel_launch_method_by_framework/README.md index 4a5557b1..ea6d2222 100644 --- a/examples/activation/softmax/kernel_launch_method_by_framework/README.md +++ b/examples/activation/softmax/kernel_launch_method_by_framework/README.md @@ -96,6 +96,7 @@ cd build_out - 参考步骤4,重新安装部署自定义算子包。 - 设置仿真环境变量,再执行仿真样例可执行程序,其中SOC_VERSION请参考上述参数说明。 ``` + source $ASCEND_HOME_DIR/bin/setenv.bash export LD_LIBRARY_PATH=${ASCEND_HOME_DIR}/tools/simulator/${SOC_VERSION}/lib:$LD_LIBRARY_PATH ./softmax_custom_sim ``` -- Gitee