Skip to content

[llama runner] add xnnpack backend #2585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
37 changes: 15 additions & 22 deletions backends/xnnpack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,18 @@ target_include_directories(
xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
${EXECUTORCH_ROOT}/third-party/flatbuffers/include)

set(xnn_executor_runner_libs ${_executor_runner_libs} xnnpack_schema)

list(TRANSFORM _xnnpack_dynamic_quant_utils__srcs PREPEND "${EXECUTORCH_ROOT}/")
add_library(xnnpack_dynamic_quant_utils ${_xnnpack_dynamic_quant_utils__srcs})
set_target_properties(xnnpack_dynamic_quant_utils PROPERTIES LINKER_LANGUAGE
CXX)
target_include_directories(xnnpack_dynamic_quant_utils
PUBLIC ${_common_include_directories})
target_compile_options(xnnpack_dynamic_quant_utils
PUBLIC ${_common_compile_options})

if(ENABLE_DYNAMIC_QUANTIZATION)
list(APPEND xnn_executor_runner_libs xnnpack_dynamic_quant_utils)
endif()
set(xnnpack_third_party)

include(cmake/Dependencies.cmake)

list(TRANSFORM _xnnpack_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
add_library(xnnpack_backend ${_xnnpack_backend__srcs})
target_link_libraries(xnnpack_backend PRIVATE ${xnn_executor_runner_libs})
add_library(xnnpack_backend STATIC ${_xnnpack_backend__srcs})
target_link_libraries(xnnpack_backend
PRIVATE
${xnnpack_third_party}
executorch
xnnpack_schema)

target_include_directories(xnnpack_backend
PUBLIC ${_common_include_directories})
target_include_directories(xnnpack_backend PUBLIC ${XNNPACK_INCLUDE_DIR})
Expand All @@ -108,11 +100,12 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
#
list(TRANSFORM _xnn_executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
target_link_libraries(xnn_executor_runner ${xnn_executor_runner_libs})
target_link_libraries(xnn_executor_runner xnnpack_backend gflags)
target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})

add_library(xnn_executor_runner_lib STATIC ${_xnn_executor_runner__srcs})
target_link_libraries(xnn_executor_runner_lib ${xnn_executor_runner_libs})
target_compile_options(xnn_executor_runner_lib
PUBLIC ${_common_compile_options})
endif()

install(
TARGETS xnnpack_backend
DESTINATION lib
INCLUDES
DESTINATION ${_common_include_directories})
7 changes: 4 additions & 3 deletions backends/xnnpack/cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
add_subdirectory("${CPUINFO_SOURCE_DIR}")
list(APPEND xnn_executor_runner_libs cpuinfo)
list(APPEND xnnpack_third_party cpuinfo)

# --- pthreadpool
set(PTHREADPOOL_SOURCE_DIR "${THIRD_PARTY_ROOT}/pthreadpool")
Expand All @@ -27,14 +27,15 @@ set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
list(APPEND xnn_executor_runner_libs pthreadpool)
list(APPEND xnnpack_third_party pthreadpool)

# --- XNNPACK
set(XNNPACK_SOURCE_DIR "${THIRD_PARTY_ROOT}/XNNPACK")
set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include")
set(XNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
set(XNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
set(XNNPACK_BUILD_TESTS OFF CACHE BOOL "")
set(XNNPACK_ENABLE_AVXVNNI OFF CACHE BOOL "")
add_subdirectory("${XNNPACK_SOURCE_DIR}")
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
list(APPEND xnn_executor_runner_libs XNNPACK)
list(APPEND xnnpack_third_party XNNPACK)
3 changes: 2 additions & 1 deletion build/executorch-config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ endif()

set(lib_list
etdump bundled_program extension_data_loader ${FLATCC_LIB} mpsdelegate
qnn_executorch_backend portable_ops_lib extension_module
qnn_executorch_backend portable_ops_lib extension_module xnnpack_backend
XNNPACK cpuinfo pthreadpool
)
foreach(lib ${lib_list})
# Name of the variable which stores result of the find_library search
Expand Down
15 changes: 13 additions & 2 deletions examples/models/llama2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,23 @@ find_package(executorch CONFIG REQUIRED)
# llama_runner library
add_subdirectory(runner)

# XNNPACK pthreadpool cpuinfo
target_link_libraries(llama_main PUBLIC gflags llama_runner portable_ops_lib)

target_link_libraries(llama_main PUBLIC gflags llama_runner
portable_ops_lib)
target_link_options(
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
$<TARGET_FILE:portable_ops_lib> \
LINKER:--no-whole-archive")

# XNNPACK pthreadpool cpuinfo
if(TARGET xnnpack_backend)
set(xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo)
target_link_libraries(llama_main PUBLIC ${xnnpack_backend_libs})
target_link_options(
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
$<TARGET_FILE:xnnpack_backend> \
LINKER:--no-whole-archive")
endif()
target_compile_options(llama_main PUBLIC ${_common_compile_options})

# Print all summary
Expand Down