Skip to content

Bug: UnboundLocalError on torchaudio in reference_loader.py with torchaudio < 2.9 #1241

@Idiotabtcodes

Description

@Idiotabtcodes

Self Checks

  • This template is only for bug reports. For questions, please visit Discussions.
  • I have thoroughly reviewed the project documentation (installation, training, inference) but couldn't find information to solve my problem. English 中文 日本語 Portuguese (Brazil)
  • I have searched for existing issues, including closed ones. Search issues
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template and fill in all required fields.

Cloud or Self Hosted

Self Hosted (Source)

Environment Details

  • Fish Speech: latest main (commit hash: 0def6f3)
  • torchaudio: 2.8.0
  • Python: 3.12

Steps to Reproduce

Description:
api_server.py fails on startup with:

File "fish_speech/inference_engine/reference_loader.py", line 39, in __init__
    backends = torchaudio.list_audio_backends()
               ^^^^^^^^^^
UnboundLocalError: cannot access local variable 'torchaudio' where it is not associated with a value

Likely Cause:

In ReferenceLoader.__init__, line 49 has:

import torchaudio.io._load_audio_fileobj

Even though this line is inside a nested try/except block that only executes when torchaudio >= 2.9, Python's compiler sees the import torchaudio... statement and marks torchaudio as a local variable for the entire method. This shadows the top-level import torchaudio on line 8, causing the UnboundLocalError on line 39 before any local assignment happens.

Fix:

Change line 49 from:

import torchaudio.io._load_audio_fileobj  # noqa: F401

to:

from torchaudio.io import _load_audio_fileobj  # noqa: F401

✔️ Expected Behavior

No response

❌ Actual Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions