Skip to content

Fix: Import torch.nn for type hints even when PyTorch is disabled#43812

Open
tobyliu2004 wants to merge 1 commit intohuggingface:mainfrom
tobyliu2004:fix-nn-import-type-checking-43784
Open

Fix: Import torch.nn for type hints even when PyTorch is disabled#43812
tobyliu2004 wants to merge 1 commit intohuggingface:mainfrom
tobyliu2004:fix-nn-import-type-checking-43784

Conversation

@tobyliu2004
Copy link
Contributor

What does this PR do?

Fixes #43784

Fixes NameError: name 'nn' is not defined when importing transformers with PyTorch < 2.4.

The Issue

When PyTorch < 2.4 is detected, transformers disables PyTorch by making is_torch_available() return False. This causes the import of torch.nn as nn to be skipped (line 42).

However, nn.Module is 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:

NameError: name 'nn' is not defined

The Fix

Import torch.nn as nn in the TYPE_CHECKING block to ensure it's always available for type hints, regardless of PyTorch version detection.

if TYPE_CHECKING:
    import torch.nn as nn

if is_torch_available():
    import torch
    if not TYPE_CHECKING:  # avoid duplicate import
        import torch.nn as nn

This follows the standard pattern for making imports available to type checkers without affecting runtime behavior.

Testing

Reproduced the bug condition:

  • Monkey-patched is_torch_available() to return False
  • Verified import succeeds with the fix

Verified no regression:

  • Import works normally with PyTorch 2.10
  • All type hints remain valid

Tested on macOS with Python 3.12 and PyTorch 2.10.

Before submitting

  • This PR fixes a bug
  • Tested both bug condition and normal usage
  • Code follows existing patterns

cc @ArthurZucker @Rocketknight1 @tomaarsen

- 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
@tomaarsen
Copy link
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 torch, or sticking with a lower transformers version until you're ready to upgrade torch.

I don't do a lot of maintenance on transformers, though, so I can leave the final decision to one of my colleagues.

  • Tom Aarsen

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.

NameError: name 'nn' is not defined when importing sentence-transformers with latest transformers

2 participants