Skip to content

Commit 643c628

Browse files
larryliu0820facebook-github-bot
authored andcommitted
Revert "Use new API to register custom ops for llama model (#2840)" (#2912)
Summary: This reverts commit 020d8be. Pull Request resolved: #2912 Reviewed By: shoumikhin Differential Revision: D55852547 Pulled By: larryliu0820 fbshipit-source-id: c8528041c03196239d6daef7e2843ee5cf8a8f3d
1 parent 61ad48d commit 643c628

20 files changed

+286
-384
lines changed

.ci/scripts/test_llama.sh

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,6 @@ 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-
5240
if [[ -z "${BUCK:-}" ]]; then
5341
BUCK=buck2
5442
fi
@@ -59,39 +47,38 @@ fi
5947

6048
which "${PYTHON_EXECUTABLE}"
6149

62-
CMAKE_PREFIX_PATH=$($PYTHON_EXECUTABLE -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
6350

6451
cmake_install_executorch_libraries() {
6552
echo "Installing libexecutorch.a, libextension_module.so, libportable_ops_lib.a"
6653
rm -rf cmake-out
54+
if [[ "${MODE}" == "xnnpack" ]]; then
55+
XNNPACK=ON
56+
else
57+
XNNPACK=OFF
58+
fi
6759
retry cmake -DBUCK2="$BUCK" \
6860
-DCMAKE_INSTALL_PREFIX=cmake-out \
69-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
70-
-DCMAKE_BUILD_TYPE=Debug \
61+
-DCMAKE_BUILD_TYPE=Release \
7162
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
7263
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
73-
-DEXECUTORCH_BUILD_CUSTOM="$CUSTOM" \
7464
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
7565
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
7666
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
7767
-Bcmake-out .
78-
cmake --build cmake-out -j9 --target install --config Debug
68+
cmake --build cmake-out -j9 --target install --config Release
7969
}
8070

8171
cmake_build_llama_runner() {
8272
echo "Building llama runner"
8373
dir="examples/models/llama2"
8474
retry cmake -DBUCK2="$BUCK" \
8575
-DCMAKE_INSTALL_PREFIX=cmake-out \
86-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
87-
-DCMAKE_BUILD_TYPE=Debug \
88-
-DEXECUTORCH_BUILD_CUSTOM="$CUSTOM" \
76+
-DCMAKE_BUILD_TYPE=Release \
8977
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
90-
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
9178
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
9279
-Bcmake-out/${dir} \
9380
${dir}
94-
cmake --build cmake-out/${dir} -j9 --config Debug
81+
cmake --build cmake-out/${dir} -j9 --config Release
9582

9683
}
9784

@@ -126,20 +113,13 @@ else
126113
exit 1
127114
fi
128115

129-
# Install custom ops before exporting
130-
echo "Installing executorch libraries"
131-
cmake_install_executorch_libraries
132-
133116
# Export model.
134117
EXPORTED_MODEL_NAME="${EXPORTED_MODEL_NAME}.pte"
135118
echo "Exporting ${EXPORTED_MODEL_NAME}"
136119
EXPORT_ARGS="-c stories110M.pt -p ${PARAMS} -d ${DTYPE} -n ${EXPORTED_MODEL_NAME}"
137-
if [[ "${MODE}" == "xnnpack+kv+custom" ]]; then
120+
if [[ "${MODE}" == "xnnpack" ]]; then
138121
EXPORT_ARGS="${EXPORT_ARGS} -kv --use_sdpa_with_kv_cache -X -qmode 8da4w -G 128"
139122
fi
140-
# Add dynamically linked library location
141-
export LD_LIBRARY_PATH=${PWD}/cmake-out/lib
142-
export DYLD_LIBRARY_PATH=${PWD}/cmake-out/lib
143123
$PYTHON_EXECUTABLE -m examples.models.llama2.export_llama ${EXPORT_ARGS}
144124

145125
# Create tokenizer.bin.
@@ -155,6 +135,7 @@ if [[ "${BUILD_TOOL}" == "buck2" ]]; then
155135
# shellcheck source=/dev/null
156136
$BUCK run examples/models/llama2:main -- ${RUNTIME_ARGS} > result.txt
157137
elif [[ "${BUILD_TOOL}" == "cmake" ]]; then
138+
cmake_install_executorch_libraries
158139
cmake_build_llama_runner
159140
# Run llama runner
160141
NOW=$(date +"%H:%M:%S")

.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+kv+custom]
93+
mode: [portable, xnnpack]
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+kv+custom]
257+
mode: [portable, xnnpack]
258258
fail-fast: false
259259
with:
260260
runner: macos-m1-stable

CMakeLists.txt

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

182181
#
183182
# cpuinfo: build cpuinfo library. Disable on unsupported platforms
@@ -187,9 +186,6 @@ cmake_dependent_option(EXECUTORCH_BUILD_CPUINFO "Build cpuinfo library." ON
187186

188187
if(EXECUTORCH_BUILD_CPUINFO)
189188
# --- cpuinfo
190-
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
191-
${CMAKE_POSITION_INDEPENDENT_CODE})
192-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
193189
set(CPUINFO_SOURCE_DIR "backends/xnnpack/third-party/cpuinfo")
194190
set(CPUINFO_BUILD_TOOLS
195191
OFF
@@ -211,15 +207,10 @@ if(EXECUTORCH_BUILD_CPUINFO)
211207
CACHE STRING "")
212208
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
213209
add_subdirectory("${CPUINFO_SOURCE_DIR}")
214-
set(CMAKE_POSITION_INDEPENDENT_CODE
215-
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG})
216210
endif()
217211

218212
if(EXECUTORCH_BUILD_PTHREADPOOL)
219213
# --- pthreadpool
220-
set(ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG
221-
${CMAKE_POSITION_INDEPENDENT_CODE})
222-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
223214
set(PTHREADPOOL_SOURCE_DIR "backends/xnnpack/third-party/pthreadpool")
224215
set(PTHREADPOOL_BUILD_TESTS
225216
OFF
@@ -239,8 +230,6 @@ if(EXECUTORCH_BUILD_PTHREADPOOL)
239230
CACHE STRING "")
240231
endif()
241232
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
242-
set(CMAKE_POSITION_INDEPENDENT_CODE
243-
${ORIGINAL_CMAKE_POSITION_INDEPENDENT_CODE_FLAG})
244233
endif()
245234

246235
if(NOT PYTHON_EXECUTABLE)
@@ -515,38 +504,25 @@ if(EXECUTORCH_BUILD_PYBIND)
515504
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sdk)
516505
endif()
517506

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

537511
if(EXECUTORCH_BUILD_MPS)
538-
list(APPEND _dep_libs mpsdelegate)
512+
set(PYBIND_LINK_MPS "mpsdelegate")
539513
endif()
540514

541515
if(EXECUTORCH_BUILD_XNNPACK)
542-
# need to explicitly specify XNNPACK here otherwise uses XNNPACK symbols
543-
# from libtorch_cpu
544-
list(APPEND _dep_libs xnnpack_backend XNNPACK)
516+
# need to explicitly specify XNNPACK here
517+
# otherwise uses XNNPACK symbols from libtorch_cpu
518+
set(PYBIND_LINK_XNNPACK xnnpack_backend XNNPACK)
545519
endif()
546520

547-
if(EXECUTORCH_BUILD_CUSTOM)
548-
list(APPEND _dep_libs custom_ops custom_ops_aot_lib)
549-
endif()
521+
# find pytorch lib, to allow pybind to take at::Tensor as input/output
522+
find_package(Torch CONFIG REQUIRED)
523+
find_library(TORCH_PYTHON_LIBRARY torch_python
524+
PATHS "${TORCH_INSTALL_PREFIX}/lib")
525+
550526
# compile options for pybind
551527

