solve apl dependency issue (#145215)

According to the [APL documentation](https://developer.arm.com/documentation/101004/2404/General-information/Arm-Performance-Libraries-example-programs), libraries ending with _mp are OpenMP multi-threaded libraries.

When a project is compiled with MSVC and the -openmp flag, the vcomp library (Visual C++ implementation of OpenMP) is used for runtime calls.

However, the current APL implementation uses the libomp.dll (LLVM) variant.

As a result, there are unexpected behaviors at runtime.

---

For Example:

```python
import torch

# Create a sparse tensor
# Input (Sparse Tensor):
# [[0, 1],
#  [1, 0]]
indices = torch.tensor([[0, 1], [1, 0]])
values = torch.tensor([1, 1], dtype=torch.float32)
size = torch.Size([2, 2])

sparse_tensor = torch.sparse_coo_tensor(indices, values, size)

# Convert sparse tensor to dense tensor
dense_tensor = sparse_tensor.to_dense()

# Expected Output (Dense Tensor):
# [[0, 1],
#  [1, 0]]
print("\nDense Tensor:")
print(dense_tensor)
```

However, it prints unexpected outputs such as:

```python
# [[0, 11],
#  [10, 0]]
```

The issue arises because the following code does not function as expected at runtime:

https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/ParallelOpenMP.h#L30

```c++
// returns 1 , however since OpenMP is enabled it should return total number of threads
int64_t num_threads = omp_get_num_threads();
```

---

In the runtime, loading multiple OpenMP libraries (in this case `libomp` and `vcomp`) is causing unexpected behaviours.

So, we've changed libraries from `_mp` to non `_mp` versions and we used `vcomp` for OpenMP calls.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/145215
Approved by: https://github.com/ozanMSFT, https://github.com/malfet

Co-authored-by: Ozan Aydin <148207261+ozanMSFT@users.noreply.github.com>
This commit is contained in:
Stefan-Alin Pahontu
2025-01-27 13:02:16 +00:00
committed by PyTorch MergeBot
parent 7b6029dcc2
commit 0674ab7e33
2 changed files with 30 additions and 4 deletions

View File

@ -10,6 +10,7 @@
SET(APL_INCLUDE_SEARCH_PATHS $ENV{ARMPL_DIR}/include)
SET(APL_LIB_SEARCH_PATHS $ENV{ARMPL_DIR}/lib)
SET(APL_BIN_SEARCH_PATHS $ENV{ARMPL_DIR}/bin)
SET(APL_FOUND ON)
@ -21,21 +22,36 @@ IF(NOT APL_INCLUDE_DIR)
ENDIF()
# Check lib file
FIND_PATH(APL_LIB_DIR NAMES libarmpl_lp64_mp.dll.lib libomp.dll.lib libarmpl_lp64_mp.a PATHS ${APL_LIB_SEARCH_PATHS})
FIND_PATH(APL_LIB_DIR NAMES armpl_lp64.dll.lib libarmpl_lp64.a PATHS ${APL_LIB_SEARCH_PATHS})
IF(NOT APL_LIB_DIR)
SET(APL_FOUND OFF)
MESSAGE(STATUS "Could not verify APL lib directory. Turning APL_FOUND off")
ENDIF()
# Check bin file
FIND_PATH(APL_BIN_DIR NAMES armpl_lp64.dll libarmpl_lp64.a PATHS ${APL_BIN_SEARCH_PATHS})
IF(NOT APL_BIN_DIR)
SET(APL_FOUND OFF)
MESSAGE(STATUS "Could not verify APL bin directory. Turning APL_FOUND off")
ENDIF()
IF (APL_FOUND)
IF(WIN32)
set(APL_LIBRARIES
"${APL_LIB_DIR}/libarmpl_lp64_mp.dll.lib"
"${APL_LIB_DIR}/libomp.dll.lib"
"${APL_LIB_DIR}/armpl_lp64.dll.lib"
)
set(APL_DLLS
"${CMAKE_INSTALL_PREFIX}/lib/armpl_lp64.dll"
)
add_custom_command(
OUTPUT ${APL_DLLS}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_INSTALL_PREFIX}/lib"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${APL_BIN_DIR}/armpl_lp64.dll" "${CMAKE_INSTALL_PREFIX}/lib/armpl_lp64.dll"
)
add_custom_target(copy_apl_dlls ALL DEPENDS ${APL_DLLS})
ELSEIF(UNIX)
set(APL_LIBRARIES
"${APL_LIB_DIR}/libarmpl_lp64_mp.a"
"${APL_LIB_DIR}/libarmpl_lp64.a"
)
ENDIF()
MESSAGE(STATUS "Found APL header: ${APL_INCLUDE_DIR}")

View File

@ -254,6 +254,16 @@ function(_OPENMP_GET_FLAGS LANG FLAG_MODE OPENMP_FLAG_VAR OPENMP_LIB_NAMES_VAR)
set(OpenMP_libomp_LIBRARY "${MKL_OPENMP_LIBRARY}" CACHE STRING "libomp location for OpenMP")
endif()
if ((NOT OpenMP_libomp_LIBRARY) AND MSVC AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
# On MSVC ARM64, OpenMP is provided by vcomp, which is a part of the Visual Studio installation.
find_library(OpenMP_libomp_LIBRARY
NAMES vcomp
HINTS ${CMAKE_${LANG}_IMPLICIT_LINK_DIRECTORIES}
DOC "vcomp location for OpenMP on MSVC ARM64"
)
mark_as_advanced(OpenMP_libomp_LIBRARY)
endif()
if (NOT OpenMP_libomp_LIBRARY)
find_library(OpenMP_libomp_LIBRARY
NAMES omp gomp iomp5