1PORTNAME=	pytorch
2DISTVERSIONPREFIX=	v
3DISTVERSION=	2.7.0
4CATEGORIES=	misc # machine-learning
5MASTER_SITES=	https://github.com/pytorch/pytorch/releases/download/v${DISTVERSION}/
6PKGNAMEPREFIX=	${PYTHON_PKGNAMEPREFIX}
7DISTFILES=	${PORTNAME}-${DISTVERSIONFULL}${EXTRACT_SUFX} # the main tarball disappears when GH_xx tags are added w/out this line
8DIST_SUBDIR=	${PORTNAME}
9
10MAINTAINER=	ports@MidnightBSD.org
11COMMENT=	PyTorch: Tensors and dynamic neural networks in Python
12WWW=		https://pytorch.org/ \
13		https://github.com/pytorch/pytorch
14
15LICENSE=	bsd3
16LICENSE_FILE=	${WRKSRC}/LICENSE
17
18BROKEN_aarch64=	build fails: CMake Error at third_party/QNNPACK/CMakeLists.txt:47 (MESSAGE): Unrecognized CMAKE_SYSTEM_NAME = FreeBSD, see https://github.com/pytorch/pytorch/issues/144608
19BROKEN_armv7=	build fails: CMake Error at third_party/QNNPACK/CMakeLists.txt:47 (MESSAGE): Unrecognized CMAKE_SYSTEM_NAME = FreeBSD, see https://github.com/pytorch/pytorch/issues/144608
20BROKEN_i386=	build fails: DispatchStub.cpp:162:29: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'AVX2'[0m
21
22BUILD_DEPENDS=	cmake:devel/cmake-core \
23		gmake:devel/gmake \
24		pybind11>0:devel/pybind11 \
25		${PYNUMPY} \
26		${LOCALBASE}/include/fxdiv.h:devel/fxdiv \
27		${PYTHON_PKGNAMEPREFIX}typing-extensions>0:devel/py-typing-extensions@${PY_FLAVOR} \
28		${PYTHON_PKGNAMEPREFIX}pyyaml>=0:devel/py-pyyaml@${PY_FLAVOR}
29LIB_DEPENDS=	libabsl_base.so:devel/abseil \
30		libblis.so:math/blis \
31		libmpi_cxx.so:net/openmpi4 \
32		libonnx.so:misc/onnx \
33		libopenblas.so:math/openblas \
34		libpthreadpool.so:devel/pthreadpool \
35		libprotobuf.so:devel/protobuf \
36		libsleef.so:math/sleef
37RUN_DEPENDS=	${PYTHON_PKGNAMEPREFIX}filelock>0:sysutils/py-filelock@${PY_FLAVOR} \
38		${PYTHON_PKGNAMEPREFIX}fsspec>0:filesystems/py-fsspec@${PY_FLAVOR}  \
39		${PYTHON_PKGNAMEPREFIX}Jinja2>0:devel/py-Jinja2@${PY_FLAVOR} \
40		${PYTHON_PKGNAMEPREFIX}networkx>0:math/py-networkx@${PY_FLAVOR} \
41		${PYNUMPY} \
42		${PYTHON_PKGNAMEPREFIX}sympy>=1.13.1:math/py-sympy@${PY_FLAVOR} \
43		${PYTHON_PKGNAMEPREFIX}typing-extensions>=4.10.0:devel/py-typing-extensions@${PY_FLAVOR} \
44		${PYTHON_PKGNAMEPREFIX}pyyaml>0:devel/py-pyyaml@${PY_FLAVOR}
45RUN_DEPENDS+=	${PYTHON_PKGNAMEPREFIX}astunparse>0:devel/py-astunparse@${PY_FLAVOR} \
46		${PYTHON_PKGNAMEPREFIX}dill>0:devel/py-dill@${PY_FLAVOR} \
47		${PYTHON_PKGNAMEPREFIX}networkx>0:math/py-networkx@${PY_FLAVOR} \
48		${PYTHON_PKGNAMEPREFIX}protobuf>0:devel/py-protobuf@${PY_FLAVOR} # optional dependencies
49
50USES=		compiler:c++17-lang localbase:ldflags python
51USE_PYTHON=	distutils autoplist
52
53USE_GITHUB=	nodefault
54GH_TUPLE=	pytorch:cpuinfo:1e83a2f:cpuinfo/cpuinfo-with-freebsd-support # https://github.com/pytorch/cpuinfo/pull/230/commits
55
56MAKE_ENV=	USE_NINJA=no # ninja breaks for some reason
57MAKE_ENV+=	BUILD_TEST=0 # ninja breaks for some reason
58MAKE_ENV+=	USE_MKLDNN=0 # disable MKLDNN that doesn't exist, see https://github.com/pytorch/pytorch/issues/100957
59MAKE_ENV+=	USE_CUDNN=0
60MAKE_ENV+=	USE_LAPACK=1 # needed on FreeBSD to run w/out GPU
61MAKE_ENV+=	USE_QNNPACK=0
62LDFLAGS+=	-lexecinfo
63LDFLAGS_powerpc64le=	-pthread
64
65BINARY_ALIAS=	make=${GMAKE}
66
67POST_PLIST=	fix-plist
68
69TEST_ENV=	${MAKE_ENV} PYTHONPATH=${PYTHONPREFIX_SITELIBDIR}
70TEST_WRKSRC=	${WRKSRC}/test
71
72.include <bsd.mport.options.mk>
73
74USES+=	llvm:max=15
75
76post-patch:
77	@cd ${WRKSRC} && \
78		${RM} -r third_party/cpuinfo third_party/fbgemm/third_party/cpuinfo && \
79		${CP} -r cpuinfo-with-freebsd-support third_party/cpuinfo && \
80		${CP} -r cpuinfo-with-freebsd-support third_party/fbgemm/third_party/cpuinfo
81
82post-install: # strip binaries
83	@${STRIP_CMD} \
84		${PYTHON_SITELIBDIR}/torch/bin/torch_shm_manager \
85		${PYTHON_SITELIBDIR}/torch/_C${PYTHON_EXT_SUFFIX}.so \
86		${PYTHON_SITELIBDIR}/functorch/_C${PYTHON_EXT_SUFFIX}.so \
87		${PYTHON_SITELIBDIR}/torch/lib/lib*.so
88
89fix-plist: # remove the stray %%PYTHON_SITELIBDIR%%/caffe2 file
90	@${REINPLACE_CMD} -e "s|.*/caffe2$$||" ${TMPPLIST}
91
92do-test:
93	cd ${TEST_WRKSRC} && ${SETENV} ${TEST_ENV} ${PYTHON_CMD} run_test.py
94
95.include <bsd.port.mk>
96