Avoid linking multiple OMP runtimes in libtorch_cpu.so if BLAS used is OpenBLAS. (#147725)

When PyTorch is built with OpenBLAS support and libopenblas is ldrectly linked with libgomp.so the libtorch_cpu.so ends up getting multiple omp runtimes linked against it. This may result in unexpected runtime behaviour /regression. This patch fixes this by avoiding linking against libomp.so if OpenBLAS is linked against libgomp.so

Fixes #146603

Pull Request resolved: https://github.com/pytorch/pytorch/pull/147725
Approved by: https://github.com/albanD
This commit is contained in:
Vinitha Vijayan
2025-04-29 23:39:43 +00:00
committed by PyTorch MergeBot
parent a1a4fee3b8
commit e872bf8f88
2 changed files with 38 additions and 0 deletions

View File

@ -59,6 +59,32 @@ ELSE (OpenBLAS_FOUND)
ENDIF (OpenBLAS_FIND_REQUIRED)
ENDIF (OpenBLAS_FOUND)
IF(OpenBLAS_LIB)
# Run ldd on the OpenBLAS library
execute_process(
COMMAND ldd "${OpenBLAS_LIB}"
OUTPUT_VARIABLE LDD_OUTPUT
ERROR_VARIABLE LDD_ERROR
RESULT_VARIABLE LDD_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT LDD_RESULT EQUAL 0)
message(WARNING "ldd failed on ${OpenBLAS_LIB}: ${LDD_ERROR}")
endif()
# Check if the output contains "libgomp"
string(FIND "${LDD_OUTPUT}" "libgomp" LIBGOMP_FOUND_INDEX)
if(LIBGOMP_FOUND_INDEX GREATER -1)
message(STATUS "OpenBLAS is directly linked against libgomp")
set(OPENBLAS_USES_LIBGOMP TRUE CACHE BOOL "OpenBLAS uses libgomp")
else()
message(STATUS "OpenBLAS is not directly linked against libgomp")
set(OPENBLAS_USES_LIBGOMP FALSE CACHE BOOL "OpenBLAS uses libgomp")
endif()
ENDIF(OpenBLAS_LIB)
MARK_AS_ADVANCED(
OpenBLAS_INCLUDE_DIR
OpenBLAS_LIB

View File

@ -277,6 +277,18 @@ function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
mark_as_advanced(OpenMP_libomp_LIBRARY)
endif()
# Check if we are using OpenBLAS which is linked against libgomp
# we may end up with multiple omp runtimes linked
# against libtorch_cpu.so
if(OpenBLAS_LIB AND OPENBLAS_USES_LIBGOMP)
find_library(OpenMP_libomp_LIBRARY
NAMES gomp
HINTS ${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}
DOC "libomp location for OpenMP"
)
mark_as_advanced(OpenMP_libomp_LIBRARY)
endif()
if (NOT OpenMP_libomp_LIBRARY)
find_library(OpenMP_libomp_LIBRARY
NAMES omp gomp iomp5