From 7d348601b0d5cd7713be5ef7dd8453495696c5bc Mon Sep 17 00:00:00 2001 From: yuantao Date: Wed, 16 Jul 2025 07:26:54 +0000 Subject: [PATCH 1/7] support simulator --- .../CMakeDetermineASCENDCompiler.cmake | 24 ++++++++++++++++++- examples/CMakeLists.txt | 8 +++++-- scripts/build.sh | 22 ++++++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/cmake/modules/CMakeDetermineASCENDCompiler.cmake b/cmake/modules/CMakeDetermineASCENDCompiler.cmake index 72b7563f..84c8a927 100644 --- a/cmake/modules/CMakeDetermineASCENDCompiler.cmake +++ b/cmake/modules/CMakeDetermineASCENDCompiler.cmake @@ -33,9 +33,31 @@ set(_CMAKE_ASCEND_HOST_IMPLICIT_LINK_DIRECTORIES set(_CMAKE_ASCEND_HOST_IMPLICIT_LINK_LIBRARIES stdc++ - runtime ) +if(DEFINED ENABLE_SIMULATOR AND ENABLE_SIMULATOR) + if(NOT DEFINED SIMULATOR_NPU_MODEL) + message(WARNING "Simulator mode is enabled but SIMULATOR_NPU_MODEL is not defined. Try get model from LD_LIBRARY_PATH.") + set(LD_LIBRARY_PATH $ENV{LD_LIBRARY_PATH}) + string(REGEX MATCH "simulator/([^:/]*)" SUBDIR "${LD_LIBRARY_PATH}") + if(SUBDIR) + set(SIMULATOR_NPU_MODEL "${CMAKE_MATCH_1}") + message(STATUS "Matched SIMULATOR_NPU_MODEL: ${SIMULATOR_NPU_MODEL}") + else() + message(FATAL_ERROR "No SIMULATOR_NPU_MODEL matched!") + endif() + endif() + + list(APPEND _CMAKE_ASCEND_HOST_IMPLICIT_LINK_DIRECTORIES + ${_CMAKE_ASCEND_HOME_PATH}/tools/simulator/${SIMULATOR_NPU_MODEL}/lib + ${_CMAKE_ASCEND_HOME_PATH}/acllib/lib64/stub) + list(APPEND _CMAKE_ASCEND_HOST_IMPLICIT_LINK_LIBRARIES + runtime_camodel) +else() + list(APPEND _CMAKE_ASCEND_HOST_IMPLICIT_LINK_LIBRARIES + runtime) +endif() + if(DEFINED ENABLE_MSPROF AND ENABLE_MSPROF) list(APPEND _CMAKE_ASCEND_HOST_IMPLICIT_LINK_LIBRARIES profapi) endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index cd138646..8743681b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,11 +11,15 @@ set(CATLASS_EXAMPLES_COMMON_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/common) add_custom_target(catlass_examples) macro(catlass_example_add_executable NAME AICORE_ARCH) + set(EXAMPLE_DESTINATION bin) + if(DEFINED ENABLE_SIMULATOR AND ENABLE_SIMULATOR) + set(EXAMPLE_DESTINATION bin_sim) + endif() add_executable(${NAME} ${ARGN}) target_compile_options(${NAME} PRIVATE $<$:--cce-aicore-arch=${AICORE_ARCH}>) add_dependencies(catlass_examples ${NAME}) - install(TARGETS ${NAME} DESTINATION bin COMPONENT ${NAME}) - install(TARGETS ${NAME} DESTINATION bin COMPONENT catlass_examples) + install(TARGETS ${NAME} DESTINATION ${EXAMPLE_DESTINATION} COMPONENT ${NAME}) + install(TARGETS ${NAME} DESTINATION ${EXAMPLE_DESTINATION} COMPONENT catlass_examples) endmacro() add_compile_options( diff --git a/scripts/build.sh b/scripts/build.sh index b32152c2..adf27fb5 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -12,6 +12,16 @@ set -o errexit set -o nounset set -o pipefail +function get_npu_model(){ + if command -v npu-smi &> /dev/null; then + echo "Ascend$(npu-smi info -t board -i 0 -c 0 | grep \"Chip Name\" | awk '{print $NF}')" + return 1 + else + echo "Ascend910B1" + return 0 + fi +} + RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' @@ -48,6 +58,7 @@ TARGET="" CMAKE_BUILD_TYPE="Release" declare -a CMAKE_OPTIONS=() CLEAN=false +POST_BUILD_HINT="" while [[ $# -gt 0 ]]; do case "$1" in @@ -65,6 +76,13 @@ while [[ $# -gt 0 ]]; do --msdebug) CMAKE_OPTIONS+=("-DENABLE_MSDEBUG=True") ;; + --simulator) + CMAKE_OPTIONS+=("-DENABLE_SIMULATOR=True") + if NPU_MODEL=$(get_npu_model); then + echo -e "${YELLOW}No npu-smi detected, using default model for simulator: ${NPU_MODEL}${NC}" + fi + CMAKE_OPTIONS+=("-DSIMULATOR_NPU_MODEL=${NPU_MODEL}") + POST_BUILD_HINT="Please run \nexport LD_LIBRARY_PATH=${ASCEND_HOME_PATH}/tools/simulator/${NPU_MODEL}/lib:\$LD_LIBRARY_PATH\nin your terminal before execute examples." ;; --enable_msprof) CMAKE_OPTIONS+=("-DENABLE_MSPROF=True") ;; @@ -137,4 +155,6 @@ case "$TARGET" in cmake --install "$BUILD_DIR" --component "$TARGET" echo -e "${GREEN}Target '$TARGET' built successfully${NC}" ;; -esac \ No newline at end of file +esac + +echo -e "$POST_BUILD_HINT" \ No newline at end of file -- Gitee From 7e9b469e3db25190997aaa5444ac782a54ebb6a7 Mon Sep 17 00:00:00 2001 From: yuantao Date: Wed, 16 Jul 2025 07:27:49 +0000 Subject: [PATCH 2/7] support simulator --- scripts/build.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/build.sh b/scripts/build.sh index adf27fb5..90655a91 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -45,6 +45,7 @@ function show_help() { echo " --clean Clean build directories" echo " --debug Build in debug mode" echo " --msdebug Enable msdebug support" + echo " --simulator Compile example in simulator mode" echo " --enable_msprof Enable msprofiling" echo " -D