Skip to content

Load the transcription stack on first use - #1480

Open
Kayvan-Zahiri wants to merge 1 commit into
SYSTRAN:masterfrom
Kayvan-Zahiri:fix/1479-lazy-transcribe-import
Open

Load the transcription stack on first use#1480
Kayvan-Zahiri wants to merge 1 commit into
SYSTRAN:masterfrom
Kayvan-Zahiri:fix/1479-lazy-transcribe-import

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

Closes #1479.

Python runs faster_whisper/__init__.py before any submodule, so
from faster_whisper.vad import get_vad_model imported
faster_whisper.transcribe and ctranslate2 along with it. The VAD needs
numpy, one helper from utils, and onnxruntime, none of the rest.

WhisperModel and BatchedInferencePipeline now resolve through a PEP 562
module __getattr__, which is the shape the issue suggested and the same one
already merged as OpenNMT/CTranslate2#2080.

Measured here on Python 3.12:

before   6.96s    ctranslate2 and faster_whisper.transcribe loaded
after    0.14s    neither loaded

Nothing about the public API changes. from faster_whisper import WhisperModel,
faster_whisper.WhisperModel, and from faster_whisper.transcribe import WhisperModel all still work, dir() still lists both names, and an unknown
attribute still raises AttributeError. There are tests for each of those, plus
one asserting that the stack does load on first use, so the laziness cannot
quietly turn into a missing import.

The import assertions run in a subprocess, since sys.modules is already
populated inside a pytest session.

black, isort and flake8 are clean. Suite goes from 19 to 24 passing.

Python runs faster_whisper/__init__.py before any submodule, so
`from faster_whisper.vad import get_vad_model` imported
faster_whisper.transcribe and ctranslate2 along with it. The VAD needs numpy,
one helper from utils and onnxruntime, none of the rest.

WhisperModel and BatchedInferencePipeline now resolve through a PEP 562
module __getattr__, the same shape as the already-merged
OpenNMT/CTranslate2#2080. Importing either still works, from the package or as
an attribute, and an unknown name still raises AttributeError.

Measured here, python 3.12:

    before   6.96s   ctranslate2 and faster_whisper.transcribe loaded
    after    0.14s   neither loaded

Closes SYSTRAN#1479

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DnFJxiG7r2xeshM4G8rm3y
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.

Importing faster_whisper.vad (or any submodule) eagerly pulls in the full Whisper/ctranslate2/transformers stack via __init__.py

1 participant