Diff pytorch-2.12.0 with a pytorch-2.13.0-r3

/usr/portage/sci-ml/pytorch/pytorch-2.13.0-r3.ebuild 2026-09-03 20:03:05.313217551 +0300
4 4
EAPI=8
5 5

  
6 6
DISTUTILS_USE_PEP517=setuptools
7
PYTHON_COMPAT=( python3_{11..14} )
7
PYTHON_COMPAT=( python3_{12..14} )
8 8
DISTUTILS_SINGLE_IMPL=1
9 9
DISTUTILS_EXT=1
10
inherit distutils-r1 prefix
11 10

  
12
DESCRIPTION="Tensors and Dynamic neural networks in Python"
11
ROCM_VERSION=6.1
12
inherit distutils-r1 prefix cuda flag-o-matic rocm multiprocessing
13

  
14
# pytorch-2.13.0 depends on specific commit of composable kernel
15
# TODO: replace it with DEPEND in the future
16
CK_COMMIT=f1746955fdaf80a3414de814bf32437686dac347
17
CK_P=composable_kernel-${CK_COMMIT:0:8}
18

  
19
# Starting from 2.7.0 pytorch moved flash attention out-of-tree,
20
# but hardcoded it as third_party subproject
21
# TODO: unbundle
22
FLASH_PV=2.7.4
23
FLASH_PN=flash-attention
24
FLASH_P=${FLASH_PN}-${FLASH_PV}
25
FLASH_ATT_URI="https://github.com/Dao-AILab/${FLASH_PN}/archive/refs/tags/v${FLASH_PV}.tar.gz -> ${FLASH_P}.gh.tar.gz"
26

  
27
DESCRIPTION="Tensors and Dynamic neural networks in Python with strong GPU acceleration"
13 28
HOMEPAGE="https://pytorch.org/"
14 29
SRC_URI="https://github.com/pytorch/${PN}/archive/refs/tags/v${PV}.tar.gz
15
	-> ${P}.tar.gz"
30
	-> ${P}.tar.gz
31
	rocm? (
32
		https://github.com/ROCm/composable_kernel/archive/${CK_COMMIT}.tar.gz
33
		-> ${CK_P}.tar.gz
34
	)
35
	cuda? (
36
		flash? ( ${FLASH_ATT_URI} )
37
		memefficient? ( ${FLASH_ATT_URI} )
38
	)
39
"
16 40

  
17 41
LICENSE="BSD"
18 42
SLOT="0"
19 43
KEYWORDS="~amd64 ~arm64"
44
IUSE="cuda cusparselt distributed fbgemm flash gloo kineto memefficient
45
	mimalloc mkl mpi nccl nnpack +numpy onednn openblas opencl openmp qnnpack
46
	rocm xnnpack"
20 47
RESTRICT="test"
21 48

  
22
REQUIRED_USE=${PYTHON_REQUIRED_USE}
49
REQUIRED_USE="
50
	${PYTHON_REQUIRED_USE}
51
	mpi? ( distributed )
52
	gloo? ( distributed )
53
	?? ( cuda rocm )
54
	rocm? (
55
		|| ( ${ROCM_REQUIRED_USE} )
56
		memefficient? ( flash )
57
	)
58
	cusparselt? ( || ( cuda rocm ) )
59
	flash? ( || ( cuda rocm ) )
60
	memefficient? ( || ( cuda rocm ) )
61
	nccl? ( rocm )
62
"
63

  
23 64
RDEPEND="
24 65
	${PYTHON_DEPS}
25
	~sci-ml/caffe2-${PV}[${PYTHON_SINGLE_USEDEP}]
66
	!sci-ml/caffe2
67
	dev-cpp/abseil-cpp:=
68
	dev-cpp/gflags:=
69
	>=dev-cpp/glog-0.6.0:=
70
	>=dev-libs/cpuinfo-2025.11.14
71
	dev-libs/libfmt:=
72
	dev-libs/protobuf:=
73
	dev-libs/sleef
74
	sci-ml/onnx
