Skip to content

fix: lazy load top-level AutoModel exports#3290

Merged
LauraGPT merged 1 commit into
mainfrom
codex/lazy-top-level-import-without-torch
Jul 19, 2026
Merged

fix: lazy load top-level AutoModel exports#3290
LauraGPT merged 1 commit into
mainfrom
codex/lazy-top-level-import-without-torch

Conversation

@LauraGPT

Copy link
Copy Markdown
Collaborator

Summary

Fixes a first-install onboarding blocker found while smoke-testing the newly published funasr==1.3.20 wheel in a fresh environment.

pip install funasr==1.3.20 succeeds, but because the package intentionally does not install a platform-specific PyTorch build, import funasr immediately fails at the top-level from funasr.auto.auto_model import AutoModel import.

This PR keeps the current dependency strategy and makes the top-level exports lazy instead:

  • import funasr and funasr.__version__ work without torch;
  • from funasr import AutoModel and AutoFrontend still resolve normally when dependencies are present;
  • if torch is missing, accessing AutoModel raises a clear PyTorch install hint;
  • bumps the package to 1.3.21 and records the fix in EN/ZH README release notes.

Validation

  • python3 -m pytest tests/test_top_level_import.py tests/test_cli.py -q -> 5 passed
  • python3 -m compileall -q funasr/__init__.py tests/test_top_level_import.py
  • git diff --check
  • python3 -m build
  • twine check dist/* -> wheel and sdist passed
  • Installed dist/funasr-1.3.21-py3-none-any.whl with --no-deps in a fresh venv: import funasr reports 1.3.21; from funasr import AutoModel raises the expected torch install hint.
  • Installed the same wheel with normal dependencies in a fresh venv: import funasr reports 1.3.21; accessing AutoModel raises the expected torch install hint because torch is still not part of install_requires.

@LauraGPT
LauraGPT merged commit ac07ca2 into main Jul 19, 2026
1 check passed
@LauraGPT
LauraGPT deleted the codex/lazy-top-level-import-without-torch branch July 19, 2026 10:27

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces lazy loading for AutoModel and AutoFrontend in funasr/__init__.py to allow importing the package and checking its version without requiring PyTorch. A test has been added to verify this behavior. The reviewer suggested making the PyTorch missing error message dynamic to correctly reference either AutoModel or AutoFrontend depending on which attribute was accessed.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread funasr/__init__.py
Comment on lines +91 to +99
if error.name == "torch":
raise ModuleNotFoundError(
"FunASR requires PyTorch before using AutoModel. Install a "
"platform-appropriate torch build first, for example: "
"`python -m pip install torch torchaudio --index-url "
"https://download.pytorch.org/whl/cu126` for CUDA 12.6, "
"or follow https://pytorch.org/get-started/locally/.",
name="torch",
) from error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message is currently hardcoded to mention AutoModel. Since _LAZY_EXPORTS also contains AutoFrontend, attempting to import AutoFrontend when PyTorch is missing will raise an error message that confusingly refers to AutoModel.

We can make this dynamic by using the name parameter passed to __getattr__ to ensure the error message is accurate for whichever component is being imported.

Suggested change
if error.name == "torch":
raise ModuleNotFoundError(
"FunASR requires PyTorch before using AutoModel. Install a "
"platform-appropriate torch build first, for example: "
"`python -m pip install torch torchaudio --index-url "
"https://download.pytorch.org/whl/cu126` for CUDA 12.6, "
"or follow https://pytorch.org/get-started/locally/.",
name="torch",
) from error
if error.name == "torch":
raise ModuleNotFoundError(
f"FunASR requires PyTorch before using {name}. Install a "
"platform-appropriate torch build first, for example: "
"'python -m pip install torch torchaudio --index-url "
"https://download.pytorch.org/whl/cu126' for CUDA 12.6, "
"or follow https://pytorch.org/get-started/locally/.",
name="torch",
) from error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant