1PORTNAME=	pytorch
2DISTVERSIONPREFIX=	v
3DISTVERSION=	2.4.1
4CATEGORIES=	misc # machine-learning
5MASTER_SITES=	https://github.com/pytorch/pytorch/releases/download/v${DISTVERSION}/
6DIST_SUBDIR=	${PORTNAME}
7
8MAINTAINER=	ports@MidnightBSD.org
9COMMENT=	Tensors and dynamic neural networks in Python (C++ library)
10WWW=		https://pytorch.org/
11
12LICENSE=	bsd3
13LICENSE_FILE=	${WRKSRC}/LICENSE
14
15BROKEN_i386=	compilation fails: error: use of undeclared identifier 'AVX2'
16
17BUILD_DEPENDS=	gmake:devel/gmake \
18		${LOCALBASE}/include/fxdiv.h:devel/fxdiv \
19		${PY_SETUPTOOLS}
20LIB_DEPENDS=	libabsl_base.so:devel/abseil \
21		libblis.so:math/blis \
22		libopenblas.so:math/openblas \
23		libmpi_cxx.so:net/openmpi4 \
24		libonnx.so:misc/onnx \
25		libpthreadpool.so:devel/pthreadpool \
26		libprotobuf.so:devel/protobuf \
27		libsleef.so:math/sleef
28
29USES=		compiler:c++17-lang cmake localbase:ldflags python # requires python even with PYTHON=off
30
31CMAKE_OFF=	BUILD_CUSTOM_PROTOBUF USE_CUDA USE_ROCM  USE_NNPACK USE_QNNPACK USE_PYTORCH_QNNPACK \
32		USE_FBGEMM # workaround recommended by the upstream here: https://github.com/pytorch/pytorch/issues/28337
33CMAKE_ON=	USE_SYSTEM_PYBIND11 \
34		USE_SYSTEM_SLEEF \
35		USE_SYSTEM_ONNX # see other USE_SYSTEM_xx in CMakeLists.txt
36CMAKE_ON+=	ONNX_USE_LITE_PROTO # workaround for the protobuf-related failure described here: https://github.com/onnx/optimizer/issues/38
37CMAKE_ARGS=	-DPSIMD_SOURCE_DIR=${WRKSRC}/third_party/psimd \
38		-DPYTHON_EXECUTABLE=${PYTHON_CMD}
39
40MAKE_ENV=	USE_NINJA=no # ninja breaks for some reason
41LDFLAGS+=	-lexecinfo \
42		-pthread
43
44BINARY_ALIAS=	make=${GMAKE} git=true
45
46OPTIONS_DEFINE=		PYTHON # PYTHON only provides python support for the py-PyTorch port - it's not a python binding
47OPTIONS_DEFAULT=	PYTHON
48OPTIONS_SUB=		yes
49
50PYTHON_CMAKE_BOOL=	BUILD_PYTHON
51PYTHON_PY_DEPENDS=	${PYNUMPY} \
52			${PYTHON_PKGNAMEPREFIX}typing-extensions>0:devel/py-typing-extensions@${PY_FLAVOR} \
53			${PYTHON_PKGNAMEPREFIX}pyyaml>=0:devel/py-pyyaml@${PY_FLAVOR}
54PYTHON_BUILD_DEPENDS=	${PYTHON_PY_DEPENDS} \
55			${PY_SETUPTOOLS} \
56			pybind11>0:devel/pybind11
57PYTHON_RUN_DEPENDS=	${PYTHON_PY_DEPENDS}
58
59.include <bsd.port.pre.mk>
60
61.if ${ARCH} == "amd64"
62PLIST_SUB+=	AMD64=""
63.else
64PLIST_SUB+=	AMD64="@comment "
65.endif
66
67post-patch:
68	# replace malloc.h -> stdlib.h in many places
69	@cd ${WRKSRC} && for f in $$(${GREP} -rl "include <malloc\.h>" * | ${GREP} -E "\.(c|cpp|h|hpp)$$"); do \
70	  ${REINPLACE_CMD} -i'' -E "s|include <malloc\.h>|include <stdlib.h>|" $$f; \
71	done
72	# remove bundled fmt
73	@${RM} -r ${WRKSRC}/third_party/fmt
74
75post-install:
76	# functorch.so is installed back into the source directory, see https://github.com/pytorch/pytorch/issues/91524
77	@${RM} $$(${FIND} ${FAKE_DESTDIR} -name functorch.so)
78	# https://github.com/pytorch/pytorch/issues/24417
79	@${FIND} ${FAKE_DESTDIR} -type d -empty -delete
80
81.include <bsd.port.post.mk>
82