-
Notifications
You must be signed in to change notification settings - Fork 31.2k
Fix looping in torch guard decorator #42260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the guard_torch_init_functions decorator by replacing an expensive loop over all modules in sys.modules (which can be very large in pytest environments) with a targeted approach that only patches a specific list of known torch modules.
- Introduces
TORCH_MODULES_TO_PATCHconstant containing specific torch module paths to patch - Refactors
guard_torch_init_functions()to iterate over the predefined module list instead of allsys.modules - Renames variables in the patching logic for clarity (
name→func_name,module_namefor clarity)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/transformers/initialization.py | Adds TORCH_MODULES_TO_PATCH constant and refactors guard_torch_init_functions() to use targeted module patching instead of looping over all sys.modules |
| setup.py | Contains an accidental debug line that should be removed |
src/transformers/initialization.py
Outdated
| # Here, we need to check several modules imported, and hot patch all of them, as sometimes torch does | ||
| # something like `from torch.nn.init import xavier_uniform_` in their internals (e.g in torch.nn.modules.activations, | ||
| # where MultiHeadAttention lives), so the function name is binded at import time and just doing | ||
| # `setattr(torch.nn.init, name, gloabls()[name])` is thus not enough |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in comment: 'gloabls' should be 'globals'.
| # `setattr(torch.nn.init, name, gloabls()[name])` is thus not enough | |
| # `setattr(torch.nn.init, name, globals()[name])` is thus not enough |
setup.py
Outdated
| "tqdm", | ||
| ) | ||
|
|
||
| a = 1 |
Copilot
AI
Nov 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line a = 1 appears to be accidentally added debug code and should be removed. It serves no purpose in the setup file.
| a = 1 |
bf3444b to
3887a78
Compare
What does this PR do?
sys.modulescan end-up being quite large, especially when usingpytest. This avoids the loop while keeping the benefits