sci-ml/pytorch: fix compilation of HIP extensions

Noticed in sci-ml/torchvision:
1) compilation failed on multilib (Gentoo-related)
2) compilation failed with Clang-libc++ (patch should be in upstream)

Bug: https://bugs.gentoo.org/959589
Signed-off-by: Sv. Lockal <lockalsash@gmail.com>
Part-of: https://github.com/gentoo/gentoo/pull/42891
Signed-off-by: Alfredo Tupone <tupone@gentoo.org>
This commit is contained in:
Sv. Lockal
2025-07-05 21:20:37 +00:00
committed by Alfredo Tupone
parent c92318c51c
commit 6d9c1bc07f
3 changed files with 62 additions and 9 deletions

View File

@@ -0,0 +1,22 @@
Fixes parsing of Clang versions like 20.1.7+libcxx
Upstream bug: https://github.com/pytorch/pytorch/issues/157665
--- a/torch/utils/cpp_extension.py
+++ b/torch/utils/cpp_extension.py
@@ -434,13 +434,12 @@ def get_compiler_abi_compatibility_and_version(compiler) -> tuple[bool, TorchVer
try:
if IS_LINUX:
minimum_required_version = MINIMUM_GCC_VERSION
- versionstr = subprocess.check_output([compiler, '-dumpfullversion', '-dumpversion'])
- version = versionstr.decode(*SUBPROCESS_DECODE_ARGS).strip().split('.')
+ compiler_info = subprocess.check_output([compiler, '-dumpfullversion', '-dumpversion'])
else:
minimum_required_version = MINIMUM_MSVC_VERSION
compiler_info = subprocess.check_output(compiler, stderr=subprocess.STDOUT)
- match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.decode(*SUBPROCESS_DECODE_ARGS).strip())
- version = ['0', '0', '0'] if match is None else list(match.groups())
+ match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.decode(*SUBPROCESS_DECODE_ARGS).strip())
+ version = ['0', '0', '0'] if match is None else list(match.groups())
except Exception:
_, error, _ = sys.exc_info()
warnings.warn(f'Error checking compiler version for {compiler}: {error}')

View File

@@ -0,0 +1,25 @@
Fixes multilib (%LIB_DIR% should be replaced in ebuild)
--- a/torch/utils/cpp_extension.py
+++ b/torch/utils/cpp_extension.py
@@ -1478,10 +1477,10 @@ def library_paths(device_type: str = "cpu") -> list[str]:
paths = [TORCH_LIB_PATH]
if device_type == "cuda" and IS_HIP_EXTENSION:
- lib_dir = 'lib'
+ lib_dir = '%LIB_DIR%'
paths.append(_join_rocm_home(lib_dir))
if HIP_HOME is not None:
- paths.append(os.path.join(HIP_HOME, 'lib'))
+ paths.append(os.path.join(HIP_HOME, '%LIB_DIR%'))
elif device_type == "cuda":
if IS_WINDOWS:
lib_dir = os.path.join('lib', 'x64')
@@ -2295,7 +2294,7 @@ def _prepare_ldflags(extra_ldflags, with_cuda, verbose, is_standalone):
if CUDNN_HOME is not None:
extra_ldflags.append(f'-L{os.path.join(CUDNN_HOME, "lib64")}')
elif IS_HIP_EXTENSION:
- extra_ldflags.append(f'-L{_join_rocm_home("lib")}')
+ extra_ldflags.append(f'-L{_join_rocm_home("%LIB_DIR%")}')
extra_ldflags.append('-lamdhip64')
return extra_ldflags

View File

@@ -34,19 +34,25 @@ DEPEND="${RDEPEND}
')
"
PATCHES=(
"${FILESDIR}"/${PN}-2.6.0-dontbuildagain.patch
"${FILESDIR}"/${PN}-2.7.1-cpp-extension-libcxx.patch
"${FILESDIR}"/${PN}-2.7.1-cpp-extension-multilib.patch
)
src_prepare() {
eapply "${FILESDIR}"/${PN}-2.6.0-dontbuildagain.patch
# Replace placeholders added by cpp-extension.patch
sed -e "s|%LIB_DIR%|$(get_libdir)|g" \
-i torch/utils/cpp_extension.py || die
# Set build dir for pytorch's setup
sed -i \
-e "/BUILD_DIR/s|build|/var/lib/caffe2/|" \
tools/setup_helpers/env.py \
|| die
sed -e "/BUILD_DIR/s|build|/var/lib/caffe2/|" \
-i tools/setup_helpers/env.py || die
# Drop legacy from pyproject.toml
sed -i \
-e "/build-backend/s|:__legacy__||" \
pyproject.toml \
|| die
sed -e "/build-backend/s|:__legacy__||" \
-i pyproject.toml || die
distutils-r1_src_prepare
# Get object file from caffe2