Skip to content

Commit

Permalink
add xnnpack backend (#2585)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcr229 authored and facebook-github-bot committed Mar 23, 2024
1 parent 14e31f0 commit 725c590
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
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

0 comments on commit 725c590

Please sign in to comment.