From be29da9d4acdf995e406bc6146c070815a1312cd Mon Sep 17 00:00:00 2001 From: leo-pony Date: Thu, 21 Nov 2024 21:19:14 +0800 Subject: [PATCH] Remove the ascend soc_type hard code compile option in CMakelist.txt --- ggml/src/ggml-cann/CMakeLists.txt | 40 ++++++++++++++++------- ggml/src/ggml-cann/kernels/CMakeLists.txt | 28 ++-------------- 2 files changed, 30 insertions(+), 38 deletions(-) diff --git a/ggml/src/ggml-cann/CMakeLists.txt b/ggml/src/ggml-cann/CMakeLists.txt index d60b8af63..756200b89 100644 --- a/ggml/src/ggml-cann/CMakeLists.txt +++ b/ggml/src/ggml-cann/CMakeLists.txt @@ -3,6 +3,33 @@ if ("cann${CANN_INSTALL_DIR}" STREQUAL "cann" AND DEFINED ENV{ASCEND_TOOLKIT_HOM message(STATUS "CANN: updated CANN_INSTALL_DIR from ASCEND_TOOLKIT_HOME=$ENV{ASCEND_TOOLKIT_HOME}") endif() +# Auto-detech Soc type and Soc version, if detect failed, will abort build +set(SOC_VERSION "") +function(detect_ascend_soc_type SOC_VERSION) + execute_process( + COMMAND bash -c "npu-smi info|awk -F' ' 'NF > 0 && NR==7 {print $3}'" + OUTPUT_VARIABLE npu_info + RESULT_VARIABLE npu_result + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if("${npu_info}" STREQUAL "" OR ${npu_result}) + message(FATAL_ERROR "Auto-detech ascend soc type failed, please specify manually or check ascend device working normally.") + endif() + set(${SOC_VERSION} "Ascend${npu_info}" PARENT_SCOPE) +endfunction() + +if(NOT SOC_TYPE) + detect_ascend_soc_type(SOC_VERSION) + set(SOC_TYPE "${SOC_VERSION}") + message(STATUS "CANN: SOC_VERSION auto-detected is:${SOC_VERSION}") +else() + string(TOLOWER ${SOC_TYPE} SOC_VERSION) +endif() + +# Construct Soc specify compile option: ASCEND_#Soc_Major_SN. Such as ASCEND_910B, ASCEND310P. +string(REGEX MATCH "[0-9]+[a-zA-Z]" SOC_TYPE_MAJOR_SN "${SOC_VERSION}") +set(SOC_TYPE_COMPILE_OPTION "ASCEND_${SOC_TYPE_MAJOR_SN}") + if (CANN_INSTALL_DIR) # Only Support Linux. if (NOT UNIX) @@ -39,18 +66,7 @@ if (CANN_INSTALL_DIR) target_include_directories(ggml-cann PRIVATE . .. ${CANN_INCLUDE_DIRS}) target_link_directories(ggml-cann PRIVATE ${CANN_INSTALL_DIR}/lib64) - # set define macro for the special soc type, as function of some ascend kernel is different for different soc type. - set(SOC_VERSION "") - if(NOT SOC_TYPE) - detect_ascend_soc_type(SOC_VERSION) - set(SOC_TYPE "${SOC_VERSION}") - endif() - string(TOLOWER ${SOC_TYPE} SOC_VERSION) - string(FIND "${SOC_VERSION}" "ascend310p" FIRST_310P_INDEX) - if(FIRST_310P_INDEX GREATER -1) - target_compile_definitions(ggml-cann PRIVATE ASCEND_310P) - endif() - message(STATUS "CANN: Compile llama.cpp with ${SOC_TYPE}.") + target_compile_definitions(ggml-cann PRIVATE "-D${SOC_TYPE_COMPILE_OPTION}") message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}") message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}") diff --git a/ggml/src/ggml-cann/kernels/CMakeLists.txt b/ggml/src/ggml-cann/kernels/CMakeLists.txt index d1e107fc9..6a4e17cce 100644 --- a/ggml/src/ggml-cann/kernels/CMakeLists.txt +++ b/ggml/src/ggml-cann/kernels/CMakeLists.txt @@ -1,24 +1,3 @@ -# Function used to auto-detech soc type, if detect failed, will abort build -set(SOC_VERSION "") -function(detect_ascend_soc_type SOC_VERSION) - execute_process( - COMMAND bash -c "npu-smi info|awk -F' ' 'NF > 0 && NR==7 {print $3}'" - OUTPUT_VARIABLE npu_info - RESULT_VARIABLE npu_result - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - if("${npu_info}" STREQUAL "" OR ${npu_result}) - message(FATAL_ERROR "Auto-detech ascend soc type failed, please specify manually or check ascend device working normally.") - endif() - set(${SOC_VERSION} "Ascend${npu_info}" PARENT_SCOPE) -endfunction() - -if(NOT SOC_TYPE) - detect_ascend_soc_type(SOC_VERSION) - set(SOC_TYPE "${SOC_VERSION}") -endif() -message(STATUS "CANN: device SOC_TYPE:${SOC_TYPE}") - file(GLOB SRC_FILES get_row_f32.cpp get_row_f16.cpp @@ -30,7 +9,6 @@ file(GLOB SRC_FILES dup.cpp ) -string(TOLOWER ${SOC_TYPE} SOC_VERSION) set(ASCEND_CANN_PACKAGE_PATH ${CANN_INSTALL_DIR}) set(RUN_MODE "npu" CACHE STRING "run mode: npu/sim") @@ -47,8 +25,6 @@ ascendc_library(ascendc_kernels STATIC ${SRC_FILES} ) -string(FIND "${SOC_VERSION}" "ascend310p" FIRST_310P_INDEX) -if(FIRST_310P_INDEX GREATER -1) - ascendc_compile_definitions(ascendc_kernels PRIVATE -DASCEND_310P) -endif() +message(STATUS "CANN: compile ascend kernels witch SOC_VERSION:${SOC_VERSION}.") +ascendc_compile_definitions(ascendc_kernels PRIVATE "-D${SOC_TYPE_COMPILE_OPTION}") # ascendc_compile_definitions(ascendc_kernels PRIVATE -DASCENDC_DUMP)