Skip to content

Commit 5d568e8

Browse files
committed
Automatically put torch in CMAKE_PREFIX_PATH so users/scripts don't have to
Removed all explicit setting of CMAKE_PREFIX_PATH to the torch directory in favor of automatically doing it when we find_package(Torch). Hat tip to huydhn for showing the way to only find torch once. ghstack-source-id: f66416e ghstack-comment-id: 2657445695 Pull Request resolved: #8474
1 parent 556a1a2 commit 5d568e8

22 files changed

+61
-70
lines changed

.ci/scripts/build_llama_android.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
1414
PYTHON_EXECUTABLE=python3
1515
fi
1616
which "${PYTHON_EXECUTABLE}"
17-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
1817

1918
install_executorch_and_backend_lib() {
2019
echo "Installing executorch and xnnpack backend"
@@ -28,7 +27,6 @@ install_executorch_and_backend_lib() {
2827
-DANDROID_ABI="${ANDROID_ABI}" \
2928
-DCMAKE_INSTALL_PREFIX=cmake-android-out \
3029
-DCMAKE_BUILD_TYPE=Release \
31-
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
3230
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
3331
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
3432
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
@@ -54,7 +52,6 @@ build_llama_runner() {
5452
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
5553
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
5654
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
57-
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
5855
-Bcmake-android-out/examples/models/llama examples/models/llama
5956

6057
cmake --build cmake-android-out/examples/models/llama -j4 --config Release

.ci/scripts/test_llama.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ cmake_install_executorch_libraries() {
154154
rm -rf cmake-out
155155
retry cmake \
156156
-DCMAKE_INSTALL_PREFIX=cmake-out \
157-
-DCMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" \
158157
-DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \
159158
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
160159
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \

.ci/scripts/test_llava.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ NPROC=8
3131
if hash nproc &> /dev/null; then NPROC=$(nproc); fi
3232

3333
python_lib=$($PYTHON_EXECUTABLE -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
34-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
3534
EXECUTORCH_COMMON_CMAKE_ARGS=" \
3635
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
3736
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \
@@ -48,7 +47,6 @@ EXECUTORCH_COMMON_CMAKE_ARGS=" \
4847
cmake_install_executorch_libraries() {
4948
cmake \
5049
${EXECUTORCH_COMMON_CMAKE_ARGS} \
51-
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \
5250
-B${BUILD_DIR} .
5351

5452
cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE}
@@ -59,7 +57,6 @@ cmake_install_executorch_libraries_for_android() {
5957
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
6058
-DANDROID_ABI=arm64-v8a \
6159
${EXECUTORCH_COMMON_CMAKE_ARGS} \
62-
"-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \
6360
-B${BUILD_DIR} .
6461

6562
cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE}
@@ -80,7 +77,7 @@ cmake_build_llava_runner() {
8077

8178
cmake \
8279
${LLAVA_COMMON_CMAKE_ARGS} \
83-
-DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \
80+
-DCMAKE_PREFIX_PATH="$python_lib" \
8481
-B${BUILD_DIR}/${dir} \
8582
${dir}
8683

@@ -96,7 +93,7 @@ cmake_build_llava_runner_for_android() {
9693
-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
9794
-DANDROID_ABI=arm64-v8a \
9895
${LLAVA_COMMON_CMAKE_ARGS} \
99-
-DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \
96+
-DCMAKE_PREFIX_PATH="$python_lib" \
10097
-DLLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE=ON \
10198
-B${BUILD_DIR}/${dir} \
10299
${dir}

.ci/scripts/test_model.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,10 @@ prepare_artifacts_upload() {
5050

5151
build_cmake_executor_runner() {
5252
echo "Building executor_runner"
53-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
5453
rm -rf ${CMAKE_OUTPUT_DIR}
5554
cmake -DCMAKE_BUILD_TYPE=Debug \
5655
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
5756
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
58-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
5957
-B${CMAKE_OUTPUT_DIR} .
6058

6159
cmake --build ${CMAKE_OUTPUT_DIR} -j4 --config Debug
@@ -100,14 +98,12 @@ test_model() {
10098

10199
build_cmake_xnn_executor_runner() {
102100
echo "Building xnn_executor_runner"
103-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
104101

105102
(rm -rf ${CMAKE_OUTPUT_DIR} \
106103
&& mkdir ${CMAKE_OUTPUT_DIR} \
107104
&& cd ${CMAKE_OUTPUT_DIR} \
108105
&& retry cmake -DCMAKE_BUILD_TYPE=Release \
109106
-DEXECUTORCH_BUILD_XNNPACK=ON \
110-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
111107
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
112108

113109
cmake --build ${CMAKE_OUTPUT_DIR} -j4

.ci/scripts/test_phi_3_mini.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ NPROC=8
2222
if hash nproc &> /dev/null; then NPROC=$(nproc); fi
2323

2424
cmake_install_executorch_libraries() {
25-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
2625
cmake -DPYTHON_EXECUTABLE=python \
2726
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
28-
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
2927
-DEXECUTORCH_ENABLE_LOGGING=1 \
3028
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
3129
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
@@ -41,10 +39,8 @@ cmake_install_executorch_libraries() {
4139
}
4240

4341
cmake_build_phi_3_mini() {
44-
CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')"
4542
cmake -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
4643
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
47-
-DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \
4844
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
4945
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
5046
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \

.ci/scripts/test_quantized_aot_lib.sh

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ CMAKE_OUTPUT_DIR=cmake-out
1616

1717
build_cmake_quantized_aot_lib() {
1818
echo "Building quantized aot lib"
19-
SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
20-
CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch"
2119
(rm -rf ${CMAKE_OUTPUT_DIR} \
2220
&& mkdir ${CMAKE_OUTPUT_DIR} \
2321
&& cd ${CMAKE_OUTPUT_DIR} \
2422
&& retry cmake -DCMAKE_BUILD_TYPE=Release \
25-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
2623
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \
2724
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..)
2825

.ci/scripts/utils.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ cmake_install_executorch_lib() {
136136
clean_executorch_install_folders
137137
retry cmake -DBUCK2="$BUCK" \
138138
-DCMAKE_INSTALL_PREFIX=cmake-out \
139-
-DCMAKE_PREFIX_PATH="$($PYTHON_EXECUTABLE -c 'import torch as _; print(_.__path__[0])')" \
140139
-DCMAKE_BUILD_TYPE=Release \
141140
-DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \
142141
-Bcmake-out .

.github/workflows/trunk.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ jobs:
394394
rm -rf cmake-out
395395
cmake \
396396
-DCMAKE_INSTALL_PREFIX=cmake-out \
397-
-DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \
398397
-DCMAKE_BUILD_TYPE=Release \
399398
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
400399
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
@@ -412,7 +411,6 @@ jobs:
412411
cmake \
413412
-DCMAKE_INSTALL_PREFIX=cmake-out \
414413
-DCMAKE_BUILD_TYPE=Release \
415-
-DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \
416414
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
417415
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
418416
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,7 @@ if(EXECUTORCH_BUILD_PYBIND)
752752
endif()
753753

754754
# find pytorch lib, to allow pybind to take at::Tensor as input/output
755-
if(NOT TARGET torch)
756-
find_package(Torch CONFIG REQUIRED)
757-
endif()
755+
find_package_torch()
758756
find_library(
759757
TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib"
760758
)

backends/arm/scripts/build_quantized_ops_aot_lib.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ build_type="Release"
1111

1212
build_type=${1:-$build_type}
1313

14-
SITE_PACKAGES="$(python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')"
15-
CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch"
16-
1714
echo "--------------------------------------------------------------------------------"
1815
echo "Build .so library to register quant ops with AoT flow ${build_type} into '$(echo $(pwd))/cmake-out-aot-lib'"
1916
echo "--------------------------------------------------------------------------------"
@@ -23,7 +20,6 @@ echo "--------------------------------------------------------------------------
2320
rm -f cmake-out-aot-lib/CMakeCache.txt
2421

2522
CXXFLAGS="-fno-exceptions -fno-rtti" cmake \
26-
-DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \
2723
-DCMAKE_BUILD_TYPE=${build_type} \
2824
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
2925
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \

0 commit comments

Comments
 (0)