Fix: Import torch.nn for type hints even when PyTorch is disabled#43812
Open
tobyliu2004 wants to merge 1 commit intohuggingface:mainfrom
Open
Fix: Import torch.nn for type hints even when PyTorch is disabled#43812tobyliu2004 wants to merge 1 commit intohuggingface:mainfrom
tobyliu2004 wants to merge 1 commit intohuggingface:mainfrom
Conversation
- Fixes NameError when PyTorch < 2.4 is detected - nn.Module used in type hints on lines 155, 491, 504, 561 - Type hints are evaluated at import time, before runtime checks - Import nn in TYPE_CHECKING block to ensure it's always available for type hints Fixes huggingface#43784
Member
|
Hello! I'm not sure we'll want to go in this direction. One of my colleagues recently deprecated torch <2.4, and it does not make much sense to re-introduce compatibility for older versions. By requiring torch 2.4 or higher, we can start using newer functionality from it. I'd recommend either upgrading your I don't do a lot of maintenance on
|
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?
Fixes #43784
Fixes
NameError: name 'nn' is not definedwhen importing transformers with PyTorch < 2.4.The Issue
When PyTorch < 2.4 is detected, transformers disables PyTorch by making
is_torch_available()returnFalse. This causes the import oftorch.nn as nnto be skipped (line 42).However,
nn.Moduleis used in type hints throughout the file (lines 155, 491, 504, 561). Type hints are evaluated at module import time, before any runtime checks, causing:The Fix
Import
torch.nn as nnin theTYPE_CHECKINGblock to ensure it's always available for type hints, regardless of PyTorch version detection.This follows the standard pattern for making imports available to type checkers without affecting runtime behavior.
Testing
Reproduced the bug condition:
is_torch_available()to returnFalseVerified no regression:
Tested on macOS with Python 3.12 and PyTorch 2.10.
Before submitting
cc @ArthurZucker @Rocketknight1 @tomaarsen