commit:     6d9c1bc07ff09bf6869fd5713a89c0d5e6d4b8fc
Author:     Sv. Lockal <lockalsash <AT> gmail <DOT> com>
AuthorDate: Sat Jul  5 21:20:37 2025 +0000
Commit:     Alfredo Tupone <tupone <AT> gentoo <DOT> org>
CommitDate: Sun Jul  6 08:48:08 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=6d9c1bc0

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 <AT> gmail.com>
Part-of: https://github.com/gentoo/gentoo/pull/42891
Signed-off-by: Alfredo Tupone <tupone <AT> gentoo.org>

 .../files/pytorch-2.7.1-cpp-extension-libcxx.patch | 22 +++++++++++++++++++
 .../pytorch-2.7.1-cpp-extension-multilib.patch     | 25 ++++++++++++++++++++++
 ...ytorch-2.7.1.ebuild => pytorch-2.7.1-r1.ebuild} | 24 +++++++++++++--------
 3 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-libcxx.patch 
b/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-libcxx.patch
new file mode 100644
index 000000000000..b4701ca7ac65
--- /dev/null
+++ b/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-libcxx.patch
@@ -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}')

diff --git a/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-multilib.patch 
b/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-multilib.patch
new file mode 100644
index 000000000000..6277afc30d23
--- /dev/null
+++ b/sci-ml/pytorch/files/pytorch-2.7.1-cpp-extension-multilib.patch
@@ -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
+ 

diff --git a/sci-ml/pytorch/pytorch-2.7.1.ebuild 
b/sci-ml/pytorch/pytorch-2.7.1-r1.ebuild
similarity index 74%
rename from sci-ml/pytorch/pytorch-2.7.1.ebuild
rename to sci-ml/pytorch/pytorch-2.7.1-r1.ebuild
index c6ade562815d..0988d3784ce9 100644
--- a/sci-ml/pytorch/pytorch-2.7.1.ebuild
+++ b/sci-ml/pytorch/pytorch-2.7.1-r1.ebuild
@@ -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

Reply via email to