Skip to content

Commit be29da9

Browse files
committed
Remove the ascend soc_type hard code compile option in CMakelist.txt
1 parent 4201656 commit be29da9

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

ggml/src/ggml-cann/CMakeLists.txt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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}")
44
endif()
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+
633
if (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}")
Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
# Function used to auto-detech soc type, if detect failed, will abort build
2-
set(SOC_VERSION "")
3-
function(detect_ascend_soc_type SOC_VERSION)
4-
execute_process(
5-
COMMAND bash -c "npu-smi info|awk -F' ' 'NF > 0 && NR==7 {print $3}'"
6-
OUTPUT_VARIABLE npu_info
7-
RESULT_VARIABLE npu_result
8-
OUTPUT_STRIP_TRAILING_WHITESPACE
9-
)
10-
if("${npu_info}" STREQUAL "" OR ${npu_result})
11-
message(FATAL_ERROR "Auto-detech ascend soc type failed, please specify manually or check ascend device working normally.")
12-
endif()
13-
set(${SOC_VERSION} "Ascend${npu_info}" PARENT_SCOPE)
14-
endfunction()
15-
16-
if(NOT SOC_TYPE)
17-
detect_ascend_soc_type(SOC_VERSION)
18-
set(SOC_TYPE "${SOC_VERSION}")
19-
endif()
20-
message(STATUS "CANN: device SOC_TYPE:${SOC_TYPE}")
21-
221
file(GLOB SRC_FILES
232
get_row_f32.cpp
243
get_row_f16.cpp
@@ -30,7 +9,6 @@ file(GLOB SRC_FILES
309
dup.cpp
3110
)
3211

33-
string(TOLOWER ${SOC_TYPE} SOC_VERSION)
3412
set(ASCEND_CANN_PACKAGE_PATH ${CANN_INSTALL_DIR})
3513
set(RUN_MODE "npu" CACHE STRING "run mode: npu/sim")
3614

@@ -47,8 +25,6 @@ ascendc_library(ascendc_kernels STATIC
4725
${SRC_FILES}
4826
)
4927

50-
string(FIND "${SOC_VERSION}" "ascend310p" FIRST_310P_INDEX)
51-
if(FIRST_310P_INDEX GREATER -1)
52-
ascendc_compile_definitions(ascendc_kernels PRIVATE -DASCEND_310P)
53-
endif()
28+
message(STATUS "CANN: compile ascend kernels witch SOC_VERSION:${SOC_VERSION}.")
29+
ascendc_compile_definitions(ascendc_kernels PRIVATE "-D${SOC_TYPE_COMPILE_OPTION}")
5430
# ascendc_compile_definitions(ascendc_kernels PRIVATE -DASCENDC_DUMP)

0 commit comments

Comments
 (0)