Do not import scipy.signal at torchmetrics import time - #3459
Open
Kayvan-Zahiri wants to merge 1 commit into
Open
Do not import scipy.signal at torchmetrics import time#3459Kayvan-Zahiri wants to merge 1 commit into
Kayvan-Zahiri wants to merge 1 commit into
Conversation
Kayvan-Zahiri
requested review from
SkafteNicki and
justusschock
as code owners
August 16, 2026 23:04
4 tasks
`import torchmetrics` pulls in `scipy.signal` eagerly from a SRMRpy
back-compatibility shim duplicated across three `__init__.py` files. SRMRpy
patches around `scipy.signal.hamming` having moved to
`scipy.signal.windows.hamming`.
torchmetrics never imports SRMRpy: it appears only in
`requirements/audio_test.txt` and `tests/unittests/audio/test_srmr.py`. So the
shim belongs with the tests, not in the library's import path.
Moves it to `tests/unittests/audio/__init__.py`, which Python imports before
`test_srmr.py` and therefore before that module's `from srmrpy import ...`.
Measured with `python -X importtime -c "import torchmetrics"`, three runs each:
before 1085ms 1072ms 1095ms (scipy.signal eagerly imported)
after 652ms 657ms 655ms (scipy.signal not imported)
Part of Lightning-AI#3457.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Kayvan-Zahiri
force-pushed
the
perf/no-eager-scipy-signal
branch
from
August 24, 2026 18:35
41836a9 to
ccc62c0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Part of #3457, the
scipy.signalhalf of it.torchvisionis #3314 / #3432 andmatplotlibis untouched here.import torchmetricspulls inscipy.signaleagerly from a SRMRpy back-compatibility shim that is duplicated acrosstorchmetrics/__init__.py,torchmetrics/audio/__init__.pyandtorchmetrics/functional/audio/__init__.py. The shim exists because SRMRpy callsscipy.signal.hamming, which SciPy moved toscipy.signal.windows.hamming.torchmetrics never imports SRMRpy. It appears only in
requirements/audio_test.txtandtests/unittests/audio/test_srmr.py:So the shim belongs with the tests. It moves to
tests/unittests/audio/__init__.py, which Python imports beforetest_srmr.pyand therefore before that module'sfrom srmrpy import .... Verified:_SCIPI_AVAILABLEwas imported by the two audio__init__.pyfiles only for this block, so it goes with it.Measurement
python -X importtime -c "import torchmetrics", three runs each, warm cache:and
scipy.signalis no longer insys.modulesafterimport torchmetrics. Roughly 40% off, which is more thanscipy.signal's own subtree because its shared parents go too.torchmetrics.audioandtorchmetrics.functional.audiostill import and evaluate, andruff check/ruff format --checkare clean on the four touched files.