1

I have a .py script that uses torchaudio (without GPU) to process some sound in Windows. To distribute it, I've used pyinstaller to turn it into a .exe. You can reproduce the issue with this simple script:

import torchaudio
import time

if __name__ == '__main__':
    t = torchaudio.transforms
    time.sleep(3)
    print("Success")

This script correctly runs from a python console python test.py but I want to create a test.exe that works in Windows (without having python installed). I create test.exe by using pyinstaller: pyinstaller test.py. This creates a build/test folder with all the required dependencies (around 1GB). test.exe is located inside that folder but when I double click on it, it fails with the following error:

Traceback (most recent call last):
  File "torch\_ops.py", line 501, in __getattr__
    op, overload_names = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator torchaudio::cuda_version

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "torchaudio\__init__.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
  File "torchaudio\_extension.py", line 136, in <module>
  File "torchaudio\_extension.py", line 121, in _check_cuda_version
  File "torch\_ops.py", line 505, in __getattr__
    raise AttributeError(
AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'
[11648] Failed to execute script 'test' due to unhandled exception!

The environment uses:

python==3.9.15
torch==1.13.0
six==1.15.0
numpy==1.22.4
scipy==1.6.0
sounddevice==0.4.5
torchaudio==0.13.0
pyinstaller==5.6.2

Note: I tried the same installing torch with cuda ending up with the same error and a build 4 times bigger.

ronkov
  • 1,263
  • 9
  • 14
  • I was able to get your tiny example script to work but I am not sure if what I did will actually make the whole application run is there another app that I can test it on? – Alexander Nov 17 '22 at 03:42
  • @Alexander thanks for the try. The idea is to have the test.py script that works with python: `python test.py` but then I want to create a test.exe that works in Windows (without having python installed). The test.exe is created using `pyinstaller test.py`, which creates a `build/test` folder, and test.exe is located inside that folder. When I double click on the .exe, it fails. – ronkov Nov 17 '22 at 09:53
  • I understand that... I was able to get it to work. However I am skeptical that just because it works on your small sample script it won't necessarily work on a more complex program using the same dependencies – Alexander Nov 17 '22 at 09:54
  • Well the idea is to make the simplest script to reproduce the error. The error is simply that I cannot use `torchaudio` together with `pyinstaller`, it doesn't import it correctly. – ronkov Nov 17 '22 at 10:00
  • I know... I am telling you that I was able to get it to work... I compiled the your sample script and the executable ran without causing a traceback or error and successfully output the success message. I am asking if you have a script that actually involves more than just importing them, so I can see if my method works for it as well? – Alexander Nov 17 '22 at 10:02
  • Not really, everything is inside the script, so maybe I should try to rebuild the environment or try in a different computer... just to check, you used the same versions of python, torch, etc., right? – ronkov Nov 17 '22 at 11:07
  • Yah I had to hack a few things around though to make it work... Ill write out a step by step when I get a chance as an answer – Alexander Nov 17 '22 at 11:09
  • Ok, so that should be it, thanks in advance – ronkov Nov 17 '22 at 11:16
  • How important is it that you use v1.6.0 for scipy? would a more recent version work? – Alexander Nov 17 '22 at 12:28
  • It's not so important, I'm trying the solution – ronkov Nov 18 '22 at 11:50

2 Answers2

1

I was able to make the script work. Here are the steps I took to get it to run.

  1. Create a new empty directory and pasted your script in as main.py

  2. py -m venv venv && venv\scripts\activate && py -m pip install --upgrade pip pyinstaller

  3. pip install torchaudio==0.13.0 torch==1.13.0 numpy=1.22.4 sounddevice==0.4.5 six==1.15.0 scipy

  4. pyinstaller -F main.py

  5. Go into venv\Lib\site-packages and copy the entire torchaudio folder and paste it into the top level directory alongside venv and main.py

  6. In main.spec set datas=[('./torchaudio','./torchaudio')]

  7. pyinstaller main.spec

And after compiling the executable runs... it still gives off a few warnings, but it runs and prints the success message.

Alexander
  • 16,091
  • 5
  • 13
  • 29
0

AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'

That can happen if your CWD is the torchaudio repo.

serg06
  • 2,027
  • 1
  • 18
  • 26