Cannot create .exe with pyinstaller from .py with torchaudio (CPU): AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'

Question:

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 "PyInstallerloaderpyimod02_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 "PyInstallerloaderpyimod02_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.

Asked By: ronkov

||

Answers:

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 && venvscriptsactivate && 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 venvLibsite-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.

Answered By: Alexander

AttributeError: ‘_OpNamespace’ ‘torchaudio’ object has no attribute ‘cuda_version’

That can happen if your CWD is the torchaudio repo.

Answered By: serg06