Skip to content

Commit 66c5fc8

Browse files
lucylqfacebook-github-bot
authored andcommitted
Add xnnpack to llama runner mac & linux CI job (#2677)
Summary: Pull Request resolved: #2677 Remake of D55290786, which is ghstack poisoned Reviewed By: kimishpatel, digantdesai Differential Revision: D55349949 fbshipit-source-id: b840dfee42b7d1ad349d766e3956c1ab8687034d
1 parent d64fc74 commit 66c5fc8

File tree

8 files changed

+103
-25
lines changed

8 files changed

+103
-25
lines changed

.ci/scripts/setup-macos.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ print_cmake_info() {
104104
codesign -f -s - "${CMAKE_EXEC}" || true
105105
}
106106

107+
setup_macos_env_variables() {
108+
CMAKE_PREFIX_PATH=$(python -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
109+
export CMAKE_PREFIX_PATH
110+
}
111+
112+
setup_macos_env_variables
107113
# NB: we need buck2 in all cases because cmake build also depends on calling
108114
# buck2 atm
109115
install_buck

.ci/scripts/test_llama.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
1212
MODEL_NAME=$1 # stories110M.pt
1313
BUILD_TOOL=$2 # buck2 or cmake
1414
DTYPE=$3 # fp16 or fp32
15-
15+
MODE=${4:-"xnnpack"} # portable or xnnpack
16+
if [[ $# -lt 4 ]]; then # Assuming 4 mandatory args
17+
echo "Expecting atleast 4 positional arguments"
18+
echo "Usage: [...]"
19+
fi
1620
if [[ -z "${MODEL_NAME:-}" ]]; then
1721
echo "Missing model name, exiting..."
1822
exit 1
@@ -28,6 +32,11 @@ if [[ -z "${DTYPE:-}" ]]; then
2832
exit 1
2933
fi
3034

35+
if [[ -z "${MODE:-}" ]]; then
36+
echo "Missing mode, choose portable or xnnpack, exiting..."
37+
exit 1
38+
fi
39+
3140
if [[ -z "${BUCK:-}" ]]; then
3241
BUCK=buck2
3342
fi
@@ -42,12 +51,18 @@ which "${PYTHON_EXECUTABLE}"
4251
cmake_install_executorch_libraries() {
4352
echo "Installing libexecutorch.a, libextension_module.so, libportable_ops_lib.a"
4453
rm -rf cmake-out
54+
if [[ "${MODE}" == "xnnpack" ]]; then
55+
XNNPACK=ON
56+
else
57+
XNNPACK=OFF
58+
fi
4559
retry cmake -DBUCK2="$BUCK" \
4660
-DCMAKE_INSTALL_PREFIX=cmake-out \
4761
-DCMAKE_BUILD_TYPE=Release \
4862
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
4963
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
5064
-DEXECUTORCH_BUILD_OPTIMIZED=ON \
65+
-DEXECUTORCH_BUILD_XNNPACK="$XNNPACK" \
5166
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
5267
-Bcmake-out .
5368
cmake --build cmake-out -j9 --target install --config Release
@@ -101,7 +116,11 @@ fi
101116
# Export model.
102117
EXPORTED_MODEL_NAME="${EXPORTED_MODEL_NAME}.pte"
103118
echo "Exporting ${EXPORTED_MODEL_NAME}"
104-
$PYTHON_EXECUTABLE -m examples.models.llama2.export_llama -c stories110M.pt -p "${PARAMS}" -d "${DTYPE}"
119+
EXPORT_ARGS="-c stories110M.pt -p ${PARAMS} -d ${DTYPE} -n ${EXPORTED_MODEL_NAME}"
120+
if [[ "${MODE}" == "xnnpack" ]]; then
121+
EXPORT_ARGS="${EXPORT_ARGS} --pt2e_quantize xnnpack_dynamic"
122+
fi
123+
$PYTHON_EXECUTABLE -m examples.models.llama2.export_llama ${EXPORT_ARGS}
105124

106125
# Create tokenizer.bin.
107126
echo "Creating tokenizer.bin"

.github/workflows/pull.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ jobs:
9090
matrix:
9191
dtype: [fp32]
9292
build-tool: [buck2, cmake]
93+
mode: [portable, xnnpack]
9394
fail-fast: false
9495
with:
9596
runner: linux.2xlarge
@@ -104,13 +105,14 @@ jobs:
104105
105106
DTYPE=${{ matrix.dtype }}
106107
BUILD_TOOL=${{ matrix.build-tool }}
108+
MODE=${{ matrix.mode }}
107109
108110
# Setup executorch
109111
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh buck2
110112
# Install requirements for export_llama
111113
PYTHON_EXECUTABLE=python bash examples/models/llama2/install_requirements.sh
112114
# Test llama2
113-
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh stories110M.pt "${BUILD_TOOL}" "${DTYPE}"
115+
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh stories110M.pt "${BUILD_TOOL}" "${DTYPE}" "${MODE}"
114116
115117
test-custom-ops-linux:
116118
name: test-custom-ops-linux

.github/workflows/trunk.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,63 @@ jobs:
215215
# Build and test coreml delegate
216216
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash backends/apple/coreml/scripts/build_all.sh
217217
popd
218+
219+
test-pybind-build-macos:
220+
name: test-pybind-build-macos
221+
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main
222+
strategy:
223+
matrix:
224+
include:
225+
- build-tool: cmake
226+
fail-fast: false
227+
with:
228+
runner: macos-m1-stable
229+
python-version: '3.11'
230+
submodules: 'true'
231+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
232+
timeout: 180
233+
script: |
234+
WORKSPACE=$(pwd)
235+
pushd "${WORKSPACE}/pytorch/executorch"
236+
bash .ci/scripts/setup-conda.sh
237+
238+
# build module for executorch.extension.pybindings.portable_lib
239+
BUILD_TOOL=${{ matrix.build-tool }}
240+
EXECUTORCH_BUILD_PYBIND=ON PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/setup-macos.sh "${BUILD_TOOL}"
241+
242+
# see if we can import the module successfully
243+
${CONDA_RUN} python -c "from executorch.extension.pybindings import portable_lib; print('success!')"
244+
popd
245+
246+
test-llama-runner-macos:
247+
name: test-llama-runner-mac
248+
uses: pytorch/test-infra/.github/workflows/macos_job.yml@main
249+
strategy:
250+
matrix:
251+
dtype: [fp32]
252+
build-tool: [buck2, cmake]
253+
mode: [portable, xnnpack]
254+
fail-fast: false
255+
with:
256+
runner: macos-m1-stable
257+
python-version: '3.11'
258+
submodules: 'true'
259+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
260+
timeout: 900
261+
script: |
262+
WORKSPACE=$(pwd)
263+
pushd "${WORKSPACE}/pytorch/executorch"
264+
bash .ci/scripts/setup-conda.sh
265+
266+
DTYPE=${{ matrix.dtype }}
267+
BUILD_TOOL=${{ matrix.build-tool }}
268+
MODE=${{ matrix.mode }}
269+
270+
# Setup executorch
271+
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/setup-macos.sh "${BUILD_TOOL}"
272+
273+
# Install requirements for export_llama
274+
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash examples/models/llama2/install_requirements.sh
275+
# Test llama2
276+
PYTHON_EXECUTABLE=python ${CONDA_RUN} bash .ci/scripts/test_llama.sh stories110M.pt "${BUILD_TOOL}" "${DTYPE}" "${MODE}"
277+
popd

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ if(EXECUTORCH_BUILD_PYBIND)
445445
# find pytorch lib, to allow pybind to take at::Tensor as input/output
446446
find_package(Torch CONFIG REQUIRED)
447447
find_library(TORCH_PYTHON_LIBRARY torch_python
448-
PATHS "${TORCH_INSTALL_PREFIX}/lib")
448+
PATHS "${TORCH_INSTALL_PREFIX}/lib")
449449

450450
# compile options for pybind
451451

examples/models/llama2/CMakeLists.txt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,15 @@ find_package(executorch CONFIG REQUIRED)
5656
# llama_runner library
5757
add_subdirectory(runner)
5858

59-
set(link_options)
6059
set(link_libraries)
6160

6261
if(EXECUTORCH_BUILD_OPTIMIZED)
63-
list(APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels portable_kernels)
64-
list(APPEND link_options
65-
"SHELL:LINKER:--whole-archive \
66-
$<TARGET_FILE:optimized_native_cpu_ops_lib> \
67-
LINKER:--no-whole-archive")
62+
list(APPEND link_libraries optimized_native_cpu_ops_lib optimized_kernels
63+
portable_kernels)
64+
target_link_options_shared_lib(optimized_native_cpu_ops_lib)
6865
else()
6966
list(APPEND link_libraries portable_ops_lib portable_kernels)
70-
list(APPEND link_options
71-
"SHELL:LINKER:--whole-archive \
72-
$<TARGET_FILE:portable_ops_lib> \
73-
LINKER:--no-whole-archive")
67+
target_link_options_shared_lib(portable_ops_lib)
7468
endif()
7569

7670
target_link_libraries(llama_main PUBLIC gflags llama_runner)
@@ -79,24 +73,21 @@ target_link_libraries(llama_main PUBLIC gflags llama_runner)
7973
if(TARGET xnnpack_backend)
8074
set(xnnpack_backend_libs xnnpack_backend XNNPACK pthreadpool cpuinfo)
8175
list(APPEND link_libraries ${xnnpack_backend_libs})
82-
list(APPEND link_options
83-
"SHELL:LINKER:--whole-archive \
84-
$<TARGET_FILE:xnnpack_backend> \
85-
LINKER:--no-whole-archive")
76+
target_link_options_shared_lib(xnnpack_backend)
8677
endif()
8778

8879
# Vulkan backend
8980
if(TARGET vulkan_backend)
9081
list(APPEND link_libraries vulkan_backend)
91-
list(APPEND link_options
92-
"SHELL:LINKER:--whole-archive \
93-
$<TARGET_FILE:vulkan_backend> \
94-
LINKER:--no-whole-archive")
82+
target_link_options_shared_lib(vulkan_backend)
9583
endif()
9684

9785
target_compile_options(llama_main PUBLIC ${_common_compile_options})
9886
target_link_libraries(llama_main PUBLIC ${link_libraries})
99-
target_link_options(llama_main PUBLIC ${link_options})
87+
88+
if(APPLE)
89+
target_link_options_shared_lib(executorch)
90+
endif()
10091

10192
# Print all summary
10293
executorch_print_configuration_summary()

examples/models/llama2/runner/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ list(TRANSFORM _llama_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
3939
target_include_directories(extension_module
4040
INTERFACE ${_common_include_directories})
4141

42-
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID)
42+
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID OR APPLE)
4343
# Building a share library on iOS requires code signing
4444
# On Android we see duplicated registration when using shared lib
4545
add_library(llama_runner STATIC ${_llama_runner__srcs})

extension/module/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if(NOT EXECUTORCH_ROOT)
1717
endif()
1818

1919
list(TRANSFORM _extension_module__srcs PREPEND "${EXECUTORCH_ROOT}/")
20-
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID)
20+
if(CMAKE_TOOLCHAIN_IOS OR CMAKE_TOOLCHAIN_ANDROID OR APPLE)
2121
# Building a share library on iOS requires code signing
2222
# On Android we see duplicated registration when using shared lib
2323
add_library(extension_module STATIC ${_extension_module__srcs})

0 commit comments

Comments
 (0)