fix: guard sys.modules access in _can_set_attn/experts_implementation#45015
Closed
pnehete23 wants to merge 1 commit intohuggingface:mainfrom
Closed
fix: guard sys.modules access in _can_set_attn/experts_implementation#45015pnehete23 wants to merge 1 commit intohuggingface:mainfrom
pnehete23 wants to merge 1 commit intohuggingface:mainfrom
Conversation
Use sys.modules.get() instead of sys.modules[] to prevent KeyError when a module is absent from sys.modules, which can happen with lazy loaders, frozen importers, or packaging tools that manipulate the module registry. The existing comment already acknowledged this class of scenario but the guard only ran after the dictionary access, so it never had a chance to fire. Fixes huggingface#45003 Made-with: Cursor
e57180f to
4a52f13
Compare
Member
|
This is clearly a code agent PR. Not reviewing it, sorry! |
This was referenced Mar 27, 2026
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 a
KeyErrorin_can_set_attn_implementationand_can_set_experts_implementationwhen a model's module is absent fromsys.modules.Fixes #45003
Root Cause
Both
_can_set_attn_implementation(line 1951) and_can_set_experts_implementation(line 1970) use a direct dictionary subscript:This raises
KeyErrorwhen the module is not registered insys.modules— which happens with lazy loaders, frozen importers, or tools like Griptape Nodes that manipulate the module registry.The existing comment on the very next line already acknowledges this class of scenario ("This can happen for a custom model in a jupyter notebook or repl"), but the
__file__guard only runs after the dictionary access, so it never fires.Fix
Replace
sys.modules[cls.__module__]withsys.modules.get(cls.__module__)and add aclass_module is Nonecheck alongside the existinghasattrguard. Both functions now gracefully returnFalseinstead of crashing.Tests
Added
TestCanSetImplementationWithMissingSysModulewith 3 tests:_can_set_attn_implementationreturnsFalsewhen module is missing fromsys.modules_can_set_experts_implementationreturnsFalsewhen module is missing fromsys.modulessys.modulesAll existing
TestAttentionImplementationtests continue to pass.Code Agent Policy