Skip to content

Commit 6ef104a

Browse files
larryliu0820facebook-github-bot
authored andcommitted
Consolidate EXECUTORCH_BUILD_CUSTOM option (#2935)
Summary: Currently `EXECUTORCH_BUILD_CUSTOM` is not being respected properly. If this option is false, we should not build `llama2/custom_ops` anywhere. If this option is true, we should build `llama2/custom_ops` in both llama runner binary and pybind. This PR consolidates it. Differential Revision: D55907750
1 parent 5c2b712 commit 6ef104a

File tree

7 files changed

+100
-62
lines changed

7 files changed

+100
-62
lines changed

.ci/scripts/test_llama.sh

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ if [[ -z "${MODE:-}" ]]; then
3737
exit 1
3838
fi
3939

40+
if [[ "${MODE}" =~ xnnpack.* ]]; then
41+
XNNPACK=ON
42+
else
43+
XNNPACK=OFF
44+
fi
45+
46+
if [[ "${MODE}" =~ .*custom.* ]]; then
47+
CUSTOM=ON
48+
else
49+
CUSTOM=OFF
50+
fi
51+
4052
if [[ -z "${BUCK:-}" ]]; then
4153
BUCK=buck2
4254
fi
@@ -47,38 +59,36 @@ fi
4759

4860
which "${PYTHON_EXECUTABLE}"
4961

50-
5162
cmake_install_executorch_libraries() {
5263
echo "Installing libexecutorch.a, libextension_module.so, libportable_ops_lib.a"
5364
rm -rf cmake-out
54-
if [[ "${MODE}" == "xnnpack" ]]; then
55-
XNNPACK=ON
56-
else
57-
XNNPACK=OFF
58-
fi
5965
retry cmake -DBUCK2="$BUCK" \
6066
-DCMAKE_INSTALL_PREFIX=cmake-out \
61-
-DCMAKE_BUILD_TYPE=Release \
67+
-DCMAKE_BUILD_TYPE=Debug \
6268
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
6369
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
70+
-DEXECUTORCH_BUILD_CUSTOM="$CUSTOM" \
6471
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
6572
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
6673
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
6774
-Bcmake-out .
68-
cmake --build cmake-out -j9 --target install --config Release
75+
cmake --build cmake-out -j9 --target install --config Debug
6976
}
7077

7178
cmake_build_llama_runner() {
7279
echo "Building llama runner"
7380
dir="examples/models/llama2"
7481
retry cmake -DBUCK2="$BUCK" \
7582
-DCMAKE_INSTALL_PREFIX=cmake-out \
76-
-DCMAKE_BUILD_TYPE=Release \
83+
-DCMAKE_BUILD_TYPE=Debug \
84+
-DEXECUTORCH_BUILD_CUSTOM="$CUSTOM" \
85+
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
86+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
7787
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
7888
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
7989
-Bcmake-out/${dir} \
8090
${dir}
81-
cmake --build cmake-out/${dir} -j9 --config Release
91+
cmake --build cmake-out/${dir} -j9 --config Debug
8292

8393
}
8494

@@ -117,9 +127,10 @@ fi
117127
EXPORTED_MODEL_NAME="${EXPORTED_MODEL_NAME}.pte"
118128
echo "Exporting ${EXPORTED_MODEL_NAME}"
119129
EXPORT_ARGS="-c stories110M.pt -p ${PARAMS} -d ${DTYPE} -n ${EXPORTED_MODEL_NAME}"
120-
if [[ "${MODE}" == "xnnpack" ]]; then
130+
if [[ "${MODE}" == "xnnpack+kv+custom" ]]; then
121131
EXPORT_ARGS="${EXPORT_ARGS} -kv --use_sdpa_with_kv_cache -X -qmode 8da4w -G 128"
122132
fi
133+
# Add dynamically linked library location
123134
$PYTHON_EXECUTABLE -m examples.models.llama2.export_llama ${EXPORT_ARGS}
124135

125136
# Create tokenizer.bin.

.github/workflows/pull.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ jobs:
9090
matrix:
9191
dtype: [fp32]
9292
build-tool: [buck2, cmake]
93-
mode: [portable, xnnpack]
93+
mode: [portable, xnnpack+kv+custom]
9494
fail-fast: false
9595
with:
9696
runner: linux.2xlarge

.github/workflows/trunk.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ jobs:
254254
matrix:
255255
dtype: [fp32]
256256
build-tool: [buck2, cmake]
257-
mode: [portable, xnnpack]
257+
mode: [portable, xnnpack+kv+custom]
258258
fail-fast: false
259259
with:
260260
runner: macos-m1-stable

CMakeLists.txt

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,20 @@ option(EXECUTORCH_BUILD_VULKAN "Build the Vulkan backend" OFF)
175175
#
176176
# pthreadpool: build pthreadpool library. Disable on unsupported platforms
177177
#
178-
cmake_dependent_option(EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library."
179-
ON "NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF)
178+
cmake_dependent_option(
179+
EXECUTORCH_BUILD_PTHREADPOOL "Build pthreadpool library." ON
180+
"NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF)
180181

181182
#
182183
# cpuinfo: build cpuinfo library. Disable on unsupported platforms
183184
#
184185
cmake_dependent_option(EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON
185186
"NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF)
186187

188+
if(EXECUTORCH_BUILD_CUSTOM)
189+
set(EXECUTORCH_BUILD_OPTIMIZED ON)
190+
endif()
191+
187192
if(EXECUTORCH_BUILD_CPUINFO)
188193
# --- cpuinfo
189194
set(CPUINFO_SOURCE_DIR "backends/xnnpack/third-party/cpuinfo")
@@ -508,24 +513,38 @@ if(EXECUTORCH_BUILD_PYBIND)
508513
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sdk)
509514
endif()
510515

516+
# find pytorch lib, to allow pybind to take at::Tensor as input/output
517+
find_package(Torch CONFIG REQUIRED)
518+
find_library(TORCH_PYTHON_LIBRARY torch_python
519+
PATHS "${TORCH_INSTALL_PREFIX}/lib")
520+
521+
set(_dep_libs
522+
${TORCH_PYTHON_LIBRARY}
523+
bundled_program
524+
etdump
525+
executorch
526+
extension_data_loader
527+
portable_ops_lib
528+
util
529+
torch)
530+
511531
if(EXECUTORCH_BUILD_COREML)
512-
set(PYBIND_LINK_COREML "coremldelegate")
532+
list(APPEND _dep_libs coremldelegate)
513533
endif()
514534

515535
if(EXECUTORCH_BUILD_MPS)
516-
set(PYBIND_LINK_MPS "mpsdelegate")
536+
list(APPEND _dep_libs mpsdelegate)
517537
endif()
518538

519539
if(EXECUTORCH_BUILD_XNNPACK)
520-
# need to explicitly specify XNNPACK here
521-
# otherwise uses XNNPACK symbols from libtorch_cpu
522-
set(PYBIND_LINK_XNNPACK xnnpack_backend XNNPACK)
540+
# need to explicitly specify XNNPACK here otherwise uses XNNPACK symbols
541+
# from libtorch_cpu
542+
list(APPEND _dep_libs xnnpack_backend XNNPACK)
523543
endif()
524544

525-
# find pytorch lib, to allow pybind to take at::Tensor as input/output
526-
find_package(Torch CONFIG REQUIRED)
527-
find_library(TORCH_PYTHON_LIBRARY torch_python
528-
PATHS "${TORCH_INSTALL_PREFIX}/lib")
545+
if(EXECUTORCH_BUILD_CUSTOM)
546+
list(APPEND _dep_libs custom_ops_lib)
547+
endif()
529548

530549
# compile options for pybind
531550

@@ -548,19 +567,7 @@ if(EXECUTORCH_BUILD_PYBIND)
548567
PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=portable_lib)
549568
target_include_directories(portable_lib PRIVATE ${TORCH_INCLUDE_DIRS})
550569
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
551-
target_link_libraries(
552-
portable_lib
553-
PUBLIC ${TORCH_PYTHON_LIBRARY}
554-
bundled_program
555-
etdump
556-
executorch
557-
extension_data_loader
558-
portable_ops_lib
559-
util
560-
torch
561-
${PYBIND_LINK_COREML}
562-
${PYBIND_LINK_MPS}
563-
${PYBIND_LINK_XNNPACK})
570+
target_link_libraries(portable_lib PUBLIC ${_dep_libs})
564571

565572
install(TARGETS portable_lib
566573
LIBRARY DESTINATION executorch/extension/pybindings)

examples/demo-apps/android/LlamaDemo/setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
1616
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
1717
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
1818
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
19+
-DEXECUTORCH_BUILD_CUSTOM=ON \
1920
-DCMAKE_BUILD_TYPE=Release \
2021
-B"${CMAKE_OUT}"
2122

examples/models/llama2/CMakeLists.txt

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,56 +49,72 @@ set(_common_compile_options -Wno-deprecated-declarations -fPIC)
4949
# Let files say "include <executorch/path/to/header.h>".
5050
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
5151

52-
# For some reason android build is not able to find where gflags is
53-
# and hence cannot find corresponding .cmake file
52+
# For some reason android build is not able to find where gflags is and hence
53+
# cannot find corresponding .cmake file
5454
set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party/gflags)
5555
find_package(gflags REQUIRED)
5656

5757
#
5858
# llama_main: test binary to run llama, with tokenizer and sampler integrated
5959
#
60-
add_executable(llama_main main.cpp
61-
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/threadpool/cpuinfo_utils.cpp)
62-
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
63-
target_link_options(llama_main PRIVATE "LINKER:--gc-sections")
64-
endif()
6560

66-
# find `executorch` libraries
67-
# Same as for gflags
61+
# find `executorch` libraries Same as for gflags
6862
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch)
6963
find_package(executorch CONFIG REQUIRED)
7064
if(CMAKE_TOOLCHAIN_IOS OR ANDROID)
7165
target_link_options_shared_lib(executorch)
7266
endif()
7367

