diff --git a/misc/py-pytorch/Makefile b/misc/py-pytorch/Makefile index 20a580d0a863..0b8689c7c067 100644 --- a/misc/py-pytorch/Makefile +++ b/misc/py-pytorch/Makefile @@ -1,149 +1,149 @@ PORTNAME= pytorch DISTVERSIONPREFIX= v DISTVERSION= 2.14.0 +PORTREVISION= 1 CATEGORIES= misc # machine-learning MASTER_SITES= https://github.com/pytorch/pytorch/releases/download/v${DISTVERSION}/ PKGNAMEPREFIX= ${PYTHON_PKGNAMEPREFIX} DISTFILES= ${PORTNAME}-${DISTVERSIONFULL}${EXTRACT_SUFX} # the main tarball disappears when GH_xx tags are added w/out this line DIST_SUBDIR= ${PORTNAME} MAINTAINER= yuri@FreeBSD.org COMMENT= PyTorch: Tensors and dynamic neural networks in Python WWW= https://pytorch.org/ \ https://github.com/pytorch/pytorch LICENSE= BSD3CLAUSE LICENSE_FILE= ${WRKSRC}/LICENSE -BROKEN_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 BROKEN_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 BROKEN_i386= build fails: DispatchStub.cpp:162:29: [0m[0;1;31merror: [0m[1muse of undeclared identifier 'AVX2'[0m BUILD_DEPENDS= cmake:devel/cmake-core \ gmake:devel/gmake \ pybind11>0:devel/pybind11 \ ${PYTHON_PKGNAMEPREFIX}numpy>=1.16:math/py-numpy@${PY_FLAVOR} \ ${LOCALBASE}/include/fxdiv.h:devel/fxdiv \ ${PYTHON_PKGNAMEPREFIX}filelock>0:sysutils/py-filelock@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}fsspec>0:filesystems/py-fsspec@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}hypothesis>0:devel/py-hypothesis@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}Jinja2>0:devel/py-Jinja2@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}optree>0:devel/py-optree@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}packaging>0:devel/py-packaging@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}psutil>0:sysutils/py-psutil@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}pyyaml>0:devel/py-pyyaml@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}requests>0:www/py-requests@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}scikit-build-core>=1.0:devel/py-scikit-build-core@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}six>0:devel/py-six@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}sympy>=1.13.3:math/py-sympy@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}typing-extensions>0:devel/py-typing-extensions@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}wheel>0:devel/py-wheel@${PY_FLAVOR} LIB_DEPENDS= libabsl_base.so:devel/abseil \ libonnx.so:misc/onnx \ libopenblas.so:math/openblas \ libprotobuf.so:devel/protobuf \ libsleef.so:math/sleef RUN_DEPENDS= ${PYTHON_PKGNAMEPREFIX}filelock>0:sysutils/py-filelock@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}fsspec>0:filesystems/py-fsspec@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}Jinja2>0:devel/py-Jinja2@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}networkx>0:math/py-networkx@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}numpy>=1.16:math/py-numpy@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}sympy>=1.13.3:math/py-sympy@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}typing-extensions>=4.10.0:devel/py-typing-extensions@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}pyyaml>0:devel/py-pyyaml@${PY_FLAVOR} RUN_DEPENDS+= ${PYTHON_PKGNAMEPREFIX}astunparse>0:devel/py-astunparse@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}dill>0:devel/py-dill@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}protobuf>0:devel/py-protobuf@${PY_FLAVOR} # optional dependencies TEST_DEPENDS= ${PYTHON_PKGNAMEPREFIX}expecttest>0:devel/py-expecttest@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}pytest-rerunfailures>0:devel/py-pytest-rerunfailures@${PY_FLAVOR} \ ${PYTHON_PKGNAMEPREFIX}pytest-xdist>0:devel/py-pytest-xdist@${PY_FLAVOR} USES= compiler:c++17-lang localbase:ldflags mpi:openmpi python shebangfix USE_PYTHON= pep517 autoplist SHEBANG_FILES= torch/utils/hipify/hipify_python.py # installed executable, unlike with the pre-2.14 setuptools build USE_LDCONFIG= ${PYTHON_SITELIBDIR}/torch/lib # The Python package is named "torch" not "pytorch" PYDISTUTILS_PKGNAME= torch PEP517_INSTALL_CMD= ${PYTHON_CMD} -m installer --destdir ${STAGEDIR} --prefix ${PREFIX} ${BUILD_WRKSRC}/dist/torch-${DISTVERSION}*.whl USE_GITHUB= nodefault GH_TUPLE= pytorch:cpuinfo:bc3c01e:cpuinfo/cpuinfo-with-freebsd-support # https://github.com/pytorch/cpuinfo/pull/230/commits MAKE_ENV= USE_NINJA=no # ninja breaks for some reason MAKE_ENV+= BUILD_TEST=0 # ninja breaks for some reason MAKE_ENV+= USE_MKLDNN=0 # disable MKLDNN that doesn't exist, see https://github.com/pytorch/pytorch/issues/100957 MAKE_ENV+= USE_CUDNN=0 MAKE_ENV+= USE_LAPACK=1 # needed on FreeBSD to run w/out GPU MAKE_ENV+= USE_QNNPACK=0 MAKE_ENV+= USE_DISTRIBUTED=1 # point FindMPI at the openmpi requested by USES=mpi, otherwise it picks up the # mpicc/mpicxx wrappers that net/mpich installs into LOCALBASE/bin, which link # libmpi/libmpicxx from mpich and drag in lang/gcc14 via the wrapper's -rpath MAKE_ENV+= CMAKE_ARGS="-DMPI_C_COMPILER=${MPI_HOME}/bin/mpicc -DMPI_CXX_COMPILER=${MPI_HOME}/bin/mpicxx" \ MAX_JOBS=${MAKE_JOBS_NUMBER} LDFLAGS+= -lexecinfo LDFLAGS_powerpc64le= -pthread BINARY_ALIAS= make=${GMAKE} TEST_ENV= ${MAKE_ENV} PYTHONPATH=${STAGEDIR}${PYTHONPREFIX_SITELIBDIR} \ TORCHINDUCTOR_CACHE_DIR=${WRKDIR}/torchinductor_cache TEST_WRKSRC= ${WRKSRC}/test OPTIONS_DEFINE= VULKAN OPTIONS_DEFAULT= VULKAN # VULKAN is experimental in PyTorch and only simple computations work VULKAN_MAKE_ENV= USE_VULKAN=1 USE_VULKAN_SHADERC_RUNTIME=1 USE_VULKAN_WRAPPER=0 # see https://docs.pytorch.org/tutorials/unstable/vulkan_workflow.html VULKAN_BUILD_DEPENDS= glslc:graphics/shaderc \ vulkan-headers>0:graphics/vulkan-headers VULKAN_LIB_DEPENDS= libvulkan.so:graphics/vulkan-loader CONFLICTS_BUILD= mpich # binaries are still linked with mpich when it is present .include post-extract: # upstream ships a stray *.orig file that 'make makepatch' would otherwise # mistake for a patch baseline and turn into a bogus binary patch @${RM} ${WRKSRC}/third_party/aiter/hsa/gfx950/pa/pa_bf16_noquant_gqa8_1tg_4w.co.orig post-patch: @cd ${WRKSRC} && \ ${RM} -r third_party/cpuinfo third_party/fbgemm/external/cpuinfo && \ ${CP} -r cpuinfo-with-freebsd-support third_party/cpuinfo && \ ${CP} -r cpuinfo-with-freebsd-support third_party/fbgemm/external/cpuinfo # change hardcoded compiler in torch/_inductor/config.py @${REINPLACE_CMD} -e 's|"g++"|"${CXX}"|' \ ${WRKSRC}/torch/_inductor/config.py do-install: # port should be moved to 'torch', but keep this for now with the hardcoded 'torch' name @${MKDIR} ${STAGEDIR}${PYTHONPREFIX_SITELIBDIR} @cd ${INSTALL_WRKSRC} && ${SETENVI} ${WRK_ENV} ${MAKE_ENV} ${PEP517_INSTALL_CMD} @${PYTHON_CMD} -B ${PORTSDIR}/Mk/Scripts/strip_RECORD.py \ ${STAGEDIR}${PYTHONPREFIX_SITELIBDIR}/torch-${DISTVERSION}*.dist-info/RECORD >> ${_PYTHONPKGLIST} @${REINPLACE_CMD} \ -e '/\.pyc$$/d' \ -e 's|^|${PYTHONPREFIX_SITELIBDIR}/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../etc/|etc/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../bin/|bin/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../include/|include/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../lib/|lib/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../libdata/|libdata/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../libexec/|libexec/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../man/|man/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../sbin/|sbin/|' \ -e 's|^${PYTHONPREFIX_SITELIBDIR}/../../../share/|share/|' \ ${_PYTHONPKGLIST} @cd ${STAGEDIR}${PREFIX} && ${FIND} lib -name '*.pyc' >> ${_PYTHONPKGLIST} do-test: @cd ${TEST_WRKSRC} && ${SETENV} ${TEST_ENV} ${PYTHON_CMD} run_test.py test-quick: @cd ${TEST_WRKSRC} && ${SETENV} ${TEST_ENV} ${PYTHON_CMD} ${FILESDIR}/example.py # tests as of 2.14.0: 231 passed 6 skipped .include diff --git a/misc/py-pytorch/files/patch-aten_src_ATen_native_quantized_cpu_qnnpack_CMakeLists.txt b/misc/py-pytorch/files/patch-aten_src_ATen_native_quantized_cpu_qnnpack_CMakeLists.txt new file mode 100644 index 000000000000..687b69ac6b83 --- /dev/null +++ b/misc/py-pytorch/files/patch-aten_src_ATen_native_quantized_cpu_qnnpack_CMakeLists.txt @@ -0,0 +1,36 @@ +-- Allow QNNPACK's own CMakeLists.txt platform check to accept FreeBSD on +-- aarch64. Upstream only allow-lists Darwin/Linux/Android for +-- CMAKE_SYSTEM_NAME here, causing configure to abort with +-- "Unrecognized CMAKE_SYSTEM_NAME = FreeBSD" (see +-- https://github.com/pytorch/pytorch/issues/144608). QNNPACK itself builds +-- and runs fine on FreeBSD/aarch64 once this check is relaxed, so only widen +-- it for that specific target; other platforms keep upstream's original +-- behavior unchanged. + +--- aten/src/ATen/native/quantized/cpu/qnnpack/CMakeLists.txt.orig 2026-09-02 17:43:46 UTC ++++ aten/src/ATen/native/quantized/cpu/qnnpack/CMakeLists.txt +@@ -54,13 +54,13 @@ if(NOT CMAKE_SYSTEM_PROCESSOR) + else() + message(FATAL_ERROR "CMAKE_SYSTEM_PROCESSOR is not defined") + endif() +-elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|x86_64|armv[5-8].*|aarch64|arm64)$") ++elseif(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[3-6]86|x86_64|AMD64|amd64|armv[5-8].*|aarch64|arm64)$") + message(FATAL_ERROR "Unrecognized CMAKE_SYSTEM_PROCESSOR = ${CMAKE_SYSTEM_PROCESSOR}") + endif() + + if(NOT CMAKE_SYSTEM_NAME) + message(FATAL_ERROR "CMAKE_SYSTEM_NAME not defined") +-elseif(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|Android)$") ++elseif(NOT CMAKE_SYSTEM_NAME MATCHES "^(Darwin|Linux|FreeBSD|Android)$") + message(FATAL_ERROR "Unrecognized CMAKE_SYSTEM_NAME = ${CMAKE_SYSTEM_NAME}") + endif() + +@@ -274,7 +274,7 @@ endif() + list(APPEND PYTORCH_QNNPACK_UKERNELS ${PYTORCH_QNNPACK_ARM_NEON_UKERNELS}) + list(APPEND PYTORCH_QNNPACK_UKERNELS ${PYTORCH_QNNPACK_AARCH64_ASM_UKERNELS}) + endif() +-if(PYTORCH_QNNPACK_TARGET_PROCESSOR MATCHES "^(i[3-6]86|x86_64)$" OR IOS_ARCH MATCHES "^(i386|x86_64)$") ++if(PYTORCH_QNNPACK_TARGET_PROCESSOR MATCHES "^(i[3-6]86|x86_64|AMD64|amd64)$" OR IOS_ARCH MATCHES "^(i386|x86_64)$") + list(APPEND PYTORCH_QNNPACK_UKERNELS ${PYTORCH_QNNPACK_X86_SSE2_UKERNELS}) + endif() + diff --git a/misc/py-pytorch/files/patch-cmake_Dependencies.cmake b/misc/py-pytorch/files/patch-cmake_Dependencies.cmake index f035ddb2eb8b..44fab43d4d32 100644 --- a/misc/py-pytorch/files/patch-cmake_Dependencies.cmake +++ b/misc/py-pytorch/files/patch-cmake_Dependencies.cmake @@ -1,11 +1,19 @@ ---- cmake/Dependencies.cmake.orig 2026-07-08 17:44:27 UTC +--- cmake/Dependencies.cmake.orig 2026-09-02 17:43:46 UTC +++ cmake/Dependencies.cmake -@@ -373,7 +373,7 @@ if(USE_NNPACK OR USE_PYTORCH_QNNPACK OR USE_XNNPACK) +@@ -373,14 +373,14 @@ if(USE_NNPACK OR USE_PYTORCH_QNNPACK OR USE_XNNPACK) set(DISABLE_NNPACK_AND_FAMILY ON) endif() else() - if(NOT IOS AND NOT (CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux|Darwin|Windows)$")) + if(NOT IOS AND NOT (CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux|FreeBSD|Darwin|Windows)$")) message(WARNING "Target platform \"${CMAKE_SYSTEM_NAME}\" is not supported in {Q/X}NNPACK. " "Supported platforms are Android, iOS, Linux, and macOS. " + "Turn this warning off by USE_{Q/X}NNPACK=OFF.") + set(DISABLE_NNPACK_AND_FAMILY ON) + endif() +- if(NOT IOS AND NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|AMD64|x86_64|armv[0-9].*|arm64|aarch64)$")) ++ if(NOT IOS AND NOT (CMAKE_SYSTEM_PROCESSOR MATCHES "^(i686|AMD64|amd64|x86_64|armv[0-9].*|arm64|aarch64)$")) + message(WARNING + "Target architecture \"${CMAKE_SYSTEM_PROCESSOR}\" is not supported in {Q/X}NNPACK. " + "Supported architectures are x86, x86-64, ARM, and ARM64. " diff --git a/misc/py-pytorch/files/patch-third__party_pthreadpool_src_pthreads.c b/misc/py-pytorch/files/patch-third__party_pthreadpool_src_pthreads.c new file mode 100644 index 000000000000..f6eb43b3e520 --- /dev/null +++ b/misc/py-pytorch/files/patch-third__party_pthreadpool_src_pthreads.c @@ -0,0 +1,24 @@ +-- Avoid including the non-existent on FreeBSD/aarch64. +-- On FreeBSD/DragonFly, alloca() is declared as a macro in only +-- for __GNUC__ >= 2, which clang on aarch64 does not satisfy in the way this +-- file expects (it falls through past the __GNUC__ branch above), leading it +-- to try '#include ', a header that does not exist on these +-- systems. Add an explicit branch for FreeBSD/DragonFly aarch64 that relies +-- on the declaration already brought in via . Scoped to aarch64 +-- only since other architectures build fine as-is. +-- See https://github.com/pytorch/pytorch/issues/144608 (related upstream +-- QNNPACK/FreeBSD tracking issue) for context on FreeBSD arm64 support gaps. + +--- third_party/pthreadpool/src/pthreads.c.orig 2026-09-02 17:44:00 UTC ++++ third_party/pthreadpool/src/pthreads.c +@@ -79,6 +79,10 @@ + #define alloca _alloca + #elif !defined(alloca) && defined(__GNUC__) + #define alloca(s) __builtin_alloca(s) ++#elif defined(__FreeBSD__) || defined(__DragonFly__) ++/* On FreeBSD/DragonFly aarch64, (included above) already defines ++ * alloca() as a macro for __GNUC__ >= 2, so the branch above is skipped, but ++ * there is no to fall back to. */ + #else + #include + #endif