diff --git a/sci-ml/pytorch/Manifest b/sci-ml/pytorch/Manifest index d41e29f2e8ca6..e525ede57b4c5 100644 --- a/sci-ml/pytorch/Manifest +++ b/sci-ml/pytorch/Manifest @@ -2,3 +2,4 @@ DIST composable_kernel-f1746955.tar.gz 7687217 BLAKE2B 973bf5b8eece2944e053c17eb DIST flash-attention-2.7.4.gh.tar.gz 5841323 BLAKE2B 432999d763f2b3d732580ddfea5d3e01370351db0656546259a5e500a07516dd03c98828bfb55855dabe4adc651033b5d97ea4725ca46158b9970f0fbc662710 SHA512 05a4afb09e666f7404d6a3f8b5256e7bed6eba60a6f1bde2b7dbb96d318975f0b458c2521c7a38d88e97b6e4c27f29077cf787849daf82586e33f43a3d9a84b3 DIST pytorch-2.12.0.tar.gz 64740318 BLAKE2B e5e46a93422454da420563d0a8057c87d1ed4b27e0bd9b3b30e46b5e40cfbb489072144ac00267af2f085432d55e12adf750daf15ea165a1252449b00dca1ff9 SHA512 f5ab0f6933d88271f772b416f8c9b3b0d3e1ffaf8d00838b455206266b40d7c805e34003d84b01cddc7f1ad917dd72d6f79a05063b0962cbf223d9042cff3206 DIST pytorch-2.13.0.tar.gz 66369404 BLAKE2B 373df49b4f2d504aafedd948e34562696335d316ebcd497dbd86a54a3a3c39310d1595050cc2bcae858d0085e6a0c59ed5299962d6a2706441ab28c1699c4821 SHA512 0f7465835ae9206746d507e1b927afb94f8860248473f2d1c5d71ccd40defbd23d0e3e27a1b09deb907e753a7c9008352830170b53441701ade0916d68a42562 +DIST pytorch-2.14.0.tar.gz 69721997 BLAKE2B 8f6ba5cca094ca3dc4d7b3cf2bd9972196b71b84506c54ecf40f9fa80f0f87b469f3bd04b1200766c19aa4faf94b5bf4f1c6477bb98db1a0b26217fa4b2de10d SHA512 403f160b73800739772c54208b0edb2e4a462a45d282cf03b38e8b1e3f3e45053974bfdba57c4249a1d0cc47f7807fd1ff11c22a321306bf74e758f2ccb4cb18 diff --git a/sci-ml/pytorch/files/pytorch-2.14.0-license.patch b/sci-ml/pytorch/files/pytorch-2.14.0-license.patch new file mode 100644 index 0000000000000..a1886ce1ff5db --- /dev/null +++ b/sci-ml/pytorch/files/pytorch-2.14.0-license.patch @@ -0,0 +1,10 @@ +--- a/pyproject.toml 2026-09-03 16:46:07.080977356 +0200 ++++ b/pyproject.toml 2026-09-03 18:11:07.550648831 +0200 +@@ -354,7 +354,6 @@ + "LICENSE", + "third_party/**/LICENSE", + "third_party/**/LICENSE.txt", +- "third_party/**/LICENSE.rst", + ] + authors = [{ name = "PyTorch Team", email = "packages@pytorch.org" }] + keywords = ["pytorch", "machine learning"] diff --git a/sci-ml/pytorch/files/pytorch-2.14.0-removekineto-pr178960.patch b/sci-ml/pytorch/files/pytorch-2.14.0-removekineto-pr178960.patch new file mode 100644 index 0000000000000..ee083a12b75a6 --- /dev/null +++ b/sci-ml/pytorch/files/pytorch-2.14.0-removekineto-pr178960.patch @@ -0,0 +1,288 @@ +From 37e33881b9e995f8328da0a149d71f4314bd7aa1 Mon Sep 17 00:00:00 2001 +From: Samaresh Kumar Singh +Date: Tue, 31 Mar 2026 21:39:08 -0500 +Subject: [PATCH 1/3] profiler: fix USE_KINETO=OFF build failure due to + unconditional ActivityType.h include + +kineto_shim.h was unconditionally including , which is a Kineto header, causing a fatal compile error when building with USE_KINETO=OFF and Kineto is not installed (e.g. on Gentoo with system libraries). + +Guard the include with #ifdef USE_KINETO and provide a minimal stub (enum class ActivityType : uint8_t { NONE = 0 }) so the data structures and function signatures that reference libkineto::ActivityType still compile. Guard the corresponding function bodies in kineto_shim.cpp (addCPUActivity, +deviceTypeFromActivity) and collection.cpp (scopeToType, kinetoType) that use concrete enum values, adding no-op stubs for the non-Kineto paths.Fixes the bug #178939 +--- + torch/csrc/profiler/collection.cpp | 8 ++++++++ + torch/csrc/profiler/kineto_shim.cpp | 16 ++++++++++++++-- + torch/csrc/profiler/kineto_shim.h | 7 +++++++ + 3 files changed, 29 insertions(+), 2 deletions(-) + +diff --git a/torch/csrc/profiler/collection.cpp b/torch/csrc/profiler/collection.cpp +index 1be2d80310910..e8054d4f44df7 100644 +--- a/torch/csrc/profiler/collection.cpp ++++ b/torch/csrc/profiler/collection.cpp +@@ -646,11 +646,13 @@ std::string toString(const ExtraFields& e) { + e.callsite_.funcname_.str()); + } + ++#ifdef USE_KINETO + auto scopeToType(at::RecordScope scope) { + return scope == at::RecordScope::USER_SCOPE + ? libkineto::ActivityType::USER_ANNOTATION + : libkineto::ActivityType::CPU_OP; + } ++#endif + + int64_t torchOpEndNS( + const ExtraFields& e, +@@ -699,6 +701,7 @@ std::string Result::overload_name() const { + [](const auto& e) -> std::string { return ""; })); + } + ++#ifdef USE_KINETO + libkineto::ActivityType Result::kinetoType() const { + return visit(c10::overloaded( + ATTRIBUTE(TorchOp, scopeToType(e.scope_)), +@@ -711,6 +714,11 @@ libkineto::ActivityType Result::kinetoType() const { + ATTRIBUTE(PythonGC, libkineto::ActivityType::PYTHON_FUNCTION), + ATTRIBUTE(Kineto, e.activity_type_))); + } ++#else ++libkineto::ActivityType Result::kinetoType() const { ++ return libkineto::ActivityType::NONE; ++} ++#endif + + uint64_t Result::correlationID() const { + return visit(c10::overloaded( +diff --git a/torch/csrc/profiler/kineto_shim.cpp b/torch/csrc/profiler/kineto_shim.cpp +index fa232e1a01016..0ddf62997a07e 100644 +--- a/torch/csrc/profiler/kineto_shim.cpp ++++ b/torch/csrc/profiler/kineto_shim.cpp +@@ -196,6 +196,7 @@ TraceWrapper::TraceWrapper(const int64_t start_time, const std::string& name) + } + #endif // USE_KINETO + ++#ifdef USE_KINETO + activity_t* TraceWrapper::addCPUActivity( + const std::string& name, + const libkineto::ActivityType type, +@@ -203,7 +204,6 @@ activity_t* TraceWrapper::addCPUActivity( + const uint64_t correlation_id, + const int64_t start_time, + const int64_t end_time) { +-#ifdef USE_KINETO + TORCH_CHECK((bool)(*this), "Cannot add event to non-existent trace."); + cpu_trace_->emplace_activity(cpu_trace_->span, type, name); + auto& act = libkineto::CpuTraceBuffer::toRef(cpu_trace_->activities.back()); +@@ -215,10 +215,18 @@ activity_t* TraceWrapper::addCPUActivity( + act.endTime = end_time; + } + return cpu_trace_->activities.back().get(); ++} + #else ++activity_t* TraceWrapper::addCPUActivity( ++ const std::string& name, ++ const libkineto::ActivityType type, ++ const DeviceAndResource device_and_resource, ++ const uint64_t correlation_id, ++ const int64_t start_time, ++ const int64_t end_time) { + return nullptr; +-#endif // USE_KINETO + } ++#endif // USE_KINETO + + void TraceWrapper::transferCpuTrace(int64_t end_time) { + #ifdef USE_KINETO +@@ -498,6 +506,7 @@ void logInvariantViolation( + + namespace autograd::profiler { + c10::DeviceType deviceTypeFromActivity(libkineto::ActivityType activity_type) { ++#ifdef USE_KINETO + // PrivateUse1 kineto backend reuse some ActivityTypes, + // If PrivateUse1 backend is enabled, this should return + // c10::DeviceType::PrivateUse1. +@@ -551,6 +560,9 @@ c10::DeviceType deviceTypeFromActivity(libkineto::ActivityType activity_type) { + return c10::DeviceType::CPU; + } + } ++#else ++ return c10::DeviceType::CPU; ++#endif // USE_KINETO + } + + void addMetadataJson(const std::string& key, const std::string& value) { +diff --git a/torch/csrc/profiler/kineto_shim.h b/torch/csrc/profiler/kineto_shim.h +index 4f9bdc6770507..44bcb0e18a6e0 100644 +--- a/torch/csrc/profiler/kineto_shim.h ++++ b/torch/csrc/profiler/kineto_shim.h +@@ -12,7 +12,14 @@ + #undef USE_KINETO + #endif + ++#ifdef USE_KINETO + #include ++#else ++// Minimal stub so non-Kineto builds can compile types that hold ActivityType. ++namespace libkineto { ++enum class ActivityType : uint8_t { NONE = 0 }; ++} // namespace libkineto ++#endif + + #include + #include + +From f5e3a9d9aa12c6e2006a9243c97ef7703fcbbe2f Mon Sep 17 00:00:00 2001 +From: Samaresh Kumar Singh +Date: Wed, 1 Apr 2026 10:34:24 -0500 +Subject: [PATCH 2/3] Fixed the USER_ANNOTATION/GPU_USER_ANNOTATION build + errors with external kineto + +When building against a system-installed kineto (e.g. Gentoo's sci-ml/kineto) that lacks USER_ANNOTATION and GPU_USER_ANNOTATION in its ActivityType enum, +init.cpp failed to compile because it referenced those enum member names directly. +--- + torch/csrc/autograd/init.cpp | 7 +------ + torch/csrc/autograd/profiler_kineto.cpp | 7 +++++++ + torch/csrc/autograd/profiler_kineto.h | 1 + + torch/csrc/profiler/kineto_shim.h | 11 +++++++++-- + 4 files changed, 18 insertions(+), 8 deletions(-) + +diff --git a/torch/csrc/autograd/init.cpp b/torch/csrc/autograd/init.cpp +index 1e2442a5d99db..f8e8795c5805e 100644 +--- a/torch/csrc/autograd/init.cpp ++++ b/torch/csrc/autograd/init.cpp +@@ -310,12 +310,7 @@ PyObject* THPAutograd_initExtension(PyObject* _unused, PyObject* unused) { + .def("privateuse1_elapsed_us", &KinetoEvent::privateuse1ElapsedUs) + .def( + "is_user_annotation", +- [](const KinetoEvent& e) { +- return e.activityType() == +- (uint8_t)libkineto::ActivityType::USER_ANNOTATION || +- e.activityType() == +- (uint8_t)libkineto::ActivityType::GPU_USER_ANNOTATION; +- }) ++ [](const KinetoEvent& e) { return e.isUserAnnotation(); }) + .def( + "is_python_function", + [](const KinetoEvent& e) { return e.isPythonFunction(); }) +diff --git a/torch/csrc/autograd/profiler_kineto.cpp b/torch/csrc/autograd/profiler_kineto.cpp +index 9a076f58d7143..6b5165f0872fb 100644 +--- a/torch/csrc/autograd/profiler_kineto.cpp ++++ b/torch/csrc/autograd/profiler_kineto.cpp +@@ -1165,6 +1165,13 @@ + &privateuse1_event_start, &privateuse1_event_end); + } + ++bool KinetoEvent::isUserAnnotation() const { ++ constexpr uint8_t kUserAnnotation = 1; ++ constexpr uint8_t kGpuUserAnnotation = 2; ++ const auto type = activityType(); ++ return type == kUserAnnotation || type == kGpuUserAnnotation; ++} ++ + void KinetoEvent::getPerfEventCounters(std::vector& in) const { + return result_->visit(c10::overloaded( + [&in](const ExtraFields& e) -> void { +diff --git a/torch/csrc/autograd/profiler_kineto.h b/torch/csrc/autograd/profiler_kineto.h +index 777b8a5851ed5..c3135c61245d4 100644 +--- a/torch/csrc/autograd/profiler_kineto.h ++++ b/torch/csrc/autograd/profiler_kineto.h +@@ -72,6 +72,7 @@ struct TORCH_API KinetoEvent { + bool isPythonFunction() const; + int64_t cudaElapsedUs() const; + int64_t privateuse1ElapsedUs() const; ++ bool isUserAnnotation() const; + void getPerfEventCounters(torch::profiler::perf_counters_t& /*in*/) const; + extra_meta_t extraMeta() const; + const typed_metadata_t& typedMetadata() const; +diff --git a/torch/csrc/profiler/kineto_shim.h b/torch/csrc/profiler/kineto_shim.h +index 44bcb0e18a6e0..ddc00c03fa9cc 100644 +--- a/torch/csrc/profiler/kineto_shim.h ++++ b/torch/csrc/profiler/kineto_shim.h +@@ -15,9 +15,16 @@ + #ifdef USE_KINETO + #include + #else +-// Minimal stub so non-Kineto builds can compile types that hold ActivityType. + namespace libkineto { +-enum class ActivityType : uint8_t { NONE = 0 }; ++enum class ActivityType : uint8_t { ++ CPU_OP = 0, ++ USER_ANNOTATION, ++ GPU_USER_ANNOTATION, ++ NONE = CPU_OP, ++}; ++inline const char* toString(ActivityType) { ++ return "CPU_OP"; ++} + } // namespace libkineto + #endif + + +From 717526b7ce90d242cb917db90f4cb13c85c93661 Mon Sep 17 00:00:00 2001 +From: Samaresh Kumar Singh +Date: Fri, 3 Apr 2026 13:01:01 -0500 +Subject: [PATCH 3/3] profiler: guard GPU ActivityType checks with KINETO GPU + backend macros + +The externalId() function referenced GPU-specific libkineto::ActivityType +values (GPU_MEMCPY, GPU_MEMSET, CONCURRENT_KERNEL, CUDA_RUNTIME, +CUDA_DRIVER, PRIVATEUSE1_RUNTIME, PRIVATEUSE1_DRIVER) that are only +present in kineto builds with GPU backend support. + +System-installed kineto packages built without CUDA/ROCm support +(LIBKINETO_NOCUPTI + LIBKINETO_NOROCTRACER) omit these enum values, +causing compile errors. Guard the check with the existing GPU backend +macros. Also restructures the condition to an early-return to avoid +duplicating the correlation ID lookup. +--- + torch/csrc/autograd/profiler_kineto.cpp | 38 ++++++++++++++----------- + 1 file changed, 22 insertions(+), 16 deletions(-) + +diff --git a/torch/csrc/autograd/profiler_kineto.cpp b/torch/csrc/autograd/profiler_kineto.cpp +index 6b5165f0872fb..466e8d66ab518 100644 +--- a/torch/csrc/autograd/profiler_kineto.cpp ++++ b/torch/csrc/autograd/profiler_kineto.cpp +@@ -1209,18 +1209,25 @@ + return static_cast(linked); + } + ++#if defined(USE_KINETO) && \ ++ (!defined(LIBKINETO_NOCUPTI) || !defined(LIBKINETO_NOROCTRACER)) + // Orphaned GPU activities (no linked CPU op) in these types should not get + // an External id, to avoid incorrect cross-linking in trace viewers. ++ // These GPU-specific ActivityType values are only present when kineto is ++ // built with GPU backend support (CUPTI or ROCtracer). CPU-only kineto ++ // builds (e.g. system packages without GPU support) omit them. + auto type = static_cast(activityType()); +- if (type != libkineto::ActivityType::GPU_MEMCPY && +- type != libkineto::ActivityType::GPU_MEMSET && +- type != libkineto::ActivityType::CONCURRENT_KERNEL && +- type != libkineto::ActivityType::CUDA_RUNTIME && +- type != libkineto::ActivityType::CUDA_DRIVER && +- type != libkineto::ActivityType::XPU_RUNTIME && +- type != libkineto::ActivityType::XPU_DRIVER && +- type != libkineto::ActivityType::PRIVATEUSE1_RUNTIME && +- type != libkineto::ActivityType::PRIVATEUSE1_DRIVER) { ++ if (type == libkineto::ActivityType::GPU_MEMCPY || ++ type == libkineto::ActivityType::GPU_MEMSET || ++ type == libkineto::ActivityType::CONCURRENT_KERNEL || ++ type == libkineto::ActivityType::CUDA_RUNTIME || ++ type == libkineto::ActivityType::CUDA_DRIVER || ++ type == libkineto::ActivityType::PRIVATEUSE1_RUNTIME || ++ type == libkineto::ActivityType::PRIVATEUSE1_DRIVER) { ++ return 0; ++ } ++#endif ++ + return static_cast(result_->visit(c10::overloaded( + [](const ExtraFields& e) -> uint64_t { + return e.correlation_id_; +@@ -1229,9 +1236,6 @@ + return e.correlation_id_; + }, + [](const auto&) -> uint64_t { return 0; }))); +- } +- +- return 0; + } + + #define FORWARD_FROM_RESULT(method_name, result_expr) \ diff --git a/sci-ml/pytorch/files/pytorch-2.14.0-unbundle_pocketfft.patch b/sci-ml/pytorch/files/pytorch-2.14.0-unbundle_pocketfft.patch new file mode 100644 index 0000000000000..505ffe7940a3c --- /dev/null +++ b/sci-ml/pytorch/files/pytorch-2.14.0-unbundle_pocketfft.patch @@ -0,0 +1,18 @@ +--- a/cmake/Dependencies.cmake ++++ b/cmake/Dependencies.cmake +@@ -276,15 +276,8 @@ + # --- [ PocketFFT + set(AT_POCKETFFT_ENABLED 0) + if(NOT AT_MKL_ENABLED) +- set(POCKETFFT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/third_party/pocketfft/") +- if(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}") +- message(FATAL_ERROR "pocketfft directory not found, expected ${POCKETFFT_INCLUDE_DIR}") +- elseif(NOT EXISTS "${POCKETFFT_INCLUDE_DIR}/pocketfft_hdronly.h") +- message(FATAL_ERROR "pocketfft headers not found in ${POCKETFFT_INCLUDE_DIR}") +- endif() + + set(AT_POCKETFFT_ENABLED 1) +- message(STATUS "Using pocketfft in directory: ${POCKETFFT_INCLUDE_DIR}") + endif() + + # ---[ Dependencies diff --git a/sci-ml/pytorch/pytorch-2.14.0.ebuild b/sci-ml/pytorch/pytorch-2.14.0.ebuild new file mode 100644 index 0000000000000..e124aae30161b --- /dev/null +++ b/sci-ml/pytorch/pytorch-2.14.0.ebuild @@ -0,0 +1,365 @@ +# Copyright 2022-2026 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +EAPI=8 + +DISTUTILS_USE_PEP517=scikit-build-core +PYTHON_COMPAT=( python3_{12..14} ) +DISTUTILS_SINGLE_IMPL=1 +DISTUTILS_EXT=1 + +ROCM_VERSION=6.1 +inherit distutils-r1 prefix cuda flag-o-matic rocm multiprocessing + +# pytorch-2.13.0 depends on specific commit of composable kernel +# TODO: replace it with DEPEND in the future +CK_COMMIT=f1746955fdaf80a3414de814bf32437686dac347 +CK_P=composable_kernel-${CK_COMMIT:0:8} + +# Starting from 2.7.0 pytorch moved flash attention out-of-tree, +# but hardcoded it as third_party subproject +# TODO: unbundle +FLASH_PV=2.7.4 +FLASH_PN=flash-attention +FLASH_P=${FLASH_PN}-${FLASH_PV} +FLASH_ATT_URI="https://github.com/Dao-AILab/${FLASH_PN}/archive/refs/tags/v${FLASH_PV}.tar.gz -> ${FLASH_P}.gh.tar.gz" + +DESCRIPTION="Tensors and Dynamic neural networks in Python with strong GPU acceleration" +HOMEPAGE="https://pytorch.org/" +SRC_URI="https://github.com/pytorch/${PN}/archive/refs/tags/v${PV}.tar.gz + -> ${P}.tar.gz + rocm? ( + https://github.com/ROCm/composable_kernel/archive/${CK_COMMIT}.tar.gz + -> ${CK_P}.tar.gz + ) + cuda? ( + flash? ( ${FLASH_ATT_URI} ) + memefficient? ( ${FLASH_ATT_URI} ) + ) +" + +LICENSE="BSD" +SLOT="0" +KEYWORDS="~amd64 ~arm64" +IUSE="cuda cusparselt distributed fbgemm flash gloo kineto memefficient + mimalloc mkl mpi nccl nnpack +numpy onednn openblas opencl openmp qnnpack + rocm xnnpack" +RESTRICT="test" + +REQUIRED_USE=" + ${PYTHON_REQUIRED_USE} + mpi? ( distributed ) + gloo? ( distributed ) + ?? ( cuda rocm ) + rocm? ( + || ( ${ROCM_REQUIRED_USE} ) + memefficient? ( flash ) + ) + cusparselt? ( || ( cuda rocm ) ) + flash? ( || ( cuda rocm ) ) + memefficient? ( || ( cuda rocm ) ) + nccl? ( rocm ) +" + +RDEPEND=" + ${PYTHON_DEPS} + !sci-ml/caffe2 + dev-cpp/abseil-cpp:= + dev-cpp/gflags:= + >=dev-cpp/glog-0.6.0:= + >=dev-libs/cpuinfo-2025.11.14 + dev-libs/libfmt:= + dev-libs/protobuf:= + dev-libs/sleef + sci-ml/onnx + $(python_gen_cond_dep ' + dev-python/sympy[${PYTHON_USEDEP}] + dev-python/typing-extensions[${PYTHON_USEDEP}] + ') + !mkl? ( !openblas? ( virtual/blas ) ) + cuda? ( + dev-libs/cudnn + >=sci-ml/cudnn-frontend-1.12.0:= + >=dev-util/nvidia-cuda-toolkit-12.9:=[profiler] + cusparselt? ( dev-libs/cusparselt ) + ) + distributed? ( + !rocm? ( sci-ml/tensorpipe[cuda?] ) + dev-cpp/cpp-httplib:= + ) + fbgemm? ( >=sci-ml/FBGEMM-1.4 ) + gloo? ( >=sci-ml/gloo-2025.06.04[cuda?,rocm?] ) + kineto? ( ~sci-ml/kineto-0.4.0_p20260603 ) + mimalloc? ( dev-libs/mimalloc ) + mkl? ( sci-libs/mkl ) + mpi? ( virtual/mpi ) + nnpack? ( + sci-ml/NNPACK + dev-libs/pthreadpool + ) + numpy? ( $(python_gen_cond_dep ' + dev-python/numpy[${PYTHON_USEDEP}] + ') ) + onednn? ( sci-ml/oneDNN ) + openblas? ( sci-libs/openblas ) + opencl? ( virtual/opencl ) + qnnpack? ( + !sci-libs/QNNPACK + sci-ml/gemmlowp + dev-libs/pthreadpool + ) + rocm? ( + nccl? ( >=dev-libs/rccl-6.3:= =dev-util/hip-6.3:= =dev-util/roctracer-6.3:= =sci-libs/hipBLAS-6.3:= =sci-libs/hipBLASLt-6.3:= =sci-libs/hipFFT-6.3:= =sci-libs/hipRAND-6.3:= =sci-libs/hipSOLVER-6.3:= =sci-libs/hipSPARSE-6.3:= =sci-libs/miopen-6.3:= =sci-libs/rocBLAS-6.3:= =sci-libs/rocRAND-6.3:= =sci-libs/rocSOLVER-6.3:= =dev-util/rocm-smi-6.3:= =dev-util/amdsmi-6.3:= =sci-libs/hipsparselt-6.3:= =sci-ml/XNNPACK-2024.11 + dev-libs/pthreadpool + ) +" + +DEPEND=" + ${RDEPEND} + dev-cpp/nlohmann_json + dev-libs/flatbuffers + dev-libs/FXdiv + dev-libs/pocketfft + dev-libs/psimd + sci-ml/FP16 + $(python_gen_cond_dep ' + =sci-libs/hipCUB-6.3:= =sci-libs/rocPRIM-6.3:= =sci-libs/rocThrust-6.3:= /dev/null || die + flatc --cpp --gen-mutable --scoped-enums mobile_bytecode.fbs || die + popd > /dev/null || die + + # prefixify the hardcoded paths, after all patches are applied + hprefixify \ + aten/CMakeLists.txt \ + caffe2/CMakeLists.txt \ + cmake/Metal.cmake \ + cmake/Modules/*.cmake \ + cmake/Modules_CUDA_fix/FindCUDNN.cmake \ + cmake/Modules_CUDA_fix/upstream/FindCUDA/make2cmake.cmake \ + cmake/Modules_CUDA_fix/upstream/FindPackageHandleStandardArgs.cmake \ + cmake/public/LoadHIP.cmake \ + cmake/public/cuda.cmake \ + cmake/Dependencies.cmake \ + tools/setup_helpers/env.py \ + torch/CMakeLists.txt \ + CMakeLists.txt + + if use rocm; then + # TODO: delete, when caffe2 depends on systemwide composable_kernel + sed -e "s:third_party/composable_kernel:../composable_kernel-${CK_COMMIT}:g" \ + -i aten/src/ATen/CMakeLists.txt || die + + # Workaround for libc++ issue https://github.com/llvm/llvm-project/issues/100802 + sed -e 's/std::memcpy/memcpy/g' \ + -i torch/headeronly/util/Half.h \ + -i aten/src/ATen/native/cuda/int4mm.cu || die + + ebegin "HIPifying cuda sources" + FBCODE_BUILD_TOOL="buck" ${EPYTHON} tools/amd_build/build_amd.py || die + eend $? + fi +} + +src_configure() { + if use cuda && [[ -z ${TORCH_CUDA_ARCH_LIST} ]]; then + ewarn "WARNING: pytorch is being built with its default CUDA compute capabilities: 3.5 and 7.0." + ewarn "These may not be optimal for your GPU." + ewarn "" + ewarn "To configure pytorch with the CUDA compute capability that is optimal for your GPU," + ewarn "set TORCH_CUDA_ARCH_LIST in your make.conf, and re-emerge pytorch." + ewarn "For example, to use CUDA capability 7.5 & 3.5, add: TORCH_CUDA_ARCH_LIST=7.5 3.5" + ewarn "For a Maxwell model GPU, an example value would be: TORCH_CUDA_ARCH_LIST=Maxwell" + ewarn "" + ewarn "You can look up your GPU's CUDA compute capability at https://developer.nvidia.com/cuda-gpus" + ewarn "or by running /opt/cuda/extras/demo_suite/deviceQuery | grep 'CUDA Capability'" + fi +} + +python_compile() { + local -x BUILD_TEST=OFF + local -x CMAKE_BUILD_DIR="${BUILD_DIR}" + local -x MAX_JOBS=$(makeopts_jobs) + local -x PYTORCH_BUILD_VERSION=${PV} + local -x PYTORCH_BUILD_NUMBER=0 + local -x USE_CCACHE=OFF + local -x USE_CUDA=$(usex cuda) + local -x USE_DISTRIBUTED=$(usex distributed) + local -x USE_FBGEMM=$(usex fbgemm) + local -x USE_FLASH_ATTENTION=$(usex flash) + local -x USE_GFLAGS=ON + local -x USE_GLOG=ON + local -x USE_GLOO=$(usex gloo) + local -x USE_ITT=OFF + local -x USE_KINETO=$(usex kineto) + local -x USE_KLEIDIAI=OFF # TODO + local -x USE_MAGMA=OFF # TODO: In GURU as sci-libs/magma + local -x USE_MEM_EFF_ATTENTION=$(usex memefficient) + local -x USE_MIMALLOC=$(usex mimalloc) + local -x USE_MKLDNN=$(usex onednn) + local -x USE_MPI=$(usex mpi) + local -x USE_NNPACK=$(usex nnpack) + local -x USE_NUMA=OFF + local -x USE_NUMPY=$(usex numpy) + local -x USE_OPENCL=$(usex opencl) + local -x USE_OPENMP=$(usex openmp) + local -x USE_PYTORCH_QNNPACK=$(usex qnnpack) + local -x USE_PYTORCH_METAL=OFF + local -x USE_ROCM=$(usex rocm) + local -x USE_SYSTEM_LIBS=ON + local -x USE_SYSTEM_XNNPACK=$(usex xnnpack) + local -x USE_TENSORPIPE=$(usex distributed $(usex !rocm)) + local -x DUSE_UCC=OFF + local -x USE_VALGRIND=OFF + local -x USE_XNNPACK=$(usex xnnpack) + local -x USE_XPU=OFF + + if use mkl; then + local -x USE_BLAS=ON + local -x BLAS=MKL + elif use openblas; then + local -x USE_BLAS=ON + local -x BLAS=OpenBLAS + else + # Note: USE_BLAS=OFF does not properly work, instead pytorch still searches for libraries. + # Even though it is easy to patch pytorch to disable BLAS usage completely, + # it is practically not useful, as it would render the library barely usable. + local -x USE_BLAS=ON + local -x BLAS=Generic + local -x GENERIC_BLAS_LIBRARIES=cblas + fi + + if use cuda; then + # bug 867706 926116 + cuda_add_sandbox + addpredict "/dev/char/" + + local -x CMAKE_CUDA_FLAGS="$(cuda_gccdir -f | tr -d \")" + local -x TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST:-7.0}" + local -x USE_CUDNN=ON + local -x USE_FLASH_ATTENTION=OFF + local -x USE_MEM_EFF_ATTENTION=OFF + local -x USE_NCCL=OFF # TODO: NVIDIA Collective Communication Library + + elif use rocm; then + export PYTORCH_ROCM_ARCH="$(get_amdgpu_flags)" + + if use memefficient; then + export AOTRITON_INSTALLED_PREFIX="${ESYSROOT}/usr" + fi + + local -x CMAKE_REQUIRE_FIND_PACKAGE_HIP=ON + local -x USE_NCCL=$(usex nccl) + local -x CMAKE_DISABLE_FIND_PACKAGE_hipsparselt=$(usex !cusparselt) # disable automagic + local -x USE_ROCM_CK_SDPA=OFF # requires flash + aiter, works only on gfx90a/gfx942/gfx950 + local -x ROCM_PATH=/usr + local -x HIP_CLANG_PATH=$(hipconfig --hipclangpath) + + # ROCm libraries produce too much warnings + append-cxxflags -Wno-deprecated-declarations -Wno-unused-result -Wno-unused-value + else + local -x USE_NCCL=OFF + fi + + distutils-r1_python_compile develop sdist +}