26 75
	$(python_gen_cond_dep '
27 76
		dev-python/sympy[${PYTHON_USEDEP}]
28 77
		dev-python/typing-extensions[${PYTHON_USEDEP}]
29 78
	')
79
	!mkl? ( !openblas? ( virtual/blas ) )
80
	cuda? (
81
		dev-libs/cudnn
82
		>=sci-ml/cudnn-frontend-1.12.0:=
83
		>=dev-util/nvidia-cuda-toolkit-12.9:=[profiler]
84
		cusparselt? ( dev-libs/cusparselt )
85
	)
86
	distributed? (
87
		!rocm? ( sci-ml/tensorpipe[cuda?] )
88
		dev-cpp/cpp-httplib:=
89
	)
90
	fbgemm? ( >=sci-ml/FBGEMM-1.4 )
91
	gloo? ( >=sci-ml/gloo-2025.06.04[cuda?,rocm?] )
92
	kineto? ( ~sci-ml/kineto-0.4.0_p20260603 )
93
	mimalloc? ( dev-libs/mimalloc )
94
	mkl? ( sci-libs/mkl )
95
	mpi? ( virtual/mpi )
96
	nnpack? (
97
		sci-ml/NNPACK
98
		dev-libs/pthreadpool
99
	)
100
	numpy? ( $(python_gen_cond_dep '
101
		dev-python/numpy[${PYTHON_USEDEP}]
102
	') )
103
	onednn? ( sci-ml/oneDNN )
104
	openblas? ( sci-libs/openblas )
105
	opencl? ( virtual/opencl )
106
	qnnpack? (
107
		!sci-libs/QNNPACK
108
		sci-ml/gemmlowp
109
		dev-libs/pthreadpool
110
	)
111
	rocm? (
112
		nccl? ( >=dev-libs/rccl-6.3:= <dev-libs/rccl-7.3:= )
113
		>=dev-util/hip-6.3:=       <dev-util/hip-7.3:=
114
		>=dev-util/roctracer-6.3:= <dev-util/roctracer-7.3:=
115
		>=sci-libs/hipBLAS-6.3:=   <sci-libs/hipBLAS-7.3:=[rocsolver(+)]
116
		>=sci-libs/hipBLASLt-6.3:= <sci-libs/hipBLASLt-7.3:=
117
		>=sci-libs/hipFFT-6.3:=    <sci-libs/hipFFT-7.3:=
118
		>=sci-libs/hipRAND-6.3:=   <sci-libs/hipRAND-7.3:=
119
		>=sci-libs/hipSOLVER-6.3:= <sci-libs/hipSOLVER-7.3:=
120
		>=sci-libs/hipSPARSE-6.3:= <sci-libs/hipSPARSE-7.3:=
121
		>=sci-libs/miopen-6.3:=    <sci-libs/miopen-7.3:=
122
		>=sci-libs/rocBLAS-6.3:=   <sci-libs/rocBLAS-7.3:=
123
		>=sci-libs/rocRAND-6.3:=   <sci-libs/rocRAND-7.3:=
124
		>=sci-libs/rocSOLVER-6.3:= <sci-libs/rocSOLVER-7.3:=
125
		memefficient? ( =sci-libs/aotriton-bin-0.13*:= )
126
		distributed? (
127
			>=dev-util/rocm-smi-6.3:= <dev-util/rocm-smi-7.3:=
128
			>=dev-util/amdsmi-6.3:= <dev-util/amdsmi-7.3:=
129
		)
130
		cusparselt? ( >=sci-libs/hipsparselt-6.3:= <sci-libs/hipsparselt-7.3:= )
131
	)
132
	xnnpack? (
133
		>=sci-ml/XNNPACK-2024.11
134
		dev-libs/pthreadpool
135
	)
30 136
"
31
DEPEND="${RDEPEND}
137

  
138
DEPEND="
139
	${RDEPEND}
140
	dev-cpp/nlohmann_json
141
	dev-libs/flatbuffers
142
	dev-libs/FXdiv
143
	dev-libs/pocketfft
144
	dev-libs/psimd
145
	sci-ml/FP16
32 146
	$(python_gen_cond_dep '
147
		<dev-python/pybind11-3.0.5[${PYTHON_USEDEP}]
33 148
		dev-python/pyyaml[${PYTHON_USEDEP}]
149
		dev-python/typing-extensions[${PYTHON_USEDEP}]
34 150
	')
151
	cuda? ( ~dev-libs/cutlass-4.4.2[tools(+)] )
152
	onednn? ( sci-ml/ideep )
153
	rocm? (
154
		>=sci-libs/hipCUB-6.3:=    <sci-libs/hipCUB-7.3:=
155
		>=sci-libs/rocPRIM-6.3:=   <sci-libs/rocPRIM-7.3:=
156
		>=sci-libs/rocThrust-6.3:= <sci-libs/rocThrust-7.3:=
157
	)
158
	qnnpack? ( dev-libs/clog )
35 159
"
36 160

  
161
BDEPEND="dev-build/cmake"
162

  
37 163
PATCHES=(
38
	"${FILESDIR}"/${PN}-2.9.0-dontbuildagain.patch
39
	"${FILESDIR}"/${PN}-2.10.0-cpp-extension-multilib.patch
164
	"${FILESDIR}"/${P}-aotriton-fixes.patch
165
	"${FILESDIR}"/${P}-glog.patch
166
	"${FILESDIR}"/${P}-mimalloc.patch
167
	"${FILESDIR}"/${P}-removekineto-pr178960.patch
168
	"${FILESDIR}"/${P}-rocm-assert-fix.patch
169
	"${FILESDIR}"/${P}-unbundle_fbgemm.patch
170
	"${FILESDIR}"/${P}-unbundle_flatbuffers.patch
171
	"${FILESDIR}"/${P}-unbundle_fmt.patch
172
	"${FILESDIR}"/${P}-unbundle_fxdiv.patch
173
	"${FILESDIR}"/${P}-unbundle_httplib.patch
174
	"${FILESDIR}"/${P}-unbundle_kineto.patch
175
	"${FILESDIR}"/${P}-unbundle_mkldnn.patch
176
	"${FILESDIR}"/${P}-unbundle_nnpack.patch
177
	"${FILESDIR}"/${P}-unbundle_pocketfft.patch
178
	"${FILESDIR}"/${P}-xnnpack.patch
40 179
)
41 180

  
42 181
src_prepare() {
182
	if use cuda && ( use flash || use memefficient ); then
183
		mv "${WORKDIR}"/${FLASH_P}/* third_party/${FLASH_PN}/ || die
184
	fi
185
	filter-lto #bug 862672
186

  
187
	eapply "${FILESDIR}/${PN}-2.10.0-cpp-extension-multilib.patch"
43 188
	# Replace placeholders added by cpp-extension.patch
44 189
	sed -e "s|%LIB_DIR%|$(get_libdir)|g" \
45 190
		-i torch/utils/cpp_extension.py || die
46 191

  
47
	# Set build dir for pytorch's setup
48
	sed -e "/BUILD_DIR/s|build|/var/lib/caffe2/|" \
49
		-i tools/setup_helpers/env.py || die
50

  
51
	# Drop legacy from pyproject.toml
52
	sed -e "/build-backend/s|:__legacy__||" \
53
		-i pyproject.toml || die
192
	# Unbundle fmt
193
	sed -i \
194
		-e 's|::fmt-header-only||' \
195
		c10/CMakeLists.txt \
196
		cmake/Dependencies.cmake \
197
		torch/CMakeLists.txt \
198
		|| die
199

  
200
	# tensorpipe is in system, not a build target of pytorch
201
	sed -e '/target_compile_options_if_supported(tensorpipe/d' -i cmake/Dependencies.cmake || die
202

  
203
	# Drop third_party from CMake tree
204
	sed -i \
205
		-e '/add_subdirectory.*third_party/d' \
206
		CMakeLists.txt \
207
		cmake/Dependencies.cmake \
208
		cmake/ProtoBuf.cmake \
209
		aten/src/ATen/CMakeLists.txt \
210
		|| die
211

  
212
	# Add needed file for cutlass as symbolic link
213
	ln -sf /usr/share/cutlass/examples third_party/cutlass/examples || die
214

  
215
	# cudnn_frontend is unbundled, but some targets still look for its
216
	# headers under third_party/cudnn_frontend/include.
217
	if use cuda; then
218
		mkdir -p third_party/cudnn_frontend || die
219
		ln -sf /usr/include third_party/cudnn_frontend/include || die
220
	fi
54 221

  
55 222
	distutils-r1_src_prepare
56 223

  
57
	hprefixify tools/setup_helpers/env.py
224
	# Noisy warnings from Logging.h
225
	sed -i 's/-Wextra-semi//' cmake/public/utils.cmake || die
226

  
227
	pushd torch/csrc/jit/serialization > /dev/null || die
228
	flatc --cpp --gen-mutable --scoped-enums mobile_bytecode.fbs || die
229
	popd > /dev/null || die
230

  
231
	# prefixify the hardcoded paths, after all patches are applied
232
	hprefixify \
233
		aten/CMakeLists.txt \
234
		caffe2/CMakeLists.txt \
235
		cmake/Metal.cmake \
236
		cmake/Modules/*.cmake \
237
		cmake/Modules_CUDA_fix/FindCUDNN.cmake \
238
		cmake/Modules_CUDA_fix/upstream/FindCUDA/make2cmake.cmake \
239
		cmake/Modules_CUDA_fix/upstream/FindPackageHandleStandardArgs.cmake \
240
		cmake/public/LoadHIP.cmake \
241
		cmake/public/cuda.cmake \
242
		cmake/Dependencies.cmake \
243
		tools/setup_helpers/env.py \
244
		torch/CMakeLists.txt \
245
		CMakeLists.txt
246

  
247
	if use rocm; then
248
		# TODO: delete, when caffe2 depends on systemwide composable_kernel
249
		sed -e "s:third_party/composable_kernel:../composable_kernel-${CK_COMMIT}:g" \
250
			-i aten/src/ATen/CMakeLists.txt || die
251

  
252
		# Workaround for libc++ issue https://github.com/llvm/llvm-project/issues/100802
253
		sed -e 's/std::memcpy/memcpy/g' \
254
			-i torch/headeronly/util/Half.h \
255
			-i aten/src/ATen/native/cuda/int4mm.cu || die
256

  
257
		ebegin "HIPifying cuda sources"
258
		FBCODE_BUILD_TOOL="buck" ${EPYTHON} tools/amd_build/build_amd.py || die
259
		eend $?
260
	fi
58 261
}
59 262

  
60
python_compile() {
61
	PYTORCH_BUILD_VERSION=${PV} \
62
	PYTORCH_BUILD_NUMBER=0 \
63
	USE_SYSTEM_LIBS=ON \
64
	CMAKE_BUILD_DIR="${BUILD_DIR}" \
65
	distutils-r1_python_compile develop sdist
263
src_configure() {
264
	if use cuda && [[ -z ${TORCH_CUDA_ARCH_LIST} ]]; then
265
		ewarn "WARNING: pytorch is being built with its default CUDA compute capabilities: 3.5 and 7.0."
266
		ewarn "These may not be optimal for your GPU."
267
		ewarn ""
268
		ewarn "To configure pytorch with the CUDA compute capability that is optimal for your GPU,"
269
		ewarn "set TORCH_CUDA_ARCH_LIST in your make.conf, and re-emerge pytorch."
270
		ewarn "For example, to use CUDA capability 7.5 & 3.5, add: TORCH_CUDA_ARCH_LIST=7.5 3.5"
271
		ewarn "For a Maxwell model GPU, an example value would be: TORCH_CUDA_ARCH_LIST=Maxwell"
272
		ewarn ""
273
		ewarn "You can look up your GPU's CUDA compute capability at https://developer.nvidia.com/cuda-gpus"
274
		ewarn "or by running /opt/cuda/extras/demo_suite/deviceQuery | grep 'CUDA Capability'"
275
	fi
66 276
}
67 277

  
68
python_install() {
69
	USE_SYSTEM_LIBS=ON distutils-r1_python_install
278
python_compile() {
279
	local -x BUILD_TEST=OFF
280
	local -x CMAKE_BUILD_DIR="${BUILD_DIR}"
281
	local -x MAX_JOBS=$(makeopts_jobs)
282
	local -x PYTORCH_BUILD_VERSION=${PV}
283
	local -x PYTORCH_BUILD_NUMBER=0
284
	local -x USE_CCACHE=OFF
285
	local -x USE_CUDA=$(usex cuda)
286
	local -x USE_DISTRIBUTED=$(usex distributed)
287
	local -x USE_FBGEMM=$(usex fbgemm)
288
	local -x USE_FLASH_ATTENTION=$(usex flash)
289
	local -x USE_GFLAGS=ON
290
	local -x USE_GLOG=ON
291
	local -x USE_GLOO=$(usex gloo)
292
	local -x USE_ITT=OFF
293
	local -x USE_KINETO=$(usex kineto)
294
	local -x USE_KLEIDIAI=OFF # TODO
295
	local -x USE_MAGMA=OFF # TODO: In GURU as sci-libs/magma
296
	local -x USE_MEM_EFF_ATTENTION=$(usex memefficient)
297
	local -x USE_MIMALLOC=$(usex mimalloc)
298
	local -x USE_MKLDNN=$(usex onednn)
299
	local -x USE_MPI=$(usex mpi)
300
	local -x USE_NNPACK=$(usex nnpack)
301
	local -x USE_NUMA=OFF
302
	local -x USE_NUMPY=$(usex numpy)
303
	local -x USE_OPENCL=$(usex opencl)
304
	local -x USE_OPENMP=$(usex openmp)
305
	local -x USE_PYTORCH_QNNPACK=$(usex qnnpack)
306
	local -x USE_PYTORCH_METAL=OFF
307
	local -x USE_ROCM=$(usex rocm)
308
	local -x USE_SYSTEM_LIBS=ON
309
	local -x USE_SYSTEM_XNNPACK=$(usex xnnpack)
310
	local -x USE_TENSORPIPE=$(usex distributed $(usex !rocm))
311
	local -x DUSE_UCC=OFF
312
	local -x USE_VALGRIND=OFF
313
	local -x USE_XNNPACK=$(usex xnnpack)
314
	local -x USE_XPU=OFF
315

  
316
	if use mkl; then
317
		local -x USE_BLAS=ON
318
		local -x BLAS=MKL
319
	elif use openblas; then
320
		local -x USE_BLAS=ON
321
		local -x BLAS=OpenBLAS
322
	else
323
		# Note: USE_BLAS=OFF does not properly work, instead pytorch still searches for libraries.
324
		# Even though it is easy to patch pytorch to disable BLAS usage completely,
325
		# it is practically not useful, as it would render the library barely usable.
326
		local -x USE_BLAS=ON
327
		local -x BLAS=Generic
328
		local -x GENERIC_BLAS_LIBRARIES=cblas
329
	fi
330

  
331
	if use cuda; then
332
		# bug 867706 926116
333
		cuda_add_sandbox
334
		addpredict "/dev/char/"
335

  
336
		local -x CMAKE_CUDA_FLAGS="$(cuda_gccdir -f | tr -d \")"
337
		local -x TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST:-7.0}"
338
		local -x USE_CUDNN=ON
339
		local -x USE_FLASH_ATTENTION=OFF
340
		local -x USE_MEM_EFF_ATTENTION=OFF
341
		local -x USE_NCCL=OFF # TODO: NVIDIA Collective Communication Library
342

  
343
	elif use rocm; then
344
		export PYTORCH_ROCM_ARCH="$(get_amdgpu_flags)"
345

  
346
		if use memefficient; then
347
			export AOTRITON_INSTALLED_PREFIX="${ESYSROOT}/usr"
348
		fi
349

  
350
		local -x CMAKE_REQUIRE_FIND_PACKAGE_HIP=ON
351
		local -x USE_NCCL=$(usex nccl)
352
		local -x CMAKE_DISABLE_FIND_PACKAGE_hipsparselt=$(usex !cusparselt) # disable automagic
353
		local -x USE_ROCM_CK_SDPA=OFF # requires flash + aiter, works only on gfx90a/gfx942/gfx950
354
		local -x ROCM_PATH=/usr
355
		local -x HIP_CLANG_PATH=$(hipconfig --hipclangpath)
356

  
357
		# ROCm libraries produce too much warnings
358
		append-cxxflags -Wno-deprecated-declarations -Wno-unused-result -Wno-unused-value
359
	else
360
		local -x USE_NCCL=OFF
361
	fi
362

  
363
	distutils-r1_python_compile develop sdist
70 364
}
Thank you!