From bcf317c32df095e9cc81080e519580637a385d16 Mon Sep 17 00:00:00 2001 From: Huy Do Date: Tue, 11 Feb 2025 20:08:39 -0800 Subject: [PATCH 1/3] Fix multiple MKL targets --- CMakeLists.txt | 10 ++++++++-- build/Codegen.cmake | 4 +++- extension/llm/custom_ops/CMakeLists.txt | 4 +++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 30f1f5c42cf..6235fd918ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -602,6 +602,13 @@ target_compile_definitions(executorch PUBLIC C10_USING_CUSTOM_GENERATED_MACROS) target_compile_options(executorch PUBLIC ${_common_compile_options}) target_link_options_shared_lib(executorch) +# +# find pytorch lib here to make it available to all sub-directories, trying to +# call find_package(Torch) multiple times doesn't work because it brings in MKL +# which is defined once globally +# +find_package(Torch CONFIG REQUIRED) + # # portable_ops_lib: A library to register core ATen ops using portable kernels, # see kernels/portable/CMakeLists.txt. @@ -749,8 +756,7 @@ if(EXECUTORCH_BUILD_PYBIND) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools) endif() - # find pytorch lib, to allow pybind to take at::Tensor as input/output - find_package(Torch CONFIG REQUIRED) + # to allow pybind to take at::Tensor as input/output 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} From d1dec351255b4f9704b491c0917357c7863cb044 Mon Sep 17 00:00:00 2001 From: Huy Do Date: Tue, 11 Feb 2025 21:12:43 -0800 Subject: [PATCH 2/3] Revert "Fix multiple MKL targets" This reverts commit bcf317c32df095e9cc81080e519580637a385d16. --- CMakeLists.txt | 10 ++-------- build/Codegen.cmake | 4 +--- extension/llm/custom_ops/CMakeLists.txt | 4 +--- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6235fd918ec..30f1f5c42cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -602,13 +602,6 @@ target_compile_definitions(executorch PUBLIC C10_USING_CUSTOM_GENERATED_MACROS) target_compile_options(executorch PUBLIC ${_common_compile_options}) target_link_options_shared_lib(executorch) -# -# find pytorch lib here to make it available to all sub-directories, trying to -# call find_package(Torch) multiple times doesn't work because it brings in MKL -# which is defined once globally -# -find_package(Torch CONFIG REQUIRED) - # # portable_ops_lib: A library to register core ATen ops using portable kernels, # see kernels/portable/CMakeLists.txt. @@ -756,7 +749,8 @@ if(EXECUTORCH_BUILD_PYBIND) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/devtools) endif() - # to allow pybind to take at::Tensor as input/output + # find pytorch lib, to allow pybind to take at::Tensor as input/output + find_package(Torch CONFIG REQUIRED) find_library( TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_INSTALL_PREFIX}/lib" ) diff --git a/build/Codegen.cmake b/build/Codegen.cmake index f2da23baeaa..c36b1a26b40 100644 --- a/build/Codegen.cmake +++ b/build/Codegen.cmake @@ -146,9 +146,7 @@ function(gen_custom_ops_aot_lib) ${_out_dir}/CustomOpsNativeFunctions.h "${GEN_KERNEL_SOURCES}" ) # Find `Torch`. - if(NOT TARGET torch) - find_package(Torch REQUIRED) - endif() + find_package(Torch REQUIRED) # 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 4b793905339..16ca4fff805 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 CONFIG REQUIRED) add_library( custom_ops_aot_lib SHARED ${_custom_ops__srcs} From 69105d04be47ce5f8d46f6ddfef20486d00b8a01 Mon Sep 17 00:00:00 2001 From: Huy Do Date: Tue, 11 Feb 2025 20:08:39 -0800 Subject: [PATCH 3/3] Fix multiple MKL targets --- CMakeLists.txt | 4 +++- build/Codegen.cmake | 4 +++- extension/llm/custom_ops/CMakeLists.txt | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) 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}