Skip to content

Commit 725c590

Browse files
mcr229facebook-github-bot
authored andcommitted
add xnnpack backend (#2585)
Summary: Adding support for OSS Dynamically Quantized Linear via XNNPACK for llama models. This now works for cmake on non-mac devices. Additionally, we modified the test-llama-runner-linux work flow and script to test exporting and running via XNNPACK DQ Linear Pull Request resolved: #2585 Reviewed By: kirklandsign Differential Revision: D55229489 Pulled By: mcr229 fbshipit-source-id: 26ffbc6d19567ebc0df3c16389451150cc988d08
1 parent 14e31f0 commit 725c590

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

backends/xnnpack/CMakeLists.txt

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,18 @@ target_include_directories(
7272
xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
7373
${EXECUTORCH_ROOT}/third-party/flatbuffers/include)
7474

75-
set(xnn_executor_runner_libs ${_executor_runner_libs} xnnpack_schema)
76-
77-
list(TRANSFORM _xnnpack_dynamic_quant_utils__srcs PREPEND "${EXECUTORCH_ROOT}/")
78-
add_library(xnnpack_dynamic_quant_utils ${_xnnpack_dynamic_quant_utils__srcs})
79-
set_target_properties(xnnpack_dynamic_quant_utils PROPERTIES LINKER_LANGUAGE
80-
CXX)
81-
target_include_directories(xnnpack_dynamic_quant_utils
82-
PUBLIC ${_common_include_directories})
83-
target_compile_options(xnnpack_dynamic_quant_utils
84-
PUBLIC ${_common_compile_options})
85-
86-
if(ENABLE_DYNAMIC_QUANTIZATION)
87-
list(APPEND xnn_executor_runner_libs xnnpack_dynamic_quant_utils)
88-
endif()
75+
set(xnnpack_third_party)
8976

9077
include(cmake/Dependencies.cmake)
9178

9279
list(TRANSFORM _xnnpack_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
93-
add_library(xnnpack_backend ${_xnnpack_backend__srcs})
94-
target_link_libraries(xnnpack_backend PRIVATE ${xnn_executor_runner_libs})
80+
add_library(xnnpack_backend STATIC ${_xnnpack_backend__srcs})
81+
target_link_libraries(xnnpack_backend
82+
PRIVATE
83+
${xnnpack_third_party}
84+
executorch
85+
xnnpack_schema)
86+
9587
target_include_directories(xnnpack_backend
9688
PUBLIC ${_common_include_directories})
9789
target_include_directories(xnnpack_backend PUBLIC ${XNNPACK_INCLUDE_DIR})
@@ -108,11 +100,12 @@ if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
108100
#
109101
list(TRANSFORM _xnn_executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
110102
add_executable(xnn_executor_runner ${_xnn_executor_runner__srcs})
111-
target_link_libraries(xnn_executor_runner ${xnn_executor_runner_libs})
103+
target_link_libraries(xnn_executor_runner xnnpack_backend gflags)
112104
target_compile_options(xnn_executor_runner PUBLIC ${_common_compile_options})
113-
114-
add_library(xnn_executor_runner_lib STATIC ${_xnn_executor_runner__srcs})
115-
target_link_libraries(xnn_executor_runner_lib ${xnn_executor_runner_libs})
116-
target_compile_options(xnn_executor_runner_lib
117-
PUBLIC ${_common_compile_options})
118105
endif()
106+
107+
install(
108+
TARGETS xnnpack_backend
109+
DESTINATION lib
110+
INCLUDES
111+
DESTINATION ${_common_include_directories})

backends/xnnpack/cmake/Dependencies.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
1818
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
1919
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
2020
add_subdirectory("${CPUINFO_SOURCE_DIR}")
21-
list(APPEND xnn_executor_runner_libs cpuinfo)
21+
list(APPEND xnnpack_third_party cpuinfo)
2222

2323
# --- pthreadpool
2424
set(PTHREADPOOL_SOURCE_DIR "${THIRD_PARTY_ROOT}/pthreadpool")
@@ -27,14 +27,15 @@ set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
2727
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
2828
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
2929
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
30-
list(APPEND xnn_executor_runner_libs pthreadpool)
30+
list(APPEND xnnpack_third_party pthreadpool)
3131

3232
# --- XNNPACK
3333
set(XNNPACK_SOURCE_DIR "${THIRD_PARTY_ROOT}/XNNPACK")
3434
set(XNNPACK_INCLUDE_DIR "${XNNPACK_SOURCE_DIR}/include")
3535
set(XNNPACK_LIBRARY_TYPE "static" CACHE STRING "")
3636
set(XNNPACK_BUILD_BENCHMARKS OFF CACHE BOOL "")
3737
set(XNNPACK_BUILD_TESTS OFF CACHE BOOL "")
38+
set(XNNPACK_ENABLE_AVXVNNI OFF CACHE BOOL "")
3839
add_subdirectory("${XNNPACK_SOURCE_DIR}")
3940
include_directories(SYSTEM ${XNNPACK_INCLUDE_DIR})
40-
list(APPEND xnn_executor_runner_libs XNNPACK)
41+
list(APPEND xnnpack_third_party XNNPACK)

build/executorch-config.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ endif()
4343

4444
set(lib_list
4545
etdump bundled_program extension_data_loader ${FLATCC_LIB} mpsdelegate
46-
qnn_executorch_backend portable_ops_lib extension_module
46+
qnn_executorch_backend portable_ops_lib extension_module xnnpack_backend
47+
XNNPACK cpuinfo pthreadpool
4748
)
4849
foreach(lib ${lib_list})
4950
# Name of the variable which stores result of the find_library search

examples/models/llama2/CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,23 @@ find_package(executorch CONFIG REQUIRED)
5252
# llama_runner library
5353
add_subdirectory(runner)
5454

55-
# XNNPACK pthreadpool cpuinfo
56-
target_link_libraries(llama_main PUBLIC gflags llama_runner portable_ops_lib)
55+
56+
target_link_libraries(llama_main PUBLIC gflags llama_runner
57+
portable_ops_lib)
5758
target_link_options(
5859
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
5960
$<TARGET_FILE:portable_ops_lib> \
6061
LINKER:--no-whole-archive")
62+
63+
# XNNPACK pthreadpool cpuinfo
64+
if(TARGET xnnpack_backend)
65+
set(xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo)
66+
target_link_libraries(llama_main PUBLIC ${xnnpack_backend_libs})
67+
target_link_options(
68+
llama_main PUBLIC "SHELL:LINKER:--whole-archive \
69+
$<TARGET_FILE:xnnpack_backend> \
70+
LINKER:--no-whole-archive")
71+
endif()
6172
target_compile_options(llama_main PUBLIC ${_common_compile_options})
6273

6374
# Print all summary

0 commit comments

Comments
 (0)