Fix NameError: name 'nn' is not defined when PyTorch < 2.4#43822
Open
veeceey wants to merge 3 commits intohuggingface:mainfrom
Open
Fix NameError: name 'nn' is not defined when PyTorch < 2.4#43822veeceey wants to merge 3 commits intohuggingface:mainfrom
veeceey wants to merge 3 commits intohuggingface:mainfrom
Conversation
Author
|
Fixed the failing CI check. The issue was that a previous commit removed the null check for The fix restores the null check and properly falls back to
This ensures the code works correctly with or without the tokenizers library installed. |
The unconditional import of `from safetensors.torch import save_file` at the module level causes import failures when PyTorch is not available, as `safetensors.torch` depends on PyTorch being installed. This follows the pattern already used in other files: - `testing_utils.py`: imports safetensors.torch inside `if is_torch_available()` - `trainer_utils.py`: imports safetensors.torch inside `if is_torch_available()` The runtime usage of `save_file` (line 495) is only called from functions that use torch.Tensor, so it's safe to make this import conditional.
b69100f to
bba530f
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?
Fixes #43784
When PyTorch < 2.4 is installed, transformers v5.x disables PyTorch by making
is_torch_available()returnFalse. This causes the conditional import oftorch.nn as nn(line 42) to be skipped. However,nn.Moduleis used in type annotations throughoutaccelerate.py(function signatures at lines 62, 191, 527, 540, 597), which are evaluated at module import time, causing:The Fix
Add
from __future__ import annotations(PEP 563) to make all annotations lazily evaluated as string literals rather than being evaluated at import time. This meansnn.Modulein function signatures becomes a string and is never resolved during import.This follows the same pattern already used in sibling modules:
transformers/integrations/tensor_parallel.py(line 14)transformers/integrations/fsdp.pyThe runtime
isinstance()calls usingnn.Moduleandnn.Parameter(lines 745, 801) are unaffected because they are inside functions that are only called when PyTorch is available.Reproduction
As reported in the issue, this can be reproduced with PyTorch < 2.4 (e.g., 2.2.1):
Before submitting
tensor_parallel.py,fsdp.py)