fix(image): lazy-import torchvision in arniqa and dists modules#3373
Open
xodn348 wants to merge 1 commit intoLightning-AI:masterfrom
Open
fix(image): lazy-import torchvision in arniqa and dists modules#3373xodn348 wants to merge 1 commit intoLightning-AI:masterfrom
xodn348 wants to merge 1 commit intoLightning-AI:masterfrom
Conversation
…rcular import Module-level 'from torchvision import ...' blocks inside the 'if _TORCHVISION_AVAILABLE:' guard in arniqa.py and dists.py ran at import time, which triggered torchvision's own __init__ chain. In environments where torchvision is only partially initialised (e.g. Kaggle, or any scenario with a circular-import race in torchvision's _meta_registrations), importing torchmetrics raised an AttributeError before the user code ran. Move the torchvision symbols into the functions/methods that actually use them, matching the pattern already used by lpips.py.
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.
Summary
arniqa.pyanddists.pyimported symbols fromtorchvisionat module loadtime inside an
if _TORCHVISION_AVAILABLE:/else:guard. Because thatblock executes unconditionally when torchvision is installed, it triggered
torchvision's own
__init__chain the momenttorchmetrics.functional.imagewas imported. In environments where torchvision's initialisation races with
itself (e.g. Kaggle's GPU runtime, or any build where
torchvision._meta_registrationstries to importtorchvision.extensionbefore
torchvisionitself is fully loaded), the eager import raised anAttributeError: partially initialized module 'torchvision' has no attribute 'extension', breakingfrom torchmetrics.image.lpip import LearnedPerceptualImagePatchSimilarityand every other top-level import oftorchmetrics.The fix moves the
from torchvision import .../from torchvision.models import ...statements inside the class constructors and methods that actuallyuse them, matching the lazy-import pattern that
lpips.pyalready uses(
import torchvisioninside_get_tv_model_features). Theif not _TORCHVISION_AVAILABLE: raise ModuleNotFoundErrorguard already runs first, sobehaviour is unchanged when torchvision is absent.
Issue
Fixes #3314
Local verification
Risk
The change only affects import time: symbols that were previously imported once
at module load are now imported on first use inside their respective
constructor/method. Python caches modules in
sys.modules, so the runtimecost per call is negligible (a dict lookup). The
lpips.pymodule alreadyuses this pattern successfully, confirming it is safe within this code base.
Users who do not have torchvision installed will continue to see the existing
ModuleNotFoundErroron first instantiation.📚 Documentation preview 📚: https://torchmetrics--3373.org.readthedocs.build/en/3373/