Skip to content

Commit 70518b7

Browse files
[CD] Fix the libgomp twice load issue (pytorch#150084) (pytorch#153518)
Fixes pytorch#149422 Pull Request resolved: pytorch#150084 Approved by: https://github.com/malfet, https://github.com/leslie-fang-intel, https://github.com/atalman Co-authored-by: LifengWang <[email protected]>
1 parent ab54c47 commit 70518b7

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

.ci/manywheel/build_common.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ for pkg in /$WHEELHOUSE_DIR/torch_no_python*.whl /$WHEELHOUSE_DIR/torch*linux*.w
333333
# ROCm workaround for roctracer dlopens
334334
if [[ "$DESIRED_CUDA" == *"rocm"* ]]; then
335335
patchedpath=$(fname_without_so_number $destpath)
336-
# Keep the so number for XPU dependencies
337-
elif [[ "$DESIRED_CUDA" == *"xpu"* ]]; then
336+
# Keep the so number for XPU dependencies and libgomp.so.1 to avoid twice load
337+
elif [[ "$DESIRED_CUDA" == *"xpu"* || "$filename" == "libgomp.so.1" ]]; then
338338
patchedpath=$destpath
339339
else
340340
patchedpath=$(fname_with_sha256 $destpath)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import ctypes
2+
import os
3+
import sys
4+
from pathlib import Path
5+
6+
7+
def get_gomp_thread():
8+
"""
9+
Retrieves the maximum number of OpenMP threads after loading the `libgomp.so.1` library
10+
and the `libtorch_cpu.so` library. It then queries the
11+
maximum number of threads available for OpenMP parallel regions using the
12+
`omp_get_max_threads` function.
13+
14+
Returns:
15+
int: The maximum number of OpenMP threads available.
16+
17+
Notes:
18+
- The function assumes the default path for `libgomp.so.1` on AlmaLinux OS.
19+
- The path to `libtorch_cpu.so` is constructed based on the Python executable's
20+
installation directory.
21+
- This function is specific to environments where PyTorch and OpenMP are used
22+
together and may require adjustments for other setups.
23+
"""
24+
python_path = Path(sys.executable).resolve()
25+
python_prefix = (
26+
python_path.parent.parent
27+
) # Typically goes to the Python installation root
28+
29+
# Get the additional ABI flags (if any); it may be an empty string.
30+
abiflags = getattr(sys, "abiflags", "")
31+
32+
# Construct the Python directory name correctly (e.g., "python3.13t").
33+
python_version = (
34+
f"python{sys.version_info.major}.{sys.version_info.minor}{abiflags}"
35+
)
36+
37+
libtorch_cpu_path = (
38+
python_prefix
39+
/ "lib"
40+
/ python_version
41+
/ "site-packages"
42+
/ "torch"
43+
/ "lib"
44+
/ "libtorch_cpu.so"
45+
)
46+
47+
# use the default gomp path of AlmaLinux OS
48+
libgomp_path = "/usr/lib64/libgomp.so.1"
49+
50+
os.environ["GOMP_CPU_AFFINITY"] = "0-3"
51+
52+
libgomp = ctypes.CDLL(libgomp_path)
53+
libgomp = ctypes.CDLL(libtorch_cpu_path)
54+
55+
libgomp.omp_get_max_threads.restype = ctypes.c_int
56+
libgomp.omp_get_max_threads.argtypes = []
57+
58+
omp_max_threads = libgomp.omp_get_max_threads()
59+
return omp_max_threads
60+
61+
62+
def main():
63+
omp_max_threads = get_gomp_thread()
64+
print(
65+
f"omp_max_threads after loading libgomp.so and libtorch_cpu.so: {omp_max_threads}"
66+
)
67+
if omp_max_threads == 1:
68+
raise RuntimeError(
69+
"omp_max_threads is 1. Check whether libgomp.so is loaded twice."
70+
)
71+
72+
73+
if __name__ == "__main__":
74+
main()

.circleci/scripts/binary_linux_test.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ fi
9292
if [[ "\$GPU_ARCH_TYPE" != *s390x* && "\$GPU_ARCH_TYPE" != *xpu* && "\$GPU_ARCH_TYPE" != *rocm* && "$PACKAGE_TYPE" != libtorch ]]; then
9393
# Exclude s390, xpu, rocm and libtorch builds from smoke testing
9494
python /pytorch/.ci/pytorch/smoke_test/smoke_test.py --package=torchonly --torch-compile-check disabled
95+
96+
if [[ "\$GPU_ARCH_TYPE" != *cpu-aarch64* ]]; then
97+
# test for issue https://github.com/pytorch/pytorch/issues/149422
98+
python /pytorch/.ci/pytorch/smoke_test/check_gomp.py
99+
fi
95100
fi
96101
97102
# Clean temp files

0 commit comments

Comments
 (0)