google-ai-edge / ai-edge-torch

Supporting PyTorch models with the Google AI Edge TFLite runtime.
Apache License 2.0
199 stars 23 forks source link

Cannot get ai-edge-tool running on WSL2 Ubuntu #74

Open BmanClark opened 1 week ago

BmanClark commented 1 week ago

Description of the bug:

Attempt 1

Initially I tried to just install ai-edge-torch and its dependencies on a clean python 3.9 as needed rather than thru a virtual environment + requirements.txt. In this case I had issues with it wanting to install a particular version of torch_xla. The latest available version gives me: ImportError: WRONG PACKAGE. Please install the package from Neuron Repository - [pip.repos.neuron.amazonaws.com](http://pip.repos.neuron.amazonaws.com/) but if I try and install from that repository it says: WARNING: Url '[pip.repos.neuron.amazonaws.com/torch-xla/](http://pip.repos.neuron.amazonaws.com/torch-xla/)' is ignored. It is either a non-existing path or lacks a specific scheme. I can see the path if I go to the location, so it exists, but fails to be recognised as a repository. So that failed.

Attempt 2

Following your current front page instruction with the virtual env + requirements, when doing pip install -r https://github.com/google-ai-edge/ai-edge-torch/releases/download/v0.1.1/requirements.txt I get errors:

  Building wheel for gast (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /mnt/c/src/MobileSAM/conversion/venv/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-k22hz1_l/gast/setup.py'"'"'; __file__='"'"'/tmp/pip-install-k22hz1_l/gast/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-5ny3jggt
       cwd: /tmp/pip-install-k22hz1_l/gast/
  Complete output (6 lines):
  usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
     or: setup.py --help [cmd1 cmd2 ...]
     or: setup.py --help-commands
     or: setup.py cmd --help

  error: invalid command 'bdist_wheel'
  ----------------------------------------
  ERROR: Failed building wheel for gast
  Running setup.py clean for gast
Failed to build gast
ERROR: tf-nightly 2.17.0.dev20240509 has requirement numpy<2.0.0,>=1.23.5; python_version <= "3.11", but you'll have numpy 2.0.0 which is incompatible.
ERROR: google-api-core 1.34.1 has requirement protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<4.0.0dev,>=3.19.5, but you'll have protobuf 4.25.3 which is incompatible.

pip install gast tells me gast is in fact installed, and pip install ai-edge-torch==0.1.1 works. However, when trying to use it I initially get errors about numpy version (as per above, it has installed 2.0). Installing numpy 1.26 fixes these, but then I hit:

2024-07-02 13:12:48.863019: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
  File "/mnt/c/src/MobileSAM/conversion/aiEdgeConvert.py", line 3, in <module>
    import ai_edge_torch
  File "/mnt/c/src/MobileSAM/conversion/venv/lib/python3.9/site-packages/ai_edge_torch/__init__.py", line 16, in <module>
    from .convert.converter import convert
  File "/mnt/c/src/MobileSAM/conversion/venv/lib/python3.9/site-packages/ai_edge_torch/convert/converter.py", line 22, in <module>
    from ai_edge_torch import model
  File "/mnt/c/src/MobileSAM/conversion/venv/lib/python3.9/site-packages/ai_edge_torch/model.py", line 28, in <module>
    from ai_edge_torch.convert import conversion_utils as cutils
  File "/mnt/c/src/MobileSAM/conversion/venv/lib/python3.9/site-packages/ai_edge_torch/convert/conversion_utils.py", line 25, in <module>
    from torch_xla import stablehlo
  File "/mnt/c/src/MobileSAM/conversion/venv/lib/python3.9/site-packages/torch_xla/__init__.py", line 7, in <module>
    import _XLAC
ImportError: libpython3.9.so.1.0: cannot open shared object file: No such file or directory

Actual vs expected behavior:

Hits an error rather than completing and giving a converted model.

Any other information you'd like to share?

I'm on Ubuntu WSL2 within Windows, but generally that works for most things including any python tasks.

pkgoogle commented 1 week ago

Hi @BmanClark, can you try updating Python to 3.11 and then restarting the installation process? I have noticed better stability with that version of Python than the others.

chunnienc commented 1 week ago

Hi @BmanClark , please install ai-edge-torch with the way you tried in attempt 2. Our release is not tested with torch_xla built by aws, please install dependencies through our requirements.txt.

For your error in attempt 2 ImportError: libpython3.9.so.1.0: cannot open shared object file: No such file or directory, please set the environment variable LD_LIBRARY_PATH properly with the path your python is installed. These threads may help your env setup:

You can also use docker to run ai-edge-torch in clean host and python environment.

BmanClark commented 5 days ago

So... @chunnienc I've done a search and libpython3.9.so does not seem to be on my system. I tried adding everywhere python was installed to the LD_LIBRARY_PATH, and it didn't help. Unsure why I'm missing that .so (which by its name seems important!), but python 3.9 seems to work generally, and I don't see how I get past that issue to get ai-edge-tool running on python 3.9.

I was unfamiliar with pyenv (as used in one of the 3.9 solutions), using update-alternatives for my python version management, but it turns out to be useful(ish) for getting python 3.11 onto my Ubuntu 20.04 WSL @pkgoogle. 20.04 doesn't have 3.11 available through normal package managers, but pyenv is kind of able to install it, but then I'm having to install a bunch of other things first (and re-install) to get it to work properly. I've got far enough that it now seems to be having issues with what I'm trying to convert rather than the tool (hopefully just need to install more 3.11 libraries), but I've had to shelve it until next week to get on with other things. Python library-wise I only seemed to need to add timm after the requirements (and now project-specific things, but that's on me).

That's a progress update - I'll get back if I succeed with 3.11 / hit other issues. If there's something else I should have done for 3.9, please say.

pkgoogle commented 1 day ago

Hi @BmanClark, as @chunnienc said please follow the 2nd process. It seems that the .so library isn't included by default on Ubuntu WSL2 when installing python. I think you will be able to install it with this:

sudo apt-get install python3.11-dev

or

sudo apt-get install python3.9-dev

If reinstalling either version:

sudo apt-get install python3.11
sudo apt-get install python3.11-dev

or

sudo apt-get install python3.9
sudo apt-get install python3.9-dev

I was able to reproduce your issue and fix it with the above by testing on a consumer Windows Setup. Let us know if somehow it still doesn't work for you. Thanks.