1if ("cann${CANN_INSTALL_DIR}" STREQUAL "cann" AND DEFINED ENV{ASCEND_TOOLKIT_HOME})
2 set(CANN_INSTALL_DIR $ENV{ASCEND_TOOLKIT_HOME})
3 message(STATUS "CANN: updated CANN_INSTALL_DIR from ASCEND_TOOLKIT_HOME=$ENV{ASCEND_TOOLKIT_HOME}")
4endif()
5
6# Auto-detech Soc type and Soc version, if detect failed, will abort build
7set(SOC_VERSION "")
8function(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)
19endfunction()
20
21if(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}")
25endif()
26
27string(TOLOWER ${SOC_TYPE} SOC_VERSION) # SOC_VERSION need lower
28
29# Construct Soc specify compile option: ASCEND_#Soc_Major_SN. Such as ASCEND_910B, ASCEND_310P.
30string(REGEX MATCH "[0-9]+[a-zA-Z]" SOC_TYPE_MAJOR_SN "${SOC_VERSION}")
31set(SOC_TYPE_COMPILE_OPTION "ASCEND_${SOC_TYPE_MAJOR_SN}")
32string(TOUPPER ${SOC_TYPE_COMPILE_OPTION} SOC_TYPE_COMPILE_OPTION)
33message(STATUS "CANN: SOC_VERSION = ${SOC_VERSION}")
34option(USE_ACL_GRAPH "Enable CANN graph execution (ACL graph mode)" OFF)
35
36if(USE_ACL_GRAPH AND (SOC_TYPE_MAJOR_SN STREQUAL "310P" OR SOC_TYPE_COMPILE_OPTION STREQUAL "ASCEND_310P"))
37 message(FATAL_ERROR
38 "CANN Graph (ACL graph mode) is not supported on 310P devices. "
39 "Please build with -DUSE_ACL_GRAPH=OFF or use a supported SOC.")
40endif()
41
42if (CANN_INSTALL_DIR)
43 # Only Support Linux.
44 if (NOT UNIX)
45 message(FATAL_ERROR "CANN: CANN toolkit supports unix but not ${CMAKE_SYSTEM_NAME}")
46 endif()
47
48 # Supported platforms: x86-64, arm64
49 if (CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
50 elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
51 else()
52 message(FATAL_ERROR "CANN: CANN toolkit supports x86-64 and arm64 but not ${CMAKE_SYSTEM_PROCESSOR}")
53 endif()
54
55 # Set header and libs
56 set(CANN_INCLUDE_DIRS
57 ${CANN_INSTALL_DIR}/include
58 ${CANN_INSTALL_DIR}/include/aclnn
59 ${CANN_INSTALL_DIR}/acllib/include
60 )
61
62 list(APPEND CANN_LIBRARIES
63 ascendcl
64 nnopbase
65 opapi
66 acl_op_compiler
67 )
68
69 file(GLOB GGML_SOURCES_CANN "*.cpp")
70
71 ggml_add_backend_library(ggml-cann ${GGML_SOURCES_CANN})
72 target_link_libraries(ggml-cann PRIVATE ${CANN_LIBRARIES})
73 target_include_directories(ggml-cann PRIVATE ${CANN_INCLUDE_DIRS})
74 target_link_directories(ggml-cann PRIVATE ${CANN_INSTALL_DIR}/lib64)
75
76 target_compile_definitions(ggml-cann PRIVATE "-D${SOC_TYPE_COMPILE_OPTION}")
77
78 if (USE_ACL_GRAPH)
79 target_compile_definitions(ggml-cann PRIVATE USE_ACL_GRAPH)
80 message(STATUS "CANN: USE_ACL_GRAPH is enabled.")
81 else()
82 message(STATUS "CANN: USE_ACL_GRAPH is disabled.")
83 endif()
84
85 message(STATUS "CANN: CANN_INCLUDE_DIRS = ${CANN_INCLUDE_DIRS}")
86 message(STATUS "CANN: CANN_LIBRARIES = ${CANN_LIBRARIES}")
87else()
88 message(FATAL_ERROR "CANN: Can't find CANN_INSTALL_DIR, did you forget to source set_var.sh?")
89endif()