7468
# custom ops library
75-
add_subdirectory(custom_ops)
69+
if(EXECUTORCH_BUILD_CUSTOM)
70+
add_subdirectory(custom_ops)
71+
endif()
7672

7773
# llama_runner library
7874
add_subdirectory(runner)
7975

80-
target_include_directories(llama_main PUBLIC
81-
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/third-party/cpuinfo/include)
82-
target_include_directories(llama_main PUBLIC
83-
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/third-party/pthreadpool/include)
84-
8576
set(link_libraries)
77+
set(_srcs main.cpp)
8678

8779
if(EXECUTORCH_BUILD_OPTIMIZED)
88-
list(APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels
89-
portable_kernels cpublas eigen_blas)
80+
list(
81+
APPEND
82+
link_libraries
83+
optimized_native_cpu_ops_lib
84+
optimized_kernels
85+
portable_kernels
86+
cpublas
87+
eigen_blas)
9088
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
9189
else()
9290
list(APPEND link_libraries portable_ops_lib portable_kernels)
9391
target_link_options_shared_lib(portable_ops_lib)
9492
endif()
9593

96-
target_link_libraries(llama_main PUBLIC gflags llama_runner custom_ops_lib)
94+
if(EXECUTORCH_BUILD_CUSTOM)
95+
target_link_options_shared_lib(custom_ops_lib)
96+
list(APPEND link_libraries custom_ops_lib)
97+
endif()
9798

