diff --git a/CMakeLists.txt b/CMakeLists.txt index 30f1f5c42cf..5f4e272336e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -750,7 +750,9 @@ if(EXECUTORCH_BUILD_PYBIND) endif() # find pytorch lib, to allow pybind to take at::Tensor as input/output - find_package(Torch CONFIG REQUIRED) + if(NOT TARGET torch) + find_package(Torch CONFIG REQUIRED) + endif() find_library( TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib" ) diff --git a/build/Codegen.cmake b/build/Codegen.cmake index c36b1a26b40..f2da23baeaa 100644 --- a/build/Codegen.cmake +++ b/build/Codegen.cmake @@ -146,7 +146,9 @@ function(gen_custom_ops_aot_lib) ${_out_dir}/CustomOpsNativeFunctions.h "${GEN_KERNEL_SOURCES}" ) # Find `Torch`. - find_package(Torch REQUIRED) + if(NOT TARGET torch) + find_package(Torch REQUIRED) + endif() # 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) diff --git a/extension/llm/custom_ops/CMakeLists.txt b/extension/llm/custom_ops/CMakeLists.txt index 16ca4fff805..4b793905339 100644 --- a/extension/llm/custom_ops/CMakeLists.txt +++ b/extension/llm/custom_ops/CMakeLists.txt @@ -69,7 +69,9 @@ install(TARGETS custom_ops DESTINATION lib) if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT) # Add a AOT library - find_package(Torch CONFIG REQUIRED) + if(NOT TARGET torch) + find_package(Torch CONFIG REQUIRED) + endif() add_library( custom_ops_aot_lib SHARED ${_custom_ops__srcs}