diff --git a/cmake/onnxruntime_python.cmake b/cmake/onnxruntime_python.cmake index 8182bc470a..cfd5db7723 100644 --- a/cmake/onnxruntime_python.cmake +++ b/cmake/onnxruntime_python.cmake @@ -154,9 +154,19 @@ if (onnxruntime_ENABLE_EAGER_MODE OR onnxruntime_ENABLE_LAZY_TENSOR) "${REPO_ROOT}/cmake/external/protobuf/src" ${TORCH_INCLUDE_DIRS}) + if (onnxruntime_ENABLE_EAGER_MODE) + # For eager mode, torch build has a mkl dependency from torch's cmake config, + # Linking to torch libraries to avoid this unnecessary mkl dependency. + target_include_directories(onnxruntime_pybind11_state PRIVATE "${TORCH_INSTALL_PREFIX}/include" "${TORCH_INSTALL_PREFIX}/include/torch/csrc/api/include") + find_library(LIBTORCH_LIBRARY torch PATHS "${TORCH_INSTALL_PREFIX}/lib") + find_library(LIBTORCH_CPU_LIBRARY torch_cpu PATHS "${TORCH_INSTALL_PREFIX}/lib") + find_library(LIBC10_LIBRARY c10 PATHS "${TORCH_INSTALL_PREFIX}/lib") + target_link_libraries(onnxruntime_pybind11_state PRIVATE ${LIBTORCH_LIBRARY} ${LIBTORCH_CPU_LIBRARY} ${LIBC10_LIBRARY}) + endif() + # Explicitly link torch_python to workaround https://github.com/pytorch/pytorch/issues/38122#issuecomment-694203281 find_library(TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib") - target_link_libraries(onnxruntime_pybind11_state PRIVATE ${TORCH_PYTHON_LIBRARY} ${TORCH_LIBRARIES}) + target_link_libraries(onnxruntime_pybind11_state PRIVATE ${TORCH_PYTHON_LIBRARY}) if (onnxruntime_ENABLE_EAGER_MODE) target_link_libraries(onnxruntime_pybind11_state PRIVATE onnxruntime_eager) endif()