mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-06 12:20:52 +01:00
[Reland] fix some MKL detection issues of CMake (#94924)
This is reland of PR #94402 that tries to solve the additional link issues. The PR #94402 failed because caffe2::mkl had been converted to private dependency while libtorch_cuda_linalg hadn't linked to it explicitly. This is fixed in commit 4373bf0ae3dee32afc178f9d51a4154d6c5904c6 We also replace more references of MKL_LIBRARIES by caffe2::mkl in this PR. Pull Request resolved: https://github.com/pytorch/pytorch/pull/94924 Approved by: https://github.com/malfet
This commit is contained in:
parent
a1dc2b1774
commit
dc2b7aa955
|
|
@ -965,6 +965,9 @@ elseif(USE_CUDA)
|
|||
torch_cpu
|
||||
torch_cuda
|
||||
)
|
||||
if(CAFFE2_USE_MKL)
|
||||
target_link_libraries(torch_cuda_linalg PRIVATE caffe2::mkl)
|
||||
endif()
|
||||
if($ENV{ATEN_STATIC_CUDA})
|
||||
target_link_libraries(torch_cuda_linalg PRIVATE
|
||||
CUDA::cusolver_static
|
||||
|
|
@ -1527,7 +1530,9 @@ if(BUILD_SHARED_LIBS)
|
|||
if(USE_MPI)
|
||||
target_link_libraries(torch_global_deps ${MPI_CXX_LIBRARIES})
|
||||
endif()
|
||||
target_link_libraries(torch_global_deps ${MKL_LIBRARIES})
|
||||
if(CAFFE2_USE_MKL)
|
||||
target_link_libraries(torch_global_deps caffe2::mkl)
|
||||
endif()
|
||||
# The CUDA libraries are linked here for a different reason: in some
|
||||
# cases we load these libraries with ctypes, and if they weren't opened
|
||||
# with RTLD_GLOBAL, we'll do the "normal" search process again (and
|
||||
|
|
@ -1660,6 +1665,9 @@ if(BUILD_TEST)
|
|||
if(NOT MSVC)
|
||||
target_compile_options(${test_name} PRIVATE -Wno-unused-variable)
|
||||
endif()
|
||||
if(CAFFE2_USE_MKL)
|
||||
target_link_libraries(${test_name} torch_library caffe2::mkl)
|
||||
endif()
|
||||
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
|
||||
if(INSTALL_TEST)
|
||||
install(TARGETS ${test_name} DESTINATION test)
|
||||
|
|
|
|||
|
|
@ -201,7 +201,7 @@ elseif(BLAS STREQUAL "MKL")
|
|||
message(STATUS "MKL OpenMP type: ${MKL_OPENMP_TYPE}")
|
||||
message(STATUS "MKL OpenMP library: ${MKL_OPENMP_LIBRARY}")
|
||||
include_directories(AFTER SYSTEM ${MKL_INCLUDE_DIR})
|
||||
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS caffe2::mkl)
|
||||
list(APPEND Caffe2_DEPENDENCY_LIBS caffe2::mkl)
|
||||
set(CAFFE2_USE_MKL ON)
|
||||
set(BLAS_INFO "mkl")
|
||||
set(BLAS_FOUND 1)
|
||||
|
|
|
|||
|
|
@ -41,10 +41,14 @@ IF (WIN32)
|
|||
ELSE (WIN32)
|
||||
SET(DEFAULT_INTEL_COMPILER_DIR "/opt/intel")
|
||||
SET(DEFAULT_INTEL_MKL_DIR "/opt/intel/mkl")
|
||||
if (EXISTS "/opt/intel/oneapi")
|
||||
SET(DEFAULT_INTEL_COMPILER_DIR "/opt/intel/oneapi")
|
||||
if (EXISTS "/opt/intel/oneapi/mkl/latest")
|
||||
SET(DEFAULT_INTEL_MKL_DIR "/opt/intel/oneapi/mkl/latest")
|
||||
SET(DEFAULT_INTEL_ONEAPI_DIR "/opt/intel/oneapi")
|
||||
if (EXISTS "${DEFAULT_INTEL_ONEAPI_DIR}")
|
||||
SET(DEFAULT_INTEL_COMPILER_DIR "${DEFAULT_INTEL_ONEAPI_DIR}")
|
||||
if (EXISTS "${DEFAULT_INTEL_ONEAPI_DIR}/compiler/latest")
|
||||
SET(DEFAULT_INTEL_COMPILER_DIR "${DEFAULT_INTEL_ONEAPI_DIR}/compiler/latest")
|
||||
endif()
|
||||
if (EXISTS "${DEFAULT_INTEL_ONEAPI_DIR}/mkl/latest")
|
||||
SET(DEFAULT_INTEL_MKL_DIR "${DEFAULT_INTEL_ONEAPI_DIR}/mkl/latest")
|
||||
endif()
|
||||
endif()
|
||||
ENDIF (WIN32)
|
||||
|
|
@ -170,7 +174,7 @@ IF (EXISTS ${INTEL_OMP_DIR})
|
|||
ENDIF()
|
||||
IF (APPLE)
|
||||
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH}
|
||||
"${INTEL_OMP_DIR}/lib")
|
||||
"${INTEL_OMP_DIR}/lib" "${INTEL_COMPILER_DIR}/mac/compiler/lib")
|
||||
ENDIF()
|
||||
ENDIF()
|
||||
|
||||
|
|
@ -379,7 +383,7 @@ ENDIF (NOT MKL_LIBRARIES)
|
|||
|
||||
# Include files
|
||||
IF (MKL_LIBRARIES)
|
||||
FIND_PATH(MKL_INCLUDE_DIR "mkl_cblas.h")
|
||||
FIND_PATH(MKL_INCLUDE_DIR NAMES "mkl_cblas.h" PATHS "/usr/include/mkl")
|
||||
MARK_AS_ADVANCED(MKL_INCLUDE_DIR)
|
||||
ENDIF (MKL_LIBRARIES)
|
||||
|
||||
|
|
|
|||
|
|
@ -227,8 +227,9 @@ function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
|
|||
# http://openmp.llvm.org/
|
||||
#
|
||||
# So here, before we test each flag combination, we first try directly
|
||||
# linking against any `libomp` MKL has found (if any). This allows us to
|
||||
# do sensible things in tricky (yet common) conditions like:
|
||||
# linking against any `libomp` MKL has linked to (if any and when MKL is
|
||||
# specified). This allows us to do sensible things in tricky (yet common)
|
||||
# conditions like:
|
||||
# - using `clang` (so no native GNU OpenMP), and
|
||||
# - having `brew` `libomp` installed at `/usr/local/`, and
|
||||
# - having `conda` `mkl` installed at `$HOME/conda/`, with includes a copy
|
||||
|
|
@ -236,26 +237,23 @@ function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
|
|||
# Rather than blindly picking one, we pick what ever `FindMKL.cmake` choses
|
||||
# to avoid conflicts.
|
||||
#
|
||||
# Crucially, we only do so for non-GNU compilers. For GNU ones,
|
||||
# `FindMKL.cmake` calls `FindOpenMP.cmake` when trying to find `gomp` and
|
||||
# thus will cause infinite recursion if this is not taken care of. Moreover,
|
||||
# for them, since the compiler provices the OpenMP library, it is most
|
||||
# likely that only one viable gomp library can be found in search path by
|
||||
# `FindOpenMP.cmake`, so the chance of having conflicts is slow.
|
||||
#
|
||||
# TODO: refactor to solve this weird dependency where
|
||||
# - for non-GNU, FindOpenMP.cmake replies on FindMKL.cmake to finish first, but
|
||||
# - for GNU, FindMKL.cmake replies on FindOpenMP.cmake to finish first.
|
||||
# thus will cause infinite recursion if this is not taken care of. Therefore,
|
||||
# we record an internal flag to detect repeatedly inclusion.
|
||||
|
||||
if(NOT "${CMAKE_${LANG}_COMPILER_ID}" STREQUAL "GNU")
|
||||
if(NOT "${CMAKE_${LANG}_COMPILER_ID}" STREQUAL "GNU" AND BLAS STREQUAL "MKL" AND NOT IN_FIND_OMP)
|
||||
set(IN_FIND_OMP ON CACHE BOOL "" FORCE)
|
||||
find_package(MKL QUIET)
|
||||
unset(IN_FIND_OMP CACHE)
|
||||
if(MKL_FOUND AND MKL_OPENMP_LIBRARY)
|
||||
# If we already link OpenMP via MKL, use that. Otherwise at run-time
|
||||
# OpenMP will complain about being initialized twice (OMP: Error #15),
|
||||
# can may cause incorrect behavior.
|
||||
set(OpenMP_libomp_LIBRARY "${MKL_OPENMP_LIBRARY}" CACHE STRING "libomp location for OpenMP")
|
||||
if("-fopenmp=libiomp5" IN_LIST OpenMP_${LANG}_FLAG_CANDIDATES)
|
||||
set(OPENMP_FLAG "-fopenmp=libiomp5")
|
||||
if(OpenMP_libomp_LIBRARY MATCHES "iomp5")
|
||||
if("-fopenmp=libiomp5" IN_LIST OpenMP_${LANG}_FLAG_CANDIDATES)
|
||||
set(OPENMP_FLAGS_TEST "-fopenmp=libiomp5")
|
||||
endif()
|
||||
endif()
|
||||
else()
|
||||
find_library(OpenMP_libomp_LIBRARY
|
||||
|
|
|
|||
|
|
@ -4,14 +4,16 @@ if(NOT TARGET caffe2::mkl)
|
|||
add_library(caffe2::mkl INTERFACE IMPORTED)
|
||||
endif()
|
||||
|
||||
set_property(
|
||||
TARGET caffe2::mkl PROPERTY INTERFACE_INCLUDE_DIRECTORIES
|
||||
${MKL_INCLUDE_DIR})
|
||||
set_property(
|
||||
TARGET caffe2::mkl PROPERTY INTERFACE_LINK_LIBRARIES
|
||||
${MKL_LIBRARIES} ${MKL_THREAD_LIB})
|
||||
# TODO: This is a hack, it will not pick up architecture dependent
|
||||
# MKL libraries correctly; see https://github.com/pytorch/pytorch/issues/73008
|
||||
set_property(
|
||||
TARGET caffe2::mkl PROPERTY INTERFACE_LINK_DIRECTORIES
|
||||
${MKL_ROOT}/lib)
|
||||
target_include_directories(caffe2::mkl INTERFACE ${MKL_INCLUDE_DIR})
|
||||
target_link_libraries(caffe2::mkl INTERFACE ${MKL_LIBRARIES})
|
||||
|
||||
# TODO: This is a hack, it will not pick up architecture dependent MKL libraries
|
||||
# correctly; see https://github.com/pytorch/pytorch/issues/73008
|
||||
foreach(MKL_LIB IN LISTS MKL_LIBRARIES)
|
||||
if(EXISTS "${MKL_LIB}")
|
||||
get_filename_component(MKL_LINK_DIR "${MKL_LIB}" DIRECTORY)
|
||||
if(IS_DIRECTORY "${MKL_LINK_DIR}")
|
||||
target_link_directories(caffe2::mkl INTERFACE "${MKL_LINK_DIR}")
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user