diff --git a/packages/python-torch/Macros.h.patch b/packages/python-torch/Macros.h.patch new file mode 100644 index 0000000000..23ea6c4be0 --- /dev/null +++ b/packages/python-torch/Macros.h.patch @@ -0,0 +1,12 @@ +diff -uNr pytorch/c10/macros/Macros.h pytorch.mod/c10/macros/Macros.h +--- pytorch/c10/macros/Macros.h 2022-10-04 19:55:33.000000000 +0900 ++++ pytorch.mod/c10/macros/Macros.h 2022-10-04 23:41:33.513647184 +0900 +@@ -390,7 +390,7 @@ + #include + #endif + +-#if defined(__ANDROID__) ++#if defined(__ANDROID__) && !defined(__TERMUX__) + #define C10_ANDROID 1 + #define C10_MOBILE 1 + #elif ( \ diff --git a/packages/python-torch/build.sh b/packages/python-torch/build.sh new file mode 100644 index 0000000000..bb3ee7c6eb --- /dev/null +++ b/packages/python-torch/build.sh @@ -0,0 +1,97 @@ +TERMUX_PKG_HOMEPAGE=https://pytorch.org/ +TERMUX_PKG_DESCRIPTION="Tensors and Dynamic neural networks in Python" +TERMUX_PKG_LICENSE="BSD 3-Clause" +TERMUX_PKG_MAINTAINER="@termux" +TERMUX_PKG_VERSION=1.12.1 +TERMUX_PKG_SRCURL=https://github.com/pytorch/pytorch.git +TERMUX_PKG_DEPENDS="python, python-numpy, libopenblas, libprotobuf, libzmq, ffmpeg, opencv" +TERMUX_PKG_HOSTBUILD=true + +TERMUX_PKG_RM_AFTER_INSTALL="lib/pkgconfig/sleef.pc" + +termux_step_post_get_source() { + termux_setup_cmake +} + +termux_step_host_build() { + cmake "$TERMUX_PKG_SRCDIR/third_party/sleef" + make -j "$TERMUX_MAKE_PROCESSES" mkrename mkrename_gnuabi mkmasked_gnuabi mkalias mkdisp +} + +termux_step_pre_configure() { + _PYTHON_VERSION=$(. $TERMUX_SCRIPTDIR/packages/python/build.sh; echo $_MAJOR_VERSION) + + if [ "$TERMUX_ON_DEVICE_BUILD" = "false" ]; then + termux_setup_python_crossenv + pushd $TERMUX_PYTHON_CROSSENV_SRCDIR + _CROSSENV_PREFIX=$TERMUX_PKG_BUILDDIR/python-crossenv-prefix + python${_PYTHON_VERSION} -m crossenv \ + $TERMUX_PREFIX/bin/python${_PYTHON_VERSION} \ + ${_CROSSENV_PREFIX} + popd + . ${_CROSSENV_PREFIX}/bin/activate + + build-pip install -U pyyaml numpy typing_extensions + fi + + pip install -U typing_extensions + + termux_setup_protobuf + + find "$TERMUX_PKG_SRCDIR" -name CMakeLists.txt -o -name '*.cmake' | \ + xargs -n 1 sed -i \ + -e 's/\([^A-Za-z0-9_]ANDROID\)\([^A-Za-z0-9_]\)/\1_NO_TERMUX\2/g' \ + -e 's/\([^A-Za-z0-9_]ANDROID\)$/\1_NO_TERMUX/g' + + LDFLAGS+=" -llog -lpython${_PYTHON_VERSION}" + + TERMUX_PKG_EXTRA_CONFIGURE_ARGS=" + -DBUILD_PYTHON=ON + -DBUILD_TEST=OFF + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_INSTALL_PREFIX=${TERMUX_PKG_SRCDIR}/torch + -DCMAKE_PREFIX_PATH=${TERMUX_PREFIX}/lib/python${_PYTHON_VERSION}/site-packages + -DNUMPY_INCLUDE_DIR=$( echo ${TERMUX_PREFIX}/lib/python${_PYTHON_VERSION}/site-packages/*/numpy/core/include ) + -DPYTHON_EXECUTABLE=$(command -v python3) + -DPYTHON_INCLUDE_DIR=${TERMUX_PREFIX}/include/python${_PYTHON_VERSION} + -DPYTHON_LIBRARY=${TERMUX_PREFIX}/lib//libpython${_PYTHON_VERSION}.so + -DTORCH_BUILD_VERSION=${TERMUX_PKG_VERSION} + -DUSE_NUMPY=ON + -DUSE_OPENCV=ON + -DUSE_FFMPEG=ON + -DUSE_ZMQ=ON + -DANDROID_NO_TERMUX=OFF + -DOpenBLAS_INCLUDE_DIR=${TERMUX_PREFIX}/include/openblas + -DNATIVE_BUILD_DIR=${TERMUX_PKG_HOSTBUILD_DIR} + -DBUILD_CUSTOM_PROTOBUF=OFF + -DPROTOBUF_PROTOC_EXECUTABLE=$(command -v protoc) + -DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=$(command -v protoc) + -DCXX_AVX512_FOUND=OFF + -DCXX_AVX2_FOUND=OFF + " + + # to build x86_64 version anyway + # /home/builder/.termux-build/python-torch/src/third_party/fbgemm/third_party/asmjit/src/asmjit/core/../core/operand.h:910:79: error: use of bitwise '&' with boolean operands [-Werror,-Wbitwise-instead-of-logical] + TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" + -DCAFFE2_COMPILER_SUPPORTS_AVX512_EXTENSIONS=OFF + " + # /home/builder/.termux-build/python-torch/src/torch/csrc/jit/codegen/onednn/graph_helper.h:3:10: fatal error: 'oneapi/dnnl/dnnl_graph.hpp' file not found + TERMUX_PKG_EXTRA_CONFIGURE_ARGS+=" + -DUSE_MKLDNN=OFF + " + + # /home/builder/.termux-build/_cache/android-r25b-api-24-v0/sysroot/usr/include/linux/types.h:21:10: fatal error: 'asm/types.h' file not found + ln -s ${TERMUX_STANDALONE_TOOLCHAIN}/sysroot/usr/include/${TERMUX_ARCH}-linux-android$( if test $TERMUX_ARCH = arm; then echo eabi; fi )/asm + + ln -s "$TERMUX_PKG_BUILDDIR" build +} + +termux_step_make_install() { + pip -v install --prefix $TERMUX_PREFIX "$TERMUX_PKG_SRCDIR" + ln -s ${TERMUX_PREFIX}/lib/python3.10/site-packages/torch/lib/*.so ${TERMUX_PREFIX}/lib +} + +termux_step_create_debscripts() { + echo "#!$TERMUX_PREFIX/bin/sh" > postinst + echo "pip3 install typing_extensions" >> postinst +} diff --git a/packages/python-torch/convert-to-long.patch b/packages/python-torch/convert-to-long.patch new file mode 100644 index 0000000000..63626ac4c3 --- /dev/null +++ b/packages/python-torch/convert-to-long.patch @@ -0,0 +1,33 @@ +diff -uNr pytorch/torch/csrc/autograd/python_function.cpp pytorch.mod/torch/csrc/autograd/python_function.cpp +--- pytorch/torch/csrc/autograd/python_function.cpp 2022-10-03 21:59:02.604422931 +0900 ++++ pytorch.mod/torch/csrc/autograd/python_function.cpp 2022-10-03 23:39:51.207871384 +0900 +@@ -448,7 +448,7 @@ + } else { + throw torch::TypeError( + "save_for_backward can only save variables, but argument %ld is of " +- "type %s", i, Py_TYPE(obj)->tp_name); ++ "type %s", (long)(i), Py_TYPE(obj)->tp_name); + } + } + // Free .to_save +diff -uNr pytorch/torch/csrc/utils/python_arg_parser.h pytorch.mod/torch/csrc/utils/python_arg_parser.h +--- pytorch/torch/csrc/utils/python_arg_parser.h 2022-10-03 19:00:05.000000000 +0900 ++++ pytorch.mod/torch/csrc/utils/python_arg_parser.h 2022-10-03 23:39:46.263853740 +0900 +@@ -423,7 +423,7 @@ + } catch (const std::exception &e) { + throw TypeError("%s(): argument '%s' must be %s, but found element of type %s at pos %ld", + signature.name.c_str(), signature.params[i].name.c_str(), +- signature.params[i].type_name().c_str(), Py_TYPE(obj)->tp_name, idx + 1); ++ signature.params[i].type_name().c_str(), Py_TYPE(obj)->tp_name, (long)(idx + 1)); + } + } + return res; +@@ -450,7 +450,7 @@ + } catch (const std::exception &e) { + throw TypeError("%s(): argument '%s' must be %s, but found element of type %s at pos %ld", + signature.name.c_str(), signature.params[i].name.c_str(), +- signature.params[i].type_name().c_str(), Py_TYPE(obj)->tp_name, idx + 1); ++ signature.params[i].type_name().c_str(), Py_TYPE(obj)->tp_name, (long)(idx + 1)); + } + } + return res; diff --git a/packages/python-torch/init_flatbuffer_module.cpp.patch b/packages/python-torch/init_flatbuffer_module.cpp.patch new file mode 100644 index 0000000000..8b870c48e0 --- /dev/null +++ b/packages/python-torch/init_flatbuffer_module.cpp.patch @@ -0,0 +1,12 @@ +diff -uNr pytorch-v1.12.1/torch/csrc/init_flatbuffer_module.cpp pytorch-v1.12.1.mod/torch/csrc/init_flatbuffer_module.cpp +--- pytorch-v1.12.1/torch/csrc/init_flatbuffer_module.cpp 2022-08-06 04:37:12.000000000 +0900 ++++ pytorch-v1.12.1.mod/torch/csrc/init_flatbuffer_module.cpp 2022-09-05 00:27:22.542774737 +0900 +@@ -27,7 +27,7 @@ + std::shared_ptr bytes_copy( + static_cast(_aligned_malloc(size, FLATBUFFERS_MAX_ALIGNMENT)), + _aligned_free); +-#elif defined(__APPLE__) ++#elif defined(__APPLE__) || defined(__ANDROID__) + void* p; + ::posix_memalign(&p, FLATBUFFERS_MAX_ALIGNMENT, size); + TORCH_INTERNAL_ASSERT(p, "Could not allocate memory for flatbuffer"); diff --git a/packages/python-torch/manager.cpp.patch b/packages/python-torch/manager.cpp.patch new file mode 100644 index 0000000000..664151c0c0 --- /dev/null +++ b/packages/python-torch/manager.cpp.patch @@ -0,0 +1,68 @@ +diff -uNr pytorch-v1.12.1/torch/lib/libshm/manager.cpp pytorch-v1.12.1.mod/torch/lib/libshm/manager.cpp +--- pytorch-v1.12.1/torch/lib/libshm/manager.cpp 2022-08-06 04:37:12.000000000 +0900 ++++ pytorch-v1.12.1.mod/torch/lib/libshm/manager.cpp 2022-09-05 00:26:25.218153450 +0900 +@@ -25,6 +25,65 @@ + #define DEBUG(...) (void)0 + #endif + ++static int shm_unlink(const char *name) { ++ size_t namelen; ++ char *fname; ++ ++ /* Construct the filename. */ ++ while (name[0] == '/') ++name; ++ ++ if (name[0] == '\0') { ++ /* The name "/" is not supported. */ ++ errno = EINVAL; ++ return -1; ++ } ++ ++ namelen = strlen(name); ++ fname = (char *) alloca(sizeof("@TERMUX_PREFIX@/tmp/") - 1 + namelen + 1); ++ memcpy(fname, "@TERMUX_PREFIX@/tmp/", sizeof("@TERMUX_PREFIX@/tmp/") - 1); ++ memcpy(fname + sizeof("@TERMUX_PREFIX@/tmp/") - 1, name, namelen + 1); ++ ++ return unlink(fname); ++} ++ ++static int shm_open(const char *name, int oflag, mode_t mode) { ++ size_t namelen; ++ char *fname; ++ int fd; ++ ++ /* Construct the filename. */ ++ while (name[0] == '/') ++name; ++ ++ if (name[0] == '\0') { ++ /* The name "/" is not supported. */ ++ errno = EINVAL; ++ return -1; ++ } ++ ++ namelen = strlen(name); ++ fname = (char *) alloca(sizeof("@TERMUX_PREFIX@/tmp/") - 1 + namelen + 1); ++ memcpy(fname, "@TERMUX_PREFIX@/tmp/", sizeof("@TERMUX_PREFIX@/tmp/") - 1); ++ memcpy(fname + sizeof("@TERMUX_PREFIX@/tmp/") - 1, name, namelen + 1); ++ ++ fd = open(fname, oflag, mode); ++ if (fd != -1) { ++ /* We got a descriptor. Now set the FD_CLOEXEC bit. */ ++ int flags = fcntl(fd, F_GETFD, 0); ++ flags |= FD_CLOEXEC; ++ flags = fcntl(fd, F_SETFD, flags); ++ ++ if (flags == -1) { ++ /* Something went wrong. We cannot return the descriptor. */ ++ int save_errno = errno; ++ close(fd); ++ fd = -1; ++ errno = save_errno; ++ } ++ } ++ ++ return fd; ++} ++ + struct ClientSession { + ClientSession(ManagerSocket s) : socket(std::move(s)), pid(0) {}