No module named 'torchaudio.backend.common'

I am trying to use pytorch-nightly with a project on my macOS. pytorch stable can be used, but the results could be better. So, I wanted to give pytorch-nightly a try. If I run the same script using pytorch-nightly, I get the error:

...
ModuleNotFoundError: No module named 'torchaudio.backend.common'

Is this module for some reason not part of pytorch-nightly?

I tried to install via conda using

mamba create -n pytorch-nightly pytorch-nightly::pytorch pytorch-nightly::torchvision pytorch-nightly::torchaudio -c pytorch-nightly

and

mamba create -n pytorch-nightly pytorch-nightly::pytorch torchvision torchaudio -c pytorch-nightly

And I also tried via pip

pip3 install --pre torch torchvision torchaudio --index-url

Of course, I used a fresh environment everytime. I just executed import torchaudio.backend.common in the interactive python console. With every pytorch-nightly env I get the ModuleNotFoundError. Doing the same with just pytorch works.

Any ideas?

This PR moved some modules around to _backend.
However, torchaudio.backend.common still works for me in 2.1.0.dev20230817+cu121, which should already contain the linked PR:

torchaudio.backend.common
<module 'torchaudio.backend._common' from '/home/pbialecki/miniforge3/envs/nightly_pip_cu121/lib/python3.10/site-packages/torchaudio/backend/_common.py'>

torchaudio.__version__
'2.1.0.dev20230817+cu121'

Hmm, that’s weird.
I get (almost) the same output as you:

>>> torchaudio.backend.common
<module 'torchaudio.backend._common' from '/Users/gernophil/miniconda3/envs/pytorch-nightly/lib/python3.11/site-packages/torchaudio/backend/_common.py'>
>>> torchaudio.__version__
'2.1.0.dev20230823'

Still I cannot run this import command:

from torchaudio.backend.common import AudioMetaData

It’s used by a pyannote fork I use and is does work with the non-nightly version.

However, by importing tfrom torchaudio.backend._common import AudioMetaData I got the message that it’s been moved to torchaudio.AudioMetaData. Should be doable now :).

Try to replace the backend module with _backend as described in the PR and see if this would work.

This is a regression. I intended to keep the existing import compatible. I will fix it.

For the future, if the backward compatibility is not an issue, please use torchaudio.AudioMetaData. (no backend or _backend)

1 Like

Has the package changed after the release of 2.1.0?
I ran my script using 2.1.0 (installed via pip in a fresh venv) on Oct 6th and got the following error:
No module named 'torchaudio.backend.common'
Yesterday I installed it again in a fresh venv (same version 2.1.0) and now I don’t get this error anymore, but only a warning.
How can this happen using the same version? Are there minor changes after the release?

How can this happen using the same version?

It should not happen, if it is really a 2.1.0 release. The only way that it happens, which I can think of, is that an old nightly build was present in the environment.

Are there minor changes after the release?

No, there wasn’t. We are preparing 2.1.1 minor release, but we don’t change what’s released.

The latest torchaudio==2.1.0+cu121 release also still works for me:

import torchaudio

torchaudio.__version__
Out[10]: '2.1.0+cu121'

torchaudio.AudioMetaData
Out[11]: torchaudio._backend.common.AudioMetaData

from torchaudio._backend.common import AudioMetaData

AudioMetaData
Out[13]: torchaudio._backend.common.AudioMetaData

That’s really weird then. I thought I freshly created the venv. I must have accidentally used a wrong venv. Who knows. Took me almost a month till I realized I can actually use torchaudio 2.1.0 with my script :sweat_smile:. Thanks for the great work by the way. MPS works so much better using 2.1.0.

Ok, seems like it’s not that weird. I just started my script in an x86_64 venv (torchaudio 2.1.0 installed at the same hour as in the arm64 venv). In this venv I still get the No module named 'torchaudio.backend.common' error. Is it possible that there is a difference between the macOS arm64 and the x86_64 package in that point?

there is indeed a difference between the torchaudio package for arm64 and x86_64:

# arm64
Python 3.9.18 (main, Aug 24 2023, 18:16:58) 
[Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchaudio.backend.common
>>> import torch
>>> print(torch.__version__)
2.1.0
>>> import torchaudio
>>> print(torchaudio.__version__)
2.1.0

# x86_64
Python 3.9.18 (main, Aug 24 2023, 18:16:58) 
[Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchaudio.backend.common
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torchaudio.backend.common'
>>> import torch
>>> print(torch.__version__)
2.1.0
>>> import torchaudio
>>> print(torchaudio.__version__)
2.1.0

I have a very similar issue.
I’m trying to run yolov7 and I keep getting this error:

Traceback (most recent call last):
  File "/media/aml/happy/ubuntu dowenloads/yolov7-main/detect.py", line 7, in <module>
    import torch.backends.cudnn as cudnn
ModuleNotFoundError: No module named 'torch.backends'

I have torch installed and I can import it with no problem but it doesn’t recognize ‘torch.backends’

if you found a solution for your issue, please share it, maybe it can help me.

The torchaudio issue is most likely unrelated to yours. Which PyTorch version are you using?

Thank you very much for your response.
I can’t access torch.version

>>> import torch
>>> print(torch.__version__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute '__version__'
>>> print(torch.version.__version__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'torch' has no attribute 'version'

I think I might have installed the wrong torch?!

Yes, maybe you’ve installed the wrong package or you might also be in a working directory containing a script called torch.py. Avoid these name conflicts or switch to another directory.