Skip to content

Commit 1308d4d

Browse files
authored
Fix multiple find_package(Torch) calls (#8407)
While debugging the build issue on #8322 w.r.t mkl, I undercover a complex interaction between #8322, #8248 (to install mkl), and https://github.com/pytorch/pytorch/blob/main/cmake/public/mkl.cmake from PyTorch. The error is as follows: ``` CMake Error at /opt/conda/envs/py_3.10/lib/cmake/mkl/MKLConfig.cmake:744 (add_library): <-- This file comes from conda mkl add_library cannot create imported target "MKL::MKL" because another target with the same name already exists. Call Stack (most recent call first): /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Caffe2/public/mkl.cmake:1 (find_package) <-- this is from PyTorch /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Caffe2/Caffe2Config.cmake:106 (include) /opt/conda/envs/py_3.10/lib/python3.10/site-packages/torch/share/cmake/Torch/TorchConfig.cmake:68 (find_package) CMakeLists.txt:753 (find_package) ``` The conclusion is that, with mkl installed, there should be just one `find_package(Torch)` call because the mkl target is defined globally. The `torch` target, on the other hand, is only defined locally. So, this change adds `if(NOT TARGET torch)` check to only call `find_package(Torch)` if needed. ### Testing The change on top of #8322 looks like this f705b01 https://github.com/pytorch/executorch/actions/runs/13278590926?pr=8399
1 parent 012b5e9 commit 1308d4d

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,9 @@ if(EXECUTORCH_BUILD_PYBIND)
750750
endif()
751751

752752
# find pytorch lib, to allow pybind to take at::Tensor as input/output
753-
find_package(Torch CONFIG REQUIRED)
753+
if(NOT TARGET torch)
754+
find_package(Torch CONFIG REQUIRED)
755+
endif()
754756
find_library(
755757
TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib"
756758
)

build/Codegen.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ function(gen_custom_ops_aot_lib)
146146
${_out_dir}/CustomOpsNativeFunctions.h "${GEN_KERNEL_SOURCES}"
147147
)
148148
# Find `Torch`.
149-
find_package(Torch REQUIRED)
149+
if(NOT TARGET torch)
150+
find_package(Torch REQUIRED)
151+
endif()
150152
# This lib uses ATen lib, so we explicitly enable rtti and exceptions.
151153
target_compile_options(${GEN_LIB_NAME} PRIVATE -frtti -fexceptions)
152154
target_compile_definitions(${GEN_LIB_NAME} PRIVATE USE_ATEN_LIB=1)

extension/llm/custom_ops/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ install(TARGETS custom_ops DESTINATION lib)
6969

7070
if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
7171
# Add a AOT library
72-
find_package(Torch CONFIG REQUIRED)
72+
if(NOT TARGET torch)
73+
find_package(Torch CONFIG REQUIRED)
74+
endif()
7375
add_library(
7476
custom_ops_aot_lib SHARED
7577
${_custom_ops__srcs}

0 commit comments

Comments
 (0)