Skip to content

perf(whisper): lazy-import timing module to avoid loading scipy/numba#1413

Open
YashD5291 wants to merge 1 commit intoml-explore:mainfrom
YashD5291:lazy-timing-imports
Open

perf(whisper): lazy-import timing module to avoid loading scipy/numba#1413
YashD5291 wants to merge 1 commit intoml-explore:mainfrom
YashD5291:lazy-timing-imports

Conversation

@YashD5291
Copy link
Copy Markdown

Summary

  • Move from .timing import add_word_timestamps from module-level to inside the if word_timestamps: guard in transcribe()
  • Makes scipy and numba truly optional when word_timestamps=False (the default)

Problem

timing.py imports scipy and numba at module level:

import numba           # timing.py:8
from scipy import signal  # timing.py:10

These are loaded unconditionally via transcribe.py's top-level from .timing import add_word_timestamps, even when word_timestamps=False. This eagerly loads ~620 Python modules and ~212 MB of native libraries that are never used.

Fix

One-line change: move the import inside the if word_timestamps: conditional where it's actually needed.

- from .timing import add_word_timestamps

  if word_timestamps:
+     from .timing import add_word_timestamps
+
      add_word_timestamps(...)

Impact

  • Import time: import mlx_whisper no longer loads scipy/numba/llvmlite
  • Packaging: Downstream apps using PyInstaller/cx_Freeze can exclude scipy (~71 MB), numba (~31 MB), and llvmlite (~110 MB) from bundles when word timestamps aren't needed
  • Zero behavior change: When word_timestamps=True, the import fires on first use and everything works identically

Testing

  • word_timestamps=False (default): scipy/numba never imported — verified via sys.modules inspection
  • word_timestamps=True: import fires inside the conditional, all word timestamp functionality works as before

Move `from .timing import add_word_timestamps` from module-level to
inside the `if word_timestamps:` guard in transcribe().

timing.py imports scipy and numba at module level for word-level
timestamp alignment (DTW + median filter). These are heavy dependencies
(~212 MB combined) that load ~620 Python modules on import. When
word_timestamps=False (the default), none of this code is ever called,
yet it all gets loaded eagerly.

This change makes scipy and numba truly optional for the common case
of transcription without word timestamps, which:
- Reduces import time significantly
- Enables downstream packagers (PyInstaller, cx_Freeze) to exclude
  scipy/numba/llvmlite from bundles when word timestamps aren't needed
- Saves ~212 MB in frozen/packaged applications
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