9899
# XNNPACK pthreadpool cpuinfo
99100
if(TARGET xnnpack_backend)
100101
set(xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo)
101102
list(APPEND link_libraries ${xnnpack_backend_libs})
103+
# HACK: main only include these when xnnpack backend is availabe, so that we
104+
# have all the threadpool sources under xnnpack.
105+
list(APPEND _common_compile_options -DET_USE_THREADPOOL)
106+
list(
107+
APPEND
108+
_srcs
109+
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/threadpool/cpuinfo_utils.cpp
110+
)
111+
list(
112+
APPEND
113+
_common_include_directories
114+
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/third-party/cpuinfo/include
115+
${CMAKE_CURRENT_SOURCE_DIR}/../../../backends/xnnpack/third-party/pthreadpool/include
116+
)
117+
# end of hack
102118
target_link_options_shared_lib(xnnpack_backend)
103119
endif()
104120

@@ -114,15 +130,19 @@ if(TARGET qnn_executorch_backend)
114130
target_link_options_shared_lib(qnn_executorch_backend)
115131
endif()
116132

117-
# This one is needed for cpuinfo where it uses android
118-
# specific log lib
133+
# This one is needed for cpuinfo where it uses android specific log lib
119134
if(ANDROID)
120135
list(APPEND link_libraries log)
121136
endif()
122137

123-
target_compile_options(llama_main PUBLIC ${_common_compile_options}
124-
-DET_USE_THREADPOOL)
125-
target_link_libraries(llama_main PUBLIC ${link_libraries})
138+
add_executable(llama_main ${_srcs})
139+
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
140+
target_link_options(llama_main PRIVATE "LINKER:--gc-sections")
141+
endif()
142+
143+
target_include_directories(llama_main PUBLIC ${_common_include_directories})
144+
target_link_libraries(llama_main PUBLIC gflags llama_runner ${link_libraries})
145+
target_compile_options(llama_main PUBLIC ${_common_compile_options})
126146

127147
if(APPLE)
128148
target_link_options_shared_lib(executorch)

examples/models/llama2/runner/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ else()
4747
add_library(llama_runner SHARED ${_llama_runner__srcs})
4848
endif()
4949

50-
set(llama_runner_deps executorch extension_module extension_data_loader
51-
custom_ops)
50+
set(llama_runner_deps executorch extension_module extension_data_loader)
5251

5352
target_link_libraries(
5453
llama_runner PUBLIC ${llama_runner_deps})

0 commit comments

Comments
 (0)