From 4a569910ab2684b2a0c7896ffd430d638055d4bd Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 13 Feb 2025 10:43:54 -0800 Subject: [PATCH 1/3] Update [ghstack-poisoned] --- .ci/scripts/build_llama_android.sh | 3 - .ci/scripts/test_llama.sh | 1 - .ci/scripts/test_llava.sh | 7 +-- .ci/scripts/test_model.sh | 4 -- .ci/scripts/test_phi_3_mini.sh | 4 -- .ci/scripts/test_quantized_aot_lib.sh | 3 - .ci/scripts/utils.sh | 1 - .github/workflows/trunk.yml | 2 - CMakeLists.txt | 4 +- .../scripts/build_quantized_ops_aot_lib.sh | 4 -- build/Codegen.cmake | 9 +-- build/Utils.cmake | 60 +++++++++++++++---- build/build_android_llm_demo.sh | 3 - build/build_apple_frameworks.sh | 1 - .../executorch-arm-delegate-tutorial.md | 3 - examples/models/llava/CMakeLists.txt | 2 +- .../portable/custom_ops/test_custom_ops.sh | 3 - .../xnnpack/quantization/test_quantize.sh | 3 - extension/llm/custom_ops/CMakeLists.txt | 4 +- extension/llm/export/quantizer_lib.py | 4 +- setup.py | 4 +- test/run_oss_cpp_tests.sh | 2 - 22 files changed, 60 insertions(+), 71 deletions(-) diff --git a/.ci/scripts/build_llama_android.sh b/.ci/scripts/build_llama_android.sh index d37c65aa8ec..a100cc13144 100644 --- a/.ci/scripts/build_llama_android.sh +++ b/.ci/scripts/build_llama_android.sh @@ -14,7 +14,6 @@ if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then PYTHON_EXECUTABLE=python3 fi which "${PYTHON_EXECUTABLE}" -CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" install_executorch_and_backend_lib() { echo "Installing executorch and xnnpack backend" @@ -28,7 +27,6 @@ install_executorch_and_backend_lib() { -DANDROID_ABI="${ANDROID_ABI}" \ -DCMAKE_INSTALL_PREFIX=cmake-android-out \ -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \ @@ -54,7 +52,6 @@ build_llama_runner() { -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -Bcmake-android-out/examples/models/llama examples/models/llama cmake --build cmake-android-out/examples/models/llama -j4 --config Release diff --git a/.ci/scripts/test_llama.sh b/.ci/scripts/test_llama.sh index 9735e26798d..9bb881ce8eb 100644 --- a/.ci/scripts/test_llama.sh +++ b/.ci/scripts/test_llama.sh @@ -154,7 +154,6 @@ cmake_install_executorch_libraries() { rm -rf cmake-out retry cmake \ -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DCMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" \ -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ diff --git a/.ci/scripts/test_llava.sh b/.ci/scripts/test_llava.sh index c511942be91..15df725f9c1 100644 --- a/.ci/scripts/test_llava.sh +++ b/.ci/scripts/test_llava.sh @@ -31,7 +31,6 @@ NPROC=8 if hash nproc &> /dev/null; then NPROC=$(nproc); fi python_lib=$($PYTHON_EXECUTABLE -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())') -CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" EXECUTORCH_COMMON_CMAKE_ARGS=" \ -DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \ -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} \ @@ -48,7 +47,6 @@ EXECUTORCH_COMMON_CMAKE_ARGS=" \ cmake_install_executorch_libraries() { cmake \ ${EXECUTORCH_COMMON_CMAKE_ARGS} \ - "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \ -B${BUILD_DIR} . cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE} @@ -59,7 +57,6 @@ cmake_install_executorch_libraries_for_android() { -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ -DANDROID_ABI=arm64-v8a \ ${EXECUTORCH_COMMON_CMAKE_ARGS} \ - "-DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}" \ -B${BUILD_DIR} . cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${CMAKE_BUILD_TYPE} @@ -80,7 +77,7 @@ cmake_build_llava_runner() { cmake \ ${LLAVA_COMMON_CMAKE_ARGS} \ - -DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \ + -DCMAKE_PREFIX_PATH="$python_lib" \ -B${BUILD_DIR}/${dir} \ ${dir} @@ -96,7 +93,7 @@ cmake_build_llava_runner_for_android() { -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \ -DANDROID_ABI=arm64-v8a \ ${LLAVA_COMMON_CMAKE_ARGS} \ - -DCMAKE_PREFIX_PATH="$python_lib;${CMAKE_PREFIX_PATH}" \ + -DCMAKE_PREFIX_PATH="$python_lib" \ -DLLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE=ON \ -B${BUILD_DIR}/${dir} \ ${dir} diff --git a/.ci/scripts/test_model.sh b/.ci/scripts/test_model.sh index ef4859135c6..157449c0717 100755 --- a/.ci/scripts/test_model.sh +++ b/.ci/scripts/test_model.sh @@ -50,12 +50,10 @@ prepare_artifacts_upload() { build_cmake_executor_runner() { echo "Building executor_runner" - CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" rm -rf ${CMAKE_OUTPUT_DIR} cmake -DCMAKE_BUILD_TYPE=Debug \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -B${CMAKE_OUTPUT_DIR} . cmake --build ${CMAKE_OUTPUT_DIR} -j4 --config Debug @@ -100,14 +98,12 @@ test_model() { build_cmake_xnn_executor_runner() { echo "Building xnn_executor_runner" - CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" (rm -rf ${CMAKE_OUTPUT_DIR} \ && mkdir ${CMAKE_OUTPUT_DIR} \ && cd ${CMAKE_OUTPUT_DIR} \ && retry cmake -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..) cmake --build ${CMAKE_OUTPUT_DIR} -j4 diff --git a/.ci/scripts/test_phi_3_mini.sh b/.ci/scripts/test_phi_3_mini.sh index 64dd6b829d8..40767013e23 100644 --- a/.ci/scripts/test_phi_3_mini.sh +++ b/.ci/scripts/test_phi_3_mini.sh @@ -22,10 +22,8 @@ NPROC=8 if hash nproc &> /dev/null; then NPROC=$(nproc); fi cmake_install_executorch_libraries() { - CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" cmake -DPYTHON_EXECUTABLE=python \ -DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DEXECUTORCH_ENABLE_LOGGING=1 \ -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ @@ -41,10 +39,8 @@ cmake_install_executorch_libraries() { } cmake_build_phi_3_mini() { - CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" cmake -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \ -DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \ -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ diff --git a/.ci/scripts/test_quantized_aot_lib.sh b/.ci/scripts/test_quantized_aot_lib.sh index 3f8ea886f5c..b522eb7b418 100755 --- a/.ci/scripts/test_quantized_aot_lib.sh +++ b/.ci/scripts/test_quantized_aot_lib.sh @@ -16,13 +16,10 @@ CMAKE_OUTPUT_DIR=cmake-out build_cmake_quantized_aot_lib() { echo "Building quantized aot lib" - SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')" - CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch" (rm -rf ${CMAKE_OUTPUT_DIR} \ && mkdir ${CMAKE_OUTPUT_DIR} \ && cd ${CMAKE_OUTPUT_DIR} \ && retry cmake -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..) diff --git a/.ci/scripts/utils.sh b/.ci/scripts/utils.sh index c21d0bb604e..be684b7bfa2 100644 --- a/.ci/scripts/utils.sh +++ b/.ci/scripts/utils.sh @@ -136,7 +136,6 @@ cmake_install_executorch_lib() { clean_executorch_install_folders retry cmake -DBUCK2="$BUCK" \ -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DCMAKE_PREFIX_PATH="$($PYTHON_EXECUTABLE -c 'import torch as _; print(_.__path__[0])')" \ -DCMAKE_BUILD_TYPE=Release \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ -Bcmake-out . diff --git a/.github/workflows/trunk.yml b/.github/workflows/trunk.yml index 18e34bff72a..04a6c96f3ec 100644 --- a/.github/workflows/trunk.yml +++ b/.github/workflows/trunk.yml @@ -394,7 +394,6 @@ jobs: rm -rf cmake-out cmake \ -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \ -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ @@ -412,7 +411,6 @@ jobs: cmake \ -DCMAKE_INSTALL_PREFIX=cmake-out \ -DCMAKE_BUILD_TYPE=Release \ - -DCMAKE_PREFIX_PATH="$(python -c 'import torch as _; print(_.__path__[0])')" \ -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ diff --git a/CMakeLists.txt b/CMakeLists.txt index ad1f5d2019a..792a4f0f267 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -752,9 +752,7 @@ if(EXECUTORCH_BUILD_PYBIND) endif() # find pytorch lib, to allow pybind to take at::Tensor as input/output - if(NOT TARGET torch) - find_package(Torch CONFIG REQUIRED) - endif() + find_package_torch() find_library( TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib" ) diff --git a/backends/arm/scripts/build_quantized_ops_aot_lib.sh b/backends/arm/scripts/build_quantized_ops_aot_lib.sh index d3d4b669f3b..3c70b48a5dc 100755 --- a/backends/arm/scripts/build_quantized_ops_aot_lib.sh +++ b/backends/arm/scripts/build_quantized_ops_aot_lib.sh @@ -11,9 +11,6 @@ build_type="Release" build_type=${1:-$build_type} -SITE_PACKAGES="$(python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')" -CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch" - echo "--------------------------------------------------------------------------------" echo "Build .so library to register quant ops with AoT flow ${build_type} into '$(echo $(pwd))/cmake-out-aot-lib'" echo "--------------------------------------------------------------------------------" @@ -23,7 +20,6 @@ echo "-------------------------------------------------------------------------- rm -f cmake-out-aot-lib/CMakeCache.txt CXXFLAGS="-fno-exceptions -fno-rtti" cmake \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DCMAKE_BUILD_TYPE=${build_type} \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \ diff --git a/build/Codegen.cmake b/build/Codegen.cmake index f2da23baeaa..9ccb7ec442b 100644 --- a/build/Codegen.cmake +++ b/build/Codegen.cmake @@ -9,6 +9,8 @@ # Selective build. See codegen/tools/gen_oplist.py for how to use these # arguments. +include(${EXECUTORCH_ROOT}/build/Utils.cmake) + function(gen_selected_ops) set(arg_names LIB_NAME OPS_SCHEMA_YAML ROOT_OPS INCLUDE_ALL_OPS) cmake_parse_arguments(GEN "" "" "${arg_names}" ${ARGN}) @@ -145,18 +147,13 @@ function(gen_custom_ops_aot_lib) ${_out_dir}/RegisterCPUCustomOps.cpp ${_out_dir}/RegisterSchema.cpp ${_out_dir}/CustomOpsNativeFunctions.h "${GEN_KERNEL_SOURCES}" ) - # Find `Torch`. - if(NOT TARGET torch) - find_package(Torch REQUIRED) - endif() + find_package_torch() # This lib uses ATen lib, so we explicitly enable rtti and exceptions. target_compile_options(${GEN_LIB_NAME} PRIVATE -frtti -fexceptions) target_compile_definitions(${GEN_LIB_NAME} PRIVATE USE_ATEN_LIB=1) include_directories(${TORCH_INCLUDE_DIRS}) target_link_libraries(${GEN_LIB_NAME} PRIVATE torch) - include(${EXECUTORCH_ROOT}/build/Utils.cmake) - target_link_options_shared_lib(${GEN_LIB_NAME}) if(TARGET portable_lib) target_link_libraries(${GEN_LIB_NAME} PRIVATE portable_lib) diff --git a/build/Utils.cmake b/build/Utils.cmake index dca3f189ec4..b4908b522cd 100644 --- a/build/Utils.cmake +++ b/build/Utils.cmake @@ -325,21 +325,59 @@ function(resolve_python_executable) endif() endfunction() -# find_package(Torch CONFIG REQUIRED) replacement for targets that -# have a header-only Torch dependency. Because find_package sets -# variables in the parent scope, we use a macro to preserve this -# rather than maintaining our own list of those variables. +# find_package(Torch CONFIG REQUIRED) replacement for targets that have a +# header-only Torch dependency. Because find_package sets variables in the +# parent scope, we use a macro to preserve this rather than maintaining our own +# list of those variables. macro(find_package_torch_headers) - # We cannot simply use CMAKE_FIND_ROOT_PATH_BOTH, because that does - # not propagate into TorchConfig.cmake. + # We cannot simply use CMAKE_FIND_ROOT_PATH_BOTH, because that does not + # propagate into TorchConfig.cmake. foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE) - set(OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} ${CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}) + set(OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} + ${CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}} + ) set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} BOTH) endforeach() - if(NOT TARGET torch) - find_package(Torch CONFIG REQUIRED) - endif() + find_package_torch() foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE) - set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} ${OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}) + set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} + ${OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}} + ) endforeach() endmacro() + +# Add the Torch CMake configuration to CMAKE_PREFIX_PATH so that find_package +# can find Torch. +function(add_torch_to_cmake_prefix_path) + if(NOT PYTHON_EXECUTABLE) + resolve_python_executable() + endif() + execute_process( + COMMAND "${PYTHON_EXECUTABLE}" -c "import torch as _; print(_.__path__[0], end='')" + OUTPUT_VARIABLE _tmp_torch_path + ERROR_VARIABLE _tmp_torch_path_error + RESULT_VARIABLE _tmp_torch_path_result + COMMAND_ECHO STDERR + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NOT _tmp_torch_path_result EQUAL 0) + message("Error while adding torch to CMAKE_PREFIX_PATH. " + "Exit code: ${_tmp_torch_path_result}" + ) + message("Output:\n${_tmp_torch_path}") + message(FATAL_ERROR "Error:\n${_tmp_torch_path_error}") + endif() + list(APPEND CMAKE_PREFIX_PATH "${_tmp_torch_path}") + set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE) +endfunction() + +# Replacement for find_package(Torch CONFIG REQUIRED); sets up +# CMAKE_PREFIX_PATH first and only does the find once. If you have a +# header-only Torch dependency, use find_package_torch_headers +# instead! +function(find_package_torch) + if(NOT TARGET torch) + add_torch_to_cmake_prefix_path() + find_package(Torch CONFIG REQUIRED) + endif() +endfunction() diff --git a/build/build_android_llm_demo.sh b/build/build_android_llm_demo.sh index b72968037c1..4119bde4c7e 100644 --- a/build/build_android_llm_demo.sh +++ b/build/build_android_llm_demo.sh @@ -11,7 +11,6 @@ if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then PYTHON_EXECUTABLE=python3 fi which "${PYTHON_EXECUTABLE}" -CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" build_jar() { pushd extension/android @@ -42,7 +41,6 @@ build_android_native_library() { fi cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \ -DANDROID_ABI="${ANDROID_ABI}" \ -DANDROID_PLATFORM=android-26 \ @@ -76,7 +74,6 @@ build_android_native_library() { -DANDROID_ABI="${ANDROID_ABI}" \ -DANDROID_PLATFORM=android-26 \ -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DEXECUTORCH_ENABLE_LOGGING=ON \ -DEXECUTORCH_LOG_LEVEL=Info \ -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \ diff --git a/build/build_apple_frameworks.sh b/build/build_apple_frameworks.sh index 7ebfe2d9492..e077091845d 100755 --- a/build/build_apple_frameworks.sh +++ b/build/build_apple_frameworks.sh @@ -150,7 +150,6 @@ cmake_build() { mkdir "$platform" && cd "$platform" || exit 1 cmake "$SOURCE_ROOT_DIR" -G Xcode \ -DCMAKE_BUILD_TYPE="$MODE" \ - -DCMAKE_PREFIX_PATH="$($PYTHON -c 'import torch as _; print(_.__path__[0])')" \ -DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN" \ -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD="c++17" \ -DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY="libc++" \ diff --git a/docs/source/executorch-arm-delegate-tutorial.md b/docs/source/executorch-arm-delegate-tutorial.md index 855a828c23a..4d9bd7e0577 100644 --- a/docs/source/executorch-arm-delegate-tutorial.md +++ b/docs/source/executorch-arm-delegate-tutorial.md @@ -229,8 +229,6 @@ python3 -m examples.arm.aot_arm_compiler --model_name="add" --delegate Before generating the `.pte` file for delegated quantized networks like MobileNetV2, we need to build the `quantized_ops_aot_lib` ```bash -SITE_PACKAGES="$(python3 -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')" -CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch" cd mkdir -p cmake-out-aot-lib @@ -238,7 +236,6 @@ cmake -DCMAKE_BUILD_TYPE=Release \ -DEXECUTORCH_BUILD_XNNPACK=OFF \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DPYTHON_EXECUTABLE=python3 \ -Bcmake-out-aot-lib \ "${et_root_dir}" diff --git a/examples/models/llava/CMakeLists.txt b/examples/models/llava/CMakeLists.txt index f22b4471538..ecd00809fdb 100644 --- a/examples/models/llava/CMakeLists.txt +++ b/examples/models/llava/CMakeLists.txt @@ -79,7 +79,7 @@ if(LLAVA_RUNNER_NO_TORCH_DUMMY_IMAGE) add_definitions(-DLLAVA_NO_TORCH_DUMMY_IMAGE=1) message("Buidling the runner without Torch, feeding a dummy image!") else() - find_package(Torch CONFIG REQUIRED) + find_package_torch() endif() # diff --git a/examples/portable/custom_ops/test_custom_ops.sh b/examples/portable/custom_ops/test_custom_ops.sh index 6d83de07d3a..747c0f20da5 100644 --- a/examples/portable/custom_ops/test_custom_ops.sh +++ b/examples/portable/custom_ops/test_custom_ops.sh @@ -53,15 +53,12 @@ get_shared_lib_ext() { test_cmake_custom_op_2() { local model_name='custom_ops_2' - SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')" - CMAKE_PREFIX_PATH="$PWD/cmake-out/lib/cmake/ExecuTorch;${SITE_PACKAGES}/torch" local example_dir=examples/portable/custom_ops local build_dir=cmake-out/${example_dir} rm -rf ${build_dir} retry cmake \ -DREGISTER_EXAMPLE_CUSTOM_OP=2 \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ -B${build_dir} \ ${example_dir} diff --git a/examples/xnnpack/quantization/test_quantize.sh b/examples/xnnpack/quantization/test_quantize.sh index d439fde6cbc..1f50667c788 100644 --- a/examples/xnnpack/quantization/test_quantize.sh +++ b/examples/xnnpack/quantization/test_quantize.sh @@ -44,8 +44,6 @@ test_buck2_quantization() { test_cmake_quantization() { echo "Building quantized ops shared library" - SITE_PACKAGES="$(${PYTHON_EXECUTABLE} -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')" - CMAKE_PREFIX_PATH="${SITE_PACKAGES}/torch" clean_executorch_install_folders @@ -56,7 +54,6 @@ test_cmake_quantization() { -DEXECUTORCH_BUILD_XNNPACK="$EXECUTORCH_BUILD_XNNPACK" \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON \ - -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" ..) cmake --build cmake-out -j4 diff --git a/extension/llm/custom_ops/CMakeLists.txt b/extension/llm/custom_ops/CMakeLists.txt index 4b793905339..95f4bc559fa 100644 --- a/extension/llm/custom_ops/CMakeLists.txt +++ b/extension/llm/custom_ops/CMakeLists.txt @@ -69,9 +69,7 @@ install(TARGETS custom_ops DESTINATION lib) if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT) # Add a AOT library - if(NOT TARGET torch) - find_package(Torch CONFIG REQUIRED) - endif() + find_package_torch() add_library( custom_ops_aot_lib SHARED ${_custom_ops__srcs} diff --git a/extension/llm/export/quantizer_lib.py b/extension/llm/export/quantizer_lib.py index 55e530553f0..40d81075d9f 100644 --- a/extension/llm/export/quantizer_lib.py +++ b/extension/llm/export/quantizer_lib.py @@ -107,10 +107,8 @@ def check_embedding_byte_registered(): raise RuntimeError( "Need to specify shared library path to register quantized ops (and their out variants) into EXIR.\n" "Follow the following steps to build the needed lib via cmake.\n" - 'Use `python -c "import torch as _; print(_.__path__)"` to find where torch package is installed.\n' - "Set that as TORCH_PACKAGE_DIR.\n" "Then from root executorch dir do the following:\n" - "rm -rf cmake-out && mkdir cmake-out && (cd cmake-out && cmake -DBUCK2= -DCMAKE_PREFIX_PATH=$TORCH_PACKAGE_DIR -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON ..) && cmake --build . -j16\n" + "rm -rf cmake-out && mkdir cmake-out && (cd cmake-out && cmake -DBUCK2= -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON ..) && cmake --build . -j16\n" 'To find the location of the lib: find cmake-out -name "libquantized_ops_aot_lib*"\n' "Then specify the said library via -s Path: srcs = tuple(cmake_cache_dir.glob(self.src)) if len(srcs) != 1: raise ValueError( - f"""Expected exactly one file matching '{self.src}'; found {repr(srcs)}. + f"""Expected exactly one file matching '{self.src}'; found {repr(srcs)}. If that file is a CMake-built extension module file, and we are installing in editable mode, please disable the corresponding build option since it's not supported yet. -Try: +Try: EXECUTORCH_BUILD_FLATC=OFF EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT=OFF pip install -e . """ diff --git a/test/run_oss_cpp_tests.sh b/test/run_oss_cpp_tests.sh index 012fded59ca..2e30589731a 100755 --- a/test/run_oss_cpp_tests.sh +++ b/test/run_oss_cpp_tests.sh @@ -32,10 +32,8 @@ build_executorch() { if [ -x "$(command -v glslc)" ]; then BUILD_VULKAN="ON" fi - CMAKE_PREFIX_PATH="$(python3 -c 'import torch as _; print(_.__path__[0])')" cmake . \ -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DCMAKE_PREFIX_PATH="${CMAKE_PREFIX_PATH}" \ -DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \ -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ From 778fb1b8317f8a1eab0de12793da190fbd0721a5 Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 13 Feb 2025 15:02:50 -0800 Subject: [PATCH 2/3] Update [ghstack-poisoned] --- build/Utils.cmake | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/build/Utils.cmake b/build/Utils.cmake index b4908b522cd..0684a0fec93 100644 --- a/build/Utils.cmake +++ b/build/Utils.cmake @@ -353,11 +353,11 @@ function(add_torch_to_cmake_prefix_path) resolve_python_executable() endif() execute_process( - COMMAND "${PYTHON_EXECUTABLE}" -c "import torch as _; print(_.__path__[0], end='')" + COMMAND "${PYTHON_EXECUTABLE}" -c + "import torch as _; print(_.__path__[0], end='')" OUTPUT_VARIABLE _tmp_torch_path ERROR_VARIABLE _tmp_torch_path_error - RESULT_VARIABLE _tmp_torch_path_result - COMMAND_ECHO STDERR + RESULT_VARIABLE _tmp_torch_path_result COMMAND_ECHO STDERR OUTPUT_STRIP_TRAILING_WHITESPACE ) if(NOT _tmp_torch_path_result EQUAL 0) @@ -368,16 +368,18 @@ function(add_torch_to_cmake_prefix_path) message(FATAL_ERROR "Error:\n${_tmp_torch_path_error}") endif() list(APPEND CMAKE_PREFIX_PATH "${_tmp_torch_path}") - set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH}" PARENT_SCOPE) + set(CMAKE_PREFIX_PATH + "${CMAKE_PREFIX_PATH}" + PARENT_SCOPE + ) endfunction() -# Replacement for find_package(Torch CONFIG REQUIRED); sets up -# CMAKE_PREFIX_PATH first and only does the find once. If you have a -# header-only Torch dependency, use find_package_torch_headers -# instead! -function(find_package_torch) +# Replacement for find_package(Torch CONFIG REQUIRED); sets up CMAKE_PREFIX_PATH +# first and only does the find once. If you have a header-only Torch dependency, +# use find_package_torch_headers instead! +macro(find_package_torch) if(NOT TARGET torch) add_torch_to_cmake_prefix_path() find_package(Torch CONFIG REQUIRED) endif() -endfunction() +endmacro() From beecf2a7eeb8a8a401f826952a3d486772bc49dd Mon Sep 17 00:00:00 2001 From: Scott Wolchok Date: Thu, 13 Feb 2025 16:15:23 -0800 Subject: [PATCH 3/3] Update [ghstack-poisoned] --- examples/portable/custom_ops/test_custom_ops.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/portable/custom_ops/test_custom_ops.sh b/examples/portable/custom_ops/test_custom_ops.sh index 747c0f20da5..5d21d393686 100644 --- a/examples/portable/custom_ops/test_custom_ops.sh +++ b/examples/portable/custom_ops/test_custom_ops.sh @@ -53,12 +53,14 @@ get_shared_lib_ext() { test_cmake_custom_op_2() { local model_name='custom_ops_2' + CMAKE_PREFIX_PATH="$PWD/cmake-out/lib/cmake/ExecuTorch" local example_dir=examples/portable/custom_ops local build_dir=cmake-out/${example_dir} rm -rf ${build_dir} retry cmake \ -DREGISTER_EXAMPLE_CUSTOM_OP=2 \ + -DCMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH" \ -DPYTHON_EXECUTABLE="$PYTHON_EXECUTABLE" \ -B${build_dir} \ ${example_dir}