chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
797 stars 130 forks source link

torchaudio #815

Open mhsmith opened 1 year ago

mhsmith commented 1 year ago

The discussion below is about building torchaudio using the Chaquopy package build tool. Since we currently only have torch available for Python 3.8, torchaudio will have to be built for the same version.

If you'd like to try building this package yourself, please read the existing discussion and then follow the instructions here. And if you're successful, please make a pull request so we can add the package to the public repository.

If anyone else wants this package too, let us know by clicking the thumbs-up button below.


Originally posted by @flamewave000 in https://github.com/chaquo/chaquopy/issues/773#issuecomment-1432689612

So I've gone through and tried to further but it seems that the chaquopy-libgfortran package is failing. The current dependency tree is as follows: demucs > torchaudio > torch > chaquopy-openblas > chaquopy-libgfortran.

The packages did not contain a version for torchaudio so I added it in the same way as lameenc. I installed gfortran but I've run into a couple of issues. Below are the steps I've taken so far.

  1. openblas requires gfortran 4.9, but the current version is 12.2.0. I updated the meta.yaml to version 12.2.0
  2. There are no $FC or $CC env variables set so the build.sh fails. I assume it means the Fortran and C compilers, so I've run export FC="/usr/bin/gfortran" && export CC="/usr/bin/gcc"
  3. The regex in build.sh is malformed for double digit versions. I modified it to sed -E 's/^GNU Fortran \(.*\) ([0-9]+(\.[0-9])+).*/\1/')
  4. The process tries to copy libgfortran.so.3 but that file and path do not exist. I changed the line to cp /usr/lib*/*/libgfortran.so.5 $PREFIX/lib
  5. libgfortran seems to compile ok
  6. Copy chaquopy_libgfortran-12.2.0-0-py3-none-android_21_x86.whl to dist/chaquopy-libgfortran.
  7. Update chaquopy-openblas requirement to chaquopy-libgfortran 12.2.0
  8. Try to compile chaquopy-openblas

At that point all hell breaks loose in the terminal. It seems like it gets into an infinite loop and then ending with the following error:

...
make[1]: /android-sdk/ndk/22.1.7171670/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android21-clangar: No such file or directory
make[1]: *** [../Makefile.tail:40: libs] Error 127
make[1]: Leaving directory '/home/flame/chaquopy/server/pypi/packages/chaquopy-openblas/build/0.2.20/py3-none-android_21_x86/src/interface'
make: *** [Makefile:139: libs] Error 1
build-wheel.py: Error: Command returned exit status 2

@BeMain were you able to get further than I did tonight?

mhsmith commented 1 year ago

You don't need to build, torch, chaquopy-openblas or chaquopy-libgfortran yourself, because they're already available in the public repository. I haven't looked at the torchaudio build, but it might not even need any of those packages at build time, in which case you don't need to to list them as requirements in the torchaudio meta.yaml file.

If you do need any of them at build time, it'll probably only be torch, in which case you can download its wheels from the repository into your server/pypi/dist directory before trying to build torchaudio.

mhsmith commented 1 year ago

A couple more notes:

flamewave000 commented 1 year ago

So I downloaded the wheel files for torch and tried to compile torchaudio. It fails with the following log: torchaudio output.txt

I changed the version I'm trying to compile from torchaudio 0.13.1 to torchaudio 0.4.0 as that is the version that aligns with the older version of torch 1.4

It looks like the issue occurs at line 8 in the setup.py

from torch.utils.cpp_extension import BuildExtension, CppExtension

The error it creates:

    Running command python setup.py egg_info
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/home/flame/chaquopy/server/pypi/packages/torchaudio/build/0.4.0/cp38-cp38-android_21_x86_64/src/setup.py", line 8, in <module>
        from torch.utils.cpp_extension import BuildExtension, CppExtension
      File "/home/flame/chaquopy/server/pypi/packages/torchaudio/build/0.4.0/cp38-cp38-android_21_x86_64/requirements/torch/__init__.py", line 81, in <module>
        from torch._C import *
    ImportError: /lib/x86_64-linux-gnu/libm.so: invalid ELF header

The error also seems to change if I try targeting arm64-v8a instead of x86_64

mhsmith commented 1 year ago

I changed the version I'm trying to compile from torchaudio 0.13.1 to torchaudio 0.4.0 as that is the version that aligns with the older version of torch 1.4

If you look at the repository again, you'll see it also contains a newer version of torch: 1.8.1. So I suggest you build whatever version of torchaudio corresponds to that instead.

For this particular error, it looks like the existing patch for torchvision will be a useful guide.

flamewave000 commented 1 year ago

So I've updated to torch 1.8.1 and torchaudio 0.8.1, and I attempted to patch the problem area with the following:

diff --git a/build_tools/setup_helpers/extension.py b/build_tools/setup_helpers/extension.py
index 3d3cb6d0..1a180633 100644
--- a/build_tools/setup_helpers/extension.py
+++ b/build_tools/setup_helpers/extension.py
@@ -6,7 +6,9 @@ import distutils.sysconfig

 from setuptools import Extension
 from setuptools.command.build_ext import build_ext
-import torch
+#import torch
+# Chaquopy: disabling torch import
+import os.path as _osp

 __all__ = [
     'get_ext_modules',
@@ -63,7 +65,7 @@ class CMakeBuild(build_ext):

         cmake_args = [
             f"-DCMAKE_BUILD_TYPE={cfg}",
-            f"-DCMAKE_PREFIX_PATH={torch.utils.cmake_prefix_path}",
+            f"-DCMAKE_PREFIX_PATH={_osp.join(_osp.dirname(_osp.dirname(_osp.dirname(_osp.dirname(__file__)))), 'requirements', 'torch', 'share', 'cmake')}",
             f"-DCMAKE_INSTALL_PREFIX={extdir}",
             '-DCMAKE_VERBOSE_MAKEFILE=ON',
             f"-DPython_INCLUDE_DIR={distutils.sysconfig.get_python_inc()}",

I looked at what the actual call to torch.utils.cmake_prefix_path was doing and just inlined it. Once I did that I no longer had errors around the python script, but now get errors around CMake.

error output.txt

BeMain commented 1 year ago

I tried this myself this morning and got as far as you did @flamewave000 I get a different error though, something that seems related to torch:

error.txt

mhsmith commented 1 year ago

@flamewave000: Your error is:

CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.

You should be able to fix that by installing the ninja tool, e.g. its Debian package name is ninja-build.

mhsmith commented 1 year ago

@BeMain: Your error is:

  CMake Error at /home/agardh/Development/chaquopy/server/pypi/packages/torchaudio/build/0.8.1/cp38-cp38-android_21_armeabi_v7a/requirements/torch/share/cmake/Caffe2/Caffe2Targets.cmake:115 (message):
    The imported target "c10" references the file

       "/home/agardh/Development/chaquopy/server/pypi/packages/torchaudio/build/0.8.1/cp38-cp38-android_21_armeabi_v7a/requirements/torch/lib/libc10.so"

    but this file does not exist.  Possible reasons include:

    * The file was deleted, renamed, or moved to another location.

    * An install or uninstall procedure did not complete successfully.

    * The installation package was faulty and contained

       "/home/agardh/Development/chaquopy/server/pypi/packages/torchaudio/build/0.8.1/cp38-cp38-android_21_armeabi_v7a/requirements/torch/share/cmake/Caffe2/Caffe2Targets.cmake"

    but not all the files it references.

  Call Stack (most recent call first):
    /home/agardh/Development/chaquopy/server/pypi/packages/torchaudio/build/0.8.1/cp38-cp38-android_21_armeabi_v7a/requirements/torch/share/cmake/Caffe2/Caffe2Config.cmake:116 (include)
    /home/agardh/Development/chaquopy/server/pypi/packages/torchaudio/build/0.8.1/cp38-cp38-android_21_armeabi_v7a/requirements/torch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
    CMakeLists.txt:55 (find_package)

I can reproduce this myself as well. The actual location of libc10.so is requirements/chaquopy/lib/libc10.so. It was moved there by the package_data patch in torch/patches/chaquopy.patch, but the CMake file wasn't updated to match. I guess this didn't affect torchvision because it used CMake in a different way.

It may be possible to work around this by patching Caffe2Targets.cmake to give the correct location, but it's been a long time since I looked at this area, so I can't give any more detailed advice.

BeMain commented 1 year ago

I'm guessing I will have to build torch myself then, to apply the patch? When trying to build torch I get the following error: ninja: error: '/bin/mkrename', needed by 'include/sleef.h', missing and no known rule to make it

Note: I downloaded the required wheels for chaquopy-openblas and numpy from https://chaquo.com/pypi-7.0/

mhsmith commented 1 year ago

Making this change while building torch itself will lead to many more complications, so I suggest you first try this: