Skip to content

Commit

Permalink
Fix multiple find_package(Torch) calls (#8407)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
huydhn authored Feb 12, 2025
1 parent 012b5e9 commit 1308d4d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
4 changes: 3 additions & 1 deletion build/Codegen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion extension/llm/custom_ops/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 1308d4d

Please sign in to comment.