@@ -3,6 +3,33 @@ if ("cann${CANN_INSTALL_DIR}" STREQUAL "cann" AND DEFINED ENV{ASCEND_TOOLKIT_HOM
33 message (STATUS "CANN: updated CANN_INSTALL_DIR from ASCEND_TOOLKIT_HOME=$ENV{ASCEND_TOOLKIT_HOME} " )
44endif ()
55
6+ # Auto-detech Soc type and Soc version, if detect failed, will abort build
7+ set (SOC_VERSION "" )
8+ function (detect_ascend_soc_type SOC_VERSION)
9+ execute_process (
10+ COMMAND bash -c "npu-smi info|awk -F' ' 'NF > 0 && NR==7 {print $3}'"
11+ OUTPUT_VARIABLE npu_info
12+ RESULT_VARIABLE npu_result
13+ OUTPUT_STRIP_TRAILING_WHITESPACE
14+ )
15+ if ("${npu_info} " STREQUAL "" OR ${npu_result} )
16+ message (FATAL_ERROR "Auto-detech ascend soc type failed, please specify manually or check ascend device working normally." )
17+ endif ()
18+ set (${SOC_VERSION} "Ascend${npu_info} " PARENT_SCOPE)
19+ endfunction ()
20+
21+ if (NOT SOC_TYPE)
22+ detect_ascend_soc_type(SOC_VERSION)
23+ set (SOC_TYPE "${SOC_VERSION} " )
24+ message (STATUS "CANN: SOC_VERSION auto-detected is:${SOC_VERSION} " )
25+ else ()
26+ string (TOLOWER ${SOC_TYPE} SOC_VERSION)
27+ endif ()
28+
29+ # Construct Soc specify compile option: ASCEND_#Soc_Major_SN. Such as ASCEND_910B, ASCEND310P.
30+ string (REGEX MATCH "[0-9]+[a-zA-Z]" SOC_TYPE_MAJOR_SN "${SOC_VERSION} " )
31+ set (SOC_TYPE_COMPILE_OPTION "ASCEND_${SOC_TYPE_MAJOR_SN} " )
32+
633if (CANN_INSTALL_DIR)
734 # Only Support Linux.
835 if (NOT UNIX )
@@ -39,18 +66,7 @@ if (CANN_INSTALL_DIR)
3966 target_include_directories (ggml-cann PRIVATE . .. ${CANN_INCLUDE_DIRS} )
4067 target_link_directories (ggml-cann PRIVATE ${CANN_INSTALL_DIR} /lib64)
4168
42- # set define macro for the special soc type, as function of some ascend kernel is different for different soc type.
43- set (SOC_VERSION "" )
44- if (NOT SOC_TYPE)
45- detect_ascend_soc_type(SOC_VERSION)
46- set (SOC_TYPE "${SOC_VERSION} " )
47- endif ()
48- string (TOLOWER ${SOC_TYPE} SOC_VERSION)
49- string (FIND "${SOC_VERSION} " "ascend310p" FIRST_310P_INDEX)
50- if (FIRST_310P_INDEX GREATER -1)
51- target_compile_definitions (ggml-cann PRIVATE ASCEND_310P)
52- endif ()
53- message (STATUS "CANN: Compile llama.cpp with ${SOC_TYPE} ." )
69+ target_compile_definitions (ggml-cann PRIVATE "-D${SOC_TYPE_COMPILE_OPTION} " )
5470
5571 message (STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS} " )
5672 message (STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES} " )
0 commit comments