552528
set(_pybind_compile_options -Wno-deprecated-declarations -fPIC -frtti
@@ -568,7 +544,19 @@ if(EXECUTORCH_BUILD_PYBIND)
568544
PUBLIC EXECUTORCH_PYTHON_MODULE_NAME=portable_lib)
569545
target_include_directories(portable_lib PRIVATE ${TORCH_INCLUDE_DIRS})
570546
target_compile_options(portable_lib PUBLIC ${_pybind_compile_options})
571-
target_link_libraries(portable_lib PUBLIC ${_dep_libs})
547+
target_link_libraries(
548+
portable_lib
549+
PUBLIC ${TORCH_PYTHON_LIBRARY}
550+
bundled_program
551+
etdump
552+
executorch
553+
extension_data_loader
554+
portable_ops_lib
555+
util
556+
torch
557+
${PYBIND_LINK_COREML}
558+
${PYBIND_LINK_MPS}
559+
${PYBIND_LINK_XNNPACK})
572560

573561
install(TARGETS portable_lib
574562
LIBRARY DESTINATION executorch/extension/pybindings)

examples/models/llama2/CMakeLists.txt

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,72 +49,56 @@ 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 and hence
53-
# cannot find corresponding .cmake file
52+
# For some reason android build is not able to find where gflags is
53+
# and hence 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()
6065

61-
# find `executorch` libraries Same as for gflags
66+
# find `executorch` libraries
67+
# Same as for gflags
6268
set(executorch_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../lib/cmake/ExecuTorch)
6369
find_package(executorch CONFIG REQUIRED)
6470
if(CMAKE_TOOLCHAIN_IOS OR ANDROID)
6571
target_link_options_shared_lib(executorch)
6672
endif()
6773

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

7377
# llama_runner library
7478
add_subdirectory(runner)
7579

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+
7685
set(link_libraries)
77-
set(_srcs main.cpp)
7886

7987
if(EXECUTORCH_BUILD_OPTIMIZED)
80-
list(
81-
APPEND
82-
link_libraries
83-
optimized_native_cpu_ops_lib
84-
optimized_kernels
85-
portable_kernels
86-
cpublas
87-
eigen_blas)
88+
list(APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels
89+
portable_kernels cpublas eigen_blas)
8890
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
8991
else()
9092
list(APPEND link_libraries portable_ops_lib portable_kernels)
9193
target_link_options_shared_lib(portable_ops_lib)
9294
endif()
9395

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

9998
# XNNPACK pthreadpool cpuinfo
10099
if(TARGET xnnpack_backend)
101100
set(xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo)
102101
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
118102
target_link_options_shared_lib(xnnpack_backend)
119103
endif()
120104

@@ -130,19 +114,15 @@ if(TARGET qnn_executorch_backend)
130114
target_link_options_shared_lib(qnn_executorch_backend)
131115
endif()
132116

133-
# This one is needed for cpuinfo where it uses android specific log lib
117+
# This one is needed for cpuinfo where it uses android
118+
# specific log lib
134119
if(ANDROID)
135120
list(APPEND link_libraries log)
136121
endif()
137122

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})
123+
target_compile_options(llama_main PUBLIC ${_common_compile_options}
124+
-DET_USE_THREADPOOL)
125+
target_link_libraries(llama_main PUBLIC ${link_libraries})
146126

147127
if(APPLE)
148128
target_link_options_shared_lib(executorch)

examples/models/llama2/TARGETS

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ runtime.python_library(
1818
],
1919
deps = [
2020
"//caffe2:torch",
21-
"//executorch/examples/models/llama2/custom_ops:custom_ops_aot_py",
21+
"//executorch/examples/models/llama2/custom_ops:llama_custom_ops_aot_lib",
2222
],
2323
)
2424

@@ -52,7 +52,6 @@ runtime.python_binary(
5252
main_module = "executorch.examples.models.llama2.export_llama",
5353
# visibility = ["//executorch/examples/..."],
5454
preload_deps = [
55-
"//executorch/examples/models/llama2/custom_ops:custom_ops_aot_lib",
5655
"//executorch/kernels/quantized:aot_lib",
5756
],
5857
deps = [

0 commit comments

Comments
 (0)