Install this version:
emerge -a =sci-ml/caffe2-cuda-2.15.0
If this version is masked, you can unmask it using the autounmask tool or standard emerge options:
autounmask =sci-ml/caffe2-cuda-2.15.0
Or alternatively:
emerge --autounmask-write -a =sci-ml/caffe2-cuda-2.15.0
| Version | EAPI | Keywords | Slot |
|---|---|---|---|
| 2.15.0 | 8 | ~amd64 | 0 |
# Copyright 2022-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2
# CUDA implementation of caffe2/PyTorch backend
EAPI=8
PYTHON_COMPAT=( python3_{12..15} )
inherit python-r1 cmake cuda flag-o-matic prefix
MY_PN=caffe2
MYPN=pytorch
MYP=${MYPN}-${PV}
# pytorch 2.15.0 pins flash-attention v2.7.4 (submodule)
FLASH_PV=2.7.4
FLASH_TAG=v${FLASH_PV}
FLASH_PN=flash-attention
FLASH_P=${FLASH_PN}-${FLASH_PV}
FLASH_ATT_URI="https://github.com/Dao-AILab/${FLASH_PN}/archive/refs/tags/${FLASH_TAG}.tar.gz -> ${FLASH_P}.gh.tar.gz"
DESCRIPTION="A deep learning framework (CUDA backend)"
HOMEPAGE="https://pytorch.org/"
SRC_URI="
https://github.com/pytorch/${MYPN}/archive/refs/tags/v${PV}.tar.gz -> ${MYP}.tar.gz
flash? ( ${FLASH_ATT_URI} )
memefficient? ( ${FLASH_ATT_URI} )
"
S="${WORKDIR}"/${MYP}
LICENSE="BSD"
SLOT="0"
KEYWORDS="~amd64"
IUSE="cusparselt distributed fbgemm flash gloo kineto memefficient
mimalloc mkl mpi nnpack +numpy onednn openblas openmp qnnpack
+xnnpack"
RESTRICT="test"
REQUIRED_USE="
${PYTHON_REQUIRED_USE}
mpi? ( distributed )
gloo? ( distributed )
memefficient? ( flash )
"
RDEPEND="
${PYTHON_DEPS}
app-eselect/eselect-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
virtual/lapack
dev-libs/cudnn
>=sci-ml/cudnn-frontend-1.12.0:=
>=dev-util/nvidia-cuda-toolkit-12.9:=[profiler]
cusparselt? ( dev-libs/cusparselt )
fbgemm? ( >=sci-ml/FBGEMM-1.4 )
gloo? ( >=sci-ml/gloo-2025.06.04[cuda] )
kineto? ( ~sci-ml/kineto-0.4.0_p20260805 )
mimalloc? ( dev-libs/mimalloc )
mpi? ( virtual/mpi )
nnpack? ( sci-ml/NNPACK dev-libs/pthreadpool )
numpy? ( $(python_gen_cond_dep 'dev-python/numpy[${PYTHON_USEDEP}]') )
onednn? ( sci-ml/oneDNN )
qnnpack? ( !sci-libs/QNNPACK sci-ml/gemmlowp dev-libs/pthreadpool )
distributed? ( sci-ml/tensorpipe[cuda] dev-cpp/cpp-httplib:= )
xnnpack? ( >=sci-ml/XNNPACK-2024.11 dev-libs/pthreadpool )
mkl? ( sci-libs/mkl )
openblas? ( sci-libs/openblas )
"
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 '
dev-python/pybind11[${PYTHON_USEDEP}]
dev-python/pyyaml[${PYTHON_USEDEP}]
dev-python/typing-extensions[${PYTHON_USEDEP}]
')
~dev-libs/cutlass-4.6.1[tools(+)]
onednn? ( sci-ml/ideep )
qnnpack? ( dev-libs/clog )
"
BDEPEND="
dev-build/cmake
dev-util/patchelf
"
PATCHES=(
"${FILESDIR}"/${P}-unbundle_kineto.patch
"${FILESDIR}"/${P}-unbundle_httplib.patch
"${FILESDIR}"/${P}-removekineto.patch
"${FILESDIR}"/${P}-aotriton-fixes.patch
"${FILESDIR}"/${P}-glog.patch
"${FILESDIR}"/${P}-unbundle_pocketfft.patch
"${FILESDIR}"/${P}-mimalloc.patch
"${FILESDIR}"/${P}-unbundle_fbgemm.patch
"${FILESDIR}"/${P}-unbundle_fxdiv.patch
"${FILESDIR}"/${P}-xnnpack.patch
"${FILESDIR}"/${P}-license.patch
"${FILESDIR}"/${P}-cpp-httplib.patch
)
CAFFE2_PREFIX="/usr/lib/caffe2/cuda"
PYTORCH_PREFIX="/usr/lib/pytorch/cuda"
pkg_setup() {
python_setup
}
src_prepare() {
if use flash || use memefficient; then
mv "${WORKDIR}"/${FLASH_P}/* third_party/${FLASH_PN}/ || die
fi
filter-lto
# cpp-extension multilib fix
eapply "${FILESDIR}/${P}-cpp-extension-multilib.patch"
sed -e "s|%LIB_DIR%|$(get_libdir)|g" \
-i torch/utils/cpp_extension.py || die
# Add cutlass examples symlink
ln -sf /usr/share/cutlass/examples third_party/cutlass/examples || die
# cudnn_frontend unbundled
mkdir -p third_party/cudnn_frontend || die
ln -sf /usr/include third_party/cudnn_frontend/include || die
# Unbundle fmt — inject find_package so cmake discovers system fmt::fmt,
# then rewrite bundled header-only references to the system target
sed -i '1i find_package(fmt REQUIRED)' cmake/Dependencies.cmake || die
find . \( -name CMakeLists.txt -o -name '*.cmake' \) | xargs sed -i \
-e 's|fmt::fmt-header-only|fmt::fmt|g' \
-e '/set_target_properties(fmt-header-only/d' \
-e '/target_compile_definitions(fmt-header-only/d' \
-e '/add_library(fmt-header-only/d' \
-e '/_fmt_no_unique_address/d' \
|| die
sed -e '/target_compile_options_if_supported(tensorpipe/d' -i cmake/Dependencies.cmake || die
# pytorch 2.13.0: CuTeDSL is prototype, don't fail if cutlass submodule missing
sed -i 's/FATAL_ERROR/WARNING/' cmake/FileMirroring.cmake || die
# Remove wrap_headers install step — install(CODE) doesn't prepend DESTDIR,
# causing sandbox violations on re-emerge. The header guards are cosmetic
# (TORCH_STABLE_ONLY compile warnings), not needed for system packages.
sed -i '/# --- Header wrapping/,/^")$/d' cmake/PostBuildSteps.cmake || die
sed -i \
-e '/add_subdirectory.*third_party/d' \
CMakeLists.txt cmake/Dependencies.cmake cmake/ProtoBuf.cmake \
aten/src/ATen/CMakeLists.txt || die
sed -i \
-e "/EXPORT/s|DESTINATION lib)|DESTINATION $(get_libdir))|" \
c10/cuda/CMakeLists.txt c10/CMakeLists.txt || die
sed -i \
-e "s|}/lib|}/\${CMAKE_INSTALL_LIBDIR}|g" \
-e "/set(__AOTRITON_LIB/s|lib/|\${CMAKE_INSTALL_LIBDIR}/|g" \
cmake/External/aotriton.cmake || die
sed -i 's/-Wextra-semi//' cmake/public/utils.cmake || die
cmake_src_prepare
pushd torch/csrc/jit/serialization > /dev/null || die
flatc --cpp --gen-mutable --scoped-enums mobile_bytecode.fbs || die
popd > /dev/null || die
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/cuda.cmake cmake/Dependencies.cmake \
torch/CMakeLists.txt CMakeLists.txt
}
src_configure() {
# Convert CUDA_TARGETS (sm_XX) to TORCH_CUDA_ARCH_LIST (X.X)
if [[ -z "${TORCH_CUDA_ARCH_LIST}" ]]; then
local arch_list=""
local target
for target in ${CUDA_TARGETS}; do
local ver="${target#sm_}"
arch_list+="${ver%?}.${ver: -1} "
done
TORCH_CUDA_ARCH_LIST="${arch_list% }"
fi
# Build against lowest enabled Python from PYTHON_TARGETS
local _pyversions=()
local _impl _pv
for _impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
use "python_targets_${_impl}" || continue
_pv="${_impl#python}"
_pv="${_pv/_/.}"
_pyversions+=("${_pv}")
done
_pyversions=($(printf '%s\n' "${_pyversions[@]}" | sort -V))
[[ ${#_pyversions[@]} -eq 0 ]] && die "No Python targets enabled"
local _python_exe="/usr/bin/python${_pyversions[0]}"
local mycmakeargs=(
-DCMAKE_INSTALL_PREFIX="${EPREFIX}${CAFFE2_PREFIX}"
-DCMAKE_INSTALL_RPATH="${EPREFIX}${CAFFE2_PREFIX}/$(get_libdir)"
-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
-DUSE_SYSTEM_LIBS=ON
-DBUILD_CUSTOM_PROTOBUF=OFF
-DBUILD_TEST=OFF
-DLIBSHM_INSTALL_LIB_SUBDIR="${EPREFIX}${CAFFE2_PREFIX}/$(get_libdir)"
-DPython_EXECUTABLE="${_python_exe}"
-DTORCH_INSTALL_LIB_DIR="${EPREFIX}${CAFFE2_PREFIX}/$(get_libdir)"
-DUSE_CCACHE=OFF
-DUSE_CUDA=ON
-DUSE_ROCM=OFF
-DUSE_DISTRIBUTED=$(usex distributed)
-DUSE_FBGEMM=$(usex fbgemm)
-DUSE_FLASH_ATTENTION=$(usex flash)
-DUSE_GFLAGS=ON
-DUSE_GLOG=ON
-DUSE_GLOO=$(usex gloo)
-DUSE_ITT=OFF
-DUSE_KINETO=$(usex kineto)
-DUSE_KLEIDIAI=OFF
-DUSE_MAGMA=OFF
-DUSE_MEM_EFF_ATTENTION=$(usex memefficient)
-DUSE_MIMALLOC=$(usex mimalloc)
-DUSE_MKLDNN=$(usex onednn)
-DUSE_MPI=$(usex mpi)
-DUSE_NCCL=OFF
-DUSE_NNPACK=$(usex nnpack)
-DUSE_NUMA=OFF
-DUSE_NUMPY=$(usex numpy)
-DUSE_OPENMP=$(usex openmp)
-DUSE_PYTORCH_QNNPACK=$(usex qnnpack)
-DUSE_PYTORCH_METAL=OFF
-DUSE_SYSTEM_CPUINFO=ON
-DUSE_SYSTEM_EIGEN_INSTALL=ON
-DUSE_SYSTEM_FP16=ON
-DUSE_SYSTEM_FXDIV=ON
-DUSE_SYSTEM_GLOO=ON
-DUSE_SYSTEM_NVTX=ON
-DUSE_SYSTEM_ONNX=ON
-DUSE_SYSTEM_PSIMD=ON
-DUSE_SYSTEM_PTHREADPOOL=ON
-DUSE_SYSTEM_PYBIND11=ON
-DUSE_SYSTEM_SLEEF=ON
-DUSE_SYSTEM_XNNPACK=$(usex xnnpack)
$(usex xnnpack '' '-DCMAKE_DISABLE_FIND_PACKAGE_XNNPACK=ON')
-DUSE_TENSORPIPE=$(usex distributed)
-DUSE_UCC=OFF
-DUSE_VALGRIND=OFF
-DUSE_XNNPACK=$(usex xnnpack)
-DUSE_XPU=OFF
-DUSE_CUDNN=ON
-DTORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST}"
-DUSE_CUSPARSELT=$(usex cusparselt)
-Wno-dev
)
if use mkl; then
mycmakeargs+=(-DBLAS=MKL)
elif use openblas; then
mycmakeargs+=(-DBLAS=OpenBLAS)
else
mycmakeargs+=(-DBLAS=Generic -DBLAS_LIBRARIES=)
fi
cuda_add_sandbox
addpredict "/dev/char/"
mycmakeargs+=( -DCMAKE_CUDA_FLAGS="$(cuda_gccdir -f | tr -d \")" )
[[ -v CUDACXX ]] && export PYTORCH_NVCC="${CUDACXX}"
if use flash; then
export FLASH_ATTENTION_FORCE_BUILD="TRUE"
export FLASH_ATTN_CUDA_ARCHS="${CUDAARCHS:-${TORCH_CUDA_ARCH_LIST}}"
fi
if use onednn; then
mycmakeargs+=(
-DMKLDNN_FOUND=ON -DMKLDNN_LIBRARIES=dnnl
-DMKLDNN_INCLUDE_DIR="${ESYSROOT}/usr/include/oneapi/dnnl"
)
fi
cmake_src_configure
}
_rebuild_torch_python() {
# Rebuild libtorch_python.so for a different Python version.
# No cmake reconfigure — cmake breaks BUILD_PYTHON detection on re-detect.
# Instead: targeted sed on INCLUDES lines in build.ninja, then ninja rebuild.
# Ninja handles parallelism, dependency tracking, and linking automatically.
local BUILD_DIR="${_CAFFE2_ORIG_BUILD_DIR}"
local _ver="${EPYTHON#python}"
# Skip the version already built by cmake (libs saved in src_compile)
if [[ -d "${T}/pylibs/${_ver}" ]] && [[ -f "${T}/pylibs/${_ver}/libtorch_python.so" ]]; then
einfo "Skipping ${EPYTHON} — already built by cmake"
return 0
fi
einfo "Rebuilding libtorch_python.so for ${EPYTHON} (${PYTHON}) in ${BUILD_DIR}"
# Targeted sed: only INCLUDES lines, only python version refs.
# All other targets (C++ backend, CUDA kernels) are untouched.
sed -i "/^ INCLUDES = /s|python3\.[0-9]\+|python${_ver}|g" \
"${BUILD_DIR}/build.ninja" || die "Failed to patch build.ninja for ${EPYTHON}"
# Ninja detects the changed build rules, rebuilds only affected .o files + link.
ninja -C "${BUILD_DIR}" lib/libtorch_python.so -j$(makeopts_jobs) \
|| die "torch_python rebuild failed for ${EPYTHON}"
# Save Python-ABI-specific libs
mkdir -p "${T}/pylibs/${_ver}" || die
local _ltp=""
for _candidate in \
"${BUILD_DIR}/lib/libtorch_python.so" \
"${BUILD_DIR}/lib64/libtorch_python.so"; do
[[ -f "${_candidate}" ]] && _ltp="${_candidate}" && break
done
[[ -z "${_ltp}" ]] && _ltp=$(find "${BUILD_DIR}" -name libtorch_python.so -type f | head -1)
[[ -z "${_ltp}" ]] && die "libtorch_python.so not found after rebuild for ${EPYTHON}"
cp "${_ltp}" "${T}/pylibs/${_ver}/" || die
local cext
for cext in "${BUILD_DIR}"/torch/_C*.so; do
[[ -f "${cext}" ]] || continue
cp "${cext}" "${T}/pylibs/${_ver}/_C.so" || die
done
einfo "Saved Python libs for ${EPYTHON}"
}
src_compile() {
# Full build against lowest Python (configured in src_configure)
PYTORCH_BUILD_VERSION=${PV} \
PYTORCH_BUILD_NUMBER=0 \
cmake_src_compile
# Save libs for the first (lowest) Python version
# NOTE: EPYTHON comes from python_setup (highest version), but cmake was
# configured for the lowest. Compute the lowest the same way as src_configure.
local _pyversions_c=()
local _impl_c _pv_c
for _impl_c in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
use "python_targets_${_impl_c}" || continue
_pv_c="${_impl_c#python}"
_pv_c="${_pv_c/_/.}"
_pyversions_c+=("${_pv_c}")
done
_pyversions_c=($(printf '%s\n' "${_pyversions_c[@]}" | sort -V))
local _first_ver="${_pyversions_c[0]}"
local _ltp=""
for _candidate in \
"${BUILD_DIR}/lib/libtorch_python.so" \
"${BUILD_DIR}/lib64/libtorch_python.so"; do
[[ -f "${_candidate}" ]] && _ltp="${_candidate}" && break
done
[[ -z "${_ltp}" ]] && _ltp=$(find "${BUILD_DIR}" -name libtorch_python.so -type f | head -1)
[[ -z "${_ltp}" ]] && die "libtorch_python.so not found in build dir"
mkdir -p "${T}/pylibs/${_first_ver}" || die
cp "${_ltp}" "${T}/pylibs/${_first_ver}/" || die
local cext
for cext in "${BUILD_DIR}"/torch/_C*.so; do
[[ -f "${cext}" ]] || continue
cp "${cext}" "${T}/pylibs/${_first_ver}/_C.so" || die
done
einfo "Saved Python libs for python${_first_ver}"
# Rebuild libtorch_python.so for each additional Python version
export _CAFFE2_ORIG_BUILD_DIR="${BUILD_DIR}"
python_foreach_impl _rebuild_torch_python
}
src_install() {
cmake_src_install
# CMake cache for pytorch-cuda
insinto "/var/lib/caffe2-cuda"
doins "${BUILD_DIR}"/CMakeCache.txt
# Remove cmake-installed libtorch_python.so — replaced by per-version copies
rm -f "${ED}${CAFFE2_PREFIX}/$(get_libdir)/libtorch_python.so" || die
# Per-Python-version libs: libtorch_python.so + _C.so
local _ver _soabi _lowest="" _dirs
_dirs=( "${T}/pylibs"/*/ )
[[ -d "${_dirs[0]}" ]] || die "No pylibs directories found — src_compile failed?"
for _ver in "${_dirs[@]}"; do
_ver="${_ver%/}"
_ver="${_ver##*/}"
[[ -z "${_lowest}" ]] && _lowest="${_ver}"
_soabi="cpython-${_ver//./}-x86_64-linux-gnu"
# libtorch_python.so into version-specific subdir
exeinto "${CAFFE2_PREFIX}/py${_ver//./}"
doexe "${T}/pylibs/${_ver}/libtorch_python.so"
patchelf --set-rpath \
"\$ORIGIN:\$ORIGIN/../$(get_libdir)" \
"${ED}${CAFFE2_PREFIX}/py${_ver//./}/libtorch_python.so" || die
# _C.so with SOABI tag
exeinto "${CAFFE2_PREFIX}"
newexe "${T}/pylibs/${_ver}/_C.so" "_C.${_soabi}.so"
patchelf --set-rpath \
"\$ORIGIN/py${_ver//./}:\$ORIGIN/$(get_libdir)" \
"${ED}${CAFFE2_PREFIX}/_C.${_soabi}.so" || die
# Also install plain _C.so (Python fallback when SOABI lookup fails)
if [[ "${_ver}" == "${_lowest}" ]]; then
exeinto "${CAFFE2_PREFIX}"
newexe "${T}/pylibs/${_ver}/_C.so" "_C.so"
patchelf --set-rpath \
"\$ORIGIN/py${_ver//./}:\$ORIGIN/$(get_libdir)" \
"${ED}${CAFFE2_PREFIX}/_C.so" || die
fi
done
# Default libtorch_python.so symlink in lib64 for non-Python consumers
dosym "../py${_lowest//./}/libtorch_python.so" \
"${CAFFE2_PREFIX}/$(get_libdir)/libtorch_python.so"
# Symlinks for pytorch-cuda — torch module dirs and lib resolution
local torch_dest="${PYTORCH_PREFIX}"
dodir "${torch_dest}/torch/bin"
dodir "${torch_dest}/torch/lib"
dodir "${torch_dest}/torch/include"
dosym "${CAFFE2_PREFIX}/include/torch" "${torch_dest}/torch/include/torch"
dosym "${CAFFE2_PREFIX}/bin/torch_shm_manager" "${torch_dest}/torch/bin/torch_shm_manager"
dosym "${CAFFE2_PREFIX}/$(get_libdir)/libtorch_global_deps.so" "${torch_dest}/torch/lib/libtorch_global_deps.so"
}
pkg_postinst() {
local active
active=$(eselect caffe2 show 2>/dev/null)
if [[ "${active}" == "(unset)" || -z "${active}" ]]; then
eselect caffe2 set cuda
elog "caffe2 backend set to: cuda"
fi
}
Manage flags for this package:
euse -i <flag> -p sci-ml/caffe2-cuda |
euse -E <flag> -p sci-ml/caffe2-cuda |
euse -D <flag> -p sci-ml/caffe2-cuda
${RDEPEND}
dev-cpp/nlohmann_json
dev-libs/flatbuffers
dev-libs/FXdiv
dev-libs/pocketfft
dev-libs/psimd
sci-ml/FP16
$(python_gen_cond_dep '
dev-python/pybind11[${PYTHON_USEDEP}]
dev-python/pyyaml[${PYTHON_USEDEP}]
dev-python/typing-extensions[${PYTHON_USEDEP}]
')
~dev-libs/cutlass-4.6.1[tools(+)]
onednn? ( sci-ml/ideep )
qnnpack? ( dev-libs/clog )
${PYTHON_DEPS}
app-eselect/eselect-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
virtual/lapack
dev-libs/cudnn
>=sci-ml/cudnn-frontend-1.12.0:=
>=dev-util/nvidia-cuda-toolkit-12.9:=[profiler]
cusparselt? ( dev-libs/cusparselt )
fbgemm? ( >=sci-ml/FBGEMM-1.4 )
gloo? ( >=sci-ml/gloo-2025.06.04[cuda] )
kineto? ( ~sci-ml/kineto-0.4.0_p20260805 )
mimalloc? ( dev-libs/mimalloc )
mpi? ( virtual/mpi )
nnpack? ( sci-ml/NNPACK dev-libs/pthreadpool )
numpy? ( $(python_gen_cond_dep 'dev-python/numpy[${PYTHON_USEDEP}]') )
onednn? ( sci-ml/oneDNN )
qnnpack? ( !sci-libs/QNNPACK sci-ml/gemmlowp dev-libs/pthreadpool )
distributed? ( sci-ml/tensorpipe[cuda] dev-cpp/cpp-httplib:= )
xnnpack? ( >=sci-ml/XNNPACK-2024.11 dev-libs/pthreadpool )
mkl? ( sci-libs/mkl )
openblas? ( sci-libs/openblas )
dev-build/cmake dev-util/patchelf
| Type | File | Size | Source URLs |
|---|---|---|---|
| DIST | flash-attention-2.7.4.gh.tar.gz | 5841323 bytes | https://github.com/Dao-AILab/flash-attention/archive/refs/tags/v2.7.4.tar.gz |
| DIST | pytorch-2.15.0.tar.gz | 80376805 bytes | https://github.com/pytorch/pytorch/archive/refs/tags/v2.15.0.tar.gz |